LocalLoginService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 libsecondlife;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Communications.Cache;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Statistics;
  35. using OpenSim.Framework.UserManagement;
  36. using InventoryFolder=OpenSim.Framework.InventoryFolder;
  37. namespace OpenSim.Region.Communications.Local
  38. {
  39. public delegate void LoginToRegionEvent(ulong regionHandle, Login login);
  40. public class LocalLoginService : LoginService
  41. {
  42. private static readonly log4net.ILog m_log
  43. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  44. private CommunicationsLocal m_Parent;
  45. private NetworkServersInfo serversInfo;
  46. private uint defaultHomeX;
  47. private uint defaultHomeY;
  48. private bool authUsers = false;
  49. public event LoginToRegionEvent OnLoginToRegion;
  50. private LoginToRegionEvent handlerLoginToRegion = null; // OnLoginToRegion;
  51. public LocalLoginService(UserManagerBase userManager, string welcomeMess,
  52. CommunicationsLocal parent, NetworkServersInfo serversInfo,
  53. bool authenticate)
  54. : base(userManager, parent.UserProfileCacheService.libraryRoot, welcomeMess)
  55. {
  56. m_Parent = parent;
  57. this.serversInfo = serversInfo;
  58. defaultHomeX = this.serversInfo.DefaultHomeLocX;
  59. defaultHomeY = this.serversInfo.DefaultHomeLocY;
  60. authUsers = authenticate;
  61. }
  62. public override UserProfileData GetTheUser(string firstname, string lastname)
  63. {
  64. UserProfileData profile = m_userManager.GetUserProfile(firstname, lastname);
  65. if (profile != null)
  66. {
  67. return profile;
  68. }
  69. if (!authUsers)
  70. {
  71. //no current user account so make one
  72. m_log.Info("[LOGIN]: No user account found so creating a new one.");
  73. m_userManager.AddUserProfile(firstname, lastname, "test", defaultHomeX, defaultHomeY);
  74. profile = m_userManager.GetUserProfile(firstname, lastname);
  75. if (profile != null)
  76. {
  77. m_Parent.InventoryService.CreateNewUserInventory(profile.UUID);
  78. }
  79. return profile;
  80. }
  81. return null;
  82. }
  83. public override bool AuthenticateUser(UserProfileData profile, string password)
  84. {
  85. if (!authUsers)
  86. {
  87. //for now we will accept any password in sandbox mode
  88. m_log.Info("[LOGIN]: Authorising user (no actual password check)");
  89. return true;
  90. }
  91. else
  92. {
  93. m_log.Info(
  94. "[LOGIN]: Authenticating " + profile.username + " " + profile.surname);
  95. if (!password.StartsWith("$1$"))
  96. password = "$1$" + Util.Md5Hash(password);
  97. password = password.Remove(0, 3); //remove $1$
  98. string s = Util.Md5Hash(password + ":" + profile.passwordSalt);
  99. bool loginresult = (profile.passwordHash.Equals(s.ToString(), StringComparison.InvariantCultureIgnoreCase)
  100. || profile.passwordHash.Equals(password, StringComparison.InvariantCultureIgnoreCase));
  101. return loginresult;
  102. }
  103. }
  104. public override void CustomiseResponse(LoginResponse response, UserProfileData theUser, string startLocationRequest)
  105. {
  106. ulong currentRegion = 0;
  107. if (startLocationRequest == "last")
  108. {
  109. currentRegion = theUser.currentAgent.currentHandle;
  110. }
  111. else if (startLocationRequest == "home")
  112. {
  113. currentRegion = theUser.homeRegion;
  114. }
  115. else
  116. {
  117. m_log.Info("[LOGIN]: Got Custom Login URL, but can't process it");
  118. // LocalBackEndServices can't possibly look up a region by name :(
  119. // TODO: Parse string in the following format: 'uri:RegionName&X&Y&Z'
  120. currentRegion = theUser.currentAgent.currentHandle;
  121. }
  122. RegionInfo reg = m_Parent.GridService.RequestNeighbourInfo(currentRegion);
  123. if (reg != null)
  124. {
  125. response.Home = "{'region_handle':[r" + (reg.RegionLocX * Constants.RegionSize).ToString() + ",r" +
  126. (reg.RegionLocY * Constants.RegionSize).ToString() + "], " +
  127. "'position':[r" + theUser.homeLocation.X.ToString() + ",r" +
  128. theUser.homeLocation.Y.ToString() + ",r" + theUser.homeLocation.Z.ToString() + "], " +
  129. "'look_at':[r" + theUser.homeLocation.X.ToString() + ",r" +
  130. theUser.homeLocation.Y.ToString() + ",r" + theUser.homeLocation.Z.ToString() + "]}";
  131. string capsPath = Util.GetRandomCapsPath();
  132. response.SimAddress = reg.ExternalEndPoint.Address.ToString();
  133. response.SimPort = (uint) reg.ExternalEndPoint.Port;
  134. response.RegionX = reg.RegionLocX;
  135. response.RegionY = reg.RegionLocY ;
  136. response.SeedCapability = "http://" + reg.ExternalHostName + ":" +
  137. serversInfo.HttpListenerPort.ToString() + "/CAPS/" + capsPath + "0000/";
  138. m_log.DebugFormat(
  139. "[CAPS]: Sending new CAPS seed url {0} to client {1}",
  140. response.SeedCapability, response.AgentID);
  141. theUser.currentAgent.currentRegion = reg.RegionID;
  142. theUser.currentAgent.currentHandle = reg.RegionHandle;
  143. LoginResponse.BuddyList buddyList = new LoginResponse.BuddyList();
  144. response.BuddList = ConvertFriendListItem(m_userManager.GetUserFriendList(theUser.UUID));
  145. Login _login = new Login();
  146. //copy data to login object
  147. _login.First = response.Firstname;
  148. _login.Last = response.Lastname;
  149. _login.Agent = response.AgentID;
  150. _login.Session = response.SessionID;
  151. _login.SecureSession = response.SecureSessionID;
  152. _login.CircuitCode = (uint) response.CircuitCode;
  153. _login.StartPos = new LLVector3(128, 128, 70);
  154. _login.CapsPath = capsPath;
  155. m_log.InfoFormat(
  156. "[LOGIN]: Telling region {0} @ {1},{2} ({3}:{4}) to expect user connection",
  157. reg.RegionName, response.RegionX, response.RegionY, response.SimAddress, response.SimPort);
  158. handlerLoginToRegion = OnLoginToRegion;
  159. if (handlerLoginToRegion != null)
  160. {
  161. handlerLoginToRegion(currentRegion, _login);
  162. }
  163. }
  164. else
  165. {
  166. m_log.Warn("[LOGIN]: Not found region " + currentRegion);
  167. }
  168. }
  169. private LoginResponse.BuddyList ConvertFriendListItem(List<FriendListItem> LFL)
  170. {
  171. LoginResponse.BuddyList buddylistreturn = new LoginResponse.BuddyList();
  172. foreach (FriendListItem fl in LFL)
  173. {
  174. LoginResponse.BuddyList.BuddyInfo buddyitem = new LoginResponse.BuddyList.BuddyInfo(fl.Friend);
  175. buddyitem.BuddyID = fl.Friend;
  176. buddyitem.BuddyRightsHave = (int)fl.FriendListOwnerPerms;
  177. buddyitem.BuddyRightsGiven = (int)fl.FriendPerms;
  178. buddylistreturn.AddNewBuddy(buddyitem);
  179. }
  180. return buddylistreturn;
  181. }
  182. // See LoginService
  183. protected override InventoryData GetInventorySkeleton(LLUUID userID)
  184. {
  185. List<InventoryFolderBase> folders = m_Parent.InventoryService.GetInventorySkeleton(userID);
  186. if (folders.Count > 0)
  187. {
  188. LLUUID rootID = LLUUID.Zero;
  189. ArrayList AgentInventoryArray = new ArrayList();
  190. Hashtable TempHash;
  191. foreach (InventoryFolderBase InvFolder in folders)
  192. {
  193. if (InvFolder.parentID == LLUUID.Zero)
  194. {
  195. rootID = InvFolder.folderID;
  196. }
  197. TempHash = new Hashtable();
  198. TempHash["name"] = InvFolder.name;
  199. TempHash["parent_id"] = InvFolder.parentID.ToString();
  200. TempHash["version"] = (Int32) InvFolder.version;
  201. TempHash["type_default"] = (Int32) InvFolder.type;
  202. TempHash["folder_id"] = InvFolder.folderID.ToString();
  203. AgentInventoryArray.Add(TempHash);
  204. }
  205. return new InventoryData(AgentInventoryArray, rootID);
  206. }
  207. else
  208. {
  209. AgentInventory userInventory = new AgentInventory();
  210. userInventory.CreateRootFolder(userID);
  211. ArrayList AgentInventoryArray = new ArrayList();
  212. Hashtable TempHash;
  213. foreach (InventoryFolder InvFolder in userInventory.InventoryFolders.Values)
  214. {
  215. TempHash = new Hashtable();
  216. TempHash["name"] = InvFolder.FolderName;
  217. TempHash["parent_id"] = InvFolder.ParentID.ToString();
  218. TempHash["version"] = (Int32) InvFolder.Version;
  219. TempHash["type_default"] = (Int32) InvFolder.DefaultType;
  220. TempHash["folder_id"] = InvFolder.FolderID.ToString();
  221. AgentInventoryArray.Add(TempHash);
  222. }
  223. return new InventoryData(AgentInventoryArray, userInventory.InventoryRoot.FolderID);
  224. }
  225. }
  226. }
  227. }