GatekeeperService.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 System.Text.RegularExpressions;
  32. using OpenSim.Framework;
  33. using OpenSim.Services.Interfaces;
  34. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Connectors.InstantMessage;
  37. using OpenSim.Services.Connectors.Hypergrid;
  38. using OpenMetaverse;
  39. using Nini.Config;
  40. using log4net;
  41. namespace OpenSim.Services.HypergridService
  42. {
  43. public class GatekeeperService : IGatekeeperService
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. private static bool m_Initialized = false;
  49. private static IGridService m_GridService;
  50. private static IPresenceService m_PresenceService;
  51. private static IUserAccountService m_UserAccountService;
  52. private static IUserAgentService m_UserAgentService;
  53. private static ISimulationService m_SimulationService;
  54. private static IGridUserService m_GridUserService;
  55. private static IBansService m_BansService;
  56. private static string m_AllowedClients = string.Empty;
  57. private static string m_DeniedClients = string.Empty;
  58. private static string m_DeniedMacs = string.Empty;
  59. private static bool m_ForeignAgentsAllowed = true;
  60. private static List<string> m_ForeignsAllowedExceptions = new List<string>();
  61. private static List<string> m_ForeignsDisallowedExceptions = new List<string>();
  62. private static UUID m_ScopeID;
  63. private static bool m_AllowTeleportsToAnyRegion;
  64. private static OSHHTPHost m_gatekeeperHost;
  65. private static string m_gatekeeperURL;
  66. private HashSet<OSHHTPHost> m_gateKeeperAlias;
  67. private static GridRegion m_DefaultGatewayRegion;
  68. private bool m_allowDuplicatePresences = false;
  69. private static string m_messageKey;
  70. public GatekeeperService(IConfigSource config, ISimulationService simService)
  71. {
  72. if (!m_Initialized)
  73. {
  74. m_Initialized = true;
  75. IConfig serverConfig = config.Configs["GatekeeperService"];
  76. if (serverConfig == null)
  77. throw new Exception(String.Format("No section GatekeeperService in config file"));
  78. string accountService = serverConfig.GetString("UserAccountService", string.Empty);
  79. string homeUsersService = serverConfig.GetString("UserAgentService", string.Empty);
  80. string gridService = serverConfig.GetString("GridService", string.Empty);
  81. string presenceService = serverConfig.GetString("PresenceService", string.Empty);
  82. string simulationService = serverConfig.GetString("SimulationService", string.Empty);
  83. string gridUserService = serverConfig.GetString("GridUserService", string.Empty);
  84. string bansService = serverConfig.GetString("BansService", string.Empty);
  85. // These are mandatory, the others aren't
  86. if (gridService == string.Empty || presenceService == string.Empty)
  87. throw new Exception("Incomplete specifications, Gatekeeper Service cannot function.");
  88. string scope = serverConfig.GetString("ScopeID", UUID.Zero.ToString());
  89. UUID.TryParse(scope, out m_ScopeID);
  90. //m_WelcomeMessage = serverConfig.GetString("WelcomeMessage", "Welcome to OpenSim!");
  91. m_AllowTeleportsToAnyRegion = serverConfig.GetBoolean("AllowTeleportsToAnyRegion", true);
  92. string[] sections = new string[] { "Const, Startup", "Hypergrid", "GatekeeperService" };
  93. string externalName = Util.GetConfigVarFromSections<string>(config, "GatekeeperURI", sections, string.Empty);
  94. if(string.IsNullOrEmpty(externalName))
  95. externalName = serverConfig.GetString("ExternalName", string.Empty);
  96. m_gatekeeperHost = new OSHHTPHost(externalName, true);
  97. if (!m_gatekeeperHost.IsResolvedHost)
  98. {
  99. m_log.Error((m_gatekeeperHost.IsValidHost ? "Could not resolve GatekeeperURI" : "GatekeeperURI is a invalid host ") + externalName ?? "");
  100. throw new Exception("GatekeeperURI is invalid");
  101. }
  102. m_gatekeeperURL = m_gatekeeperHost.URIwEndSlash;
  103. string gatekeeperURIAlias = Util.GetConfigVarFromSections<string>(config, "GatekeeperURIAlias", sections, string.Empty);
  104. if (!string.IsNullOrWhiteSpace(gatekeeperURIAlias))
  105. {
  106. string[] alias = gatekeeperURIAlias.Split(',');
  107. for (int i = 0; i < alias.Length; ++i)
  108. {
  109. OSHHTPHost tmp = new OSHHTPHost(alias[i].Trim(), false);
  110. if (tmp.IsValidHost)
  111. {
  112. if (m_gateKeeperAlias == null)
  113. m_gateKeeperAlias = new HashSet<OSHHTPHost>();
  114. m_gateKeeperAlias.Add(tmp);
  115. }
  116. }
  117. }
  118. object[] args = new object[] { config };
  119. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  120. m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args);
  121. if (accountService != string.Empty)
  122. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(accountService, args);
  123. if (homeUsersService != string.Empty)
  124. m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(homeUsersService, args);
  125. if (gridUserService != string.Empty)
  126. m_GridUserService = ServerUtils.LoadPlugin<IGridUserService>(gridUserService, args);
  127. if (bansService != string.Empty)
  128. m_BansService = ServerUtils.LoadPlugin<IBansService>(bansService, args);
  129. if (simService != null)
  130. m_SimulationService = simService;
  131. else if (simulationService != string.Empty)
  132. m_SimulationService = ServerUtils.LoadPlugin<ISimulationService>(simulationService, args);
  133. string[] possibleAccessControlConfigSections = new string[] { "AccessControl", "GatekeeperService" };
  134. m_AllowedClients = Util.GetConfigVarFromSections<string>(
  135. config, "AllowedClients", possibleAccessControlConfigSections, string.Empty);
  136. m_DeniedClients = Util.GetConfigVarFromSections<string>(
  137. config, "DeniedClients", possibleAccessControlConfigSections, string.Empty);
  138. m_DeniedMacs = Util.GetConfigVarFromSections<string>(
  139. config, "DeniedMacs", possibleAccessControlConfigSections, string.Empty);
  140. m_ForeignAgentsAllowed = serverConfig.GetBoolean("ForeignAgentsAllowed", true);
  141. LoadDomainExceptionsFromConfig(serverConfig, "AllowExcept", m_ForeignsAllowedExceptions);
  142. LoadDomainExceptionsFromConfig(serverConfig, "DisallowExcept", m_ForeignsDisallowedExceptions);
  143. if (m_GridService == null || m_PresenceService == null || m_SimulationService == null)
  144. throw new Exception("Unable to load a required plugin, Gatekeeper Service cannot function.");
  145. IConfig presenceConfig = config.Configs["PresenceService"];
  146. if (presenceConfig != null)
  147. {
  148. m_allowDuplicatePresences = presenceConfig.GetBoolean("AllowDuplicatePresences", m_allowDuplicatePresences);
  149. }
  150. IConfig messagingConfig = config.Configs["Messaging"];
  151. if (messagingConfig != null)
  152. m_messageKey = messagingConfig.GetString("MessageKey", String.Empty);
  153. m_log.Debug("[GATEKEEPER SERVICE]: Starting...");
  154. }
  155. }
  156. public GatekeeperService(IConfigSource config)
  157. : this(config, null)
  158. {
  159. }
  160. protected void LoadDomainExceptionsFromConfig(IConfig config, string variable, List<string> exceptions)
  161. {
  162. string value = config.GetString(variable, string.Empty);
  163. string[] parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  164. foreach (string s in parts)
  165. exceptions.Add(s.Trim());
  166. }
  167. public bool LinkRegion(string regionName, out UUID regionID, out ulong regionHandle, out string externalName, out string imageURL, out string reason, out int sizeX, out int sizeY)
  168. {
  169. regionID = UUID.Zero;
  170. regionHandle = 0;
  171. sizeX = (int)Constants.RegionSize;
  172. sizeY = (int)Constants.RegionSize;
  173. externalName = m_gatekeeperURL + ((regionName != string.Empty) ? " " + regionName : "");
  174. imageURL = string.Empty;
  175. reason = string.Empty;
  176. GridRegion region = null;
  177. //m_log.DebugFormat("[GATEKEEPER SERVICE]: Request to link to {0}", (regionName == string.Empty)? "default region" : regionName);
  178. if (!m_AllowTeleportsToAnyRegion || regionName == string.Empty)
  179. {
  180. List<GridRegion> defs = m_GridService.GetDefaultHypergridRegions(m_ScopeID);
  181. if (defs != null && defs.Count > 0)
  182. {
  183. region = defs[0];
  184. m_DefaultGatewayRegion = region;
  185. }
  186. else
  187. {
  188. reason = "Grid setup problem. Try specifying a particular region here.";
  189. m_log.DebugFormat("[GATEKEEPER SERVICE]: Unable to send information. Please specify a default region for this grid!");
  190. return false;
  191. }
  192. }
  193. else
  194. {
  195. region = m_GridService.GetRegionByName(m_ScopeID, regionName);
  196. if (region == null)
  197. {
  198. reason = "Region not found";
  199. return false;
  200. }
  201. }
  202. regionID = region.RegionID;
  203. regionHandle = region.RegionHandle;
  204. sizeX = region.RegionSizeX;
  205. sizeY = region.RegionSizeY;
  206. string regionimage = "regionImage" + regionID.ToString();
  207. regionimage = regionimage.Replace("-", "");
  208. imageURL = region.ServerURI + "index.php?method=" + regionimage;
  209. return true;
  210. }
  211. public GridRegion GetHyperlinkRegion(UUID regionID, UUID agentID, string agentHomeURI, out string message)
  212. {
  213. message = null;
  214. if (!m_AllowTeleportsToAnyRegion)
  215. {
  216. // Don't even check the given regionID
  217. m_log.DebugFormat(
  218. "[GATEKEEPER SERVICE]: Returning gateway region {0} {1} @ {2} to user {3}{4} as teleporting to arbitrary regions is not allowed.",
  219. m_DefaultGatewayRegion.RegionName,
  220. m_DefaultGatewayRegion.RegionID,
  221. m_DefaultGatewayRegion.ServerURI,
  222. agentID,
  223. agentHomeURI == null ? "" : " @ " + agentHomeURI);
  224. message = "Teleporting to the default region.";
  225. return m_DefaultGatewayRegion;
  226. }
  227. GridRegion region = m_GridService.GetRegionByUUID(m_ScopeID, regionID);
  228. if (region == null)
  229. {
  230. m_log.DebugFormat(
  231. "[GATEKEEPER SERVICE]: Could not find region with ID {0} as requested by user {1}{2}. Returning null.",
  232. regionID, agentID, (agentHomeURI == null) ? "" : " @ " + agentHomeURI);
  233. message = "The teleport destination could not be found.";
  234. return null;
  235. }
  236. m_log.DebugFormat(
  237. "[GATEKEEPER SERVICE]: Returning region {0} {1} @ {2} to user {3}{4}.",
  238. region.RegionName,
  239. region.RegionID,
  240. region.ServerURI,
  241. agentID,
  242. agentHomeURI == null ? "" : " @ " + agentHomeURI);
  243. return region;
  244. }
  245. #region Login Agent
  246. public bool LoginAgent(GridRegion source, AgentCircuitData aCircuit, GridRegion destination, out string reason)
  247. {
  248. reason = string.Empty;
  249. string authURL = string.Empty;
  250. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  251. authURL = aCircuit.ServiceURLs["HomeURI"].ToString();
  252. m_log.InfoFormat("[GATEKEEPER SERVICE]: Login request for {0} {1} @ {2} ({3}) at {4} using viewer {5}, channel {6}, IP {7}, Mac {8}, Id0 {9}, Teleport Flags: {10}. From region {11}",
  253. aCircuit.firstname, aCircuit.lastname, authURL, aCircuit.AgentID, destination.RegionID,
  254. aCircuit.Viewer, aCircuit.Channel, aCircuit.IPAddress, aCircuit.Mac, aCircuit.Id0, (TeleportFlags)aCircuit.teleportFlags,
  255. (source == null) ? "Unknown" : string.Format("{0} ({1}){2}", source.RegionName, source.RegionID, (source.RawServerURI == null) ? "" : " @ " + source.ServerURI));
  256. string curViewer = Util.GetViewerName(aCircuit);
  257. string curMac = aCircuit.Mac.ToString();
  258. //
  259. // Check client
  260. //
  261. if (!String.IsNullOrWhiteSpace(m_AllowedClients))
  262. {
  263. Regex arx = new Regex(m_AllowedClients);
  264. Match am = arx.Match(curViewer);
  265. if (!am.Success)
  266. {
  267. reason = "Login failed: client " + curViewer + " is not allowed";
  268. m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client {0} is not allowed", curViewer);
  269. return false;
  270. }
  271. }
  272. if (!String.IsNullOrWhiteSpace(m_DeniedClients))
  273. {
  274. Regex drx = new Regex(m_DeniedClients);
  275. Match dm = drx.Match(curViewer);
  276. if (dm.Success)
  277. {
  278. reason = "Login failed: client " + curViewer + " is denied";
  279. m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client {0} is denied", curViewer);
  280. return false;
  281. }
  282. }
  283. if (!String.IsNullOrWhiteSpace(m_DeniedMacs))
  284. {
  285. m_log.InfoFormat("[GATEKEEPER SERVICE]: Checking users Mac {0} against list of denied macs {1} ...", curMac, m_DeniedMacs);
  286. if (m_DeniedMacs.Contains(curMac))
  287. {
  288. reason = "Login failed: client with Mac " + curMac + " is denied";
  289. m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: client with mac {0} is denied", curMac);
  290. return false;
  291. }
  292. }
  293. //
  294. // Authenticate the user
  295. //
  296. if (!Authenticate(aCircuit))
  297. {
  298. reason = "Unable to verify identity";
  299. m_log.InfoFormat("[GATEKEEPER SERVICE]: Unable to verify identity of agent {0} {1}. Refusing service.", aCircuit.firstname, aCircuit.lastname);
  300. return false;
  301. }
  302. m_log.DebugFormat("[GATEKEEPER SERVICE]: Identity verified for {0} {1} @ {2}", aCircuit.firstname, aCircuit.lastname, authURL);
  303. //
  304. // Check for impersonations
  305. //
  306. UserAccount account = null;
  307. if (m_UserAccountService != null)
  308. {
  309. // Check to see if we have a local user with that UUID
  310. account = m_UserAccountService.GetUserAccount(m_ScopeID, aCircuit.AgentID);
  311. if (account != null)
  312. {
  313. // Make sure this is the user coming home, and not a foreign user with same UUID as a local user
  314. if (m_UserAgentService != null)
  315. {
  316. if (!m_UserAgentService.IsAgentComingHome(aCircuit.SessionID, m_gatekeeperURL))
  317. {
  318. // Can't do, sorry
  319. reason = "Unauthorized";
  320. m_log.InfoFormat("[GATEKEEPER SERVICE]: Foreign agent {0} {1} has same ID as local user. Refusing service.",
  321. aCircuit.firstname, aCircuit.lastname);
  322. return false;
  323. }
  324. }
  325. }
  326. }
  327. //
  328. // Foreign agents allowed? Exceptions?
  329. //
  330. if (account == null)
  331. {
  332. bool allowed = m_ForeignAgentsAllowed;
  333. if (m_ForeignAgentsAllowed && IsException(aCircuit, m_ForeignsAllowedExceptions))
  334. allowed = false;
  335. if (!m_ForeignAgentsAllowed && IsException(aCircuit, m_ForeignsDisallowedExceptions))
  336. allowed = true;
  337. if (!allowed)
  338. {
  339. reason = "Destination does not allow visitors from your world";
  340. m_log.InfoFormat("[GATEKEEPER SERVICE]: Foreign agents are not permitted {0} {1} @ {2}. Refusing service.",
  341. aCircuit.firstname, aCircuit.lastname, aCircuit.ServiceURLs["HomeURI"]);
  342. return false;
  343. }
  344. }
  345. //
  346. // Is the user banned?
  347. // This uses a Ban service that's more powerful than the configs
  348. //
  349. string uui = (account != null ? aCircuit.AgentID.ToString() : Util.ProduceUserUniversalIdentifier(aCircuit));
  350. if (m_BansService != null && m_BansService.IsBanned(uui, aCircuit.IPAddress, aCircuit.Id0, authURL))
  351. {
  352. reason = "You are banned from this world";
  353. m_log.InfoFormat("[GATEKEEPER SERVICE]: Login failed, reason: user {0} is banned", uui);
  354. return false;
  355. }
  356. UUID agentID = aCircuit.AgentID;
  357. if(agentID == new UUID("6571e388-6218-4574-87db-f9379718315e"))
  358. {
  359. // really?
  360. reason = "Invalid account ID";
  361. return false;
  362. }
  363. if(m_GridUserService != null)
  364. {
  365. string PrincipalIDstr = agentID.ToString();
  366. GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(PrincipalIDstr);
  367. if(!m_allowDuplicatePresences)
  368. {
  369. if(guinfo != null && guinfo.Online && guinfo.LastRegionID != UUID.Zero)
  370. {
  371. if(SendAgentGodKillToRegion(UUID.Zero, agentID, guinfo))
  372. {
  373. if(account != null)
  374. m_log.InfoFormat(
  375. "[GATEKEEPER SERVICE]: Login failed for {0} {1}, reason: already logged in",
  376. account.FirstName, account.LastName);
  377. reason = "You appear to be already logged in on the destination grid " +
  378. "Please wait a a minute or two and retry. " +
  379. "If this takes longer than a few minutes please contact the grid owner.";
  380. return false;
  381. }
  382. }
  383. }
  384. }
  385. m_log.DebugFormat("[GATEKEEPER SERVICE]: User {0} is ok", aCircuit.Name);
  386. bool isFirstLogin = false;
  387. //
  388. // Login the presence, if it's not there yet (by the login service)
  389. //
  390. PresenceInfo presence = m_PresenceService.GetAgent(aCircuit.SessionID);
  391. if (presence != null) // it has been placed there by the login service
  392. isFirstLogin = true;
  393. else
  394. {
  395. if (!m_PresenceService.LoginAgent(aCircuit.AgentID.ToString(), aCircuit.SessionID, aCircuit.SecureSessionID))
  396. {
  397. reason = "Unable to login presence";
  398. m_log.InfoFormat("[GATEKEEPER SERVICE]: Presence login failed for foreign agent {0} {1}. Refusing service.",
  399. aCircuit.firstname, aCircuit.lastname);
  400. return false;
  401. }
  402. }
  403. //
  404. // Get the region
  405. //
  406. destination = m_GridService.GetRegionByUUID(m_ScopeID, destination.RegionID);
  407. if (destination == null)
  408. {
  409. reason = "Destination region not found";
  410. return false;
  411. }
  412. m_log.DebugFormat(
  413. "[GATEKEEPER SERVICE]: Destination {0} is ok for {1}", destination.RegionName, aCircuit.Name);
  414. //
  415. // Adjust the visible name
  416. //
  417. if (account != null)
  418. {
  419. aCircuit.firstname = account.FirstName;
  420. aCircuit.lastname = account.LastName;
  421. }
  422. if (account == null)
  423. {
  424. if (!aCircuit.lastname.StartsWith("@"))
  425. aCircuit.firstname = aCircuit.firstname + "." + aCircuit.lastname;
  426. try
  427. {
  428. Uri uri = new Uri(aCircuit.ServiceURLs["HomeURI"].ToString());
  429. aCircuit.lastname = "@" + uri.Authority;
  430. }
  431. catch
  432. {
  433. m_log.WarnFormat("[GATEKEEPER SERVICE]: Malformed HomeURI (this should never happen): {0}", aCircuit.ServiceURLs["HomeURI"]);
  434. aCircuit.lastname = "@" + aCircuit.ServiceURLs["HomeURI"].ToString();
  435. }
  436. }
  437. //
  438. // Finally launch the agent at the destination
  439. //
  440. Constants.TeleportFlags loginFlag = isFirstLogin ? Constants.TeleportFlags.ViaLogin : Constants.TeleportFlags.ViaHGLogin;
  441. // Preserve our TeleportFlags we have gathered so-far
  442. loginFlag |= (Constants.TeleportFlags) aCircuit.teleportFlags;
  443. m_log.DebugFormat("[GATEKEEPER SERVICE]: Launching {0}, Teleport Flags: {1}", aCircuit.Name, loginFlag);
  444. EntityTransferContext ctx = new EntityTransferContext();
  445. if (!m_SimulationService.QueryAccess(
  446. destination, aCircuit.AgentID, aCircuit.ServiceURLs["HomeURI"].ToString(),
  447. true, aCircuit.startpos, new List<UUID>(), ctx, out reason))
  448. return false;
  449. bool didit = m_SimulationService.CreateAgent(source, destination, aCircuit, (uint)loginFlag, ctx, out reason);
  450. if(didit)
  451. {
  452. m_log.DebugFormat("[GATEKEEPER SERVICE]: Login presence {0} is ok", aCircuit.Name);
  453. if(!isFirstLogin && m_GridUserService != null && account == null)
  454. {
  455. // Also login foreigners with GridUser service
  456. string userId = aCircuit.AgentID.ToString();
  457. string first = aCircuit.firstname, last = aCircuit.lastname;
  458. if (last.StartsWith("@"))
  459. {
  460. string[] parts = aCircuit.firstname.Split('.');
  461. if (parts.Length >= 2)
  462. {
  463. first = parts[0];
  464. last = parts[1];
  465. }
  466. }
  467. userId += ";" + aCircuit.ServiceURLs["HomeURI"] + ";" + first + " " + last;
  468. m_GridUserService.LoggedIn(userId);
  469. }
  470. }
  471. return didit;
  472. }
  473. protected bool Authenticate(AgentCircuitData aCircuit)
  474. {
  475. if (!CheckAddress(aCircuit.ServiceSessionID))
  476. return false;
  477. if (string.IsNullOrEmpty(aCircuit.IPAddress))
  478. {
  479. m_log.DebugFormat("[GATEKEEPER SERVICE]: Agent did not provide a client IP address.");
  480. return false;
  481. }
  482. string userURL = string.Empty;
  483. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  484. userURL = aCircuit.ServiceURLs["HomeURI"].ToString();
  485. OSHHTPHost userHomeHost = new OSHHTPHost(userURL, true);
  486. if(!userHomeHost.IsResolvedHost)
  487. {
  488. m_log.DebugFormat("[GATEKEEPER SERVICE]: Agent did not provide an authentication server URL");
  489. return false;
  490. }
  491. if (m_gatekeeperHost.Equals(userHomeHost))
  492. {
  493. return m_UserAgentService.VerifyAgent(aCircuit.SessionID, aCircuit.ServiceSessionID);
  494. }
  495. else
  496. {
  497. IUserAgentService userAgentService = new UserAgentServiceConnector(userURL);
  498. try
  499. {
  500. return userAgentService.VerifyAgent(aCircuit.SessionID, aCircuit.ServiceSessionID);
  501. }
  502. catch
  503. {
  504. m_log.DebugFormat("[GATEKEEPER SERVICE]: Unable to contact authentication service at {0}", userURL);
  505. return false;
  506. }
  507. }
  508. }
  509. // Check that the service token was generated for *this* grid.
  510. // If it wasn't then that's a fake agent.
  511. protected bool CheckAddress(string serviceToken)
  512. {
  513. string[] parts = serviceToken.Split(new char[] { ';' });
  514. if (parts.Length < 2)
  515. return false;
  516. OSHHTPHost reqGrid = new OSHHTPHost(parts[0], false);
  517. if(!reqGrid.IsValidHost)
  518. {
  519. m_log.DebugFormat("[GATEKEEPER SERVICE]: Visitor provided malformed gird address {0}", parts[0]);
  520. return false;
  521. }
  522. m_log.DebugFormat("[GATEKEEPER SERVICE]: Verifying grid {0} against {1}", reqGrid.URI, m_gatekeeperHost.URI);
  523. if(m_gatekeeperHost.Equals(reqGrid))
  524. return true;
  525. if (m_gateKeeperAlias != null && m_gateKeeperAlias.Contains(reqGrid))
  526. return true;
  527. return false;
  528. }
  529. #endregion
  530. #region Misc
  531. private bool IsException(AgentCircuitData aCircuit, List<string> exceptions)
  532. {
  533. bool exception = false;
  534. if (exceptions.Count > 0) // we have exceptions
  535. {
  536. // Retrieve the visitor's origin
  537. string userURL = aCircuit.ServiceURLs["HomeURI"].ToString();
  538. if (!userURL.EndsWith("/"))
  539. userURL += "/";
  540. if (exceptions.Find(delegate(string s)
  541. {
  542. if (!s.EndsWith("/"))
  543. s += "/";
  544. return s == userURL;
  545. }) != null)
  546. exception = true;
  547. }
  548. return exception;
  549. }
  550. private bool SendAgentGodKillToRegion(UUID scopeID, UUID agentID , GridUserInfo guinfo)
  551. {
  552. UUID regionID = guinfo.LastRegionID;
  553. GridRegion regInfo = m_GridService.GetRegionByUUID(scopeID, regionID);
  554. if(regInfo == null)
  555. return false;
  556. string regURL = regInfo.ServerURI;
  557. if(string.IsNullOrEmpty(regURL))
  558. return false;
  559. GridInstantMessage msg = new GridInstantMessage();
  560. msg.imSessionID = UUID.Zero.Guid;
  561. msg.fromAgentID = Constants.servicesGodAgentID.Guid;
  562. msg.toAgentID = agentID.Guid;
  563. msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  564. msg.fromAgentName = "GRID";
  565. msg.message = string.Format("New login detected");
  566. msg.dialog = 250; // God kick
  567. msg.fromGroup = false;
  568. msg.offline = (byte)0;
  569. msg.ParentEstateID = 0;
  570. msg.Position = Vector3.Zero;
  571. msg.RegionID = scopeID.Guid;
  572. msg.binaryBucket = new byte[1] {0};
  573. InstantMessageServiceConnector.SendInstantMessage(regURL,msg, m_messageKey);
  574. m_GridUserService.LoggedOut(agentID.ToString(),
  575. UUID.Zero, guinfo.LastRegionID, guinfo.LastPosition, guinfo.LastLookAt);
  576. return true;
  577. }
  578. #endregion
  579. }
  580. }