UserLoginService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text.RegularExpressions;
  33. using log4net;
  34. using Nwc.XmlRpc;
  35. using OpenMetaverse;
  36. using OpenSim.Data;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Communications;
  39. using OpenSim.Framework.Communications.Services;
  40. using OpenSim.Framework.Communications.Cache;
  41. using OpenSim.Framework.Capabilities;
  42. using OpenSim.Framework.Servers;
  43. using OpenSim.Framework.Servers.HttpServer;
  44. namespace OpenSim.Grid.UserServer.Modules
  45. {
  46. public delegate void UserLoggedInAtLocation(UUID agentID, UUID sessionID, UUID RegionID,
  47. ulong regionhandle, float positionX, float positionY, float positionZ,
  48. string firstname, string lastname);
  49. /// <summary>
  50. /// Login service used in grid mode.
  51. /// </summary>
  52. public class UserLoginService : LoginService
  53. {
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. public event UserLoggedInAtLocation OnUserLoggedInAtLocation;
  56. private UserLoggedInAtLocation handlerUserLoggedInAtLocation;
  57. public UserConfig m_config;
  58. private readonly IRegionProfileRouter m_regionProfileService;
  59. protected BaseHttpServer m_httpServer;
  60. public UserLoginService(
  61. UserManagerBase userManager, IInterServiceInventoryServices inventoryService,
  62. LibraryRootFolder libraryRootFolder,
  63. UserConfig config, string welcomeMess, IRegionProfileRouter regionProfileService)
  64. : base(userManager, libraryRootFolder, welcomeMess)
  65. {
  66. m_config = config;
  67. m_defaultHomeX = m_config.DefaultX;
  68. m_defaultHomeY = m_config.DefaultY;
  69. m_interInventoryService = inventoryService;
  70. m_regionProfileService = regionProfileService;
  71. }
  72. public void RegisterHandlers(BaseHttpServer httpServer, bool registerLLSDHandler, bool registerOpenIDHandlers)
  73. {
  74. m_httpServer = httpServer;
  75. m_httpServer.AddXmlRPCHandler("login_to_simulator", XmlRpcLoginMethod);
  76. m_httpServer.AddHTTPHandler("login", ProcessHTMLLogin);
  77. m_httpServer.AddXmlRPCHandler("set_login_params", XmlRPCSetLoginParams);
  78. m_httpServer.AddXmlRPCHandler("check_auth_session", XmlRPCCheckAuthSession, false);
  79. if (registerLLSDHandler)
  80. {
  81. m_httpServer.SetDefaultLLSDHandler(LLSDLoginMethod);
  82. }
  83. if (registerOpenIDHandlers)
  84. {
  85. // Handler for OpenID avatar identity pages
  86. m_httpServer.AddStreamHandler(new OpenIdStreamHandler("GET", "/users/", this));
  87. // Handlers for the OpenID endpoint server
  88. m_httpServer.AddStreamHandler(new OpenIdStreamHandler("POST", "/openid/server/", this));
  89. m_httpServer.AddStreamHandler(new OpenIdStreamHandler("GET", "/openid/server/", this));
  90. }
  91. }
  92. public void setloginlevel(int level)
  93. {
  94. m_minLoginLevel = level;
  95. m_log.InfoFormat("[GRID]: Login Level set to {0} ", level);
  96. }
  97. public void setwelcometext(string text)
  98. {
  99. m_welcomeMessage = text;
  100. m_log.InfoFormat("[GRID]: Login text set to {0} ", text);
  101. }
  102. public override void LogOffUser(UserProfileData theUser, string message)
  103. {
  104. RegionProfileData SimInfo;
  105. try
  106. {
  107. SimInfo = m_regionProfileService.RequestSimProfileData(
  108. theUser.CurrentAgent.Handle, m_config.GridServerURL,
  109. m_config.GridSendKey, m_config.GridRecvKey);
  110. if (SimInfo == null)
  111. {
  112. m_log.Error("[GRID]: Region user was in isn't currently logged in");
  113. return;
  114. }
  115. }
  116. catch (Exception)
  117. {
  118. m_log.Error("[GRID]: Unable to look up region to log user off");
  119. return;
  120. }
  121. // Prepare notification
  122. Hashtable SimParams = new Hashtable();
  123. SimParams["agent_id"] = theUser.ID.ToString();
  124. SimParams["region_secret"] = theUser.CurrentAgent.SecureSessionID.ToString();
  125. SimParams["region_secret2"] = SimInfo.regionSecret;
  126. //m_log.Info(SimInfo.regionSecret);
  127. SimParams["regionhandle"] = theUser.CurrentAgent.Handle.ToString();
  128. SimParams["message"] = message;
  129. ArrayList SendParams = new ArrayList();
  130. SendParams.Add(SimParams);
  131. m_log.InfoFormat(
  132. "[ASSUMED CRASH]: Telling region {0} @ {1},{2} ({3}) that their agent is dead: {4}",
  133. SimInfo.regionName, SimInfo.regionLocX, SimInfo.regionLocY, SimInfo.httpServerURI,
  134. theUser.FirstName + " " + theUser.SurName);
  135. try
  136. {
  137. XmlRpcRequest GridReq = new XmlRpcRequest("logoff_user", SendParams);
  138. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  139. if (GridResp.IsFault)
  140. {
  141. m_log.ErrorFormat(
  142. "[LOGIN]: XMLRPC request for {0} failed, fault code: {1}, reason: {2}, This is likely an old region revision.",
  143. SimInfo.httpServerURI, GridResp.FaultCode, GridResp.FaultString);
  144. }
  145. }
  146. catch (Exception)
  147. {
  148. m_log.Error("[LOGIN]: Error telling region to logout user!");
  149. }
  150. // Prepare notification
  151. SimParams = new Hashtable();
  152. SimParams["agent_id"] = theUser.ID.ToString();
  153. SimParams["region_secret"] = SimInfo.regionSecret;
  154. //m_log.Info(SimInfo.regionSecret);
  155. SimParams["regionhandle"] = theUser.CurrentAgent.Handle.ToString();
  156. SimParams["message"] = message;
  157. SendParams = new ArrayList();
  158. SendParams.Add(SimParams);
  159. m_log.InfoFormat(
  160. "[ASSUMED CRASH]: Telling region {0} @ {1},{2} ({3}) that their agent is dead: {4}",
  161. SimInfo.regionName, SimInfo.regionLocX, SimInfo.regionLocY, SimInfo.httpServerURI,
  162. theUser.FirstName + " " + theUser.SurName);
  163. try
  164. {
  165. XmlRpcRequest GridReq = new XmlRpcRequest("logoff_user", SendParams);
  166. XmlRpcResponse GridResp = GridReq.Send(SimInfo.httpServerURI, 6000);
  167. if (GridResp.IsFault)
  168. {
  169. m_log.ErrorFormat(
  170. "[LOGIN]: XMLRPC request for {0} failed, fault code: {1}, reason: {2}, This is likely an old region revision.",
  171. SimInfo.httpServerURI, GridResp.FaultCode, GridResp.FaultString);
  172. }
  173. }
  174. catch (Exception)
  175. {
  176. m_log.Error("[LOGIN]: Error telling region to logout user!");
  177. }
  178. //base.LogOffUser(theUser);
  179. }
  180. protected override RegionInfo RequestClosestRegion(string region)
  181. {
  182. RegionProfileData profileData = m_regionProfileService.RequestSimProfileData(region,
  183. m_config.GridServerURL, m_config.GridSendKey, m_config.GridRecvKey);
  184. if (profileData != null)
  185. {
  186. return profileData.ToRegionInfo();
  187. }
  188. else
  189. {
  190. return null;
  191. }
  192. }
  193. protected override RegionInfo GetRegionInfo(ulong homeRegionHandle)
  194. {
  195. RegionProfileData profileData = m_regionProfileService.RequestSimProfileData(homeRegionHandle,
  196. m_config.GridServerURL, m_config.GridSendKey,
  197. m_config.GridRecvKey);
  198. if (profileData != null)
  199. {
  200. return profileData.ToRegionInfo();
  201. }
  202. else
  203. {
  204. return null;
  205. }
  206. }
  207. protected override RegionInfo GetRegionInfo(UUID homeRegionId)
  208. {
  209. RegionProfileData profileData = m_regionProfileService.RequestSimProfileData(homeRegionId,
  210. m_config.GridServerURL, m_config.GridSendKey,
  211. m_config.GridRecvKey);
  212. if (profileData != null)
  213. {
  214. return profileData.ToRegionInfo();
  215. }
  216. else
  217. {
  218. return null;
  219. }
  220. }
  221. protected override bool PrepareLoginToRegion(RegionInfo regionInfo, UserProfileData user, LoginResponse response, IPEndPoint remoteClient)
  222. {
  223. return PrepareLoginToRegion(RegionProfileData.FromRegionInfo(regionInfo), user, response, remoteClient);
  224. }
  225. /// <summary>
  226. /// Prepare a login to the given region. This involves both telling the region to expect a connection
  227. /// and appropriately customising the response to the user.
  228. /// </summary>
  229. /// <param name="regionInfo"></param>
  230. /// <param name="user"></param>
  231. /// <param name="response"></param>
  232. /// <returns>true if the region was successfully contacted, false otherwise</returns>
  233. private bool PrepareLoginToRegion(RegionProfileData regionInfo, UserProfileData user, LoginResponse response, IPEndPoint remoteClient)
  234. {
  235. try
  236. {
  237. response.SimAddress = Util.GetHostFromURL(regionInfo.serverURI).ToString();
  238. response.SimPort = uint.Parse(regionInfo.serverURI.Split(new char[] { '/', ':' })[4]);
  239. response.RegionX = regionInfo.regionLocX;
  240. response.RegionY = regionInfo.regionLocY;
  241. string capsPath = CapsUtil.GetRandomCapsObjectPath();
  242. // Adam's working code commented for now -- Diva 5/25/2009
  243. //// For NAT
  244. ////string host = NetworkUtil.GetHostFor(remoteClient.Address, regionInfo.ServerIP);
  245. //string host = response.SimAddress;
  246. //// TODO: This doesnt support SSL. -Adam
  247. //string serverURI = "http://" + host + ":" + regionInfo.ServerPort;
  248. //response.SeedCapability = serverURI + CapsUtil.GetCapsSeedPath(capsPath);
  249. // Take off trailing / so that the caps path isn't //CAPS/someUUID
  250. if (regionInfo.httpServerURI.EndsWith("/"))
  251. regionInfo.httpServerURI = regionInfo.httpServerURI.Substring(0, regionInfo.httpServerURI.Length - 1);
  252. response.SeedCapability = regionInfo.httpServerURI + CapsUtil.GetCapsSeedPath(capsPath);
  253. // Notify the target of an incoming user
  254. m_log.InfoFormat(
  255. "[LOGIN]: Telling {0} @ {1},{2} ({3}) to prepare for client connection",
  256. regionInfo.regionName, response.RegionX, response.RegionY, regionInfo.httpServerURI);
  257. // Update agent with target sim
  258. user.CurrentAgent.Region = regionInfo.UUID;
  259. user.CurrentAgent.Handle = regionInfo.regionHandle;
  260. // Prepare notification
  261. Hashtable loginParams = new Hashtable();
  262. loginParams["session_id"] = user.CurrentAgent.SessionID.ToString();
  263. loginParams["secure_session_id"] = user.CurrentAgent.SecureSessionID.ToString();
  264. loginParams["firstname"] = user.FirstName;
  265. loginParams["lastname"] = user.SurName;
  266. loginParams["agent_id"] = user.ID.ToString();
  267. loginParams["circuit_code"] = (Int32)Convert.ToUInt32(response.CircuitCode);
  268. loginParams["startpos_x"] = user.CurrentAgent.Position.X.ToString();
  269. loginParams["startpos_y"] = user.CurrentAgent.Position.Y.ToString();
  270. loginParams["startpos_z"] = user.CurrentAgent.Position.Z.ToString();
  271. loginParams["regionhandle"] = user.CurrentAgent.Handle.ToString();
  272. loginParams["caps_path"] = capsPath;
  273. // Get appearance
  274. AvatarAppearance appearance = m_userManager.GetUserAppearance(user.ID);
  275. if (appearance != null)
  276. {
  277. loginParams["appearance"] = appearance.ToHashTable();
  278. m_log.DebugFormat("[LOGIN]: Found appearance for {0} {1}", user.FirstName, user.SurName);
  279. }
  280. else
  281. {
  282. m_log.DebugFormat("[LOGIN]: Appearance not for {0} {1}. Creating default.", user.FirstName, user.SurName);
  283. appearance = new AvatarAppearance(user.ID);
  284. }
  285. ArrayList SendParams = new ArrayList();
  286. SendParams.Add(loginParams);
  287. // Send
  288. XmlRpcRequest GridReq = new XmlRpcRequest("expect_user", SendParams);
  289. XmlRpcResponse GridResp = GridReq.Send(regionInfo.httpServerURI, 6000);
  290. if (!GridResp.IsFault)
  291. {
  292. bool responseSuccess = true;
  293. if (GridResp.Value != null)
  294. {
  295. Hashtable resp = (Hashtable)GridResp.Value;
  296. if (resp.ContainsKey("success"))
  297. {
  298. if ((string)resp["success"] == "FALSE")
  299. {
  300. responseSuccess = false;
  301. }
  302. }
  303. }
  304. if (responseSuccess)
  305. {
  306. handlerUserLoggedInAtLocation = OnUserLoggedInAtLocation;
  307. if (handlerUserLoggedInAtLocation != null)
  308. {
  309. handlerUserLoggedInAtLocation(user.ID, user.CurrentAgent.SessionID,
  310. user.CurrentAgent.Region,
  311. user.CurrentAgent.Handle,
  312. user.CurrentAgent.Position.X,
  313. user.CurrentAgent.Position.Y,
  314. user.CurrentAgent.Position.Z,
  315. user.FirstName, user.SurName);
  316. }
  317. }
  318. else
  319. {
  320. m_log.ErrorFormat("[LOGIN]: Region responded that it is not available to receive clients");
  321. return false;
  322. }
  323. }
  324. else
  325. {
  326. m_log.ErrorFormat("[LOGIN]: XmlRpc request to region failed with message {0}, code {1} ", GridResp.FaultString, GridResp.FaultCode);
  327. return false;
  328. }
  329. }
  330. catch (Exception e)
  331. {
  332. m_log.ErrorFormat("[LOGIN]: Region not available for login, {0}", e);
  333. return false;
  334. }
  335. return true;
  336. }
  337. public XmlRpcResponse XmlRPCSetLoginParams(XmlRpcRequest request, IPEndPoint remoteClient)
  338. {
  339. XmlRpcResponse response = new XmlRpcResponse();
  340. Hashtable requestData = (Hashtable)request.Params[0];
  341. UserProfileData userProfile;
  342. Hashtable responseData = new Hashtable();
  343. UUID uid;
  344. string pass = requestData["password"].ToString();
  345. if (!UUID.TryParse((string)requestData["avatar_uuid"], out uid))
  346. {
  347. responseData["error"] = "No authorization";
  348. response.Value = responseData;
  349. return response;
  350. }
  351. userProfile = m_userManager.GetUserProfile(uid);
  352. if (userProfile == null ||
  353. (!AuthenticateUser(userProfile, pass)) ||
  354. userProfile.GodLevel < 200)
  355. {
  356. responseData["error"] = "No authorization";
  357. response.Value = responseData;
  358. return response;
  359. }
  360. if (requestData.ContainsKey("login_level"))
  361. {
  362. m_minLoginLevel = Convert.ToInt32(requestData["login_level"]);
  363. }
  364. if (requestData.ContainsKey("login_motd"))
  365. {
  366. m_welcomeMessage = requestData["login_motd"].ToString();
  367. }
  368. response.Value = responseData;
  369. return response;
  370. }
  371. }
  372. }