LLProxyLoginModule.cs 11 KB

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