aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Servers/LoginServer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Servers/LoginServer.cs')
-rw-r--r--Servers/LoginServer.cs305
1 files changed, 305 insertions, 0 deletions
diff --git a/Servers/LoginServer.cs b/Servers/LoginServer.cs
new file mode 100644
index 0000000..e0c090a
--- /dev/null
+++ b/Servers/LoginServer.cs
@@ -0,0 +1,305 @@
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
28using Nwc.XmlRpc;
29using System;
30using System.IO;
31using System.Net;
32using System.Net.Sockets;
33using System.Text;
34using System.Text.RegularExpressions;
35using System.Threading;
36using System.Collections;
37using System.Security.Cryptography;
38using System.Xml;
39using libsecondlife;
40using OpenSim;
41using OpenSim.Framework.Interfaces;
42using OpenSim.Framework.Grid;
43using OpenSim.Framework.Inventory;
44using OpenSim.Framework.User;
45using OpenSim.Framework.Utilities;
46
47namespace OpenSim.UserServer
48{
49
50 /// <summary>
51 /// When running in local (default) mode , handles client logins.
52 /// </summary>
53 public class LoginServer : LoginService, IUserServer
54 {
55 private IGridServer m_gridServer;
56 public IPAddress clientAddress = IPAddress.Loopback;
57 public IPAddress remoteAddress = IPAddress.Any;
58 private int NumClients;
59 private string _defaultResponse;
60 private bool userAccounts = false;
61 private string _mpasswd;
62 private bool _needPasswd = false;
63 private LocalUserProfileManager userManager;
64 private int m_simPort;
65 private string m_simAddr;
66
67 public LocalUserProfileManager LocalUserManager
68 {
69 get
70 {
71 return userManager;
72 }
73 }
74
75 public LoginServer(IGridServer gridServer, string simAddr, int simPort, bool useAccounts)
76 {
77 m_gridServer = gridServer;
78 m_simPort = simPort;
79 m_simAddr = simAddr;
80 this.userAccounts = useAccounts;
81 }
82
83 public void Startup()
84 {
85 this._needPasswd = false;
86 //read in default response string
87 StreamReader SR;
88 string lines;
89 SR = File.OpenText("new-login.dat");
90
91 while (!SR.EndOfStream)
92 {
93 lines = SR.ReadLine();
94 _defaultResponse += lines;
95 }
96 SR.Close();
97 this._mpasswd = EncodePassword("testpass");
98
99 userManager = new LocalUserProfileManager(this.m_gridServer, m_simPort, m_simAddr);
100 userManager.InitUserProfiles();
101 userManager.SetKeys("", "", "", "Welcome to OpenSim");
102 }
103
104 public XmlRpcResponse XmlRpcLoginMethod(XmlRpcRequest request)
105 {
106 Console.WriteLine("login attempt");
107 Hashtable requestData = (Hashtable)request.Params[0];
108 string first;
109 string last;
110 string passwd;
111 LLUUID Agent;
112 LLUUID Session;
113
114 XmlRpcResponse response = new XmlRpcResponse();
115
116 //get login name
117 if (requestData.Contains("first"))
118 {
119 first = (string)requestData["first"];
120 }
121 else
122 {
123 first = "test";
124 }
125
126 if (requestData.Contains("last"))
127 {
128 last = (string)requestData["last"];
129 }
130 else
131 {
132 last = "User" + NumClients.ToString();
133 }
134
135 if (requestData.Contains("passwd"))
136 {
137 passwd = (string)requestData["passwd"];
138 }
139 else
140 {
141 passwd = "notfound";
142 }
143
144 if (!Authenticate(first, last, passwd))
145 {
146 Hashtable loginError = new Hashtable();
147 loginError["reason"] = "key"; ;
148 loginError["message"] = "You have entered an invalid name/password combination. Check Caps/lock.";
149 loginError["login"] = "false";
150 response.Value = loginError;
151 }
152
153 NumClients++;
154
155 //create a agent and session LLUUID
156 Agent = GetAgentId(first, last);
157 int SessionRand = Util.RandomClass.Next(1, 999);
158 Session = new LLUUID("aaaabbbb-0200-" + SessionRand.ToString("0000") + "-8664-58f53e442797");
159 LLUUID secureSess = LLUUID.Random();
160 //create some login info
161 Hashtable LoginFlagsHash = new Hashtable();
162 LoginFlagsHash["daylight_savings"] = "N";
163 LoginFlagsHash["stipend_since_login"] = "N";
164 LoginFlagsHash["gendered"] = "Y";
165 LoginFlagsHash["ever_logged_in"] = "Y";
166 ArrayList LoginFlags = new ArrayList();
167 LoginFlags.Add(LoginFlagsHash);
168
169 Hashtable GlobalT = new Hashtable();
170 GlobalT["sun_texture_id"] = "cce0f112-878f-4586-a2e2-a8f104bba271";
171 GlobalT["cloud_texture_id"] = "fc4b9f0b-d008-45c6-96a4-01dd947ac621";
172 GlobalT["moon_texture_id"] = "fc4b9f0b-d008-45c6-96a4-01dd947ac621";
173 ArrayList GlobalTextures = new ArrayList();
174 GlobalTextures.Add(GlobalT);
175
176 response = (XmlRpcResponse)(new XmlRpcResponseDeserializer()).Deserialize(this._defaultResponse);
177 Hashtable responseData = (Hashtable)response.Value;
178
179 responseData["sim_port"] = m_simPort;
180 responseData["sim_ip"] = m_simAddr;
181 responseData["agent_id"] = Agent.ToStringHyphenated();
182 responseData["session_id"] = Session.ToStringHyphenated();
183 responseData["secure_session_id"] = secureSess.ToStringHyphenated();
184 responseData["circuit_code"] = (Int32)(Util.RandomClass.Next());
185 responseData["seconds_since_epoch"] = (Int32)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
186 responseData["login-flags"] = LoginFlags;
187 responseData["global-textures"] = GlobalTextures;
188
189 //inventory
190 ArrayList InventoryList = (ArrayList)responseData["inventory-skeleton"];
191 Hashtable Inventory1 = (Hashtable)InventoryList[0];
192 Hashtable Inventory2 = (Hashtable)InventoryList[1];
193 LLUUID BaseFolderID = LLUUID.Random();
194 LLUUID InventoryFolderID = LLUUID.Random();
195 Inventory2["name"] = "Textures";
196 Inventory2["folder_id"] = BaseFolderID.ToStringHyphenated();
197 Inventory2["type_default"] = 0;
198 Inventory1["folder_id"] = InventoryFolderID.ToStringHyphenated();
199
200 ArrayList InventoryRoot = (ArrayList)responseData["inventory-root"];
201 Hashtable Inventoryroot = (Hashtable)InventoryRoot[0];
202 Inventoryroot["folder_id"] = InventoryFolderID.ToStringHyphenated();
203
204 CustomiseLoginResponse(responseData, first, last);
205
206 Login _login = new Login();
207 //copy data to login object
208 _login.First = first;
209 _login.Last = last;
210 _login.Agent = Agent;
211 _login.Session = Session;
212 _login.SecureSession = secureSess;
213 _login.BaseFolder = BaseFolderID;
214 _login.InventoryFolder = InventoryFolderID;
215
216 //working on local computer if so lets add to the gridserver's list of sessions?
217 if (m_gridServer.GetName() == "Local")
218 {
219 ((LocalGridBase)m_gridServer).AddNewSession(_login);
220 }
221
222 return response;
223 }
224
225 protected virtual void CustomiseLoginResponse(Hashtable responseData, string first, string last)
226 {
227 }
228
229 protected virtual LLUUID GetAgentId(string firstName, string lastName)
230 {
231 LLUUID Agent;
232 int AgentRand = Util.RandomClass.Next(1, 9999);
233 Agent = new LLUUID("99998888-0100-" + AgentRand.ToString("0000") + "-8ec1-0b1d5cd6aead");
234 return Agent;
235 }
236
237 protected virtual bool Authenticate(string first, string last, string passwd)
238 {
239 if (this._needPasswd)
240 {
241 //every user needs the password to login
242 string encodedPass = passwd.Remove(0, 3); //remove $1$
243 if (encodedPass == this._mpasswd)
244 {
245 return true;
246 }
247 else
248 {
249 return false;
250 }
251 }
252 else
253 {
254 //do not need password to login
255 return true;
256 }
257 }
258
259 private static string EncodePassword(string passwd)
260 {
261 Byte[] originalBytes;
262 Byte[] encodedBytes;
263 MD5 md5;
264
265 md5 = new MD5CryptoServiceProvider();
266 originalBytes = ASCIIEncoding.Default.GetBytes(passwd);
267 encodedBytes = md5.ComputeHash(originalBytes);
268
269 return Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
270 }
271
272 public bool CreateUserAccount(string firstName, string lastName, string password)
273 {
274 Console.WriteLine("creating new user account");
275 string mdPassword = EncodePassword(password);
276 Console.WriteLine("with password: " + mdPassword);
277 this.userManager.CreateNewProfile(firstName, lastName, mdPassword);
278 return true;
279 }
280
281 //IUserServer implementation
282 public AgentInventory RequestAgentsInventory(LLUUID agentID)
283 {
284 AgentInventory aInventory = null;
285 if (this.userAccounts)
286 {
287 aInventory = this.userManager.GetUsersInventory(agentID);
288 }
289
290 return aInventory;
291 }
292
293 public bool UpdateAgentsInventory(LLUUID agentID, AgentInventory inventory)
294 {
295 return true;
296 }
297
298 public void SetServerInfo(string ServerUrl, string SendKey, string RecvKey)
299 {
300
301 }
302 }
303
304
305}