UserManagementModule.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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.Collections.Concurrent;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Threading;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Services.Connectors.Hypergrid;
  39. using OpenMetaverse;
  40. using log4net;
  41. using Nini.Config;
  42. using Mono.Addins;
  43. namespace OpenSim.Region.CoreModules.Framework.UserManagement
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UserManagementModule")]
  46. public class UserManagementModule : ISharedRegionModule, IUserManagement, IPeople
  47. {
  48. private const int BADURLEXPIRE = 2 * 60;
  49. private const int NOEXPIRE = int.MinValue;
  50. private const int LOCALEXPIRE = 3600000;
  51. private const int HGEXPIRE = 3600000;
  52. private const int BADEXPIRE = 3600000;
  53. private const int BADHGEXPIRE =600000;
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. protected bool m_Enabled;
  56. protected List<Scene> m_Scenes = new List<Scene>();
  57. protected IServiceThrottleModule m_ServiceThrottle;
  58. protected IUserAccountService m_userAccountService = null;
  59. protected IGridUserService m_gridUserService = null;
  60. protected GridInfo m_thisGridInfo;
  61. // The cache
  62. protected ExpiringCacheOS<UUID, UserData> m_userCacheByID = new ExpiringCacheOS<UUID, UserData>(120000);
  63. protected bool m_DisplayChangingHomeURI = false;
  64. UUID m_scopeID = UUID.Zero;
  65. ~UserManagementModule()
  66. {
  67. Dispose(false);
  68. }
  69. private bool disposed = false;
  70. public void Dispose()
  71. {
  72. Dispose(true);
  73. GC.SuppressFinalize(this);
  74. }
  75. private void Dispose(bool disposing)
  76. {
  77. if (!disposed)
  78. {
  79. disposed = true;
  80. m_userCacheByID.Dispose();
  81. m_userCacheByID = null;
  82. }
  83. }
  84. #region ISharedRegionModule
  85. public virtual void Initialise(IConfigSource config)
  86. {
  87. string umanmod = config.Configs["Modules"].GetString("UserManagementModule", Name);
  88. if (umanmod == Name)
  89. {
  90. m_Enabled = true;
  91. Init(config);
  92. m_log.DebugFormat("[USER MANAGEMENT MODULE]: {0} is enabled", Name);
  93. }
  94. }
  95. public virtual bool IsSharedModule
  96. {
  97. get { return true; }
  98. }
  99. public virtual string Name
  100. {
  101. get { return "BasicUserManagementModule"; }
  102. }
  103. public virtual Type ReplaceableInterface
  104. {
  105. get { return null; }
  106. }
  107. public virtual void AddRegion(Scene scene)
  108. {
  109. if (m_Enabled)
  110. {
  111. lock (m_Scenes)
  112. {
  113. m_Scenes.Add(scene);
  114. }
  115. if(m_thisGridInfo == null)
  116. m_thisGridInfo = scene.SceneGridInfo;
  117. scene.RegisterModuleInterface<IUserManagement>(this);
  118. scene.RegisterModuleInterface<IPeople>(this);
  119. scene.EventManager.OnNewClient += EventManager_OnNewClient;
  120. scene.EventManager.OnPrimsLoaded += EventManager_OnPrimsLoaded;
  121. }
  122. }
  123. public virtual void RemoveRegion(Scene scene)
  124. {
  125. if (m_Enabled)
  126. {
  127. scene.UnregisterModuleInterface<IUserManagement>(this);
  128. lock (m_Scenes)
  129. {
  130. m_Scenes.Remove(scene);
  131. }
  132. }
  133. }
  134. public virtual void RegionLoaded(Scene s)
  135. {
  136. if (!m_Enabled)
  137. return;
  138. if(m_ServiceThrottle == null)
  139. m_ServiceThrottle = s.RequestModuleInterface<IServiceThrottleModule>();
  140. if(m_userAccountService == null)
  141. m_userAccountService = s.UserAccountService;
  142. if(m_gridUserService == null)
  143. m_gridUserService = s.GridUserService;
  144. if (s.RegionInfo.ScopeID != UUID.Zero)
  145. m_scopeID = s.RegionInfo.ScopeID;
  146. }
  147. public virtual void PostInitialise()
  148. {
  149. }
  150. public virtual void Close()
  151. {
  152. m_Enabled = false;
  153. lock (m_Scenes)
  154. {
  155. m_Scenes.Clear();
  156. }
  157. m_thisGridInfo = null;
  158. Dispose(false);
  159. }
  160. #endregion ISharedRegionModule
  161. #region Event Handlers
  162. protected virtual void EventManager_OnPrimsLoaded(Scene s)
  163. {
  164. // let's sniff all the user names referenced by objects in the scene
  165. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Caching creators' data from {0} ({1} objects)...", s.RegionInfo.RegionName, s.GetEntities().Length);
  166. s.ForEachSOG(delegate(SceneObjectGroup sog) { CacheCreators(sog); });
  167. }
  168. protected virtual void EventManager_OnNewClient(IClientAPI client)
  169. {
  170. client.OnConnectionClosed += HandleConnectionClosed;
  171. client.OnNameFromUUIDRequest += HandleUUIDNameRequest;
  172. client.OnAvatarPickerRequest += HandleAvatarPickerRequest;
  173. }
  174. protected virtual void HandleConnectionClosed(IClientAPI client)
  175. {
  176. client.OnNameFromUUIDRequest -= HandleUUIDNameRequest;
  177. client.OnAvatarPickerRequest -= HandleAvatarPickerRequest;
  178. client.OnConnectionClosed -= HandleConnectionClosed;
  179. }
  180. protected virtual void HandleUUIDNameRequest(UUID uuid, IClientAPI client)
  181. {
  182. // m_log.DebugFormat(
  183. // "[USER MANAGEMENT MODULE]: Handling request for name binding of UUID {0} from {1}",
  184. // uuid, remote_client.Name);
  185. if(!m_Enabled || m_Scenes.Count <= 0)
  186. return;
  187. if (m_userCacheByID.TryGetValue(uuid, out UserData user))
  188. {
  189. if (user.HasGridUserTried)
  190. {
  191. client.SendNameReply(uuid, user.FirstName, user.LastName);
  192. return;
  193. }
  194. }
  195. if(m_ServiceThrottle == null)
  196. return;
  197. IClientAPI deferedcli = client;
  198. // Not found in cache, queue continuation
  199. m_ServiceThrottle.Enqueue("uuidname", uuid.ToString(), delegate
  200. {
  201. if(deferedcli.IsActive)
  202. {
  203. if (GetUser(uuid, deferedcli.ScopeId, out UserData defuser))
  204. {
  205. if(deferedcli.IsActive)
  206. deferedcli.SendNameReply(uuid, defuser.FirstName, defuser.LastName);
  207. }
  208. }
  209. deferedcli = null;
  210. });
  211. }
  212. public virtual void HandleAvatarPickerRequest(IClientAPI client, UUID avatarID, UUID RequestID, string query)
  213. {
  214. //EventManager.TriggerAvatarPickerRequest();
  215. m_log.DebugFormat("[USER MANAGEMENT MODULE]: HandleAvatarPickerRequest for {0}", query);
  216. List<UserData> users = GetUserData(query, 500, 1);
  217. client.SendAvatarPickerReply(RequestID, users);
  218. }
  219. public bool CheckUrl(string url, out bool islocal, out OSHHTPHost host)
  220. {
  221. host = new OSHHTPHost(url);
  222. int type = m_thisGridInfo.IsLocalGrid(host);
  223. if(type < 0 )
  224. {
  225. islocal = false;
  226. return false;
  227. }
  228. islocal = type == 1;
  229. return true;
  230. }
  231. protected virtual void AddAdditionalUsers(string query, List<UserData> users, HashSet<UUID> found)
  232. {
  233. }
  234. #endregion Event Handlers
  235. #region IPeople
  236. public virtual UserData GetUserData(UUID id)
  237. {
  238. if(GetUser(id, out UserData u))
  239. return u;
  240. return null;
  241. }
  242. public virtual List<UserData> GetUserData(string query, int page_size, int page_number)
  243. {
  244. if(m_Scenes.Count <= 0 || m_userAccountService == null)
  245. return new List<UserData>();;
  246. var users = new List<UserData>();
  247. var found = new HashSet<UUID>();
  248. // search the user accounts service
  249. if (m_userAccountService != null)
  250. {
  251. List<UserAccount> accs = m_userAccountService.GetUserAccounts(m_scopeID, query);
  252. if (accs != null)
  253. {
  254. for(int i = 0; i < accs.Count; ++i)
  255. {
  256. UserAccount acc = accs[i];
  257. UUID id = acc.PrincipalID;
  258. UserData ud = new UserData();
  259. ud.FirstName = acc.FirstName;
  260. ud.LastName = acc.LastName;
  261. ud.Id = id;
  262. ud.HasGridUserTried = true;
  263. ud.IsUnknownUser = false;
  264. ud.IsLocal = acc.LocalToGrid;
  265. users.Add(ud);
  266. found.Add(id);
  267. }
  268. }
  269. }
  270. // search the local cache
  271. string q = query.ToLower();
  272. foreach (UserData data in m_userCacheByID.Values)
  273. {
  274. if (found.Contains(data.Id))
  275. continue;
  276. if (data.Id == UUID.Zero || data.IsUnknownUser)
  277. continue;
  278. if (data.FirstName.ToLower().StartsWith(q) || data.LastName.ToLower().StartsWith(q))
  279. users.Add(data);
  280. }
  281. AddAdditionalUsers(query, users, found);
  282. return users;
  283. }
  284. #endregion IPeople
  285. protected virtual void CacheCreators(SceneObjectGroup sog)
  286. {
  287. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: processing {0} {1}; {2}", sog.RootPart.Name, sog.RootPart.CreatorData, sog.RootPart.CreatorIdentification);
  288. AddCreatorUser(sog.RootPart.CreatorID, sog.RootPart.CreatorData);
  289. foreach (SceneObjectPart sop in sog.Parts)
  290. {
  291. AddCreatorUser(sop.CreatorID, sop.CreatorData);
  292. foreach (TaskInventoryItem item in sop.TaskInventory.Values)
  293. AddCreatorUser(item.CreatorID, item.CreatorData);
  294. }
  295. }
  296. /// <summary>
  297. ///
  298. /// </summary>
  299. /// <param name="uuid"></param>
  300. /// <param name="names">Caller please provide a properly instantiated array for names, string[2]</param>
  301. /// <returns></returns>
  302. protected virtual bool TryGetUserNames(UUID uuid, string[] names)
  303. {
  304. if (names == null)
  305. names = new string[2];
  306. if(GetUser(uuid, out UserData u))
  307. {
  308. names[0] = u.FirstName;
  309. names[1] = u.LastName;
  310. return true;
  311. }
  312. names[0] = "UnknownUMM3";
  313. names[1] = uuid.ToString();
  314. return false;
  315. }
  316. #region IUserManagement
  317. public virtual UUID GetUserIdByName(string name)
  318. {
  319. string[] parts = name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  320. if (parts.Length < 2)
  321. throw new Exception("Name must have 2 components");
  322. return GetUserIdByName(parts[0], parts[1]);
  323. }
  324. public virtual UUID GetUserIdByName(string firstName, string lastName)
  325. {
  326. if(m_Scenes.Count <= 0)
  327. return UUID.Zero;
  328. // TODO: Optimize for reverse lookup if this gets used by non-console commands.
  329. foreach (UserData user in m_userCacheByID.Values)
  330. {
  331. if (user.FirstName.Equals(firstName, StringComparison.InvariantCultureIgnoreCase) &&
  332. user.LastName.Equals(lastName, StringComparison.InvariantCultureIgnoreCase))
  333. return user.Id;
  334. }
  335. if(m_userAccountService != null)
  336. {
  337. UserAccount account = m_userAccountService.GetUserAccount(UUID.Zero, firstName, lastName);
  338. if (account != null)
  339. {
  340. AddUser(account);
  341. return account.PrincipalID;
  342. }
  343. }
  344. return UUID.Zero;
  345. }
  346. public virtual string GetUserName(UUID uuid)
  347. {
  348. if(GetUser(uuid, out UserData user))
  349. return user.FirstName + " " + user.LastName;
  350. return "UnknownUMM2 " + uuid.ToString();
  351. }
  352. public virtual Dictionary<UUID,string> GetUsersNames(string[] ids, UUID scopeID)
  353. {
  354. var ret = new Dictionary<UUID,string>();
  355. if(m_Scenes.Count <= 0)
  356. return ret;
  357. List<string> missing = new List<string>(ids.Length);
  358. var untried = new Dictionary<UUID, UserData>();
  359. foreach (string id in ids)
  360. {
  361. if(!UUID.TryParse(id, out UUID uuid) || uuid == UUID.Zero)
  362. continue;
  363. if (m_userCacheByID.TryGetValue(uuid, out UserData userdata))
  364. {
  365. string name = userdata.FirstName + " " + userdata.LastName;
  366. if (userdata.HasGridUserTried)
  367. {
  368. ret[uuid] = name;
  369. continue;
  370. }
  371. untried[uuid] = userdata;
  372. }
  373. missing.Add(id);
  374. }
  375. if(missing.Count == 0)
  376. return ret;
  377. ids = null;
  378. List<UserAccount> accounts = m_userAccountService.GetUserAccounts(scopeID, missing);
  379. if (accounts.Count != 0)
  380. {
  381. foreach (UserAccount uac in accounts)
  382. {
  383. if (uac != null)
  384. {
  385. string name = uac.FirstName + " " + uac.LastName;
  386. UUID id = uac.PrincipalID;
  387. var userdata = new UserData();
  388. userdata.Id = id;
  389. userdata.FirstName = uac.FirstName;
  390. userdata.LastName = uac.LastName;
  391. userdata.HomeURL = string.Empty;
  392. userdata.IsUnknownUser = false;
  393. userdata.IsLocal = true;
  394. userdata.HasGridUserTried = true;
  395. m_userCacheByID.Add(id, userdata, 1800000);
  396. ret[id] = name;
  397. missing.Remove(id.ToString()); // slowww
  398. untried.Remove(id);
  399. }
  400. }
  401. }
  402. if (missing.Count == 0 || m_gridUserService == null)
  403. return ret;
  404. GridUserInfo[] pinfos = m_gridUserService.GetGridUserInfo(missing.ToArray());
  405. if (pinfos.Length > 0)
  406. {
  407. foreach (GridUserInfo uInfo in pinfos)
  408. {
  409. if (uInfo != null && uInfo.UserID.Length >= 36)
  410. {
  411. string url, first, last, tmp;
  412. UUID u;
  413. if (uInfo.UserID.Length >= 36 && Util.ParseUniversalUserIdentifier(uInfo.UserID, out u, out url, out first, out last, out tmp))
  414. {
  415. bool isvalid = CheckUrl(url, out bool islocal, out OSHHTPHost host);
  416. var userdata = new UserData();
  417. userdata.Id = u;
  418. if (isvalid)
  419. {
  420. if (islocal)
  421. {
  422. userdata.FirstName = first;
  423. userdata.LastName = last;
  424. userdata.HomeURL = string.Empty;
  425. userdata.IsLocal = true;
  426. }
  427. else
  428. {
  429. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  430. userdata.HomeURL = host.URI;
  431. userdata.LastName = "@" + host.HostAndPort;
  432. userdata.IsLocal = false;
  433. }
  434. userdata.IsUnknownUser = false;
  435. userdata.HasGridUserTried = true;
  436. m_userCacheByID.Add(u, userdata, 1800000);
  437. string name = userdata.FirstName + " " + userdata.LastName;
  438. ret[u] = name;
  439. missing.Remove(u.ToString());
  440. untried.Remove(u);
  441. }
  442. }
  443. else
  444. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  445. }
  446. }
  447. }
  448. // add the untried in cache
  449. if (untried.Count > 0)
  450. {
  451. foreach (UserData ud in untried.Values)
  452. {
  453. UUID id = ud.Id;
  454. ud.HasGridUserTried = true;
  455. m_userCacheByID.Add(id, ud, 1800000);
  456. ret[id] = ud.FirstName + " " + ud.LastName;
  457. missing.Remove(id.ToString());
  458. }
  459. }
  460. // add the UMMthings ( not sure we should)
  461. if (missing.Count > 0)
  462. {
  463. foreach (string id in missing)
  464. {
  465. if (UUID.TryParse(id, out UUID uuid))
  466. ret[uuid] = "Unknown UserUMMAU43";
  467. }
  468. }
  469. return ret;
  470. }
  471. public virtual Dictionary<UUID, string> GetKnownUserNames(string[] ids, UUID scopeID)
  472. {
  473. var ret = new Dictionary<UUID, string>();
  474. if (m_Scenes.Count <= 0)
  475. return ret;
  476. List<string> missing = new List<string>(ids.Length);
  477. var untried = new Dictionary<UUID, UserData>();
  478. foreach (string id in ids)
  479. {
  480. if (!UUID.TryParse(id, out UUID uuid) || uuid == UUID.Zero)
  481. continue;
  482. if (m_userCacheByID.TryGetValue(uuid, out UserData userdata))
  483. {
  484. if (userdata.HasGridUserTried)
  485. {
  486. if(!userdata.IsUnknownUser)
  487. ret[uuid] = userdata.FirstName + " " + userdata.LastName;
  488. continue;
  489. }
  490. else
  491. untried[uuid] = userdata;
  492. }
  493. missing.Add(id);
  494. }
  495. if (missing.Count == 0)
  496. return ret;
  497. ids = null;
  498. List<UserAccount> accounts = m_userAccountService.GetUserAccounts(scopeID, missing);
  499. if (accounts.Count != 0)
  500. {
  501. foreach (UserAccount uac in accounts)
  502. {
  503. if (uac != null)
  504. {
  505. UUID id = uac.PrincipalID;
  506. var userdata = new UserData();
  507. userdata.Id = id;
  508. userdata.FirstName = uac.FirstName;
  509. userdata.LastName = uac.LastName;
  510. userdata.HomeURL = string.Empty;
  511. userdata.IsUnknownUser = false;
  512. userdata.IsLocal = true;
  513. userdata.HasGridUserTried = true;
  514. m_userCacheByID.Add(id, userdata, 1800000);
  515. ret[id] = uac.FirstName + " " + uac.LastName;
  516. missing.Remove(id.ToString()); // slowww
  517. untried.Remove(id);
  518. }
  519. }
  520. }
  521. if (missing.Count == 0 || m_gridUserService == null)
  522. return ret;
  523. GridUserInfo[] pinfos = m_gridUserService.GetGridUserInfo(missing.ToArray());
  524. missing = null;
  525. if (pinfos.Length > 0)
  526. {
  527. foreach (GridUserInfo uInfo in pinfos)
  528. {
  529. if (uInfo != null && uInfo.UserID.Length >= 36)
  530. {
  531. string url, first, last, tmp;
  532. UUID uuid;
  533. if (Util.ParseUniversalUserIdentifier(uInfo.UserID, out uuid, out url, out first, out last, out tmp))
  534. {
  535. bool isvalid = CheckUrl(url, out bool islocal, out OSHHTPHost host);
  536. var userdata = new UserData();
  537. userdata.Id = uuid;
  538. if (isvalid)
  539. {
  540. if (islocal)
  541. {
  542. userdata.FirstName = first;
  543. userdata.LastName = last;
  544. userdata.HomeURL = string.Empty;
  545. userdata.IsLocal = true;
  546. }
  547. else
  548. {
  549. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  550. userdata.HomeURL = host.URI;
  551. userdata.LastName = "@" + host.HostAndPort;
  552. userdata.IsLocal = false;
  553. }
  554. userdata.IsUnknownUser = false;
  555. userdata.HasGridUserTried = true;
  556. m_userCacheByID.Add(uuid, userdata, 1800000);
  557. string name = userdata.FirstName + " " + userdata.LastName;
  558. untried.Remove(uuid);
  559. ret[uuid] = name;
  560. }
  561. }
  562. else
  563. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  564. }
  565. }
  566. }
  567. foreach(UserData ud in untried.Values)
  568. {
  569. ud.HasGridUserTried = true;
  570. m_userCacheByID.Add(ud.Id, ud, 1800000);
  571. if(!ud.IsUnknownUser)
  572. ret[ud.Id] = ud.FirstName + " " + ud.LastName;
  573. }
  574. return ret;
  575. }
  576. public virtual string GetUserHomeURL(UUID userID)
  577. {
  578. if (GetUser(userID, out UserData user) && user != null)
  579. {
  580. if (user.LastWebFail > 0 && Util.GetTimeStamp() - user.LastWebFail > BADURLEXPIRE)
  581. user.LastWebFail = -1;
  582. return user.HomeURL;
  583. }
  584. return string.Empty;
  585. }
  586. public virtual string GetUserHomeURL(UUID userID, out bool recentFail)
  587. {
  588. recentFail = false;
  589. if (GetUser(userID, out UserData user))
  590. {
  591. if (user.LastWebFail > 0)
  592. {
  593. if (Util.GetTimeStamp() - user.LastWebFail > BADURLEXPIRE)
  594. user.LastWebFail = -1;
  595. else
  596. recentFail = true;
  597. }
  598. return user.HomeURL;
  599. }
  600. return string.Empty;
  601. }
  602. public virtual string GetUserServerURL(UUID userID, string serverType)
  603. {
  604. UserData userdata;
  605. if(!GetUser(userID, out userdata))
  606. return string.Empty;
  607. if(userdata.IsLocal)
  608. return string.Empty;
  609. if(userdata.LastWebFail > 0)
  610. {
  611. if(Util.GetTimeStamp() - userdata.LastWebFail > BADURLEXPIRE)
  612. return string.Empty;
  613. userdata.LastWebFail = -1;
  614. }
  615. if (userdata.ServerURLs != null)
  616. {
  617. if(userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  618. {
  619. string turl = ourl as string;
  620. OSHHTPHost otmp = new OSHHTPHost(turl);
  621. if (otmp.IsValidHost)
  622. return otmp.URI;
  623. }
  624. return string.Empty;
  625. }
  626. if (!string.IsNullOrEmpty(userdata.HomeURL))
  627. {
  628. string homeuri = userdata.HomeURL.ToLower();
  629. if (!WebUtil.GlobalExpiringBadURLs.ContainsKey(homeuri))
  630. {
  631. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Requested url type {0} for {1}", serverType, userID);
  632. UserAgentServiceConnector uConn = new UserAgentServiceConnector(homeuri);
  633. try
  634. {
  635. userdata.ServerURLs = uConn.GetServerURLs(userID);
  636. }
  637. catch(System.Net.WebException e)
  638. {
  639. m_log.DebugFormat("[USER MANAGEMENT MODULE]: GetServerURLs call failed {0}", e.Message);
  640. WebUtil.GlobalExpiringBadURLs.Add(homeuri, BADURLEXPIRE * 1000);
  641. userdata.ServerURLs = new Dictionary<string, object>();
  642. }
  643. catch (Exception e)
  644. {
  645. m_log.Debug("[USER MANAGEMENT MODULE]: GetServerURLs call failed ", e);
  646. userdata.ServerURLs = new Dictionary<string, object>();
  647. }
  648. if (userdata.ServerURLs != null && userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  649. {
  650. string turl = ourl as string;
  651. OSHHTPHost otmp = new OSHHTPHost(turl);
  652. if (otmp.IsValidHost)
  653. return otmp.URI;
  654. }
  655. }
  656. }
  657. return string.Empty;
  658. }
  659. public void UserWebFailed(UUID id)
  660. {
  661. if(m_userCacheByID.TryGetValue(id, out UserData u))
  662. u.LastWebFail = Util.GetTimeStamp();
  663. }
  664. public virtual string GetUserServerURL(UUID userID, string serverType, out bool recentFail)
  665. {
  666. recentFail = false;
  667. if (!GetUser(userID, out UserData userdata))
  668. return string.Empty;
  669. if (userdata.IsLocal)
  670. return string.Empty;
  671. if (userdata.LastWebFail > 0)
  672. {
  673. if (Util.GetTimeStamp() - userdata.LastWebFail > BADURLEXPIRE)
  674. recentFail = true;
  675. else
  676. userdata.LastWebFail = -1;
  677. }
  678. if (userdata.ServerURLs != null)
  679. {
  680. if (userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  681. {
  682. string turl = ourl as string;
  683. OSHHTPHost otmp = new OSHHTPHost(turl);
  684. if (otmp.IsValidHost)
  685. return otmp.URI;
  686. }
  687. return string.Empty;
  688. }
  689. if (!recentFail && !string.IsNullOrEmpty(userdata.HomeURL))
  690. {
  691. string homeurl = userdata.HomeURL.ToLower();
  692. if(!WebUtil.GlobalExpiringBadURLs.ContainsKey(homeurl))
  693. {
  694. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Requested url type {0} for {1}", serverType, userID);
  695. UserAgentServiceConnector uConn = new UserAgentServiceConnector(homeurl);
  696. try
  697. {
  698. userdata.ServerURLs = uConn.GetServerURLs(userID);
  699. }
  700. catch (System.Net.WebException e)
  701. {
  702. m_log.DebugFormat("[USER MANAGEMENT MODULE]: GetServerURLs call failed {0}", e.Message);
  703. userdata.ServerURLs = new Dictionary<string, object>();
  704. userdata.LastWebFail = Util.GetTimeStamp();
  705. WebUtil.GlobalExpiringBadURLs.Add(homeurl, BADURLEXPIRE * 1000);
  706. recentFail = true;
  707. }
  708. catch (Exception e)
  709. {
  710. m_log.Debug("[USER MANAGEMENT MODULE]: GetServerURLs call failed ", e);
  711. userdata.ServerURLs = new Dictionary<string, object>();
  712. userdata.LastWebFail = Util.GetTimeStamp();
  713. recentFail = true;
  714. }
  715. if (userdata.ServerURLs != null && userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  716. {
  717. string turl = ourl as string;
  718. OSHHTPHost otmp = new OSHHTPHost(turl);
  719. if (otmp.IsValidHost)
  720. return otmp.URI;
  721. }
  722. }
  723. }
  724. return string.Empty;
  725. }
  726. public virtual string GetUserUUI(UUID userID)
  727. {
  728. string uui;
  729. GetUserUUI(userID, out uui);
  730. return uui;
  731. }
  732. public virtual bool GetUserUUI(UUID userID, out string uui)
  733. {
  734. if (GetUser(userID, out UserData ud) && ud != null)
  735. {
  736. if (ud.LastName.StartsWith("@"))
  737. {
  738. string[] parts = ud.FirstName.Split('.');
  739. if (parts.Length >= 2)
  740. uui = userID.ToString() + ";" + ud.HomeURL + ";" + parts[0] + " " + parts[1];
  741. else
  742. uui = userID.ToString() + ";" + ud.HomeURL + ";" + ud.FirstName + " " + ud.LastName;
  743. }
  744. else
  745. uui = userID.ToString();
  746. return true;
  747. }
  748. uui = userID.ToString();
  749. return false;
  750. }
  751. #region Cache Management
  752. public virtual bool GetUser(UUID uuid, out UserData userdata)
  753. {
  754. return GetUser(uuid, m_scopeID, out userdata);
  755. }
  756. public virtual bool GetUser(UUID uuid, UUID scopeID, out UserData userdata)
  757. {
  758. if (m_Scenes.Count <= 0)
  759. {
  760. userdata = new UserData();
  761. return false;
  762. }
  763. if (m_userCacheByID.TryGetValue(uuid, out userdata))
  764. {
  765. if (userdata.HasGridUserTried)
  766. return true;
  767. }
  768. else
  769. {
  770. userdata = new UserData();
  771. userdata.Id = uuid;
  772. userdata.FirstName = "Unknown";
  773. userdata.LastName = uuid.ToString();
  774. userdata.HomeURL = string.Empty;
  775. userdata.IsUnknownUser = true;
  776. userdata.HasGridUserTried = false;
  777. }
  778. if (!userdata.HasGridUserTried)
  779. {
  780. /* rewrite here */
  781. UserAccount account = m_userAccountService.GetUserAccount(scopeID, uuid);
  782. if (account != null)
  783. {
  784. userdata.FirstName = account.FirstName;
  785. userdata.LastName = account.LastName;
  786. userdata.HomeURL = string.Empty;
  787. userdata.IsUnknownUser = false;
  788. userdata.IsLocal = true;
  789. userdata.HasGridUserTried = true;
  790. }
  791. }
  792. if (!userdata.HasGridUserTried)
  793. {
  794. GridUserInfo uInfo = null;
  795. if (null != m_gridUserService)
  796. {
  797. uInfo = m_gridUserService.GetGridUserInfo(uuid.ToString());
  798. }
  799. if (uInfo != null)
  800. {
  801. string url, first, last, tmp;
  802. UUID u;
  803. if (uInfo.UserID.Length >= 36 && Util.ParseUniversalUserIdentifier(uInfo.UserID, out u, out url, out first, out last, out tmp))
  804. {
  805. bool isvalid = CheckUrl(url, out bool islocal, out OSHHTPHost host);
  806. if (isvalid)
  807. {
  808. if(islocal)
  809. {
  810. userdata.FirstName = first;
  811. userdata.LastName = last;
  812. userdata.HomeURL = string.Empty;
  813. userdata.IsLocal = true;
  814. userdata.IsUnknownUser = false;
  815. }
  816. else
  817. {
  818. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  819. userdata.HomeURL = host.URI;
  820. userdata.LastName = "@" + host.HostAndPort;
  821. userdata.IsLocal = false;
  822. userdata.IsUnknownUser = false;
  823. }
  824. }
  825. }
  826. else
  827. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  828. }
  829. userdata.HasGridUserTried = true;
  830. }
  831. /* END: do not wrap this code in any lock here */
  832. m_userCacheByID.Add(uuid, userdata, 1800000);
  833. return !userdata.IsUnknownUser;
  834. }
  835. public void AddUser(UserAccount account)
  836. {
  837. UUID id = account.PrincipalID;
  838. bool local = account.LocalToGrid;
  839. UserData user = new UserData();
  840. user.Id = id;
  841. user.FirstName = account.FirstName;
  842. user.LastName = account.LastName;
  843. user.HomeURL = string.Empty;
  844. user.IsUnknownUser = false;
  845. user.HasGridUserTried = true;
  846. user.IsLocal = local;
  847. m_userCacheByID.Add(id, user, local ? LOCALEXPIRE : HGEXPIRE);
  848. }
  849. public void AddSystemUser(UUID uuid, string first, string last)
  850. {
  851. UserData user = new UserData()
  852. {
  853. Id = uuid,
  854. FirstName = first,
  855. LastName = last,
  856. IsLocal = true,
  857. HasGridUserTried = true,
  858. HomeURL = string.Empty,
  859. IsUnknownUser = false
  860. };
  861. m_userCacheByID.Add(uuid, user, NOEXPIRE);
  862. }
  863. public void AddNPCUser(UUID uuid, string first, string last)
  864. {
  865. UserData user = new UserData()
  866. {
  867. Id = uuid,
  868. FirstName = first,
  869. LastName = last,
  870. HasGridUserTried = true,
  871. IsLocal = true,
  872. HomeURL = string.Empty,
  873. IsUnknownUser = false
  874. };
  875. m_userCacheByID.Add(uuid, user, NOEXPIRE);
  876. }
  877. public virtual void AddUser(UUID uuid, string first, string last, string homeURL)
  878. {
  879. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, first {1}, last {2}, url {3}", uuid, first, last, homeURL);
  880. UserData oldUser;
  881. if (m_userCacheByID.TryGetValue(uuid, out oldUser))
  882. {
  883. if (!oldUser.IsUnknownUser)
  884. {
  885. if (homeURL != oldUser.HomeURL && m_DisplayChangingHomeURI)
  886. {
  887. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Different HomeURI for {0} {1} ({2}): {3} and {4}",
  888. first, last, uuid.ToString(), homeURL, oldUser.HomeURL);
  889. }
  890. /* no update needed */
  891. return;
  892. }
  893. }
  894. oldUser = new UserData();
  895. oldUser.Id = uuid;
  896. oldUser.HasGridUserTried = false;
  897. oldUser.IsUnknownUser = false;
  898. bool local;
  899. if (CheckUrl(homeURL, out local, out OSHHTPHost host))
  900. {
  901. if (local)
  902. {
  903. oldUser.FirstName = first;
  904. oldUser.LastName = last;
  905. oldUser.IsLocal = true;
  906. oldUser.HomeURL = string.Empty;
  907. oldUser.HasGridUserTried = true;
  908. m_userCacheByID.Add(uuid, oldUser, LOCALEXPIRE);
  909. }
  910. else
  911. {
  912. oldUser.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  913. oldUser.LastName = "@" + host.HostAndPort;
  914. oldUser.HomeURL = host.URI;
  915. oldUser.IsLocal = false;
  916. m_userCacheByID.Add(uuid, oldUser, HGEXPIRE);
  917. }
  918. }
  919. else
  920. {
  921. oldUser.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  922. oldUser.LastName = "UMMM0Unknown";
  923. oldUser.IsLocal = true;
  924. oldUser.HomeURL = string.Empty;
  925. oldUser.HasGridUserTried = true;
  926. oldUser.IsUnknownUser = true;
  927. m_userCacheByID.Add(uuid, oldUser, BADEXPIRE);
  928. }
  929. }
  930. public virtual void AddCreatorUser(UUID id, string creatorData)
  931. {
  932. // m_log.InfoFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, creatorData {1}", id, creatorData);
  933. if(string.IsNullOrEmpty(creatorData))
  934. return;
  935. if(m_userCacheByID.ContainsKey(id))
  936. return;
  937. string homeURL;
  938. string firstname = string.Empty;
  939. string lastname = string.Empty;
  940. //creatorData = <endpoint>;<name>
  941. string[] parts = creatorData.Split(';');
  942. if(parts.Length > 1)
  943. {
  944. string[] nameparts = parts[1].Split(' ');
  945. if(nameparts.Length < 2)
  946. return;
  947. firstname = nameparts[0];
  948. for(int xi = 1; xi < nameparts.Length; ++xi)
  949. lastname += nameparts[xi];
  950. if (string.IsNullOrWhiteSpace(firstname))
  951. return;
  952. if (string.IsNullOrWhiteSpace(lastname))
  953. return;
  954. }
  955. else
  956. return;
  957. homeURL = parts[0];
  958. var oldUser = new UserData();
  959. oldUser.Id = id;
  960. oldUser.HasGridUserTried = false;
  961. oldUser.IsUnknownUser = false;
  962. OSHTTPURI homeuri = new OSHTTPURI(homeURL);
  963. if (homeuri.IsValidHost)
  964. {
  965. if (m_thisGridInfo.IsLocalGrid(homeuri.URL) == 1) // local
  966. {
  967. oldUser.FirstName = firstname;
  968. oldUser.LastName = lastname;
  969. oldUser.IsLocal = true;
  970. oldUser.HomeURL = string.Empty;
  971. oldUser.HasGridUserTried = true;
  972. }
  973. else
  974. {
  975. oldUser.FirstName = firstname + "." + lastname.Replace(" ", ".");
  976. oldUser.LastName = "@" + homeuri.HostAndPort;
  977. oldUser.HomeURL = homeuri.URL;
  978. oldUser.IsLocal = false;
  979. }
  980. }
  981. else
  982. {
  983. oldUser.FirstName = firstname + "." + lastname.Replace(" ", ".");
  984. oldUser.LastName = "UMMM1Unknown";
  985. oldUser.IsLocal = true;
  986. oldUser.HomeURL = string.Empty;
  987. oldUser.HasGridUserTried = true;
  988. oldUser.IsUnknownUser = true;
  989. }
  990. m_userCacheByID.Add(id, oldUser, NOEXPIRE);
  991. }
  992. public bool RemoveUser(UUID uuid)
  993. {
  994. return m_userCacheByID.Remove(uuid);
  995. }
  996. #endregion
  997. public virtual bool IsLocalGridUser(UUID uuid)
  998. {
  999. if (m_Scenes.Count <= 0)
  1000. return true;
  1001. if (m_userCacheByID.TryGetValue(uuid, out UserData u))
  1002. {
  1003. if (u.HasGridUserTried)
  1004. return u.IsLocal;
  1005. }
  1006. if(m_userAccountService == null)
  1007. return true;
  1008. UserAccount account = m_userAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, uuid);
  1009. if (account == null)
  1010. return false;
  1011. if(u == null)
  1012. AddUser(account);
  1013. else
  1014. u.HasGridUserTried = true;
  1015. return true;
  1016. }
  1017. #endregion IUserManagement
  1018. protected virtual void Init(IConfigSource config)
  1019. {
  1020. AddSystemUser(UUID.Zero, "Unknown", "User");
  1021. AddSystemUser(Constants.m_MrOpenSimID, "Mr", "Opensim");
  1022. RegisterConsoleCmds();
  1023. IConfig userManagementConfig = config.Configs["UserManagement"];
  1024. if (userManagementConfig != null)
  1025. m_DisplayChangingHomeURI = userManagementConfig.GetBoolean("DisplayChangingHomeURI", false);
  1026. }
  1027. protected virtual void RegisterConsoleCmds()
  1028. {
  1029. MainConsole.Instance.Commands.AddCommand("Users", true,
  1030. "show name",
  1031. "show name <uuid>",
  1032. "Show the bindings between a single user UUID and a user name",
  1033. String.Empty,
  1034. HandleShowUser);
  1035. MainConsole.Instance.Commands.AddCommand("Users", true,
  1036. "show names",
  1037. "show names",
  1038. "Show the bindings between user UUIDs and user names",
  1039. String.Empty,
  1040. HandleShowUsers);
  1041. MainConsole.Instance.Commands.AddCommand("Users", true,
  1042. "reset user cache",
  1043. "reset user cache",
  1044. "reset user cache to allow changed settings to be applied",
  1045. String.Empty,
  1046. HandleResetUserCache);
  1047. }
  1048. protected virtual void HandleResetUserCache(string module, string[] cmd)
  1049. {
  1050. m_userCacheByID.Clear();
  1051. }
  1052. protected virtual void HandleShowUser(string module, string[] cmd)
  1053. {
  1054. if (cmd.Length < 3)
  1055. {
  1056. MainConsole.Instance.Output("Usage: show name <uuid>");
  1057. return;
  1058. }
  1059. UUID userId;
  1060. if (!ConsoleUtil.TryParseConsoleUuid(MainConsole.Instance, cmd[2], out userId))
  1061. return;
  1062. UserData ud;
  1063. if(!GetUser(userId, out ud))
  1064. {
  1065. MainConsole.Instance.Output("No name known for user with id {0}", userId);
  1066. return;
  1067. }
  1068. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  1069. cdt.AddColumn("UUID", 36);
  1070. cdt.AddColumn("Name", 30);
  1071. cdt.AddColumn("HomeURL", 40);
  1072. cdt.AddRow(userId, string.Format("{0} {1}", ud.FirstName, ud.LastName), ud.HomeURL);
  1073. MainConsole.Instance.Output(cdt.ToString());
  1074. }
  1075. protected virtual void HandleShowUsers(string module, string[] cmd)
  1076. {
  1077. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  1078. cdt.AddColumn("UUID", 36);
  1079. cdt.AddColumn("Name", 30);
  1080. cdt.AddColumn("HomeURL", 40);
  1081. cdt.AddColumn("Checked", 10);
  1082. ICollection<UserData> copy = m_userCacheByID.Values;
  1083. foreach(UserData u in copy)
  1084. {
  1085. cdt.AddRow(u.Id, string.Format("{0} {1}", u.FirstName, u.LastName), u.HomeURL, u.HasGridUserTried ? "yes" : "no");
  1086. }
  1087. MainConsole.Instance.Output(cdt.ToString());
  1088. }
  1089. }
  1090. }