GridInventoryService.cs 9.9 KB

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