UserLoginService.cs 20 KB

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