GatekeeperService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenSim.Services.Interfaces;
  33. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  34. using OpenSim.Server.Base;
  35. using OpenSim.Services.Connectors.Hypergrid;
  36. using OpenMetaverse;
  37. using Nini.Config;
  38. using log4net;
  39. namespace OpenSim.Services.HypergridService
  40. {
  41. public class GatekeeperService : IGatekeeperService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private static bool m_Initialized = false;
  47. private static IGridService m_GridService;
  48. private static IPresenceService m_PresenceService;
  49. private static IUserAccountService m_UserAccountService;
  50. private static IUserAgentService m_UserAgentService;
  51. private static ISimulationService m_SimulationService;
  52. private static UUID m_ScopeID;
  53. private static bool m_AllowTeleportsToAnyRegion;
  54. private static string m_ExternalName;
  55. private static GridRegion m_DefaultGatewayRegion;
  56. public GatekeeperService(IConfigSource config, ISimulationService simService)
  57. {
  58. if (!m_Initialized)
  59. {
  60. m_Initialized = true;
  61. IConfig serverConfig = config.Configs["GatekeeperService"];
  62. if (serverConfig == null)
  63. throw new Exception(String.Format("No section GatekeeperService in config file"));
  64. string accountService = serverConfig.GetString("UserAccountService", String.Empty);
  65. string homeUsersService = serverConfig.GetString("UserAgentService", string.Empty);
  66. string gridService = serverConfig.GetString("GridService", String.Empty);
  67. string presenceService = serverConfig.GetString("PresenceService", String.Empty);
  68. string simulationService = serverConfig.GetString("SimulationService", String.Empty);
  69. // These 3 are mandatory, the others aren't
  70. if (gridService == string.Empty || presenceService == string.Empty)
  71. throw new Exception("Incomplete specifications, Gatekeeper Service cannot function.");
  72. string scope = serverConfig.GetString("ScopeID", UUID.Zero.ToString());
  73. UUID.TryParse(scope, out m_ScopeID);
  74. //m_WelcomeMessage = serverConfig.GetString("WelcomeMessage", "Welcome to OpenSim!");
  75. m_AllowTeleportsToAnyRegion = serverConfig.GetBoolean("AllowTeleportsToAnyRegion", true);
  76. m_ExternalName = serverConfig.GetString("ExternalName", string.Empty);
  77. Object[] args = new Object[] { config };
  78. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  79. m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args);
  80. if (accountService != string.Empty)
  81. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(accountService, args);
  82. if (homeUsersService != string.Empty)
  83. m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(homeUsersService, args);
  84. if (simService != null)
  85. m_SimulationService = simService;
  86. else if (simulationService != string.Empty)
  87. m_SimulationService = ServerUtils.LoadPlugin<ISimulationService>(simulationService, args);
  88. if (m_GridService == null || m_PresenceService == null || m_SimulationService == null)
  89. throw new Exception("Unable to load a required plugin, Gatekeeper Service cannot function.");
  90. m_log.Debug("[GATEKEEPER SERVICE]: Starting...");
  91. }
  92. }
  93. public GatekeeperService(IConfigSource config)
  94. : this(config, null)
  95. {
  96. }
  97. public bool LinkRegion(string regionName, out UUID regionID, out ulong regionHandle, out string externalName, out string imageURL, out string reason)
  98. {
  99. regionID = UUID.Zero;
  100. regionHandle = 0;
  101. externalName = m_ExternalName;
  102. imageURL = string.Empty;
  103. reason = string.Empty;
  104. m_log.DebugFormat("[GATEKEEPER SERVICE]: Request to link to {0}", (regionName == string.Empty)? "default region" : regionName);
  105. if (!m_AllowTeleportsToAnyRegion || regionName == string.Empty)
  106. {
  107. List<GridRegion> defs = m_GridService.GetDefaultRegions(m_ScopeID);
  108. if (defs != null && defs.Count > 0)
  109. m_DefaultGatewayRegion = defs[0];
  110. try
  111. {
  112. regionID = m_DefaultGatewayRegion.RegionID;
  113. regionHandle = m_DefaultGatewayRegion.RegionHandle;
  114. }
  115. catch
  116. {
  117. reason = "Grid setup problem. Try specifying a particular region here.";
  118. m_log.DebugFormat("[GATEKEEPER SERVICE]: Unable to send information. Please specify a default region for this grid!");
  119. return false;
  120. }
  121. return true;
  122. }
  123. GridRegion region = m_GridService.GetRegionByName(m_ScopeID, regionName);
  124. if (region == null)
  125. {
  126. reason = "Region not found";
  127. return false;
  128. }
  129. regionID = region.RegionID;
  130. regionHandle = region.RegionHandle;
  131. string regionimage = "regionImage" + region.RegionID.ToString();
  132. regionimage = regionimage.Replace("-", "");
  133. imageURL = "http://" + region.ExternalHostName + ":" + region.HttpPort + "/index.php?method=" + regionimage;
  134. return true;
  135. }
  136. public GridRegion GetHyperlinkRegion(UUID regionID)
  137. {
  138. m_log.DebugFormat("[GATEKEEPER SERVICE]: Request to get hyperlink region {0}", regionID);
  139. if (!m_AllowTeleportsToAnyRegion)
  140. // Don't even check the given regionID
  141. return m_DefaultGatewayRegion;
  142. GridRegion region = m_GridService.GetRegionByUUID(m_ScopeID, regionID);
  143. return region;
  144. }
  145. #region Login Agent
  146. public bool LoginAgent(AgentCircuitData aCircuit, GridRegion destination, out string reason)
  147. {
  148. reason = string.Empty;
  149. string authURL = string.Empty;
  150. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  151. authURL = aCircuit.ServiceURLs["HomeURI"].ToString();
  152. m_log.DebugFormat("[GATEKEEPER SERVICE]: Request to login foreign agent {0} {1} @ {2} ({3}) at destination {4}",
  153. aCircuit.firstname, aCircuit.lastname, authURL, aCircuit.AgentID, destination.RegionName);
  154. //
  155. // Authenticate the user
  156. //
  157. if (!Authenticate(aCircuit))
  158. {
  159. reason = "Unable to verify identity";
  160. m_log.InfoFormat("[GATEKEEPER SERVICE]: Unable to verify identity of agent {0} {1}. Refusing service.", aCircuit.firstname, aCircuit.lastname);
  161. return false;
  162. }
  163. m_log.DebugFormat("[GATEKEEPER SERVICE]: Identity verified for {0} {1} @ {2}", aCircuit.firstname, aCircuit.lastname, authURL);
  164. //
  165. // Check for impersonations
  166. //
  167. UserAccount account = null;
  168. if (m_UserAccountService != null)
  169. {
  170. // Check to see if we have a local user with that UUID
  171. account = m_UserAccountService.GetUserAccount(m_ScopeID, aCircuit.AgentID);
  172. if (account != null)
  173. {
  174. // Make sure this is the user coming home, and not a foreign user with same UUID as a local user
  175. if (m_UserAgentService != null)
  176. {
  177. if (!m_UserAgentService.AgentIsComingHome(aCircuit.SessionID, m_ExternalName))
  178. {
  179. // Can't do, sorry
  180. reason = "Unauthorized";
  181. m_log.InfoFormat("[GATEKEEPER SERVICE]: Foreign agent {0} {1} has same ID as local user. Refusing service.",
  182. aCircuit.firstname, aCircuit.lastname);
  183. return false;
  184. }
  185. }
  186. }
  187. }
  188. m_log.DebugFormat("[GATEKEEPER SERVICE]: User is ok");
  189. // May want to authorize
  190. bool isFirstLogin = false;
  191. //
  192. // Login the presence, if it's not there yet (by the login service)
  193. //
  194. PresenceInfo presence = m_PresenceService.GetAgent(aCircuit.SessionID);
  195. if (presence != null) // it has been placed there by the login service
  196. isFirstLogin = true;
  197. else
  198. if (!m_PresenceService.LoginAgent(aCircuit.AgentID.ToString(), aCircuit.SessionID, aCircuit.SecureSessionID))
  199. {
  200. reason = "Unable to login presence";
  201. m_log.InfoFormat("[GATEKEEPER SERVICE]: Presence login failed for foreign agent {0} {1}. Refusing service.",
  202. aCircuit.firstname, aCircuit.lastname);
  203. return false;
  204. }
  205. m_log.DebugFormat("[GATEKEEPER SERVICE]: Login presence ok");
  206. //
  207. // Get the region
  208. //
  209. destination = m_GridService.GetRegionByUUID(m_ScopeID, destination.RegionID);
  210. if (destination == null)
  211. {
  212. reason = "Destination region not found";
  213. return false;
  214. }
  215. m_log.DebugFormat("[GATEKEEPER SERVICE]: destination ok: {0}", destination.RegionName);
  216. //
  217. // Adjust the visible name
  218. //
  219. if (account != null)
  220. {
  221. aCircuit.firstname = account.FirstName;
  222. aCircuit.lastname = account.LastName;
  223. }
  224. if (account == null && !aCircuit.lastname.StartsWith("@"))
  225. {
  226. aCircuit.firstname = aCircuit.firstname + "." + aCircuit.lastname;
  227. try
  228. {
  229. Uri uri = new Uri(aCircuit.ServiceURLs["HomeURI"].ToString());
  230. aCircuit.lastname = "@" + uri.Host; // + ":" + uri.Port;
  231. }
  232. catch
  233. {
  234. m_log.WarnFormat("[GATEKEEPER SERVICE]: Malformed HomeURI (this should never happen): {0}", aCircuit.ServiceURLs["HomeURI"]);
  235. aCircuit.lastname = "@" + aCircuit.ServiceURLs["HomeURI"].ToString();
  236. }
  237. }
  238. //
  239. // Finally launch the agent at the destination
  240. //
  241. Constants.TeleportFlags loginFlag = isFirstLogin ? Constants.TeleportFlags.ViaLogin : Constants.TeleportFlags.ViaHGLogin;
  242. m_log.DebugFormat("[GATEKEEPER SERVICE]: launching agent {0}", loginFlag);
  243. return m_SimulationService.CreateAgent(destination, aCircuit, (uint)loginFlag, out reason);
  244. }
  245. protected bool Authenticate(AgentCircuitData aCircuit)
  246. {
  247. if (!CheckAddress(aCircuit.ServiceSessionID))
  248. return false;
  249. string userURL = string.Empty;
  250. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  251. userURL = aCircuit.ServiceURLs["HomeURI"].ToString();
  252. if (userURL == string.Empty)
  253. {
  254. m_log.DebugFormat("[GATEKEEPER SERVICE]: Agent did not provide an authentication server URL");
  255. return false;
  256. }
  257. if (userURL == m_ExternalName)
  258. return m_UserAgentService.VerifyAgent(aCircuit.SessionID, aCircuit.ServiceSessionID);
  259. else
  260. {
  261. Object[] args = new Object[] { userURL };
  262. IUserAgentService userAgentService = new UserAgentServiceConnector(userURL);
  263. if (userAgentService != null)
  264. {
  265. try
  266. {
  267. return userAgentService.VerifyAgent(aCircuit.SessionID, aCircuit.ServiceSessionID);
  268. }
  269. catch
  270. {
  271. m_log.DebugFormat("[GATEKEEPER SERVICE]: Unable to contact authentication service at {0}", userURL);
  272. return false;
  273. }
  274. }
  275. }
  276. return false;
  277. }
  278. // Check that the service token was generated for *this* grid.
  279. // If it wasn't then that's a fake agent.
  280. protected bool CheckAddress(string serviceToken)
  281. {
  282. string[] parts = serviceToken.Split(new char[] { ';' });
  283. if (parts.Length < 2)
  284. return false;
  285. string addressee = parts[0];
  286. m_log.DebugFormat("[GATEKEEPER SERVICE]: Verifying {0} against {1}", addressee, m_ExternalName);
  287. return (addressee == m_ExternalName);
  288. }
  289. #endregion
  290. #region Misc
  291. #endregion
  292. }
  293. }