UserAgentService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. public UserAgentService(IConfigSource config)
  59. {
  60. if (!m_Initialized)
  61. {
  62. m_log.DebugFormat("[HOME USERS SECURITY]: Starting...");
  63. IConfig serverConfig = config.Configs["UserAgentService"];
  64. if (serverConfig == null)
  65. throw new Exception(String.Format("No section UserAgentService in config file"));
  66. string gridService = serverConfig.GetString("GridService", String.Empty);
  67. string gridUserService = serverConfig.GetString("GridUserService", String.Empty);
  68. if (gridService == string.Empty || gridUserService == string.Empty)
  69. throw new Exception(String.Format("Incomplete specifications, UserAgent Service cannot function."));
  70. Object[] args = new Object[] { config };
  71. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  72. m_GridUserService = ServerUtils.LoadPlugin<IGridUserService>(gridUserService, args);
  73. m_GatekeeperConnector = new GatekeeperServiceConnector();
  74. m_Initialized = true;
  75. }
  76. }
  77. public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt)
  78. {
  79. position = new Vector3(128, 128, 0); lookAt = Vector3.UnitY;
  80. m_log.DebugFormat("[USER AGENT SERVICE]: Request to get home region of user {0}", userID);
  81. GridRegion home = null;
  82. GridUserInfo uinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  83. if (uinfo != null)
  84. {
  85. if (uinfo.HomeRegionID != UUID.Zero)
  86. {
  87. home = m_GridService.GetRegionByUUID(UUID.Zero, uinfo.HomeRegionID);
  88. position = uinfo.HomePosition;
  89. lookAt = uinfo.HomeLookAt;
  90. }
  91. if (home == null)
  92. {
  93. List<GridRegion> defs = m_GridService.GetDefaultRegions(UUID.Zero);
  94. if (defs != null && defs.Count > 0)
  95. home = defs[0];
  96. }
  97. }
  98. return home;
  99. }
  100. public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, out string reason)
  101. {
  102. m_log.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} {1} to grid {2}",
  103. agentCircuit.firstname, agentCircuit.lastname, gatekeeper.ExternalHostName +":"+ gatekeeper.HttpPort);
  104. // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination
  105. GridRegion region = new GridRegion(gatekeeper);
  106. region.RegionName = finalDestination.RegionName;
  107. region.RegionID = finalDestination.RegionID;
  108. region.RegionLocX = finalDestination.RegionLocX;
  109. region.RegionLocY = finalDestination.RegionLocY;
  110. // Generate a new service session
  111. agentCircuit.ServiceSessionID = "http://" + region.ExternalHostName + ":" + region.HttpPort + ";" + UUID.Random();
  112. TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region);
  113. bool success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out reason);
  114. if (!success)
  115. {
  116. m_log.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} {1} to grid {2}, reason: {3}",
  117. agentCircuit.firstname, agentCircuit.lastname, region.ExternalHostName + ":" + region.HttpPort, reason);
  118. // restore the old travel info
  119. lock (m_TravelingAgents)
  120. m_TravelingAgents[agentCircuit.SessionID] = old;
  121. return false;
  122. }
  123. return true;
  124. }
  125. public void SetClientToken(UUID sessionID, string token)
  126. {
  127. if (m_TravelingAgents.ContainsKey(sessionID))
  128. {
  129. m_log.DebugFormat("[USER AGENT SERVICE]: Setting token {0} for session {1}", token, sessionID);
  130. m_TravelingAgents[sessionID].ClientToken = token;
  131. }
  132. }
  133. TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region)
  134. {
  135. TravelingAgentInfo travel = new TravelingAgentInfo();
  136. TravelingAgentInfo old = null;
  137. lock (m_TravelingAgents)
  138. {
  139. if (m_TravelingAgents.ContainsKey(agentCircuit.SessionID))
  140. {
  141. old = m_TravelingAgents[agentCircuit.SessionID];
  142. }
  143. m_TravelingAgents[agentCircuit.SessionID] = travel;
  144. }
  145. travel.UserID = agentCircuit.AgentID;
  146. travel.GridExternalName = region.ExternalHostName + ":" + region.HttpPort;
  147. travel.ServiceToken = agentCircuit.ServiceSessionID;
  148. if (old != null)
  149. travel.ClientToken = old.ClientToken;
  150. return old;
  151. }
  152. public void LogoutAgent(UUID userID, UUID sessionID)
  153. {
  154. m_log.DebugFormat("[USER AGENT SERVICE]: User {0} logged out", userID);
  155. lock (m_TravelingAgents)
  156. {
  157. List<UUID> travels = new List<UUID>();
  158. foreach (KeyValuePair<UUID, TravelingAgentInfo> kvp in m_TravelingAgents)
  159. if (kvp.Value == null) // do some clean up
  160. travels.Add(kvp.Key);
  161. else if (kvp.Value.UserID == userID)
  162. travels.Add(kvp.Key);
  163. foreach (UUID session in travels)
  164. m_TravelingAgents.Remove(session);
  165. }
  166. GridUserInfo guinfo = m_GridUserService.GetGridUserInfo(userID.ToString());
  167. if (guinfo != null)
  168. m_GridUserService.LoggedOut(userID.ToString(), guinfo.LastRegionID, guinfo.LastPosition, guinfo.LastLookAt);
  169. }
  170. // We need to prevent foreign users with the same UUID as a local user
  171. public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName)
  172. {
  173. if (!m_TravelingAgents.ContainsKey(sessionID))
  174. return false;
  175. TravelingAgentInfo travel = m_TravelingAgents[sessionID];
  176. return travel.GridExternalName == thisGridExternalName;
  177. }
  178. public bool VerifyClient(UUID sessionID, string token)
  179. {
  180. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying Client session {0} with token {1}", sessionID, token);
  181. //return true;
  182. // Commenting this for now until I understand better what part of a sender's
  183. // info stays unchanged throughout a session
  184. if (m_TravelingAgents.ContainsKey(sessionID))
  185. return m_TravelingAgents[sessionID].ClientToken == token;
  186. return false;
  187. }
  188. public bool VerifyAgent(UUID sessionID, string token)
  189. {
  190. if (m_TravelingAgents.ContainsKey(sessionID))
  191. {
  192. m_log.DebugFormat("[USER AGENT SERVICE]: Verifying agent token {0} against {1}", token, m_TravelingAgents[sessionID].ServiceToken);
  193. return m_TravelingAgents[sessionID].ServiceToken == token;
  194. }
  195. m_log.DebugFormat("[USER AGENT SERVICE]: Token verification for session {0}: no such session", sessionID);
  196. return false;
  197. }
  198. }
  199. class TravelingAgentInfo
  200. {
  201. public UUID UserID;
  202. public string GridExternalName = string.Empty;
  203. public string ServiceToken = string.Empty;
  204. public string ClientToken = string.Empty;
  205. }
  206. }