LLLoginService.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. GridRegion dest;
  304. AgentCircuitData aCircuit = LaunchAgentAtGrid(gatekeeper, destination, account, avatar, session, secureSession, position, where, clientVersion, clientIP, out where, out reason, out dest);
  305. destination = dest;
  306. if (aCircuit == null)
  307. {
  308. m_PresenceService.LogoutAgent(session);
  309. m_log.InfoFormat("[LLOGIN SERVICE]: Login failed, reason: {0}", reason);
  310. return new LLFailedLoginResponse("key", reason, "false");
  311. }
  312. // Get Friends list
  313. FriendInfo[] friendsList = new FriendInfo[0];
  314. if (m_FriendsService != null)
  315. {
  316. friendsList = m_FriendsService.GetFriends(account.PrincipalID);
  317. m_log.DebugFormat("[LLOGIN SERVICE]: Retrieved {0} friends", friendsList.Length);
  318. }
  319. //
  320. // Finally, fill out the response and return it
  321. //
  322. LLLoginResponse response = new LLLoginResponse(account, aCircuit, guinfo, destination, inventorySkel, friendsList, m_LibraryService,
  323. where, startLocation, position, lookAt, gestures, m_WelcomeMessage, home, clientIP, m_MapTileURL, m_SearchURL);
  324. m_log.DebugFormat("[LLOGIN SERVICE]: All clear. Sending login response to client.");
  325. return response;
  326. }
  327. catch (Exception e)
  328. {
  329. m_log.WarnFormat("[LLOGIN SERVICE]: Exception processing login for {0} {1}: {2} {3}", firstName, lastName, e.ToString(), e.StackTrace);
  330. if (m_PresenceService != null)
  331. m_PresenceService.LogoutAgent(session);
  332. return LLFailedLoginResponse.InternalError;
  333. }
  334. }
  335. 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)
  336. {
  337. m_log.DebugFormat("[LLOGIN SERVICE]: FindDestination for start location {0}", startLocation);
  338. gatekeeper = null;
  339. where = "home";
  340. position = new Vector3(128, 128, 0);
  341. lookAt = new Vector3(0, 1, 0);
  342. if (m_GridService == null)
  343. return null;
  344. if (startLocation.Equals("home"))
  345. {
  346. // logging into home region
  347. if (pinfo == null)
  348. return null;
  349. GridRegion region = null;
  350. bool tryDefaults = false;
  351. if (home == null)
  352. {
  353. m_log.WarnFormat(
  354. "[LLOGIN SERVICE]: User {0} {1} tried to login to a 'home' start location but they have none set",
  355. account.FirstName, account.LastName);
  356. tryDefaults = true;
  357. }
  358. else
  359. {
  360. region = home;
  361. position = pinfo.HomePosition;
  362. lookAt = pinfo.HomeLookAt;
  363. }
  364. if (tryDefaults)
  365. {
  366. List<GridRegion> defaults = m_GridService.GetDefaultRegions(scopeID);
  367. if (defaults != null && defaults.Count > 0)
  368. {
  369. region = defaults[0];
  370. where = "safe";
  371. }
  372. else
  373. {
  374. 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",
  375. account.FirstName, account.LastName);
  376. defaults = m_GridService.GetRegionsByName(scopeID, "", 1);
  377. if (defaults != null && defaults.Count > 0)
  378. {
  379. region = defaults[0];
  380. where = "safe";
  381. }
  382. }
  383. }
  384. return region;
  385. }
  386. else if (startLocation.Equals("last"))
  387. {
  388. // logging into last visited region
  389. where = "last";
  390. if (pinfo == null)
  391. return null;
  392. GridRegion region = null;
  393. if (pinfo.LastRegionID.Equals(UUID.Zero) || (region = m_GridService.GetRegionByUUID(scopeID, pinfo.LastRegionID)) == null)
  394. {
  395. List<GridRegion> defaults = m_GridService.GetDefaultRegions(scopeID);
  396. if (defaults != null && defaults.Count > 0)
  397. {
  398. region = defaults[0];
  399. where = "safe";
  400. }
  401. else
  402. {
  403. m_log.Info("[LLOGIN SERVICE]: Last Region Not Found Attempting to find random region");
  404. defaults = m_GridService.GetRegionsByName(scopeID, "", 1);
  405. if (defaults != null && defaults.Count > 0)
  406. {
  407. region = defaults[0];
  408. where = "safe";
  409. }
  410. }
  411. }
  412. else
  413. {
  414. position = pinfo.LastPosition;
  415. lookAt = pinfo.LastLookAt;
  416. }
  417. return region;
  418. }
  419. else
  420. {
  421. // free uri form
  422. // e.g. New Moon&135&46 New [email protected]:8002&153&34
  423. where = "url";
  424. Regex reURI = new Regex(@"^uri:(?<region>[^&]+)&(?<x>\d+)&(?<y>\d+)&(?<z>\d+)$");
  425. Match uriMatch = reURI.Match(startLocation);
  426. if (uriMatch == null)
  427. {
  428. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, but can't process it", startLocation);
  429. return null;
  430. }
  431. else
  432. {
  433. position = new Vector3(float.Parse(uriMatch.Groups["x"].Value, Culture.NumberFormatInfo),
  434. float.Parse(uriMatch.Groups["y"].Value, Culture.NumberFormatInfo),
  435. float.Parse(uriMatch.Groups["z"].Value, Culture.NumberFormatInfo));
  436. string regionName = uriMatch.Groups["region"].ToString();
  437. if (regionName != null)
  438. {
  439. if (!regionName.Contains("@"))
  440. {
  441. List<GridRegion> regions = m_GridService.GetRegionsByName(scopeID, regionName, 1);
  442. if ((regions == null) || (regions != null && regions.Count == 0))
  443. {
  444. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, can't locate region {1}. Trying defaults.", startLocation, regionName);
  445. regions = m_GridService.GetDefaultRegions(scopeID);
  446. if (regions != null && regions.Count > 0)
  447. {
  448. where = "safe";
  449. return regions[0];
  450. }
  451. else
  452. {
  453. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, Grid does not provide default regions.", startLocation);
  454. return null;
  455. }
  456. }
  457. return regions[0];
  458. }
  459. else
  460. {
  461. if (m_UserAgentService == null)
  462. {
  463. 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");
  464. return null;
  465. }
  466. string[] parts = regionName.Split(new char[] { '@' });
  467. if (parts.Length < 2)
  468. {
  469. m_log.InfoFormat("[LLLOGIN SERVICE]: Got Custom Login URI {0}, can't locate region {1}", startLocation, regionName);
  470. return null;
  471. }
  472. // Valid specification of a remote grid
  473. regionName = parts[0];
  474. string domainLocator = parts[1];
  475. parts = domainLocator.Split(new char[] {':'});
  476. string domainName = parts[0];
  477. uint port = 0;
  478. if (parts.Length > 1)
  479. UInt32.TryParse(parts[1], out port);
  480. GridRegion region = FindForeignRegion(domainName, port, regionName, out gatekeeper);
  481. return region;
  482. }
  483. }
  484. else
  485. {
  486. List<GridRegion> defaults = m_GridService.GetDefaultRegions(scopeID);
  487. if (defaults != null && defaults.Count > 0)
  488. {
  489. where = "safe";
  490. return defaults[0];
  491. }
  492. else
  493. return null;
  494. }
  495. }
  496. //response.LookAt = "[r0,r1,r0]";
  497. //// can be: last, home, safe, url
  498. //response.StartLocation = "url";
  499. }
  500. }
  501. private GridRegion FindForeignRegion(string domainName, uint port, string regionName, out GridRegion gatekeeper)
  502. {
  503. gatekeeper = new GridRegion();
  504. gatekeeper.ExternalHostName = domainName;
  505. gatekeeper.HttpPort = port;
  506. gatekeeper.RegionName = regionName;
  507. gatekeeper.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  508. UUID regionID;
  509. ulong handle;
  510. string imageURL = string.Empty, reason = string.Empty;
  511. if (m_GatekeeperConnector.LinkRegion(gatekeeper, out regionID, out handle, out domainName, out imageURL, out reason))
  512. {
  513. GridRegion destination = m_GatekeeperConnector.GetHyperlinkRegion(gatekeeper, regionID);
  514. return destination;
  515. }
  516. return null;
  517. }
  518. private string hostName = string.Empty;
  519. private int port = 0;
  520. private void SetHostAndPort(string url)
  521. {
  522. try
  523. {
  524. Uri uri = new Uri(url);
  525. hostName = uri.Host;
  526. port = uri.Port;
  527. }
  528. catch
  529. {
  530. m_log.WarnFormat("[LLLogin SERVICE]: Unable to parse GatekeeperURL {0}", url);
  531. }
  532. }
  533. protected AgentCircuitData LaunchAgentAtGrid(GridRegion gatekeeper, GridRegion destination, UserAccount account, AvatarData avatar,
  534. UUID session, UUID secureSession, Vector3 position, string currentWhere, string viewer, IPEndPoint clientIP, out string where, out string reason, out GridRegion dest)
  535. {
  536. where = currentWhere;
  537. ISimulationService simConnector = null;
  538. reason = string.Empty;
  539. uint circuitCode = 0;
  540. AgentCircuitData aCircuit = null;
  541. if (m_UserAgentService == null)
  542. {
  543. // HG standalones have both a localSimulatonDll and a remoteSimulationDll
  544. // non-HG standalones have just a localSimulationDll
  545. // independent login servers have just a remoteSimulationDll
  546. if (m_LocalSimulationService != null)
  547. simConnector = m_LocalSimulationService;
  548. else if (m_RemoteSimulationService != null)
  549. simConnector = m_RemoteSimulationService;
  550. }
  551. else // User Agent Service is on
  552. {
  553. if (gatekeeper == null) // login to local grid
  554. {
  555. if (hostName == string.Empty)
  556. SetHostAndPort(m_GatekeeperURL);
  557. gatekeeper = new GridRegion(destination);
  558. gatekeeper.ExternalHostName = hostName;
  559. gatekeeper.HttpPort = (uint)port;
  560. }
  561. else // login to foreign grid
  562. {
  563. }
  564. }
  565. bool success = false;
  566. if (m_UserAgentService == null && simConnector != null)
  567. {
  568. circuitCode = (uint)Util.RandomClass.Next(); ;
  569. aCircuit = MakeAgent(destination, account, avatar, session, secureSession, circuitCode, position, viewer);
  570. success = LaunchAgentDirectly(simConnector, destination, aCircuit, out reason);
  571. if (!success && m_GridService != null)
  572. {
  573. // Try the fallback regions
  574. List<GridRegion> fallbacks = m_GridService.GetFallbackRegions(account.ScopeID, destination.RegionLocX, destination.RegionLocY);
  575. if (fallbacks != null)
  576. {
  577. foreach (GridRegion r in fallbacks)
  578. {
  579. success = LaunchAgentDirectly(simConnector, r, aCircuit, out reason);
  580. if (success)
  581. {
  582. where = "safe";
  583. destination = r;
  584. break;
  585. }
  586. }
  587. }
  588. }
  589. }
  590. if (m_UserAgentService != null)
  591. {
  592. circuitCode = (uint)Util.RandomClass.Next(); ;
  593. aCircuit = MakeAgent(destination, account, avatar, session, secureSession, circuitCode, position, viewer);
  594. success = LaunchAgentIndirectly(gatekeeper, destination, aCircuit, clientIP, out reason);
  595. if (!success && m_GridService != null)
  596. {
  597. // Try the fallback regions
  598. List<GridRegion> fallbacks = m_GridService.GetFallbackRegions(account.ScopeID, destination.RegionLocX, destination.RegionLocY);
  599. if (fallbacks != null)
  600. {
  601. foreach (GridRegion r in fallbacks)
  602. {
  603. success = LaunchAgentIndirectly(gatekeeper, r, aCircuit, clientIP, out reason);
  604. if (success)
  605. {
  606. where = "safe";
  607. destination = r;
  608. break;
  609. }
  610. }
  611. }
  612. }
  613. }
  614. dest = destination;
  615. if (success)
  616. return aCircuit;
  617. else
  618. return null;
  619. }
  620. private AgentCircuitData MakeAgent(GridRegion region, UserAccount account,
  621. AvatarData avatar, UUID session, UUID secureSession, uint circuit, Vector3 position, string viewer)
  622. {
  623. AgentCircuitData aCircuit = new AgentCircuitData();
  624. aCircuit.AgentID = account.PrincipalID;
  625. if (avatar != null)
  626. aCircuit.Appearance = avatar.ToAvatarAppearance(account.PrincipalID);
  627. else
  628. aCircuit.Appearance = new AvatarAppearance(account.PrincipalID);
  629. //aCircuit.BaseFolder = irrelevant
  630. aCircuit.CapsPath = CapsUtil.GetRandomCapsObjectPath();
  631. aCircuit.child = false; // the first login agent is root
  632. aCircuit.ChildrenCapSeeds = new Dictionary<ulong, string>();
  633. aCircuit.circuitcode = circuit;
  634. aCircuit.firstname = account.FirstName;
  635. //aCircuit.InventoryFolder = irrelevant
  636. aCircuit.lastname = account.LastName;
  637. aCircuit.SecureSessionID = secureSession;
  638. aCircuit.SessionID = session;
  639. aCircuit.startpos = position;
  640. aCircuit.Viewer = viewer;
  641. SetServiceURLs(aCircuit, account);
  642. return aCircuit;
  643. //m_UserAgentService.LoginAgentToGrid(aCircuit, GatekeeperServiceConnector, region, out reason);
  644. //if (simConnector.CreateAgent(region, aCircuit, 0, out reason))
  645. // return aCircuit;
  646. //return null;
  647. }
  648. private void SetServiceURLs(AgentCircuitData aCircuit, UserAccount account)
  649. {
  650. aCircuit.ServiceURLs = new Dictionary<string, object>();
  651. if (account.ServiceURLs == null)
  652. return;
  653. foreach (KeyValuePair<string, object> kvp in account.ServiceURLs)
  654. {
  655. if (kvp.Value == null || (kvp.Value != null && kvp.Value.ToString() == string.Empty))
  656. {
  657. aCircuit.ServiceURLs[kvp.Key] = m_LoginServerConfig.GetString(kvp.Key, string.Empty);
  658. }
  659. else
  660. {
  661. aCircuit.ServiceURLs[kvp.Key] = kvp.Value;
  662. }
  663. }
  664. }
  665. private bool LaunchAgentDirectly(ISimulationService simConnector, GridRegion region, AgentCircuitData aCircuit, out string reason)
  666. {
  667. return simConnector.CreateAgent(region, aCircuit, (int)Constants.TeleportFlags.ViaLogin, out reason);
  668. }
  669. private bool LaunchAgentIndirectly(GridRegion gatekeeper, GridRegion destination, AgentCircuitData aCircuit, IPEndPoint clientIP, out string reason)
  670. {
  671. m_log.Debug("[LLOGIN SERVICE] Launching agent at " + destination.RegionName);
  672. if (m_UserAgentService.LoginAgentToGrid(aCircuit, gatekeeper, destination, clientIP, out reason))
  673. return true;
  674. return false;
  675. }
  676. #region Console Commands
  677. private void RegisterCommands()
  678. {
  679. //MainConsole.Instance.Commands.AddCommand
  680. MainConsole.Instance.Commands.AddCommand("loginservice", false, "login level",
  681. "login level <level>",
  682. "Set the minimum user level to log in", HandleLoginCommand);
  683. MainConsole.Instance.Commands.AddCommand("loginservice", false, "login reset",
  684. "login reset",
  685. "Reset the login level to allow all users",
  686. HandleLoginCommand);
  687. MainConsole.Instance.Commands.AddCommand("loginservice", false, "login text",
  688. "login text <text>",
  689. "Set the text users will see on login", HandleLoginCommand);
  690. }
  691. private void HandleLoginCommand(string module, string[] cmd)
  692. {
  693. string subcommand = cmd[1];
  694. switch (subcommand)
  695. {
  696. case "level":
  697. // Set the minimum level to allow login
  698. // Useful to allow grid update without worrying about users.
  699. // or fixing critical issues
  700. //
  701. if (cmd.Length > 2)
  702. Int32.TryParse(cmd[2], out m_MinLoginLevel);
  703. break;
  704. case "reset":
  705. m_MinLoginLevel = 0;
  706. break;
  707. case "text":
  708. if (cmd.Length > 2)
  709. m_WelcomeMessage = cmd[2];
  710. break;
  711. }
  712. }
  713. }
  714. #endregion
  715. }