LLLoginHandlers.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.IO;
  30. using System.Reflection;
  31. using System.Net;
  32. using System.Text;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Server.Handlers.Base;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using Nwc.XmlRpc;
  41. using Nini.Config;
  42. using log4net;
  43. namespace OpenSim.Server.Handlers.Login
  44. {
  45. public class LLLoginHandlers
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private ILoginService m_LocalService;
  49. private bool m_Proxy;
  50. public LLLoginHandlers(ILoginService service, bool hasProxy)
  51. {
  52. m_LocalService = service;
  53. m_Proxy = hasProxy;
  54. }
  55. public XmlRpcResponse HandleXMLRPCLogin(XmlRpcRequest request, IPEndPoint remoteClient)
  56. {
  57. Hashtable requestData = (Hashtable)request.Params[0];
  58. if (m_Proxy && request.Params[3] != null)
  59. {
  60. IPEndPoint ep = Util.GetClientIPFromXFF((string)request.Params[3]);
  61. if (ep != null)
  62. // Bang!
  63. remoteClient = ep;
  64. }
  65. if (requestData != null)
  66. {
  67. // Debug code to show exactly what login parameters the viewer is sending us.
  68. // TODO: Extract into a method that can be generally applied if one doesn't already exist.
  69. // foreach (string key in requestData.Keys)
  70. // {
  71. // object value = requestData[key];
  72. // Console.WriteLine("{0}:{1}", key, value);
  73. // if (value is ArrayList)
  74. // {
  75. // ICollection col = value as ICollection;
  76. // foreach (object item in col)
  77. // Console.WriteLine(" {0}", item);
  78. // }
  79. // }
  80. if (requestData.ContainsKey("first") && requestData["first"] != null &&
  81. requestData.ContainsKey("last") && requestData["last"] != null && (
  82. (requestData.ContainsKey("passwd") && requestData["passwd"] != null) ||
  83. (!requestData.ContainsKey("passwd") && requestData.ContainsKey("web_login_key") && requestData["web_login_key"] != null && requestData["web_login_key"].ToString() != UUID.Zero.ToString())
  84. ))
  85. {
  86. string first = requestData["first"].ToString();
  87. string last = requestData["last"].ToString();
  88. string passwd = null;
  89. if (requestData.ContainsKey("passwd"))
  90. {
  91. passwd = requestData["passwd"].ToString();
  92. }
  93. else if (requestData.ContainsKey("web_login_key"))
  94. {
  95. passwd = "$1$" + requestData["web_login_key"].ToString();
  96. m_log.InfoFormat("[LOGIN]: XMLRPC Login Req key {0}", passwd);
  97. }
  98. string startLocation = string.Empty;
  99. UUID scopeID = UUID.Zero;
  100. if (requestData["scope_id"] != null)
  101. scopeID = new UUID(requestData["scope_id"].ToString());
  102. if (requestData.ContainsKey("start"))
  103. startLocation = requestData["start"].ToString();
  104. string clientVersion = "Unknown";
  105. if (requestData.Contains("version") && requestData["version"] != null)
  106. clientVersion = requestData["version"].ToString();
  107. // We should do something interesting with the client version...
  108. string channel = "Unknown";
  109. if (requestData.Contains("channel") && requestData["channel"] != null)
  110. channel = requestData["channel"].ToString();
  111. string mac = "Unknown";
  112. if (requestData.Contains("mac") && requestData["mac"] != null)
  113. mac = requestData["mac"].ToString();
  114. string id0 = "Unknown";
  115. if (requestData.Contains("id0") && requestData["id0"] != null)
  116. id0 = requestData["id0"].ToString();
  117. //m_log.InfoFormat("[LOGIN]: XMLRPC Login Requested for {0} {1}, starting in {2}, using {3}", first, last, startLocation, clientVersion);
  118. LoginResponse reply = null;
  119. reply = m_LocalService.Login(first, last, passwd, startLocation, scopeID, clientVersion, channel, mac, id0, remoteClient);
  120. XmlRpcResponse response = new XmlRpcResponse();
  121. response.Value = reply.ToHashtable();
  122. return response;
  123. }
  124. }
  125. return FailedXMLRPCResponse();
  126. }
  127. public XmlRpcResponse HandleXMLRPCLoginBlocked(XmlRpcRequest request, IPEndPoint client)
  128. {
  129. XmlRpcResponse response = new XmlRpcResponse();
  130. Hashtable resp = new Hashtable();
  131. resp["reason"] = "presence";
  132. resp["message"] = "Logins are currently restricted. Please try again later.";
  133. resp["login"] = "false";
  134. response.Value = resp;
  135. return response;
  136. }
  137. public XmlRpcResponse HandleXMLRPCSetLoginLevel(XmlRpcRequest request, IPEndPoint remoteClient)
  138. {
  139. Hashtable requestData = (Hashtable)request.Params[0];
  140. if (requestData != null)
  141. {
  142. if (requestData.ContainsKey("first") && requestData["first"] != null &&
  143. requestData.ContainsKey("last") && requestData["last"] != null &&
  144. requestData.ContainsKey("level") && requestData["level"] != null &&
  145. requestData.ContainsKey("passwd") && requestData["passwd"] != null)
  146. {
  147. string first = requestData["first"].ToString();
  148. string last = requestData["last"].ToString();
  149. string passwd = requestData["passwd"].ToString();
  150. int level = Int32.Parse(requestData["level"].ToString());
  151. m_log.InfoFormat("[LOGIN]: XMLRPC Set Level to {2} Requested by {0} {1}", first, last, level);
  152. Hashtable reply = m_LocalService.SetLevel(first, last, passwd, level, remoteClient);
  153. XmlRpcResponse response = new XmlRpcResponse();
  154. response.Value = reply;
  155. return response;
  156. }
  157. }
  158. XmlRpcResponse failResponse = new XmlRpcResponse();
  159. Hashtable failHash = new Hashtable();
  160. failHash["success"] = "false";
  161. failResponse.Value = failHash;
  162. return failResponse;
  163. }
  164. public OSD HandleLLSDLogin(OSD request, IPEndPoint remoteClient)
  165. {
  166. if (request.Type == OSDType.Map)
  167. {
  168. OSDMap map = (OSDMap)request;
  169. if (map.ContainsKey("first") && map.ContainsKey("last") && map.ContainsKey("passwd"))
  170. {
  171. string startLocation = string.Empty;
  172. if (map.ContainsKey("start"))
  173. startLocation = map["start"].AsString();
  174. UUID scopeID = UUID.Zero;
  175. if (map.ContainsKey("scope_id"))
  176. scopeID = new UUID(map["scope_id"].AsString());
  177. m_log.Info("[LOGIN]: LLSD Login Requested for: '" + map["first"].AsString() + "' '" + map["last"].AsString() + "' / " + startLocation);
  178. LoginResponse reply = null;
  179. reply = m_LocalService.Login(map["first"].AsString(), map["last"].AsString(), map["passwd"].AsString(), startLocation, scopeID,
  180. map["version"].AsString(), map["channel"].AsString(), map["mac"].AsString(), map["id0"].AsString(), remoteClient);
  181. return reply.ToOSDMap();
  182. }
  183. }
  184. return FailedOSDResponse();
  185. }
  186. public void HandleWebSocketLoginEvents(string path, WebSocketHttpServerHandler sock)
  187. {
  188. sock.MaxPayloadSize = 16384; //16 kb payload
  189. sock.InitialMsgTimeout = 5000; //5 second first message to trigger at least one of these events
  190. sock.NoDelay_TCP_Nagle = true;
  191. sock.OnData += delegate(object sender, WebsocketDataEventArgs data) { sock.Close("fail"); };
  192. sock.OnPing += delegate(object sender, PingEventArgs pingdata) { sock.Close("fail"); };
  193. sock.OnPong += delegate(object sender, PongEventArgs pongdata) { sock.Close("fail"); };
  194. sock.OnText += delegate(object sender, WebsocketTextEventArgs text)
  195. {
  196. OSD request = null;
  197. try
  198. {
  199. request = OSDParser.DeserializeJson(text.Data);
  200. if (!(request is OSDMap))
  201. {
  202. sock.SendMessage(OSDParser.SerializeJsonString(FailedOSDResponse()));
  203. }
  204. else
  205. {
  206. OSDMap req = request as OSDMap;
  207. string first = req["firstname"].AsString();
  208. string last = req["lastname"].AsString();
  209. string passwd = req["passwd"].AsString();
  210. string start = req["startlocation"].AsString();
  211. string version = req["version"].AsString();
  212. string channel = req["channel"].AsString();
  213. string mac = req["mac"].AsString();
  214. string id0 = req["id0"].AsString();
  215. UUID scope = UUID.Zero;
  216. IPEndPoint endPoint =
  217. (sender as WebSocketHttpServerHandler).GetRemoteIPEndpoint();
  218. LoginResponse reply = null;
  219. reply = m_LocalService.Login(first, last, passwd, start, scope, version,
  220. channel, mac, id0, endPoint);
  221. sock.SendMessage(OSDParser.SerializeJsonString(reply.ToOSDMap()));
  222. }
  223. }
  224. catch (Exception)
  225. {
  226. sock.SendMessage(OSDParser.SerializeJsonString(FailedOSDResponse()));
  227. }
  228. finally
  229. {
  230. sock.Close("success");
  231. }
  232. };
  233. sock.HandshakeAndUpgrade();
  234. }
  235. private XmlRpcResponse FailedXMLRPCResponse()
  236. {
  237. Hashtable hash = new Hashtable();
  238. hash["reason"] = "key";
  239. hash["message"] = "Incomplete login credentials. Check your username and password.";
  240. hash["login"] = "false";
  241. XmlRpcResponse response = new XmlRpcResponse();
  242. response.Value = hash;
  243. return response;
  244. }
  245. private OSD FailedOSDResponse()
  246. {
  247. OSDMap map = new OSDMap();
  248. map["reason"] = OSD.FromString("key");
  249. map["message"] = OSD.FromString("Invalid login credentials. Check your username and passwd.");
  250. map["login"] = OSD.FromString("false");
  251. return map;
  252. }
  253. }
  254. }