LLStandaloneLoginService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Communications.Services;
  39. using OpenSim.Framework.Communications.Cache;
  40. using OpenSim.Framework.Capabilities;
  41. using OpenSim.Framework.Servers;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Region.Framework.Interfaces;
  44. using OpenSim.Services.Interfaces;
  45. namespace OpenSim.Client.Linden
  46. {
  47. public class LLStandaloneLoginService : LoginService
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. protected NetworkServersInfo m_serversInfo;
  51. protected bool m_authUsers = false;
  52. /// <summary>
  53. /// Used to make requests to the local regions.
  54. /// </summary>
  55. protected ILoginServiceToRegionsConnector m_regionsConnector;
  56. public LLStandaloneLoginService(
  57. UserManagerBase userManager, string welcomeMess,
  58. IInventoryService interServiceInventoryService,
  59. NetworkServersInfo serversInfo,
  60. bool authenticate, LibraryRootFolder libraryRootFolder, ILoginServiceToRegionsConnector regionsConnector)
  61. : base(userManager, libraryRootFolder, welcomeMess)
  62. {
  63. this.m_serversInfo = serversInfo;
  64. m_defaultHomeX = this.m_serversInfo.DefaultHomeLocX;
  65. m_defaultHomeY = this.m_serversInfo.DefaultHomeLocY;
  66. m_authUsers = authenticate;
  67. m_InventoryService = interServiceInventoryService;
  68. m_regionsConnector = regionsConnector;
  69. // Standard behavior: In StandAlone, silent logout of last hung session
  70. m_warn_already_logged = false;
  71. }
  72. public override UserProfileData GetTheUser(string firstname, string lastname)
  73. {
  74. UserProfileData profile = m_userManager.GetUserProfile(firstname, lastname);
  75. if (profile != null)
  76. {
  77. return profile;
  78. }
  79. if (!m_authUsers)
  80. {
  81. //no current user account so make one
  82. m_log.Info("[LOGIN]: No user account found so creating a new one.");
  83. m_userManager.AddUser(firstname, lastname, "test", "", m_defaultHomeX, m_defaultHomeY);
  84. return m_userManager.GetUserProfile(firstname, lastname);
  85. }
  86. return null;
  87. }
  88. public override bool AuthenticateUser(UserProfileData profile, string password)
  89. {
  90. if (!m_authUsers)
  91. {
  92. //for now we will accept any password in sandbox mode
  93. m_log.Info("[LOGIN]: Authorising user (no actual password check)");
  94. return true;
  95. }
  96. else
  97. {
  98. m_log.Info(
  99. "[LOGIN]: Authenticating " + profile.FirstName + " " + profile.SurName);
  100. if (!password.StartsWith("$1$"))
  101. password = "$1$" + Util.Md5Hash(password);
  102. password = password.Remove(0, 3); //remove $1$
  103. string s = Util.Md5Hash(password + ":" + profile.PasswordSalt);
  104. bool loginresult = (profile.PasswordHash.Equals(s.ToString(), StringComparison.InvariantCultureIgnoreCase)
  105. || profile.PasswordHash.Equals(password, StringComparison.InvariantCulture));
  106. return loginresult;
  107. }
  108. }
  109. protected override RegionInfo RequestClosestRegion(string region)
  110. {
  111. return m_regionsConnector.RequestClosestRegion(region);
  112. }
  113. protected override RegionInfo GetRegionInfo(ulong homeRegionHandle)
  114. {
  115. return m_regionsConnector.RequestNeighbourInfo(homeRegionHandle);
  116. }
  117. protected override RegionInfo GetRegionInfo(UUID homeRegionId)
  118. {
  119. return m_regionsConnector.RequestNeighbourInfo(homeRegionId);
  120. }
  121. /// <summary>
  122. /// Prepare a login to the given region. This involves both telling the region to expect a connection
  123. /// and appropriately customising the response to the user.
  124. /// </summary>
  125. /// <param name="sim"></param>
  126. /// <param name="user"></param>
  127. /// <param name="response"></param>
  128. /// <returns>true if the region was successfully contacted, false otherwise</returns>
  129. protected override bool PrepareLoginToRegion(RegionInfo regionInfo, UserProfileData user, LoginResponse response, IPEndPoint remoteClient)
  130. {
  131. IPEndPoint endPoint = regionInfo.ExternalEndPoint;
  132. response.SimAddress = endPoint.Address.ToString();
  133. response.SimPort = (uint)endPoint.Port;
  134. response.RegionX = regionInfo.RegionLocX;
  135. response.RegionY = regionInfo.RegionLocY;
  136. string capsPath = CapsUtil.GetRandomCapsObjectPath();
  137. string capsSeedPath = CapsUtil.GetCapsSeedPath(capsPath);
  138. // Don't use the following! It Fails for logging into any region not on the same port as the http server!
  139. // Kept here so it doesn't happen again!
  140. // response.SeedCapability = regionInfo.ServerURI + capsSeedPath;
  141. string seedcap = "http://";
  142. if (m_serversInfo.HttpUsesSSL)
  143. {
  144. // For NAT
  145. string host = NetworkUtil.GetHostFor(remoteClient.Address, m_serversInfo.HttpSSLCN);
  146. seedcap = "https://" + host + ":" + m_serversInfo.httpSSLPort + capsSeedPath;
  147. }
  148. else
  149. {
  150. // For NAT
  151. string host = NetworkUtil.GetHostFor(remoteClient.Address, regionInfo.ExternalHostName);
  152. seedcap = "http://" + host + ":" + m_serversInfo.HttpListenerPort + capsSeedPath;
  153. }
  154. response.SeedCapability = seedcap;
  155. // Notify the target of an incoming user
  156. m_log.InfoFormat(
  157. "[LOGIN]: Telling {0} @ {1},{2} ({3}) to prepare for client connection",
  158. regionInfo.RegionName, response.RegionX, response.RegionY, regionInfo.ServerURI);
  159. // Update agent with target sim
  160. user.CurrentAgent.Region = regionInfo.RegionID;
  161. user.CurrentAgent.Handle = regionInfo.RegionHandle;
  162. AgentCircuitData agent = new AgentCircuitData();
  163. agent.AgentID = user.ID;
  164. agent.firstname = user.FirstName;
  165. agent.lastname = user.SurName;
  166. agent.SessionID = user.CurrentAgent.SessionID;
  167. agent.SecureSessionID = user.CurrentAgent.SecureSessionID;
  168. agent.circuitcode = Convert.ToUInt32(response.CircuitCode);
  169. agent.BaseFolder = UUID.Zero;
  170. agent.InventoryFolder = UUID.Zero;
  171. agent.startpos = user.CurrentAgent.Position;
  172. agent.CapsPath = capsPath;
  173. agent.Appearance = m_userManager.GetUserAppearance(user.ID);
  174. if (agent.Appearance == null)
  175. {
  176. m_log.WarnFormat("[INTER]: Appearance not found for {0} {1}. Creating default.", agent.firstname, agent.lastname);
  177. agent.Appearance = new AvatarAppearance(agent.AgentID);
  178. }
  179. if (m_regionsConnector.RegionLoginsEnabled)
  180. {
  181. string reason;
  182. bool success = m_regionsConnector.NewUserConnection(regionInfo.RegionHandle, agent, out reason);
  183. if (!success)
  184. {
  185. response.ErrorReason = "key";
  186. response.ErrorMessage = reason;
  187. }
  188. return success;
  189. // return m_regionsConnector.NewUserConnection(regionInfo.RegionHandle, agent, out reason);
  190. }
  191. return false;
  192. }
  193. public override void LogOffUser(UserProfileData theUser, string message)
  194. {
  195. RegionInfo SimInfo;
  196. try
  197. {
  198. SimInfo = this.m_regionsConnector.RequestNeighbourInfo(theUser.CurrentAgent.Handle);
  199. if (SimInfo == null)
  200. {
  201. m_log.Error("[LOCAL LOGIN]: Region user was in isn't currently logged in");
  202. return;
  203. }
  204. }
  205. catch (Exception)
  206. {
  207. m_log.Error("[LOCAL LOGIN]: Unable to look up region to log user off");
  208. return;
  209. }
  210. m_regionsConnector.LogOffUserFromGrid(SimInfo.RegionHandle, theUser.ID, theUser.CurrentAgent.SecureSessionID, "Logging you off");
  211. }
  212. }
  213. }