LLProxyLoginModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.Collections.Generic;
  30. using System.Text;
  31. using Nwc.XmlRpc;
  32. using System.Net;
  33. using System.Net.Sockets;
  34. using System.Reflection;
  35. using System.Security.Authentication;
  36. using log4net;
  37. using Nini.Config;
  38. using OpenMetaverse;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.Communications;
  41. using OpenSim.Framework.Servers;
  42. using OpenSim.Region.Framework.Interfaces;
  43. using OpenSim.Region.Framework.Scenes;
  44. namespace OpenSim.Client.Linden
  45. {
  46. /// <summary>
  47. /// Handles login user (expect user) and logoff user messages from the remote LL login server
  48. /// </summary>
  49. public class LLProxyLoginModule : ISharedRegionModule
  50. {
  51. private uint m_port = 0;
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. public LLProxyLoginModule(uint port)
  54. {
  55. m_log.DebugFormat("[CLIENT]: LLProxyLoginModule port {0}", port);
  56. m_port = port;
  57. }
  58. protected bool RegionLoginsEnabled
  59. {
  60. get
  61. {
  62. if (m_firstScene != null)
  63. {
  64. return m_firstScene.SceneGridService.RegionLoginsEnabled;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. }
  72. protected List<Scene> m_scenes = new List<Scene>();
  73. protected Scene m_firstScene;
  74. protected bool m_enabled = false; // Module is only enabled if running in grid mode
  75. #region IRegionModule Members
  76. public void Initialise(IConfigSource source)
  77. {
  78. IConfig startupConfig = source.Configs["Modules"];
  79. if (startupConfig != null)
  80. {
  81. m_enabled = startupConfig.GetBoolean("LLProxyLoginModule", false);
  82. }
  83. }
  84. public void AddRegion(Scene scene)
  85. {
  86. if (m_firstScene == null)
  87. {
  88. m_firstScene = scene;
  89. if (m_enabled)
  90. {
  91. AddHttpHandlers();
  92. }
  93. }
  94. if (m_enabled)
  95. {
  96. AddScene(scene);
  97. }
  98. }
  99. public void RemoveRegion(Scene scene)
  100. {
  101. if (m_enabled)
  102. {
  103. RemoveScene(scene);
  104. }
  105. }
  106. public void PostInitialise()
  107. {
  108. }
  109. public void Close()
  110. {
  111. }
  112. public void RegionLoaded(Scene scene)
  113. {
  114. }
  115. public Type ReplaceableInterface
  116. {
  117. get { return null; }
  118. }
  119. public string Name
  120. {
  121. get { return "LLProxyLoginModule"; }
  122. }
  123. public bool IsSharedModule
  124. {
  125. get { return true; }
  126. }
  127. #endregion
  128. /// <summary>
  129. /// Adds "expect_user" and "logoff_user" xmlrpc method handlers
  130. /// </summary>
  131. protected void AddHttpHandlers()
  132. {
  133. //we will add our handlers to the first scene we received, as all scenes share a http server. But will this ever change?
  134. MainServer.GetHttpServer(m_port).AddXmlRPCHandler("expect_user", ExpectUser, false);
  135. MainServer.GetHttpServer(m_port).AddXmlRPCHandler("logoff_user", LogOffUser, false);
  136. }
  137. protected void AddScene(Scene scene)
  138. {
  139. lock (m_scenes)
  140. {
  141. if (!m_scenes.Contains(scene))
  142. {
  143. m_scenes.Add(scene);
  144. }
  145. }
  146. }
  147. protected void RemoveScene(Scene scene)
  148. {
  149. lock (m_scenes)
  150. {
  151. if (m_scenes.Contains(scene))
  152. {
  153. m_scenes.Remove(scene);
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// Received from the user server when a user starts logging in. This call allows
  159. /// the region to prepare for direct communication from the client. Sends back an empty
  160. /// xmlrpc response on completion.
  161. /// </summary>
  162. /// <param name="request"></param>
  163. /// <returns></returns>
  164. public XmlRpcResponse ExpectUser(XmlRpcRequest request, IPEndPoint remoteClient)
  165. {
  166. XmlRpcResponse resp = new XmlRpcResponse();
  167. try
  168. {
  169. ulong regionHandle = 0;
  170. Hashtable requestData = (Hashtable)request.Params[0];
  171. AgentCircuitData agentData = new AgentCircuitData();
  172. if (requestData.ContainsKey("session_id"))
  173. agentData.SessionID = new UUID((string)requestData["session_id"]);
  174. if (requestData.ContainsKey("secure_session_id"))
  175. agentData.SecureSessionID = new UUID((string)requestData["secure_session_id"]);
  176. if (requestData.ContainsKey("firstname"))
  177. agentData.firstname = (string)requestData["firstname"];
  178. if (requestData.ContainsKey("lastname"))
  179. agentData.lastname = (string)requestData["lastname"];
  180. if (requestData.ContainsKey("agent_id"))
  181. agentData.AgentID = new UUID((string)requestData["agent_id"]);
  182. if (requestData.ContainsKey("circuit_code"))
  183. agentData.circuitcode = Convert.ToUInt32(requestData["circuit_code"]);
  184. if (requestData.ContainsKey("caps_path"))
  185. agentData.CapsPath = (string)requestData["caps_path"];
  186. if (requestData.ContainsKey("regionhandle"))
  187. regionHandle = Convert.ToUInt64((string)requestData["regionhandle"]);
  188. else
  189. m_log.Warn("[CLIENT]: request from login server did not contain regionhandle");
  190. // Appearance
  191. if (requestData.ContainsKey("appearance"))
  192. agentData.Appearance = new AvatarAppearance((Hashtable)requestData["appearance"]);
  193. m_log.DebugFormat(
  194. "[CLIENT]: Told by user service to prepare for a connection from {0} {1} {2}, circuit {3}",
  195. agentData.firstname, agentData.lastname, agentData.AgentID, agentData.circuitcode);
  196. if (requestData.ContainsKey("child_agent") && requestData["child_agent"].Equals("1"))
  197. {
  198. //m_log.Debug("[CLIENT]: Child agent detected");
  199. agentData.child = true;
  200. }
  201. else
  202. {
  203. //m_log.Debug("[CLIENT]: Main agent detected");
  204. agentData.startpos =
  205. new Vector3((float)Convert.ToDecimal((string)requestData["startpos_x"]),
  206. (float)Convert.ToDecimal((string)requestData["startpos_y"]),
  207. (float)Convert.ToDecimal((string)requestData["startpos_z"]));
  208. agentData.child = false;
  209. }
  210. if (!RegionLoginsEnabled)
  211. {
  212. m_log.InfoFormat(
  213. "[CLIENT]: Denying access for user {0} {1} because region login is currently disabled",
  214. agentData.firstname, agentData.lastname);
  215. Hashtable respdata = new Hashtable();
  216. respdata["success"] = "FALSE";
  217. respdata["reason"] = "region login currently disabled";
  218. resp.Value = respdata;
  219. }
  220. else
  221. {
  222. bool success = false;
  223. string denyMess = "";
  224. Scene scene;
  225. if (TryGetRegion(regionHandle, out scene))
  226. {
  227. if (scene.RegionInfo.EstateSettings.IsBanned(agentData.AgentID))
  228. {
  229. denyMess = "User is banned from this region";
  230. m_log.InfoFormat(
  231. "[CLIENT]: Denying access for user {0} {1} because user is banned",
  232. agentData.firstname, agentData.lastname);
  233. }
  234. else
  235. {
  236. string reason;
  237. if (scene.NewUserConnection(agentData, out reason))
  238. {
  239. success = true;
  240. }
  241. else
  242. {
  243. denyMess = String.Format("Login refused by region: {0}", reason);
  244. m_log.InfoFormat(
  245. "[CLIENT]: Denying access for user {0} {1} because user connection was refused by the region",
  246. agentData.firstname, agentData.lastname);
  247. }
  248. }
  249. }
  250. else
  251. {
  252. denyMess = "Region not found";
  253. }
  254. if (success)
  255. {
  256. Hashtable respdata = new Hashtable();
  257. respdata["success"] = "TRUE";
  258. resp.Value = respdata;
  259. }
  260. else
  261. {
  262. Hashtable respdata = new Hashtable();
  263. respdata["success"] = "FALSE";
  264. respdata["reason"] = denyMess;
  265. resp.Value = respdata;
  266. }
  267. }
  268. }
  269. catch (Exception e)
  270. {
  271. m_log.WarnFormat("[CLIENT]: Unable to receive user. Reason: {0}", e);
  272. Hashtable respdata = new Hashtable();
  273. respdata["success"] = "FALSE";
  274. respdata["reason"] = "Exception occurred";
  275. resp.Value = respdata;
  276. }
  277. return resp;
  278. }
  279. // Grid Request Processing
  280. /// <summary>
  281. /// Ooops, our Agent must be dead if we're getting this request!
  282. /// </summary>
  283. /// <param name="request"></param>
  284. /// <returns></returns>
  285. public XmlRpcResponse LogOffUser(XmlRpcRequest request, IPEndPoint remoteClient)
  286. {
  287. m_log.Debug("[CONNECTION DEBUGGING]: LogOff User Called");
  288. Hashtable requestData = (Hashtable)request.Params[0];
  289. string message = (string)requestData["message"];
  290. UUID agentID = UUID.Zero;
  291. UUID RegionSecret = UUID.Zero;
  292. UUID.TryParse((string)requestData["agent_id"], out agentID);
  293. UUID.TryParse((string)requestData["region_secret"], out RegionSecret);
  294. ulong regionHandle = Convert.ToUInt64((string)requestData["regionhandle"]);
  295. Scene scene;
  296. if (TryGetRegion(regionHandle, out scene))
  297. {
  298. scene.HandleLogOffUserFromGrid(agentID, RegionSecret, message);
  299. }
  300. return new XmlRpcResponse();
  301. }
  302. protected bool TryGetRegion(ulong regionHandle, out Scene scene)
  303. {
  304. lock (m_scenes)
  305. {
  306. foreach (Scene nextScene in m_scenes)
  307. {
  308. if (nextScene.RegionInfo.RegionHandle == regionHandle)
  309. {
  310. scene = nextScene;
  311. return true;
  312. }
  313. }
  314. }
  315. scene = null;
  316. return false;
  317. }
  318. }
  319. }