diff options
Diffstat (limited to 'OpenSim/Services/HypergridService/UserAgentService.cs')
-rw-r--r-- | OpenSim/Services/HypergridService/UserAgentService.cs | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/OpenSim/Services/HypergridService/UserAgentService.cs b/OpenSim/Services/HypergridService/UserAgentService.cs new file mode 100644 index 0000000..3af7ef9 --- /dev/null +++ b/OpenSim/Services/HypergridService/UserAgentService.cs | |||
@@ -0,0 +1,243 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Net; | ||
31 | using System.Reflection; | ||
32 | |||
33 | using OpenSim.Framework; | ||
34 | using OpenSim.Services.Connectors.Hypergrid; | ||
35 | using OpenSim.Services.Interfaces; | ||
36 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
37 | using OpenSim.Server.Base; | ||
38 | |||
39 | using OpenMetaverse; | ||
40 | using log4net; | ||
41 | using Nini.Config; | ||
42 | |||
43 | namespace OpenSim.Services.HypergridService | ||
44 | { | ||
45 | /// <summary> | ||
46 | /// This service is for HG1.5 only, to make up for the fact that clients don't | ||
47 | /// keep any private information in themselves, and that their 'home service' | ||
48 | /// needs to do it for them. | ||
49 | /// Once we have better clients, this shouldn't be needed. | ||
50 | /// </summary> | ||
51 | public class UserAgentService : IUserAgentService | ||
52 | { | ||
53 | private static readonly ILog m_log = | ||
54 | LogManager.GetLogger( | ||
55 | MethodBase.GetCurrentMethod().DeclaringType); | ||
56 | |||
57 | // This will need to go into a DB table | ||
58 | static Dictionary<UUID, TravelingAgentInfo> m_TravelingAgents = new Dictionary<UUID, TravelingAgentInfo>(); | ||
59 | |||
60 | static bool m_Initialized = false; | ||
61 | |||
62 | protected static IGridUserService m_GridUserService; | ||
63 | protected static IGridService m_GridService; | ||
64 | protected static GatekeeperServiceConnector m_GatekeeperConnector; | ||
65 | |||
66 | public UserAgentService(IConfigSource config) | ||
67 | { | ||
68 | if (!m_Initialized) | ||
69 | { | ||
70 | m_log.DebugFormat("[HOME USERS SECURITY]: Starting..."); | ||
71 | |||
72 | IConfig serverConfig = config.Configs["UserAgentService"]; | ||
73 | if (serverConfig == null) | ||
74 | throw new Exception(String.Format("No section UserAgentService in config file")); | ||
75 | |||
76 | string gridService = serverConfig.GetString("GridService", String.Empty); | ||
77 | string gridUserService = serverConfig.GetString("GridUserService", String.Empty); | ||
78 | |||
79 | if (gridService == string.Empty || gridUserService == string.Empty) | ||
80 | throw new Exception(String.Format("Incomplete specifications, UserAgent Service cannot function.")); | ||
81 | |||
82 | Object[] args = new Object[] { config }; | ||
83 | m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args); | ||
84 | m_GridUserService = ServerUtils.LoadPlugin<IGridUserService>(gridUserService, args); | ||
85 | m_GatekeeperConnector = new GatekeeperServiceConnector(); | ||
86 | |||
87 | m_Initialized = true; | ||
88 | } | ||
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 | |||
95 | m_log.DebugFormat("[USER AGENT SERVICE]: Request to get home region of user {0}", userID); | ||
96 | |||
97 | GridRegion home = null; | ||
98 | GridUserInfo uinfo = m_GridUserService.GetGridUserInfo(userID.ToString()); | ||
99 | if (uinfo != null) | ||
100 | { | ||
101 | if (uinfo.HomeRegionID != UUID.Zero) | ||
102 | { | ||
103 | home = m_GridService.GetRegionByUUID(UUID.Zero, uinfo.HomeRegionID); | ||
104 | position = uinfo.HomePosition; | ||
105 | lookAt = uinfo.HomeLookAt; | ||
106 | } | ||
107 | if (home == null) | ||
108 | { | ||
109 | List<GridRegion> defs = m_GridService.GetDefaultRegions(UUID.Zero); | ||
110 | if (defs != null && defs.Count > 0) | ||
111 | home = defs[0]; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | return home; | ||
116 | } | ||
117 | |||
118 | public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, out string reason) | ||
119 | { | ||
120 | m_log.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} {1} to grid {2}", | ||
121 | agentCircuit.firstname, agentCircuit.lastname, gatekeeper.ExternalHostName +":"+ gatekeeper.HttpPort); | ||
122 | |||
123 | // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination | ||
124 | GridRegion region = new GridRegion(gatekeeper); | ||
125 | region.RegionName = finalDestination.RegionName; | ||
126 | region.RegionID = finalDestination.RegionID; | ||
127 | region.RegionLocX = finalDestination.RegionLocX; | ||
128 | region.RegionLocY = finalDestination.RegionLocY; | ||
129 | |||
130 | // Generate a new service session | ||
131 | agentCircuit.ServiceSessionID = "http://" + region.ExternalHostName + ":" + region.HttpPort + ";" + UUID.Random(); | ||
132 | TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region); | ||
133 | |||
134 | bool success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)Constants.TeleportFlags.ViaLogin, out reason); | ||
135 | |||
136 | if (!success) | ||
137 | { | ||
138 | m_log.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} {1} to grid {2}, reason: {3}", | ||
139 | agentCircuit.firstname, agentCircuit.lastname, region.ExternalHostName + ":" + region.HttpPort, reason); | ||
140 | |||
141 | // restore the old travel info | ||
142 | lock (m_TravelingAgents) | ||
143 | m_TravelingAgents[agentCircuit.SessionID] = old; | ||
144 | |||
145 | return false; | ||
146 | } | ||
147 | |||
148 | return true; | ||
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 | |||
162 | m_TravelingAgents[agentCircuit.SessionID] = travel; | ||
163 | } | ||
164 | travel.UserID = agentCircuit.AgentID; | ||
165 | travel.GridExternalName = region.ExternalHostName + ":" + region.HttpPort; | ||
166 | travel.ServiceToken = agentCircuit.ServiceSessionID; | ||
167 | if (old != null) | ||
168 | travel.ClientToken = old.ClientToken; | ||
169 | |||
170 | return old; | ||
171 | } | ||
172 | |||
173 | public void LogoutAgent(UUID userID, UUID sessionID) | ||
174 | { | ||
175 | m_log.DebugFormat("[USER AGENT SERVICE]: User {0} logged out", userID); | ||
176 | |||
177 | lock (m_TravelingAgents) | ||
178 | { | ||
179 | List<UUID> travels = new List<UUID>(); | ||
180 | foreach (KeyValuePair<UUID, TravelingAgentInfo> kvp in m_TravelingAgents) | ||
181 | if (kvp.Value == null) // do some clean up | ||
182 | travels.Add(kvp.Key); | ||
183 | else if (kvp.Value.UserID == userID) | ||
184 | travels.Add(kvp.Key); | ||
185 | foreach (UUID session in travels) | ||
186 | m_TravelingAgents.Remove(session); | ||
187 | } | ||
188 | } | ||
189 | |||
190 | // We need to prevent foreign users with the same UUID as a local user | ||
191 | public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName) | ||
192 | { | ||
193 | if (!m_TravelingAgents.ContainsKey(sessionID)) | ||
194 | return false; | ||
195 | |||
196 | TravelingAgentInfo travel = m_TravelingAgents[sessionID]; | ||
197 | return travel.GridExternalName == thisGridExternalName; | ||
198 | } | ||
199 | |||
200 | public bool VerifyClient(UUID sessionID, string token) | ||
201 | { | ||
202 | return true; | ||
203 | |||
204 | // Commenting this for now until I understand better what part of a sender's | ||
205 | // info stays unchanged throughout a session | ||
206 | // | ||
207 | //if (m_TravelingAgents.ContainsKey(sessionID)) | ||
208 | //{ | ||
209 | // // Aquiles heel. Must trust the first grid upon login | ||
210 | // if (m_TravelingAgents[sessionID].ClientToken == string.Empty) | ||
211 | // { | ||
212 | // m_TravelingAgents[sessionID].ClientToken = token; | ||
213 | // return true; | ||
214 | // } | ||
215 | // return m_TravelingAgents[sessionID].ClientToken == token; | ||
216 | //} | ||
217 | //return false; | ||
218 | } | ||
219 | |||
220 | public bool VerifyAgent(UUID sessionID, string token) | ||
221 | { | ||
222 | if (m_TravelingAgents.ContainsKey(sessionID)) | ||
223 | { | ||
224 | m_log.DebugFormat("[USER AGENT SERVICE]: Verifying agent token {0} against {1}", token, m_TravelingAgents[sessionID].ServiceToken); | ||
225 | return m_TravelingAgents[sessionID].ServiceToken == token; | ||
226 | } | ||
227 | |||
228 | m_log.DebugFormat("[USER AGENT SERVICE]: Token verification for session {0}: no such session", sessionID); | ||
229 | |||
230 | return false; | ||
231 | } | ||
232 | |||
233 | } | ||
234 | |||
235 | class TravelingAgentInfo | ||
236 | { | ||
237 | public UUID UserID; | ||
238 | public string GridExternalName = string.Empty; | ||
239 | public string ServiceToken = string.Empty; | ||
240 | public string ClientToken = string.Empty; | ||
241 | } | ||
242 | |||
243 | } | ||