HGInventoryAccessModule.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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.Generic;
  29. using System.Reflection;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Client;
  32. using OpenSim.Region.Framework.Interfaces;
  33. using OpenSim.Region.Framework.Scenes;
  34. using OpenSim.Services.Connectors.Hypergrid;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Server.Base;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenMetaverse;
  39. using log4net;
  40. using Nini.Config;
  41. using Mono.Addins;
  42. namespace OpenSim.Region.CoreModules.Framework.InventoryAccess
  43. {
  44. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGInventoryAccessModule")]
  45. public class HGInventoryAccessModule : BasicInventoryAccessModule, INonSharedRegionModule, IInventoryAccessModule
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private static HGAssetMapper m_assMapper;
  49. public static HGAssetMapper AssetMapper
  50. {
  51. get { return m_assMapper; }
  52. }
  53. private string m_HomeURI;
  54. private bool m_OutboundPermission;
  55. private string m_ThisGatekeeper;
  56. private bool m_RestrictInventoryAccessAbroad;
  57. private bool m_bypassPermissions = true;
  58. // private bool m_Initialized = false;
  59. #region INonSharedRegionModule
  60. public override string Name
  61. {
  62. get { return "HGInventoryAccessModule"; }
  63. }
  64. public override void Initialise(IConfigSource source)
  65. {
  66. IConfig moduleConfig = source.Configs["Modules"];
  67. if (moduleConfig != null)
  68. {
  69. string name = moduleConfig.GetString("InventoryAccessModule", "");
  70. if (name == Name)
  71. {
  72. m_Enabled = true;
  73. InitialiseCommon(source);
  74. m_log.InfoFormat("[HG INVENTORY ACCESS MODULE]: {0} enabled.", Name);
  75. IConfig thisModuleConfig = source.Configs["HGInventoryAccessModule"];
  76. if (thisModuleConfig != null)
  77. {
  78. m_HomeURI = Util.GetConfigVarFromSections<string>(source, "HomeURI",
  79. new string[] { "Startup", "Hypergrid", "HGInventoryAccessModule" }, String.Empty);
  80. m_ThisGatekeeper = Util.GetConfigVarFromSections<string>(source, "GatekeeperURI",
  81. new string[] { "Startup", "Hypergrid", "HGInventoryAccessModule" }, String.Empty);
  82. // Legacy. Renove soon!
  83. m_ThisGatekeeper = thisModuleConfig.GetString("Gatekeeper", m_ThisGatekeeper);
  84. m_OutboundPermission = thisModuleConfig.GetBoolean("OutboundPermission", true);
  85. m_RestrictInventoryAccessAbroad = thisModuleConfig.GetBoolean("RestrictInventoryAccessAbroad", true);
  86. }
  87. else
  88. m_log.Warn("[HG INVENTORY ACCESS MODULE]: HGInventoryAccessModule configs not found. ProfileServerURI not set!");
  89. m_bypassPermissions = !Util.GetConfigVarFromSections<bool>(source, "serverside_object_permissions",
  90. new string[] { "Startup", "Permissions" }, true);
  91. }
  92. }
  93. }
  94. public override void AddRegion(Scene scene)
  95. {
  96. if (!m_Enabled)
  97. return;
  98. base.AddRegion(scene);
  99. m_assMapper = new HGAssetMapper(scene, m_HomeURI);
  100. scene.EventManager.OnNewInventoryItemUploadComplete += UploadInventoryItem;
  101. scene.EventManager.OnTeleportStart += TeleportStart;
  102. scene.EventManager.OnTeleportFail += TeleportFail;
  103. // We're fgoing to enforce some stricter permissions if Outbound is false
  104. scene.Permissions.OnTakeObject += CanTakeObject;
  105. scene.Permissions.OnTakeCopyObject += CanTakeObject;
  106. scene.Permissions.OnTransferUserInventory += OnTransferUserInventory;
  107. }
  108. #endregion
  109. #region Event handlers
  110. protected override void OnNewClient(IClientAPI client)
  111. {
  112. base.OnNewClient(client);
  113. client.OnCompleteMovementToRegion += new Action<IClientAPI, bool>(OnCompleteMovementToRegion);
  114. }
  115. protected void OnCompleteMovementToRegion(IClientAPI client, bool arg2)
  116. {
  117. //m_log.DebugFormat("[HG INVENTORY ACCESS MODULE]: OnCompleteMovementToRegion of user {0}", client.Name);
  118. object sp = null;
  119. if (client.Scene.TryGetScenePresence(client.AgentId, out sp))
  120. {
  121. if (sp is ScenePresence)
  122. {
  123. AgentCircuitData aCircuit = ((ScenePresence)sp).Scene.AuthenticateHandler.GetAgentCircuitData(client.AgentId);
  124. if (aCircuit != null && (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0)
  125. {
  126. if (m_RestrictInventoryAccessAbroad)
  127. {
  128. IUserManagement uMan = m_Scene.RequestModuleInterface<IUserManagement>();
  129. if (uMan.IsLocalGridUser(client.AgentId))
  130. ProcessInventoryForComingHome(client);
  131. else
  132. ProcessInventoryForArriving(client);
  133. }
  134. }
  135. }
  136. }
  137. }
  138. protected void TeleportStart(IClientAPI client, GridRegion destination, GridRegion finalDestination, uint teleportFlags, bool gridLogout)
  139. {
  140. if (gridLogout && m_RestrictInventoryAccessAbroad)
  141. {
  142. IUserManagement uMan = m_Scene.RequestModuleInterface<IUserManagement>();
  143. if (uMan != null && uMan.IsLocalGridUser(client.AgentId))
  144. {
  145. // local grid user
  146. ProcessInventoryForHypergriding(client);
  147. }
  148. else
  149. {
  150. // Foreigner
  151. ProcessInventoryForLeaving(client);
  152. }
  153. }
  154. }
  155. protected void TeleportFail(IClientAPI client, bool gridLogout)
  156. {
  157. if (gridLogout && m_RestrictInventoryAccessAbroad)
  158. {
  159. IUserManagement uMan = m_Scene.RequestModuleInterface<IUserManagement>();
  160. if (uMan.IsLocalGridUser(client.AgentId))
  161. {
  162. ProcessInventoryForComingHome(client);
  163. }
  164. else
  165. {
  166. ProcessInventoryForArriving(client);
  167. }
  168. }
  169. }
  170. public void UploadInventoryItem(UUID avatarID, AssetType type, UUID assetID, string name, int userlevel)
  171. {
  172. if (type == AssetType.Link)
  173. return;
  174. string userAssetServer = string.Empty;
  175. if (IsForeignUser(avatarID, out userAssetServer) && userAssetServer != string.Empty && m_OutboundPermission)
  176. {
  177. m_assMapper.Post(assetID, avatarID, userAssetServer);
  178. }
  179. }
  180. #endregion
  181. #region Overrides of Basic Inventory Access methods
  182. protected override string GenerateLandmark(ScenePresence presence, out string prefix, out string suffix)
  183. {
  184. if (UserManagementModule != null && !UserManagementModule.IsLocalGridUser(presence.UUID))
  185. prefix = "HG ";
  186. else
  187. prefix = string.Empty;
  188. suffix = " @ " + m_ThisGatekeeper;
  189. Vector3 pos = presence.AbsolutePosition;
  190. return String.Format("Landmark version 2\nregion_id {0}\nlocal_pos {1} {2} {3}\nregion_handle {4}\ngatekeeper {5}\n",
  191. presence.Scene.RegionInfo.RegionID,
  192. pos.X, pos.Y, pos.Z,
  193. presence.RegionHandle,
  194. m_ThisGatekeeper);
  195. }
  196. ///
  197. /// CapsUpdateInventoryItemAsset
  198. ///
  199. public override UUID CapsUpdateInventoryItemAsset(IClientAPI remoteClient, UUID itemID, byte[] data)
  200. {
  201. UUID newAssetID = base.CapsUpdateInventoryItemAsset(remoteClient, itemID, data);
  202. UploadInventoryItem(remoteClient.AgentId, AssetType.Unknown, newAssetID, "", 0);
  203. return newAssetID;
  204. }
  205. ///
  206. /// Used in DeleteToInventory
  207. ///
  208. protected override void ExportAsset(UUID agentID, UUID assetID)
  209. {
  210. if (!assetID.Equals(UUID.Zero))
  211. UploadInventoryItem(agentID, AssetType.Unknown, assetID, "", 0);
  212. else
  213. m_log.Debug("[HGScene]: Scene.Inventory did not create asset");
  214. }
  215. ///
  216. /// RezObject
  217. ///
  218. public override SceneObjectGroup RezObject(IClientAPI remoteClient, UUID itemID, Vector3 RayEnd, Vector3 RayStart,
  219. UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  220. bool RezSelected, bool RemoveItem, UUID fromTaskID, bool attachment)
  221. {
  222. m_log.DebugFormat("[HGScene]: RezObject itemID={0} fromTaskID={1}", itemID, fromTaskID);
  223. //if (fromTaskID.Equals(UUID.Zero))
  224. //{
  225. InventoryItemBase item = new InventoryItemBase(itemID);
  226. item.Owner = remoteClient.AgentId;
  227. item = m_Scene.InventoryService.GetItem(item);
  228. //if (item == null)
  229. //{ // Fetch the item
  230. // item = new InventoryItemBase();
  231. // item.Owner = remoteClient.AgentId;
  232. // item.ID = itemID;
  233. // item = m_assMapper.Get(item, userInfo.RootFolder.ID, userInfo);
  234. //}
  235. string userAssetServer = string.Empty;
  236. if (item != null && IsForeignUser(remoteClient.AgentId, out userAssetServer))
  237. {
  238. m_assMapper.Get(item.AssetID, remoteClient.AgentId, userAssetServer);
  239. }
  240. //}
  241. // OK, we're done fetching. Pass it up to the default RezObject
  242. SceneObjectGroup sog = base.RezObject(remoteClient, itemID, RayEnd, RayStart, RayTargetID, BypassRayCast, RayEndIsIntersection,
  243. RezSelected, RemoveItem, fromTaskID, attachment);
  244. if (sog == null)
  245. remoteClient.SendAgentAlertMessage("Unable to rez: problem accessing inventory or locating assets", false);
  246. return sog;
  247. }
  248. public override void TransferInventoryAssets(InventoryItemBase item, UUID sender, UUID receiver)
  249. {
  250. string userAssetServer = string.Empty;
  251. if (IsForeignUser(sender, out userAssetServer) && userAssetServer != string.Empty)
  252. m_assMapper.Get(item.AssetID, sender, userAssetServer);
  253. if (IsForeignUser(receiver, out userAssetServer) && userAssetServer != string.Empty && m_OutboundPermission)
  254. m_assMapper.Post(item.AssetID, receiver, userAssetServer);
  255. }
  256. public override bool IsForeignUser(UUID userID, out string assetServerURL)
  257. {
  258. assetServerURL = string.Empty;
  259. if (UserManagementModule != null && !UserManagementModule.IsLocalGridUser(userID))
  260. { // foreign
  261. ScenePresence sp = null;
  262. if (m_Scene.TryGetScenePresence(userID, out sp))
  263. {
  264. AgentCircuitData aCircuit = m_Scene.AuthenticateHandler.GetAgentCircuitData(sp.ControllingClient.CircuitCode);
  265. if (aCircuit != null && aCircuit.ServiceURLs != null && aCircuit.ServiceURLs.ContainsKey("AssetServerURI"))
  266. {
  267. assetServerURL = aCircuit.ServiceURLs["AssetServerURI"].ToString();
  268. assetServerURL = assetServerURL.Trim(new char[] { '/' });
  269. }
  270. }
  271. else
  272. {
  273. assetServerURL = UserManagementModule.GetUserServerURL(userID, "AssetServerURI");
  274. assetServerURL = assetServerURL.Trim(new char[] { '/' });
  275. }
  276. return true;
  277. }
  278. return false;
  279. }
  280. protected override InventoryItemBase GetItem(UUID agentID, UUID itemID)
  281. {
  282. InventoryItemBase item = base.GetItem(agentID, itemID);
  283. if (item == null)
  284. return null;
  285. string userAssetServer = string.Empty;
  286. if (IsForeignUser(agentID, out userAssetServer))
  287. m_assMapper.Get(item.AssetID, agentID, userAssetServer);
  288. return item;
  289. }
  290. #endregion
  291. #region Inventory manipulation upon arriving/leaving
  292. //
  293. // These 2 are for local and foreign users coming back, respectively
  294. //
  295. private void ProcessInventoryForComingHome(IClientAPI client)
  296. {
  297. m_log.DebugFormat("[HG INVENTORY ACCESS MODULE]: Restoring root folder for local user {0}", client.Name);
  298. if (client is IClientCore)
  299. {
  300. IClientCore core = (IClientCore)client;
  301. IClientInventory inv;
  302. if (core.TryGet<IClientInventory>(out inv))
  303. {
  304. InventoryFolderBase root = m_Scene.InventoryService.GetRootFolder(client.AgentId);
  305. InventoryCollection content = m_Scene.InventoryService.GetFolderContent(client.AgentId, root.ID);
  306. List<InventoryFolderBase> keep = new List<InventoryFolderBase>();
  307. foreach (InventoryFolderBase f in content.Folders)
  308. {
  309. if (f.Name != "My Suitcase" && f.Name != "Current Outfit")
  310. keep.Add(f);
  311. }
  312. inv.SendBulkUpdateInventory(keep.ToArray(), content.Items.ToArray());
  313. }
  314. }
  315. }
  316. private void ProcessInventoryForArriving(IClientAPI client)
  317. {
  318. // No-op for now, but we may need to do something for freign users inventory
  319. }
  320. //
  321. // These 2 are for local and foreign users going away respectively
  322. //
  323. private void ProcessInventoryForHypergriding(IClientAPI client)
  324. {
  325. if (client is IClientCore)
  326. {
  327. IClientCore core = (IClientCore)client;
  328. IClientInventory inv;
  329. if (core.TryGet<IClientInventory>(out inv))
  330. {
  331. InventoryFolderBase root = m_Scene.InventoryService.GetRootFolder(client.AgentId);
  332. if (root != null)
  333. {
  334. m_log.DebugFormat("[HG INVENTORY ACCESS MODULE]: Changing root inventory for user {0}", client.Name);
  335. InventoryCollection content = m_Scene.InventoryService.GetFolderContent(client.AgentId, root.ID);
  336. List<InventoryFolderBase> keep = new List<InventoryFolderBase>();
  337. foreach (InventoryFolderBase f in content.Folders)
  338. {
  339. if (f.Name != "My Suitcase" && f.Name != "Current Outfit")
  340. {
  341. f.Name = f.Name + " (Unavailable)";
  342. keep.Add(f);
  343. }
  344. }
  345. // items directly under the root folder
  346. foreach (InventoryItemBase it in content.Items)
  347. it.Name = it.Name + " (Unavailable)"; ;
  348. // Send the new names
  349. inv.SendBulkUpdateInventory(keep.ToArray(), content.Items.ToArray());
  350. }
  351. }
  352. }
  353. }
  354. private void ProcessInventoryForLeaving(IClientAPI client)
  355. {
  356. // No-op for now
  357. }
  358. #endregion
  359. #region Permissions
  360. private bool CanTakeObject(UUID objectID, UUID stealer, Scene scene)
  361. {
  362. if (m_bypassPermissions) return true;
  363. if (!m_OutboundPermission && !UserManagementModule.IsLocalGridUser(stealer))
  364. {
  365. SceneObjectGroup sog = null;
  366. if (m_Scene.TryGetSceneObjectGroup(objectID, out sog) && sog.OwnerID == stealer)
  367. return true;
  368. return false;
  369. }
  370. return true;
  371. }
  372. private bool OnTransferUserInventory(UUID itemID, UUID userID, UUID recipientID)
  373. {
  374. if (m_bypassPermissions) return true;
  375. if (!m_OutboundPermission && !UserManagementModule.IsLocalGridUser(recipientID))
  376. return false;
  377. return true;
  378. }
  379. #endregion
  380. }
  381. }