-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_foreignagent.php
executable file
·285 lines (251 loc) · 8.55 KB
/
rest_foreignagent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/******************************************************************************
* phpGridServer
*
* GNU LESSER GENERAL PUBLIC LICENSE
* Version 2.1, February 1999
*
*/
require_once("lib/services.php");
if(function_exists("apache_request_headers"))
{
$headers = apache_request_headers();
if(isset($headers["X-SecondLife-Shard"]))
{
http_response_code("400");
exit;
}
}
$_AGENT_POST = require_once("lib/rpc/agentpost.php");
/* this is the only needed data, we need to know for actually relaying the home agent correctly */
$serverDataUri = getServerDataFromAgentData($_AGENT_POST);
if($serverDataUri->isHome())
{
/* sims retry the home agents on the foreignagent path, so we relay that to homeagent implementation */
return require_once("rest_homeagent.php");
}
else
{
$serializer = new JSONHandler();
/* this is now what we need for the foreignagent path, homeagent relay does it own initialization */
function DoAgentResponse($success, $reason)
{
global $serializer;
$res = new RPCSuccessResponse();
$res->Params[] = new RPCStruct();
$res->Params[0]->success = $success;
$res->Params[0]->reason = "".$reason;
$res->Params[0]->your_ip = getRemoteIpAddr();
header("Content-Type: application/json");
echo $serializer->serializeRPC($res);
exit;
}
/* we reference the services here */
$hgServerDataService = getService("HGServerData");
$userAccountService = getService("UserAccount");
$gridUserService = getService("GridUser");
$presenceService = getService("Presence");
$gridService = getService("Grid");
$serverParamService = getService("ServerParam");
/* we take the remaining data here */
$userAccount = getUserAccountFromAgentData($_AGENT_POST);
$sessionInfo = getSessionInfoFromAgentData($_AGENT_POST);
/* from going through opensim code, it seems that it makes problems to have colliding UUIDs than it is worth about handling those on the sim */
/* that would require a proxy for separation */
try
{
$userAccountService->getAccountByID(null, $userAccount->PrincipalID);
trigger_error("UUID collision with Foreign Agent and Home Agent (Origin Grid: ".$serverDataUri->HomeURI.")");
DoAgentResponse(False, "UUID collision detected");
}
catch(Exception $e)
{
}
}
/* we load that PHP file here since we do not need that on the homeagent relaying the sims do */
require_once("lib/connectors/hypergrid/UserAgentRemoteConnector.php");
$userAgentConnector = new UserAgentRemoteConnector($serverDataUri->HomeURI);
$lockedmsg = $serverParamService->getParam("lockmessage", "");
if($lockedmsg != "")
{
DoAgentResponse(False, $lockedmsg);
}
$lockedmsg = $serverParamService->getParam("lockmessage_".$userAccount->PrincipalID, "");
if($lockedmsg != "")
{
DoAgentResponse(False, $lockedmsg);
}
/* verify user first before we store anything */
$servicesessionid = explode(";", $sessionInfo->ServiceSessionID);
if(count($servicesessionid) != 2)
{
trigger_error("Failed to verify user identity (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI."). Invalid Service Session ID: ".$sessionInfo->ServiceSessionID);
DoAgentResponse(False, "Failed to verify user identity (Code 1)");
}
$servicesessionid[0] = ServerDataURI::appendPortToURI($servicesessionid[0]);
$homeGrid = ServerDataURI::getHome();
if($homeGrid->GatekeeperURI != $servicesessionid[0])
{
trigger_error("Failed to verify user identity (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI."). Invalid grid Name in ServiceSessionID: ".$homeGrid->GatekeeperURI." != ${servicesessionid[0]}");
DoAgentResponse(False, "Failed to verify user identity (Code 2)");
}
try
{
if(!$userAgentConnector->verifyAgent($sessionInfo->SessionID, $sessionInfo->ServiceSessionID))
{
throw new Exception("Failed to verify here");
}
}
catch(Exception $e)
{
trigger_error("Failed to verify user identity (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI."). Agent verification failed:".get_class($e).".:".$e->getMessage());
DoAgentResponse(False, "Failed to verify user identity (Code 3)");
}
try
{
if(!$userAgentConnector->verifyClient($sessionInfo->SessionID,$clientInfo->ClientIP))
{
throw new Exception();
}
}
catch(Exception $e)
{
trigger_error("Failed to verify user identity (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI."). Client IP is not valid:".get_class($e).".:".$e->getMessage());
DoAgentResponse(False, "Failed to verify user identity (Code 4)");
}
/* now we know that we got a fully authenticated agent coming around */
try
{
/* update Server URI when a new agent arrives */
$hgServerDataService->storeServerURI($serverDataUri);
}
catch(Exception $e)
{
trigger_error("Failed to store HyperGrid Server URIs (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI."): ".get_class($e).".:".$e->getMessage());
DoAgentResponse(False, "Failed to store HyperGrid Server URIs");
}
try
{
/* that is a foreign agent coming in */
/* we have to replace the destination info */
$regionInfo = $gridService->getRegionByUuid(null, $destination->ID);
$destination = DestinationInfo::fromRegionInfo($regionInfo);
$destination->LocalToGrid = True;
$destination->TeleportFlags |= TeleportFlags::ViaHGLogin;
for($i = 0; $i < count($_AGENT_POST); ++$i)
{
if($_AGENT_POST[$i] instanceof DestinationInfo)
{
$_AGENT_POST[$i] = $destination; /* replace with Grid local destination info */
}
if($_AGENT_POST[$i] instanceof CircuitInfo)
{
$_AGENT_POST[$i]->Destination = $destination; /* replace with Grid local destination info */
}
}
}
catch(Exception $e)
{
trigger_error("Incoming foreign agent (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI."): Could not get target region information ".$e->getMessage()." ; ".get_class($e));
DoAgentResponse(False, "Could not retrieve target region information. ".trim($e->getMessage()));
}
/* filter @ */
while(substr($userAccount->LastName, 0, 1) == "@")
{
$sp = explode("@", $userAccount->FirstName, 2);
$sp = explode(".", $sp[0], 2);
if(count($sp) == 1)
{
$userAccount->FirstName = $sp;
$userAccount->LastName = "";
}
else
{
$userAccount->FirstName = $sp[0];
$userAccount->LastName = $sp[1];
}
}
$userAccount->FirstName = trim($userAccount->FirstName);
$userAccount->LastName = trim($userAccount->LastName);
/* we have to add a Presence and we need that GridUser entry */
/* the following is the UUI we use within GridUserInfo */
$UUI = $userAccount->PrincipalID.";".$serverDataUri->HomeURI.";".$userAccount->FirstName." ".$userAccount->LastName;
$presence = new Presence();
$presence->UserID = $UUI;
$presence->SessionID = $sessionInfo->SessionID;
$presence->SecureSessionID = $sessionInfo->SecureSessionID;
$presence->ClientIPAddress = $clientInfo->ClientIP;
$presence->RegionID = $destination->ID;
try
{
$presenceService->loginPresence($presence);
}
catch(Exception $e)
{
trigger_error("Failed to add presence (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI.")");
$gridUserService->loggedOut($UUI);
DoAgentResponse(False, "Failed to add Presence at target grid");
}
try
{
$gridUserService->loggedIn($UUI);
}
catch(Exception $e)
{
trigger_error("Failed to add GridUser (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI.")");
$presenceService->logoutPresence($sessionInfo->SessionID);
DoAgentResponse(False, "Failed to add GridUser at target grid");
}
try
{
$gridUser = $gridUserService->getGridUserHG($userAccount->PrincipalID);
}
catch(Exception $e)
{
trigger_error("Failed to verify GridUser (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI.") ".$e->getMessage());
$presenceService->logoutPresence($sessionInfo->SessionID);
DoAgentResponse(False, "Failed to verify GridUser");
}
if(substr("".$userAccount->LastName, 0, 1) == "@")
{
/* do not replace the name in this case */
}
else
{
$userAccount->FirstName = $userAccount->FirstName.".".$userAccount->LastName;
}
$uricomponents = parse_url($serverDataUri->HomeURI);
if(!isset($uricomponents["port"]))
{
$userAccount->LastName = "@".$uricomponents["host"];
}
else if($uricomponents["port"] == 80)
{
$userAccount->LastName = "@".$uricomponents["host"];
}
else
{
$userAccount->LastName = "@".$uricomponents["host"].":".$uricomponents["port"];
}
try
{
$launchAgentService = getService("LaunchAgent");
$circuitInfo = $launchAgentService->launchAgent($_AGENT_POST);
}
catch(Exception $e)
{
$msg = $e->getMessage();
trigger_error("Failed to launch foreign agent (".$userAccount->PrincipalID.",".$serverDataUri->HomeURI.") ".get_class($e).":".$msg);
try
{
$gridUserService->loggedOut($UUI);
}
catch(Exception $ex) {}
try
{
$presenceService->logoutPresence($sessionInfo->SessionID);
}
catch(Exception $ex) {}
DoAgentResponse(False, trim($msg));
}
DoAgentResponse(True, "authorized");