UserAgentService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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, out string reason)
  113. {
  114. m_log.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} {1} to grid {2}",
  115. agentCircuit.firstname, agentCircuit.lastname, gatekeeper.ExternalHostName +":"+ gatekeeper.HttpPort);
  116. // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination
  117. GridRegion region = new GridRegion(gatekeeper);
  118. region.RegionName = finalDestination.RegionName;
  119. region.RegionID = finalDestination.RegionID;
  120. region.RegionLocX = finalDestination.RegionLocX;
  121. region.RegionLocY = finalDestination.RegionLocY;
  122. // Generate a new service session
  123. agentCircuit.ServiceSessionID = "http://" + region.ExternalHostName + ":" + region.HttpPort + ";" + UUID.Random();
  124. TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region);
  125. //bool success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out reason);
  126. bool success = false;
  127. string gridName = "http://" + gatekeeper.ExternalHostName + ":" + gatekeeper.HttpPort;
  128. if (m_GridName == gridName)
  129. success = m_GatekeeperService.LoginAgent(agentCircuit, finalDestination, out reason);
  130. else
  131. success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out reason);
  132. if (!success)
  133. {
  134. m_log.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} {1} to grid {2}, reason: {3}",
  135. agentCircuit.firstname, agentCircuit.lastname, region.ExternalHostName + ":" + region.HttpPort, reason);
  136. // restore the old travel info
  137. lock (m_TravelingAgents)
  138. m_TravelingAgents[agentCircuit.SessionID] = old;
  139. return false;
  140. }
  141. return true;
  142. }
  143. public void SetClientToken(UUID sessionID, string token)
  144. {
  145. if (m_TravelingAgents.ContainsKey(sessionID))
  146. {
  147. m_log.DebugFormat("[USER AGENT SERVICE]: Setting token {0} for session {1}", token, sessionID);
  148. m_TravelingAgents[sessionID].ClientToken = token;
  149. }
  150. }
  151. TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region)
  152. {
  153. TravelingAgentInfo travel = new TravelingAgentInfo();
  154. TravelingAgentInfo old = null;
  155. lock (m_TravelingAgents)
  156. {
  157. if (m_TravelingAgents.ContainsKey(agentCircuit.SessionID))
  158. {
  159. old = m_TravelingAgents[agentCircuit.SessionID];
  160. }
  161. m_TravelingAgents[agentCircuit.SessionID] = travel;
  162. }
  163. travel.UserID = agentCircuit.AgentID;
  164. travel.GridExternalName = "http://" + region.ExternalHostName + ":" + region.HttpPort;
  165. travel.ServiceToken = agentCircuit.ServiceSessionID;
  166. if (old != null)
  167. travel.ClientToken = old.ClientToken;
  168. return old;
  169. }
  170. public void LogoutAgent(UUID userID, UUID sessionID)
  171. {
  172. m_log.DebugFormat("[USER AGENT SERVICE]: User {0} logged out", userID);
  173. lock (m_TravelingAgents)
  174. {
  175. List<UUID> travels = new List<UUID>();
  176. foreach (KeyValuePair<UUID, TravelingAgentInfo> kvp in m_TravelingAgents)
  177. if (kvp.Value == null) // do some clean up
  178. travels.Add(kvp.Key);
  179. else if (kvp.Value.UserID == userID)
  180. travels.Add(kvp.Key);
  181. foreach (UUID session in travels)
  182. m_TravelingAgents.Remove(session);
  183. }
  184. GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  185. if (guinfo != null)
  186. m_GridUserService.LoggedOut(userID.ToString(), guinfo.LastRegionID, guinfo.LastPosition, guinfo.LastLookAt);
  187. }
  188. // We need to prevent foreign users with the same UUID as a local user
  189. public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName)
  190. {
  191. if (!m_TravelingAgents.ContainsKey(sessionID))
  192. return false;
  193. TravelingAgentInfo travel = m_TravelingAgents[sessionID];
  194. return travel.GridExternalName == thisGridExternalName;
  195. }
  196. public bool VerifyClient(UUID sessionID, string token)
  197. {
  198. if (m_BypassClientVerification)
  199. return true;
  200. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying Client session {0} with token {1}", sessionID, token);
  201. if (m_TravelingAgents.ContainsKey(sessionID))
  202. return m_TravelingAgents[sessionID].ClientToken == token;
  203. return false;
  204. }
  205. public bool VerifyAgent(UUID sessionID, string token)
  206. {
  207. if (m_TravelingAgents.ContainsKey(sessionID))
  208. {
  209. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying agent token {0} against {1}", token, m_TravelingAgents[sessionID].ServiceToken);
  210. return m_TravelingAgents[sessionID].ServiceToken == token;
  211. }
  212. m_log.DebugFormat("[USER AGENT SERVICE]: Token verification for session {0}: no such session", sessionID);
  213. return false;
  214. }
  215. }
  216. class TravelingAgentInfo
  217. {
  218. public UUID UserID;
  219. public string GridExternalName = string.Empty;
  220. public string ServiceToken = string.Empty;
  221. public string ClientToken = string.Empty;
  222. }
  223. }