UserAgentService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. }
  88. }
  89. public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt)
  90. {
  91. position = new Vector3(128, 128, 0); lookAt = Vector3.UnitY;
  92. m_log.DebugFormat("[USER AGENT SERVICE]: Request to get home region of user {0}", userID);
  93. GridRegion home = null;
  94. GridUserInfo uinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  95. if (uinfo != null)
  96. {
  97. if (uinfo.HomeRegionID != UUID.Zero)
  98. {
  99. home = m_GridService.GetRegionByUUID(UUID.Zero, uinfo.HomeRegionID);
  100. position = uinfo.HomePosition;
  101. lookAt = uinfo.HomeLookAt;
  102. }
  103. if (home == null)
  104. {
  105. List<GridRegion> defs = m_GridService.GetDefaultRegions(UUID.Zero);
  106. if (defs != null && defs.Count > 0)
  107. home = defs[0];
  108. }
  109. }
  110. return home;
  111. }
  112. public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, IPEndPoint clientIP, out string reason)
  113. {
  114. m_log.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} {1} (@{2}) to grid {3}",
  115. agentCircuit.firstname, agentCircuit.lastname, ((clientIP == null) ? "stored IP" : clientIP.Address.ToString()),
  116. gatekeeper.ExternalHostName +":"+ gatekeeper.HttpPort);
  117. // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination
  118. GridRegion region = new GridRegion(gatekeeper);
  119. region.RegionName = finalDestination.RegionName;
  120. region.RegionID = finalDestination.RegionID;
  121. region.RegionLocX = finalDestination.RegionLocX;
  122. region.RegionLocY = finalDestination.RegionLocY;
  123. // Generate a new service session
  124. agentCircuit.ServiceSessionID = "http://" + region.ExternalHostName + ":" + region.HttpPort + ";" + UUID.Random();
  125. TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region);
  126. //bool success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out reason);
  127. bool success = false;
  128. string myExternalIP = string.Empty;
  129. string gridName = "http://" + gatekeeper.ExternalHostName + ":" + gatekeeper.HttpPort;
  130. if (m_GridName == gridName)
  131. success = m_GatekeeperService.LoginAgent(agentCircuit, finalDestination, out reason);
  132. else
  133. success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out myExternalIP, out reason);
  134. if (!success)
  135. {
  136. m_log.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} {1} to grid {2}, reason: {3}",
  137. agentCircuit.firstname, agentCircuit.lastname, region.ExternalHostName + ":" + region.HttpPort, reason);
  138. // restore the old travel info
  139. lock (m_TravelingAgents)
  140. m_TravelingAgents[agentCircuit.SessionID] = old;
  141. return false;
  142. }
  143. m_log.DebugFormat("[USER AGENT SERVICE]: Gatekeeper sees me as {0}", myExternalIP);
  144. // else set the IP addresses associated with this client
  145. if (clientIP != null)
  146. m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress = clientIP.Address.ToString();
  147. m_TravelingAgents[agentCircuit.SessionID].MyIpAddress = myExternalIP;
  148. return true;
  149. }
  150. public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, out string reason)
  151. {
  152. reason = string.Empty;
  153. return LoginAgentToGrid(agentCircuit, gatekeeper, finalDestination, null, out reason);
  154. }
  155. private void SetClientIP(UUID sessionID, string ip)
  156. {
  157. if (m_TravelingAgents.ContainsKey(sessionID))
  158. {
  159. m_log.DebugFormat("[USER AGENT SERVICE]: Setting IP {0} for session {1}", ip, sessionID);
  160. m_TravelingAgents[sessionID].ClientIPAddress = ip;
  161. }
  162. }
  163. TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region)
  164. {
  165. TravelingAgentInfo travel = new TravelingAgentInfo();
  166. TravelingAgentInfo old = null;
  167. lock (m_TravelingAgents)
  168. {
  169. if (m_TravelingAgents.ContainsKey(agentCircuit.SessionID))
  170. {
  171. old = m_TravelingAgents[agentCircuit.SessionID];
  172. }
  173. m_TravelingAgents[agentCircuit.SessionID] = travel;
  174. }
  175. travel.UserID = agentCircuit.AgentID;
  176. travel.GridExternalName = "http://" + region.ExternalHostName + ":" + region.HttpPort;
  177. travel.ServiceToken = agentCircuit.ServiceSessionID;
  178. if (old != null)
  179. travel.ClientIPAddress = old.ClientIPAddress;
  180. return old;
  181. }
  182. public void LogoutAgent(UUID userID, UUID sessionID)
  183. {
  184. m_log.DebugFormat("[USER AGENT SERVICE]: User {0} logged out", userID);
  185. lock (m_TravelingAgents)
  186. {
  187. List<UUID> travels = new List<UUID>();
  188. foreach (KeyValuePair<UUID, TravelingAgentInfo> kvp in m_TravelingAgents)
  189. if (kvp.Value == null) // do some clean up
  190. travels.Add(kvp.Key);
  191. else if (kvp.Value.UserID == userID)
  192. travels.Add(kvp.Key);
  193. foreach (UUID session in travels)
  194. m_TravelingAgents.Remove(session);
  195. }
  196. GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  197. if (guinfo != null)
  198. m_GridUserService.LoggedOut(userID.ToString(), guinfo.LastRegionID, guinfo.LastPosition, guinfo.LastLookAt);
  199. }
  200. // We need to prevent foreign users with the same UUID as a local user
  201. public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName)
  202. {
  203. if (!m_TravelingAgents.ContainsKey(sessionID))
  204. return false;
  205. TravelingAgentInfo travel = m_TravelingAgents[sessionID];
  206. return travel.GridExternalName == thisGridExternalName;
  207. }
  208. public bool VerifyClient(UUID sessionID, string reportedIP)
  209. {
  210. if (m_BypassClientVerification)
  211. return true;
  212. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying Client session {0} with reported IP {1}.",
  213. sessionID, reportedIP);
  214. if (m_TravelingAgents.ContainsKey(sessionID))
  215. {
  216. m_log.DebugFormat("[USER AGENT SERVICE]: Comparing with login IP {0} and MyIP {1}",
  217. m_TravelingAgents[sessionID].ClientIPAddress, m_TravelingAgents[sessionID].MyIpAddress);
  218. return m_TravelingAgents[sessionID].ClientIPAddress == reportedIP ||
  219. m_TravelingAgents[sessionID].MyIpAddress == reportedIP; // NATed
  220. }
  221. return false;
  222. }
  223. public bool VerifyAgent(UUID sessionID, string token)
  224. {
  225. if (m_TravelingAgents.ContainsKey(sessionID))
  226. {
  227. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying agent token {0} against {1}", token, m_TravelingAgents[sessionID].ServiceToken);
  228. return m_TravelingAgents[sessionID].ServiceToken == token;
  229. }
  230. m_log.DebugFormat("[USER AGENT SERVICE]: Token verification for session {0}: no such session", sessionID);
  231. return false;
  232. }
  233. }
  234. class TravelingAgentInfo
  235. {
  236. public UUID UserID;
  237. public string GridExternalName = string.Empty;
  238. public string ServiceToken = string.Empty;
  239. public string ClientIPAddress = string.Empty; // as seen from this user agent service
  240. public string MyIpAddress = string.Empty; // the user agent service's external IP, as seen from the next gatekeeper
  241. }
  242. }