LLProxyLoginModule.cs 12 KB

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