GridInventoryService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 Nwc.XmlRpc;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications;
  37. using OpenSim.Framework.Communications.Cache;
  38. namespace OpenSim.Grid.InventoryServer
  39. {
  40. /// <summary>
  41. /// Used on a grid server to satisfy external inventory requests
  42. /// </summary>
  43. public class GridInventoryService : InventoryServiceBase
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private bool m_doLookup = false;
  47. public bool DoLookup
  48. {
  49. get { return m_doLookup; }
  50. set { m_doLookup = value; }
  51. }
  52. private static readonly int INVENTORY_DEFAULT_SESSION_TIME = 30; // secs
  53. private string m_userserver_url;
  54. private AuthedSessionCache m_session_cache = new AuthedSessionCache(INVENTORY_DEFAULT_SESSION_TIME);
  55. public GridInventoryService(string userserver_url)
  56. {
  57. m_userserver_url = userserver_url;
  58. }
  59. /// <summary>
  60. /// Check that the source of an inventory request is one that we trust.
  61. /// </summary>
  62. /// <param name="peer"></param>
  63. /// <returns></returns>
  64. public bool CheckTrustSource(IPEndPoint peer)
  65. {
  66. if (m_doLookup)
  67. {
  68. m_log.InfoFormat("[GRID AGENT INVENTORY]: Checking trusted source {0}", peer);
  69. UriBuilder ub = new UriBuilder(m_userserver_url);
  70. IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host);
  71. foreach (IPAddress uaddr in uaddrs)
  72. {
  73. if (uaddr.Equals(peer.Address))
  74. {
  75. return true;
  76. }
  77. }
  78. m_log.WarnFormat(
  79. "[GRID AGENT INVENTORY]: Rejecting request since source {0} was not in the list of trusted sources",
  80. peer);
  81. return false;
  82. }
  83. else
  84. {
  85. return true;
  86. }
  87. }
  88. /// <summary>
  89. /// Check that the source of an inventory request for a particular agent is a current session belonging to
  90. /// that agent.
  91. /// </summary>
  92. /// <param name="session_id"></param>
  93. /// <param name="avatar_id"></param>
  94. /// <returns></returns>
  95. public bool CheckAuthSession(string session_id, string avatar_id)
  96. {
  97. if (m_doLookup)
  98. {
  99. m_log.InfoFormat("[GRID AGENT INVENTORY]: checking authed session {0} {1}", session_id, avatar_id);
  100. if (m_session_cache.getCachedSession(session_id, avatar_id) == null)
  101. {
  102. // cache miss, ask userserver
  103. Hashtable requestData = new Hashtable();
  104. requestData["avatar_uuid"] = avatar_id;
  105. requestData["session_id"] = session_id;
  106. ArrayList SendParams = new ArrayList();
  107. SendParams.Add(requestData);
  108. XmlRpcRequest UserReq = new XmlRpcRequest("check_auth_session", SendParams);
  109. XmlRpcResponse UserResp = UserReq.Send(m_userserver_url, 3000);
  110. Hashtable responseData = (Hashtable)UserResp.Value;
  111. if (responseData.ContainsKey("auth_session") && responseData["auth_session"].ToString() == "TRUE")
  112. {
  113. m_log.Info("[GRID AGENT INVENTORY]: got authed session from userserver");
  114. // add to cache; the session time will be automatically renewed
  115. m_session_cache.Add(session_id, avatar_id);
  116. return true;
  117. }
  118. }
  119. else
  120. {
  121. // cache hits
  122. m_log.Info("[GRID AGENT INVENTORY]: got authed session from cache");
  123. return true;
  124. }
  125. m_log.Warn("[GRID AGENT INVENTORY]: unknown session_id, request rejected");
  126. return false;
  127. }
  128. else
  129. {
  130. return true;
  131. }
  132. }
  133. public override void RequestInventoryForUser(UUID userID, InventoryReceiptCallback callback)
  134. {
  135. }
  136. /// <summary>
  137. /// Return a user's entire inventory
  138. /// </summary>
  139. /// <param name="rawUserID"></param>
  140. /// <returns>The user's inventory. If an inventory cannot be found then an empty collection is returned.</returns>
  141. public InventoryCollection GetUserInventory(Guid rawUserID)
  142. {
  143. UUID userID = new UUID(rawUserID);
  144. m_log.InfoFormat("[GRID AGENT INVENTORY]: Processing request for inventory of {0}", userID);
  145. // Uncomment me to simulate a slow responding inventory server
  146. //Thread.Sleep(16000);
  147. InventoryCollection invCollection = new InventoryCollection();
  148. List<InventoryFolderBase> allFolders = GetInventorySkeleton(userID);
  149. if (null == allFolders)
  150. {
  151. m_log.WarnFormat("[GRID AGENT INVENTORY]: No inventory found for user {0}", rawUserID);
  152. return invCollection;
  153. }
  154. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  155. foreach (InventoryFolderBase folder in allFolders)
  156. {
  157. List<InventoryItemBase> items = RequestFolderItems(folder.ID);
  158. if (items != null)
  159. {
  160. allItems.InsertRange(0, items);
  161. }
  162. }
  163. invCollection.UserID = userID;
  164. invCollection.Folders = allFolders;
  165. invCollection.Items = allItems;
  166. // foreach (InventoryFolderBase folder in invCollection.Folders)
  167. // {
  168. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back folder {0} {1}", folder.Name, folder.ID);
  169. // }
  170. //
  171. // foreach (InventoryItemBase item in invCollection.Items)
  172. // {
  173. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back item {0} {1}, folder {2}", item.Name, item.ID, item.Folder);
  174. // }
  175. m_log.InfoFormat(
  176. "[GRID AGENT INVENTORY]: Sending back inventory response to user {0} containing {1} folders and {2} items",
  177. invCollection.UserID, invCollection.Folders.Count, invCollection.Items.Count);
  178. return invCollection;
  179. }
  180. public List<InventoryItemBase> GetFolderItems(Guid folderID)
  181. {
  182. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  183. List<InventoryItemBase> items = RequestFolderItems(new UUID(folderID));
  184. if (items != null)
  185. {
  186. allItems.InsertRange(0, items);
  187. }
  188. m_log.InfoFormat(
  189. "[GRID AGENT INVENTORY]: Sending back inventory response containing {0} items", allItems.Count.ToString());
  190. return allItems;
  191. }
  192. /// <summary>
  193. /// Guid to UUID wrapper for same name IInventoryServices method
  194. /// </summary>
  195. /// <param name="rawUserID"></param>
  196. /// <returns></returns>
  197. public List<InventoryFolderBase> GetInventorySkeleton(Guid rawUserID)
  198. {
  199. UUID userID = new UUID(rawUserID);
  200. return GetInventorySkeleton(userID);
  201. }
  202. /// <summary>
  203. /// Create an inventory for the given user.
  204. /// </summary>
  205. /// <param name="rawUserID"></param>
  206. /// <returns></returns>
  207. public bool CreateUsersInventory(Guid rawUserID)
  208. {
  209. UUID userID = new UUID(rawUserID);
  210. m_log.InfoFormat("[GRID AGENT INVENTORY]: Creating new set of inventory folders for user {0}", userID);
  211. return CreateNewUserInventory(userID);
  212. }
  213. public List<InventoryItemBase> GetActiveGestures(Guid rawUserID)
  214. {
  215. UUID userID = new UUID(rawUserID);
  216. m_log.InfoFormat("[GRID AGENT INVENTORY]: fetching active gestures for user {0}", userID);
  217. return GetActiveGestures(userID);
  218. }
  219. }
  220. }