UserAgentService.cs 31 KB

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