UserManagementModule.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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.IO;
  30. using System.Reflection;
  31. using System.Threading;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Monitoring;
  35. using OpenSim.Region.ClientStack.LindenUDP;
  36. using OpenSim.Region.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Services.Connectors.Hypergrid;
  41. using OpenMetaverse;
  42. using OpenMetaverse.Packets;
  43. using log4net;
  44. using Nini.Config;
  45. using Mono.Addins;
  46. using DirFindFlags = OpenMetaverse.DirectoryManager.DirFindFlags;
  47. namespace OpenSim.Region.CoreModules.Framework.UserManagement
  48. {
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UserManagementModule")]
  50. public class UserManagementModule : ISharedRegionModule, IUserManagement, IPeople
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected bool m_Enabled;
  54. protected List<Scene> m_Scenes = new List<Scene>();
  55. protected IServiceThrottleModule m_ServiceThrottle;
  56. // The cache
  57. protected Dictionary<UUID, UserData> m_UserCache = new Dictionary<UUID, UserData>();
  58. protected bool m_DisplayChangingHomeURI = false;
  59. #region ISharedRegionModule
  60. public virtual void Initialise(IConfigSource config)
  61. {
  62. string umanmod = config.Configs["Modules"].GetString("UserManagementModule", Name);
  63. if (umanmod == Name)
  64. {
  65. m_Enabled = true;
  66. Init();
  67. m_log.DebugFormat("[USER MANAGEMENT MODULE]: {0} is enabled", Name);
  68. }
  69. if(!m_Enabled)
  70. {
  71. return;
  72. }
  73. IConfig userManagementConfig = config.Configs["UserManagement"];
  74. if (userManagementConfig == null)
  75. return;
  76. m_DisplayChangingHomeURI = userManagementConfig.GetBoolean("DisplayChangingHomeURI", false);
  77. }
  78. public virtual bool IsSharedModule
  79. {
  80. get { return true; }
  81. }
  82. public virtual string Name
  83. {
  84. get { return "BasicUserManagementModule"; }
  85. }
  86. public virtual Type ReplaceableInterface
  87. {
  88. get { return null; }
  89. }
  90. public virtual void AddRegion(Scene scene)
  91. {
  92. if (m_Enabled)
  93. {
  94. lock (m_Scenes)
  95. {
  96. m_Scenes.Add(scene);
  97. }
  98. scene.RegisterModuleInterface<IUserManagement>(this);
  99. scene.RegisterModuleInterface<IPeople>(this);
  100. scene.EventManager.OnNewClient += new EventManager.OnNewClientDelegate(EventManager_OnNewClient);
  101. scene.EventManager.OnPrimsLoaded += new EventManager.PrimsLoaded(EventManager_OnPrimsLoaded);
  102. }
  103. }
  104. public virtual void RemoveRegion(Scene scene)
  105. {
  106. if (m_Enabled)
  107. {
  108. scene.UnregisterModuleInterface<IUserManagement>(this);
  109. lock (m_Scenes)
  110. {
  111. m_Scenes.Remove(scene);
  112. }
  113. }
  114. }
  115. public virtual void RegionLoaded(Scene s)
  116. {
  117. if (m_Enabled && m_ServiceThrottle == null)
  118. m_ServiceThrottle = s.RequestModuleInterface<IServiceThrottleModule>();
  119. }
  120. public virtual void PostInitialise()
  121. {
  122. }
  123. public virtual void Close()
  124. {
  125. lock (m_Scenes)
  126. {
  127. m_Scenes.Clear();
  128. }
  129. lock (m_UserCache)
  130. m_UserCache.Clear();
  131. }
  132. #endregion ISharedRegionModule
  133. #region Event Handlers
  134. protected virtual void EventManager_OnPrimsLoaded(Scene s)
  135. {
  136. // let's sniff all the user names referenced by objects in the scene
  137. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Caching creators' data from {0} ({1} objects)...", s.RegionInfo.RegionName, s.GetEntities().Length);
  138. s.ForEachSOG(delegate(SceneObjectGroup sog) { CacheCreators(sog); });
  139. }
  140. protected virtual void EventManager_OnNewClient(IClientAPI client)
  141. {
  142. client.OnConnectionClosed += new Action<IClientAPI>(HandleConnectionClosed);
  143. client.OnNameFromUUIDRequest += new UUIDNameRequest(HandleUUIDNameRequest);
  144. client.OnAvatarPickerRequest += new AvatarPickerRequest(HandleAvatarPickerRequest);
  145. }
  146. protected virtual void HandleConnectionClosed(IClientAPI client)
  147. {
  148. client.OnNameFromUUIDRequest -= new UUIDNameRequest(HandleUUIDNameRequest);
  149. client.OnAvatarPickerRequest -= new AvatarPickerRequest(HandleAvatarPickerRequest);
  150. client.OnConnectionClosed -= new Action<IClientAPI>(HandleConnectionClosed);
  151. }
  152. protected virtual void HandleUUIDNameRequest(UUID uuid, IClientAPI client)
  153. {
  154. // m_log.DebugFormat(
  155. // "[USER MANAGEMENT MODULE]: Handling request for name binding of UUID {0} from {1}",
  156. // uuid, remote_client.Name);
  157. if(m_Scenes.Count <= 0)
  158. return;
  159. if (m_Scenes[0].LibraryService != null && (m_Scenes[0].LibraryService.LibraryRootFolder.Owner == uuid))
  160. {
  161. client.SendNameReply(uuid, "Mr", "OpenSim");
  162. }
  163. else
  164. {
  165. UserData user;
  166. /* bypass that continuation here when entry is already available */
  167. lock (m_UserCache)
  168. {
  169. if (m_UserCache.TryGetValue(uuid, out user))
  170. {
  171. if (!user.IsUnknownUser && user.HasGridUserTried)
  172. {
  173. client.SendNameReply(uuid, user.FirstName, user.LastName);
  174. return;
  175. }
  176. }
  177. }
  178. // Not found in cache, queue continuation
  179. m_ServiceThrottle.Enqueue("uuidname", uuid.ToString(), delegate
  180. {
  181. //m_log.DebugFormat("[YYY]: Name request {0}", uuid);
  182. // As least upto September 2013, clients permanently cache UUID -> Name bindings. Some clients
  183. // appear to clear this when the user asks it to clear the cache, but others may not.
  184. //
  185. // So to avoid clients
  186. // (particularly Hypergrid clients) permanently binding "Unknown User" to a given UUID, we will
  187. // instead drop the request entirely.
  188. if(!client.IsActive)
  189. return;
  190. if (GetUser(uuid, client.ScopeId, out user))
  191. {
  192. if(client.IsActive)
  193. client.SendNameReply(uuid, user.FirstName, user.LastName);
  194. }
  195. // else
  196. // m_log.DebugFormat(
  197. // "[USER MANAGEMENT MODULE]: No bound name for {0} found, ignoring request from {1}",
  198. // uuid, client.Name);
  199. });
  200. }
  201. }
  202. public virtual void HandleAvatarPickerRequest(IClientAPI client, UUID avatarID, UUID RequestID, string query)
  203. {
  204. //EventManager.TriggerAvatarPickerRequest();
  205. m_log.DebugFormat("[USER MANAGEMENT MODULE]: HandleAvatarPickerRequest for {0}", query);
  206. List<UserData> users = GetUserData(query, 500, 1);
  207. AvatarPickerReplyPacket replyPacket = (AvatarPickerReplyPacket)PacketPool.Instance.GetPacket(PacketType.AvatarPickerReply);
  208. // TODO: don't create new blocks if recycling an old packet
  209. AvatarPickerReplyPacket.DataBlock[] searchData =
  210. new AvatarPickerReplyPacket.DataBlock[users.Count];
  211. AvatarPickerReplyPacket.AgentDataBlock agentData = new AvatarPickerReplyPacket.AgentDataBlock();
  212. agentData.AgentID = avatarID;
  213. agentData.QueryID = RequestID;
  214. replyPacket.AgentData = agentData;
  215. //byte[] bytes = new byte[AvatarResponses.Count*32];
  216. int i = 0;
  217. foreach (UserData item in users)
  218. {
  219. UUID translatedIDtem = item.Id;
  220. searchData[i] = new AvatarPickerReplyPacket.DataBlock();
  221. searchData[i].AvatarID = translatedIDtem;
  222. searchData[i].FirstName = Utils.StringToBytes((string)item.FirstName);
  223. searchData[i].LastName = Utils.StringToBytes((string)item.LastName);
  224. i++;
  225. }
  226. if (users.Count == 0)
  227. {
  228. searchData = new AvatarPickerReplyPacket.DataBlock[0];
  229. }
  230. replyPacket.Data = searchData;
  231. AvatarPickerReplyAgentDataArgs agent_data = new AvatarPickerReplyAgentDataArgs();
  232. agent_data.AgentID = replyPacket.AgentData.AgentID;
  233. agent_data.QueryID = replyPacket.AgentData.QueryID;
  234. List<AvatarPickerReplyDataArgs> data_args = new List<AvatarPickerReplyDataArgs>();
  235. for (i = 0; i < replyPacket.Data.Length; i++)
  236. {
  237. AvatarPickerReplyDataArgs data_arg = new AvatarPickerReplyDataArgs();
  238. data_arg.AvatarID = replyPacket.Data[i].AvatarID;
  239. data_arg.FirstName = replyPacket.Data[i].FirstName;
  240. data_arg.LastName = replyPacket.Data[i].LastName;
  241. data_args.Add(data_arg);
  242. }
  243. client.SendAvatarPickerReply(agent_data, data_args);
  244. }
  245. protected virtual void AddAdditionalUsers(string query, List<UserData> users)
  246. {
  247. }
  248. #endregion Event Handlers
  249. #region IPeople
  250. public virtual List<UserData> GetUserData(string query, int page_size, int page_number)
  251. {
  252. if(m_Scenes.Count <= 0)
  253. return new List<UserData>();;
  254. // search the user accounts service
  255. List<UserAccount> accs = m_Scenes[0].UserAccountService.GetUserAccounts(m_Scenes[0].RegionInfo.ScopeID, query);
  256. List<UserData> users = new List<UserData>();
  257. if (accs != null)
  258. {
  259. foreach (UserAccount acc in accs)
  260. {
  261. UserData ud = new UserData();
  262. ud.FirstName = acc.FirstName;
  263. ud.LastName = acc.LastName;
  264. ud.Id = acc.PrincipalID;
  265. ud.HasGridUserTried = true;
  266. ud.IsUnknownUser = false;
  267. users.Add(ud);
  268. }
  269. }
  270. // search the local cache
  271. foreach (UserData data in m_UserCache.Values)
  272. {
  273. if (data.Id != UUID.Zero && !data.IsUnknownUser &&
  274. users.Find(delegate(UserData d) { return d.Id == data.Id; }) == null &&
  275. (data.FirstName.ToLower().StartsWith(query.ToLower()) || data.LastName.ToLower().StartsWith(query.ToLower())))
  276. users.Add(data);
  277. }
  278. AddAdditionalUsers(query, users);
  279. return users;
  280. }
  281. #endregion IPeople
  282. protected virtual void CacheCreators(SceneObjectGroup sog)
  283. {
  284. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: processing {0} {1}; {2}", sog.RootPart.Name, sog.RootPart.CreatorData, sog.RootPart.CreatorIdentification);
  285. AddUser(sog.RootPart.CreatorID, sog.RootPart.CreatorData);
  286. foreach (SceneObjectPart sop in sog.Parts)
  287. {
  288. AddUser(sop.CreatorID, sop.CreatorData);
  289. foreach (TaskInventoryItem item in sop.TaskInventory.Values)
  290. AddUser(item.CreatorID, item.CreatorData);
  291. }
  292. }
  293. /// <summary>
  294. ///
  295. /// </summary>
  296. /// <param name="uuid"></param>
  297. /// <param name="names">Caller please provide a properly instantiated array for names, string[2]</param>
  298. /// <returns></returns>
  299. protected virtual bool TryGetUserNames(UUID uuid, string[] names)
  300. {
  301. if (names == null)
  302. names = new string[2];
  303. if (TryGetUserNamesFromCache(uuid, names))
  304. return true;
  305. if (TryGetUserNamesFromServices(uuid, names))
  306. return true;
  307. return false;
  308. }
  309. protected virtual bool TryGetUserNamesFromCache(UUID uuid, string[] names)
  310. {
  311. lock (m_UserCache)
  312. {
  313. if (m_UserCache.ContainsKey(uuid))
  314. {
  315. names[0] = m_UserCache[uuid].FirstName;
  316. names[1] = m_UserCache[uuid].LastName;
  317. return true;
  318. }
  319. }
  320. return false;
  321. }
  322. /// <summary>
  323. /// Try to get the names bound to the given uuid, from the services.
  324. /// </summary>
  325. /// <returns>True if the name was found, false if not.</returns>
  326. /// <param name='uuid'></param>
  327. /// <param name='names'>The array of names if found. If not found, then names[0] = "Unknown" and names[1] = "User"</param>
  328. protected virtual bool TryGetUserNamesFromServices(UUID uuid, string[] names)
  329. {
  330. if(m_Scenes.Count <= 0)
  331. return false;
  332. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(UUID.Zero, uuid);
  333. if (account != null)
  334. {
  335. names[0] = account.FirstName;
  336. names[1] = account.LastName;
  337. UserData user = new UserData();
  338. user.FirstName = account.FirstName;
  339. user.LastName = account.LastName;
  340. lock (m_UserCache)
  341. m_UserCache[uuid] = user;
  342. return true;
  343. }
  344. else
  345. {
  346. // Let's try the GridUser service
  347. GridUserInfo uInfo = m_Scenes[0].GridUserService.GetGridUserInfo(uuid.ToString());
  348. if (uInfo != null)
  349. {
  350. string url, first, last, tmp;
  351. UUID u;
  352. if (Util.ParseUniversalUserIdentifier(uInfo.UserID, out u, out url, out first, out last, out tmp))
  353. {
  354. AddUser(uuid, first, last, url);
  355. if (m_UserCache.ContainsKey(uuid))
  356. {
  357. names[0] = m_UserCache[uuid].FirstName;
  358. names[1] = m_UserCache[uuid].LastName;
  359. return true;
  360. }
  361. }
  362. else
  363. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  364. }
  365. // else
  366. // {
  367. // m_log.DebugFormat("[USER MANAGEMENT MODULE]: No grid user found for {0}", uuid);
  368. // }
  369. names[0] = "Unknown";
  370. names[1] = "UserUMMTGUN9";
  371. return false;
  372. }
  373. }
  374. #region IUserManagement
  375. public virtual UUID GetUserIdByName(string name)
  376. {
  377. string[] parts = name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  378. if (parts.Length < 2)
  379. throw new Exception("Name must have 2 components");
  380. return GetUserIdByName(parts[0], parts[1]);
  381. }
  382. public virtual UUID GetUserIdByName(string firstName, string lastName)
  383. {
  384. if(m_Scenes.Count <= 0)
  385. return UUID.Zero;
  386. // TODO: Optimize for reverse lookup if this gets used by non-console commands.
  387. lock (m_UserCache)
  388. {
  389. foreach (UserData user in m_UserCache.Values)
  390. {
  391. if (user.FirstName == firstName && user.LastName == lastName)
  392. return user.Id;
  393. }
  394. }
  395. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(UUID.Zero, firstName, lastName);
  396. if (account != null)
  397. return account.PrincipalID;
  398. return UUID.Zero;
  399. }
  400. public virtual string GetUserName(UUID uuid)
  401. {
  402. UserData user;
  403. GetUser(uuid, out user);
  404. return user.FirstName + " " + user.LastName;
  405. }
  406. public virtual Dictionary<UUID,string> GetUsersNames(string[] ids, UUID scopeID)
  407. {
  408. Dictionary<UUID,string> ret = new Dictionary<UUID,string>();
  409. if(m_Scenes.Count <= 0)
  410. return ret;
  411. List<string> missing = new List<string>();
  412. Dictionary<UUID,string> untried = new Dictionary<UUID, string>();
  413. // look in cache
  414. UserData userdata = new UserData();
  415. UUID uuid = UUID.Zero;
  416. foreach(string id in ids)
  417. {
  418. if(UUID.TryParse(id, out uuid))
  419. {
  420. lock (m_UserCache)
  421. {
  422. if (m_UserCache.TryGetValue(uuid, out userdata) &&
  423. userdata.FirstName != "Unknown" && userdata.FirstName != string.Empty)
  424. {
  425. string name = userdata.FirstName + " " + userdata.LastName;
  426. if(userdata.HasGridUserTried)
  427. ret[uuid] = name;
  428. else
  429. {
  430. untried[uuid] = name;
  431. missing.Add(id);
  432. }
  433. }
  434. else
  435. missing.Add(id);
  436. }
  437. }
  438. }
  439. if(missing.Count == 0)
  440. return ret;
  441. // try user account service
  442. List<UserAccount> accounts = m_Scenes[0].UserAccountService.GetUserAccounts(
  443. scopeID, missing);
  444. if(accounts.Count != 0)
  445. {
  446. foreach(UserAccount uac in accounts)
  447. {
  448. if(uac != null)
  449. {
  450. string name = uac.FirstName + " " + uac.LastName;
  451. ret[uac.PrincipalID] = name;
  452. missing.Remove(uac.PrincipalID.ToString()); // slowww
  453. untried.Remove(uac.PrincipalID);
  454. userdata = new UserData();
  455. userdata.Id = uac.PrincipalID;
  456. userdata.FirstName = uac.FirstName;
  457. userdata.LastName = uac.LastName;
  458. userdata.HomeURL = string.Empty;
  459. userdata.IsUnknownUser = false;
  460. userdata.HasGridUserTried = true;
  461. lock (m_UserCache)
  462. m_UserCache[uac.PrincipalID] = userdata;
  463. }
  464. }
  465. }
  466. if (missing.Count == 0 || m_Scenes[0].GridUserService == null)
  467. return ret;
  468. // try grid user service
  469. GridUserInfo[] pinfos = m_Scenes[0].GridUserService.GetGridUserInfo(missing.ToArray());
  470. if(pinfos.Length > 0)
  471. {
  472. foreach(GridUserInfo uInfo in pinfos)
  473. {
  474. if (uInfo != null)
  475. {
  476. string url, first, last, tmp;
  477. if(uInfo.UserID.Length <= 36)
  478. continue;
  479. if (Util.ParseUniversalUserIdentifier(uInfo.UserID, out uuid, out url, out first, out last, out tmp))
  480. {
  481. if (url != string.Empty)
  482. {
  483. try
  484. {
  485. userdata = new UserData();
  486. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  487. userdata.LastName = "@" + new Uri(url).Authority;
  488. userdata.Id = uuid;
  489. userdata.HomeURL = url;
  490. userdata.IsUnknownUser = false;
  491. userdata.HasGridUserTried = true;
  492. lock (m_UserCache)
  493. m_UserCache[uuid] = userdata;
  494. string name = userdata.FirstName + " " + userdata.LastName;
  495. ret[uuid] = name;
  496. missing.Remove(uuid.ToString());
  497. untried.Remove(uuid);
  498. }
  499. catch
  500. {
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }
  507. // add the untried in cache that still failed
  508. if(untried.Count > 0)
  509. {
  510. foreach(KeyValuePair<UUID, string> kvp in untried)
  511. {
  512. ret[kvp.Key] = kvp.Value;
  513. missing.Remove((kvp.Key).ToString());
  514. }
  515. }
  516. // add the UMMthings ( not sure we should)
  517. if(missing.Count > 0)
  518. {
  519. foreach(string id in missing)
  520. {
  521. if(UUID.TryParse(id, out uuid) && uuid != UUID.Zero)
  522. {
  523. if (m_Scenes[0].LibraryService != null &&
  524. (m_Scenes[0].LibraryService.LibraryRootFolder.Owner == uuid))
  525. ret[uuid] = "Mr OpenSim";
  526. else
  527. ret[uuid] = "Unknown UserUMMAU43";
  528. }
  529. }
  530. }
  531. return ret;
  532. }
  533. public virtual string GetUserHomeURL(UUID userID)
  534. {
  535. UserData user;
  536. if(GetUser(userID, out user))
  537. {
  538. return user.HomeURL;
  539. }
  540. return string.Empty;
  541. }
  542. public virtual string GetUserServerURL(UUID userID, string serverType)
  543. {
  544. UserData userdata;
  545. if(!GetUser(userID, out userdata))
  546. {
  547. return string.Empty;
  548. }
  549. if (userdata.ServerURLs != null && userdata.ServerURLs.ContainsKey(serverType) && userdata.ServerURLs[serverType] != null)
  550. {
  551. return userdata.ServerURLs[serverType].ToString();
  552. }
  553. if (!string.IsNullOrEmpty(userdata.HomeURL))
  554. {
  555. // m_log.DebugFormat("[USER MANAGEMENT MODULE]: Requested url type {0} for {1}", serverType, userID);
  556. UserAgentServiceConnector uConn = new UserAgentServiceConnector(userdata.HomeURL);
  557. try
  558. {
  559. userdata.ServerURLs = uConn.GetServerURLs(userID);
  560. }
  561. catch(System.Net.WebException e)
  562. {
  563. m_log.DebugFormat("[USER MANAGEMENT MODULE]: GetServerURLs call failed {0}", e.Message);
  564. userdata.ServerURLs = new Dictionary<string, object>();
  565. }
  566. catch (Exception e)
  567. {
  568. m_log.Debug("[USER MANAGEMENT MODULE]: GetServerURLs call failed ", e);
  569. userdata.ServerURLs = new Dictionary<string, object>();
  570. }
  571. if (userdata.ServerURLs != null && userdata.ServerURLs.ContainsKey(serverType) && userdata.ServerURLs[serverType] != null)
  572. return userdata.ServerURLs[serverType].ToString();
  573. }
  574. return string.Empty;
  575. }
  576. public virtual string GetUserUUI(UUID userID)
  577. {
  578. string uui;
  579. GetUserUUI(userID, out uui);
  580. return uui;
  581. }
  582. public virtual bool GetUserUUI(UUID userID, out string uui)
  583. {
  584. UserData ud;
  585. bool result = GetUser(userID, out ud);
  586. if (ud != null)
  587. {
  588. string homeURL = ud.HomeURL;
  589. string first = ud.FirstName, last = ud.LastName;
  590. if (ud.LastName.StartsWith("@"))
  591. {
  592. string[] parts = ud.FirstName.Split('.');
  593. if (parts.Length >= 2)
  594. {
  595. first = parts[0];
  596. last = parts[1];
  597. }
  598. uui = userID + ";" + homeURL + ";" + first + " " + last;
  599. return result;
  600. }
  601. }
  602. uui = userID.ToString();
  603. return result;
  604. }
  605. #region Cache Management
  606. public virtual bool GetUser(UUID uuid, out UserData userdata)
  607. {
  608. return GetUser(uuid, m_Scenes[0].RegionInfo.ScopeID, out userdata);
  609. }
  610. public virtual bool GetUser(UUID uuid, UUID scopeID, out UserData userdata)
  611. {
  612. if (m_Scenes.Count <= 0)
  613. {
  614. userdata = new UserData();
  615. return false;
  616. }
  617. lock (m_UserCache)
  618. {
  619. if (m_UserCache.TryGetValue(uuid, out userdata))
  620. {
  621. if (userdata.HasGridUserTried)
  622. {
  623. return true;
  624. }
  625. }
  626. else
  627. {
  628. userdata = new UserData();
  629. userdata.Id = uuid;
  630. userdata.FirstName = "Unknown";
  631. userdata.LastName = "UserUMMAU42";
  632. userdata.HomeURL = string.Empty;
  633. userdata.IsUnknownUser = true;
  634. userdata.HasGridUserTried = false;
  635. }
  636. }
  637. /* BEGIN: do not wrap this code in any lock here
  638. * There are HTTP calls in here.
  639. */
  640. if (!userdata.HasGridUserTried)
  641. {
  642. /* rewrite here */
  643. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(scopeID, uuid);
  644. if (account != null)
  645. {
  646. userdata.FirstName = account.FirstName;
  647. userdata.LastName = account.LastName;
  648. userdata.HomeURL = string.Empty;
  649. userdata.IsUnknownUser = false;
  650. userdata.HasGridUserTried = true;
  651. }
  652. }
  653. if (!userdata.HasGridUserTried)
  654. {
  655. GridUserInfo uInfo = null;
  656. if (null != m_Scenes[0].GridUserService)
  657. {
  658. uInfo = m_Scenes[0].GridUserService.GetGridUserInfo(uuid.ToString());
  659. }
  660. if (uInfo != null)
  661. {
  662. string url, first, last, tmp;
  663. UUID u;
  664. if(uInfo.UserID.Length <= 36)
  665. {
  666. /* not a UUI */
  667. }
  668. else if (Util.ParseUniversalUserIdentifier(uInfo.UserID, out u, out url, out first, out last, out tmp))
  669. {
  670. if (url != string.Empty)
  671. {
  672. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  673. userdata.HomeURL = url;
  674. try
  675. {
  676. userdata.LastName = "@" + new Uri(url).Authority;
  677. userdata.IsUnknownUser = false;
  678. }
  679. catch
  680. {
  681. userdata.LastName = "@unknown";
  682. }
  683. userdata.HasGridUserTried = true;
  684. }
  685. }
  686. else
  687. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  688. }
  689. }
  690. /* END: do not wrap this code in any lock here */
  691. lock (m_UserCache)
  692. {
  693. m_UserCache[uuid] = userdata;
  694. }
  695. return !userdata.IsUnknownUser;
  696. }
  697. public virtual void AddUser(UUID uuid, string first, string last, bool isNPC = false)
  698. {
  699. lock (m_UserCache)
  700. {
  701. if (!m_UserCache.ContainsKey(uuid))
  702. {
  703. UserData user = new UserData();
  704. user.Id = uuid;
  705. user.FirstName = first;
  706. user.LastName = last;
  707. user.IsUnknownUser = false;
  708. user.HasGridUserTried = isNPC;
  709. m_UserCache.Add(uuid, user);
  710. }
  711. }
  712. }
  713. public virtual void AddUser(UUID uuid, string first, string last, string homeURL)
  714. {
  715. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, first {1}, last {2}, url {3}", uuid, first, last, homeURL);
  716. UserData oldUser;
  717. lock (m_UserCache)
  718. {
  719. if (m_UserCache.TryGetValue(uuid, out oldUser))
  720. {
  721. if (!oldUser.IsUnknownUser)
  722. {
  723. if (homeURL != oldUser.HomeURL && m_DisplayChangingHomeURI)
  724. {
  725. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Different HomeURI for {0} {1} ({2}): {3} and {4}",
  726. first, last, uuid.ToString(), homeURL, oldUser.HomeURL);
  727. }
  728. /* no update needed */
  729. return;
  730. }
  731. }
  732. else if(!m_UserCache.ContainsKey(uuid))
  733. {
  734. oldUser = new UserData();
  735. oldUser.HasGridUserTried = false;
  736. oldUser.IsUnknownUser = false;
  737. if (homeURL != string.Empty)
  738. {
  739. oldUser.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  740. try
  741. {
  742. oldUser.LastName = "@" + new Uri(homeURL).Authority;
  743. oldUser.IsUnknownUser = false;
  744. }
  745. catch
  746. {
  747. oldUser.LastName = "@unknown";
  748. }
  749. }
  750. else
  751. {
  752. oldUser.FirstName = first;
  753. oldUser.LastName = last;
  754. }
  755. oldUser.HomeURL = homeURL;
  756. oldUser.Id = uuid;
  757. m_UserCache.Add(uuid, oldUser);
  758. }
  759. }
  760. }
  761. public virtual void AddUser(UUID id, string creatorData)
  762. {
  763. // m_log.InfoFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, creatorData {1}", id, creatorData);
  764. if(string.IsNullOrEmpty(creatorData))
  765. {
  766. AddUser(id, string.Empty, string.Empty, string.Empty);
  767. }
  768. else
  769. {
  770. string homeURL;
  771. string firstname = string.Empty;
  772. string lastname = string.Empty;
  773. //creatorData = <endpoint>;<name>
  774. string[] parts = creatorData.Split(';');
  775. if(parts.Length > 1)
  776. {
  777. string[] nameparts = parts[1].Split(' ');
  778. firstname = nameparts[0];
  779. for(int xi = 1; xi < nameparts.Length; ++xi)
  780. {
  781. if(xi != 1)
  782. {
  783. lastname += " ";
  784. }
  785. lastname += nameparts[xi];
  786. }
  787. }
  788. else
  789. {
  790. firstname = "Unknown";
  791. lastname = "UserUMMAU5";
  792. }
  793. if (parts.Length >= 1)
  794. {
  795. homeURL = parts[0];
  796. if(Uri.IsWellFormedUriString(homeURL, UriKind.Absolute))
  797. {
  798. AddUser(id, firstname, lastname, homeURL);
  799. }
  800. else
  801. {
  802. m_log.DebugFormat("[SCENE]: Unable to parse Uri {0} for CreatorID {1}", parts[0], creatorData);
  803. lock (m_UserCache)
  804. {
  805. if(!m_UserCache.ContainsKey(id))
  806. {
  807. UserData newUser = new UserData();
  808. newUser.Id = id;
  809. newUser.FirstName = firstname + "." + lastname.Replace(' ', '.');
  810. newUser.LastName = "@unknown";
  811. newUser.HomeURL = string.Empty;
  812. newUser.HasGridUserTried = false;
  813. newUser.IsUnknownUser = true; /* we mark those users as Unknown user so a re-retrieve may be activated */
  814. m_UserCache.Add(id, newUser);
  815. }
  816. }
  817. }
  818. }
  819. else
  820. {
  821. lock(m_UserCache)
  822. {
  823. if(!m_UserCache.ContainsKey(id))
  824. {
  825. UserData newUser = new UserData();
  826. newUser.Id = id;
  827. newUser.FirstName = "Unknown";
  828. newUser.LastName = "UserUMMAU4";
  829. newUser.HomeURL = string.Empty;
  830. newUser.IsUnknownUser = true;
  831. newUser.HasGridUserTried = false;
  832. m_UserCache.Add(id, newUser);
  833. }
  834. }
  835. }
  836. }
  837. }
  838. public bool RemoveUser(UUID uuid)
  839. {
  840. lock (m_UserCache)
  841. {
  842. return m_UserCache.Remove(uuid);
  843. }
  844. }
  845. #endregion
  846. public virtual bool IsLocalGridUser(UUID uuid)
  847. {
  848. lock (m_Scenes)
  849. {
  850. if (m_Scenes.Count == 0)
  851. return true;
  852. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, uuid);
  853. if (account == null || (account != null && !account.LocalToGrid))
  854. return false;
  855. }
  856. return true;
  857. }
  858. #endregion IUserManagement
  859. protected virtual void Init()
  860. {
  861. AddUser(UUID.Zero, "Unknown", "User");
  862. RegisterConsoleCmds();
  863. }
  864. protected virtual void RegisterConsoleCmds()
  865. {
  866. MainConsole.Instance.Commands.AddCommand("Users", true,
  867. "show name",
  868. "show name <uuid>",
  869. "Show the bindings between a single user UUID and a user name",
  870. String.Empty,
  871. HandleShowUser);
  872. MainConsole.Instance.Commands.AddCommand("Users", true,
  873. "show names",
  874. "show names",
  875. "Show the bindings between user UUIDs and user names",
  876. String.Empty,
  877. HandleShowUsers);
  878. MainConsole.Instance.Commands.AddCommand("Users", true,
  879. "reset user cache",
  880. "reset user cache",
  881. "reset user cache to allow changed settings to be applied",
  882. String.Empty,
  883. HandleResetUserCache);
  884. }
  885. protected virtual void HandleResetUserCache(string module, string[] cmd)
  886. {
  887. lock(m_UserCache)
  888. {
  889. m_UserCache.Clear();
  890. }
  891. }
  892. protected virtual void HandleShowUser(string module, string[] cmd)
  893. {
  894. if (cmd.Length < 3)
  895. {
  896. MainConsole.Instance.Output("Usage: show name <uuid>");
  897. return;
  898. }
  899. UUID userId;
  900. if (!ConsoleUtil.TryParseConsoleUuid(MainConsole.Instance, cmd[2], out userId))
  901. return;
  902. UserData ud;
  903. if(!GetUser(userId, out ud))
  904. {
  905. MainConsole.Instance.Output("No name known for user with id {0}", null, userId);
  906. return;
  907. }
  908. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  909. cdt.AddColumn("UUID", 36);
  910. cdt.AddColumn("Name", 30);
  911. cdt.AddColumn("HomeURL", 40);
  912. cdt.AddRow(userId, string.Format("{0} {1}", ud.FirstName, ud.LastName), ud.HomeURL);
  913. MainConsole.Instance.Output(cdt.ToString());
  914. }
  915. protected virtual void HandleShowUsers(string module, string[] cmd)
  916. {
  917. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  918. cdt.AddColumn("UUID", 36);
  919. cdt.AddColumn("Name", 30);
  920. cdt.AddColumn("HomeURL", 40);
  921. cdt.AddColumn("Checked", 10);
  922. Dictionary<UUID, UserData> copyDict;
  923. lock(m_UserCache)
  924. {
  925. copyDict = new Dictionary<UUID, UserData>(m_UserCache);
  926. }
  927. foreach(KeyValuePair<UUID, UserData> kvp in copyDict)
  928. {
  929. cdt.AddRow(kvp.Key, string.Format("{0} {1}", kvp.Value.FirstName, kvp.Value.LastName), kvp.Value.HomeURL, kvp.Value.HasGridUserTried ? "yes" : "no");
  930. }
  931. MainConsole.Instance.Output(cdt.ToString());
  932. }
  933. }
  934. }