UserLoginService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. public override void LogOffUser(UserProfileData theUser, string message)
  57. {
  58. RegionProfileData SimInfo = null;
  59. try
  60. {
  61. SimInfo = RegionProfileData.RequestSimProfileData(
  62. theUser.CurrentAgent.Handle, m_config.GridServerURL,
  63. m_config.GridSendKey, m_config.GridRecvKey);
  64. if (SimInfo == null)
  65. {
  66. m_log.Error("[GRID]: Region user was in isn't currently logged in");
  67. return;
  68. }
  69. }
  70. catch (Exception)
  71. {
  72. m_log.Error("[GRID]: Unable to look up region to log user off");
  73. return;
  74. }
  75. // Prepare notification
  76. Hashtable SimParams = new Hashtable();
  77. SimParams["agent_id"] = theUser.ID.ToString();
  78. SimParams["region_secret"] = theUser.CurrentAgent.SecureSessionID.ToString();
  79. //SimParams["region_secret"] = SimInfo.regionSecret;
  80. //m_log.Info(SimInfo.regionSecret);
  81. SimParams["regionhandle"] = theUser.CurrentAgent.Handle.ToString();
  82. SimParams["message"] = message;
  83. ArrayList SendParams = new ArrayList();
  84. SendParams.Add(SimParams);
  85. // Update agent with target sim
  86. m_log.InfoFormat(
  87. "[ASSUMED CRASH]: Telling region {0} @ {1},{2} ({3}) that their agent is dead: {4}",
  88. SimInfo.regionName, SimInfo.regionLocX, SimInfo.regionLocY, SimInfo.httpServerURI, theUser.FirstName + " " + theUser.SurName);
  89. try
  90. {
  91. XmlRpcRequest GridReq = new XmlRpcRequest("logoff_user", SendParams);
  92. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  93. if (GridResp.IsFault)
  94. {
  95. m_log.ErrorFormat(
  96. "[LOGIN]: XMLRPC request for {0} failed, fault code: {1}, reason: {2}, This is likely an old region revision.",
  97. SimInfo.httpServerURI, GridResp.FaultCode, GridResp.FaultString);
  98. }
  99. }
  100. catch (Exception)
  101. {
  102. m_log.Error("[LOGIN]: Error telling region to logout user!");
  103. }
  104. //base.LogOffUser(theUser);
  105. }
  106. //public override void LogOffUser(UserProfileData theUser)
  107. //{
  108. //}
  109. /// <summary>
  110. /// Customises the login response and fills in missing values.
  111. /// </summary>
  112. /// <param name="response">The existing response</param>
  113. /// <param name="theUser">The user profile</param>
  114. public override void CustomiseResponse(LoginResponse response, UserProfileData theUser, string startLocationRequest)
  115. {
  116. bool tryDefault = false;
  117. //CFK: Since the try is always "tried", the "Home Location" message should always appear, so comment this one.
  118. //CFK: m_log.Info("[LOGIN]: Load information from the gridserver");
  119. try
  120. {
  121. RegionProfileData SimInfo = null;
  122. int start_x = -1;
  123. int start_y = -1;
  124. int start_z = -1;
  125. if (startLocationRequest == "last")
  126. {
  127. SimInfo =
  128. RegionProfileData.RequestSimProfileData(
  129. theUser.CurrentAgent.Handle, m_config.GridServerURL,
  130. m_config.GridSendKey, m_config.GridRecvKey);
  131. }
  132. else if (startLocationRequest == "home")
  133. {
  134. SimInfo =
  135. RegionProfileData.RequestSimProfileData(
  136. theUser.HomeRegion, m_config.GridServerURL,
  137. m_config.GridSendKey, m_config.GridRecvKey);
  138. }
  139. else
  140. {
  141. string[] startLocationRequestParsed = Util.ParseStartLocationRequest(startLocationRequest);
  142. m_log.Info("[DEBUGLOGINPARSE]: 1:" + startLocationRequestParsed[0] + ", 2:" + startLocationRequestParsed[1] + ", 3:" + startLocationRequestParsed[2] + ", 4:" + startLocationRequestParsed[3]);
  143. if (startLocationRequestParsed[0] == "last")
  144. {
  145. // TODO: Parse out startlocationrequest string in the format; 'uri:RegionName&X&Y&Z'
  146. SimInfo =
  147. RegionProfileData.RequestSimProfileData(
  148. theUser.CurrentAgent.Handle, m_config.GridServerURL,
  149. m_config.GridSendKey, m_config.GridRecvKey);
  150. }
  151. else
  152. {
  153. m_log.Info("[LOGIN]: Looking up Sim: " + startLocationRequestParsed[0]);
  154. SimInfo =
  155. RegionProfileData.RequestSimProfileData(
  156. startLocationRequestParsed[0], m_config.GridServerURL,
  157. m_config.GridSendKey, m_config.GridRecvKey);
  158. if (SimInfo == null)
  159. {
  160. m_log.Info("[LOGIN]: Didn't find region with a close name match sending to home location");
  161. SimInfo =
  162. RegionProfileData.RequestSimProfileData(
  163. theUser.HomeRegion, m_config.GridServerURL,
  164. m_config.GridSendKey, m_config.GridRecvKey);
  165. }
  166. else
  167. {
  168. start_x = Convert.ToInt32(startLocationRequestParsed[1]);
  169. start_y = Convert.ToInt32(startLocationRequestParsed[2]);
  170. start_z = Convert.ToInt32(startLocationRequestParsed[3]);
  171. }
  172. }
  173. }
  174. // Customise the response
  175. //CFK: This is redundant and the next message should always appear.
  176. //CFK: m_log.Info("[LOGIN]: Home Location");
  177. response.Home = "{'region_handle':[r" + (SimInfo.regionLocX * Constants.RegionSize).ToString() + ",r" +
  178. (SimInfo.regionLocY * Constants.RegionSize).ToString() + "], " +
  179. "'position':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  180. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "], " +
  181. "'look_at':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  182. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "]}";
  183. // Destination
  184. //CFK: The "Notifying" message always seems to appear, so subsume the data from this message into
  185. //CFK: the next one for X & Y and comment this one.
  186. //CFK: m_log.Info("[LOGIN]: CUSTOMISERESPONSE: Region X: " + SimInfo.regionLocX +
  187. //CFK: "; Region Y: " + SimInfo.regionLocY);
  188. response.SimAddress = Util.GetHostFromDNS(SimInfo.serverURI.Split(new char[] { '/', ':' })[3]).ToString();
  189. response.SimPort = uint.Parse(SimInfo.serverURI.Split(new char[] { '/', ':' })[4]);
  190. response.RegionX = SimInfo.regionLocX;
  191. response.RegionY = SimInfo.regionLocY;
  192. //Not sure if the + "/CAPS/" should in fact be +"CAPS/" depending if there is already a / as part of httpServerURI
  193. string capsPath = Util.GetRandomCapsPath();
  194. response.SeedCapability = SimInfo.httpServerURI + "CAPS/" + capsPath + "0000/";
  195. m_log.DebugFormat(
  196. "[LOGIN]: Sending new CAPS seed url {0} to client {1}",
  197. response.SeedCapability, response.AgentID);
  198. // Notify the target of an incoming user
  199. //CFK: The "Notifying" message always seems to appear, so subsume the data from this message into
  200. //CFK: the next one for X & Y and comment this one.
  201. //CFK: m_log.Info("[LOGIN]: " + SimInfo.regionName + " (" + SimInfo.serverURI + ") " +
  202. //CFK: SimInfo.regionLocX + "," + SimInfo.regionLocY);
  203. theUser.CurrentAgent.Region = SimInfo.UUID;
  204. theUser.CurrentAgent.Handle = SimInfo.regionHandle;
  205. if (start_x >= 0 && start_y >= 0 && start_z >= 0)
  206. {
  207. LLVector3 tmp_v = new LLVector3(start_x, start_y, start_z);
  208. theUser.CurrentAgent.Position = tmp_v;
  209. }
  210. // Prepare notification
  211. Hashtable SimParams = new Hashtable();
  212. SimParams["session_id"] = theUser.CurrentAgent.SessionID.ToString();
  213. SimParams["secure_session_id"] = theUser.CurrentAgent.SecureSessionID.ToString();
  214. SimParams["firstname"] = theUser.FirstName;
  215. SimParams["lastname"] = theUser.SurName;
  216. SimParams["agent_id"] = theUser.ID.ToString();
  217. SimParams["circuit_code"] = (Int32) Convert.ToUInt32(response.CircuitCode);
  218. SimParams["startpos_x"] = theUser.CurrentAgent.Position.X.ToString();
  219. SimParams["startpos_y"] = theUser.CurrentAgent.Position.Y.ToString();
  220. SimParams["startpos_z"] = theUser.CurrentAgent.Position.Z.ToString();
  221. SimParams["regionhandle"] = theUser.CurrentAgent.Handle.ToString();
  222. SimParams["caps_path"] = capsPath;
  223. ArrayList SendParams = new ArrayList();
  224. SendParams.Add(SimParams);
  225. // Update agent with target sim
  226. m_log.InfoFormat(
  227. "[LOGIN]: Telling region {0} @ {1},{2} ({3}) to expect user connection",
  228. SimInfo.regionName, response.RegionX, response.RegionY, SimInfo.httpServerURI);
  229. XmlRpcRequest GridReq = new XmlRpcRequest("expect_user", SendParams);
  230. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  231. if (GridResp.IsFault)
  232. {
  233. m_log.ErrorFormat(
  234. "[LOGIN]: XMLRPC request for {0} failed, fault code: {1}, reason: {2}",
  235. SimInfo.httpServerURI, GridResp.FaultCode, GridResp.FaultString);
  236. }
  237. if (!GridResp.IsFault)
  238. {
  239. bool responseSuccess = true;
  240. if (GridResp.Value != null)
  241. {
  242. Hashtable resp = (Hashtable)GridResp.Value;
  243. if (resp.ContainsKey("success"))
  244. {
  245. if ((string)resp["success"] == "FALSE")
  246. {
  247. responseSuccess = false;
  248. tryDefault = true;
  249. }
  250. }
  251. }
  252. if (responseSuccess)
  253. {
  254. handlerUserLoggedInAtLocation = OnUserLoggedInAtLocation;
  255. if (handlerUserLoggedInAtLocation != null)
  256. {
  257. //m_log.Info("[LOGIN]: Letting other objects know about login");
  258. handlerUserLoggedInAtLocation(theUser.ID, theUser.CurrentAgent.SessionID, theUser.CurrentAgent.Region,
  259. theUser.CurrentAgent.Handle, theUser.CurrentAgent.Position.X, theUser.CurrentAgent.Position.Y, theUser.CurrentAgent.Position.Z,
  260. theUser.FirstName, theUser.SurName);
  261. }
  262. }
  263. }
  264. }
  265. catch (Exception)
  266. //catch (System.AccessViolationException)
  267. {
  268. tryDefault = true;
  269. }
  270. if (tryDefault)
  271. {
  272. // Send him to default region instead
  273. // Load information from the gridserver
  274. ulong defaultHandle = (((ulong)m_config.DefaultX * Constants.RegionSize) << 32) | ((ulong)m_config.DefaultY * Constants.RegionSize);
  275. m_log.Warn(
  276. "[LOGIN]: Home region not available: sending to default " + defaultHandle.ToString());
  277. try
  278. {
  279. RegionProfileData SimInfo = RegionProfileData.RequestSimProfileData(
  280. defaultHandle, m_config.GridServerURL,
  281. m_config.GridSendKey, m_config.GridRecvKey);
  282. // Customise the response
  283. m_log.Info("[LOGIN]: Home Location");
  284. response.Home = "{'region_handle':[r" + (SimInfo.regionLocX * Constants.RegionSize).ToString() + ",r" +
  285. (SimInfo.regionLocY * Constants.RegionSize).ToString() + "], " +
  286. "'position':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  287. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "], " +
  288. "'look_at':[r" + theUser.HomeLocation.X.ToString() + ",r" +
  289. theUser.HomeLocation.Y.ToString() + ",r" + theUser.HomeLocation.Z.ToString() + "]}";
  290. // Destination
  291. m_log.Info("[LOGIN]: " +
  292. "CUSTOMISERESPONSE: Region X: " + SimInfo.regionLocX + "; Region Y: " +
  293. SimInfo.regionLocY);
  294. response.SimAddress = Util.GetHostFromDNS(SimInfo.serverURI.Split(new char[] { '/', ':' })[3]).ToString();
  295. response.SimPort = uint.Parse(SimInfo.serverURI.Split(new char[] { '/', ':' })[4]);
  296. response.RegionX = SimInfo.regionLocX;
  297. response.RegionY = SimInfo.regionLocY;
  298. //Not sure if the + "/CAPS/" should in fact be +"CAPS/" depending if there is already a / as part of httpServerURI
  299. string capsPath = Util.GetRandomCapsPath();
  300. response.SeedCapability = SimInfo.httpServerURI + "CAPS/" + capsPath + "0000/";
  301. // Notify the target of an incoming user
  302. m_log.Info("[LOGIN]: Notifying " + SimInfo.regionName + " (" + SimInfo.serverURI + ")");
  303. // Update agent with target sim
  304. theUser.CurrentAgent.Region = SimInfo.UUID;
  305. theUser.CurrentAgent.Handle = SimInfo.regionHandle;
  306. // Prepare notification
  307. Hashtable SimParams = new Hashtable();
  308. SimParams["session_id"] = theUser.CurrentAgent.SessionID.ToString();
  309. SimParams["secure_session_id"] = theUser.CurrentAgent.SecureSessionID.ToString();
  310. SimParams["firstname"] = theUser.FirstName;
  311. SimParams["lastname"] = theUser.SurName;
  312. SimParams["agent_id"] = theUser.ID.ToString();
  313. SimParams["circuit_code"] = (Int32) Convert.ToUInt32(response.CircuitCode);
  314. SimParams["startpos_x"] = theUser.CurrentAgent.Position.X.ToString();
  315. SimParams["startpos_y"] = theUser.CurrentAgent.Position.Y.ToString();
  316. SimParams["startpos_z"] = theUser.CurrentAgent.Position.Z.ToString();
  317. SimParams["regionhandle"] = theUser.CurrentAgent.Handle.ToString();
  318. SimParams["caps_path"] = capsPath;
  319. ArrayList SendParams = new ArrayList();
  320. SendParams.Add(SimParams);
  321. m_log.Info("[LOGIN]: Informing region at " + SimInfo.httpServerURI);
  322. // Send
  323. XmlRpcRequest GridReq = new XmlRpcRequest("expect_user", SendParams);
  324. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  325. if (!GridResp.IsFault)
  326. {
  327. bool responseSuccess = true;
  328. if (GridResp.Value != null)
  329. {
  330. Hashtable resp = (Hashtable) GridResp.Value;
  331. if (resp.ContainsKey("success"))
  332. {
  333. if ((string)resp["success"] == "FALSE")
  334. {
  335. responseSuccess = false;
  336. tryDefault = true;
  337. }
  338. }
  339. }
  340. if (responseSuccess)
  341. {
  342. handlerUserLoggedInAtLocation = OnUserLoggedInAtLocation;
  343. if (handlerUserLoggedInAtLocation != null)
  344. {
  345. m_log.Info("[LOGIN]: Letting other objects know about login");
  346. handlerUserLoggedInAtLocation(theUser.ID, theUser.CurrentAgent.SessionID, theUser.CurrentAgent.Region,
  347. theUser.CurrentAgent.Handle, theUser.CurrentAgent.Position.X, theUser.CurrentAgent.Position.Y, theUser.CurrentAgent.Position.Z,
  348. theUser.FirstName, theUser.SurName);
  349. }
  350. }
  351. else
  352. {
  353. response.CreateDeadRegionResponse();
  354. }
  355. }
  356. else
  357. {
  358. response.CreateDeadRegionResponse();
  359. }
  360. }
  361. catch (Exception e)
  362. {
  363. m_log.Warn("[LOGIN]: Default region also not available");
  364. m_log.Warn("[LOGIN]: " + e.ToString());
  365. }
  366. }
  367. }
  368. // See LoginService
  369. protected override InventoryData GetInventorySkeleton(LLUUID userID, string serverUrl)
  370. {
  371. string invUrl = m_config.InventoryUrl;
  372. //if (!String.IsNullOrEmpty(serverUrl))
  373. //{
  374. // invUrl = serverUrl+"/";
  375. //}
  376. m_log.DebugFormat(
  377. "[LOGIN]: Contacting inventory service at {0} for inventory skeleton of user {1}",
  378. m_config.InventoryUrl, userID);
  379. List<InventoryFolderBase> folders
  380. = SynchronousRestObjectPoster.BeginPostObject<Guid, List<InventoryFolderBase>>(
  381. "POST", invUrl + "RootFolders/", userID.UUID);
  382. if (null == folders || folders.Count == 0)
  383. {
  384. m_log.InfoFormat(
  385. "[LOGIN]: A root inventory folder for user {0} was not found. Requesting creation.", userID);
  386. // Although the create user function creates a new agent inventory along with a new user profile, some
  387. // tools are creating the user profile directly in the database without creating the inventory. At
  388. // this time we'll accomodate them by lazily creating the user inventory now if it doesn't already
  389. // exist.
  390. bool created =
  391. SynchronousRestObjectPoster.BeginPostObject<Guid, bool>(
  392. "POST", invUrl + "CreateInventory/", userID.UUID);
  393. if (!created)
  394. {
  395. throw new Exception(
  396. String.Format(
  397. "The inventory creation request for user {0} did not succeed."
  398. + " Please contact your inventory service provider for more information.",
  399. userID));
  400. }
  401. else
  402. {
  403. m_log.InfoFormat("[LOGIN]: A new inventory skeleton was successfully created for user {0}", userID);
  404. }
  405. folders = SynchronousRestObjectPoster.BeginPostObject<Guid, List<InventoryFolderBase>>(
  406. "POST", invUrl + "RootFolders/", userID.UUID);
  407. }
  408. if (folders != null && folders.Count > 0)
  409. {
  410. LLUUID rootID = LLUUID.Zero;
  411. ArrayList AgentInventoryArray = new ArrayList();
  412. Hashtable TempHash;
  413. foreach (InventoryFolderBase InvFolder in folders)
  414. {
  415. // m_log.DebugFormat("[LOGIN]: Received agent inventory folder {0}", InvFolder.name);
  416. if (InvFolder.ParentID == LLUUID.Zero)
  417. {
  418. rootID = InvFolder.ID;
  419. }
  420. TempHash = new Hashtable();
  421. TempHash["name"] = InvFolder.Name;
  422. TempHash["parent_id"] = InvFolder.ParentID.ToString();
  423. TempHash["version"] = (Int32) InvFolder.Version;
  424. TempHash["type_default"] = (Int32) InvFolder.Type;
  425. TempHash["folder_id"] = InvFolder.ID.ToString();
  426. AgentInventoryArray.Add(TempHash);
  427. }
  428. return new InventoryData(AgentInventoryArray, rootID);
  429. }
  430. else
  431. {
  432. throw new Exception(
  433. String.Format(
  434. "A root inventory folder for user {0} could not be retrieved from the inventory service",
  435. userID));
  436. }
  437. }
  438. }
  439. }