UserManagementModule.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. Dictionary<UUID,string> ret = new Dictionary<UUID,string>();
  355. if(m_Scenes.Count <= 0)
  356. return ret;
  357. UserData userdata = new UserData();
  358. UUID uuid = UUID.Zero;
  359. foreach(string id in ids)
  360. {
  361. if(UUID.TryParse(id, out uuid))
  362. {
  363. if (GetUser(uuid, out userdata))
  364. ret[uuid] = userdata.FirstName + " " + userdata.LastName;
  365. else
  366. ret[uuid] = "Unknown " + uuid.ToString() + "UMM1";
  367. }
  368. }
  369. return ret;
  370. }
  371. public virtual string GetUserHomeURL(UUID userID)
  372. {
  373. if (GetUser(userID, out UserData user) && user != null)
  374. {
  375. if (user.LastWebFail > 0 && Util.GetTimeStamp() - user.LastWebFail > BADURLEXPIRE)
  376. user.LastWebFail = -1;
  377. return user.HomeURL;
  378. }
  379. return string.Empty;
  380. }
  381. public virtual string GetUserHomeURL(UUID userID, out bool recentFail)
  382. {
  383. recentFail = false;
  384. if (GetUser(userID, out UserData user))
  385. {
  386. if (user.LastWebFail > 0)
  387. {
  388. if (Util.GetTimeStamp() - user.LastWebFail > BADURLEXPIRE)
  389. user.LastWebFail = -1;
  390. else
  391. recentFail = true;
  392. }
  393. return user.HomeURL;
  394. }
  395. return string.Empty;
  396. }
  397. public virtual string GetUserServerURL(UUID userID, string serverType)
  398. {
  399. UserData userdata;
  400. if(!GetUser(userID, out userdata))
  401. return string.Empty;
  402. if(userdata.IsLocal)
  403. return string.Empty;
  404. if(userdata.LastWebFail > 0)
  405. {
  406. if(Util.GetTimeStamp() - userdata.LastWebFail > BADURLEXPIRE)
  407. return string.Empty;
  408. userdata.LastWebFail = -1;
  409. }
  410. if (userdata.ServerURLs != null)
  411. {
  412. if(userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  413. {
  414. string turl = ourl as string;
  415. OSHHTPHost otmp = new OSHHTPHost(turl);
  416. if (otmp.IsValidHost)
  417. return otmp.URI;
  418. }
  419. return string.Empty;
  420. }
  421. if (!string.IsNullOrEmpty(userdata.HomeURL))
  422. {
  423. string homeuri = userdata.HomeURL.ToLower();
  424. if (!WebUtil.GlobalExpiringBadURLs.ContainsKey(homeuri))
  425. {
  426. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Requested url type {0} for {1}", serverType, userID);
  427. UserAgentServiceConnector uConn = new UserAgentServiceConnector(homeuri);
  428. try
  429. {
  430. userdata.ServerURLs = uConn.GetServerURLs(userID);
  431. }
  432. catch(System.Net.WebException e)
  433. {
  434. m_log.DebugFormat("[USER MANAGEMENT MODULE]: GetServerURLs call failed {0}", e.Message);
  435. WebUtil.GlobalExpiringBadURLs.Add(homeuri, BADURLEXPIRE * 1000);
  436. userdata.ServerURLs = new Dictionary<string, object>();
  437. }
  438. catch (Exception e)
  439. {
  440. m_log.Debug("[USER MANAGEMENT MODULE]: GetServerURLs call failed ", e);
  441. userdata.ServerURLs = new Dictionary<string, object>();
  442. }
  443. if (userdata.ServerURLs != null && userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  444. {
  445. string turl = ourl as string;
  446. OSHHTPHost otmp = new OSHHTPHost(turl);
  447. if (otmp.IsValidHost)
  448. return otmp.URI;
  449. }
  450. }
  451. }
  452. return string.Empty;
  453. }
  454. public void UserWebFailed(UUID id)
  455. {
  456. if(m_userCacheByID.TryGetValue(id, out UserData u))
  457. u.LastWebFail = Util.GetTimeStamp();
  458. }
  459. public virtual string GetUserServerURL(UUID userID, string serverType, out bool recentFail)
  460. {
  461. recentFail = false;
  462. if (!GetUser(userID, out UserData userdata))
  463. return string.Empty;
  464. if (userdata.IsLocal)
  465. return string.Empty;
  466. if (userdata.LastWebFail > 0)
  467. {
  468. if (Util.GetTimeStamp() - userdata.LastWebFail > BADURLEXPIRE)
  469. recentFail = true;
  470. else
  471. userdata.LastWebFail = -1;
  472. }
  473. if (userdata.ServerURLs != null)
  474. {
  475. if (userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  476. {
  477. string turl = ourl as string;
  478. OSHHTPHost otmp = new OSHHTPHost(turl);
  479. if (otmp.IsValidHost)
  480. return otmp.URI;
  481. }
  482. return string.Empty;
  483. }
  484. if (!recentFail && !string.IsNullOrEmpty(userdata.HomeURL))
  485. {
  486. string homeurl = userdata.HomeURL.ToLower();
  487. if(!WebUtil.GlobalExpiringBadURLs.ContainsKey(homeurl))
  488. {
  489. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Requested url type {0} for {1}", serverType, userID);
  490. UserAgentServiceConnector uConn = new UserAgentServiceConnector(homeurl);
  491. try
  492. {
  493. userdata.ServerURLs = uConn.GetServerURLs(userID);
  494. }
  495. catch (System.Net.WebException e)
  496. {
  497. m_log.DebugFormat("[USER MANAGEMENT MODULE]: GetServerURLs call failed {0}", e.Message);
  498. userdata.ServerURLs = new Dictionary<string, object>();
  499. userdata.LastWebFail = Util.GetTimeStamp();
  500. WebUtil.GlobalExpiringBadURLs.Add(homeurl, BADURLEXPIRE * 1000);
  501. recentFail = true;
  502. }
  503. catch (Exception e)
  504. {
  505. m_log.Debug("[USER MANAGEMENT MODULE]: GetServerURLs call failed ", e);
  506. userdata.ServerURLs = new Dictionary<string, object>();
  507. userdata.LastWebFail = Util.GetTimeStamp();
  508. recentFail = true;
  509. }
  510. if (userdata.ServerURLs != null && userdata.ServerURLs.TryGetValue(serverType, out object ourl) && ourl != null)
  511. {
  512. string turl = ourl as string;
  513. OSHHTPHost otmp = new OSHHTPHost(turl);
  514. if (otmp.IsValidHost)
  515. return otmp.URI;
  516. }
  517. }
  518. }
  519. return string.Empty;
  520. }
  521. public virtual string GetUserUUI(UUID userID)
  522. {
  523. string uui;
  524. GetUserUUI(userID, out uui);
  525. return uui;
  526. }
  527. public virtual bool GetUserUUI(UUID userID, out string uui)
  528. {
  529. bool result = GetUser(userID, out UserData ud);
  530. if (ud != null)
  531. {
  532. string homeURL = ud.HomeURL;
  533. string first = ud.FirstName, last = ud.LastName;
  534. if (ud.LastName.StartsWith("@"))
  535. {
  536. string[] parts = ud.FirstName.Split('.');
  537. if (parts.Length >= 2)
  538. {
  539. first = parts[0];
  540. last = parts[1];
  541. }
  542. uui = userID + ";" + homeURL + ";" + first + " " + last;
  543. return result;
  544. }
  545. }
  546. uui = userID.ToString();
  547. return result;
  548. }
  549. #region Cache Management
  550. public virtual bool GetUser(UUID uuid, out UserData userdata)
  551. {
  552. return GetUser(uuid, m_scopeID, out userdata);
  553. }
  554. public virtual bool GetUser(UUID uuid, UUID scopeID, out UserData userdata)
  555. {
  556. if (m_Scenes.Count <= 0)
  557. {
  558. userdata = new UserData();
  559. return false;
  560. }
  561. if (m_userCacheByID.TryGetValue(uuid, out userdata))
  562. {
  563. if (userdata.HasGridUserTried)
  564. return true;
  565. }
  566. else
  567. {
  568. userdata = new UserData();
  569. userdata.Id = uuid;
  570. userdata.FirstName = "Unknown";
  571. userdata.LastName = uuid.ToString();
  572. userdata.HomeURL = string.Empty;
  573. userdata.IsUnknownUser = true;
  574. userdata.HasGridUserTried = false;
  575. }
  576. if (!userdata.HasGridUserTried)
  577. {
  578. /* rewrite here */
  579. UserAccount account = m_userAccountService.GetUserAccount(scopeID, uuid);
  580. if (account != null)
  581. {
  582. userdata.FirstName = account.FirstName;
  583. userdata.LastName = account.LastName;
  584. userdata.HomeURL = string.Empty;
  585. userdata.IsUnknownUser = false;
  586. userdata.IsLocal = true;
  587. userdata.HasGridUserTried = true;
  588. }
  589. }
  590. if (!userdata.HasGridUserTried)
  591. {
  592. GridUserInfo uInfo = null;
  593. if (null != m_gridUserService)
  594. {
  595. uInfo = m_gridUserService.GetGridUserInfo(uuid.ToString());
  596. }
  597. if (uInfo != null)
  598. {
  599. string url, first, last, tmp;
  600. UUID u;
  601. if (uInfo.UserID.Length >= 36 && Util.ParseUniversalUserIdentifier(uInfo.UserID, out u, out url, out first, out last, out tmp))
  602. {
  603. bool isvalid = CheckUrl(url, out bool islocal, out OSHHTPHost host);
  604. if (isvalid)
  605. {
  606. if(islocal)
  607. {
  608. userdata.FirstName = first;
  609. userdata.LastName = last;
  610. userdata.HomeURL = string.Empty;
  611. userdata.IsLocal = true;
  612. userdata.IsUnknownUser = false;
  613. }
  614. else
  615. {
  616. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  617. userdata.HomeURL = host.URI;
  618. userdata.LastName = "@" + host.HostAndPort;
  619. userdata.IsLocal = false;
  620. userdata.IsUnknownUser = false;
  621. }
  622. }
  623. }
  624. else
  625. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  626. }
  627. userdata.HasGridUserTried = true;
  628. }
  629. /* END: do not wrap this code in any lock here */
  630. m_userCacheByID.Add(uuid, userdata, 1800000);
  631. return !userdata.IsUnknownUser;
  632. }
  633. public void AddUser(UserAccount account)
  634. {
  635. UUID id = account.PrincipalID;
  636. bool local = account.LocalToGrid;
  637. UserData user = new UserData();
  638. user.Id = id;
  639. user.FirstName = account.FirstName;
  640. user.LastName = account.LastName;
  641. user.HomeURL = string.Empty;
  642. user.IsUnknownUser = false;
  643. user.HasGridUserTried = true;
  644. user.IsLocal = local;
  645. m_userCacheByID.Add(id, user, local ? LOCALEXPIRE : HGEXPIRE);
  646. }
  647. public void AddSystemUser(UUID uuid, string first, string last)
  648. {
  649. UserData user = new UserData()
  650. {
  651. Id = uuid,
  652. FirstName = first,
  653. LastName = last,
  654. IsLocal = true,
  655. HasGridUserTried = true,
  656. HomeURL = string.Empty,
  657. IsUnknownUser = false
  658. };
  659. m_userCacheByID.Add(uuid, user, NOEXPIRE);
  660. }
  661. public void AddNPCUser(UUID uuid, string first, string last)
  662. {
  663. UserData user = new UserData()
  664. {
  665. Id = uuid,
  666. FirstName = first,
  667. LastName = last,
  668. HasGridUserTried = true,
  669. IsLocal = true,
  670. HomeURL = string.Empty,
  671. IsUnknownUser = false
  672. };
  673. m_userCacheByID.Add(uuid, user, NOEXPIRE);
  674. }
  675. public virtual void AddUser(UUID uuid, string first, string last, string homeURL)
  676. {
  677. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, first {1}, last {2}, url {3}", uuid, first, last, homeURL);
  678. UserData oldUser;
  679. if (m_userCacheByID.TryGetValue(uuid, out oldUser))
  680. {
  681. if (!oldUser.IsUnknownUser)
  682. {
  683. if (homeURL != oldUser.HomeURL && m_DisplayChangingHomeURI)
  684. {
  685. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Different HomeURI for {0} {1} ({2}): {3} and {4}",
  686. first, last, uuid.ToString(), homeURL, oldUser.HomeURL);
  687. }
  688. /* no update needed */
  689. return;
  690. }
  691. }
  692. oldUser = new UserData();
  693. oldUser.Id = uuid;
  694. oldUser.HasGridUserTried = false;
  695. oldUser.IsUnknownUser = false;
  696. bool local;
  697. if (CheckUrl(homeURL, out local, out OSHHTPHost host))
  698. {
  699. if (local)
  700. {
  701. oldUser.FirstName = first;
  702. oldUser.LastName = last;
  703. oldUser.IsLocal = true;
  704. oldUser.HomeURL = string.Empty;
  705. oldUser.HasGridUserTried = true;
  706. m_userCacheByID.Add(uuid, oldUser, LOCALEXPIRE);
  707. }
  708. else
  709. {
  710. oldUser.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  711. oldUser.LastName = "@" + host.HostAndPort;
  712. oldUser.HomeURL = host.URI;
  713. oldUser.IsLocal = false;
  714. m_userCacheByID.Add(uuid, oldUser, HGEXPIRE);
  715. }
  716. }
  717. else
  718. {
  719. oldUser.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  720. oldUser.LastName = "UMMM0Unknown";
  721. oldUser.IsLocal = true;
  722. oldUser.HomeURL = string.Empty;
  723. oldUser.HasGridUserTried = true;
  724. oldUser.IsUnknownUser = true;
  725. m_userCacheByID.Add(uuid, oldUser, BADEXPIRE);
  726. }
  727. }
  728. public virtual void AddCreatorUser(UUID id, string creatorData)
  729. {
  730. // m_log.InfoFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, creatorData {1}", id, creatorData);
  731. if(string.IsNullOrEmpty(creatorData))
  732. return;
  733. if(m_userCacheByID.ContainsKey(id))
  734. return;
  735. string homeURL;
  736. string firstname = string.Empty;
  737. string lastname = string.Empty;
  738. //creatorData = <endpoint>;<name>
  739. string[] parts = creatorData.Split(';');
  740. if(parts.Length > 1)
  741. {
  742. string[] nameparts = parts[1].Split(' ');
  743. if(nameparts.Length < 2)
  744. return;
  745. firstname = nameparts[0];
  746. for(int xi = 1; xi < nameparts.Length; ++xi)
  747. lastname += nameparts[xi];
  748. if (string.IsNullOrWhiteSpace(firstname))
  749. return;
  750. if (string.IsNullOrWhiteSpace(lastname))
  751. return;
  752. }
  753. else
  754. return;
  755. homeURL = parts[0];
  756. var oldUser = new UserData();
  757. oldUser.Id = id;
  758. oldUser.HasGridUserTried = false;
  759. oldUser.IsUnknownUser = false;
  760. if (CheckUrl(homeURL, out bool local, out OSHHTPHost host))
  761. {
  762. if (local)
  763. {
  764. oldUser.FirstName = firstname;
  765. oldUser.LastName = lastname;
  766. oldUser.IsLocal = true;
  767. oldUser.HomeURL = string.Empty;
  768. oldUser.HasGridUserTried = true;
  769. }
  770. else
  771. {
  772. oldUser.FirstName = firstname + "." + lastname.Replace(" ", ".");
  773. oldUser.LastName = "@" + host.HostAndPort;
  774. oldUser.HomeURL = host.URI;
  775. oldUser.IsLocal = false;
  776. }
  777. }
  778. else
  779. {
  780. oldUser.FirstName = firstname + "." + lastname.Replace(" ", ".");
  781. oldUser.LastName = "UMMM1Unknown";
  782. oldUser.IsLocal = true;
  783. oldUser.HomeURL = string.Empty;
  784. oldUser.HasGridUserTried = true;
  785. oldUser.IsUnknownUser = true;
  786. }
  787. m_userCacheByID.Add(id, oldUser, NOEXPIRE);
  788. }
  789. public bool RemoveUser(UUID uuid)
  790. {
  791. return m_userCacheByID.Remove(uuid);
  792. }
  793. #endregion
  794. public virtual bool IsLocalGridUser(UUID uuid)
  795. {
  796. if (m_Scenes.Count <= 0)
  797. return true;
  798. if (m_userCacheByID.TryGetValue(uuid, out UserData u))
  799. {
  800. if (u.HasGridUserTried)
  801. return u.IsLocal;
  802. }
  803. if(m_userAccountService == null)
  804. return true;
  805. UserAccount account = m_userAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, uuid);
  806. if (account == null)
  807. return false;
  808. if(u == null)
  809. AddUser(account);
  810. else
  811. u.HasGridUserTried = true;
  812. return true;
  813. }
  814. #endregion IUserManagement
  815. protected virtual void Init(IConfigSource config)
  816. {
  817. AddSystemUser(UUID.Zero, "Unknown", "User");
  818. AddSystemUser(Constants.m_MrOpenSimID, "Mr", "Opensim");
  819. RegisterConsoleCmds();
  820. IConfig userManagementConfig = config.Configs["UserManagement"];
  821. if (userManagementConfig != null)
  822. m_DisplayChangingHomeURI = userManagementConfig.GetBoolean("DisplayChangingHomeURI", false);
  823. }
  824. protected virtual void RegisterConsoleCmds()
  825. {
  826. MainConsole.Instance.Commands.AddCommand("Users", true,
  827. "show name",
  828. "show name <uuid>",
  829. "Show the bindings between a single user UUID and a user name",
  830. String.Empty,
  831. HandleShowUser);
  832. MainConsole.Instance.Commands.AddCommand("Users", true,
  833. "show names",
  834. "show names",
  835. "Show the bindings between user UUIDs and user names",
  836. String.Empty,
  837. HandleShowUsers);
  838. MainConsole.Instance.Commands.AddCommand("Users", true,
  839. "reset user cache",
  840. "reset user cache",
  841. "reset user cache to allow changed settings to be applied",
  842. String.Empty,
  843. HandleResetUserCache);
  844. }
  845. protected virtual void HandleResetUserCache(string module, string[] cmd)
  846. {
  847. lock(m_userCacheByID)
  848. {
  849. m_userCacheByID.Clear();
  850. }
  851. }
  852. protected virtual void HandleShowUser(string module, string[] cmd)
  853. {
  854. if (cmd.Length < 3)
  855. {
  856. MainConsole.Instance.Output("Usage: show name <uuid>");
  857. return;
  858. }
  859. UUID userId;
  860. if (!ConsoleUtil.TryParseConsoleUuid(MainConsole.Instance, cmd[2], out userId))
  861. return;
  862. UserData ud;
  863. if(!GetUser(userId, out ud))
  864. {
  865. MainConsole.Instance.Output("No name known for user with id {0}", userId);
  866. return;
  867. }
  868. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  869. cdt.AddColumn("UUID", 36);
  870. cdt.AddColumn("Name", 30);
  871. cdt.AddColumn("HomeURL", 40);
  872. cdt.AddRow(userId, string.Format("{0} {1}", ud.FirstName, ud.LastName), ud.HomeURL);
  873. MainConsole.Instance.Output(cdt.ToString());
  874. }
  875. protected virtual void HandleShowUsers(string module, string[] cmd)
  876. {
  877. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  878. cdt.AddColumn("UUID", 36);
  879. cdt.AddColumn("Name", 30);
  880. cdt.AddColumn("HomeURL", 40);
  881. cdt.AddColumn("Checked", 10);
  882. ICollection<UserData> copy = m_userCacheByID.Values;
  883. foreach(UserData u in copy)
  884. {
  885. cdt.AddRow(u.Id, string.Format("{0} {1}", u.FirstName, u.LastName), u.HomeURL, u.HasGridUserTried ? "yes" : "no");
  886. }
  887. MainConsole.Instance.Output(cdt.ToString());
  888. }
  889. }
  890. }