GatekeeperService.cs 28 KB

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