UserAgentService.cs 32 KB

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