GatekeeperService.cs 13 KB

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