GridInventoryService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /// <summary>
  134. /// Return a user's entire inventory
  135. /// </summary>
  136. /// <param name="rawUserID"></param>
  137. /// <returns>The user's inventory. If an inventory cannot be found then an empty collection is returned.</returns>
  138. public InventoryCollection GetUserInventory(Guid rawUserID)
  139. {
  140. UUID userID = new UUID(rawUserID);
  141. m_log.InfoFormat("[GRID AGENT INVENTORY]: Processing request for inventory of {0}", userID);
  142. // Uncomment me to simulate a slow responding inventory server
  143. //Thread.Sleep(16000);
  144. InventoryCollection invCollection = new InventoryCollection();
  145. List<InventoryFolderBase> allFolders = GetInventorySkeleton(userID);
  146. if (null == allFolders)
  147. {
  148. m_log.WarnFormat("[GRID AGENT INVENTORY]: No inventory found for user {0}", rawUserID);
  149. return invCollection;
  150. }
  151. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  152. foreach (InventoryFolderBase folder in allFolders)
  153. {
  154. List<InventoryItemBase> items = RequestFolderItems(folder.ID);
  155. if (items != null)
  156. {
  157. allItems.InsertRange(0, items);
  158. }
  159. }
  160. invCollection.UserID = userID;
  161. invCollection.Folders = allFolders;
  162. invCollection.Items = allItems;
  163. // foreach (InventoryFolderBase folder in invCollection.Folders)
  164. // {
  165. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back folder {0} {1}", folder.Name, folder.ID);
  166. // }
  167. //
  168. // foreach (InventoryItemBase item in invCollection.Items)
  169. // {
  170. // m_log.DebugFormat("[GRID AGENT INVENTORY]: Sending back item {0} {1}, folder {2}", item.Name, item.ID, item.Folder);
  171. // }
  172. m_log.InfoFormat(
  173. "[GRID AGENT INVENTORY]: Sending back inventory response to user {0} containing {1} folders and {2} items",
  174. invCollection.UserID, invCollection.Folders.Count, invCollection.Items.Count);
  175. return invCollection;
  176. }
  177. public List<InventoryItemBase> GetFolderItems(Guid folderID)
  178. {
  179. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  180. List<InventoryItemBase> items = RequestFolderItems(new UUID(folderID));
  181. if (items != null)
  182. {
  183. allItems.InsertRange(0, items);
  184. }
  185. m_log.InfoFormat(
  186. "[GRID AGENT INVENTORY]: Sending back inventory response containing {0} items", allItems.Count.ToString());
  187. return allItems;
  188. }
  189. /// <summary>
  190. /// Guid to UUID wrapper for same name IInventoryServices method
  191. /// </summary>
  192. /// <param name="rawUserID"></param>
  193. /// <returns></returns>
  194. public List<InventoryFolderBase> GetInventorySkeleton(Guid rawUserID)
  195. {
  196. UUID userID = new UUID(rawUserID);
  197. return GetInventorySkeleton(userID);
  198. }
  199. /// <summary>
  200. /// Create an inventory for the given user.
  201. /// </summary>
  202. /// <param name="rawUserID"></param>
  203. /// <returns></returns>
  204. public bool CreateUsersInventory(Guid rawUserID)
  205. {
  206. UUID userID = new UUID(rawUserID);
  207. m_log.InfoFormat("[GRID AGENT INVENTORY]: Creating new set of inventory folders for user {0}", userID);
  208. return CreateNewUserInventory(userID);
  209. }
  210. public List<InventoryItemBase> GetActiveGestures(Guid rawUserID)
  211. {
  212. UUID userID = new UUID(rawUserID);
  213. m_log.InfoFormat("[GRID AGENT INVENTORY]: fetching active gestures for user {0}", userID);
  214. return GetActiveGestures(userID);
  215. }
  216. }
  217. }