1 | /**
|
---|
2 | * vboxweb.cpp:
|
---|
3 | * hand-coded parts of the webservice server. This is linked with the
|
---|
4 | * generated code in out/.../src/VBox/Main/webservice/methodmaps.cpp
|
---|
5 | * (plus static gSOAP server code) to implement the actual webservice
|
---|
6 | * server, to which clients can connect.
|
---|
7 | *
|
---|
8 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | // shared webservice header
|
---|
24 | #include "vboxweb.h"
|
---|
25 |
|
---|
26 | // vbox headers
|
---|
27 | #include <VBox/com/com.h>
|
---|
28 | #include <VBox/com/ErrorInfo.h>
|
---|
29 | #include <VBox/com/errorprint.h>
|
---|
30 | #include <VBox/com/EventQueue.h>
|
---|
31 | #include <VBox/VRDPAuth.h>
|
---|
32 | #include <VBox/version.h>
|
---|
33 |
|
---|
34 | #include <iprt/thread.h>
|
---|
35 | #include <iprt/rand.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/getopt.h>
|
---|
38 | #include <iprt/ctype.h>
|
---|
39 | #include <iprt/process.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/ldr.h>
|
---|
42 |
|
---|
43 | // workaround for compile problems on gcc 4.1
|
---|
44 | #ifdef __GNUC__
|
---|
45 | #pragma GCC visibility push(default)
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | // gSOAP headers (must come after vbox includes because it checks for conflicting defs)
|
---|
49 | #include "soapH.h"
|
---|
50 |
|
---|
51 | // standard headers
|
---|
52 | #include <map>
|
---|
53 |
|
---|
54 | #ifdef __GNUC__
|
---|
55 | #pragma GCC visibility pop
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | // include generated namespaces table
|
---|
59 | #include "vboxwebsrv.nsmap"
|
---|
60 |
|
---|
61 | /****************************************************************************
|
---|
62 | *
|
---|
63 | * private typedefs
|
---|
64 | *
|
---|
65 | ****************************************************************************/
|
---|
66 |
|
---|
67 | typedef std::map<uint64_t, ManagedObjectRef*>
|
---|
68 | ManagedObjectsMapById;
|
---|
69 | typedef std::map<uint64_t, ManagedObjectRef*>::iterator
|
---|
70 | ManagedObjectsIteratorById;
|
---|
71 | typedef std::map<uintptr_t, ManagedObjectRef*>
|
---|
72 | ManagedObjectsMapByPtr;
|
---|
73 |
|
---|
74 | typedef std::map<uint64_t, WebServiceSession*>
|
---|
75 | SessionsMap;
|
---|
76 | typedef std::map<uint64_t, WebServiceSession*>::iterator
|
---|
77 | SessionsMapIterator;
|
---|
78 |
|
---|
79 | int fntWatchdog(RTTHREAD ThreadSelf, void *pvUser);
|
---|
80 |
|
---|
81 | /****************************************************************************
|
---|
82 | *
|
---|
83 | * Read-only global variables
|
---|
84 | *
|
---|
85 | ****************************************************************************/
|
---|
86 |
|
---|
87 | ComPtr<IVirtualBox> g_pVirtualBox = NULL;
|
---|
88 |
|
---|
89 | // generated strings in methodmaps.cpp
|
---|
90 | extern const char *g_pcszISession,
|
---|
91 | *g_pcszIVirtualBox;
|
---|
92 |
|
---|
93 | // globals for vboxweb command-line arguments
|
---|
94 | #define DEFAULT_TIMEOUT_SECS 300
|
---|
95 | #define DEFAULT_TIMEOUT_SECS_STRING "300"
|
---|
96 | int g_iWatchdogTimeoutSecs = DEFAULT_TIMEOUT_SECS;
|
---|
97 | int g_iWatchdogCheckInterval = 5;
|
---|
98 |
|
---|
99 | const char *g_pcszBindToHost = NULL; // host; NULL = current machine
|
---|
100 | unsigned int g_uBindToPort = 18083; // port
|
---|
101 | unsigned int g_uBacklog = 100; // backlog = max queue size for requests
|
---|
102 |
|
---|
103 | bool g_fVerbose = false; // be verbose
|
---|
104 | PRTSTREAM g_pstrLog = NULL;
|
---|
105 |
|
---|
106 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
107 | bool g_fDaemonize = false; // run in background.
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | /****************************************************************************
|
---|
111 | *
|
---|
112 | * Writeable global variables
|
---|
113 | *
|
---|
114 | ****************************************************************************/
|
---|
115 |
|
---|
116 | // this mutex protects the auth lib and authentication
|
---|
117 | util::RWLockHandle *g_pAuthLibLockHandle;
|
---|
118 |
|
---|
119 | // this mutex protects all of the below
|
---|
120 | util::RWLockHandle *g_pSessionsLockHandle;
|
---|
121 |
|
---|
122 | SessionsMap g_mapSessions;
|
---|
123 | ULONG64 g_iMaxManagedObjectID = 0;
|
---|
124 | ULONG64 g_cManagedObjects = 0;
|
---|
125 |
|
---|
126 | /****************************************************************************
|
---|
127 | *
|
---|
128 | * main
|
---|
129 | *
|
---|
130 | ****************************************************************************/
|
---|
131 |
|
---|
132 | static const RTGETOPTDEF g_aOptions[]
|
---|
133 | = {
|
---|
134 | { "--help", 'h', RTGETOPT_REQ_NOTHING },
|
---|
135 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
136 | { "--background", 'b', RTGETOPT_REQ_NOTHING },
|
---|
137 | #endif
|
---|
138 | { "--host", 'H', RTGETOPT_REQ_STRING },
|
---|
139 | { "--port", 'p', RTGETOPT_REQ_UINT32 },
|
---|
140 | { "--timeout", 't', RTGETOPT_REQ_UINT32 },
|
---|
141 | { "--check-interval", 'i', RTGETOPT_REQ_UINT32 },
|
---|
142 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
143 | { "--logfile", 'F', RTGETOPT_REQ_STRING },
|
---|
144 | };
|
---|
145 |
|
---|
146 | void DisplayHelp()
|
---|
147 | {
|
---|
148 | RTStrmPrintf(g_pStdErr, "\nUsage: vboxwebsrv [options]\n\nSupported options (default values in brackets):\n");
|
---|
149 | for (unsigned i = 0;
|
---|
150 | i < RT_ELEMENTS(g_aOptions);
|
---|
151 | ++i)
|
---|
152 | {
|
---|
153 | std::string str(g_aOptions[i].pszLong);
|
---|
154 | str += ", -";
|
---|
155 | str += g_aOptions[i].iShort;
|
---|
156 | str += ":";
|
---|
157 |
|
---|
158 | const char *pcszDescr = "";
|
---|
159 |
|
---|
160 | switch (g_aOptions[i].iShort)
|
---|
161 | {
|
---|
162 | case 'h':
|
---|
163 | pcszDescr = "Print this help message and exit.";
|
---|
164 | break;
|
---|
165 |
|
---|
166 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
167 | case 'b':
|
---|
168 | pcszDescr = "Run in background (daemon mode).";
|
---|
169 | break;
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | case 'H':
|
---|
173 | pcszDescr = "The host to bind to (localhost).";
|
---|
174 | break;
|
---|
175 |
|
---|
176 | case 'p':
|
---|
177 | pcszDescr = "The port to bind to (18083).";
|
---|
178 | break;
|
---|
179 |
|
---|
180 | case 't':
|
---|
181 | pcszDescr = "Session timeout in seconds; 0 = disable timeouts (" DEFAULT_TIMEOUT_SECS_STRING ").";
|
---|
182 | break;
|
---|
183 |
|
---|
184 | case 'i':
|
---|
185 | pcszDescr = "Frequency of timeout checks in seconds (5).";
|
---|
186 | break;
|
---|
187 |
|
---|
188 | case 'v':
|
---|
189 | pcszDescr = "Be verbose.";
|
---|
190 | break;
|
---|
191 |
|
---|
192 | case 'F':
|
---|
193 | pcszDescr = "Name of file to write log to (no file).";
|
---|
194 | break;
|
---|
195 | }
|
---|
196 |
|
---|
197 | RTStrmPrintf(g_pStdErr, "%-23s%s\n", str.c_str(), pcszDescr);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Implementation for WEBLOG macro defined in vboxweb.h; this prints a message
|
---|
203 | * to the console and optionally to the file that may have been given to the
|
---|
204 | * vboxwebsrv command line.
|
---|
205 | * @param pszFormat
|
---|
206 | */
|
---|
207 | void WebLog(const char *pszFormat, ...)
|
---|
208 | {
|
---|
209 | va_list args;
|
---|
210 | va_start(args, pszFormat);
|
---|
211 | char *psz = NULL;
|
---|
212 | RTStrAPrintfV(&psz, pszFormat, args);
|
---|
213 | va_end(args);
|
---|
214 |
|
---|
215 | // terminal
|
---|
216 | RTPrintf("%s", psz);
|
---|
217 |
|
---|
218 | // log file
|
---|
219 | if (g_pstrLog)
|
---|
220 | {
|
---|
221 | RTStrmPrintf(g_pstrLog, "%s", psz);
|
---|
222 | RTStrmFlush(g_pstrLog);
|
---|
223 | }
|
---|
224 |
|
---|
225 | // logger instance
|
---|
226 | RTLogLoggerEx(LOG_INSTANCE, RTLOGGRPFLAGS_DJ, LOG_GROUP, "%s", psz);
|
---|
227 |
|
---|
228 | RTStrFree(psz);
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Helper for printing SOAP error messages.
|
---|
233 | * @param soap
|
---|
234 | */
|
---|
235 | void WebLogSoapError(struct soap *soap)
|
---|
236 | {
|
---|
237 | if (soap_check_state(soap))
|
---|
238 | {
|
---|
239 | WebLog("Error: soap struct not initialized\n");
|
---|
240 | return;
|
---|
241 | }
|
---|
242 |
|
---|
243 | const char *pcszFaultString = *soap_faultstring(soap);
|
---|
244 | const char **ppcszDetail = soap_faultcode(soap);
|
---|
245 | WebLog("#### SOAP FAULT: %s [%s]\n",
|
---|
246 | pcszFaultString ? pcszFaultString : "[no fault string available]",
|
---|
247 | (ppcszDetail && *ppcszDetail) ? *ppcszDetail : "no details available");
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Called from main(). This implements the loop that takes SOAP calls
|
---|
252 | * from HTTP and serves them, calling the COM method implementations
|
---|
253 | * in the generated methodmaps.cpp code.
|
---|
254 | */
|
---|
255 | void beginProcessing()
|
---|
256 | {
|
---|
257 | // set up gSOAP
|
---|
258 | struct soap soap;
|
---|
259 | soap_init(&soap);
|
---|
260 |
|
---|
261 | soap.bind_flags |= SO_REUSEADDR;
|
---|
262 | // avoid EADDRINUSE on bind()
|
---|
263 |
|
---|
264 | int m, s; // master and slave sockets
|
---|
265 | m = soap_bind(&soap,
|
---|
266 | g_pcszBindToHost, // host: current machine
|
---|
267 | g_uBindToPort, // port
|
---|
268 | g_uBacklog); // backlog = max queue size for requests
|
---|
269 | if (m < 0)
|
---|
270 | WebLogSoapError(&soap);
|
---|
271 | else
|
---|
272 | {
|
---|
273 | WebLog("Socket connection successful: host = %s, port = %u, master socket = %d\n",
|
---|
274 | (g_pcszBindToHost) ? g_pcszBindToHost : "default (localhost)",
|
---|
275 | g_uBindToPort,
|
---|
276 | m);
|
---|
277 |
|
---|
278 | for (uint64_t i = 1;
|
---|
279 | ;
|
---|
280 | i++)
|
---|
281 | {
|
---|
282 | // call gSOAP to handle incoming SOAP connection
|
---|
283 | s = soap_accept(&soap);
|
---|
284 | if (s < 0)
|
---|
285 | {
|
---|
286 | WebLogSoapError(&soap);
|
---|
287 | break;
|
---|
288 | }
|
---|
289 |
|
---|
290 | WebLog("%llu: accepted connection from IP=%lu.%lu.%lu.%lu socket=%d... ",
|
---|
291 | i,
|
---|
292 | (soap.ip>>24)&0xFF,
|
---|
293 | (soap.ip>>16)&0xFF,
|
---|
294 | (soap.ip>>8)&0xFF,
|
---|
295 | soap.ip&0xFF,
|
---|
296 | s);
|
---|
297 |
|
---|
298 | // now process the RPC request (this goes into the
|
---|
299 | // generated code in methodmaps.cpp with all the COM calls)
|
---|
300 | if (soap_serve(&soap) != SOAP_OK)
|
---|
301 | {
|
---|
302 | WebLogSoapError(&soap);
|
---|
303 | }
|
---|
304 |
|
---|
305 | WebLog("Request served\n");
|
---|
306 |
|
---|
307 | soap_destroy(&soap); // clean up class instances
|
---|
308 | soap_end(&soap); // clean up everything and close socket
|
---|
309 |
|
---|
310 | // we have to process main event queue
|
---|
311 | int vrc = com::EventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | soap_done(&soap); // close master socket and detach environment
|
---|
315 | }
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Start up the webservice server. This keeps running and waits
|
---|
319 | * for incoming SOAP connections; for each request that comes in,
|
---|
320 | * it calls method implementation code, most of it in the generated
|
---|
321 | * code in methodmaps.cpp.
|
---|
322 | *
|
---|
323 | * @param argc
|
---|
324 | * @param argv[]
|
---|
325 | * @return
|
---|
326 | */
|
---|
327 | int main(int argc, char* argv[])
|
---|
328 | {
|
---|
329 | int rc;
|
---|
330 |
|
---|
331 | // intialize runtime
|
---|
332 | RTR3Init();
|
---|
333 |
|
---|
334 | RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " Webservice Version " VBOX_VERSION_STRING "\n"
|
---|
335 | "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
336 | "All rights reserved.\n");
|
---|
337 |
|
---|
338 | int c;
|
---|
339 | RTGETOPTUNION ValueUnion;
|
---|
340 | RTGETOPTSTATE GetState;
|
---|
341 | RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0 /* fFlags */);
|
---|
342 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
343 | {
|
---|
344 | switch (c)
|
---|
345 | {
|
---|
346 | case 'H':
|
---|
347 | g_pcszBindToHost = ValueUnion.psz;
|
---|
348 | break;
|
---|
349 |
|
---|
350 | case 'p':
|
---|
351 | g_uBindToPort = ValueUnion.u32;
|
---|
352 | break;
|
---|
353 |
|
---|
354 | case 't':
|
---|
355 | g_iWatchdogTimeoutSecs = ValueUnion.u32;
|
---|
356 | break;
|
---|
357 |
|
---|
358 | case 'i':
|
---|
359 | g_iWatchdogCheckInterval = ValueUnion.u32;
|
---|
360 | break;
|
---|
361 |
|
---|
362 | case 'F':
|
---|
363 | {
|
---|
364 | int rc2 = RTStrmOpen(ValueUnion.psz, "a", &g_pstrLog);
|
---|
365 | if (rc2)
|
---|
366 | {
|
---|
367 | RTPrintf("Error: Cannot open log file \"%s\" for writing, error %d.\n", ValueUnion.psz, rc2);
|
---|
368 | exit(2);
|
---|
369 | }
|
---|
370 |
|
---|
371 | WebLog("Sun VirtualBox Webservice Version %s\n"
|
---|
372 | "Opened log file \"%s\"\n", VBOX_VERSION_STRING, ValueUnion.psz);
|
---|
373 | }
|
---|
374 | break;
|
---|
375 |
|
---|
376 | case 'h':
|
---|
377 | DisplayHelp();
|
---|
378 | exit(0);
|
---|
379 | break;
|
---|
380 |
|
---|
381 | case 'v':
|
---|
382 | g_fVerbose = true;
|
---|
383 | break;
|
---|
384 |
|
---|
385 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
386 | case 'b':
|
---|
387 | g_fDaemonize = true;
|
---|
388 | break;
|
---|
389 | #endif
|
---|
390 | case VINF_GETOPT_NOT_OPTION:
|
---|
391 | RTStrmPrintf(g_pStdErr, "unhandled parameter: %s\n", ValueUnion.psz);
|
---|
392 | return 1;
|
---|
393 |
|
---|
394 | default:
|
---|
395 | if (c > 0)
|
---|
396 | {
|
---|
397 | if (RT_C_IS_GRAPH(c))
|
---|
398 | RTStrmPrintf(g_pStdErr, "unhandled option: -%c", c);
|
---|
399 | else
|
---|
400 | RTStrmPrintf(g_pStdErr, "unhandled option: %i", c);
|
---|
401 | }
|
---|
402 | else if (c == VERR_GETOPT_UNKNOWN_OPTION)
|
---|
403 | RTStrmPrintf(g_pStdErr, "unknown option: %s", ValueUnion.psz);
|
---|
404 | else if (ValueUnion.pDef)
|
---|
405 | RTStrmPrintf(g_pStdErr, "%s: %Rrs", ValueUnion.pDef->pszLong, c);
|
---|
406 | else
|
---|
407 | RTStrmPrintf(g_pStdErr, "%Rrs", c);
|
---|
408 | exit(1);
|
---|
409 | break;
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
414 | if (g_fDaemonize)
|
---|
415 | {
|
---|
416 | rc = RTProcDaemonize(false /* fNoChDir */, false /* fNoClose */,
|
---|
417 | NULL);
|
---|
418 | if (RT_FAILURE(rc))
|
---|
419 | {
|
---|
420 | RTStrmPrintf(g_pStdErr, "vboxwebsrv: failed to daemonize, rc=%Rrc. exiting.\n", rc);
|
---|
421 | exit(1);
|
---|
422 | }
|
---|
423 | }
|
---|
424 | #endif
|
---|
425 |
|
---|
426 | // intialize COM/XPCOM
|
---|
427 | rc = com::Initialize();
|
---|
428 | if (FAILED(rc))
|
---|
429 | {
|
---|
430 | RTPrintf("ERROR: failed to initialize COM!\n");
|
---|
431 | return rc;
|
---|
432 | }
|
---|
433 |
|
---|
434 | ComPtr<ISession> session;
|
---|
435 |
|
---|
436 | rc = g_pVirtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
437 | if (FAILED(rc))
|
---|
438 | RTPrintf("ERROR: failed to create the VirtualBox object!\n");
|
---|
439 | else
|
---|
440 | {
|
---|
441 | rc = session.createInprocObject(CLSID_Session);
|
---|
442 | if (FAILED(rc))
|
---|
443 | RTPrintf("ERROR: failed to create a session object!\n");
|
---|
444 | }
|
---|
445 |
|
---|
446 | if (FAILED(rc))
|
---|
447 | {
|
---|
448 | com::ErrorInfo info;
|
---|
449 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
450 | {
|
---|
451 | com::GluePrintRCMessage(rc);
|
---|
452 | RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
|
---|
453 | }
|
---|
454 | else
|
---|
455 | com::GluePrintErrorInfo(info);
|
---|
456 | return rc;
|
---|
457 | }
|
---|
458 |
|
---|
459 | // create the global mutexes
|
---|
460 | g_pAuthLibLockHandle = new util::RWLockHandle(util::LOCKCLASS_OBJECTSTATE);
|
---|
461 | g_pSessionsLockHandle = new util::RWLockHandle(util::LOCKCLASS_OBJECTSTATE);
|
---|
462 |
|
---|
463 | if (g_iWatchdogTimeoutSecs > 0)
|
---|
464 | {
|
---|
465 | // start our watchdog thread
|
---|
466 | RTTHREAD tWatchdog;
|
---|
467 | if (RTThreadCreate(&tWatchdog,
|
---|
468 | fntWatchdog,
|
---|
469 | NULL,
|
---|
470 | 32*1024,
|
---|
471 | RTTHREADTYPE_MAIN_WORKER,
|
---|
472 | 0,
|
---|
473 | "Watchdog"))
|
---|
474 | {
|
---|
475 | RTStrmPrintf(g_pStdErr, "[!] Cannot start watchdog thread\n");
|
---|
476 | exit(1);
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | beginProcessing();
|
---|
481 |
|
---|
482 | com::Shutdown();
|
---|
483 | }
|
---|
484 |
|
---|
485 | /****************************************************************************
|
---|
486 | *
|
---|
487 | * Watchdog thread
|
---|
488 | *
|
---|
489 | ****************************************************************************/
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Watchdog thread, runs in the background while the webservice is alive.
|
---|
493 | *
|
---|
494 | * This gets started by main() and runs in the background to check all sessions
|
---|
495 | * for whether they have been no requests in a configurable timeout period. In
|
---|
496 | * that case, the session is automatically logged off.
|
---|
497 | */
|
---|
498 | int fntWatchdog(RTTHREAD ThreadSelf, void *pvUser)
|
---|
499 | {
|
---|
500 | WEBDEBUG(("Watchdog thread started\n"));
|
---|
501 |
|
---|
502 | while (1)
|
---|
503 | {
|
---|
504 | WEBDEBUG(("Watchdog: sleeping %d seconds\n", g_iWatchdogCheckInterval));
|
---|
505 | RTThreadSleep(g_iWatchdogCheckInterval * 1000);
|
---|
506 |
|
---|
507 | time_t tNow;
|
---|
508 | time(&tNow);
|
---|
509 |
|
---|
510 | // lock the sessions while we're iterating; this blocks
|
---|
511 | // out the COM code from messing with it
|
---|
512 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
513 | WEBDEBUG(("Watchdog: checking %d sessions\n", g_mapSessions.size()));
|
---|
514 |
|
---|
515 | SessionsMap::iterator it = g_mapSessions.begin(),
|
---|
516 | itEnd = g_mapSessions.end();
|
---|
517 | while (it != itEnd)
|
---|
518 | {
|
---|
519 | WebServiceSession *pSession = it->second;
|
---|
520 | WEBDEBUG(("Watchdog: tNow: %d, session timestamp: %d\n", tNow, pSession->getLastObjectLookup()));
|
---|
521 | if ( tNow
|
---|
522 | > pSession->getLastObjectLookup() + g_iWatchdogTimeoutSecs
|
---|
523 | )
|
---|
524 | {
|
---|
525 | WEBDEBUG(("Watchdog: Session %llX timed out, deleting\n", pSession->getID()));
|
---|
526 | delete pSession;
|
---|
527 | it = g_mapSessions.begin();
|
---|
528 | }
|
---|
529 | else
|
---|
530 | ++it;
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | WEBDEBUG(("Watchdog thread ending\n"));
|
---|
535 | return 0;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /****************************************************************************
|
---|
539 | *
|
---|
540 | * SOAP exceptions
|
---|
541 | *
|
---|
542 | ****************************************************************************/
|
---|
543 |
|
---|
544 | /**
|
---|
545 | * Helper function to raise a SOAP fault. Called by the other helper
|
---|
546 | * functions, which raise specific SOAP faults.
|
---|
547 | *
|
---|
548 | * @param soap
|
---|
549 | * @param str
|
---|
550 | * @param extype
|
---|
551 | * @param ex
|
---|
552 | */
|
---|
553 | void RaiseSoapFault(struct soap *soap,
|
---|
554 | const char *pcsz,
|
---|
555 | int extype,
|
---|
556 | void *ex)
|
---|
557 | {
|
---|
558 | // raise the fault
|
---|
559 | soap_sender_fault(soap, pcsz, NULL);
|
---|
560 |
|
---|
561 | struct SOAP_ENV__Detail *pDetail = (struct SOAP_ENV__Detail*)soap_malloc(soap, sizeof(struct SOAP_ENV__Detail));
|
---|
562 |
|
---|
563 | // without the following, gSOAP crashes miserably when sending out the
|
---|
564 | // data because it will try to serialize all fields (stupid documentation)
|
---|
565 | memset(pDetail, 0, sizeof(struct SOAP_ENV__Detail));
|
---|
566 |
|
---|
567 | // fill extended info depending on SOAP version
|
---|
568 | if (soap->version == 2) // SOAP 1.2 is used
|
---|
569 | {
|
---|
570 | soap->fault->SOAP_ENV__Detail = pDetail;
|
---|
571 | soap->fault->SOAP_ENV__Detail->__type = extype;
|
---|
572 | soap->fault->SOAP_ENV__Detail->fault = ex;
|
---|
573 | soap->fault->SOAP_ENV__Detail->__any = NULL; // no other XML data
|
---|
574 | }
|
---|
575 | else
|
---|
576 | {
|
---|
577 | soap->fault->detail = pDetail;
|
---|
578 | soap->fault->detail->__type = extype;
|
---|
579 | soap->fault->detail->fault = ex;
|
---|
580 | soap->fault->detail->__any = NULL; // no other XML data
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | /**
|
---|
585 | * Raises a SOAP fault that signals that an invalid object was passed.
|
---|
586 | *
|
---|
587 | * @param soap
|
---|
588 | * @param obj
|
---|
589 | */
|
---|
590 | void RaiseSoapInvalidObjectFault(struct soap *soap,
|
---|
591 | WSDLT_ID obj)
|
---|
592 | {
|
---|
593 | _vbox__InvalidObjectFault *ex = soap_new__vbox__InvalidObjectFault(soap, 1);
|
---|
594 | ex->badObjectID = obj;
|
---|
595 |
|
---|
596 | std::string str("VirtualBox error: ");
|
---|
597 | str += "Invalid managed object reference \"" + obj + "\"";
|
---|
598 |
|
---|
599 | RaiseSoapFault(soap,
|
---|
600 | str.c_str(),
|
---|
601 | SOAP_TYPE__vbox__InvalidObjectFault,
|
---|
602 | ex);
|
---|
603 | }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Return a safe C++ string from the given COM string,
|
---|
607 | * without crashing if the COM string is empty.
|
---|
608 | * @param bstr
|
---|
609 | * @return
|
---|
610 | */
|
---|
611 | std::string ConvertComString(const com::Bstr &bstr)
|
---|
612 | {
|
---|
613 | com::Utf8Str ustr(bstr);
|
---|
614 | const char *pcsz;
|
---|
615 | if ((pcsz = ustr.raw()))
|
---|
616 | return pcsz;
|
---|
617 | return "";
|
---|
618 | }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Return a safe C++ string from the given COM UUID,
|
---|
622 | * without crashing if the UUID is empty.
|
---|
623 | * @param bstr
|
---|
624 | * @return
|
---|
625 | */
|
---|
626 | std::string ConvertComString(const com::Guid &uuid)
|
---|
627 | {
|
---|
628 | com::Utf8Str ustr(uuid.toString());
|
---|
629 | const char *pcsz;
|
---|
630 | if ((pcsz = ustr.raw()))
|
---|
631 | return pcsz;
|
---|
632 | return "";
|
---|
633 | }
|
---|
634 |
|
---|
635 | /**
|
---|
636 | * Raises a SOAP runtime fault.
|
---|
637 | *
|
---|
638 | * @param pObj
|
---|
639 | */
|
---|
640 | void RaiseSoapRuntimeFault(struct soap *soap,
|
---|
641 | HRESULT apirc,
|
---|
642 | IUnknown *pObj)
|
---|
643 | {
|
---|
644 | com::ErrorInfo info(pObj);
|
---|
645 |
|
---|
646 | WEBDEBUG((" error, raising SOAP exception\n"));
|
---|
647 |
|
---|
648 | RTStrmPrintf(g_pStdErr, "API return code: 0x%08X (%Rhrc)\n", apirc, apirc);
|
---|
649 | RTStrmPrintf(g_pStdErr, "COM error info result code: 0x%lX\n", info.getResultCode());
|
---|
650 | RTStrmPrintf(g_pStdErr, "COM error info text: %ls\n", info.getText().raw());
|
---|
651 |
|
---|
652 | // allocated our own soap fault struct
|
---|
653 | _vbox__RuntimeFault *ex = soap_new__vbox__RuntimeFault(soap, 1);
|
---|
654 | ex->resultCode = info.getResultCode();
|
---|
655 | ex->text = ConvertComString(info.getText());
|
---|
656 | ex->component = ConvertComString(info.getComponent());
|
---|
657 | ex->interfaceID = ConvertComString(info.getInterfaceID());
|
---|
658 |
|
---|
659 | // compose descriptive message
|
---|
660 | com::Utf8StrFmt str("VirtualBox error: %s (0x%RU32)", ex->text.c_str(), ex->resultCode);
|
---|
661 |
|
---|
662 | RaiseSoapFault(soap,
|
---|
663 | str.c_str(),
|
---|
664 | SOAP_TYPE__vbox__RuntimeFault,
|
---|
665 | ex);
|
---|
666 | }
|
---|
667 |
|
---|
668 | /****************************************************************************
|
---|
669 | *
|
---|
670 | * splitting and merging of object IDs
|
---|
671 | *
|
---|
672 | ****************************************************************************/
|
---|
673 |
|
---|
674 | uint64_t str2ulonglong(const char *pcsz)
|
---|
675 | {
|
---|
676 | uint64_t u = 0;
|
---|
677 | RTStrToUInt64Full(pcsz, 16, &u);
|
---|
678 | return u;
|
---|
679 | }
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Splits a managed object reference (in string form, as
|
---|
683 | * passed in from a SOAP method call) into two integers for
|
---|
684 | * session and object IDs, respectively.
|
---|
685 | *
|
---|
686 | * @param id
|
---|
687 | * @param sessid
|
---|
688 | * @param objid
|
---|
689 | * @return
|
---|
690 | */
|
---|
691 | bool SplitManagedObjectRef(const WSDLT_ID &id,
|
---|
692 | uint64_t *pSessid,
|
---|
693 | uint64_t *pObjid)
|
---|
694 | {
|
---|
695 | // 64-bit numbers in hex have 16 digits; hence
|
---|
696 | // the object-ref string must have 16 + "-" + 16 characters
|
---|
697 | std::string str;
|
---|
698 | if ( (id.length() == 33)
|
---|
699 | && (id[16] == '-')
|
---|
700 | )
|
---|
701 | {
|
---|
702 | char psz[34];
|
---|
703 | memcpy(psz, id.c_str(), 34);
|
---|
704 | psz[16] = '\0';
|
---|
705 | if (pSessid)
|
---|
706 | *pSessid = str2ulonglong(psz);
|
---|
707 | if (pObjid)
|
---|
708 | *pObjid = str2ulonglong(psz + 17);
|
---|
709 | return true;
|
---|
710 | }
|
---|
711 |
|
---|
712 | return false;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * Creates a managed object reference (in string form) from
|
---|
717 | * two integers representing a session and object ID, respectively.
|
---|
718 | *
|
---|
719 | * @param sz Buffer with at least 34 bytes space to receive MOR string.
|
---|
720 | * @param sessid
|
---|
721 | * @param objid
|
---|
722 | * @return
|
---|
723 | */
|
---|
724 | void MakeManagedObjectRef(char *sz,
|
---|
725 | uint64_t &sessid,
|
---|
726 | uint64_t &objid)
|
---|
727 | {
|
---|
728 | RTStrFormatNumber(sz, sessid, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
|
---|
729 | sz[16] = '-';
|
---|
730 | RTStrFormatNumber(sz + 17, objid, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
|
---|
731 | }
|
---|
732 |
|
---|
733 | /****************************************************************************
|
---|
734 | *
|
---|
735 | * class WebServiceSession
|
---|
736 | *
|
---|
737 | ****************************************************************************/
|
---|
738 |
|
---|
739 | class WebServiceSessionPrivate
|
---|
740 | {
|
---|
741 | public:
|
---|
742 | ManagedObjectsMapById _mapManagedObjectsById;
|
---|
743 | ManagedObjectsMapByPtr _mapManagedObjectsByPtr;
|
---|
744 | };
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Constructor for the session object.
|
---|
748 | *
|
---|
749 | * Preconditions: Caller must have locked g_pSessionsLockHandle in write mode.
|
---|
750 | *
|
---|
751 | * @param username
|
---|
752 | * @param password
|
---|
753 | */
|
---|
754 | WebServiceSession::WebServiceSession()
|
---|
755 | : _fDestructing(false),
|
---|
756 | _pISession(NULL),
|
---|
757 | _tLastObjectLookup(0)
|
---|
758 | {
|
---|
759 | _pp = new WebServiceSessionPrivate;
|
---|
760 | _uSessionID = RTRandU64();
|
---|
761 |
|
---|
762 | // register this session globally
|
---|
763 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
764 | g_mapSessions[_uSessionID] = this;
|
---|
765 | }
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Destructor. Cleans up and destroys all contained managed object references on the way.
|
---|
769 | *
|
---|
770 | * Preconditions: Caller must have locked g_pSessionsLockHandle in write mode.
|
---|
771 | */
|
---|
772 | WebServiceSession::~WebServiceSession()
|
---|
773 | {
|
---|
774 | // delete us from global map first so we can't be found
|
---|
775 | // any more while we're cleaning up
|
---|
776 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
777 | g_mapSessions.erase(_uSessionID);
|
---|
778 |
|
---|
779 | // notify ManagedObjectRef destructor so it won't
|
---|
780 | // remove itself from the maps; this avoids rebalancing
|
---|
781 | // the map's tree on every delete as well
|
---|
782 | _fDestructing = true;
|
---|
783 |
|
---|
784 | // if (_pISession)
|
---|
785 | // {
|
---|
786 | // delete _pISession;
|
---|
787 | // _pISession = NULL;
|
---|
788 | // }
|
---|
789 |
|
---|
790 | ManagedObjectsMapById::iterator it,
|
---|
791 | end = _pp->_mapManagedObjectsById.end();
|
---|
792 | for (it = _pp->_mapManagedObjectsById.begin();
|
---|
793 | it != end;
|
---|
794 | ++it)
|
---|
795 | {
|
---|
796 | ManagedObjectRef *pRef = it->second;
|
---|
797 | delete pRef; // this frees the contained ComPtr as well
|
---|
798 | }
|
---|
799 |
|
---|
800 | delete _pp;
|
---|
801 | }
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * Authenticate the username and password against an authentification authority.
|
---|
805 | *
|
---|
806 | * @return 0 if the user was successfully authenticated, or an error code
|
---|
807 | * otherwise.
|
---|
808 | */
|
---|
809 |
|
---|
810 | int WebServiceSession::authenticate(const char *pcszUsername,
|
---|
811 | const char *pcszPassword)
|
---|
812 | {
|
---|
813 | int rc = VERR_WEB_NOT_AUTHENTICATED;
|
---|
814 |
|
---|
815 | util::AutoReadLock lock(g_pAuthLibLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
816 |
|
---|
817 | static bool fAuthLibLoaded = false;
|
---|
818 | static PVRDPAUTHENTRY pfnAuthEntry = NULL;
|
---|
819 | static PVRDPAUTHENTRY2 pfnAuthEntry2 = NULL;
|
---|
820 |
|
---|
821 | if (!fAuthLibLoaded)
|
---|
822 | {
|
---|
823 | // retrieve authentication library from system properties
|
---|
824 | ComPtr<ISystemProperties> systemProperties;
|
---|
825 | g_pVirtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
|
---|
826 |
|
---|
827 | com::Bstr authLibrary;
|
---|
828 | systemProperties->COMGETTER(WebServiceAuthLibrary)(authLibrary.asOutParam());
|
---|
829 | com::Utf8Str filename = authLibrary;
|
---|
830 |
|
---|
831 | WEBDEBUG(("external authentication library is '%ls'\n", authLibrary.raw()));
|
---|
832 |
|
---|
833 | if (filename == "null")
|
---|
834 | // authentication disabled, let everyone in:
|
---|
835 | fAuthLibLoaded = true;
|
---|
836 | else
|
---|
837 | {
|
---|
838 | RTLDRMOD hlibAuth = 0;
|
---|
839 | do
|
---|
840 | {
|
---|
841 | rc = RTLdrLoad(filename.raw(), &hlibAuth);
|
---|
842 | if (RT_FAILURE(rc))
|
---|
843 | {
|
---|
844 | WEBDEBUG(("%s() Failed to load external authentication library. Error code: %Rrc\n", __FUNCTION__, rc));
|
---|
845 | break;
|
---|
846 | }
|
---|
847 |
|
---|
848 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, "VRDPAuth2", (void**)&pfnAuthEntry2)))
|
---|
849 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, "VRDPAuth2", rc));
|
---|
850 |
|
---|
851 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, "VRDPAuth", (void**)&pfnAuthEntry)))
|
---|
852 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, "VRDPAuth", rc));
|
---|
853 |
|
---|
854 | if (pfnAuthEntry || pfnAuthEntry2)
|
---|
855 | fAuthLibLoaded = true;
|
---|
856 |
|
---|
857 | } while (0);
|
---|
858 | }
|
---|
859 | }
|
---|
860 |
|
---|
861 | rc = VERR_WEB_NOT_AUTHENTICATED;
|
---|
862 | VRDPAuthResult result;
|
---|
863 | if (pfnAuthEntry2)
|
---|
864 | {
|
---|
865 | result = pfnAuthEntry2(NULL, VRDPAuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
|
---|
866 | WEBDEBUG(("%s(): result of VRDPAuth2(): %d\n", __FUNCTION__, result));
|
---|
867 | if (result == VRDPAuthAccessGranted)
|
---|
868 | rc = 0;
|
---|
869 | }
|
---|
870 | else if (pfnAuthEntry)
|
---|
871 | {
|
---|
872 | result = pfnAuthEntry(NULL, VRDPAuthGuestNotAsked, pcszUsername, pcszPassword, NULL);
|
---|
873 | WEBDEBUG(("%s(): result of VRDPAuth(%s, [%d]): %d\n", __FUNCTION__, pcszUsername, strlen(pcszPassword), result));
|
---|
874 | if (result == VRDPAuthAccessGranted)
|
---|
875 | rc = 0;
|
---|
876 | }
|
---|
877 | else if (fAuthLibLoaded)
|
---|
878 | // fAuthLibLoaded = true but both pointers are NULL:
|
---|
879 | // then the authlib was "null" and auth was disabled
|
---|
880 | rc = 0;
|
---|
881 | else
|
---|
882 | {
|
---|
883 | WEBDEBUG(("Could not resolve VRDPAuth2 or VRDPAuth entry point"));
|
---|
884 | }
|
---|
885 |
|
---|
886 | lock.release();
|
---|
887 |
|
---|
888 | if (!rc)
|
---|
889 | {
|
---|
890 | do
|
---|
891 | {
|
---|
892 | // now create the ISession object that this webservice session can use
|
---|
893 | // (and of which IWebsessionManager::getSessionObject returns a managed object reference)
|
---|
894 | ComPtr<ISession> session;
|
---|
895 | if (FAILED(rc = session.createInprocObject(CLSID_Session)))
|
---|
896 | {
|
---|
897 | WEBDEBUG(("ERROR: cannot create session object!"));
|
---|
898 | break;
|
---|
899 | }
|
---|
900 |
|
---|
901 | _pISession = new ManagedObjectRef(*this, g_pcszISession, session);
|
---|
902 |
|
---|
903 | if (g_fVerbose)
|
---|
904 | {
|
---|
905 | ISession *p = session;
|
---|
906 | std::string strMOR = _pISession->toWSDL();
|
---|
907 | WEBDEBUG((" * %s: created session object with comptr 0x%lX, MOR = %s\n", __FUNCTION__, p, strMOR.c_str()));
|
---|
908 | }
|
---|
909 | } while (0);
|
---|
910 | }
|
---|
911 |
|
---|
912 | return rc;
|
---|
913 | }
|
---|
914 |
|
---|
915 | /**
|
---|
916 | * Look up, in this session, whether a ManagedObjectRef has already been
|
---|
917 | * created for the given COM pointer.
|
---|
918 | *
|
---|
919 | * Note how we require that a ComPtr<IUnknown> is passed, which causes a
|
---|
920 | * queryInterface call when the caller passes in a different type, since
|
---|
921 | * a ComPtr<IUnknown> will point to something different than a
|
---|
922 | * ComPtr<IVirtualBox>, for example. As we store the ComPtr<IUnknown> in
|
---|
923 | * our private hash table, we must search for one too.
|
---|
924 | *
|
---|
925 | * Preconditions: Caller must have locked g_pSessionsLockHandle in read mode.
|
---|
926 | *
|
---|
927 | * @param pcu pointer to a COM object.
|
---|
928 | * @return The existing ManagedObjectRef that represents the COM object, or NULL if there's none yet.
|
---|
929 | */
|
---|
930 | ManagedObjectRef* WebServiceSession::findRefFromPtr(const ComPtr<IUnknown> &pcu)
|
---|
931 | {
|
---|
932 | // Assert(g_pSessionsLockHandle->isReadLockOnCurrentThread()); // @todo
|
---|
933 |
|
---|
934 | IUnknown *p = pcu;
|
---|
935 | uintptr_t ulp = (uintptr_t)p;
|
---|
936 | ManagedObjectRef *pRef;
|
---|
937 | // WEBDEBUG((" %s: looking up 0x%lX\n", __FUNCTION__, ulp));
|
---|
938 | ManagedObjectsMapByPtr::iterator it = _pp->_mapManagedObjectsByPtr.find(ulp);
|
---|
939 | if (it != _pp->_mapManagedObjectsByPtr.end())
|
---|
940 | {
|
---|
941 | pRef = it->second;
|
---|
942 | WSDLT_ID id = pRef->toWSDL();
|
---|
943 | WEBDEBUG((" %s: found existing ref %s for COM obj 0x%lX\n", __FUNCTION__, id.c_str(), ulp));
|
---|
944 | }
|
---|
945 | else
|
---|
946 | pRef = NULL;
|
---|
947 | return pRef;
|
---|
948 | }
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * Static method which attempts to find the session for which the given managed
|
---|
952 | * object reference was created, by splitting the reference into the session and
|
---|
953 | * object IDs and then looking up the session object for that session ID.
|
---|
954 | *
|
---|
955 | * Preconditions: Caller must have locked g_pSessionsLockHandle in read mode.
|
---|
956 | *
|
---|
957 | * @param id Managed object reference (with combined session and object IDs).
|
---|
958 | * @return
|
---|
959 | */
|
---|
960 | WebServiceSession* WebServiceSession::findSessionFromRef(const WSDLT_ID &id)
|
---|
961 | {
|
---|
962 | // Assert(g_pSessionsLockHandle->isReadLockOnCurrentThread()); // @todo
|
---|
963 |
|
---|
964 | WebServiceSession *pSession = NULL;
|
---|
965 | uint64_t sessid;
|
---|
966 | if (SplitManagedObjectRef(id,
|
---|
967 | &sessid,
|
---|
968 | NULL))
|
---|
969 | {
|
---|
970 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
971 | if (it != g_mapSessions.end())
|
---|
972 | pSession = it->second;
|
---|
973 | }
|
---|
974 | return pSession;
|
---|
975 | }
|
---|
976 |
|
---|
977 | /**
|
---|
978 | *
|
---|
979 | */
|
---|
980 | WSDLT_ID WebServiceSession::getSessionObject() const
|
---|
981 | {
|
---|
982 | return _pISession->toWSDL();
|
---|
983 | }
|
---|
984 |
|
---|
985 | /**
|
---|
986 | * Touches the webservice session to prevent it from timing out.
|
---|
987 | *
|
---|
988 | * Each webservice session has an internal timestamp that records
|
---|
989 | * the last request made to it from the client that started it.
|
---|
990 | * If no request was made within a configurable timeframe, then
|
---|
991 | * the client is logged off automatically,
|
---|
992 | * by calling IWebsessionManager::logoff()
|
---|
993 | */
|
---|
994 | void WebServiceSession::touch()
|
---|
995 | {
|
---|
996 | time(&_tLastObjectLookup);
|
---|
997 | }
|
---|
998 |
|
---|
999 | /**
|
---|
1000 | *
|
---|
1001 | */
|
---|
1002 | void WebServiceSession::DumpRefs()
|
---|
1003 | {
|
---|
1004 | WEBDEBUG((" dumping object refs:\n"));
|
---|
1005 | ManagedObjectsIteratorById
|
---|
1006 | iter = _pp->_mapManagedObjectsById.begin(),
|
---|
1007 | end = _pp->_mapManagedObjectsById.end();
|
---|
1008 | for (;
|
---|
1009 | iter != end;
|
---|
1010 | ++iter)
|
---|
1011 | {
|
---|
1012 | ManagedObjectRef *pRef = iter->second;
|
---|
1013 | uint64_t id = pRef->getID();
|
---|
1014 | void *p = pRef->getComPtr();
|
---|
1015 | WEBDEBUG((" objid %llX: comptr 0x%lX\n", id, p));
|
---|
1016 | }
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /****************************************************************************
|
---|
1020 | *
|
---|
1021 | * class ManagedObjectRef
|
---|
1022 | *
|
---|
1023 | ****************************************************************************/
|
---|
1024 |
|
---|
1025 | /**
|
---|
1026 | * Constructor, which assigns a unique ID to this managed object
|
---|
1027 | * reference and stores it two global hashes:
|
---|
1028 | *
|
---|
1029 | * a) G_mapManagedObjectsById, which maps ManagedObjectID's to
|
---|
1030 | * instances of this class; this hash is then used by the
|
---|
1031 | * findObjectFromRef() template function in vboxweb.h
|
---|
1032 | * to quickly retrieve the COM object from its managed
|
---|
1033 | * object ID (mostly in the context of the method mappers
|
---|
1034 | * in methodmaps.cpp, when a web service client passes in
|
---|
1035 | * a managed object ID);
|
---|
1036 | *
|
---|
1037 | * b) G_mapManagedObjectsByComPtr, which maps COM pointers to
|
---|
1038 | * instances of this class; this hash is used by
|
---|
1039 | * createRefFromObject() to quickly figure out whether an
|
---|
1040 | * instance already exists for a given COM pointer.
|
---|
1041 | *
|
---|
1042 | * This does _not_ check whether another instance already
|
---|
1043 | * exists in the hash. This gets called only from the
|
---|
1044 | * createRefFromObject() template function in vboxweb.h, which
|
---|
1045 | * does perform that check.
|
---|
1046 | *
|
---|
1047 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1048 | *
|
---|
1049 | * @param pObj
|
---|
1050 | */
|
---|
1051 | ManagedObjectRef::ManagedObjectRef(WebServiceSession &session,
|
---|
1052 | const char *pcszInterface,
|
---|
1053 | const ComPtr<IUnknown> &pc)
|
---|
1054 | : _session(session),
|
---|
1055 | _pObj(pc),
|
---|
1056 | _pcszInterface(pcszInterface)
|
---|
1057 | {
|
---|
1058 | ComPtr<IUnknown> pcUnknown(pc);
|
---|
1059 | _ulp = (uintptr_t)(IUnknown*)pcUnknown;
|
---|
1060 |
|
---|
1061 | _id = ++g_iMaxManagedObjectID;
|
---|
1062 | // and count globally
|
---|
1063 | ULONG64 cTotal = ++g_cManagedObjects; // raise global count and make a copy for the debug message below
|
---|
1064 |
|
---|
1065 | char sz[34];
|
---|
1066 | MakeManagedObjectRef(sz, session._uSessionID, _id);
|
---|
1067 | _strID = sz;
|
---|
1068 |
|
---|
1069 | session._pp->_mapManagedObjectsById[_id] = this;
|
---|
1070 | session._pp->_mapManagedObjectsByPtr[_ulp] = this;
|
---|
1071 |
|
---|
1072 | session.touch();
|
---|
1073 |
|
---|
1074 | WEBDEBUG((" * %s: MOR created for ulp 0x%lX (%s), new ID is %llX; now %lld objects total\n", __FUNCTION__, _ulp, pcszInterface, _id, cTotal));
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /**
|
---|
1078 | * Destructor; removes the instance from the global hash of
|
---|
1079 | * managed objects.
|
---|
1080 | *
|
---|
1081 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1082 | */
|
---|
1083 | ManagedObjectRef::~ManagedObjectRef()
|
---|
1084 | {
|
---|
1085 | ULONG64 cTotal = --g_cManagedObjects;
|
---|
1086 |
|
---|
1087 | WEBDEBUG((" * %s: deleting MOR for ID %llX (%s); now %lld objects total\n", __FUNCTION__, _id, _pcszInterface, cTotal));
|
---|
1088 |
|
---|
1089 | // if we're being destroyed from the session's destructor,
|
---|
1090 | // then that destructor is iterating over the maps, so
|
---|
1091 | // don't remove us there! (data integrity + speed)
|
---|
1092 | if (!_session._fDestructing)
|
---|
1093 | {
|
---|
1094 | WEBDEBUG((" * %s: removing from session maps\n", __FUNCTION__));
|
---|
1095 | _session._pp->_mapManagedObjectsById.erase(_id);
|
---|
1096 | if (_session._pp->_mapManagedObjectsByPtr.erase(_ulp) != 1)
|
---|
1097 | WEBDEBUG((" WARNING: could not find %llX in _mapManagedObjectsByPtr\n", _ulp));
|
---|
1098 | }
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Converts the ID of this managed object reference to string
|
---|
1103 | * form, for returning with SOAP data or similar.
|
---|
1104 | *
|
---|
1105 | * @return The ID in string form.
|
---|
1106 | */
|
---|
1107 | WSDLT_ID ManagedObjectRef::toWSDL() const
|
---|
1108 | {
|
---|
1109 | return _strID;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | /**
|
---|
1113 | * Static helper method for findObjectFromRef() template that actually
|
---|
1114 | * looks up the object from a given integer ID.
|
---|
1115 | *
|
---|
1116 | * This has been extracted into this non-template function to reduce
|
---|
1117 | * code bloat as we have the actual STL map lookup only in this function.
|
---|
1118 | *
|
---|
1119 | * This also "touches" the timestamp in the session whose ID is encoded
|
---|
1120 | * in the given integer ID, in order to prevent the session from timing
|
---|
1121 | * out.
|
---|
1122 | *
|
---|
1123 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1124 | *
|
---|
1125 | * @param strId
|
---|
1126 | * @param iter
|
---|
1127 | * @return
|
---|
1128 | */
|
---|
1129 | int ManagedObjectRef::findRefFromId(const WSDLT_ID &id,
|
---|
1130 | ManagedObjectRef **pRef,
|
---|
1131 | bool fNullAllowed)
|
---|
1132 | {
|
---|
1133 | int rc = 0;
|
---|
1134 |
|
---|
1135 | do
|
---|
1136 | {
|
---|
1137 | // allow NULL (== empty string) input reference, which should return a NULL pointer
|
---|
1138 | if (!id.length() && fNullAllowed)
|
---|
1139 | {
|
---|
1140 | *pRef = NULL;
|
---|
1141 | return 0;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | uint64_t sessid;
|
---|
1145 | uint64_t objid;
|
---|
1146 | WEBDEBUG((" %s(): looking up objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1147 | if (!SplitManagedObjectRef(id,
|
---|
1148 | &sessid,
|
---|
1149 | &objid))
|
---|
1150 | {
|
---|
1151 | rc = VERR_WEB_INVALID_MANAGED_OBJECT_REFERENCE;
|
---|
1152 | break;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | WEBDEBUG((" %s(): sessid %llX, objid %llX\n", __FUNCTION__, sessid, objid));
|
---|
1156 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
1157 | if (it == g_mapSessions.end())
|
---|
1158 | {
|
---|
1159 | WEBDEBUG((" %s: cannot find session for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1160 | rc = VERR_WEB_INVALID_SESSION_ID;
|
---|
1161 | break;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | WebServiceSession *pSess = it->second;
|
---|
1165 | // "touch" session to prevent it from timing out
|
---|
1166 | pSess->touch();
|
---|
1167 |
|
---|
1168 | ManagedObjectsIteratorById iter = pSess->_pp->_mapManagedObjectsById.find(objid);
|
---|
1169 | if (iter == pSess->_pp->_mapManagedObjectsById.end())
|
---|
1170 | {
|
---|
1171 | WEBDEBUG((" %s: cannot find comobj for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1172 | rc = VERR_WEB_INVALID_OBJECT_ID;
|
---|
1173 | break;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | *pRef = iter->second;
|
---|
1177 |
|
---|
1178 | } while (0);
|
---|
1179 |
|
---|
1180 | return rc;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /****************************************************************************
|
---|
1184 | *
|
---|
1185 | * interface IManagedObjectRef
|
---|
1186 | *
|
---|
1187 | ****************************************************************************/
|
---|
1188 |
|
---|
1189 | /**
|
---|
1190 | * This is the hard-coded implementation for the IManagedObjectRef::getInterfaceName()
|
---|
1191 | * that our WSDL promises to our web service clients. This method returns a
|
---|
1192 | * string describing the interface that this managed object reference
|
---|
1193 | * supports, e.g. "IMachine".
|
---|
1194 | *
|
---|
1195 | * @param soap
|
---|
1196 | * @param req
|
---|
1197 | * @param resp
|
---|
1198 | * @return
|
---|
1199 | */
|
---|
1200 | int __vbox__IManagedObjectRef_USCOREgetInterfaceName(
|
---|
1201 | struct soap *soap,
|
---|
1202 | _vbox__IManagedObjectRef_USCOREgetInterfaceName *req,
|
---|
1203 | _vbox__IManagedObjectRef_USCOREgetInterfaceNameResponse *resp)
|
---|
1204 | {
|
---|
1205 | HRESULT rc = SOAP_OK;
|
---|
1206 | WEBDEBUG(("\n-- entering %s\n", __FUNCTION__));
|
---|
1207 |
|
---|
1208 | do {
|
---|
1209 | ManagedObjectRef *pRef;
|
---|
1210 | if (!ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false))
|
---|
1211 | resp->returnval = pRef->getInterfaceName();
|
---|
1212 |
|
---|
1213 | } while (0);
|
---|
1214 |
|
---|
1215 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1216 | if (FAILED(rc))
|
---|
1217 | return SOAP_FAULT;
|
---|
1218 | return SOAP_OK;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | /**
|
---|
1222 | * This is the hard-coded implementation for the IManagedObjectRef::release()
|
---|
1223 | * that our WSDL promises to our web service clients. This method releases
|
---|
1224 | * a managed object reference and removes it from our stacks.
|
---|
1225 | *
|
---|
1226 | * @param soap
|
---|
1227 | * @param req
|
---|
1228 | * @param resp
|
---|
1229 | * @return
|
---|
1230 | */
|
---|
1231 | int __vbox__IManagedObjectRef_USCORErelease(
|
---|
1232 | struct soap *soap,
|
---|
1233 | _vbox__IManagedObjectRef_USCORErelease *req,
|
---|
1234 | _vbox__IManagedObjectRef_USCOREreleaseResponse *resp)
|
---|
1235 | {
|
---|
1236 | HRESULT rc = SOAP_OK;
|
---|
1237 | WEBDEBUG(("\n-- entering %s\n", __FUNCTION__));
|
---|
1238 |
|
---|
1239 | do {
|
---|
1240 | ManagedObjectRef *pRef;
|
---|
1241 | if ((rc = ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false)))
|
---|
1242 | {
|
---|
1243 | RaiseSoapInvalidObjectFault(soap, req->_USCOREthis);
|
---|
1244 | break;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | WEBDEBUG((" found reference; deleting!\n"));
|
---|
1248 | delete pRef;
|
---|
1249 | // this removes the object from all stacks; since
|
---|
1250 | // there's a ComPtr<> hidden inside the reference,
|
---|
1251 | // this should also invoke Release() on the COM
|
---|
1252 | // object
|
---|
1253 | } while (0);
|
---|
1254 |
|
---|
1255 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1256 | if (FAILED(rc))
|
---|
1257 | return SOAP_FAULT;
|
---|
1258 | return SOAP_OK;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | /****************************************************************************
|
---|
1262 | *
|
---|
1263 | * interface IWebsessionManager
|
---|
1264 | *
|
---|
1265 | ****************************************************************************/
|
---|
1266 |
|
---|
1267 | /**
|
---|
1268 | * Hard-coded implementation for IWebsessionManager::logon. As opposed to the underlying
|
---|
1269 | * COM API, this is the first method that a webservice client must call before the
|
---|
1270 | * webservice will do anything useful.
|
---|
1271 | *
|
---|
1272 | * This returns a managed object reference to the global IVirtualBox object; into this
|
---|
1273 | * reference a session ID is encoded which remains constant with all managed object
|
---|
1274 | * references returned by other methods.
|
---|
1275 | *
|
---|
1276 | * This also creates an instance of ISession, which is stored internally with the
|
---|
1277 | * webservice session and can be retrieved with IWebsessionManager::getSessionObject
|
---|
1278 | * (__vbox__IWebsessionManager_USCOREgetSessionObject). In order for the
|
---|
1279 | * VirtualBox web service to do anything useful, one usually needs both a
|
---|
1280 | * VirtualBox and an ISession object, for which these two methods are designed.
|
---|
1281 | *
|
---|
1282 | * When the webservice client is done, it should call IWebsessionManager::logoff. This
|
---|
1283 | * will clean up internally (destroy all remaining managed object references and
|
---|
1284 | * related COM objects used internally).
|
---|
1285 | *
|
---|
1286 | * After logon, an internal timeout ensures that if the webservice client does not
|
---|
1287 | * call any methods, after a configurable number of seconds, the webservice will log
|
---|
1288 | * off the client automatically. This is to ensure that the webservice does not
|
---|
1289 | * drown in managed object references and eventually deny service. Still, it is
|
---|
1290 | * a much better solution, both for performance and cleanliness, for the webservice
|
---|
1291 | * client to clean up itself.
|
---|
1292 | *
|
---|
1293 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1294 | * Since this gets called from main() like other SOAP method
|
---|
1295 | * implementations, this is ensured.
|
---|
1296 | *
|
---|
1297 | * @param
|
---|
1298 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1299 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1300 | * @return
|
---|
1301 | */
|
---|
1302 | int __vbox__IWebsessionManager_USCORElogon(
|
---|
1303 | struct soap*,
|
---|
1304 | _vbox__IWebsessionManager_USCORElogon *req,
|
---|
1305 | _vbox__IWebsessionManager_USCORElogonResponse *resp)
|
---|
1306 | {
|
---|
1307 | HRESULT rc = SOAP_OK;
|
---|
1308 | WEBDEBUG(("\n-- entering %s\n", __FUNCTION__));
|
---|
1309 |
|
---|
1310 | do {
|
---|
1311 | // WebServiceSession constructor tinkers with global MOR map and requires a write lock
|
---|
1312 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1313 |
|
---|
1314 | // create new session; the constructor stores the new session
|
---|
1315 | // in the global map automatically
|
---|
1316 | WebServiceSession *pSession = new WebServiceSession();
|
---|
1317 |
|
---|
1318 | // authenticate the user
|
---|
1319 | if (!(pSession->authenticate(req->username.c_str(),
|
---|
1320 | req->password.c_str())))
|
---|
1321 | {
|
---|
1322 | // in the new session, create a managed object reference (moref) for the
|
---|
1323 | // global VirtualBox object; this encodes the session ID in the moref so
|
---|
1324 | // that it will be implicitly be included in all future requests of this
|
---|
1325 | // webservice client
|
---|
1326 | ManagedObjectRef *pRef = new ManagedObjectRef(*pSession, g_pcszIVirtualBox, g_pVirtualBox);
|
---|
1327 | resp->returnval = pRef->toWSDL();
|
---|
1328 | WEBDEBUG(("VirtualBox object ref is %s\n", resp->returnval.c_str()));
|
---|
1329 | }
|
---|
1330 | } while (0);
|
---|
1331 |
|
---|
1332 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1333 | if (FAILED(rc))
|
---|
1334 | return SOAP_FAULT;
|
---|
1335 | return SOAP_OK;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /**
|
---|
1339 | * Returns the ISession object that was created for the webservice client
|
---|
1340 | * on logon.
|
---|
1341 | *
|
---|
1342 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1343 | * Since this gets called from main() like other SOAP method
|
---|
1344 | * implementations, this is ensured.
|
---|
1345 | */
|
---|
1346 | int __vbox__IWebsessionManager_USCOREgetSessionObject(
|
---|
1347 | struct soap*,
|
---|
1348 | _vbox__IWebsessionManager_USCOREgetSessionObject *req,
|
---|
1349 | _vbox__IWebsessionManager_USCOREgetSessionObjectResponse *resp)
|
---|
1350 | {
|
---|
1351 | HRESULT rc = SOAP_OK;
|
---|
1352 | WEBDEBUG(("\n-- entering %s\n", __FUNCTION__));
|
---|
1353 |
|
---|
1354 | do {
|
---|
1355 | WebServiceSession* pSession;
|
---|
1356 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1357 | {
|
---|
1358 | resp->returnval = pSession->getSessionObject();
|
---|
1359 | }
|
---|
1360 | } while (0);
|
---|
1361 |
|
---|
1362 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1363 | if (FAILED(rc))
|
---|
1364 | return SOAP_FAULT;
|
---|
1365 | return SOAP_OK;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | /**
|
---|
1369 | * hard-coded implementation for IWebsessionManager::logoff.
|
---|
1370 | *
|
---|
1371 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1372 | * Since this gets called from main() like other SOAP method
|
---|
1373 | * implementations, this is ensured.
|
---|
1374 | *
|
---|
1375 | * @param
|
---|
1376 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1377 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1378 | * @return
|
---|
1379 | */
|
---|
1380 | int __vbox__IWebsessionManager_USCORElogoff(
|
---|
1381 | struct soap*,
|
---|
1382 | _vbox__IWebsessionManager_USCORElogoff *req,
|
---|
1383 | _vbox__IWebsessionManager_USCORElogoffResponse *resp)
|
---|
1384 | {
|
---|
1385 | HRESULT rc = SOAP_OK;
|
---|
1386 | WEBDEBUG(("\n-- entering %s\n", __FUNCTION__));
|
---|
1387 |
|
---|
1388 | do {
|
---|
1389 | WebServiceSession* pSession;
|
---|
1390 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1391 | {
|
---|
1392 | delete pSession;
|
---|
1393 | // destructor cleans up
|
---|
1394 |
|
---|
1395 | WEBDEBUG(("session destroyed, %d sessions left open\n", g_mapSessions.size()));
|
---|
1396 | }
|
---|
1397 | } while (0);
|
---|
1398 |
|
---|
1399 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1400 | if (FAILED(rc))
|
---|
1401 | return SOAP_FAULT;
|
---|
1402 | return SOAP_OK;
|
---|
1403 | }
|
---|