UserLoginService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.Threading;
  31. using libsecondlife;
  32. using Nwc.XmlRpc;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Data;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Statistics;
  39. using OpenSim.Framework.UserManagement;
  40. using InventoryFolder=OpenSim.Framework.InventoryFolder;
  41. namespace OpenSim.Grid.UserServer
  42. {
  43. public delegate void UserLoggedInAtLocation(LLUUID agentID, LLUUID sessionID, LLUUID RegionID,
  44. ulong regionhandle, float positionX, float positionY, float positionZ, string firstname, string lastname);
  45. public class UserLoginService : LoginService
  46. {
  47. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  48. public event UserLoggedInAtLocation OnUserLoggedInAtLocation;
  49. private UserLoggedInAtLocation handlerUserLoggedInAtLocation = null;
  50. public UserConfig m_config;
  51. public UserLoginService(
  52. UserManagerBase userManager, LibraryRootFolder libraryRootFolder,
  53. UserConfig config, string welcomeMess)
  54. : base(userManager, libraryRootFolder, welcomeMess)
  55. {
  56. m_config = config;
  57. }
  58. /// <summary>
  59. /// Customises the login response and fills in missing values.
  60. /// </summary>
  61. /// <param name="response">The existing response</param>
  62. /// <param name="theUser">The user profile</param>
  63. public override void CustomiseResponse(LoginResponse response, UserProfileData theUser, string startLocationRequest)
  64. {
  65. bool tryDefault = false;
  66. //CFK: Since the try is always "tried", the "Home Location" message should always appear, so comment this one.
  67. //CFK: m_log.Info("[LOGIN]: Load information from the gridserver");
  68. try
  69. {
  70. RegionProfileData SimInfo = null;
  71. if (startLocationRequest == "last")
  72. {
  73. SimInfo =
  74. RegionProfileData.RequestSimProfileData(
  75. theUser.currentAgent.currentHandle, m_config.GridServerURL,
  76. m_config.GridSendKey, m_config.GridRecvKey);
  77. }
  78. else if (startLocationRequest == "home")
  79. {
  80. SimInfo =
  81. RegionProfileData.RequestSimProfileData(
  82. theUser.homeRegion, m_config.GridServerURL,
  83. m_config.GridSendKey, m_config.GridRecvKey);
  84. }
  85. else
  86. {
  87. string[] startLocationRequestParsed = Util.ParseStartLocationRequest(startLocationRequest);
  88. m_log.Info("[DEBUGLOGINPARSE]: 1:" + startLocationRequestParsed[0] + ", 2:" + startLocationRequestParsed[1] + ", 3:" + startLocationRequestParsed[2] + ", 4:" + startLocationRequestParsed[3]);
  89. if (startLocationRequestParsed[0] == "last")
  90. {
  91. // TODO: Parse out startlocationrequest string in the format; 'uri:RegionName&X&Y&Z'
  92. SimInfo =
  93. RegionProfileData.RequestSimProfileData(
  94. theUser.currentAgent.currentHandle, m_config.GridServerURL,
  95. m_config.GridSendKey, m_config.GridRecvKey);
  96. }
  97. else
  98. {
  99. m_log.Info("[LOGIN]: Looking up Sim: " + startLocationRequestParsed[0]);
  100. SimInfo =
  101. RegionProfileData.RequestSimProfileData(
  102. startLocationRequestParsed[0], m_config.GridServerURL,
  103. m_config.GridSendKey, m_config.GridRecvKey);
  104. if (SimInfo == null)
  105. {
  106. m_log.Info("[LOGIN]: Didn't find region with a close name match sending to home location");
  107. SimInfo =
  108. RegionProfileData.RequestSimProfileData(
  109. theUser.homeRegion, m_config.GridServerURL,
  110. m_config.GridSendKey, m_config.GridRecvKey);
  111. }
  112. }
  113. }
  114. // Customise the response
  115. //CFK: This is redundant and the next message should always appear.
  116. //CFK: m_log.Info("[LOGIN]: Home Location");
  117. response.Home = "{'region_handle':[r" + (SimInfo.regionLocX * Constants.RegionSize).ToString() + ",r" +
  118. (SimInfo.regionLocY * Constants.RegionSize).ToString() + "], " +
  119. "'position':[r" + theUser.homeLocation.X.ToString() + ",r" +
  120. theUser.homeLocation.Y.ToString() + ",r" + theUser.homeLocation.Z.ToString() + "], " +
  121. "'look_at':[r" + theUser.homeLocation.X.ToString() + ",r" +
  122. theUser.homeLocation.Y.ToString() + ",r" + theUser.homeLocation.Z.ToString() + "]}";
  123. // Destination
  124. //CFK: The "Notifying" message always seems to appear, so subsume the data from this message into
  125. //CFK: the next one for X & Y and comment this one.
  126. //CFK: m_log.Info("[LOGIN]: CUSTOMISERESPONSE: Region X: " + SimInfo.regionLocX +
  127. //CFK: "; Region Y: " + SimInfo.regionLocY);
  128. response.SimAddress = Util.GetHostFromDNS(SimInfo.serverURI.Split(new char[] { '/', ':' })[3]).ToString();
  129. response.SimPort = uint.Parse(SimInfo.serverURI.Split(new char[] { '/', ':' })[4]);
  130. response.RegionX = SimInfo.regionLocX;
  131. response.RegionY = SimInfo.regionLocY;
  132. //Not sure if the + "/CAPS/" should in fact be +"CAPS/" depending if there is already a / as part of httpServerURI
  133. string capsPath = Util.GetRandomCapsPath();
  134. response.SeedCapability = SimInfo.httpServerURI + "CAPS/" + capsPath + "0000/";
  135. m_log.DebugFormat(
  136. "[CAPS]: Sending new CAPS seed url {0} to client {1}",
  137. response.SeedCapability, response.AgentID);
  138. // Notify the target of an incoming user
  139. //CFK: The "Notifying" message always seems to appear, so subsume the data from this message into
  140. //CFK: the next one for X & Y and comment this one.
  141. //CFK: m_log.Info("[LOGIN]: " + SimInfo.regionName + " (" + SimInfo.serverURI + ") " +
  142. //CFK: SimInfo.regionLocX + "," + SimInfo.regionLocY);
  143. theUser.currentAgent.currentRegion = SimInfo.UUID;
  144. theUser.currentAgent.currentHandle = SimInfo.regionHandle;
  145. // Prepare notification
  146. Hashtable SimParams = new Hashtable();
  147. SimParams["session_id"] = theUser.currentAgent.sessionID.ToString();
  148. SimParams["secure_session_id"] = theUser.currentAgent.secureSessionID.ToString();
  149. SimParams["firstname"] = theUser.username;
  150. SimParams["lastname"] = theUser.surname;
  151. SimParams["agent_id"] = theUser.UUID.ToString();
  152. SimParams["circuit_code"] = (Int32) Convert.ToUInt32(response.CircuitCode);
  153. SimParams["startpos_x"] = theUser.currentAgent.currentPos.X.ToString();
  154. SimParams["startpos_y"] = theUser.currentAgent.currentPos.Y.ToString();
  155. SimParams["startpos_z"] = theUser.currentAgent.currentPos.Z.ToString();
  156. SimParams["regionhandle"] = theUser.currentAgent.currentHandle.ToString();
  157. SimParams["caps_path"] = capsPath;
  158. ArrayList SendParams = new ArrayList();
  159. SendParams.Add(SimParams);
  160. // Update agent with target sim
  161. m_log.InfoFormat(
  162. "[LOGIN]: Telling region {0} @ {1},{2} ({3}) to expect user connection",
  163. SimInfo.regionName, response.RegionX, response.RegionY, SimInfo.httpServerURI);
  164. XmlRpcRequest GridReq = new XmlRpcRequest("expect_user", SendParams);
  165. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  166. if (GridResp.IsFault)
  167. {
  168. m_log.ErrorFormat(
  169. "[LOGIN]: XMLRPC request for {0} failed, fault code: {1}, reason: {2}",
  170. SimInfo.httpServerURI, GridResp.FaultCode, GridResp.FaultString);
  171. }
  172. handlerUserLoggedInAtLocation = OnUserLoggedInAtLocation;
  173. if (handlerUserLoggedInAtLocation != null)
  174. {
  175. m_log.Info("[LOGIN]: Letting other objects know about login");
  176. handlerUserLoggedInAtLocation(theUser.UUID, theUser.currentAgent.sessionID, theUser.currentAgent.currentRegion,
  177. theUser.currentAgent.currentHandle, theUser.currentAgent.currentPos.X,theUser.currentAgent.currentPos.Y,theUser.currentAgent.currentPos.Z,
  178. theUser.username,theUser.surname);
  179. }
  180. }
  181. catch (Exception)
  182. //catch (System.AccessViolationException)
  183. {
  184. tryDefault = true;
  185. }
  186. if (tryDefault)
  187. {
  188. // Send him to default region instead
  189. // Load information from the gridserver
  190. ulong defaultHandle = (((ulong)m_config.DefaultX * Constants.RegionSize) << 32) | ((ulong)m_config.DefaultY * Constants.RegionSize);
  191. m_log.Warn(
  192. "[LOGIN]: Home region not available: sending to default " + defaultHandle.ToString());
  193. try
  194. {
  195. RegionProfileData SimInfo = RegionProfileData.RequestSimProfileData(
  196. defaultHandle, m_config.GridServerURL,
  197. m_config.GridSendKey, m_config.GridRecvKey);
  198. // Customise the response
  199. m_log.Info("[LOGIN]: Home Location");
  200. response.Home = "{'region_handle':[r" + (SimInfo.regionLocX * Constants.RegionSize).ToString() + ",r" +
  201. (SimInfo.regionLocY * Constants.RegionSize).ToString() + "], " +
  202. "'position':[r" + theUser.homeLocation.X.ToString() + ",r" +
  203. theUser.homeLocation.Y.ToString() + ",r" + theUser.homeLocation.Z.ToString() + "], " +
  204. "'look_at':[r" + theUser.homeLocation.X.ToString() + ",r" +
  205. theUser.homeLocation.Y.ToString() + ",r" + theUser.homeLocation.Z.ToString() + "]}";
  206. // Destination
  207. m_log.Info("[LOGIN]: " +
  208. "CUSTOMISERESPONSE: Region X: " + SimInfo.regionLocX + "; Region Y: " +
  209. SimInfo.regionLocY);
  210. response.SimAddress = Util.GetHostFromDNS(SimInfo.serverURI.Split(new char[] { '/', ':' })[3]).ToString();
  211. response.SimPort = uint.Parse(SimInfo.serverURI.Split(new char[] { '/', ':' })[4]);
  212. response.RegionX = SimInfo.regionLocX;
  213. response.RegionY = SimInfo.regionLocY;
  214. //Not sure if the + "/CAPS/" should in fact be +"CAPS/" depending if there is already a / as part of httpServerURI
  215. string capsPath = Util.GetRandomCapsPath();
  216. response.SeedCapability = SimInfo.httpServerURI + "CAPS/" + capsPath + "0000/";
  217. // Notify the target of an incoming user
  218. m_log.Info("[LOGIN]: Notifying " + SimInfo.regionName + " (" + SimInfo.serverURI + ")");
  219. // Update agent with target sim
  220. theUser.currentAgent.currentRegion = SimInfo.UUID;
  221. theUser.currentAgent.currentHandle = SimInfo.regionHandle;
  222. // Prepare notification
  223. Hashtable SimParams = new Hashtable();
  224. SimParams["session_id"] = theUser.currentAgent.sessionID.ToString();
  225. SimParams["secure_session_id"] = theUser.currentAgent.secureSessionID.ToString();
  226. SimParams["firstname"] = theUser.username;
  227. SimParams["lastname"] = theUser.surname;
  228. SimParams["agent_id"] = theUser.UUID.ToString();
  229. SimParams["circuit_code"] = (Int32) Convert.ToUInt32(response.CircuitCode);
  230. SimParams["startpos_x"] = theUser.currentAgent.currentPos.X.ToString();
  231. SimParams["startpos_y"] = theUser.currentAgent.currentPos.Y.ToString();
  232. SimParams["startpos_z"] = theUser.currentAgent.currentPos.Z.ToString();
  233. SimParams["regionhandle"] = theUser.currentAgent.currentHandle.ToString();
  234. SimParams["caps_path"] = capsPath;
  235. ArrayList SendParams = new ArrayList();
  236. SendParams.Add(SimParams);
  237. m_log.Info("[LOGIN]: Informing region at " + SimInfo.httpServerURI);
  238. // Send
  239. XmlRpcRequest GridReq = new XmlRpcRequest("expect_user", SendParams);
  240. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  241. handlerUserLoggedInAtLocation = OnUserLoggedInAtLocation;
  242. if (handlerUserLoggedInAtLocation != null)
  243. {
  244. m_log.Info("[LOGIN]: Letting other objects know about login");
  245. handlerUserLoggedInAtLocation(theUser.UUID, theUser.currentAgent.sessionID, theUser.currentAgent.currentRegion,
  246. theUser.currentAgent.currentHandle, theUser.currentAgent.currentPos.X, theUser.currentAgent.currentPos.Y, theUser.currentAgent.currentPos.Z,
  247. theUser.username, theUser.surname);
  248. }
  249. }
  250. catch (Exception e)
  251. {
  252. m_log.Warn("[LOGIN]: Default region also not available");
  253. m_log.Warn("[LOGIN]: " + e.ToString());
  254. }
  255. }
  256. }
  257. // See LoginService
  258. protected override InventoryData GetInventorySkeleton(LLUUID userID)
  259. {
  260. List<InventoryFolderBase> folders
  261. = SynchronousRestObjectPoster.BeginPostObject<Guid, List<InventoryFolderBase>>(
  262. "POST", m_config.InventoryUrl + "RootFolders/", userID.UUID);
  263. // In theory, the user will only ever be missing a root folder in situations where a grid
  264. // which didn't previously run a grid wide inventory server is being transitioned to one
  265. // which does.
  266. if (null == folders | folders.Count == 0)
  267. {
  268. m_log.Warn(
  269. "[LOGIN]: " +
  270. "root inventory folder user " + userID + " not found. Creating.");
  271. RestObjectPoster.BeginPostObject<Guid>(
  272. m_config.InventoryUrl + "CreateInventory/", userID.UUID);
  273. // A big delay should be okay here since the recreation of the user's root folders should
  274. // only ever happen once. We need to sleep to let the inventory server do its work -
  275. // previously 1000ms has been found to be too short.
  276. Thread.Sleep(10000);
  277. folders = SynchronousRestObjectPoster.BeginPostObject<Guid, List<InventoryFolderBase>>(
  278. "POST", m_config.InventoryUrl + "RootFolders/", userID.UUID);
  279. }
  280. if (folders.Count > 0)
  281. {
  282. LLUUID rootID = LLUUID.Zero;
  283. ArrayList AgentInventoryArray = new ArrayList();
  284. Hashtable TempHash;
  285. foreach (InventoryFolderBase InvFolder in folders)
  286. {
  287. // m_log.DebugFormat("[LOGIN]: Received agent inventory folder {0}", InvFolder.name);
  288. if (InvFolder.parentID == LLUUID.Zero)
  289. {
  290. rootID = InvFolder.folderID;
  291. }
  292. TempHash = new Hashtable();
  293. TempHash["name"] = InvFolder.name;
  294. TempHash["parent_id"] = InvFolder.parentID.ToString();
  295. TempHash["version"] = (Int32) InvFolder.version;
  296. TempHash["type_default"] = (Int32) InvFolder.type;
  297. TempHash["folder_id"] = InvFolder.folderID.ToString();
  298. AgentInventoryArray.Add(TempHash);
  299. }
  300. return new InventoryData(AgentInventoryArray, rootID);
  301. }
  302. else
  303. {
  304. m_log.Warn("[LOGIN]: The root inventory folder could still not be retrieved" +
  305. " for user ID " + userID);
  306. AgentInventory userInventory = new AgentInventory();
  307. userInventory.CreateRootFolder(userID);
  308. ArrayList AgentInventoryArray = new ArrayList();
  309. Hashtable TempHash;
  310. foreach (InventoryFolder InvFolder in userInventory.InventoryFolders.Values)
  311. {
  312. TempHash = new Hashtable();
  313. TempHash["name"] = InvFolder.FolderName;
  314. TempHash["parent_id"] = InvFolder.ParentID.ToString();
  315. TempHash["version"] = (Int32) InvFolder.Version;
  316. TempHash["type_default"] = (Int32) InvFolder.DefaultType;
  317. TempHash["folder_id"] = InvFolder.FolderID.ToString();
  318. AgentInventoryArray.Add(TempHash);
  319. }
  320. return new InventoryData(AgentInventoryArray, userInventory.InventoryRoot.FolderID);
  321. }
  322. }
  323. }
  324. }