UserManagementModule.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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, 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)
  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. m_Scenes[0].RegionInfo.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. if(m_Scenes.Count <= 0)
  609. {
  610. userdata = new UserData();
  611. return false;
  612. }
  613. lock (m_UserCache)
  614. {
  615. if (m_UserCache.TryGetValue(uuid, out userdata))
  616. {
  617. if (userdata.HasGridUserTried)
  618. {
  619. return true;
  620. }
  621. }
  622. else
  623. {
  624. userdata = new UserData();
  625. userdata.Id = uuid;
  626. userdata.FirstName = "Unknown";
  627. userdata.LastName = "UserUMMAU42";
  628. userdata.HomeURL = string.Empty;
  629. userdata.IsUnknownUser = true;
  630. userdata.HasGridUserTried = false;
  631. }
  632. }
  633. /* BEGIN: do not wrap this code in any lock here
  634. * There are HTTP calls in here.
  635. */
  636. if (!userdata.HasGridUserTried)
  637. {
  638. /* rewrite here */
  639. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, uuid);
  640. if (account != null)
  641. {
  642. userdata.FirstName = account.FirstName;
  643. userdata.LastName = account.LastName;
  644. userdata.HomeURL = string.Empty;
  645. userdata.IsUnknownUser = false;
  646. userdata.HasGridUserTried = true;
  647. }
  648. }
  649. if (!userdata.HasGridUserTried)
  650. {
  651. GridUserInfo uInfo = null;
  652. if (null != m_Scenes[0].GridUserService)
  653. {
  654. uInfo = m_Scenes[0].GridUserService.GetGridUserInfo(uuid.ToString());
  655. }
  656. if (uInfo != null)
  657. {
  658. string url, first, last, tmp;
  659. UUID u;
  660. if(uInfo.UserID.Length <= 36)
  661. {
  662. /* not a UUI */
  663. }
  664. else if (Util.ParseUniversalUserIdentifier(uInfo.UserID, out u, out url, out first, out last, out tmp))
  665. {
  666. if (url != string.Empty)
  667. {
  668. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  669. userdata.HomeURL = url;
  670. try
  671. {
  672. userdata.LastName = "@" + new Uri(url).Authority;
  673. userdata.IsUnknownUser = false;
  674. }
  675. catch
  676. {
  677. userdata.LastName = "@unknown";
  678. }
  679. userdata.HasGridUserTried = true;
  680. }
  681. }
  682. else
  683. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  684. }
  685. }
  686. /* END: do not wrap this code in any lock here */
  687. lock (m_UserCache)
  688. {
  689. m_UserCache[uuid] = userdata;
  690. }
  691. return !userdata.IsUnknownUser;
  692. }
  693. public virtual void AddUser(UUID uuid, string first, string last, bool isNPC = false)
  694. {
  695. lock (m_UserCache)
  696. {
  697. if (!m_UserCache.ContainsKey(uuid))
  698. {
  699. UserData user = new UserData();
  700. user.Id = uuid;
  701. user.FirstName = first;
  702. user.LastName = last;
  703. user.IsUnknownUser = false;
  704. user.HasGridUserTried = isNPC;
  705. m_UserCache.Add(uuid, user);
  706. }
  707. }
  708. }
  709. public virtual void AddUser(UUID uuid, string first, string last, string homeURL)
  710. {
  711. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, first {1}, last {2}, url {3}", uuid, first, last, homeURL);
  712. UserData oldUser;
  713. lock (m_UserCache)
  714. {
  715. if (m_UserCache.TryGetValue(uuid, out oldUser))
  716. {
  717. if (!oldUser.IsUnknownUser)
  718. {
  719. if (homeURL != oldUser.HomeURL && m_DisplayChangingHomeURI)
  720. {
  721. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Different HomeURI for {0} {1} ({2}): {3} and {4}",
  722. first, last, uuid.ToString(), homeURL, oldUser.HomeURL);
  723. }
  724. /* no update needed */
  725. return;
  726. }
  727. }
  728. else if(!m_UserCache.ContainsKey(uuid))
  729. {
  730. oldUser = new UserData();
  731. oldUser.HasGridUserTried = false;
  732. oldUser.IsUnknownUser = false;
  733. if (homeURL != string.Empty)
  734. {
  735. oldUser.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  736. try
  737. {
  738. oldUser.LastName = "@" + new Uri(homeURL).Authority;
  739. oldUser.IsUnknownUser = false;
  740. }
  741. catch
  742. {
  743. oldUser.LastName = "@unknown";
  744. }
  745. }
  746. else
  747. {
  748. oldUser.FirstName = first;
  749. oldUser.LastName = last;
  750. }
  751. oldUser.HomeURL = homeURL;
  752. oldUser.Id = uuid;
  753. m_UserCache.Add(uuid, oldUser);
  754. }
  755. }
  756. }
  757. public virtual void AddUser(UUID id, string creatorData)
  758. {
  759. // m_log.InfoFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, creatorData {1}", id, creatorData);
  760. if(string.IsNullOrEmpty(creatorData))
  761. {
  762. AddUser(id, string.Empty, string.Empty, string.Empty);
  763. }
  764. else
  765. {
  766. string homeURL;
  767. string firstname = string.Empty;
  768. string lastname = string.Empty;
  769. //creatorData = <endpoint>;<name>
  770. string[] parts = creatorData.Split(';');
  771. if(parts.Length > 1)
  772. {
  773. string[] nameparts = parts[1].Split(' ');
  774. firstname = nameparts[0];
  775. for(int xi = 1; xi < nameparts.Length; ++xi)
  776. {
  777. if(xi != 1)
  778. {
  779. lastname += " ";
  780. }
  781. lastname += nameparts[xi];
  782. }
  783. }
  784. else
  785. {
  786. firstname = "Unknown";
  787. lastname = "UserUMMAU5";
  788. }
  789. if (parts.Length >= 1)
  790. {
  791. homeURL = parts[0];
  792. if(Uri.IsWellFormedUriString(homeURL, UriKind.Absolute))
  793. {
  794. AddUser(id, firstname, lastname, homeURL);
  795. }
  796. else
  797. {
  798. m_log.DebugFormat("[SCENE]: Unable to parse Uri {0} for CreatorID {1}", parts[0], creatorData);
  799. lock (m_UserCache)
  800. {
  801. if(!m_UserCache.ContainsKey(id))
  802. {
  803. UserData newUser = new UserData();
  804. newUser.Id = id;
  805. newUser.FirstName = firstname + "." + lastname.Replace(' ', '.');
  806. newUser.LastName = "@unknown";
  807. newUser.HomeURL = string.Empty;
  808. newUser.HasGridUserTried = false;
  809. newUser.IsUnknownUser = true; /* we mark those users as Unknown user so a re-retrieve may be activated */
  810. m_UserCache.Add(id, newUser);
  811. }
  812. }
  813. }
  814. }
  815. else
  816. {
  817. lock(m_UserCache)
  818. {
  819. if(!m_UserCache.ContainsKey(id))
  820. {
  821. UserData newUser = new UserData();
  822. newUser.Id = id;
  823. newUser.FirstName = "Unknown";
  824. newUser.LastName = "UserUMMAU4";
  825. newUser.HomeURL = string.Empty;
  826. newUser.IsUnknownUser = true;
  827. newUser.HasGridUserTried = false;
  828. m_UserCache.Add(id, newUser);
  829. }
  830. }
  831. }
  832. }
  833. }
  834. public bool RemoveUser(UUID uuid)
  835. {
  836. lock (m_UserCache)
  837. {
  838. return m_UserCache.Remove(uuid);
  839. }
  840. }
  841. #endregion
  842. public virtual bool IsLocalGridUser(UUID uuid)
  843. {
  844. lock (m_Scenes)
  845. {
  846. if (m_Scenes.Count == 0)
  847. return true;
  848. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, uuid);
  849. if (account == null || (account != null && !account.LocalToGrid))
  850. return false;
  851. }
  852. return true;
  853. }
  854. #endregion IUserManagement
  855. protected virtual void Init()
  856. {
  857. AddUser(UUID.Zero, "Unknown", "User");
  858. RegisterConsoleCmds();
  859. }
  860. protected virtual void RegisterConsoleCmds()
  861. {
  862. MainConsole.Instance.Commands.AddCommand("Users", true,
  863. "show name",
  864. "show name <uuid>",
  865. "Show the bindings between a single user UUID and a user name",
  866. String.Empty,
  867. HandleShowUser);
  868. MainConsole.Instance.Commands.AddCommand("Users", true,
  869. "show names",
  870. "show names",
  871. "Show the bindings between user UUIDs and user names",
  872. String.Empty,
  873. HandleShowUsers);
  874. MainConsole.Instance.Commands.AddCommand("Users", true,
  875. "reset user cache",
  876. "reset user cache",
  877. "reset user cache to allow changed settings to be applied",
  878. String.Empty,
  879. HandleResetUserCache);
  880. }
  881. protected virtual void HandleResetUserCache(string module, string[] cmd)
  882. {
  883. lock(m_UserCache)
  884. {
  885. m_UserCache.Clear();
  886. }
  887. }
  888. protected virtual void HandleShowUser(string module, string[] cmd)
  889. {
  890. if (cmd.Length < 3)
  891. {
  892. MainConsole.Instance.OutputFormat("Usage: show name <uuid>");
  893. return;
  894. }
  895. UUID userId;
  896. if (!ConsoleUtil.TryParseConsoleUuid(MainConsole.Instance, cmd[2], out userId))
  897. return;
  898. UserData ud;
  899. if(!GetUser(userId, out ud))
  900. {
  901. MainConsole.Instance.OutputFormat("No name known for user with id {0}", userId);
  902. return;
  903. }
  904. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  905. cdt.AddColumn("UUID", 36);
  906. cdt.AddColumn("Name", 30);
  907. cdt.AddColumn("HomeURL", 40);
  908. cdt.AddRow(userId, string.Format("{0} {1}", ud.FirstName, ud.LastName), ud.HomeURL);
  909. MainConsole.Instance.Output(cdt.ToString());
  910. }
  911. protected virtual void HandleShowUsers(string module, string[] cmd)
  912. {
  913. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  914. cdt.AddColumn("UUID", 36);
  915. cdt.AddColumn("Name", 30);
  916. cdt.AddColumn("HomeURL", 40);
  917. cdt.AddColumn("Checked", 10);
  918. Dictionary<UUID, UserData> copyDict;
  919. lock(m_UserCache)
  920. {
  921. copyDict = new Dictionary<UUID, UserData>(m_UserCache);
  922. }
  923. foreach(KeyValuePair<UUID, UserData> kvp in copyDict)
  924. {
  925. cdt.AddRow(kvp.Key, string.Format("{0} {1}", kvp.Value.FirstName, kvp.Value.LastName), kvp.Value.HomeURL, kvp.Value.HasGridUserTried ? "yes" : "no");
  926. }
  927. MainConsole.Instance.Output(cdt.ToString());
  928. }
  929. }
  930. }