HGStandaloneLoginModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.Net;
  31. using System.Reflection;
  32. using System.Text.RegularExpressions;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using Nwc.XmlRpc;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Communications;
  39. using OpenSim.Framework.Communications.Services;
  40. using OpenSim.Framework.Communications.Cache;
  41. using OpenSim.Framework.Capabilities;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Region.Framework.Interfaces;
  45. namespace OpenSim.Region.CoreModules.Hypergrid
  46. {
  47. public class HGStandaloneLoginModule : IRegionModule, ILoginServiceToRegionsConnector
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. protected List<Scene> m_scenes = new List<Scene>();
  51. protected Scene m_firstScene;
  52. protected bool m_enabled = false; // Module is only enabled if running in standalone mode
  53. protected HGLoginAuthService m_loginService;
  54. #region IRegionModule Members
  55. public void Initialise(Scene scene, IConfigSource source)
  56. {
  57. if (m_firstScene == null)
  58. {
  59. m_firstScene = scene;
  60. IConfig startupConfig = source.Configs["Startup"];
  61. if (startupConfig != null)
  62. {
  63. m_enabled = !startupConfig.GetBoolean("gridmode", false);
  64. }
  65. if (m_enabled)
  66. {
  67. m_log.Debug("[HGLogin]: HGlogin module enabled");
  68. bool authenticate = true;
  69. string welcomeMessage = "Welcome to OpenSim";
  70. IConfig standaloneConfig = source.Configs["StandAlone"];
  71. if (standaloneConfig != null)
  72. {
  73. authenticate = standaloneConfig.GetBoolean("accounts_authenticate", true);
  74. welcomeMessage = standaloneConfig.GetString("welcome_message");
  75. }
  76. //TODO: fix casting.
  77. LibraryRootFolder rootFolder = m_firstScene.CommsManager.UserProfileCacheService.LibraryRoot as LibraryRootFolder;
  78. IHttpServer httpServer = MainServer.Instance;
  79. //TODO: fix the casting of the user service, maybe by registering the userManagerBase with scenes, or refactoring so we just need a IUserService reference
  80. m_loginService
  81. = new HGLoginAuthService(
  82. (UserManagerBase)m_firstScene.CommsManager.UserAdminService,
  83. welcomeMessage,
  84. m_firstScene.CommsManager.InterServiceInventoryService,
  85. m_firstScene.CommsManager.NetworkServersInfo,
  86. authenticate,
  87. rootFolder,
  88. this);
  89. httpServer.AddXmlRPCHandler("hg_login", m_loginService.XmlRpcLoginMethod);
  90. httpServer.AddXmlRPCHandler("check_auth_session", m_loginService.XmlRPCCheckAuthSession, false);
  91. httpServer.AddXmlRPCHandler("get_avatar_appearance", XmlRPCGetAvatarAppearance);
  92. httpServer.AddXmlRPCHandler("update_avatar_appearance", XmlRPCUpdateAvatarAppearance);
  93. }
  94. }
  95. if (m_enabled)
  96. {
  97. AddScene(scene);
  98. }
  99. }
  100. public void PostInitialise()
  101. {
  102. }
  103. public void Close()
  104. {
  105. }
  106. public string Name
  107. {
  108. get { return "HGStandaloneLoginModule"; }
  109. }
  110. public bool IsSharedModule
  111. {
  112. get { return true; }
  113. }
  114. #endregion
  115. protected void AddScene(Scene scene)
  116. {
  117. lock (m_scenes)
  118. {
  119. if (!m_scenes.Contains(scene))
  120. {
  121. m_scenes.Add(scene);
  122. }
  123. }
  124. }
  125. public bool NewUserConnection(ulong regionHandle, AgentCircuitData agent, out string reason)
  126. {
  127. reason = String.Empty;
  128. return true;
  129. }
  130. public void LogOffUserFromGrid(ulong regionHandle, UUID AvatarID, UUID RegionSecret, string message)
  131. {
  132. Scene scene;
  133. if (TryGetRegion(regionHandle, out scene))
  134. {
  135. scene.HandleLogOffUserFromGrid(AvatarID, RegionSecret, message);
  136. }
  137. }
  138. public RegionInfo RequestNeighbourInfo(ulong regionhandle)
  139. {
  140. Scene scene;
  141. if (TryGetRegion(regionhandle, out scene))
  142. {
  143. return scene.RegionInfo;
  144. }
  145. return null;
  146. }
  147. public RegionInfo RequestClosestRegion(string region)
  148. {
  149. Scene scene;
  150. if (TryGetRegion(region, out scene))
  151. {
  152. return scene.RegionInfo;
  153. }
  154. else if (m_scenes.Count > 0)
  155. {
  156. return m_scenes[0].RegionInfo;
  157. }
  158. return null;
  159. }
  160. public RegionInfo RequestNeighbourInfo(UUID regionID)
  161. {
  162. Scene scene;
  163. if (TryGetRegion(regionID, out scene))
  164. {
  165. return scene.RegionInfo;
  166. }
  167. return null;
  168. }
  169. protected bool TryGetRegion(ulong regionHandle, out Scene scene)
  170. {
  171. lock (m_scenes)
  172. {
  173. foreach (Scene nextScene in m_scenes)
  174. {
  175. if (nextScene.RegionInfo.RegionHandle == regionHandle)
  176. {
  177. scene = nextScene;
  178. return true;
  179. }
  180. }
  181. }
  182. scene = null;
  183. return false;
  184. }
  185. protected bool TryGetRegion(UUID regionID, out Scene scene)
  186. {
  187. lock (m_scenes)
  188. {
  189. foreach (Scene nextScene in m_scenes)
  190. {
  191. if (nextScene.RegionInfo.RegionID == regionID)
  192. {
  193. scene = nextScene;
  194. return true;
  195. }
  196. }
  197. }
  198. scene = null;
  199. return false;
  200. }
  201. protected bool TryGetRegion(string regionName, out Scene scene)
  202. {
  203. lock (m_scenes)
  204. {
  205. foreach (Scene nextScene in m_scenes)
  206. {
  207. if (nextScene.RegionInfo.RegionName.Equals(regionName, StringComparison.InvariantCultureIgnoreCase))
  208. {
  209. scene = nextScene;
  210. return true;
  211. }
  212. }
  213. }
  214. scene = null;
  215. return false;
  216. }
  217. public XmlRpcResponse XmlRPCGetAvatarAppearance(XmlRpcRequest request, IPEndPoint remoteClient)
  218. {
  219. XmlRpcResponse response = new XmlRpcResponse();
  220. Hashtable requestData = (Hashtable)request.Params[0];
  221. AvatarAppearance appearance;
  222. Hashtable responseData;
  223. if (requestData.Contains("owner"))
  224. {
  225. appearance = m_firstScene.CommsManager.AvatarService.GetUserAppearance(new UUID((string)requestData["owner"]));
  226. if (appearance == null)
  227. {
  228. responseData = new Hashtable();
  229. responseData["error_type"] = "no appearance";
  230. responseData["error_desc"] = "There was no appearance found for this avatar";
  231. }
  232. else
  233. {
  234. responseData = appearance.ToHashTable();
  235. }
  236. }
  237. else
  238. {
  239. responseData = new Hashtable();
  240. responseData["error_type"] = "unknown_avatar";
  241. responseData["error_desc"] = "The avatar appearance requested is not in the database";
  242. }
  243. response.Value = responseData;
  244. return response;
  245. }
  246. public XmlRpcResponse XmlRPCUpdateAvatarAppearance(XmlRpcRequest request, IPEndPoint remoteClient)
  247. {
  248. XmlRpcResponse response = new XmlRpcResponse();
  249. Hashtable requestData = (Hashtable)request.Params[0];
  250. Hashtable responseData;
  251. if (requestData.Contains("owner"))
  252. {
  253. AvatarAppearance appearance = new AvatarAppearance(requestData);
  254. // TODO: Sometime in the future we may have a database layer that is capable of updating appearance when
  255. // the TextureEntry is null. When that happens, this check can be removed
  256. if (appearance.Texture != null)
  257. m_firstScene.CommsManager.AvatarService.UpdateUserAppearance(new UUID((string)requestData["owner"]), appearance);
  258. responseData = new Hashtable();
  259. responseData["returnString"] = "TRUE";
  260. }
  261. else
  262. {
  263. responseData = new Hashtable();
  264. responseData["error_type"] = "unknown_avatar";
  265. responseData["error_desc"] = "The avatar appearance requested is not in the database";
  266. }
  267. response.Value = responseData;
  268. return response;
  269. }
  270. }
  271. }