LLLoginService.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 OpenSim.Framework;
  37. using OpenSim.Framework.Capabilities;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Server.Base;
  40. using OpenSim.Services.Interfaces;
  41. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  42. using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  43. using OpenSim.Services.Connectors.Hypergrid;
  44. namespace OpenSim.Services.LLLoginService
  45. {
  46. public class LLLoginService : ILoginService
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private static bool Initialized = false;
  50. protected IUserAccountService m_UserAccountService;
  51. protected IGridUserService m_GridUserService;
  52. protected IAuthenticationService m_AuthenticationService;
  53. protected IInventoryService m_InventoryService;
  54. protected IGridService m_GridService;
  55. protected IPresenceService m_PresenceService;
  56. protected ISimulationService m_LocalSimulationService;
  57. protected ISimulationService m_RemoteSimulationService;
  58. protected ILibraryService m_LibraryService;
  59. protected IFriendsService m_FriendsService;
  60. protected IAvatarService m_AvatarService;
  61. protected IUserAgentService m_UserAgentService;
  62. protected GatekeeperServiceConnector m_GatekeeperConnector;
  63. protected string m_DefaultRegionName;
  64. protected string m_WelcomeMessage;
  65. protected bool m_RequireInventory;
  66. protected int m_MinLoginLevel;
  67. protected string m_GatekeeperURL;
  68. protected bool m_AllowRemoteSetLoginLevel;
  69. protected string m_MapTileURL;
  70. protected string m_SearchURL;
  71. IConfig m_LoginServerConfig;
  72. public LLLoginService(IConfigSource config, ISimulationService simService, ILibraryService libraryService)
  73. {
  74. m_LoginServerConfig = config.Configs["LoginService"];
  75. if (m_LoginServerConfig == null)
  76. throw new Exception(String.Format("No section LoginService in config file"));
  77. string accountService = m_LoginServerConfig.GetString("UserAccountService", String.Empty);
  78. string gridUserService = m_LoginServerConfig.GetString("GridUserService", String.Empty);
  79. string agentService = m_LoginServerConfig.GetString("UserAgentService", String.Empty);
  80. string authService = m_LoginServerConfig.GetString("AuthenticationService", String.Empty);
  81. string invService = m_LoginServerConfig.GetString("InventoryService", String.Empty);
  82. string gridService = m_LoginServerConfig.GetString("GridService", String.Empty);
  83. string presenceService = m_LoginServerConfig.GetString("PresenceService", String.Empty);
  84. string libService = m_LoginServerConfig.GetString("LibraryService", String.Empty);
  85. string friendsService = m_LoginServerConfig.GetString("FriendsService", String.Empty);
  86. string avatarService = m_LoginServerConfig.GetString("AvatarService", String.Empty);
  87. string simulationService = m_LoginServerConfig.GetString("SimulationService", String.Empty);
  88. m_DefaultRegionName = m_LoginServerConfig.GetString("DefaultRegion", String.Empty);
  89. m_WelcomeMessage = m_LoginServerConfig.GetString("WelcomeMessage", "Welcome to OpenSim!");
  90. m_RequireInventory = m_LoginServerConfig.GetBoolean("RequireInventory", true);
  91. m_AllowRemoteSetLoginLevel = m_LoginServerConfig.GetBoolean("AllowRemoteSetLoginLevel", false);
  92. m_MinLoginLevel = m_LoginServerConfig.GetInt("MinLoginLevel", 0);
  93. m_GatekeeperURL = m_LoginServerConfig.GetString("GatekeeperURI", string.Empty);
  94. m_MapTileURL = m_LoginServerConfig.GetString("MapTileURL", string.Empty);
  95. m_SearchURL = m_LoginServerConfig.GetString("SearchURL", string.Empty);
  96. // These are required; the others aren't
  97. if (accountService == string.Empty || authService == string.Empty)
  98. throw new Exception("LoginService is missing service specifications");
  99. Object[] args = new Object[] { config };
  100. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(accountService, args);
  101. m_GridUserService = ServerUtils.LoadPlugin<IGridUserService>(gridUserService, args);
  102. m_AuthenticationService = ServerUtils.LoadPlugin<IAuthenticationService>(authService, args);
  103. m_InventoryService = ServerUtils.LoadPlugin<IInventoryService>(invService, args);
  104. if (gridService != string.Empty)
  105. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  106. if (presenceService != string.Empty)
  107. m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args);
  108. if (avatarService != string.Empty)
  109. m_AvatarService = ServerUtils.LoadPlugin<IAvatarService>(avatarService, args);
  110. if (friendsService != string.Empty)
  111. m_FriendsService = ServerUtils.LoadPlugin<IFriendsService>(friendsService, args);
  112. if (simulationService != string.Empty)
  113. m_RemoteSimulationService = ServerUtils.LoadPlugin<ISimulationService>(simulationService, args);
  114. if (agentService != string.Empty)
  115. m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(agentService, args);
  116. //
  117. // deal with the services given as argument
  118. //
  119. m_LocalSimulationService = simService;
  120. if (libraryService != null)
  121. {
  122. m_log.DebugFormat("[LLOGIN SERVICE]: Using LibraryService given as argument");
  123. m_LibraryService = libraryService;
  124. }
  125. else if (libService != string.Empty)
  126. {
  127. m_log.DebugFormat("[LLOGIN SERVICE]: Using instantiated LibraryService");
  128. m_LibraryService = ServerUtils.LoadPlugin<ILibraryService>(libService, args);
  129. }
  130. m_GatekeeperConnector = new GatekeeperServiceConnector();
  131. if (!Initialized)
  132. {
  133. Initialized = true;
  134. RegisterCommands();
  135. }
  136. m_log.DebugFormat("[LLOGIN SERVICE]: Starting...");
  137. }
  138. public LLLoginService(IConfigSource config) : this(config, null, null)
  139. {
  140. }
  141. public Hashtable SetLevel(string firstName, string lastName, string passwd, int level, IPEndPoint clientIP)
  142. {
  143. Hashtable response = new Hashtable();
  144. response["success"] = "false";
  145. if (!m_AllowRemoteSetLoginLevel)
  146. return response;
  147. try
  148. {
  149. UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero, firstName, lastName);
  150. if (account == null)
  151. {
  152. m_log.InfoFormat("[LLOGIN SERVICE]: Set Level failed, user {0} {1} not found", firstName, lastName);
  153. return response;
  154. }
  155. if (account.UserLevel < 200)
  156. {
  157. m_log.InfoFormat("[LLOGIN SERVICE]: Set Level failed, reason: user level too low");
  158. return response;
  159. }
  160. //
  161. // Authenticate this user
  162. //
  163. // We don't support clear passwords here
  164. //
  165. string token = m_AuthenticationService.Authenticate(account.PrincipalID, passwd, 30);
  166. UUID secureSession = UUID.Zero;
  167. if ((token == string.Empty) || (token != string.Empty && !UUID.TryParse(token, out secureSession)))
  168. {
  169. m_log.InfoFormat("[LLOGIN SERVICE]: SetLevel failed, reason: authentication failed");
  170. return response;
  171. }
  172. }
  173. catch (Exception e)
  174. {
  175. m_log.Error("[LLOGIN SERVICE]: SetLevel failed, exception " + e.ToString());
  176. return response;
  177. }
  178. m_MinLoginLevel = level;
  179. m_log.InfoFormat("[LLOGIN SERVICE]: Login level set to {0} by {1} {2}", level, firstName, lastName);
  180. response["success"] = true;
  181. return response;
  182. }
  183. public LoginResponse Login(string firstName, string lastName, string passwd, string startLocation, UUID scopeID, string clientVersion, IPEndPoint clientIP)
  184. {
  185. bool success = false;
  186. UUID session = UUID.Random();
  187. m_log.InfoFormat("[LLOGIN SERVICE]: Login request for {0} {1} from {2} with user agent {3} starting in {4}",
  188. firstName, lastName, clientIP.Address.ToString(), clientVersion, startLocation);
  189. try
  190. {
  191. //
  192. // Get the account and check that it exists
  193. //
  194. UserAccount account = m_UserAccountService.GetUserAccount(scopeID, firstName, lastName);
  195. if (account == null)
  196. {
  197. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: user not found");
  198. return LLFailedLoginResponse.UserProblem;
  199. }
  200. if (account.UserLevel < m_MinLoginLevel)
  201. {
  202. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: login is blocked for user level {0}", account.UserLevel);
  203. return LLFailedLoginResponse.LoginBlockedProblem;
  204. }
  205. // If a scope id is requested, check that the account is in
  206. // that scope, or unscoped.
  207. //
  208. if (scopeID != UUID.Zero)
  209. {
  210. if (account.ScopeID != scopeID && account.ScopeID != UUID.Zero)
  211. {
  212. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: user not found");
  213. return LLFailedLoginResponse.UserProblem;
  214. }
  215. }
  216. else
  217. {
  218. scopeID = account.ScopeID;
  219. }
  220. //
  221. // Authenticate this user
  222. //
  223. if (!passwd.StartsWith("$1$"))
  224. passwd = "$1$" + Util.Md5Hash(passwd);
  225. passwd = passwd.Remove(0, 3); //remove $1$
  226. string token = m_AuthenticationService.Authenticate(account.PrincipalID, passwd, 30);
  227. UUID secureSession = UUID.Zero;
  228. if ((token == string.Empty) || (token != string.Empty && !UUID.TryParse(token, out secureSession)))
  229. {
  230. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: authentication failed");
  231. return LLFailedLoginResponse.UserProblem;
  232. }
  233. //
  234. // Get the user's inventory
  235. //
  236. if (m_RequireInventory && m_InventoryService == null)
  237. {
  238. m_log.WarnFormat("[LLOGIN SERVICE]: Login failed, reason: inventory service not set up");
  239. return LLFailedLoginResponse.InventoryProblem;
  240. }
  241. List<InventoryFolderBase> inventorySkel = m_InventoryService.GetInventorySkeleton(account.PrincipalID);
  242. if (m_RequireInventory && ((inventorySkel == null) || (inventorySkel != null && inventorySkel.Count == 0)))
  243. {
  244. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: unable to retrieve user inventory");
  245. return LLFailedLoginResponse.InventoryProblem;
  246. }
  247. // Get active gestures
  248. List<InventoryItemBase> gestures = m_InventoryService.GetActiveGestures(account.PrincipalID);
  249. m_log.DebugFormat("[LLOGIN SERVICE]: {0} active gestures", gestures.Count);
  250. //
  251. // Login the presence
  252. //
  253. if (m_PresenceService != null)
  254. {
  255. success = m_PresenceService.LoginAgent(account.PrincipalID.ToString(), session, secureSession);
  256. if (!success)
  257. {
  258. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: could not login presence");
  259. return LLFailedLoginResponse.GridProblem;
  260. }
  261. }
  262. //
  263. // Change Online status and get the home region
  264. //
  265. GridRegion home = null;
  266. GridUserInfo guinfo = m_GridUserService.LoggedIn(account.PrincipalID.ToString());
  267. if (guinfo != null && (guinfo.HomeRegionID != UUID.Zero) && m_GridService != null)
  268. {
  269. home = m_GridService.GetRegionByUUID(scopeID, guinfo.HomeRegionID);
  270. }
  271. if (guinfo == null)
  272. {
  273. // something went wrong, make something up, so that we don't have to test this anywhere else
  274. guinfo = new GridUserInfo();
  275. guinfo.LastPosition = guinfo.HomePosition = new Vector3(128, 128, 30);
  276. }
  277. //
  278. // Find the destination region/grid
  279. //
  280. string where = string.Empty;
  281. Vector3 position = Vector3.Zero;
  282. Vector3 lookAt = Vector3.Zero;
  283. GridRegion gatekeeper = null;
  284. GridRegion destination = FindDestination(account, scopeID, guinfo, session, startLocation, home, out gatekeeper, out where, out position, out lookAt);
  285. if (destination == null)
  286. {
  287. m_PresenceService.LogoutAgent(session);
  288. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: destination not found");
  289. return LLFailedLoginResponse.GridProblem;
  290. }
  291. //
  292. // Get the avatar
  293. //
  294. AvatarData avatar = null;
  295. if (m_AvatarService != null)
  296. {
  297. avatar = m_AvatarService.GetAvatar(account.PrincipalID);
  298. }
  299. //
  300. // Instantiate/get the simulation interface and launch an agent at the destination
  301. //
  302. string reason = string.Empty;
  303. AgentCircuitData aCircuit = LaunchAgentAtGrid(gatekeeper, destination, account, avatar, session, secureSession, position, where, clientVersion, clientIP, out where, out reason);
  304. if (aCircuit == null)
  305. {
  306. m_PresenceService.LogoutAgent(session);
  307. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: {0}", reason);
  308. return LLFailedLoginResponse.AuthorizationProblem;
  309. }
  310. // Get Friends list
  311. FriendInfo[] friendsList = new FriendInfo[0];
  312. if (m_FriendsService != null)
  313. {
  314. friendsList = m_FriendsService.GetFriends(account.PrincipalID);
  315. m_log.DebugFormat("[LLOGIN SERVICE]: Retrieved {0} friends", friendsList.Length);
  316. }
  317. //
  318. // Finally, fill out the response and return it
  319. //
  320. LLLoginResponse response = new LLLoginResponse(account, aCircuit, guinfo, destination, inventorySkel, friendsList, m_LibraryService,
  321. where, startLocation, position, lookAt, gestures, m_WelcomeMessage, home, clientIP, m_MapTileURL, m_SearchURL);
  322. m_log.DebugFormat("[LLOGIN SERVICE]: All clear. Sending login response to client.");
  323. return response;
  324. }
  325. catch (Exception e)
  326. {
  327. m_log.WarnFormat("[LLOGIN SERVICE]: Exception processing login for {0} {1}: {2} {3}", firstName, lastName, e.ToString(), e.StackTrace);
  328. if (m_PresenceService != null)
  329. m_PresenceService.LogoutAgent(session);
  330. return LLFailedLoginResponse.InternalError;
  331. }
  332. }
  333. protected GridRegion FindDestination(UserAccount account, UUID scopeID, GridUserInfo pinfo, UUID sessionID, string startLocation, GridRegion home, out GridRegion gatekeeper, out string where, out Vector3 position, out Vector3 lookAt)
  334. {
  335. m_log.DebugFormat("[LLOGIN SERVICE]: FindDestination for start location {0}", startLocation);
  336. gatekeeper = null;
  337. where = "home";
  338. position = new Vector3(128, 128, 0);
  339. lookAt = new Vector3(0, 1, 0);
  340. if (m_GridService == null)
  341. return null;
  342. if (startLocation.Equals("home"))
  343. {
  344. // logging into home region
  345. if (pinfo == null)
  346. return null;
  347. GridRegion region = null;
  348. bool tryDefaults = false;
  349. if (home == null)
  350. {
  351. m_log.WarnFormat(
  352. "[LLOGIN SERVICE]: User {0} {1} tried to login to a 'home' start location but they have none set",
  353. account.FirstName, account.LastName);
  354. tryDefaults = true;
  355. }
  356. else
  357. {
  358. region = home;
  359. position = pinfo.HomePosition;
  360. lookAt = pinfo.HomeLookAt;
  361. }
  362. if (tryDefaults)
  363. {
  364. List<GridRegion> defaults = m_GridService.GetDefaultRegions(scopeID);
  365. if (defaults != null && defaults.Count > 0)
  366. {
  367. region = defaults[0];
  368. where = "safe";
  369. }
  370. else
  371. {
  372. m_log.WarnFormat("[LLOGIN SERVICE]: User {0} {1} does not have a valid home and this grid does not have default locations. Attempting to find random region",
  373. account.FirstName, account.LastName);
  374. defaults = m_GridService.GetRegionsByName(scopeID, "", 1);
  375. if (defaults != null && defaults.Count > 0)
  376. {
  377. region = defaults[0];
  378. where = "safe";
  379. }
  380. }
  381. }
  382. return region;
  383. }
  384. else if (startLocation.Equals("last"))
  385. {
  386. // logging into last visited region
  387. where = "last";
  388. if (pinfo == null)
  389. return null;
  390. GridRegion region = null;
  391. if (pinfo.LastRegionID.Equals(UUID.Zero) || (region = m_GridService.GetRegionByUUID(scopeID, pinfo.LastRegionID)) == null)
  392. {
  393. List<GridRegion> defaults = m_GridService.GetDefaultRegions(scopeID);
  394. if (defaults != null && defaults.Count > 0)
  395. {
  396. region = defaults[0];
  397. where = "safe";
  398. }
  399. else
  400. {
  401. m_log.Info("[LLOGIN SERVICE]: Last Region Not Found Attempting to find random region");
  402. defaults = m_GridService.GetRegionsByName(scopeID, "", 1);
  403. if (defaults != null && defaults.Count > 0)
  404. {
  405. region = defaults[0];
  406. where = "safe";
  407. }
  408. }
  409. }
  410. else
  411. {
  412. position = pinfo.LastPosition;
  413. lookAt = pinfo.LastLookAt;
  414. }
  415. return region;
  416. }
  417. else
  418. {
  419. // free uri form
  420. // e.g. New Moon&135&46 New [email protected]:8002&153&34
  421. where = "url";
  422. Regex reURI = new Regex(@"^uri:(?<region>[^&]+)&(?<x>\d+)&(?<y>\d+)&(?<z>\d+)$");
  423. Match uriMatch = reURI.Match(startLocation);
  424. if (uriMatch == null)
  425. {
  426. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, but can't process it", startLocation);
  427. return null;
  428. }
  429. else
  430. {
  431. position = new Vector3(float.Parse(uriMatch.Groups["x"].Value, Culture.NumberFormatInfo),
  432. float.Parse(uriMatch.Groups["y"].Value, Culture.NumberFormatInfo),
  433. float.Parse(uriMatch.Groups["z"].Value, Culture.NumberFormatInfo));
  434. string regionName = uriMatch.Groups["region"].ToString();
  435. if (regionName != null)
  436. {
  437. if (!regionName.Contains("@"))
  438. {
  439. List<GridRegion> regions = m_GridService.GetRegionsByName(scopeID, regionName, 1);
  440. if ((regions == null) || (regions != null && regions.Count == 0))
  441. {
  442. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, can't locate region {1}. Trying defaults.", startLocation, regionName);
  443. regions = m_GridService.GetDefaultRegions(scopeID);
  444. if (regions != null && regions.Count > 0)
  445. {
  446. where = "safe";
  447. return regions[0];
  448. }
  449. else
  450. {
  451. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, Grid does not provide default regions.", startLocation);
  452. return null;
  453. }
  454. }
  455. return regions[0];
  456. }
  457. else
  458. {
  459. if (m_UserAgentService == null)
  460. {
  461. m_log.WarnFormat("[LLLOGIN SERVICE]: This llogin service is not running a user agent service, as such it can't lauch agents at foreign grids");
  462. return null;
  463. }
  464. string[] parts = regionName.Split(new char[] { '@' });
  465. if (parts.Length < 2)
  466. {
  467. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, can't locate region {1}", startLocation, regionName);
  468. return null;
  469. }
  470. // Valid specification of a remote grid
  471. regionName = parts[0];
  472. string domainLocator = parts[1];
  473. parts = domainLocator.Split(new char[] {':'});
  474. string domainName = parts[0];
  475. uint port = 0;
  476. if (parts.Length > 1)
  477. UInt32.TryParse(parts[1], out port);
  478. GridRegion region = FindForeignRegion(domainName, port, regionName, out gatekeeper);
  479. return region;
  480. }
  481. }
  482. else
  483. {
  484. List<GridRegion> defaults = m_GridService.GetDefaultRegions(scopeID);
  485. if (defaults != null && defaults.Count > 0)
  486. {
  487. where = "safe";
  488. return defaults[0];
  489. }
  490. else
  491. return null;
  492. }
  493. }
  494. //response.LookAt = "[r0,r1,r0]";
  495. //// can be: last, home, safe, url
  496. //response.StartLocation = "url";
  497. }
  498. }
  499. private GridRegion FindForeignRegion(string domainName, uint port, string regionName, out GridRegion gatekeeper)
  500. {
  501. gatekeeper = new GridRegion();
  502. gatekeeper.ExternalHostName = domainName;
  503. gatekeeper.HttpPort = port;
  504. gatekeeper.RegionName = regionName;
  505. gatekeeper.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  506. UUID regionID;
  507. ulong handle;
  508. string imageURL = string.Empty, reason = string.Empty;
  509. if (m_GatekeeperConnector.LinkRegion(gatekeeper, out regionID, out handle, out domainName, out imageURL, out reason))
  510. {
  511. GridRegion destination = m_GatekeeperConnector.GetHyperlinkRegion(gatekeeper, regionID);
  512. return destination;
  513. }
  514. return null;
  515. }
  516. private string hostName = string.Empty;
  517. private int port = 0;
  518. private void SetHostAndPort(string url)
  519. {
  520. try
  521. {
  522. Uri uri = new Uri(url);
  523. hostName = uri.Host;
  524. port = uri.Port;
  525. }
  526. catch
  527. {
  528. m_log.WarnFormat("[LLLogin SERVICE]: Unable to parse GatekeeperURL {0}", url);
  529. }
  530. }
  531. protected AgentCircuitData LaunchAgentAtGrid(GridRegion gatekeeper, GridRegion destination, UserAccount account, AvatarData avatar,
  532. UUID session, UUID secureSession, Vector3 position, string currentWhere, string viewer, IPEndPoint clientIP, out string where, out string reason)
  533. {
  534. where = currentWhere;
  535. ISimulationService simConnector = null;
  536. reason = string.Empty;
  537. uint circuitCode = 0;
  538. AgentCircuitData aCircuit = null;
  539. if (m_UserAgentService == null)
  540. {
  541. // HG standalones have both a localSimulatonDll and a remoteSimulationDll
  542. // non-HG standalones have just a localSimulationDll
  543. // independent login servers have just a remoteSimulationDll
  544. if (m_LocalSimulationService != null)
  545. simConnector = m_LocalSimulationService;
  546. else if (m_RemoteSimulationService != null)
  547. simConnector = m_RemoteSimulationService;
  548. }
  549. else // User Agent Service is on
  550. {
  551. if (gatekeeper == null) // login to local grid
  552. {
  553. if (hostName == string.Empty)
  554. SetHostAndPort(m_GatekeeperURL);
  555. gatekeeper = new GridRegion(destination);
  556. gatekeeper.ExternalHostName = hostName;
  557. gatekeeper.HttpPort = (uint)port;
  558. }
  559. else // login to foreign grid
  560. {
  561. }
  562. }
  563. bool success = false;
  564. if (m_UserAgentService == null && simConnector != null)
  565. {
  566. circuitCode = (uint)Util.RandomClass.Next(); ;
  567. aCircuit = MakeAgent(destination, account, avatar, session, secureSession, circuitCode, position, viewer);
  568. success = LaunchAgentDirectly(simConnector, destination, aCircuit, out reason);
  569. if (!success && m_GridService != null)
  570. {
  571. // Try the fallback regions
  572. List<GridRegion> fallbacks = m_GridService.GetFallbackRegions(account.ScopeID, destination.RegionLocX, destination.RegionLocY);
  573. if (fallbacks != null)
  574. {
  575. foreach (GridRegion r in fallbacks)
  576. {
  577. success = LaunchAgentDirectly(simConnector, r, aCircuit, out reason);
  578. if (success)
  579. {
  580. where = "safe";
  581. destination = r;
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. if (m_UserAgentService != null)
  589. {
  590. circuitCode = (uint)Util.RandomClass.Next(); ;
  591. aCircuit = MakeAgent(destination, account, avatar, session, secureSession, circuitCode, position, viewer);
  592. success = LaunchAgentIndirectly(gatekeeper, destination, aCircuit, clientIP, out reason);
  593. if (!success && m_GridService != null)
  594. {
  595. // Try the fallback regions
  596. List<GridRegion> fallbacks = m_GridService.GetFallbackRegions(account.ScopeID, destination.RegionLocX, destination.RegionLocY);
  597. if (fallbacks != null)
  598. {
  599. foreach (GridRegion r in fallbacks)
  600. {
  601. success = LaunchAgentIndirectly(gatekeeper, r, aCircuit, clientIP, out reason);
  602. if (success)
  603. {
  604. where = "safe";
  605. destination = r;
  606. break;
  607. }
  608. }
  609. }
  610. }
  611. }
  612. if (success)
  613. return aCircuit;
  614. else
  615. return null;
  616. }
  617. private AgentCircuitData MakeAgent(GridRegion region, UserAccount account,
  618. AvatarData avatar, UUID session, UUID secureSession, uint circuit, Vector3 position, string viewer)
  619. {
  620. AgentCircuitData aCircuit = new AgentCircuitData();
  621. aCircuit.AgentID = account.PrincipalID;
  622. if (avatar != null)
  623. aCircuit.Appearance = avatar.ToAvatarAppearance(account.PrincipalID);
  624. else
  625. aCircuit.Appearance = new AvatarAppearance(account.PrincipalID);
  626. //aCircuit.BaseFolder = irrelevant
  627. aCircuit.CapsPath = CapsUtil.GetRandomCapsObjectPath();
  628. aCircuit.child = false; // the first login agent is root
  629. aCircuit.ChildrenCapSeeds = new Dictionary<ulong, string>();
  630. aCircuit.circuitcode = circuit;
  631. aCircuit.firstname = account.FirstName;
  632. //aCircuit.InventoryFolder = irrelevant
  633. aCircuit.lastname = account.LastName;
  634. aCircuit.SecureSessionID = secureSession;
  635. aCircuit.SessionID = session;
  636. aCircuit.startpos = position;
  637. aCircuit.Viewer = viewer;
  638. SetServiceURLs(aCircuit, account);
  639. return aCircuit;
  640. //m_UserAgentService.LoginAgentToGrid(aCircuit, GatekeeperServiceConnector, region, out reason);
  641. //if (simConnector.CreateAgent(region, aCircuit, 0, out reason))
  642. // return aCircuit;
  643. //return null;
  644. }
  645. private void SetServiceURLs(AgentCircuitData aCircuit, UserAccount account)
  646. {
  647. aCircuit.ServiceURLs = new Dictionary<string, object>();
  648. if (account.ServiceURLs == null)
  649. return;
  650. foreach (KeyValuePair<string, object> kvp in account.ServiceURLs)
  651. {
  652. if (kvp.Value == null || (kvp.Value != null && kvp.Value.ToString() == string.Empty))
  653. {
  654. aCircuit.ServiceURLs[kvp.Key] = m_LoginServerConfig.GetString(kvp.Key, string.Empty);
  655. }
  656. else
  657. {
  658. aCircuit.ServiceURLs[kvp.Key] = kvp.Value;
  659. }
  660. }
  661. }
  662. private bool LaunchAgentDirectly(ISimulationService simConnector, GridRegion region, AgentCircuitData aCircuit, out string reason)
  663. {
  664. return simConnector.CreateAgent(region, aCircuit, (int)Constants.TeleportFlags.ViaLogin, out reason);
  665. }
  666. private bool LaunchAgentIndirectly(GridRegion gatekeeper, GridRegion destination, AgentCircuitData aCircuit, IPEndPoint clientIP, out string reason)
  667. {
  668. m_log.Debug("[LLOGIN SERVICE] Launching agent at " + destination.RegionName);
  669. if (m_UserAgentService.LoginAgentToGrid(aCircuit, gatekeeper, destination, clientIP, out reason))
  670. return true;
  671. return false;
  672. }
  673. #region Console Commands
  674. private void RegisterCommands()
  675. {
  676. //MainConsole.Instance.Commands.AddCommand
  677. MainConsole.Instance.Commands.AddCommand("loginservice", false, "login level",
  678. "login level <level>",
  679. "Set the minimum user level to log in", HandleLoginCommand);
  680. MainConsole.Instance.Commands.AddCommand("loginservice", false, "login reset",
  681. "login reset",
  682. "Reset the login level to allow all users",
  683. HandleLoginCommand);
  684. MainConsole.Instance.Commands.AddCommand("loginservice", false, "login text",
  685. "login text <text>",
  686. "Set the text users will see on login", HandleLoginCommand);
  687. }
  688. private void HandleLoginCommand(string module, string[] cmd)
  689. {
  690. string subcommand = cmd[1];
  691. switch (subcommand)
  692. {
  693. case "level":
  694. // Set the minimum level to allow login
  695. // Useful to allow grid update without worrying about users.
  696. // or fixing critical issues
  697. //
  698. if (cmd.Length > 2)
  699. Int32.TryParse(cmd[2], out m_MinLoginLevel);
  700. break;
  701. case "reset":
  702. m_MinLoginLevel = 0;
  703. break;
  704. case "text":
  705. if (cmd.Length > 2)
  706. m_WelcomeMessage = cmd[2];
  707. break;
  708. }
  709. }
  710. }
  711. #endregion
  712. }