UserAgentService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.Hypergrid;
  33. using OpenSim.Services.Interfaces;
  34. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  35. using OpenSim.Server.Base;
  36. using OpenMetaverse;
  37. using log4net;
  38. using Nini.Config;
  39. namespace OpenSim.Services.HypergridService
  40. {
  41. /// <summary>
  42. /// This service is for HG1.5 only, to make up for the fact that clients don't
  43. /// keep any private information in themselves, and that their 'home service'
  44. /// needs to do it for them.
  45. /// Once we have better clients, this shouldn't be needed.
  46. /// </summary>
  47. public class UserAgentService : IUserAgentService
  48. {
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(
  51. MethodBase.GetCurrentMethod().DeclaringType);
  52. // This will need to go into a DB table
  53. static Dictionary<UUID, TravelingAgentInfo> m_TravelingAgents = new Dictionary<UUID, TravelingAgentInfo>();
  54. static bool m_Initialized = false;
  55. protected static IGridUserService m_GridUserService;
  56. protected static IGridService m_GridService;
  57. protected static GatekeeperServiceConnector m_GatekeeperConnector;
  58. protected static IGatekeeperService m_GatekeeperService;
  59. protected static string m_GridName;
  60. protected static bool m_BypassClientVerification;
  61. public UserAgentService(IConfigSource config)
  62. {
  63. if (!m_Initialized)
  64. {
  65. m_Initialized = true;
  66. m_log.DebugFormat("[HOME USERS SECURITY]: Starting...");
  67. IConfig serverConfig = config.Configs["UserAgentService"];
  68. if (serverConfig == null)
  69. throw new Exception(String.Format("No section UserAgentService in config file"));
  70. string gridService = serverConfig.GetString("GridService", String.Empty);
  71. string gridUserService = serverConfig.GetString("GridUserService", String.Empty);
  72. string gatekeeperService = serverConfig.GetString("GatekeeperService", String.Empty);
  73. m_BypassClientVerification = serverConfig.GetBoolean("BypassClientVerification", false);
  74. if (gridService == string.Empty || gridUserService == string.Empty || gatekeeperService == string.Empty)
  75. throw new Exception(String.Format("Incomplete specifications, UserAgent Service cannot function."));
  76. Object[] args = new Object[] { config };
  77. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  78. m_GridUserService = ServerUtils.LoadPlugin<IGridUserService>(gridUserService, args);
  79. m_GatekeeperConnector = new GatekeeperServiceConnector();
  80. m_GatekeeperService = ServerUtils.LoadPlugin<IGatekeeperService>(gatekeeperService, args);
  81. m_GridName = serverConfig.GetString("ExternalName", string.Empty);
  82. if (m_GridName == string.Empty)
  83. {
  84. serverConfig = config.Configs["GatekeeperService"];
  85. m_GridName = serverConfig.GetString("ExternalName", string.Empty);
  86. }
  87. if (!m_GridName.EndsWith("/"))
  88. m_GridName = m_GridName + "/";
  89. }
  90. }
  91. public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt)
  92. {
  93. position = new Vector3(128, 128, 0); lookAt = Vector3.UnitY;
  94. m_log.DebugFormat("[USER AGENT SERVICE]: Request to get home region of user {0}", userID);
  95. GridRegion home = null;
  96. GridUserInfo uinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  97. if (uinfo != null)
  98. {
  99. if (uinfo.HomeRegionID != UUID.Zero)
  100. {
  101. home = m_GridService.GetRegionByUUID(UUID.Zero, uinfo.HomeRegionID);
  102. position = uinfo.HomePosition;
  103. lookAt = uinfo.HomeLookAt;
  104. }
  105. if (home == null)
  106. {
  107. List<GridRegion> defs = m_GridService.GetDefaultRegions(UUID.Zero);
  108. if (defs != null && defs.Count > 0)
  109. home = defs[0];
  110. }
  111. }
  112. return home;
  113. }
  114. public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, IPEndPoint clientIP, out string reason)
  115. {
  116. m_log.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} {1} (@{2}) to grid {3}",
  117. agentCircuit.firstname, agentCircuit.lastname, ((clientIP == null) ? "stored IP" : clientIP.Address.ToString()), gatekeeper.ServerURI);
  118. // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination
  119. GridRegion region = new GridRegion(gatekeeper);
  120. region.ServerURI = gatekeeper.ServerURI;
  121. region.ExternalHostName = finalDestination.ExternalHostName;
  122. region.InternalEndPoint = finalDestination.InternalEndPoint;
  123. region.RegionName = finalDestination.RegionName;
  124. region.RegionID = finalDestination.RegionID;
  125. region.RegionLocX = finalDestination.RegionLocX;
  126. region.RegionLocY = finalDestination.RegionLocY;
  127. // Generate a new service session
  128. agentCircuit.ServiceSessionID = region.ServerURI + ";" + UUID.Random();
  129. TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region);
  130. bool success = false;
  131. string myExternalIP = string.Empty;
  132. string gridName = gatekeeper.ServerURI;
  133. m_log.DebugFormat("[USER AGENT SERVICE]: this grid: {0}, desired grid: {1}", m_GridName, gridName);
  134. if (m_GridName == gridName)
  135. success = m_GatekeeperService.LoginAgent(agentCircuit, finalDestination, out reason);
  136. else
  137. success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out myExternalIP, out reason);
  138. if (!success)
  139. {
  140. m_log.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} {1} to grid {2}, reason: {3}",
  141. agentCircuit.firstname, agentCircuit.lastname, region.ServerURI, reason);
  142. // restore the old travel info
  143. lock (m_TravelingAgents)
  144. m_TravelingAgents[agentCircuit.SessionID] = old;
  145. return false;
  146. }
  147. m_log.DebugFormat("[USER AGENT SERVICE]: Gatekeeper sees me as {0}", myExternalIP);
  148. // else set the IP addresses associated with this client
  149. if (clientIP != null)
  150. m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress = clientIP.Address.ToString();
  151. m_TravelingAgents[agentCircuit.SessionID].MyIpAddress = myExternalIP;
  152. return true;
  153. }
  154. public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, out string reason)
  155. {
  156. reason = string.Empty;
  157. return LoginAgentToGrid(agentCircuit, gatekeeper, finalDestination, null, out reason);
  158. }
  159. private void SetClientIP(UUID sessionID, string ip)
  160. {
  161. if (m_TravelingAgents.ContainsKey(sessionID))
  162. {
  163. m_log.DebugFormat("[USER AGENT SERVICE]: Setting IP {0} for session {1}", ip, sessionID);
  164. m_TravelingAgents[sessionID].ClientIPAddress = ip;
  165. }
  166. }
  167. TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region)
  168. {
  169. TravelingAgentInfo travel = new TravelingAgentInfo();
  170. TravelingAgentInfo old = null;
  171. lock (m_TravelingAgents)
  172. {
  173. if (m_TravelingAgents.ContainsKey(agentCircuit.SessionID))
  174. {
  175. // Very important! Override whatever this agent comes with.
  176. // UserAgentService always sets the IP for every new agent
  177. // with the original IP address.
  178. agentCircuit.IPAddress = m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress;
  179. old = m_TravelingAgents[agentCircuit.SessionID];
  180. }
  181. m_TravelingAgents[agentCircuit.SessionID] = travel;
  182. }
  183. travel.UserID = agentCircuit.AgentID;
  184. travel.GridExternalName = region.ServerURI;
  185. travel.ServiceToken = agentCircuit.ServiceSessionID;
  186. if (old != null)
  187. travel.ClientIPAddress = old.ClientIPAddress;
  188. return old;
  189. }
  190. public void LogoutAgent(UUID userID, UUID sessionID)
  191. {
  192. m_log.DebugFormat("[USER AGENT SERVICE]: User {0} logged out", userID);
  193. lock (m_TravelingAgents)
  194. {
  195. List<UUID> travels = new List<UUID>();
  196. foreach (KeyValuePair<UUID, TravelingAgentInfo> kvp in m_TravelingAgents)
  197. if (kvp.Value == null) // do some clean up
  198. travels.Add(kvp.Key);
  199. else if (kvp.Value.UserID == userID)
  200. travels.Add(kvp.Key);
  201. foreach (UUID session in travels)
  202. m_TravelingAgents.Remove(session);
  203. }
  204. GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  205. if (guinfo != null)
  206. m_GridUserService.LoggedOut(userID.ToString(), sessionID, guinfo.LastRegionID, guinfo.LastPosition, guinfo.LastLookAt);
  207. }
  208. // We need to prevent foreign users with the same UUID as a local user
  209. public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName)
  210. {
  211. if (!m_TravelingAgents.ContainsKey(sessionID))
  212. return false;
  213. TravelingAgentInfo travel = m_TravelingAgents[sessionID];
  214. return travel.GridExternalName.ToLower() == thisGridExternalName.ToLower();
  215. }
  216. public bool VerifyClient(UUID sessionID, string reportedIP)
  217. {
  218. if (m_BypassClientVerification)
  219. return true;
  220. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying Client session {0} with reported IP {1}.",
  221. sessionID, reportedIP);
  222. if (m_TravelingAgents.ContainsKey(sessionID))
  223. {
  224. bool result = m_TravelingAgents[sessionID].ClientIPAddress == reportedIP ||
  225. m_TravelingAgents[sessionID].MyIpAddress == reportedIP; // NATed
  226. m_log.DebugFormat("[USER AGENT SERVICE]: Comparing {0} with login IP {1} and MyIP {1}; result is {3}",
  227. reportedIP, m_TravelingAgents[sessionID].ClientIPAddress, m_TravelingAgents[sessionID].MyIpAddress, result);
  228. return result;
  229. }
  230. return false;
  231. }
  232. public bool VerifyAgent(UUID sessionID, string token)
  233. {
  234. if (m_TravelingAgents.ContainsKey(sessionID))
  235. {
  236. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying agent token {0} against {1}", token, m_TravelingAgents[sessionID].ServiceToken);
  237. return m_TravelingAgents[sessionID].ServiceToken == token;
  238. }
  239. m_log.DebugFormat("[USER AGENT SERVICE]: Token verification for session {0}: no such session", sessionID);
  240. return false;
  241. }
  242. }
  243. class TravelingAgentInfo
  244. {
  245. public UUID UserID;
  246. public string GridExternalName = string.Empty;
  247. public string ServiceToken = string.Empty;
  248. public string ClientIPAddress = string.Empty; // as seen from this user agent service
  249. public string MyIpAddress = string.Empty; // the user agent service's external IP, as seen from the next gatekeeper
  250. }
  251. }