OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
InventoryServerInConnector.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Net;
32 using System.Reflection;
33 using log4net;
34 using Nini.Config;
35 using Nwc.XmlRpc;
36 using OpenSim.Server.Base;
37 using OpenSim.Services.Interfaces;
38 using OpenSim.Framework;
39 using OpenSim.Framework.Servers.HttpServer;
40 using OpenSim.Server.Handlers.Base;
41 using OpenMetaverse;
42 
43 namespace OpenSim.Server.Handlers.Inventory
44 {
46  {
47  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 
50 
51  private bool m_doLookup = false;
52 
53  //private static readonly int INVENTORY_DEFAULT_SESSION_TIME = 30; // secs
54  //private AuthedSessionCache m_session_cache = new AuthedSessionCache(INVENTORY_DEFAULT_SESSION_TIME);
55 
56  private string m_userserver_url;
57  protected string m_ConfigName = "InventoryService";
58 
59  public InventoryServiceInConnector(IConfigSource config, IHttpServer server, string configName) :
60  base(config, server, configName)
61  {
62  if (configName != string.Empty)
63  m_ConfigName = configName;
64 
65  IConfig serverConfig = config.Configs[m_ConfigName];
66  if (serverConfig == null)
67  throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName));
68 
69  string inventoryService = serverConfig.GetString("LocalServiceModule",
70  String.Empty);
71 
72  if (inventoryService == String.Empty)
73  throw new Exception("No LocalServiceModule in config file");
74 
75  Object[] args = new Object[] { config };
76  m_InventoryService =
77  ServerUtils.LoadPlugin<IInventoryService>(inventoryService, args);
78 
79  m_userserver_url = serverConfig.GetString("UserServerURI", String.Empty);
80  m_doLookup = serverConfig.GetBoolean("SessionAuthentication", false);
81 
82  AddHttpHandlers(server);
83  m_log.Debug("[INVENTORY HANDLER]: handlers initialized");
84  }
85 
86  protected virtual void AddHttpHandlers(IHttpServer m_httpServer)
87  {
88  m_httpServer.AddStreamHandler(
89  new RestDeserialiseSecureHandler<Guid, List<InventoryFolderBase>>(
90  "POST", "/SystemFolders/", GetSystemFolders, CheckAuthSession));
91 
92  m_httpServer.AddStreamHandler(
93  new RestDeserialiseSecureHandler<Guid, InventoryCollection>(
94  "POST", "/GetFolderContent/", GetFolderContent, CheckAuthSession));
95 
96  m_httpServer.AddStreamHandler(
97  new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
98  "POST", "/UpdateFolder/", m_InventoryService.UpdateFolder, CheckAuthSession));
99 
100  m_httpServer.AddStreamHandler(
101  new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
102  "POST", "/MoveFolder/", m_InventoryService.MoveFolder, CheckAuthSession));
103 
104  m_httpServer.AddStreamHandler(
105  new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
106  "POST", "/PurgeFolder/", m_InventoryService.PurgeFolder, CheckAuthSession));
107 
108  m_httpServer.AddStreamHandler(
109  new RestDeserialiseSecureHandler<List<Guid>, bool>(
110  "POST", "/DeleteFolders/", DeleteFolders, CheckAuthSession));
111 
112  m_httpServer.AddStreamHandler(
113  new RestDeserialiseSecureHandler<List<Guid>, bool>(
114  "POST", "/DeleteItem/", DeleteItems, CheckAuthSession));
115 
116  m_httpServer.AddStreamHandler(
117  new RestDeserialiseSecureHandler<InventoryItemBase, InventoryItemBase>(
118  "POST", "/QueryItem/", m_InventoryService.GetItem, CheckAuthSession));
119 
120  m_httpServer.AddStreamHandler(
121  new RestDeserialiseSecureHandler<InventoryFolderBase, InventoryFolderBase>(
122  "POST", "/QueryFolder/", m_InventoryService.GetFolder, CheckAuthSession));
123 
124  m_httpServer.AddStreamHandler(
125  new RestDeserialiseTrustedHandler<Guid, bool>(
126  "POST", "/CreateInventory/", CreateUsersInventory, CheckTrustSource));
127 
128  m_httpServer.AddStreamHandler(
129  new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
130  "POST", "/NewFolder/", m_InventoryService.AddFolder, CheckAuthSession));
131 
132  m_httpServer.AddStreamHandler(
133  new RestDeserialiseSecureHandler<InventoryFolderBase, bool>(
134  "POST", "/CreateFolder/", m_InventoryService.AddFolder, CheckAuthSession));
135 
136  m_httpServer.AddStreamHandler(
137  new RestDeserialiseSecureHandler<InventoryItemBase, bool>(
138  "POST", "/NewItem/", m_InventoryService.AddItem, CheckAuthSession));
139 
140  m_httpServer.AddStreamHandler(
141  new RestDeserialiseTrustedHandler<InventoryItemBase, bool>(
142  "POST", "/AddNewItem/", m_InventoryService.AddItem, CheckTrustSource));
143 
144  m_httpServer.AddStreamHandler(
145  new RestDeserialiseSecureHandler<Guid, List<InventoryItemBase>>(
146  "POST", "/GetItems/", GetFolderItems, CheckAuthSession));
147 
148  m_httpServer.AddStreamHandler(
149  new RestDeserialiseSecureHandler<List<InventoryItemBase>, bool>(
150  "POST", "/MoveItems/", MoveItems, CheckAuthSession));
151 
152  m_httpServer.AddStreamHandler(new InventoryServerMoveItemsHandler(m_InventoryService));
153 
154 
155  // for persistent active gestures
156  m_httpServer.AddStreamHandler(
157  new RestDeserialiseTrustedHandler<Guid, List<InventoryItemBase>>
158  ("POST", "/ActiveGestures/", GetActiveGestures, CheckTrustSource));
159 
160  // WARNING: Root folders no longer just delivers the root and immediate child folders (e.g
161  // system folders such as Objects, Textures), but it now returns the entire inventory skeleton.
162  // It would have been better to rename this request, but complexities in the BaseHttpServer
163  // (e.g. any http request not found is automatically treated as an xmlrpc request) make it easier
164  // to do this for now.
165  m_httpServer.AddStreamHandler(
166  new RestDeserialiseTrustedHandler<Guid, List<InventoryFolderBase>>
167  ("POST", "/RootFolders/", GetInventorySkeleton, CheckTrustSource));
168 
169  m_httpServer.AddStreamHandler(
170  new RestDeserialiseTrustedHandler<InventoryItemBase, int>
171  ("POST", "/AssetPermissions/", GetAssetPermissions, CheckTrustSource));
172 
173  }
174 
175  #region Wrappers for converting the Guid parameter
176 
177  public List<InventoryFolderBase> GetSystemFolders(Guid guid)
178  {
179  UUID userID = new UUID(guid);
180  return new List<InventoryFolderBase>(GetSystemFolders(userID).Values);
181  }
182 
183  // This shouldn't be here, it should be in the inventory service.
184  // But I don't want to deal with types and dependencies for now.
185  private Dictionary<AssetType, InventoryFolderBase> GetSystemFolders(UUID userID)
186  {
187  InventoryFolderBase root = m_InventoryService.GetRootFolder(userID);
188  if (root != null)
189  {
190  InventoryCollection content = m_InventoryService.GetFolderContent(userID, root.ID);
191  if (content != null)
192  {
193  Dictionary<AssetType, InventoryFolderBase> folders = new Dictionary<AssetType, InventoryFolderBase>();
194  foreach (InventoryFolderBase folder in content.Folders)
195  {
196  if ((folder.Type != (short)AssetType.Folder) && (folder.Type != (short)AssetType.Unknown))
197  folders[(AssetType)folder.Type] = folder;
198  }
199  // Put the root folder there, as type Folder
200  folders[AssetType.Folder] = root;
201  return folders;
202  }
203  }
204  m_log.WarnFormat("[INVENTORY SERVICE]: System folders for {0} not found", userID);
205  return new Dictionary<AssetType, InventoryFolderBase>();
206  }
207 
209  {
210  return m_InventoryService.GetFolderContent(UUID.Zero, new UUID(guid));
211  }
212 
213  public List<InventoryItemBase> GetFolderItems(Guid folderID)
214  {
215  List<InventoryItemBase> allItems = new List<InventoryItemBase>();
216 
217  // TODO: UUID.Zero is passed as the userID here, making the old assumption that the OpenSim
218  // inventory server only has a single inventory database and not per-user inventory databases.
219  // This could be changed but it requirs a bit of hackery to pass another parameter into this
220  // callback
221  List<InventoryItemBase> items = m_InventoryService.GetFolderItems(UUID.Zero, new UUID(folderID));
222 
223  if (items != null)
224  {
225  allItems.InsertRange(0, items);
226  }
227  return allItems;
228  }
229 
230  public bool CreateUsersInventory(Guid rawUserID)
231  {
232  UUID userID = new UUID(rawUserID);
233 
234 
235  return m_InventoryService.CreateUserInventory(userID);
236  }
237 
238  public List<InventoryItemBase> GetActiveGestures(Guid rawUserID)
239  {
240  UUID userID = new UUID(rawUserID);
241 
242  return m_InventoryService.GetActiveGestures(userID);
243  }
244 
245  public List<InventoryFolderBase> GetInventorySkeleton(Guid rawUserID)
246  {
247  UUID userID = new UUID(rawUserID);
248  return m_InventoryService.GetInventorySkeleton(userID);
249  }
250 
252  {
253  return m_InventoryService.GetAssetPermissions(item.Owner, item.AssetID);
254  }
255 
256  public bool DeleteFolders(List<Guid> items)
257  {
258  List<UUID> uuids = new List<UUID>();
259  foreach (Guid g in items)
260  uuids.Add(new UUID(g));
261  // oops we lost the user info here. Bad bad handlers
262  return m_InventoryService.DeleteFolders(UUID.Zero, uuids);
263  }
264 
265  public bool DeleteItems(List<Guid> items)
266  {
267  List<UUID> uuids = new List<UUID>();
268  foreach (Guid g in items)
269  uuids.Add(new UUID(g));
270  // oops we lost the user info here. Bad bad handlers
271  return m_InventoryService.DeleteItems(UUID.Zero, uuids);
272  }
273 
274  public bool MoveItems(List<InventoryItemBase> items)
275  {
276  // oops we lost the user info here. Bad bad handlers
277  // let's peek at one item
278  UUID ownerID = UUID.Zero;
279  if (items.Count > 0)
280  ownerID = items[0].Owner;
281  return m_InventoryService.MoveItems(ownerID, items);
282  }
283  #endregion
284 
290  public bool CheckTrustSource(IPEndPoint peer)
291  {
292  if (m_doLookup)
293  {
294  m_log.InfoFormat("[INVENTORY IN CONNECTOR]: Checking trusted source {0}", peer);
295  UriBuilder ub = new UriBuilder(m_userserver_url);
296  IPAddress[] uaddrs = Dns.GetHostAddresses(ub.Host);
297  foreach (IPAddress uaddr in uaddrs)
298  {
299  if (uaddr.Equals(peer.Address))
300  {
301  return true;
302  }
303  }
304 
305  m_log.WarnFormat(
306  "[INVENTORY IN CONNECTOR]: Rejecting request since source {0} was not in the list of trusted sources",
307  peer);
308 
309  return false;
310  }
311  else
312  {
313  return true;
314  }
315  }
316 
324  public virtual bool CheckAuthSession(string session_id, string avatar_id)
325  {
326  return true;
327  }
328 
329  }
330 }
bool CheckTrustSource(IPEndPoint peer)
Check that the source of an inventory request is one that we trust.
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
Definition: IHttpServer.cs:36
virtual bool CheckAuthSession(string session_id, string avatar_id)
Check that the source of an inventory request for a particular agent is a current session belonging t...
Inventory Item - contains all the properties associated with an individual inventory piece...
Interactive OpenSim region server
Definition: OpenSim.cs:55
List< InventoryFolderBase > Folders
InventoryServiceInConnector(IConfigSource config, IHttpServer server, string configName)
Used to serialize a whole inventory for transfer over the network.