InventoryServerInConnector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using Nwc.XmlRpc;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Server.Handlers.Base;
  40. using OpenMetaverse;
  41. namespace OpenSim.Server.Handlers.Inventory
  42. {
  43. public class InventoryServiceInConnector : ServiceConnector
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private IInventoryService m_InventoryService;
  47. private bool m_doLookup = false;
  48. //private static readonly int INVENTORY_DEFAULT_SESSION_TIME = 30; // secs
  49. //private AuthedSessionCache m_session_cache = new AuthedSessionCache(INVENTORY_DEFAULT_SESSION_TIME);
  50. private string m_userserver_url;
  51. public InventoryServiceInConnector(IConfigSource config, IHttpServer server) :
  52. base(config, server)
  53. {
  54. IConfig serverConfig = config.Configs["InventoryService"];
  55. if (serverConfig == null)
  56. throw new Exception("No section 'InventoryService' in config file");
  57. string inventoryService = serverConfig.GetString("LocalServiceModule",
  58. String.Empty);
  59. if (inventoryService == String.Empty)
  60. throw new Exception("No InventoryService in config file");
  61. Object[] args = new Object[] { config };
  62. m_InventoryService =
  63. ServerUtils.LoadPlugin<IInventoryService>(inventoryService, args);
  64. m_userserver_url = serverConfig.GetString("UserServerURI", String.Empty);
  65. m_doLookup = serverConfig.GetBoolean("SessionAuthentication", false);
  66. AddHttpHandlers(server);
  67. }
  68. protected virtual void AddHttpHandlers(IHttpServer m_httpServer)
  69. {
  70. m_httpServer.AddStreamHandler(
  71. new RestDeserialiseSecureHandler<Guid, InventoryCollection>(
  72. "POST", "/GetInventory/", GetUserInventory, CheckAuthSession));
  73. m_httpServer.AddStreamHandler(
  74. new RestDeserialiseSecureHandler<Guid, List<InventoryFolderBase>>(
  75. "POST", "/SystemFolders/", GetSystemFolders, CheckAuthSession));
  76. m_httpServer.AddStreamHandler(
  77. new RestDeserialiseSecureHandler<Guid, InventoryCollection>(
  78. "POST", "/GetFolderContent/", GetFolderContent, CheckAuthSession));
  79. m_httpServer.AddStreamHandler(
  80. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  81. "POST", "/UpdateFolder/", m_InventoryService.UpdateFolder, CheckAuthSession));
  82. m_httpServer.AddStreamHandler(
  83. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  84. "POST", "/MoveFolder/", m_InventoryService.MoveFolder, CheckAuthSession));
  85. m_httpServer.AddStreamHandler(
  86. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  87. "POST", "/PurgeFolder/", m_InventoryService.PurgeFolder, CheckAuthSession));
  88. m_httpServer.AddStreamHandler(
  89. new RestDeserialiseSecureHandler<InventoryItemBase, bool>(
  90. "POST", "/DeleteItem/", m_InventoryService.DeleteItem, CheckAuthSession));
  91. m_httpServer.AddStreamHandler(
  92. new RestDeserialiseSecureHandler<InventoryItemBase, InventoryItemBase>(
  93. "POST", "/QueryItem/", m_InventoryService.QueryItem, CheckAuthSession));
  94. m_httpServer.AddStreamHandler(
  95. new RestDeserialiseSecureHandler<InventoryFolderBase, InventoryFolderBase>(
  96. "POST", "/QueryFolder/", m_InventoryService.QueryFolder, CheckAuthSession));
  97. m_httpServer.AddStreamHandler(
  98. new RestDeserialiseTrustedHandler<Guid, bool>(
  99. "POST", "/CreateInventory/", CreateUsersInventory, CheckTrustSource));
  100. m_httpServer.AddStreamHandler(
  101. new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
  102. "POST", "/NewFolder/", m_InventoryService.AddFolder, CheckAuthSession));
  103. m_httpServer.AddStreamHandler(
  104. new RestDeserialiseTrustedHandler<InventoryFolderBase, bool>(
  105. "POST", "/CreateFolder/", m_InventoryService.AddFolder, CheckTrustSource));
  106. m_httpServer.AddStreamHandler(
  107. new RestDeserialiseSecureHandler<InventoryItemBase, bool>(
  108. "POST", "/NewItem/", m_InventoryService.AddItem, CheckAuthSession));
  109. m_httpServer.AddStreamHandler(
  110. new RestDeserialiseTrustedHandler<InventoryItemBase, bool>(
  111. "POST", "/AddNewItem/", m_InventoryService.AddItem, CheckTrustSource));
  112. m_httpServer.AddStreamHandler(
  113. new RestDeserialiseTrustedHandler<Guid, List<InventoryItemBase>>(
  114. "POST", "/GetItems/", GetFolderItems, CheckTrustSource));
  115. // for persistent active gestures
  116. m_httpServer.AddStreamHandler(
  117. new RestDeserialiseTrustedHandler<Guid, List<InventoryItemBase>>
  118. ("POST", "/ActiveGestures/", GetActiveGestures, CheckTrustSource));
  119. // WARNING: Root folders no longer just delivers the root and immediate child folders (e.g
  120. // system folders such as Objects, Textures), but it now returns the entire inventory skeleton.
  121. // It would have been better to rename this request, but complexities in the BaseHttpServer
  122. // (e.g. any http request not found is automatically treated as an xmlrpc request) make it easier
  123. // to do this for now.
  124. m_httpServer.AddStreamHandler(
  125. new RestDeserialiseTrustedHandler<Guid, List<InventoryFolderBase>>
  126. ("POST", "/RootFolders/", GetInventorySkeleton, CheckTrustSource));
  127. }
  128. #region Wrappers for converting the Guid parameter
  129. public InventoryCollection GetUserInventory(Guid guid)
  130. {
  131. UUID userID = new UUID(guid);
  132. return m_InventoryService.GetUserInventory(userID);
  133. }
  134. public List<InventoryFolderBase> GetSystemFolders(Guid guid)
  135. {
  136. UUID userID = new UUID(guid);
  137. return new List<InventoryFolderBase>(GetSystemFolders(userID).Values);
  138. }
  139. // This shouldn't be here, it should be in the inventory service.
  140. // But I don't want to deal with types and dependencies for now.
  141. private Dictionary<AssetType, InventoryFolderBase> GetSystemFolders(UUID userID)
  142. {
  143. InventoryFolderBase root = m_InventoryService.GetRootFolder(userID);
  144. if (root != null)
  145. {
  146. InventoryCollection content = m_InventoryService.GetFolderContent(userID, root.ID);
  147. if (content != null)
  148. {
  149. Dictionary<AssetType, InventoryFolderBase> folders = new Dictionary<AssetType, InventoryFolderBase>();
  150. foreach (InventoryFolderBase folder in content.Folders)
  151. {
  152. if ((folder.Type != (short)AssetType.Folder) && (folder.Type != (short)AssetType.Unknown))
  153. folders[(AssetType)folder.Type] = folder;
  154. }
  155. return folders;
  156. }
  157. }
  158. m_log.WarnFormat("[INVENTORY SERVICE]: System folders for {0} not found", userID);
  159. return new Dictionary<AssetType, InventoryFolderBase>();
  160. }
  161. public InventoryCollection GetFolderContent(Guid guid)
  162. {
  163. return m_InventoryService.GetFolderContent(UUID.Zero, new UUID(guid));
  164. }
  165. public List<InventoryItemBase> GetFolderItems(Guid folderID)
  166. {
  167. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  168. // TODO: UUID.Zero is passed as the userID here, making the old assumption that the OpenSim
  169. // inventory server only has a single inventory database and not per-user inventory databases.
  170. // This could be changed but it requirs a bit of hackery to pass another parameter into this
  171. // callback
  172. List<InventoryItemBase> items = m_InventoryService.GetFolderItems(UUID.Zero, new UUID(folderID));
  173. if (items != null)
  174. {
  175. allItems.InsertRange(0, items);
  176. }
  177. return allItems;
  178. }
  179. public bool CreateUsersInventory(Guid rawUserID)
  180. {
  181. UUID userID = new UUID(rawUserID);
  182. return m_InventoryService.CreateUserInventory(userID);
  183. }
  184. public List<InventoryItemBase> GetActiveGestures(Guid rawUserID)
  185. {
  186. UUID userID = new UUID(rawUserID);
  187. return m_InventoryService.GetActiveGestures(userID);
  188. }
  189. public List<InventoryFolderBase> GetInventorySkeleton(Guid rawUserID)
  190. {
  191. UUID userID = new UUID(rawUserID);
  192. return m_InventoryService.GetInventorySkeleton(userID);
  193. }
  194. #endregion
  195. /// <summary>
  196. /// Check that the source of an inventory request is one that we trust.
  197. /// </summary>
  198. /// <param name="peer"></param>
  199. /// <returns></returns>
  200. public bool CheckTrustSource(IPEndPoint peer)
  201. {
  202. if (m_doLookup)
  203. {
  204. m_log.InfoFormat("[INVENTORY IN CONNECTOR]: Checking trusted source {0}", peer);
  205. UriBuilder ub = new UriBuilder(m_userserver_url);
  206. IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host);
  207. foreach (IPAddress uaddr in uaddrs)
  208. {
  209. if (uaddr.Equals(peer.Address))
  210. {
  211. return true;
  212. }
  213. }
  214. m_log.WarnFormat(
  215. "[INVENTORY IN CONNECTOR]: Rejecting request since source {0} was not in the list of trusted sources",
  216. peer);
  217. return false;
  218. }
  219. else
  220. {
  221. return true;
  222. }
  223. }
  224. /// <summary>
  225. /// Check that the source of an inventory request for a particular agent is a current session belonging to
  226. /// that agent.
  227. /// </summary>
  228. /// <param name="session_id"></param>
  229. /// <param name="avatar_id"></param>
  230. /// <returns></returns>
  231. public bool CheckAuthSession(string session_id, string avatar_id)
  232. {
  233. if (m_doLookup)
  234. {
  235. m_log.InfoFormat("[INVENTORY IN CONNECTOR]: checking authed session {0} {1}", session_id, avatar_id);
  236. //if (m_session_cache.getCachedSession(session_id, avatar_id) == null)
  237. //{
  238. // cache miss, ask userserver
  239. Hashtable requestData = new Hashtable();
  240. requestData["avatar_uuid"] = avatar_id;
  241. requestData["session_id"] = session_id;
  242. ArrayList SendParams = new ArrayList();
  243. SendParams.Add(requestData);
  244. XmlRpcRequest UserReq = new XmlRpcRequest("check_auth_session", SendParams);
  245. XmlRpcResponse UserResp = UserReq.Send(m_userserver_url, 3000);
  246. Hashtable responseData = (Hashtable)UserResp.Value;
  247. if (responseData.ContainsKey("auth_session") && responseData["auth_session"].ToString() == "TRUE")
  248. {
  249. m_log.Info("[INVENTORY IN CONNECTOR]: got authed session from userserver");
  250. //// add to cache; the session time will be automatically renewed
  251. //m_session_cache.Add(session_id, avatar_id);
  252. return true;
  253. }
  254. //}
  255. //else
  256. //{
  257. // // cache hits
  258. // m_log.Info("[GRID AGENT INVENTORY]: got authed session from cache");
  259. // return true;
  260. //}
  261. m_log.Warn("[INVENTORY IN CONNECTOR]: unknown session_id, request rejected");
  262. return false;
  263. }
  264. else
  265. {
  266. return true;
  267. }
  268. }
  269. }
  270. }