UserAgentService.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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.Net;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenSim.Services.Connectors.Friends;
  33. using OpenSim.Services.Connectors.Hypergrid;
  34. using OpenSim.Services.Interfaces;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using OpenSim.Server.Base;
  37. using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  38. using OpenMetaverse;
  39. using log4net;
  40. using Nini.Config;
  41. namespace OpenSim.Services.HypergridService
  42. {
  43. /// <summary>
  44. /// This service is for HG1.5 only, to make up for the fact that clients don't
  45. /// keep any private information in themselves, and that their 'home service'
  46. /// needs to do it for them.
  47. /// Once we have better clients, this shouldn't be needed.
  48. /// </summary>
  49. public class UserAgentService : IUserAgentService
  50. {
  51. private static readonly ILog m_log =
  52. LogManager.GetLogger(
  53. MethodBase.GetCurrentMethod().DeclaringType);
  54. // This will need to go into a DB table
  55. static Dictionary<UUID, TravelingAgentInfo> m_TravelingAgents = new Dictionary<UUID, TravelingAgentInfo>();
  56. static bool m_Initialized = false;
  57. protected static IGridUserService m_GridUserService;
  58. protected static IGridService m_GridService;
  59. protected static GatekeeperServiceConnector m_GatekeeperConnector;
  60. protected static IGatekeeperService m_GatekeeperService;
  61. protected static IFriendsService m_FriendsService;
  62. protected static IPresenceService m_PresenceService;
  63. protected static IUserAccountService m_UserAccountService;
  64. protected static IFriendsSimConnector m_FriendsLocalSimConnector; // standalone, points to HGFriendsModule
  65. protected static FriendsSimConnector m_FriendsSimConnector; // grid
  66. protected static string m_GridName;
  67. protected static int m_LevelOutsideContacts;
  68. protected static bool m_BypassClientVerification;
  69. private static Dictionary<int, bool> m_ForeignTripsAllowed = new Dictionary<int, bool>();
  70. private static Dictionary<int, List<string>> m_TripsAllowedExceptions = new Dictionary<int, List<string>>();
  71. private static Dictionary<int, List<string>> m_TripsDisallowedExceptions = new Dictionary<int, List<string>>();
  72. public UserAgentService(IConfigSource config) : this(config, null)
  73. {
  74. }
  75. public UserAgentService(IConfigSource config, IFriendsSimConnector friendsConnector)
  76. {
  77. // Let's set this always, because we don't know the sequence
  78. // of instantiations
  79. if (friendsConnector != null)
  80. m_FriendsLocalSimConnector = friendsConnector;
  81. if (!m_Initialized)
  82. {
  83. m_Initialized = true;
  84. m_log.DebugFormat("[HOME USERS SECURITY]: Starting...");
  85. m_FriendsSimConnector = new FriendsSimConnector();
  86. IConfig serverConfig = config.Configs["UserAgentService"];
  87. if (serverConfig == null)
  88. throw new Exception(String.Format("No section UserAgentService in config file"));
  89. string gridService = serverConfig.GetString("GridService", String.Empty);
  90. string gridUserService = serverConfig.GetString("GridUserService", String.Empty);
  91. string gatekeeperService = serverConfig.GetString("GatekeeperService", String.Empty);
  92. string friendsService = serverConfig.GetString("FriendsService", String.Empty);
  93. string presenceService = serverConfig.GetString("PresenceService", String.Empty);
  94. string userAccountService = serverConfig.GetString("UserAccountService", String.Empty);
  95. m_BypassClientVerification = serverConfig.GetBoolean("BypassClientVerification", false);
  96. if (gridService == string.Empty || gridUserService == string.Empty || gatekeeperService == string.Empty)
  97. throw new Exception(String.Format("Incomplete specifications, UserAgent Service cannot function."));
  98. Object[] args = new Object[] { config };
  99. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  100. m_GridUserService = ServerUtils.LoadPlugin<IGridUserService>(gridUserService, args);
  101. m_GatekeeperConnector = new GatekeeperServiceConnector();
  102. m_GatekeeperService = ServerUtils.LoadPlugin<IGatekeeperService>(gatekeeperService, args);
  103. m_FriendsService = ServerUtils.LoadPlugin<IFriendsService>(friendsService, args);
  104. m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args);
  105. m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(userAccountService, args);
  106. m_LevelOutsideContacts = serverConfig.GetInt("LevelOutsideContacts", 0);
  107. LoadTripPermissionsFromConfig(serverConfig, "ForeignTripsAllowed");
  108. LoadDomainExceptionsFromConfig(serverConfig, "AllowExcept", m_TripsAllowedExceptions);
  109. LoadDomainExceptionsFromConfig(serverConfig, "DisallowExcept", m_TripsDisallowedExceptions);
  110. m_GridName = serverConfig.GetString("ExternalName", string.Empty);
  111. if (m_GridName == string.Empty)
  112. {
  113. serverConfig = config.Configs["GatekeeperService"];
  114. m_GridName = serverConfig.GetString("ExternalName", string.Empty);
  115. }
  116. if (!m_GridName.EndsWith("/"))
  117. m_GridName = m_GridName + "/";
  118. }
  119. }
  120. protected void LoadTripPermissionsFromConfig(IConfig config, string variable)
  121. {
  122. foreach (string keyName in config.GetKeys())
  123. {
  124. if (keyName.StartsWith(variable + "_Level_"))
  125. {
  126. int level = 0;
  127. if (Int32.TryParse(keyName.Replace(variable + "_Level_", ""), out level))
  128. m_ForeignTripsAllowed.Add(level, config.GetBoolean(keyName, true));
  129. }
  130. }
  131. }
  132. protected void LoadDomainExceptionsFromConfig(IConfig config, string variable, Dictionary<int, List<string>> exceptions)
  133. {
  134. foreach (string keyName in config.GetKeys())
  135. {
  136. if (keyName.StartsWith(variable + "_Level_"))
  137. {
  138. int level = 0;
  139. if (Int32.TryParse(keyName.Replace(variable + "_Level_", ""), out level) && !exceptions.ContainsKey(level))
  140. {
  141. exceptions.Add(level, new List<string>());
  142. string value = config.GetString(keyName, string.Empty);
  143. string[] parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  144. foreach (string s in parts)
  145. exceptions[level].Add(s.Trim());
  146. }
  147. }
  148. }
  149. }
  150. public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt)
  151. {
  152. position = new Vector3(128, 128, 0); lookAt = Vector3.UnitY;
  153. m_log.DebugFormat("[USER AGENT SERVICE]: Request to get home region of user {0}", userID);
  154. GridRegion home = null;
  155. GridUserInfo uinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  156. if (uinfo != null)
  157. {
  158. if (uinfo.HomeRegionID != UUID.Zero)
  159. {
  160. home = m_GridService.GetRegionByUUID(UUID.Zero, uinfo.HomeRegionID);
  161. position = uinfo.HomePosition;
  162. lookAt = uinfo.HomeLookAt;
  163. }
  164. if (home == null)
  165. {
  166. List<GridRegion> defs = m_GridService.GetDefaultRegions(UUID.Zero);
  167. if (defs != null && defs.Count > 0)
  168. home = defs[0];
  169. }
  170. }
  171. return home;
  172. }
  173. public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, IPEndPoint clientIP, out string reason)
  174. {
  175. m_log.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} {1} (@{2}) to grid {3}",
  176. agentCircuit.firstname, agentCircuit.lastname, ((clientIP == null) ? "stored IP" : clientIP.Address.ToString()), gatekeeper.ServerURI);
  177. string gridName = gatekeeper.ServerURI;
  178. UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero, agentCircuit.AgentID);
  179. if (account == null)
  180. {
  181. m_log.WarnFormat("[USER AGENT SERVICE]: Someone attempted to lauch a foreign user from here {0} {1}", agentCircuit.firstname, agentCircuit.lastname);
  182. reason = "Forbidden to launch your agents from here";
  183. return false;
  184. }
  185. // Is this user allowed to go there?
  186. if (m_GridName != gridName)
  187. {
  188. if (m_ForeignTripsAllowed.ContainsKey(account.UserLevel))
  189. {
  190. bool allowed = m_ForeignTripsAllowed[account.UserLevel];
  191. if (m_ForeignTripsAllowed[account.UserLevel] && IsException(gridName, account.UserLevel, m_TripsAllowedExceptions))
  192. allowed = false;
  193. if (!m_ForeignTripsAllowed[account.UserLevel] && IsException(gridName, account.UserLevel, m_TripsDisallowedExceptions))
  194. allowed = true;
  195. if (!allowed)
  196. {
  197. reason = "Your world does not allow you to visit the destination";
  198. m_log.InfoFormat("[USER AGENT SERVICE]: Agents not permitted to visit {0}. Refusing service.", gridName);
  199. return false;
  200. }
  201. }
  202. }
  203. // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination
  204. GridRegion region = new GridRegion(gatekeeper);
  205. region.ServerURI = gatekeeper.ServerURI;
  206. region.ExternalHostName = finalDestination.ExternalHostName;
  207. region.InternalEndPoint = finalDestination.InternalEndPoint;
  208. region.RegionName = finalDestination.RegionName;
  209. region.RegionID = finalDestination.RegionID;
  210. region.RegionLocX = finalDestination.RegionLocX;
  211. region.RegionLocY = finalDestination.RegionLocY;
  212. // Generate a new service session
  213. agentCircuit.ServiceSessionID = region.ServerURI + ";" + UUID.Random();
  214. TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region);
  215. bool success = false;
  216. string myExternalIP = string.Empty;
  217. m_log.DebugFormat("[USER AGENT SERVICE]: this grid: {0}, desired grid: {1}", m_GridName, gridName);
  218. if (m_GridName == gridName)
  219. success = m_GatekeeperService.LoginAgent(agentCircuit, finalDestination, out reason);
  220. else
  221. {
  222. success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out myExternalIP, out reason);
  223. if (success)
  224. // Report them as nowhere
  225. m_PresenceService.ReportAgent(agentCircuit.SessionID, UUID.Zero);
  226. }
  227. if (!success)
  228. {
  229. m_log.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} {1} to grid {2}, reason: {3}",
  230. agentCircuit.firstname, agentCircuit.lastname, region.ServerURI, reason);
  231. // restore the old travel info
  232. lock (m_TravelingAgents)
  233. {
  234. if (old == null)
  235. m_TravelingAgents.Remove(agentCircuit.SessionID);
  236. else
  237. m_TravelingAgents[agentCircuit.SessionID] = old;
  238. }
  239. return false;
  240. }
  241. m_log.DebugFormat("[USER AGENT SERVICE]: Gatekeeper sees me as {0}", myExternalIP);
  242. // else set the IP addresses associated with this client
  243. if (clientIP != null)
  244. m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress = clientIP.Address.ToString();
  245. m_TravelingAgents[agentCircuit.SessionID].MyIpAddress = myExternalIP;
  246. return true;
  247. }
  248. public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, out string reason)
  249. {
  250. reason = string.Empty;
  251. return LoginAgentToGrid(agentCircuit, gatekeeper, finalDestination, null, out reason);
  252. }
  253. private void SetClientIP(UUID sessionID, string ip)
  254. {
  255. if (m_TravelingAgents.ContainsKey(sessionID))
  256. {
  257. m_log.DebugFormat("[USER AGENT SERVICE]: Setting IP {0} for session {1}", ip, sessionID);
  258. m_TravelingAgents[sessionID].ClientIPAddress = ip;
  259. }
  260. }
  261. TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region)
  262. {
  263. TravelingAgentInfo travel = new TravelingAgentInfo();
  264. TravelingAgentInfo old = null;
  265. lock (m_TravelingAgents)
  266. {
  267. if (m_TravelingAgents.ContainsKey(agentCircuit.SessionID))
  268. {
  269. // Very important! Override whatever this agent comes with.
  270. // UserAgentService always sets the IP for every new agent
  271. // with the original IP address.
  272. agentCircuit.IPAddress = m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress;
  273. old = m_TravelingAgents[agentCircuit.SessionID];
  274. }
  275. m_TravelingAgents[agentCircuit.SessionID] = travel;
  276. }
  277. travel.UserID = agentCircuit.AgentID;
  278. travel.GridExternalName = region.ServerURI;
  279. travel.ServiceToken = agentCircuit.ServiceSessionID;
  280. if (old != null)
  281. travel.ClientIPAddress = old.ClientIPAddress;
  282. return old;
  283. }
  284. public void LogoutAgent(UUID userID, UUID sessionID)
  285. {
  286. m_log.DebugFormat("[USER AGENT SERVICE]: User {0} logged out", userID);
  287. lock (m_TravelingAgents)
  288. {
  289. List<UUID> travels = new List<UUID>();
  290. foreach (KeyValuePair<UUID, TravelingAgentInfo> kvp in m_TravelingAgents)
  291. if (kvp.Value == null) // do some clean up
  292. travels.Add(kvp.Key);
  293. else if (kvp.Value.UserID == userID)
  294. travels.Add(kvp.Key);
  295. foreach (UUID session in travels)
  296. m_TravelingAgents.Remove(session);
  297. }
  298. GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  299. if (guinfo != null)
  300. m_GridUserService.LoggedOut(userID.ToString(), sessionID, guinfo.LastRegionID, guinfo.LastPosition, guinfo.LastLookAt);
  301. }
  302. // We need to prevent foreign users with the same UUID as a local user
  303. public bool IsAgentComingHome(UUID sessionID, string thisGridExternalName)
  304. {
  305. if (!m_TravelingAgents.ContainsKey(sessionID))
  306. return false;
  307. TravelingAgentInfo travel = m_TravelingAgents[sessionID];
  308. return travel.GridExternalName.ToLower() == thisGridExternalName.ToLower();
  309. }
  310. public bool VerifyClient(UUID sessionID, string reportedIP)
  311. {
  312. if (m_BypassClientVerification)
  313. return true;
  314. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying Client session {0} with reported IP {1}.",
  315. sessionID, reportedIP);
  316. if (m_TravelingAgents.ContainsKey(sessionID))
  317. {
  318. bool result = m_TravelingAgents[sessionID].ClientIPAddress == reportedIP ||
  319. m_TravelingAgents[sessionID].MyIpAddress == reportedIP; // NATed
  320. m_log.DebugFormat("[USER AGENT SERVICE]: Comparing {0} with login IP {1} and MyIP {1}; result is {3}",
  321. reportedIP, m_TravelingAgents[sessionID].ClientIPAddress, m_TravelingAgents[sessionID].MyIpAddress, result);
  322. return result;
  323. }
  324. return false;
  325. }
  326. public bool VerifyAgent(UUID sessionID, string token)
  327. {
  328. if (m_TravelingAgents.ContainsKey(sessionID))
  329. {
  330. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying agent token {0} against {1}", token, m_TravelingAgents[sessionID].ServiceToken);
  331. return m_TravelingAgents[sessionID].ServiceToken == token;
  332. }
  333. m_log.DebugFormat("[USER AGENT SERVICE]: Token verification for session {0}: no such session", sessionID);
  334. return false;
  335. }
  336. [Obsolete]
  337. public List<UUID> StatusNotification(List<string> friends, UUID foreignUserID, bool online)
  338. {
  339. if (m_FriendsService == null || m_PresenceService == null)
  340. {
  341. m_log.WarnFormat("[USER AGENT SERVICE]: Unable to perform status notifications because friends or presence services are missing");
  342. return new List<UUID>();
  343. }
  344. List<UUID> localFriendsOnline = new List<UUID>();
  345. m_log.DebugFormat("[USER AGENT SERVICE]: Status notification: foreign user {0} wants to notify {1} local friends", foreignUserID, friends.Count);
  346. // First, let's double check that the reported friends are, indeed, friends of that user
  347. // And let's check that the secret matches
  348. List<string> usersToBeNotified = new List<string>();
  349. foreach (string uui in friends)
  350. {
  351. UUID localUserID;
  352. string secret = string.Empty, tmp = string.Empty;
  353. if (Util.ParseUniversalUserIdentifier(uui, out localUserID, out tmp, out tmp, out tmp, out secret))
  354. {
  355. FriendInfo[] friendInfos = m_FriendsService.GetFriends(localUserID);
  356. foreach (FriendInfo finfo in friendInfos)
  357. {
  358. if (finfo.Friend.StartsWith(foreignUserID.ToString()) && finfo.Friend.EndsWith(secret))
  359. {
  360. // great!
  361. usersToBeNotified.Add(localUserID.ToString());
  362. }
  363. }
  364. }
  365. }
  366. // Now, let's send the notifications
  367. m_log.DebugFormat("[USER AGENT SERVICE]: Status notification: user has {0} local friends", usersToBeNotified.Count);
  368. // First, let's send notifications to local users who are online in the home grid
  369. PresenceInfo[] friendSessions = m_PresenceService.GetAgents(usersToBeNotified.ToArray());
  370. if (friendSessions != null && friendSessions.Length > 0)
  371. {
  372. PresenceInfo friendSession = null;
  373. foreach (PresenceInfo pinfo in friendSessions)
  374. if (pinfo.RegionID != UUID.Zero) // let's guard against traveling agents
  375. {
  376. friendSession = pinfo;
  377. break;
  378. }
  379. if (friendSession != null)
  380. {
  381. ForwardStatusNotificationToSim(friendSession.RegionID, foreignUserID, friendSession.UserID, online);
  382. usersToBeNotified.Remove(friendSession.UserID.ToString());
  383. UUID id;
  384. if (UUID.TryParse(friendSession.UserID, out id))
  385. localFriendsOnline.Add(id);
  386. }
  387. }
  388. // Lastly, let's notify the rest who may be online somewhere else
  389. foreach (string user in usersToBeNotified)
  390. {
  391. UUID id = new UUID(user);
  392. if (m_TravelingAgents.ContainsKey(id) && m_TravelingAgents[id].GridExternalName != m_GridName)
  393. {
  394. string url = m_TravelingAgents[id].GridExternalName;
  395. // forward
  396. m_log.WarnFormat("[USER AGENT SERVICE]: User {0} is visiting {1}. HG Status notifications still not implemented.", user, url);
  397. }
  398. }
  399. // and finally, let's send the online friends
  400. if (online)
  401. {
  402. return localFriendsOnline;
  403. }
  404. else
  405. return new List<UUID>();
  406. }
  407. [Obsolete]
  408. protected void ForwardStatusNotificationToSim(UUID regionID, UUID foreignUserID, string user, bool online)
  409. {
  410. UUID userID;
  411. if (UUID.TryParse(user, out userID))
  412. {
  413. if (m_FriendsLocalSimConnector != null)
  414. {
  415. m_log.DebugFormat("[USER AGENT SERVICE]: Local Notify, user {0} is {1}", foreignUserID, (online ? "online" : "offline"));
  416. m_FriendsLocalSimConnector.StatusNotify(foreignUserID, userID, online);
  417. }
  418. else
  419. {
  420. GridRegion region = m_GridService.GetRegionByUUID(UUID.Zero /* !!! */, regionID);
  421. if (region != null)
  422. {
  423. m_log.DebugFormat("[USER AGENT SERVICE]: Remote Notify to region {0}, user {1} is {2}", region.RegionName, foreignUserID, (online ? "online" : "offline"));
  424. m_FriendsSimConnector.StatusNotify(region, foreignUserID, userID.ToString(), online);
  425. }
  426. }
  427. }
  428. }
  429. public List<UUID> GetOnlineFriends(UUID foreignUserID, List<string> friends)
  430. {
  431. List<UUID> online = new List<UUID>();
  432. if (m_FriendsService == null || m_PresenceService == null)
  433. {
  434. m_log.WarnFormat("[USER AGENT SERVICE]: Unable to get online friends because friends or presence services are missing");
  435. return online;
  436. }
  437. m_log.DebugFormat("[USER AGENT SERVICE]: Foreign user {0} wants to know status of {1} local friends", foreignUserID, friends.Count);
  438. // First, let's double check that the reported friends are, indeed, friends of that user
  439. // And let's check that the secret matches and the rights
  440. List<string> usersToBeNotified = new List<string>();
  441. foreach (string uui in friends)
  442. {
  443. UUID localUserID;
  444. string secret = string.Empty, tmp = string.Empty;
  445. if (Util.ParseUniversalUserIdentifier(uui, out localUserID, out tmp, out tmp, out tmp, out secret))
  446. {
  447. FriendInfo[] friendInfos = m_FriendsService.GetFriends(localUserID);
  448. foreach (FriendInfo finfo in friendInfos)
  449. {
  450. if (finfo.Friend.StartsWith(foreignUserID.ToString()) && finfo.Friend.EndsWith(secret) &&
  451. (finfo.TheirFlags & (int)FriendRights.CanSeeOnline) != 0 && (finfo.TheirFlags != -1))
  452. {
  453. // great!
  454. usersToBeNotified.Add(localUserID.ToString());
  455. }
  456. }
  457. }
  458. }
  459. // Now, let's find out their status
  460. m_log.DebugFormat("[USER AGENT SERVICE]: GetOnlineFriends: user has {0} local friends with status rights", usersToBeNotified.Count);
  461. // First, let's send notifications to local users who are online in the home grid
  462. PresenceInfo[] friendSessions = m_PresenceService.GetAgents(usersToBeNotified.ToArray());
  463. if (friendSessions != null && friendSessions.Length > 0)
  464. {
  465. foreach (PresenceInfo pi in friendSessions)
  466. {
  467. UUID presenceID;
  468. if (UUID.TryParse(pi.UserID, out presenceID))
  469. online.Add(presenceID);
  470. }
  471. }
  472. return online;
  473. }
  474. public Dictionary<string, object> GetUserInfo(UUID userID)
  475. {
  476. Dictionary<string, object> info = new Dictionary<string, object>();
  477. if (m_UserAccountService == null)
  478. {
  479. m_log.WarnFormat("[USER AGENT SERVICE]: Unable to get user flags because user account service is missing");
  480. info["result"] = "fail";
  481. info["message"] = "UserAccountService is missing!";
  482. return info;
  483. }
  484. UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero /*!!!*/, userID);
  485. if (account != null)
  486. {
  487. info.Add("user_flags", (object)account.UserFlags);
  488. info.Add("user_created", (object)account.Created);
  489. info.Add("user_title", (object)account.UserTitle);
  490. info.Add("result", "success");
  491. }
  492. return info;
  493. }
  494. public Dictionary<string, object> GetServerURLs(UUID userID)
  495. {
  496. if (m_UserAccountService == null)
  497. {
  498. m_log.WarnFormat("[USER AGENT SERVICE]: Unable to get server URLs because user account service is missing");
  499. return new Dictionary<string, object>();
  500. }
  501. UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero /*!!!*/, userID);
  502. if (account != null)
  503. return account.ServiceURLs;
  504. return new Dictionary<string, object>();
  505. }
  506. public string LocateUser(UUID userID)
  507. {
  508. foreach (TravelingAgentInfo t in m_TravelingAgents.Values)
  509. {
  510. if (t == null)
  511. {
  512. m_log.ErrorFormat("[USER AGENT SERVICE]: Oops! Null TravelingAgentInfo. Please report this on mantis");
  513. continue;
  514. }
  515. if (t.UserID == userID && !m_GridName.Equals(t.GridExternalName))
  516. return t.GridExternalName;
  517. }
  518. return string.Empty;
  519. }
  520. public string GetUUI(UUID userID, UUID targetUserID)
  521. {
  522. // Let's see if it's a local user
  523. UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero, targetUserID);
  524. if (account != null)
  525. return targetUserID.ToString() + ";" + m_GridName + ";" + account.FirstName + " " + account.LastName ;
  526. // Let's try the list of friends
  527. FriendInfo[] friends = m_FriendsService.GetFriends(userID);
  528. if (friends != null && friends.Length > 0)
  529. {
  530. foreach (FriendInfo f in friends)
  531. if (f.Friend.StartsWith(targetUserID.ToString()))
  532. {
  533. // Let's remove the secret
  534. UUID id; string tmp = string.Empty, secret = string.Empty;
  535. if (Util.ParseUniversalUserIdentifier(f.Friend, out id, out tmp, out tmp, out tmp, out secret))
  536. return f.Friend.Replace(secret, "0");
  537. }
  538. }
  539. return string.Empty;
  540. }
  541. public UUID GetUUID(String first, String last)
  542. {
  543. // Let's see if it's a local user
  544. UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero, first, last);
  545. if (account != null)
  546. {
  547. // check user level
  548. if (account.UserLevel < m_LevelOutsideContacts)
  549. return UUID.Zero;
  550. else
  551. return account.PrincipalID;
  552. }
  553. else
  554. return UUID.Zero;
  555. }
  556. #region Misc
  557. private bool IsException(string dest, int level, Dictionary<int, List<string>> exceptions)
  558. {
  559. if (!exceptions.ContainsKey(level))
  560. return false;
  561. bool exception = false;
  562. if (exceptions[level].Count > 0) // we have exceptions
  563. {
  564. string destination = dest;
  565. if (!destination.EndsWith("/"))
  566. destination += "/";
  567. if (exceptions[level].Find(delegate(string s)
  568. {
  569. if (!s.EndsWith("/"))
  570. s += "/";
  571. return s == destination;
  572. }) != null)
  573. exception = true;
  574. }
  575. return exception;
  576. }
  577. #endregion
  578. }
  579. class TravelingAgentInfo
  580. {
  581. public UUID UserID;
  582. public string GridExternalName = string.Empty;
  583. public string ServiceToken = string.Empty;
  584. public string ClientIPAddress = string.Empty; // as seen from this user agent service
  585. public string MyIpAddress = string.Empty; // the user agent service's external IP, as seen from the next gatekeeper
  586. }
  587. }