UserLoginService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. }
  113. else
  114. {
  115. start_x = Convert.ToInt32(startLocationRequestParsed[1]);
  116. start_y = Convert.ToInt32(startLocationRequestParsed[2]);
  117. start_z = Convert.ToInt32(startLocationRequestParsed[3]);
  118. }
  119. }
  120. }
  121. // Customise the response
  122. //CFK: This is redundant and the next message should always appear.
  123. //CFK: m_log.Info("[LOGIN]: Home Location");
  124. response.Home = "{'region_handle':[r" + (SimInfo.regionLocX * Constants.RegionSize).ToString() + ",r" +
  125. (SimInfo.regionLocY * Constants.RegionSize).ToString() + "], " +
  126. "'position':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  127. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "], " +
  128. "'look_at':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  129. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "]}";
  130. // Destination
  131. //CFK: The "Notifying" message always seems to appear, so subsume the data from this message into
  132. //CFK: the next one for X & Y and comment this one.
  133. //CFK: m_log.Info("[LOGIN]: CUSTOMISERESPONSE: Region X: " + SimInfo.regionLocX +
  134. //CFK: "; Region Y: " + SimInfo.regionLocY);
  135. response.SimAddress = Util.GetHostFromDNS(SimInfo.serverURI.Split(new char[] { '/', ':' })[3]).ToString();
  136. response.SimPort = uint.Parse(SimInfo.serverURI.Split(new char[] { '/', ':' })[4]);
  137. response.RegionX = SimInfo.regionLocX;
  138. response.RegionY = SimInfo.regionLocY;
  139. //Not sure if the + "/CAPS/" should in fact be +"CAPS/" depending if there is already a / as part of httpServerURI
  140. string capsPath = Util.GetRandomCapsPath();
  141. response.SeedCapability = SimInfo.httpServerURI + "CAPS/" + capsPath + "0000/";
  142. m_log.DebugFormat(
  143. "[LOGIN]: Sending new CAPS seed url {0} to client {1}",
  144. response.SeedCapability, response.AgentID);
  145. // Notify the target of an incoming user
  146. //CFK: The "Notifying" message always seems to appear, so subsume the data from this message into
  147. //CFK: the next one for X & Y and comment this one.
  148. //CFK: m_log.Info("[LOGIN]: " + SimInfo.regionName + " (" + SimInfo.serverURI + ") " +
  149. //CFK: SimInfo.regionLocX + "," + SimInfo.regionLocY);
  150. theUser.CurrentAgent.Region = SimInfo.UUID;
  151. theUser.CurrentAgent.Handle = SimInfo.regionHandle;
  152. if (start_x >= 0 && start_y >= 0 && start_z >= 0)
  153. {
  154. LLVector3 tmp_v = new LLVector3(start_x, start_y, start_z);
  155. theUser.CurrentAgent.Position = tmp_v;
  156. }
  157. // Prepare notification
  158. Hashtable SimParams = new Hashtable();
  159. SimParams["session_id"] = theUser.CurrentAgent.SessionID.ToString();
  160. SimParams["secure_session_id"] = theUser.CurrentAgent.SecureSessionID.ToString();
  161. SimParams["firstname"] = theUser.FirstName;
  162. SimParams["lastname"] = theUser.SurName;
  163. SimParams["agent_id"] = theUser.ID.ToString();
  164. SimParams["circuit_code"] = (Int32) Convert.ToUInt32(response.CircuitCode);
  165. SimParams["startpos_x"] = theUser.CurrentAgent.Position.X.ToString();
  166. SimParams["startpos_y"] = theUser.CurrentAgent.Position.Y.ToString();
  167. SimParams["startpos_z"] = theUser.CurrentAgent.Position.Z.ToString();
  168. SimParams["regionhandle"] = theUser.CurrentAgent.Handle.ToString();
  169. SimParams["caps_path"] = capsPath;
  170. ArrayList SendParams = new ArrayList();
  171. SendParams.Add(SimParams);
  172. // Update agent with target sim
  173. m_log.InfoFormat(
  174. "[LOGIN]: Telling region {0} @ {1},{2} ({3}) to expect user connection",
  175. SimInfo.regionName, response.RegionX, response.RegionY, SimInfo.httpServerURI);
  176. XmlRpcRequest GridReq = new XmlRpcRequest("expect_user", SendParams);
  177. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  178. if (GridResp.IsFault)
  179. {
  180. m_log.ErrorFormat(
  181. "[LOGIN]: XMLRPC request for {0} failed, fault code: {1}, reason: {2}",
  182. SimInfo.httpServerURI, GridResp.FaultCode, GridResp.FaultString);
  183. }
  184. handlerUserLoggedInAtLocation = OnUserLoggedInAtLocation;
  185. if (handlerUserLoggedInAtLocation != null)
  186. {
  187. //m_log.Info("[LOGIN]: Letting other objects know about login");
  188. handlerUserLoggedInAtLocation(theUser.ID, theUser.CurrentAgent.SessionID, theUser.CurrentAgent.Region,
  189. theUser.CurrentAgent.Handle, theUser.CurrentAgent.Position.X,theUser.CurrentAgent.Position.Y,theUser.CurrentAgent.Position.Z,
  190. theUser.FirstName,theUser.SurName);
  191. }
  192. }
  193. catch (Exception)
  194. //catch (System.AccessViolationException)
  195. {
  196. tryDefault = true;
  197. }
  198. if (tryDefault)
  199. {
  200. // Send him to default region instead
  201. // Load information from the gridserver
  202. ulong defaultHandle = (((ulong)m_config.DefaultX * Constants.RegionSize) << 32) | ((ulong)m_config.DefaultY * Constants.RegionSize);
  203. m_log.Warn(
  204. "[LOGIN]: Home region not available: sending to default " + defaultHandle.ToString());
  205. try
  206. {
  207. RegionProfileData SimInfo = RegionProfileData.RequestSimProfileData(
  208. defaultHandle, m_config.GridServerURL,
  209. m_config.GridSendKey, m_config.GridRecvKey);
  210. // Customise the response
  211. m_log.Info("[LOGIN]: Home Location");
  212. response.Home = "{'region_handle':[r" + (SimInfo.regionLocX * Constants.RegionSize).ToString() + ",r" +
  213. (SimInfo.regionLocY * Constants.RegionSize).ToString() + "], " +
  214. "'position':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  215. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "], " +
  216. "'look_at':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  217. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "]}";
  218. // Destination
  219. m_log.Info("[LOGIN]: " +
  220. "CUSTOMISERESPONSE: Region X: " + SimInfo.regionLocX + "; Region Y: " +
  221. SimInfo.regionLocY);
  222. response.SimAddress = Util.GetHostFromDNS(SimInfo.serverURI.Split(new char[] { '/', ':' })[3]).ToString();
  223. response.SimPort = uint.Parse(SimInfo.serverURI.Split(new char[] { '/', ':' })[4]);
  224. response.RegionX = SimInfo.regionLocX;
  225. response.RegionY = SimInfo.regionLocY;
  226. //Not sure if the + "/CAPS/" should in fact be +"CAPS/" depending if there is already a / as part of httpServerURI
  227. string capsPath = Util.GetRandomCapsPath();
  228. response.SeedCapability = SimInfo.httpServerURI + "CAPS/" + capsPath + "0000/";
  229. // Notify the target of an incoming user
  230. m_log.Info("[LOGIN]: Notifying " + SimInfo.regionName + " (" + SimInfo.serverURI + ")");
  231. // Update agent with target sim
  232. theUser.CurrentAgent.Region = SimInfo.UUID;
  233. theUser.CurrentAgent.Handle = SimInfo.regionHandle;
  234. // Prepare notification
  235. Hashtable SimParams = new Hashtable();
  236. SimParams["session_id"] = theUser.CurrentAgent.SessionID.ToString();
  237. SimParams["secure_session_id"] = theUser.CurrentAgent.SecureSessionID.ToString();
  238. SimParams["firstname"] = theUser.FirstName;
  239. SimParams["lastname"] = theUser.SurName;
  240. SimParams["agent_id"] = theUser.ID.ToString();
  241. SimParams["circuit_code"] = (Int32) Convert.ToUInt32(response.CircuitCode);
  242. SimParams["startpos_x"] = theUser.CurrentAgent.Position.X.ToString();
  243. SimParams["startpos_y"] = theUser.CurrentAgent.Position.Y.ToString();
  244. SimParams["startpos_z"] = theUser.CurrentAgent.Position.Z.ToString();
  245. SimParams["regionhandle"] = theUser.CurrentAgent.Handle.ToString();
  246. SimParams["caps_path"] = capsPath;
  247. ArrayList SendParams = new ArrayList();
  248. SendParams.Add(SimParams);
  249. m_log.Info("[LOGIN]: Informing region at " + SimInfo.httpServerURI);
  250. // Send
  251. XmlRpcRequest GridReq = new XmlRpcRequest("expect_user", SendParams);
  252. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  253. handlerUserLoggedInAtLocation = OnUserLoggedInAtLocation;
  254. if (handlerUserLoggedInAtLocation != null)
  255. {
  256. m_log.Info("[LOGIN]: Letting other objects know about login");
  257. handlerUserLoggedInAtLocation(theUser.ID, theUser.CurrentAgent.SessionID, theUser.CurrentAgent.Region,
  258. theUser.CurrentAgent.Handle, theUser.CurrentAgent.Position.X, theUser.CurrentAgent.Position.Y, theUser.CurrentAgent.Position.Z,
  259. theUser.FirstName, theUser.SurName);
  260. }
  261. }
  262. catch (Exception e)
  263. {
  264. m_log.Warn("[LOGIN]: Default region also not available");
  265. m_log.Warn("[LOGIN]: " + e.ToString());
  266. }
  267. }
  268. }
  269. // See LoginService
  270. protected override InventoryData GetInventorySkeleton(LLUUID userID)
  271. {
  272. m_log.DebugFormat(
  273. "[LOGIN]: Contacting inventory service at {0} for inventory skeleton of user {1}",
  274. m_config.InventoryUrl, userID);
  275. List<InventoryFolderBase> folders
  276. = SynchronousRestObjectPoster.BeginPostObject<Guid, List<InventoryFolderBase>>(
  277. "POST", m_config.InventoryUrl + "RootFolders/", userID.UUID);
  278. if (null == folders || folders.Count == 0)
  279. {
  280. m_log.InfoFormat(
  281. "[LOGIN]: A root inventory folder for user {0} was not found. Requesting creation.", userID);
  282. // Although the create user function creates a new agent inventory along with a new user profile, some
  283. // tools are creating the user profile directly in the database without creating the inventory. At
  284. // this time we'll accomodate them by lazily creating the user inventory now if it doesn't already
  285. // exist.
  286. bool created =
  287. SynchronousRestObjectPoster.BeginPostObject<Guid, bool>(
  288. "POST", m_config.InventoryUrl + "CreateInventory/", userID.UUID);
  289. if (!created)
  290. {
  291. throw new Exception(
  292. String.Format(
  293. "The inventory creation request for user {0} did not succeed."
  294. + " Please contact your inventory service provider for more information.",
  295. userID));
  296. }
  297. else
  298. {
  299. m_log.InfoFormat("[LOGIN]: A new inventory skeleton was successfully created for user {0}", userID);
  300. }
  301. folders = SynchronousRestObjectPoster.BeginPostObject<Guid, List<InventoryFolderBase>>(
  302. "POST", m_config.InventoryUrl + "RootFolders/", userID.UUID);
  303. }
  304. if (folders != null && folders.Count > 0)
  305. {
  306. LLUUID rootID = LLUUID.Zero;
  307. ArrayList AgentInventoryArray = new ArrayList();
  308. Hashtable TempHash;
  309. foreach (InventoryFolderBase InvFolder in folders)
  310. {
  311. // m_log.DebugFormat("[LOGIN]: Received agent inventory folder {0}", InvFolder.name);
  312. if (InvFolder.ParentID == LLUUID.Zero)
  313. {
  314. rootID = InvFolder.ID;
  315. }
  316. TempHash = new Hashtable();
  317. TempHash["name"] = InvFolder.Name;
  318. TempHash["parent_id"] = InvFolder.ParentID.ToString();
  319. TempHash["version"] = (Int32) InvFolder.Version;
  320. TempHash["type_default"] = (Int32) InvFolder.Type;
  321. TempHash["folder_id"] = InvFolder.ID.ToString();
  322. AgentInventoryArray.Add(TempHash);
  323. }
  324. return new InventoryData(AgentInventoryArray, rootID);
  325. }
  326. else
  327. {
  328. throw new Exception(
  329. String.Format(
  330. "A root inventory folder for user {0} could not be retrieved from the inventory service",
  331. userID));
  332. }
  333. }
  334. }
  335. }