LLLoginHandlers.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 (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. bool LibOMVclient = false;
  119. if (request.Params.Count > 4 && (string)request.Params[4] == "gridproxy")
  120. LibOMVclient = true;
  121. LoginResponse reply = null;
  122. reply = m_LocalService.Login(first, last, passwd, startLocation, scopeID, clientVersion, channel, mac, id0, remoteClient, LibOMVclient);
  123. XmlRpcResponse response = new XmlRpcResponse();
  124. response.Value = reply.ToHashtable();
  125. return response;
  126. }
  127. }
  128. return FailedXMLRPCResponse();
  129. }
  130. public XmlRpcResponse HandleXMLRPCLoginBlocked(XmlRpcRequest request, IPEndPoint client)
  131. {
  132. XmlRpcResponse response = new XmlRpcResponse();
  133. Hashtable resp = new Hashtable();
  134. resp["reason"] = "presence";
  135. resp["message"] = "Logins are currently restricted. Please try again later.";
  136. resp["login"] = "false";
  137. response.Value = resp;
  138. return response;
  139. }
  140. public XmlRpcResponse HandleXMLRPCSetLoginLevel(XmlRpcRequest request, IPEndPoint remoteClient)
  141. {
  142. Hashtable requestData = (Hashtable)request.Params[0];
  143. if (requestData != null)
  144. {
  145. if (requestData.ContainsKey("first") && requestData["first"] != null &&
  146. requestData.ContainsKey("last") && requestData["last"] != null &&
  147. requestData.ContainsKey("level") && requestData["level"] != null &&
  148. requestData.ContainsKey("passwd") && requestData["passwd"] != null)
  149. {
  150. string first = requestData["first"].ToString();
  151. string last = requestData["last"].ToString();
  152. string passwd = requestData["passwd"].ToString();
  153. int level = Int32.Parse(requestData["level"].ToString());
  154. m_log.InfoFormat("[LOGIN]: XMLRPC Set Level to {2} Requested by {0} {1}", first, last, level);
  155. Hashtable reply = m_LocalService.SetLevel(first, last, passwd, level, remoteClient);
  156. XmlRpcResponse response = new XmlRpcResponse();
  157. response.Value = reply;
  158. return response;
  159. }
  160. }
  161. XmlRpcResponse failResponse = new XmlRpcResponse();
  162. Hashtable failHash = new Hashtable();
  163. failHash["success"] = "false";
  164. failResponse.Value = failHash;
  165. return failResponse;
  166. }
  167. public OSD HandleLLSDLogin(OSD request, IPEndPoint remoteClient)
  168. {
  169. if (request.Type == OSDType.Map)
  170. {
  171. OSDMap map = (OSDMap)request;
  172. if (map.ContainsKey("first") && map.ContainsKey("last") && map.ContainsKey("passwd"))
  173. {
  174. string startLocation = string.Empty;
  175. if (map.ContainsKey("start"))
  176. startLocation = map["start"].AsString();
  177. UUID scopeID = UUID.Zero;
  178. if (map.ContainsKey("scope_id"))
  179. scopeID = new UUID(map["scope_id"].AsString());
  180. m_log.Info("[LOGIN]: LLSD Login Requested for: '" + map["first"].AsString() + "' '" + map["last"].AsString() + "' / " + startLocation);
  181. LoginResponse reply = null;
  182. reply = m_LocalService.Login(map["first"].AsString(), map["last"].AsString(), map["passwd"].AsString(), startLocation, scopeID,
  183. map["version"].AsString(), map["channel"].AsString(), map["mac"].AsString(), map["id0"].AsString(), remoteClient,false);
  184. return reply.ToOSDMap();
  185. }
  186. }
  187. return FailedOSDResponse();
  188. }
  189. public void HandleWebSocketLoginEvents(string path, WebSocketHttpServerHandler sock)
  190. {
  191. sock.MaxPayloadSize = 16384; //16 kb payload
  192. sock.InitialMsgTimeout = 5000; //5 second first message to trigger at least one of these events
  193. sock.NoDelay_TCP_Nagle = true;
  194. sock.OnData += delegate(object sender, WebsocketDataEventArgs data) { sock.Close("fail"); };
  195. sock.OnPing += delegate(object sender, PingEventArgs pingdata) { sock.Close("fail"); };
  196. sock.OnPong += delegate(object sender, PongEventArgs pongdata) { sock.Close("fail"); };
  197. sock.OnText += delegate(object sender, WebsocketTextEventArgs text)
  198. {
  199. OSD request = null;
  200. try
  201. {
  202. request = OSDParser.DeserializeJson(text.Data);
  203. if (!(request is OSDMap))
  204. {
  205. sock.SendMessage(OSDParser.SerializeJsonString(FailedOSDResponse()));
  206. }
  207. else
  208. {
  209. OSDMap req = request as OSDMap;
  210. string first = req["firstname"].AsString();
  211. string last = req["lastname"].AsString();
  212. string passwd = req["passwd"].AsString();
  213. string start = req["startlocation"].AsString();
  214. string version = req["version"].AsString();
  215. string channel = req["channel"].AsString();
  216. string mac = req["mac"].AsString();
  217. string id0 = req["id0"].AsString();
  218. UUID scope = UUID.Zero;
  219. IPEndPoint endPoint =
  220. (sender as WebSocketHttpServerHandler).GetRemoteIPEndpoint();
  221. LoginResponse reply = null;
  222. reply = m_LocalService.Login(first, last, passwd, start, scope, version,
  223. channel, mac, id0, endPoint,false);
  224. sock.SendMessage(OSDParser.SerializeJsonString(reply.ToOSDMap()));
  225. }
  226. }
  227. catch (Exception)
  228. {
  229. sock.SendMessage(OSDParser.SerializeJsonString(FailedOSDResponse()));
  230. }
  231. finally
  232. {
  233. sock.Close("success");
  234. }
  235. };
  236. sock.HandshakeAndUpgrade();
  237. }
  238. private XmlRpcResponse FailedXMLRPCResponse()
  239. {
  240. Hashtable hash = new Hashtable();
  241. hash["reason"] = "key";
  242. hash["message"] = "Incomplete login credentials. Check your username and password.";
  243. hash["login"] = "false";
  244. XmlRpcResponse response = new XmlRpcResponse();
  245. response.Value = hash;
  246. return response;
  247. }
  248. private OSD FailedOSDResponse()
  249. {
  250. OSDMap map = new OSDMap();
  251. map["reason"] = OSD.FromString("key");
  252. map["message"] = OSD.FromString("Invalid login credentials. Check your username and passwd.");
  253. map["login"] = OSD.FromString("false");
  254. return map;
  255. }
  256. }
  257. }