LLStandaloneLoginService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. protected override bool PrepareLoginToRegion(
  122. RegionInfo regionInfo, UserProfileData user, LoginResponse response, IPEndPoint remoteClient)
  123. {
  124. IPEndPoint endPoint = regionInfo.ExternalEndPoint;
  125. response.SimAddress = endPoint.Address.ToString();
  126. response.SimPort = (uint)endPoint.Port;
  127. response.RegionX = regionInfo.RegionLocX;
  128. response.RegionY = regionInfo.RegionLocY;
  129. string capsPath = CapsUtil.GetRandomCapsObjectPath();
  130. string capsSeedPath = CapsUtil.GetCapsSeedPath(capsPath);
  131. // Don't use the following! It Fails for logging into any region not on the same port as the http server!
  132. // Kept here so it doesn't happen again!
  133. // response.SeedCapability = regionInfo.ServerURI + capsSeedPath;
  134. string seedcap = "http://";
  135. if (m_serversInfo.HttpUsesSSL)
  136. {
  137. // For NAT
  138. string host = NetworkUtil.GetHostFor(remoteClient.Address, m_serversInfo.HttpSSLCN);
  139. seedcap = "https://" + host + ":" + m_serversInfo.httpSSLPort + capsSeedPath;
  140. }
  141. else
  142. {
  143. // For NAT
  144. string host = NetworkUtil.GetHostFor(remoteClient.Address, regionInfo.ExternalHostName);
  145. seedcap = "http://" + host + ":" + m_serversInfo.HttpListenerPort + capsSeedPath;
  146. }
  147. response.SeedCapability = seedcap;
  148. // Notify the target of an incoming user
  149. m_log.InfoFormat(
  150. "[LOGIN]: Telling {0} @ {1},{2} ({3}) to prepare for client connection",
  151. regionInfo.RegionName, response.RegionX, response.RegionY, regionInfo.ServerURI);
  152. // Update agent with target sim
  153. user.CurrentAgent.Region = regionInfo.RegionID;
  154. user.CurrentAgent.Handle = regionInfo.RegionHandle;
  155. AgentCircuitData agent = new AgentCircuitData();
  156. agent.AgentID = user.ID;
  157. agent.firstname = user.FirstName;
  158. agent.lastname = user.SurName;
  159. agent.SessionID = user.CurrentAgent.SessionID;
  160. agent.SecureSessionID = user.CurrentAgent.SecureSessionID;
  161. agent.circuitcode = Convert.ToUInt32(response.CircuitCode);
  162. agent.BaseFolder = UUID.Zero;
  163. agent.InventoryFolder = UUID.Zero;
  164. agent.startpos = user.CurrentAgent.Position;
  165. agent.CapsPath = capsPath;
  166. agent.Appearance = m_userManager.GetUserAppearance(user.ID);
  167. if (agent.Appearance == null)
  168. {
  169. m_log.WarnFormat(
  170. "[INTER]: Appearance not found for {0} {1}. Creating default.", agent.firstname, agent.lastname);
  171. agent.Appearance = new AvatarAppearance(agent.AgentID);
  172. }
  173. if (m_regionsConnector.RegionLoginsEnabled)
  174. {
  175. string reason;
  176. bool success = m_regionsConnector.NewUserConnection(regionInfo.RegionHandle, agent, out reason);
  177. if (!success)
  178. {
  179. response.ErrorReason = "key";
  180. response.ErrorMessage = reason;
  181. }
  182. return success;
  183. // return m_regionsConnector.NewUserConnection(regionInfo.RegionHandle, agent, out reason);
  184. }
  185. return false;
  186. }
  187. public override void LogOffUser(UserProfileData theUser, string message)
  188. {
  189. RegionInfo SimInfo;
  190. try
  191. {
  192. SimInfo = this.m_regionsConnector.RequestNeighbourInfo(theUser.CurrentAgent.Handle);
  193. if (SimInfo == null)
  194. {
  195. m_log.Error("[LOCAL LOGIN]: Region user was in isn't currently logged in");
  196. return;
  197. }
  198. }
  199. catch (Exception)
  200. {
  201. m_log.Error("[LOCAL LOGIN]: Unable to look up region to log user off");
  202. return;
  203. }
  204. m_regionsConnector.LogOffUserFromGrid(
  205. SimInfo.RegionHandle, theUser.ID, theUser.CurrentAgent.SecureSessionID, "Logging you off");
  206. }
  207. }
  208. }