LoginServer.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using Nwc.XmlRpc;
  28. using System;
  29. using System.IO;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Text;
  33. using System.Text.RegularExpressions;
  34. using System.Threading;
  35. using System.Collections;
  36. using System.Security.Cryptography;
  37. using System.Xml;
  38. using libsecondlife;
  39. using OpenSim;
  40. using OpenSim.Framework.Interfaces;
  41. using OpenSim.Framework.Grid;
  42. using OpenSim.Framework.Inventory;
  43. using OpenSim.Framework.User;
  44. using OpenSim.Framework.Utilities;
  45. using OpenSim.Framework.Types;
  46. namespace OpenSim.UserServer
  47. {
  48. /// <summary>
  49. /// When running in local (default) mode , handles client logins.
  50. /// </summary>
  51. public class LoginServer : LoginService, IUserServer
  52. {
  53. private IGridServer m_gridServer;
  54. public IPAddress clientAddress = IPAddress.Loopback;
  55. public IPAddress remoteAddress = IPAddress.Any;
  56. private int NumClients;
  57. private bool userAccounts = false;
  58. private string _mpasswd;
  59. private bool _needPasswd = false;
  60. private LocalUserProfileManager userManager;
  61. private int m_simPort;
  62. private string m_simAddr;
  63. public LocalUserProfileManager LocalUserManager
  64. {
  65. get
  66. {
  67. return userManager;
  68. }
  69. }
  70. public LoginServer(IGridServer gridServer, string simAddr, int simPort, bool useAccounts)
  71. {
  72. m_gridServer = gridServer;
  73. m_simPort = simPort;
  74. m_simAddr = simAddr;
  75. this.userAccounts = useAccounts;
  76. }
  77. public void Startup()
  78. {
  79. this._needPasswd = false;
  80. // read in default response string
  81. /* StreamReader SR;
  82. string lines;
  83. SR = File.OpenText("new-login.dat");
  84. while (!SR.EndOfStream)
  85. {
  86. lines = SR.ReadLine();
  87. _defaultResponse += lines;
  88. }
  89. SR.Close();
  90. * */
  91. this._mpasswd = EncodePassword("testpass");
  92. userManager = new LocalUserProfileManager(this.m_gridServer, m_simPort, m_simAddr);
  93. //userManager.InitUserProfiles();
  94. userManager.SetKeys("", "", "", "Welcome to OpenSim");
  95. }
  96. public XmlRpcResponse XmlRpcLoginMethod(XmlRpcRequest request)
  97. {
  98. Console.WriteLine("login attempt");
  99. Hashtable requestData = (Hashtable)request.Params[0];
  100. string first;
  101. string last;
  102. string passwd;
  103. LLUUID Agent;
  104. LLUUID Session;
  105. LoginResponse loginResponse = new LoginResponse();
  106. //get login name
  107. if (requestData.Contains("first"))
  108. {
  109. first = (string)requestData["first"];
  110. }
  111. else
  112. {
  113. first = "test";
  114. }
  115. if (requestData.Contains("last"))
  116. {
  117. last = (string)requestData["last"];
  118. }
  119. else
  120. {
  121. last = "User" + NumClients.ToString();
  122. }
  123. if (requestData.Contains("passwd"))
  124. {
  125. passwd = (string)requestData["passwd"];
  126. }
  127. else
  128. {
  129. passwd = "notfound";
  130. }
  131. if (!Authenticate(first, last, passwd))
  132. {
  133. return loginResponse.LoginFailedResponse();
  134. }
  135. NumClients++;
  136. // Create a agent and session LLUUID
  137. Agent = GetAgentId(first, last);
  138. int SessionRand = Util.RandomClass.Next(1, 999);
  139. Session = new LLUUID("aaaabbbb-0200-" + SessionRand.ToString("0000") + "-8664-58f53e442797");
  140. LLUUID secureSess = LLUUID.Random();
  141. loginResponse.SimPort = m_simPort.ToString();
  142. loginResponse.SimAddress = m_simAddr.ToString();
  143. loginResponse.AgentID = Agent.ToStringHyphenated();
  144. loginResponse.SessionID = Session.ToStringHyphenated();
  145. loginResponse.SecureSessionID = secureSess.ToStringHyphenated();
  146. loginResponse.CircuitCode = (Int32)(Util.RandomClass.Next());
  147. XmlRpcResponse response = loginResponse.ToXmlRpcResponse();
  148. Hashtable responseData = (Hashtable)response.Value;
  149. // inventory
  150. ArrayList InventoryList = (ArrayList)responseData["inventory-skeleton"];
  151. Hashtable Inventory1 = (Hashtable)InventoryList[0];
  152. Hashtable Inventory2 = (Hashtable)InventoryList[1];
  153. LLUUID BaseFolderID = LLUUID.Random();
  154. LLUUID InventoryFolderID = LLUUID.Random();
  155. Inventory2["name"] = "Textures";
  156. Inventory2["folder_id"] = BaseFolderID.ToStringHyphenated();
  157. Inventory2["type_default"] = 0;
  158. Inventory1["folder_id"] = InventoryFolderID.ToStringHyphenated();
  159. ArrayList InventoryRoot = (ArrayList)responseData["inventory-root"];
  160. Hashtable Inventoryroot = (Hashtable)InventoryRoot[0];
  161. Inventoryroot["folder_id"] = InventoryFolderID.ToStringHyphenated();
  162. CustomiseLoginResponse(responseData, first, last);
  163. Login _login = new Login();
  164. //copy data to login object
  165. _login.First = first;
  166. _login.Last = last;
  167. _login.Agent = Agent;
  168. _login.Session = Session;
  169. _login.SecureSession = secureSess;
  170. _login.BaseFolder = BaseFolderID;
  171. _login.InventoryFolder = InventoryFolderID;
  172. //working on local computer if so lets add to the gridserver's list of sessions?
  173. if (m_gridServer.GetName() == "Local")
  174. {
  175. ((LocalGridBase)m_gridServer).AddNewSession(_login);
  176. }
  177. return response;
  178. }
  179. protected virtual void CustomiseLoginResponse(Hashtable responseData, string first, string last)
  180. {
  181. }
  182. protected virtual LLUUID GetAgentId(string firstName, string lastName)
  183. {
  184. LLUUID Agent;
  185. int AgentRand = Util.RandomClass.Next(1, 9999);
  186. Agent = new LLUUID("99998888-0100-" + AgentRand.ToString("0000") + "-8ec1-0b1d5cd6aead");
  187. return Agent;
  188. }
  189. protected virtual bool Authenticate(string first, string last, string passwd)
  190. {
  191. if (this._needPasswd)
  192. {
  193. //every user needs the password to login
  194. string encodedPass = passwd.Remove(0, 3); //remove $1$
  195. if (encodedPass == this._mpasswd)
  196. {
  197. return true;
  198. }
  199. else
  200. {
  201. return false;
  202. }
  203. }
  204. else
  205. {
  206. //do not need password to login
  207. return true;
  208. }
  209. }
  210. private static string EncodePassword(string passwd)
  211. {
  212. Byte[] originalBytes;
  213. Byte[] encodedBytes;
  214. MD5 md5;
  215. md5 = new MD5CryptoServiceProvider();
  216. originalBytes = ASCIIEncoding.Default.GetBytes(passwd);
  217. encodedBytes = md5.ComputeHash(originalBytes);
  218. return Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
  219. }
  220. public bool CreateUserAccount(string firstName, string lastName, string password)
  221. {
  222. Console.WriteLine("creating new user account");
  223. string mdPassword = EncodePassword(password);
  224. Console.WriteLine("with password: " + mdPassword);
  225. this.userManager.CreateNewProfile(firstName, lastName, mdPassword);
  226. return true;
  227. }
  228. //IUserServer implementation
  229. public AgentInventory RequestAgentsInventory(LLUUID agentID)
  230. {
  231. AgentInventory aInventory = null;
  232. if (this.userAccounts)
  233. {
  234. aInventory = this.userManager.GetUsersInventory(agentID);
  235. }
  236. return aInventory;
  237. }
  238. public bool UpdateAgentsInventory(LLUUID agentID, AgentInventory inventory)
  239. {
  240. return true;
  241. }
  242. public void SetServerInfo(string ServerUrl, string SendKey, string RecvKey)
  243. {
  244. }
  245. }
  246. }