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 Oracle Corporation
|
---|
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 |
|
---|
19 | // shared webservice header
|
---|
20 | #include "vboxweb.h"
|
---|
21 |
|
---|
22 | // vbox headers
|
---|
23 | #include <VBox/com/com.h>
|
---|
24 | #include <VBox/com/ErrorInfo.h>
|
---|
25 | #include <VBox/com/errorprint.h>
|
---|
26 | #include <VBox/com/EventQueue.h>
|
---|
27 | #include <VBox/VBoxAuth.h>
|
---|
28 | #include <VBox/version.h>
|
---|
29 |
|
---|
30 | #include <iprt/buildconfig.h>
|
---|
31 | #include <iprt/ctype.h>
|
---|
32 | #include <iprt/getopt.h>
|
---|
33 | #include <iprt/initterm.h>
|
---|
34 | #include <iprt/ldr.h>
|
---|
35 | #include <iprt/message.h>
|
---|
36 | #include <iprt/process.h>
|
---|
37 | #include <iprt/rand.h>
|
---|
38 | #include <iprt/semaphore.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/thread.h>
|
---|
41 | #include <iprt/time.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 | #include <list>
|
---|
54 |
|
---|
55 | #ifdef __GNUC__
|
---|
56 | #pragma GCC visibility pop
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | // include generated namespaces table
|
---|
60 | #include "vboxwebsrv.nsmap"
|
---|
61 |
|
---|
62 | /****************************************************************************
|
---|
63 | *
|
---|
64 | * private typedefs
|
---|
65 | *
|
---|
66 | ****************************************************************************/
|
---|
67 |
|
---|
68 | typedef std::map<uint64_t, ManagedObjectRef*>
|
---|
69 | ManagedObjectsMapById;
|
---|
70 | typedef std::map<uint64_t, ManagedObjectRef*>::iterator
|
---|
71 | ManagedObjectsIteratorById;
|
---|
72 | typedef std::map<uintptr_t, ManagedObjectRef*>
|
---|
73 | ManagedObjectsMapByPtr;
|
---|
74 |
|
---|
75 | typedef std::map<uint64_t, WebServiceSession*>
|
---|
76 | SessionsMap;
|
---|
77 | typedef std::map<uint64_t, WebServiceSession*>::iterator
|
---|
78 | SessionsMapIterator;
|
---|
79 |
|
---|
80 | int fntWatchdog(RTTHREAD ThreadSelf, void *pvUser);
|
---|
81 |
|
---|
82 | /****************************************************************************
|
---|
83 | *
|
---|
84 | * Read-only global variables
|
---|
85 | *
|
---|
86 | ****************************************************************************/
|
---|
87 |
|
---|
88 | ComPtr<IVirtualBox> g_pVirtualBox = NULL;
|
---|
89 |
|
---|
90 | // generated strings in methodmaps.cpp
|
---|
91 | extern const char *g_pcszISession,
|
---|
92 | *g_pcszIVirtualBox;
|
---|
93 |
|
---|
94 | // globals for vboxweb command-line arguments
|
---|
95 | #define DEFAULT_TIMEOUT_SECS 300
|
---|
96 | #define DEFAULT_TIMEOUT_SECS_STRING "300"
|
---|
97 | int g_iWatchdogTimeoutSecs = DEFAULT_TIMEOUT_SECS;
|
---|
98 | int g_iWatchdogCheckInterval = 5;
|
---|
99 |
|
---|
100 | const char *g_pcszBindToHost = NULL; // host; NULL = current machine
|
---|
101 | unsigned int g_uBindToPort = 18083; // port
|
---|
102 | unsigned int g_uBacklog = 100; // backlog = max queue size for requests
|
---|
103 | unsigned int g_cMaxWorkerThreads = 100; // max. no. of worker threads
|
---|
104 | unsigned int g_cMaxKeepAlive = 100; // maximum number of soap requests in one connection
|
---|
105 |
|
---|
106 | bool g_fVerbose = false; // be verbose
|
---|
107 | PRTSTREAM g_pStrmLog = NULL;
|
---|
108 |
|
---|
109 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
110 | bool g_fDaemonize = false; // run in background.
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | const WSDLT_ID g_EmptyWSDLID; // for NULL MORs
|
---|
114 |
|
---|
115 | /****************************************************************************
|
---|
116 | *
|
---|
117 | * Writeable global variables
|
---|
118 | *
|
---|
119 | ****************************************************************************/
|
---|
120 |
|
---|
121 | // The one global SOAP queue created by main().
|
---|
122 | class SoapQ;
|
---|
123 | SoapQ *g_pSoapQ = NULL;
|
---|
124 |
|
---|
125 | // this mutex protects the auth lib and authentication
|
---|
126 | util::WriteLockHandle *g_pAuthLibLockHandle;
|
---|
127 |
|
---|
128 | // this mutex protects all of the below
|
---|
129 | util::WriteLockHandle *g_pSessionsLockHandle;
|
---|
130 |
|
---|
131 | SessionsMap g_mapSessions;
|
---|
132 | ULONG64 g_iMaxManagedObjectID = 0;
|
---|
133 | ULONG64 g_cManagedObjects = 0;
|
---|
134 |
|
---|
135 | // this mutex protects g_mapThreads
|
---|
136 | util::RWLockHandle *g_pThreadsLockHandle;
|
---|
137 |
|
---|
138 | // this mutex synchronizes logging
|
---|
139 | util::WriteLockHandle *g_pWebLogLockHandle;
|
---|
140 |
|
---|
141 | // Threads map, so we can quickly map an RTTHREAD struct to a logger prefix
|
---|
142 | typedef std::map<RTTHREAD, com::Utf8Str> ThreadsMap;
|
---|
143 | ThreadsMap g_mapThreads;
|
---|
144 |
|
---|
145 | /****************************************************************************
|
---|
146 | *
|
---|
147 | * Command line help
|
---|
148 | *
|
---|
149 | ****************************************************************************/
|
---|
150 |
|
---|
151 | static const RTGETOPTDEF g_aOptions[]
|
---|
152 | = {
|
---|
153 | { "--help", 'h', RTGETOPT_REQ_NOTHING }, /* for DisplayHelp() */
|
---|
154 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
155 | { "--background", 'b', RTGETOPT_REQ_NOTHING },
|
---|
156 | #endif
|
---|
157 | { "--host", 'H', RTGETOPT_REQ_STRING },
|
---|
158 | { "--port", 'p', RTGETOPT_REQ_UINT32 },
|
---|
159 | { "--timeout", 't', RTGETOPT_REQ_UINT32 },
|
---|
160 | { "--check-interval", 'i', RTGETOPT_REQ_UINT32 },
|
---|
161 | { "--threads", 'T', RTGETOPT_REQ_UINT32 },
|
---|
162 | { "--keepalive", 'k', RTGETOPT_REQ_UINT32 },
|
---|
163 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
164 | { "--pidfile", 'P', RTGETOPT_REQ_STRING },
|
---|
165 | { "--logfile", 'F', RTGETOPT_REQ_STRING },
|
---|
166 | };
|
---|
167 |
|
---|
168 | void DisplayHelp()
|
---|
169 | {
|
---|
170 | RTStrmPrintf(g_pStdErr, "\nUsage: vboxwebsrv [options]\n\nSupported options (default values in brackets):\n");
|
---|
171 | for (unsigned i = 0;
|
---|
172 | i < RT_ELEMENTS(g_aOptions);
|
---|
173 | ++i)
|
---|
174 | {
|
---|
175 | std::string str(g_aOptions[i].pszLong);
|
---|
176 | str += ", -";
|
---|
177 | str += g_aOptions[i].iShort;
|
---|
178 | str += ":";
|
---|
179 |
|
---|
180 | const char *pcszDescr = "";
|
---|
181 |
|
---|
182 | switch (g_aOptions[i].iShort)
|
---|
183 | {
|
---|
184 | case 'h':
|
---|
185 | pcszDescr = "Print this help message and exit.";
|
---|
186 | break;
|
---|
187 |
|
---|
188 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
189 | case 'b':
|
---|
190 | pcszDescr = "Run in background (daemon mode).";
|
---|
191 | break;
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | case 'H':
|
---|
195 | pcszDescr = "The host to bind to (localhost).";
|
---|
196 | break;
|
---|
197 |
|
---|
198 | case 'p':
|
---|
199 | pcszDescr = "The port to bind to (18083).";
|
---|
200 | break;
|
---|
201 |
|
---|
202 | case 't':
|
---|
203 | pcszDescr = "Session timeout in seconds; 0 = disable timeouts (" DEFAULT_TIMEOUT_SECS_STRING ").";
|
---|
204 | break;
|
---|
205 |
|
---|
206 | case 'T':
|
---|
207 | pcszDescr = "Maximum number of worker threads to run in parallel (100).";
|
---|
208 | break;
|
---|
209 |
|
---|
210 | case 'k':
|
---|
211 | pcszDescr = "Maximum number of requests before a socket will be closed (100).";
|
---|
212 | break;
|
---|
213 |
|
---|
214 | case 'i':
|
---|
215 | pcszDescr = "Frequency of timeout checks in seconds (5).";
|
---|
216 | break;
|
---|
217 |
|
---|
218 | case 'v':
|
---|
219 | pcszDescr = "Be verbose.";
|
---|
220 | break;
|
---|
221 |
|
---|
222 | case 'P':
|
---|
223 | pcszDescr = "Name of the PID file which is created when the daemon was started.";
|
---|
224 | break;
|
---|
225 |
|
---|
226 | case 'F':
|
---|
227 | pcszDescr = "Name of file to write log to (no file).";
|
---|
228 | break;
|
---|
229 | }
|
---|
230 |
|
---|
231 | RTStrmPrintf(g_pStdErr, "%-23s%s\n", str.c_str(), pcszDescr);
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | /****************************************************************************
|
---|
236 | *
|
---|
237 | * SoapQ, SoapThread (multithreading)
|
---|
238 | *
|
---|
239 | ****************************************************************************/
|
---|
240 |
|
---|
241 | class SoapQ;
|
---|
242 |
|
---|
243 | class SoapThread
|
---|
244 | {
|
---|
245 | public:
|
---|
246 | /**
|
---|
247 | * Constructor. Creates the new thread and makes it call process() for processing the queue.
|
---|
248 | * @param u Thread number. (So we can count from 1 and be readable.)
|
---|
249 | * @param q SoapQ instance which has the queue to process.
|
---|
250 | * @param soap struct soap instance from main() which we copy here.
|
---|
251 | */
|
---|
252 | SoapThread(size_t u,
|
---|
253 | SoapQ &q,
|
---|
254 | const struct soap *soap)
|
---|
255 | : m_u(u),
|
---|
256 | m_strThread(com::Utf8StrFmt("SoapQWrk%02d", m_u)),
|
---|
257 | m_pQ(&q)
|
---|
258 | {
|
---|
259 | // make a copy of the soap struct for the new thread
|
---|
260 | m_soap = soap_copy(soap);
|
---|
261 |
|
---|
262 | /* The soap.max_keep_alive value can be set to the maximum keep-alive calls allowed,
|
---|
263 | * which is important to avoid a client from holding a thread indefinitely.
|
---|
264 | * http://www.cs.fsu.edu/~engelen/soapdoc2.html#sec:keepalive
|
---|
265 | *
|
---|
266 | * Strings with 8-bit content can hold ASCII (default) or UTF8. The latter is
|
---|
267 | * possible by enabling the SOAP_C_UTFSTRING flag.
|
---|
268 | */
|
---|
269 | soap_set_omode(m_soap, SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING);
|
---|
270 | soap_set_imode(m_soap, SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING);
|
---|
271 | m_soap->max_keep_alive = g_cMaxKeepAlive;
|
---|
272 |
|
---|
273 | int rc = RTThreadCreate(&m_pThread,
|
---|
274 | fntWrapper,
|
---|
275 | this, // pvUser
|
---|
276 | 0, // cbStack,
|
---|
277 | RTTHREADTYPE_MAIN_HEAVY_WORKER,
|
---|
278 | 0,
|
---|
279 | m_strThread.c_str());
|
---|
280 | if (RT_FAILURE(rc))
|
---|
281 | {
|
---|
282 | RTMsgError("Cannot start worker thread %d: %Rrc\n", u, rc);
|
---|
283 | exit(1);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | void process();
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Static function that can be passed to RTThreadCreate and that calls
|
---|
291 | * process() on the SoapThread instance passed as the thread parameter.
|
---|
292 | * @param pThread
|
---|
293 | * @param pvThread
|
---|
294 | * @return
|
---|
295 | */
|
---|
296 | static int fntWrapper(RTTHREAD pThread, void *pvThread)
|
---|
297 | {
|
---|
298 | SoapThread *pst = (SoapThread*)pvThread;
|
---|
299 | pst->process(); // this never returns really
|
---|
300 | return 0;
|
---|
301 | }
|
---|
302 |
|
---|
303 | size_t m_u; // thread number
|
---|
304 | com::Utf8Str m_strThread; // thread name ("SoapQWrkXX")
|
---|
305 | SoapQ *m_pQ; // the single SOAP queue that all the threads service
|
---|
306 | struct soap *m_soap; // copy of the soap structure for this thread (from soap_copy())
|
---|
307 | RTTHREAD m_pThread; // IPRT thread struct for this thread
|
---|
308 | };
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * SOAP queue encapsulation. There is only one instance of this, to
|
---|
312 | * which add() adds a queue item (called on the main thread),
|
---|
313 | * and from which get() fetch items, called from each queue thread.
|
---|
314 | */
|
---|
315 | class SoapQ
|
---|
316 | {
|
---|
317 | public:
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Constructor. Creates the soap queue.
|
---|
321 | * @param pSoap
|
---|
322 | */
|
---|
323 | SoapQ(const struct soap *pSoap)
|
---|
324 | : m_soap(pSoap),
|
---|
325 | m_mutex(util::LOCKCLASS_OBJECTSTATE), // lowest lock order, no other may be held while this is held
|
---|
326 | m_cIdleThreads(0)
|
---|
327 | {
|
---|
328 | RTSemEventMultiCreate(&m_event);
|
---|
329 | }
|
---|
330 |
|
---|
331 | ~SoapQ()
|
---|
332 | {
|
---|
333 | RTSemEventMultiDestroy(m_event);
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Adds the given socket to the SOAP queue and posts the
|
---|
338 | * member event sem to wake up the workers. Called on the main thread
|
---|
339 | * whenever a socket has work to do. Creates a new SOAP thread on the
|
---|
340 | * first call or when all existing threads are busy.
|
---|
341 | * @param s Socket from soap_accept() which has work to do.
|
---|
342 | */
|
---|
343 | uint32_t add(int s)
|
---|
344 | {
|
---|
345 | uint32_t cItems;
|
---|
346 | util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);
|
---|
347 |
|
---|
348 | // if no threads have yet been created, or if all threads are busy,
|
---|
349 | // create a new SOAP thread
|
---|
350 | if ( !m_cIdleThreads
|
---|
351 | // but only if we're not exceeding the global maximum (default is 100)
|
---|
352 | && (m_llAllThreads.size() < g_cMaxWorkerThreads)
|
---|
353 | )
|
---|
354 | {
|
---|
355 | SoapThread *pst = new SoapThread(m_llAllThreads.size() + 1,
|
---|
356 | *this,
|
---|
357 | m_soap);
|
---|
358 | m_llAllThreads.push_back(pst);
|
---|
359 | util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
360 | g_mapThreads[pst->m_pThread] = com::Utf8StrFmt("[%3u]", pst->m_u);
|
---|
361 | ++m_cIdleThreads;
|
---|
362 | }
|
---|
363 |
|
---|
364 | // enqueue the socket of this connection and post eventsem so that
|
---|
365 | // one of the threads (possibly the one just created) can pick it up
|
---|
366 | m_llSocketsQ.push_back(s);
|
---|
367 | cItems = m_llSocketsQ.size();
|
---|
368 | qlock.release();
|
---|
369 |
|
---|
370 | // unblock one of the worker threads
|
---|
371 | RTSemEventMultiSignal(m_event);
|
---|
372 |
|
---|
373 | return cItems;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Blocks the current thread until work comes in; then returns
|
---|
378 | * the SOAP socket which has work to do. This reduces m_cIdleThreads
|
---|
379 | * by one, and the caller MUST call done() when it's done processing.
|
---|
380 | * Called from the worker threads.
|
---|
381 | * @param cIdleThreads out: no. of threads which are currently idle (not counting the caller)
|
---|
382 | * @param cThreads out: total no. of SOAP threads running
|
---|
383 | * @return
|
---|
384 | */
|
---|
385 | int get(size_t &cIdleThreads, size_t &cThreads)
|
---|
386 | {
|
---|
387 | while (1)
|
---|
388 | {
|
---|
389 | // wait for something to happen
|
---|
390 | RTSemEventMultiWait(m_event, RT_INDEFINITE_WAIT);
|
---|
391 |
|
---|
392 | util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);
|
---|
393 | if (m_llSocketsQ.size())
|
---|
394 | {
|
---|
395 | int socket = m_llSocketsQ.front();
|
---|
396 | m_llSocketsQ.pop_front();
|
---|
397 | cIdleThreads = --m_cIdleThreads;
|
---|
398 | cThreads = m_llAllThreads.size();
|
---|
399 |
|
---|
400 | // reset the multi event only if the queue is now empty; otherwise
|
---|
401 | // another thread will also wake up when we release the mutex and
|
---|
402 | // process another one
|
---|
403 | if (m_llSocketsQ.size() == 0)
|
---|
404 | RTSemEventMultiReset(m_event);
|
---|
405 |
|
---|
406 | qlock.release();
|
---|
407 |
|
---|
408 | return socket;
|
---|
409 | }
|
---|
410 |
|
---|
411 | // nothing to do: keep looping
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * To be called by a worker thread after fetching an item from the
|
---|
417 | * queue via get() and having finished its lengthy processing.
|
---|
418 | */
|
---|
419 | void done()
|
---|
420 | {
|
---|
421 | util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);
|
---|
422 | ++m_cIdleThreads;
|
---|
423 | }
|
---|
424 |
|
---|
425 | const struct soap *m_soap; // soap structure created by main(), passed to constructor
|
---|
426 |
|
---|
427 | util::WriteLockHandle m_mutex;
|
---|
428 | RTSEMEVENTMULTI m_event; // posted by add(), blocked on by get()
|
---|
429 |
|
---|
430 | std::list<SoapThread*> m_llAllThreads; // all the threads created by the constructor
|
---|
431 | size_t m_cIdleThreads; // threads which are currently idle (statistics)
|
---|
432 |
|
---|
433 | // A std::list abused as a queue; this contains the actual jobs to do,
|
---|
434 | // each int being a socket from soap_accept()
|
---|
435 | std::list<int> m_llSocketsQ;
|
---|
436 | };
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * Thread function for each of the SOAP queue worker threads. This keeps
|
---|
440 | * running, blocks on the event semaphore in SoapThread.SoapQ and picks
|
---|
441 | * up a socket from the queue therein, which has been put there by
|
---|
442 | * beginProcessing().
|
---|
443 | */
|
---|
444 | void SoapThread::process()
|
---|
445 | {
|
---|
446 | WebLog("New SOAP thread started\n");
|
---|
447 |
|
---|
448 | while (1)
|
---|
449 | {
|
---|
450 | // wait for a socket to arrive on the queue
|
---|
451 | size_t cIdleThreads = 0, cThreads = 0;
|
---|
452 | m_soap->socket = m_pQ->get(cIdleThreads, cThreads);
|
---|
453 |
|
---|
454 | WebLog("Processing connection from IP=%lu.%lu.%lu.%lu socket=%d (%d out of %d threads idle)\n",
|
---|
455 | (m_soap->ip >> 24) & 0xFF,
|
---|
456 | (m_soap->ip >> 16) & 0xFF,
|
---|
457 | (m_soap->ip >> 8) & 0xFF,
|
---|
458 | m_soap->ip & 0xFF,
|
---|
459 | m_soap->socket,
|
---|
460 | cIdleThreads,
|
---|
461 | cThreads);
|
---|
462 |
|
---|
463 | // process the request; this goes into the COM code in methodmaps.cpp
|
---|
464 | soap_serve(m_soap);
|
---|
465 |
|
---|
466 | soap_destroy(m_soap); // clean up class instances
|
---|
467 | soap_end(m_soap); // clean up everything and close socket
|
---|
468 |
|
---|
469 | // tell the queue we're idle again
|
---|
470 | m_pQ->done();
|
---|
471 | }
|
---|
472 | }
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Implementation for WEBLOG macro defined in vboxweb.h; this prints a message
|
---|
476 | * to the console and optionally to the file that may have been given to the
|
---|
477 | * vboxwebsrv command line.
|
---|
478 | * @param pszFormat
|
---|
479 | */
|
---|
480 | void WebLog(const char *pszFormat, ...)
|
---|
481 | {
|
---|
482 | va_list args;
|
---|
483 | va_start(args, pszFormat);
|
---|
484 | char *psz = NULL;
|
---|
485 | RTStrAPrintfV(&psz, pszFormat, args);
|
---|
486 | va_end(args);
|
---|
487 |
|
---|
488 | const char *pcszPrefix = "[ ]";
|
---|
489 | util::AutoReadLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
490 | ThreadsMap::iterator it = g_mapThreads.find(RTThreadSelf());
|
---|
491 | if (it != g_mapThreads.end())
|
---|
492 | pcszPrefix = it->second.c_str();
|
---|
493 | thrLock.release();
|
---|
494 |
|
---|
495 | // make a timestamp
|
---|
496 | RTTIMESPEC ts;
|
---|
497 | RTTimeLocalNow(&ts);
|
---|
498 | RTTIME t;
|
---|
499 | RTTimeExplode(&t, &ts);
|
---|
500 |
|
---|
501 | com::Utf8StrFmt strPrefix("%04d-%02d-%02d %02d:%02d:%02d %s",
|
---|
502 | t.i32Year, t.u8Month, t.u8MonthDay,
|
---|
503 | t.u8Hour, t.u8Minute, t.u8Second,
|
---|
504 | pcszPrefix);
|
---|
505 |
|
---|
506 | // synchronize the actual output
|
---|
507 | util::AutoWriteLock logLock(g_pWebLogLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
508 | // terminal
|
---|
509 | RTPrintf("%s %s", strPrefix.c_str(), psz);
|
---|
510 |
|
---|
511 | // log file
|
---|
512 | if (g_pStrmLog)
|
---|
513 | {
|
---|
514 | RTStrmPrintf(g_pStrmLog, "%s %s", strPrefix.c_str(), psz);
|
---|
515 | RTStrmFlush(g_pStrmLog);
|
---|
516 | }
|
---|
517 |
|
---|
518 | #ifdef DEBUG
|
---|
519 | // logger instance
|
---|
520 | RTLogLoggerEx(LOG_INSTANCE, RTLOGGRPFLAGS_DJ, LOG_GROUP, "%s %s", pcszPrefix, psz);
|
---|
521 | #endif
|
---|
522 | logLock.release();
|
---|
523 |
|
---|
524 | RTStrFree(psz);
|
---|
525 | }
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * Helper for printing SOAP error messages.
|
---|
529 | * @param soap
|
---|
530 | */
|
---|
531 | void WebLogSoapError(struct soap *soap)
|
---|
532 | {
|
---|
533 | if (soap_check_state(soap))
|
---|
534 | {
|
---|
535 | WebLog("Error: soap struct not initialized\n");
|
---|
536 | return;
|
---|
537 | }
|
---|
538 |
|
---|
539 | const char *pcszFaultString = *soap_faultstring(soap);
|
---|
540 | const char **ppcszDetail = soap_faultcode(soap);
|
---|
541 | WebLog("#### SOAP FAULT: %s [%s]\n",
|
---|
542 | pcszFaultString ? pcszFaultString : "[no fault string available]",
|
---|
543 | (ppcszDetail && *ppcszDetail) ? *ppcszDetail : "no details available");
|
---|
544 | }
|
---|
545 |
|
---|
546 | /****************************************************************************
|
---|
547 | *
|
---|
548 | * SOAP queue pumper thread
|
---|
549 | *
|
---|
550 | ****************************************************************************/
|
---|
551 |
|
---|
552 | void doQueuesLoop()
|
---|
553 | {
|
---|
554 | // set up gSOAP
|
---|
555 | struct soap soap;
|
---|
556 | soap_init(&soap);
|
---|
557 |
|
---|
558 | soap.bind_flags |= SO_REUSEADDR;
|
---|
559 | // avoid EADDRINUSE on bind()
|
---|
560 |
|
---|
561 | int m, s; // master and slave sockets
|
---|
562 | m = soap_bind(&soap,
|
---|
563 | g_pcszBindToHost, // host: current machine
|
---|
564 | g_uBindToPort, // port
|
---|
565 | g_uBacklog); // backlog = max queue size for requests
|
---|
566 | if (m < 0)
|
---|
567 | WebLogSoapError(&soap);
|
---|
568 | else
|
---|
569 | {
|
---|
570 | WebLog("Socket connection successful: host = %s, port = %u, master socket = %d\n",
|
---|
571 | (g_pcszBindToHost) ? g_pcszBindToHost : "default (localhost)",
|
---|
572 | g_uBindToPort,
|
---|
573 | m);
|
---|
574 |
|
---|
575 | // initialize thread queue, mutex and eventsem
|
---|
576 | g_pSoapQ = new SoapQ(&soap);
|
---|
577 |
|
---|
578 | for (uint64_t i = 1;
|
---|
579 | ;
|
---|
580 | i++)
|
---|
581 | {
|
---|
582 | // call gSOAP to handle incoming SOAP connection
|
---|
583 | s = soap_accept(&soap);
|
---|
584 | if (s < 0)
|
---|
585 | {
|
---|
586 | WebLogSoapError(&soap);
|
---|
587 | break;
|
---|
588 | }
|
---|
589 |
|
---|
590 | // add the socket to the queue and tell worker threads to
|
---|
591 | // pick up the jobn
|
---|
592 | size_t cItemsOnQ = g_pSoapQ->add(s);
|
---|
593 | WebLog("Request %llu on socket %d queued for processing (%d items on Q)\n", i, s, cItemsOnQ);
|
---|
594 | }
|
---|
595 | }
|
---|
596 | soap_done(&soap); // close master socket and detach environment
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Thread function for the "queue pumper" thread started from main(). This implements
|
---|
601 | * the loop that takes SOAP calls from HTTP and serves them by handing sockets to the
|
---|
602 | * SOAP queue worker threads.
|
---|
603 | */
|
---|
604 | int fntQPumper(RTTHREAD ThreadSelf, void *pvUser)
|
---|
605 | {
|
---|
606 | // store a log prefix for this thread
|
---|
607 | util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
608 | g_mapThreads[RTThreadSelf()] = "[ P ]";
|
---|
609 | thrLock.release();
|
---|
610 |
|
---|
611 | doQueuesLoop();
|
---|
612 |
|
---|
613 | return 0;
|
---|
614 | }
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Start up the webservice server. This keeps running and waits
|
---|
618 | * for incoming SOAP connections; for each request that comes in,
|
---|
619 | * it calls method implementation code, most of it in the generated
|
---|
620 | * code in methodmaps.cpp.
|
---|
621 | *
|
---|
622 | * @param argc
|
---|
623 | * @param argv[]
|
---|
624 | * @return
|
---|
625 | */
|
---|
626 | int main(int argc, char* argv[])
|
---|
627 | {
|
---|
628 | // initialize runtime
|
---|
629 | int rc = RTR3Init();
|
---|
630 | if (RT_FAILURE(rc))
|
---|
631 | return RTMsgInitFailure(rc);
|
---|
632 |
|
---|
633 | // store a log prefix for this thread
|
---|
634 | g_mapThreads[RTThreadSelf()] = "[M ]";
|
---|
635 |
|
---|
636 | RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " web service version " VBOX_VERSION_STRING "\n"
|
---|
637 | "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
638 | "All rights reserved.\n");
|
---|
639 |
|
---|
640 | int c;
|
---|
641 | const char *pszPidFile = NULL;
|
---|
642 | RTGETOPTUNION ValueUnion;
|
---|
643 | RTGETOPTSTATE GetState;
|
---|
644 | RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0 /*fFlags*/);
|
---|
645 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
646 | {
|
---|
647 | switch (c)
|
---|
648 | {
|
---|
649 | case 'H':
|
---|
650 | g_pcszBindToHost = ValueUnion.psz;
|
---|
651 | break;
|
---|
652 |
|
---|
653 | case 'p':
|
---|
654 | g_uBindToPort = ValueUnion.u32;
|
---|
655 | break;
|
---|
656 |
|
---|
657 | case 't':
|
---|
658 | g_iWatchdogTimeoutSecs = ValueUnion.u32;
|
---|
659 | break;
|
---|
660 |
|
---|
661 | case 'i':
|
---|
662 | g_iWatchdogCheckInterval = ValueUnion.u32;
|
---|
663 | break;
|
---|
664 |
|
---|
665 | case 'F':
|
---|
666 | {
|
---|
667 | int rc2 = RTStrmOpen(ValueUnion.psz, "a", &g_pStrmLog);
|
---|
668 | if (rc2)
|
---|
669 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot open log file \"%s\" for writing: %Rrc", ValueUnion.psz, rc2);
|
---|
670 |
|
---|
671 | WebLog(VBOX_PRODUCT " Webservice Version %s\n"
|
---|
672 | "Opened log file \"%s\"\n", VBOX_VERSION_STRING, ValueUnion.psz);
|
---|
673 | break;
|
---|
674 | }
|
---|
675 |
|
---|
676 | case 'P':
|
---|
677 | pszPidFile = ValueUnion.psz;
|
---|
678 | break;
|
---|
679 |
|
---|
680 | case 'T':
|
---|
681 | g_cMaxWorkerThreads = ValueUnion.u32;
|
---|
682 | break;
|
---|
683 |
|
---|
684 | case 'k':
|
---|
685 | g_cMaxKeepAlive = ValueUnion.u32;
|
---|
686 | break;
|
---|
687 |
|
---|
688 | case 'h':
|
---|
689 | DisplayHelp();
|
---|
690 | return 0;
|
---|
691 |
|
---|
692 | case 'v':
|
---|
693 | g_fVerbose = true;
|
---|
694 | break;
|
---|
695 |
|
---|
696 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
697 | case 'b':
|
---|
698 | g_fDaemonize = true;
|
---|
699 | break;
|
---|
700 | #endif
|
---|
701 | case 'V':
|
---|
702 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
703 | return 0;
|
---|
704 |
|
---|
705 | default:
|
---|
706 | rc = RTGetOptPrintError(c, &ValueUnion);
|
---|
707 | return rc;
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
712 | if (g_fDaemonize)
|
---|
713 | {
|
---|
714 | rc = RTProcDaemonizeUsingFork(false /* fNoChDir */, false /* fNoClose */, pszPidFile);
|
---|
715 | if (RT_FAILURE(rc))
|
---|
716 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to daemonize, rc=%Rrc. exiting.", rc);
|
---|
717 | }
|
---|
718 | #endif
|
---|
719 |
|
---|
720 | // initialize COM/XPCOM
|
---|
721 | HRESULT hrc = com::Initialize();
|
---|
722 | if (FAILED(hrc))
|
---|
723 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to initialize COM! hrc=%Rhrc\n", hrc);
|
---|
724 |
|
---|
725 | ComPtr<ISession> session;
|
---|
726 |
|
---|
727 | hrc = g_pVirtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
728 | if (FAILED(hrc))
|
---|
729 | RTMsgError("failed to create the VirtualBox object!");
|
---|
730 | else
|
---|
731 | {
|
---|
732 | hrc = session.createInprocObject(CLSID_Session);
|
---|
733 | if (FAILED(hrc))
|
---|
734 | RTMsgError("failed to create a session object!");
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (FAILED(hrc))
|
---|
738 | {
|
---|
739 | com::ErrorInfo info;
|
---|
740 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
741 | {
|
---|
742 | com::GluePrintRCMessage(hrc);
|
---|
743 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
744 | }
|
---|
745 | else
|
---|
746 | com::GluePrintErrorInfo(info);
|
---|
747 | return RTEXITCODE_FAILURE;
|
---|
748 | }
|
---|
749 |
|
---|
750 | // create the global mutexes
|
---|
751 | g_pAuthLibLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
|
---|
752 | g_pSessionsLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
|
---|
753 | g_pThreadsLockHandle = new util::RWLockHandle(util::LOCKCLASS_OBJECTSTATE);
|
---|
754 | g_pWebLogLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
|
---|
755 |
|
---|
756 | // SOAP queue pumper thread
|
---|
757 | rc = RTThreadCreate(NULL,
|
---|
758 | fntQPumper,
|
---|
759 | NULL, // pvUser
|
---|
760 | 0, // cbStack (default)
|
---|
761 | RTTHREADTYPE_MAIN_WORKER,
|
---|
762 | 0, // flags
|
---|
763 | "SoapQPumper");
|
---|
764 | if (RT_FAILURE(rc))
|
---|
765 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot start SOAP queue pumper thread: %Rrc", rc);
|
---|
766 |
|
---|
767 | // watchdog thread
|
---|
768 | if (g_iWatchdogTimeoutSecs > 0)
|
---|
769 | {
|
---|
770 | // start our watchdog thread
|
---|
771 | rc = RTThreadCreate(NULL,
|
---|
772 | fntWatchdog,
|
---|
773 | NULL,
|
---|
774 | 0,
|
---|
775 | RTTHREADTYPE_MAIN_WORKER,
|
---|
776 | 0,
|
---|
777 | "Watchdog");
|
---|
778 | if (RT_FAILURE(rc))
|
---|
779 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot start watchdog thread: %Rrc", rc);
|
---|
780 | }
|
---|
781 |
|
---|
782 | com::EventQueue *pQ = com::EventQueue::getMainEventQueue();
|
---|
783 | for (;;)
|
---|
784 | {
|
---|
785 | // we have to process main event queue
|
---|
786 | WEBDEBUG(("Pumping COM event queue\n"));
|
---|
787 | rc = pQ->processEventQueue(RT_INDEFINITE_WAIT);
|
---|
788 | if (RT_FAILURE(rc))
|
---|
789 | RTMsgError("processEventQueue -> %Rrc", rc);
|
---|
790 | }
|
---|
791 |
|
---|
792 | com::Shutdown();
|
---|
793 |
|
---|
794 | return 0;
|
---|
795 | }
|
---|
796 |
|
---|
797 | /****************************************************************************
|
---|
798 | *
|
---|
799 | * Watchdog thread
|
---|
800 | *
|
---|
801 | ****************************************************************************/
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * Watchdog thread, runs in the background while the webservice is alive.
|
---|
805 | *
|
---|
806 | * This gets started by main() and runs in the background to check all sessions
|
---|
807 | * for whether they have been no requests in a configurable timeout period. In
|
---|
808 | * that case, the session is automatically logged off.
|
---|
809 | */
|
---|
810 | int fntWatchdog(RTTHREAD ThreadSelf, void *pvUser)
|
---|
811 | {
|
---|
812 | // store a log prefix for this thread
|
---|
813 | util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
814 | g_mapThreads[RTThreadSelf()] = "[W ]";
|
---|
815 | thrLock.release();
|
---|
816 |
|
---|
817 | WEBDEBUG(("Watchdog thread started\n"));
|
---|
818 |
|
---|
819 | while (1)
|
---|
820 | {
|
---|
821 | WEBDEBUG(("Watchdog: sleeping %d seconds\n", g_iWatchdogCheckInterval));
|
---|
822 | RTThreadSleep(g_iWatchdogCheckInterval * 1000);
|
---|
823 |
|
---|
824 | time_t tNow;
|
---|
825 | time(&tNow);
|
---|
826 |
|
---|
827 | // we're messing with sessions, so lock them
|
---|
828 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
829 | WEBDEBUG(("Watchdog: checking %d sessions\n", g_mapSessions.size()));
|
---|
830 |
|
---|
831 | SessionsMap::iterator it = g_mapSessions.begin(),
|
---|
832 | itEnd = g_mapSessions.end();
|
---|
833 | while (it != itEnd)
|
---|
834 | {
|
---|
835 | WebServiceSession *pSession = it->second;
|
---|
836 | WEBDEBUG(("Watchdog: tNow: %d, session timestamp: %d\n", tNow, pSession->getLastObjectLookup()));
|
---|
837 | if ( tNow
|
---|
838 | > pSession->getLastObjectLookup() + g_iWatchdogTimeoutSecs
|
---|
839 | )
|
---|
840 | {
|
---|
841 | WEBDEBUG(("Watchdog: Session %llX timed out, deleting\n", pSession->getID()));
|
---|
842 | delete pSession;
|
---|
843 | it = g_mapSessions.begin();
|
---|
844 | }
|
---|
845 | else
|
---|
846 | ++it;
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | WEBDEBUG(("Watchdog thread ending\n"));
|
---|
851 | return 0;
|
---|
852 | }
|
---|
853 |
|
---|
854 | /****************************************************************************
|
---|
855 | *
|
---|
856 | * SOAP exceptions
|
---|
857 | *
|
---|
858 | ****************************************************************************/
|
---|
859 |
|
---|
860 | /**
|
---|
861 | * Helper function to raise a SOAP fault. Called by the other helper
|
---|
862 | * functions, which raise specific SOAP faults.
|
---|
863 | *
|
---|
864 | * @param soap
|
---|
865 | * @param str
|
---|
866 | * @param extype
|
---|
867 | * @param ex
|
---|
868 | */
|
---|
869 | void RaiseSoapFault(struct soap *soap,
|
---|
870 | const char *pcsz,
|
---|
871 | int extype,
|
---|
872 | void *ex)
|
---|
873 | {
|
---|
874 | // raise the fault
|
---|
875 | soap_sender_fault(soap, pcsz, NULL);
|
---|
876 |
|
---|
877 | struct SOAP_ENV__Detail *pDetail = (struct SOAP_ENV__Detail*)soap_malloc(soap, sizeof(struct SOAP_ENV__Detail));
|
---|
878 |
|
---|
879 | // without the following, gSOAP crashes miserably when sending out the
|
---|
880 | // data because it will try to serialize all fields (stupid documentation)
|
---|
881 | memset(pDetail, 0, sizeof(struct SOAP_ENV__Detail));
|
---|
882 |
|
---|
883 | // fill extended info depending on SOAP version
|
---|
884 | if (soap->version == 2) // SOAP 1.2 is used
|
---|
885 | {
|
---|
886 | soap->fault->SOAP_ENV__Detail = pDetail;
|
---|
887 | soap->fault->SOAP_ENV__Detail->__type = extype;
|
---|
888 | soap->fault->SOAP_ENV__Detail->fault = ex;
|
---|
889 | soap->fault->SOAP_ENV__Detail->__any = NULL; // no other XML data
|
---|
890 | }
|
---|
891 | else
|
---|
892 | {
|
---|
893 | soap->fault->detail = pDetail;
|
---|
894 | soap->fault->detail->__type = extype;
|
---|
895 | soap->fault->detail->fault = ex;
|
---|
896 | soap->fault->detail->__any = NULL; // no other XML data
|
---|
897 | }
|
---|
898 | }
|
---|
899 |
|
---|
900 | /**
|
---|
901 | * Raises a SOAP fault that signals that an invalid object was passed.
|
---|
902 | *
|
---|
903 | * @param soap
|
---|
904 | * @param obj
|
---|
905 | */
|
---|
906 | void RaiseSoapInvalidObjectFault(struct soap *soap,
|
---|
907 | WSDLT_ID obj)
|
---|
908 | {
|
---|
909 | _vbox__InvalidObjectFault *ex = soap_new__vbox__InvalidObjectFault(soap, 1);
|
---|
910 | ex->badObjectID = obj;
|
---|
911 |
|
---|
912 | std::string str("VirtualBox error: ");
|
---|
913 | str += "Invalid managed object reference \"" + obj + "\"";
|
---|
914 |
|
---|
915 | RaiseSoapFault(soap,
|
---|
916 | str.c_str(),
|
---|
917 | SOAP_TYPE__vbox__InvalidObjectFault,
|
---|
918 | ex);
|
---|
919 | }
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * Return a safe C++ string from the given COM string,
|
---|
923 | * without crashing if the COM string is empty.
|
---|
924 | * @param bstr
|
---|
925 | * @return
|
---|
926 | */
|
---|
927 | std::string ConvertComString(const com::Bstr &bstr)
|
---|
928 | {
|
---|
929 | com::Utf8Str ustr(bstr);
|
---|
930 | return ustr.c_str(); // @todo r=dj since the length is known, we can probably use a better std::string allocator
|
---|
931 | }
|
---|
932 |
|
---|
933 | /**
|
---|
934 | * Return a safe C++ string from the given COM UUID,
|
---|
935 | * without crashing if the UUID is empty.
|
---|
936 | * @param bstr
|
---|
937 | * @return
|
---|
938 | */
|
---|
939 | std::string ConvertComString(const com::Guid &uuid)
|
---|
940 | {
|
---|
941 | com::Utf8Str ustr(uuid.toString());
|
---|
942 | return ustr.c_str(); // @todo r=dj since the length is known, we can probably use a better std::string allocator
|
---|
943 | }
|
---|
944 |
|
---|
945 | /**
|
---|
946 | * Raises a SOAP runtime fault. Implementation for the RaiseSoapRuntimeFault template
|
---|
947 | * function in vboxweb.h.
|
---|
948 | *
|
---|
949 | * @param pObj
|
---|
950 | */
|
---|
951 | void RaiseSoapRuntimeFault2(struct soap *soap,
|
---|
952 | HRESULT apirc,
|
---|
953 | IUnknown *pObj,
|
---|
954 | const com::Guid &iid)
|
---|
955 | {
|
---|
956 | com::ErrorInfo info(pObj, iid.ref());
|
---|
957 |
|
---|
958 | WEBDEBUG((" error, raising SOAP exception\n"));
|
---|
959 |
|
---|
960 | RTStrmPrintf(g_pStdErr, "API return code: 0x%08X (%Rhrc)\n", apirc, apirc);
|
---|
961 | RTStrmPrintf(g_pStdErr, "COM error info result code: 0x%lX\n", info.getResultCode());
|
---|
962 | RTStrmPrintf(g_pStdErr, "COM error info text: %ls\n", info.getText().raw());
|
---|
963 |
|
---|
964 | // allocated our own soap fault struct
|
---|
965 | _vbox__RuntimeFault *ex = soap_new__vbox__RuntimeFault(soap, 1);
|
---|
966 | // some old vbox methods return errors without setting an error in the error info,
|
---|
967 | // so use the error info code if it's set and the HRESULT from the method otherwise
|
---|
968 | if (S_OK == (ex->resultCode = info.getResultCode()))
|
---|
969 | ex->resultCode = apirc;
|
---|
970 | ex->text = ConvertComString(info.getText());
|
---|
971 | ex->component = ConvertComString(info.getComponent());
|
---|
972 | ex->interfaceID = ConvertComString(info.getInterfaceID());
|
---|
973 |
|
---|
974 | // compose descriptive message
|
---|
975 | com::Utf8StrFmt str("VirtualBox error: %s (0x%lX)", ex->text.c_str(), ex->resultCode);
|
---|
976 |
|
---|
977 | RaiseSoapFault(soap,
|
---|
978 | str.c_str(),
|
---|
979 | SOAP_TYPE__vbox__RuntimeFault,
|
---|
980 | ex);
|
---|
981 | }
|
---|
982 |
|
---|
983 | /****************************************************************************
|
---|
984 | *
|
---|
985 | * splitting and merging of object IDs
|
---|
986 | *
|
---|
987 | ****************************************************************************/
|
---|
988 |
|
---|
989 | uint64_t str2ulonglong(const char *pcsz)
|
---|
990 | {
|
---|
991 | uint64_t u = 0;
|
---|
992 | RTStrToUInt64Full(pcsz, 16, &u);
|
---|
993 | return u;
|
---|
994 | }
|
---|
995 |
|
---|
996 | /**
|
---|
997 | * Splits a managed object reference (in string form, as
|
---|
998 | * passed in from a SOAP method call) into two integers for
|
---|
999 | * session and object IDs, respectively.
|
---|
1000 | *
|
---|
1001 | * @param id
|
---|
1002 | * @param sessid
|
---|
1003 | * @param objid
|
---|
1004 | * @return
|
---|
1005 | */
|
---|
1006 | bool SplitManagedObjectRef(const WSDLT_ID &id,
|
---|
1007 | uint64_t *pSessid,
|
---|
1008 | uint64_t *pObjid)
|
---|
1009 | {
|
---|
1010 | // 64-bit numbers in hex have 16 digits; hence
|
---|
1011 | // the object-ref string must have 16 + "-" + 16 characters
|
---|
1012 | std::string str;
|
---|
1013 | if ( (id.length() == 33)
|
---|
1014 | && (id[16] == '-')
|
---|
1015 | )
|
---|
1016 | {
|
---|
1017 | char psz[34];
|
---|
1018 | memcpy(psz, id.c_str(), 34);
|
---|
1019 | psz[16] = '\0';
|
---|
1020 | if (pSessid)
|
---|
1021 | *pSessid = str2ulonglong(psz);
|
---|
1022 | if (pObjid)
|
---|
1023 | *pObjid = str2ulonglong(psz + 17);
|
---|
1024 | return true;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | return false;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | /**
|
---|
1031 | * Creates a managed object reference (in string form) from
|
---|
1032 | * two integers representing a session and object ID, respectively.
|
---|
1033 | *
|
---|
1034 | * @param sz Buffer with at least 34 bytes space to receive MOR string.
|
---|
1035 | * @param sessid
|
---|
1036 | * @param objid
|
---|
1037 | * @return
|
---|
1038 | */
|
---|
1039 | void MakeManagedObjectRef(char *sz,
|
---|
1040 | uint64_t &sessid,
|
---|
1041 | uint64_t &objid)
|
---|
1042 | {
|
---|
1043 | RTStrFormatNumber(sz, sessid, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
|
---|
1044 | sz[16] = '-';
|
---|
1045 | RTStrFormatNumber(sz + 17, objid, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | /****************************************************************************
|
---|
1049 | *
|
---|
1050 | * class WebServiceSession
|
---|
1051 | *
|
---|
1052 | ****************************************************************************/
|
---|
1053 |
|
---|
1054 | class WebServiceSessionPrivate
|
---|
1055 | {
|
---|
1056 | public:
|
---|
1057 | ManagedObjectsMapById _mapManagedObjectsById;
|
---|
1058 | ManagedObjectsMapByPtr _mapManagedObjectsByPtr;
|
---|
1059 | };
|
---|
1060 |
|
---|
1061 | /**
|
---|
1062 | * Constructor for the session object.
|
---|
1063 | *
|
---|
1064 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1065 | *
|
---|
1066 | * @param username
|
---|
1067 | * @param password
|
---|
1068 | */
|
---|
1069 | WebServiceSession::WebServiceSession()
|
---|
1070 | : _fDestructing(false),
|
---|
1071 | _pISession(NULL),
|
---|
1072 | _tLastObjectLookup(0)
|
---|
1073 | {
|
---|
1074 | _pp = new WebServiceSessionPrivate;
|
---|
1075 | _uSessionID = RTRandU64();
|
---|
1076 |
|
---|
1077 | // register this session globally
|
---|
1078 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1079 | g_mapSessions[_uSessionID] = this;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | /**
|
---|
1083 | * Destructor. Cleans up and destroys all contained managed object references on the way.
|
---|
1084 | *
|
---|
1085 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1086 | */
|
---|
1087 | WebServiceSession::~WebServiceSession()
|
---|
1088 | {
|
---|
1089 | // delete us from global map first so we can't be found
|
---|
1090 | // any more while we're cleaning up
|
---|
1091 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1092 | g_mapSessions.erase(_uSessionID);
|
---|
1093 |
|
---|
1094 | // notify ManagedObjectRef destructor so it won't
|
---|
1095 | // remove itself from the maps; this avoids rebalancing
|
---|
1096 | // the map's tree on every delete as well
|
---|
1097 | _fDestructing = true;
|
---|
1098 |
|
---|
1099 | // if (_pISession)
|
---|
1100 | // {
|
---|
1101 | // delete _pISession;
|
---|
1102 | // _pISession = NULL;
|
---|
1103 | // }
|
---|
1104 |
|
---|
1105 | ManagedObjectsMapById::iterator it,
|
---|
1106 | end = _pp->_mapManagedObjectsById.end();
|
---|
1107 | for (it = _pp->_mapManagedObjectsById.begin();
|
---|
1108 | it != end;
|
---|
1109 | ++it)
|
---|
1110 | {
|
---|
1111 | ManagedObjectRef *pRef = it->second;
|
---|
1112 | delete pRef; // this frees the contained ComPtr as well
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | delete _pp;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | /**
|
---|
1119 | * Authenticate the username and password against an authentication authority.
|
---|
1120 | *
|
---|
1121 | * @return 0 if the user was successfully authenticated, or an error code
|
---|
1122 | * otherwise.
|
---|
1123 | */
|
---|
1124 |
|
---|
1125 | int WebServiceSession::authenticate(const char *pcszUsername,
|
---|
1126 | const char *pcszPassword)
|
---|
1127 | {
|
---|
1128 | int rc = VERR_WEB_NOT_AUTHENTICATED;
|
---|
1129 |
|
---|
1130 | util::AutoReadLock lock(g_pAuthLibLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1131 |
|
---|
1132 | static bool fAuthLibLoaded = false;
|
---|
1133 | static PAUTHENTRY pfnAuthEntry = NULL;
|
---|
1134 | static PAUTHENTRY2 pfnAuthEntry2 = NULL;
|
---|
1135 | static PAUTHENTRY3 pfnAuthEntry3 = NULL;
|
---|
1136 |
|
---|
1137 | if (!fAuthLibLoaded)
|
---|
1138 | {
|
---|
1139 | // retrieve authentication library from system properties
|
---|
1140 | ComPtr<ISystemProperties> systemProperties;
|
---|
1141 | g_pVirtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
|
---|
1142 |
|
---|
1143 | com::Bstr authLibrary;
|
---|
1144 | systemProperties->COMGETTER(WebServiceAuthLibrary)(authLibrary.asOutParam());
|
---|
1145 | com::Utf8Str filename = authLibrary;
|
---|
1146 |
|
---|
1147 | WEBDEBUG(("external authentication library is '%ls'\n", authLibrary.raw()));
|
---|
1148 |
|
---|
1149 | if (filename == "null")
|
---|
1150 | // authentication disabled, let everyone in:
|
---|
1151 | fAuthLibLoaded = true;
|
---|
1152 | else
|
---|
1153 | {
|
---|
1154 | RTLDRMOD hlibAuth = 0;
|
---|
1155 | do
|
---|
1156 | {
|
---|
1157 | rc = RTLdrLoad(filename.c_str(), &hlibAuth);
|
---|
1158 | if (RT_FAILURE(rc))
|
---|
1159 | {
|
---|
1160 | WEBDEBUG(("%s() Failed to load external authentication library. Error code: %Rrc\n", __FUNCTION__, rc));
|
---|
1161 | break;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, AUTHENTRY3_NAME, (void**)&pfnAuthEntry3)))
|
---|
1165 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, AUTHENTRY3_NAME, rc));
|
---|
1166 |
|
---|
1167 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, AUTHENTRY2_NAME, (void**)&pfnAuthEntry2)))
|
---|
1168 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, AUTHENTRY2_NAME, rc));
|
---|
1169 |
|
---|
1170 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, AUTHENTRY_NAME, (void**)&pfnAuthEntry)))
|
---|
1171 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, AUTHENTRY_NAME, rc));
|
---|
1172 |
|
---|
1173 | if (pfnAuthEntry || pfnAuthEntry2 || pfnAuthEntry3)
|
---|
1174 | fAuthLibLoaded = true;
|
---|
1175 |
|
---|
1176 | } while (0);
|
---|
1177 | }
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | rc = VERR_WEB_NOT_AUTHENTICATED;
|
---|
1181 | AuthResult result;
|
---|
1182 | if (pfnAuthEntry3)
|
---|
1183 | {
|
---|
1184 | result = pfnAuthEntry3("webservice", NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
|
---|
1185 | WEBDEBUG(("%s(): result of AuthEntry(): %d\n", __FUNCTION__, result));
|
---|
1186 | if (result == AuthResultAccessGranted)
|
---|
1187 | rc = 0;
|
---|
1188 | }
|
---|
1189 | else if (pfnAuthEntry2)
|
---|
1190 | {
|
---|
1191 | result = pfnAuthEntry2(NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
|
---|
1192 | WEBDEBUG(("%s(): result of VRDPAuth2(): %d\n", __FUNCTION__, result));
|
---|
1193 | if (result == AuthResultAccessGranted)
|
---|
1194 | rc = 0;
|
---|
1195 | }
|
---|
1196 | else if (pfnAuthEntry)
|
---|
1197 | {
|
---|
1198 | result = pfnAuthEntry(NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL);
|
---|
1199 | WEBDEBUG(("%s(): result of VRDPAuth(%s, [%d]): %d\n", __FUNCTION__, pcszUsername, strlen(pcszPassword), result));
|
---|
1200 | if (result == AuthResultAccessGranted)
|
---|
1201 | rc = 0;
|
---|
1202 | }
|
---|
1203 | else if (fAuthLibLoaded)
|
---|
1204 | // fAuthLibLoaded = true but both pointers are NULL:
|
---|
1205 | // then the authlib was "null" and auth was disabled
|
---|
1206 | rc = 0;
|
---|
1207 | else
|
---|
1208 | {
|
---|
1209 | WEBDEBUG(("Could not resolve AuthEntry, VRDPAuth2 or VRDPAuth entry point"));
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | lock.release();
|
---|
1213 |
|
---|
1214 | if (!rc)
|
---|
1215 | {
|
---|
1216 | do
|
---|
1217 | {
|
---|
1218 | // now create the ISession object that this webservice session can use
|
---|
1219 | // (and of which IWebsessionManager::getSessionObject returns a managed object reference)
|
---|
1220 | ComPtr<ISession> session;
|
---|
1221 | if (FAILED(rc = session.createInprocObject(CLSID_Session)))
|
---|
1222 | {
|
---|
1223 | WEBDEBUG(("ERROR: cannot create session object!"));
|
---|
1224 | break;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | ComPtr<IUnknown> p2 = session;
|
---|
1228 | _pISession = new ManagedObjectRef(*this,
|
---|
1229 | p2, // IUnknown *pobjUnknown
|
---|
1230 | session, // void *pobjInterface
|
---|
1231 | com::Guid(COM_IIDOF(ISession)),
|
---|
1232 | g_pcszISession);
|
---|
1233 |
|
---|
1234 | if (g_fVerbose)
|
---|
1235 | {
|
---|
1236 | ISession *p = session;
|
---|
1237 | WEBDEBUG((" * %s: created session object with comptr 0x%lX, MOR = %s\n", __FUNCTION__, p, _pISession->getWSDLID().c_str()));
|
---|
1238 | }
|
---|
1239 | } while (0);
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | return rc;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | /**
|
---|
1246 | * Look up, in this session, whether a ManagedObjectRef has already been
|
---|
1247 | * created for the given COM pointer.
|
---|
1248 | *
|
---|
1249 | * Note how we require that a ComPtr<IUnknown> is passed, which causes a
|
---|
1250 | * queryInterface call when the caller passes in a different type, since
|
---|
1251 | * a ComPtr<IUnknown> will point to something different than a
|
---|
1252 | * ComPtr<IVirtualBox>, for example. As we store the ComPtr<IUnknown> in
|
---|
1253 | * our private hash table, we must search for one too.
|
---|
1254 | *
|
---|
1255 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1256 | *
|
---|
1257 | * @param pcu pointer to a COM object.
|
---|
1258 | * @return The existing ManagedObjectRef that represents the COM object, or NULL if there's none yet.
|
---|
1259 | */
|
---|
1260 | ManagedObjectRef* WebServiceSession::findRefFromPtr(const IUnknown *pObject)
|
---|
1261 | {
|
---|
1262 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1263 |
|
---|
1264 | uintptr_t ulp = (uintptr_t)pObject;
|
---|
1265 | // WEBDEBUG((" %s: looking up 0x%lX\n", __FUNCTION__, ulp));
|
---|
1266 | ManagedObjectsMapByPtr::iterator it = _pp->_mapManagedObjectsByPtr.find(ulp);
|
---|
1267 | if (it != _pp->_mapManagedObjectsByPtr.end())
|
---|
1268 | {
|
---|
1269 | ManagedObjectRef *pRef = it->second;
|
---|
1270 | WEBDEBUG((" %s: found existing ref %s (%s) for COM obj 0x%lX\n", __FUNCTION__, pRef->getWSDLID().c_str(), pRef->getInterfaceName(), ulp));
|
---|
1271 | return pRef;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | return NULL;
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 | /**
|
---|
1278 | * Static method which attempts to find the session for which the given managed
|
---|
1279 | * object reference was created, by splitting the reference into the session and
|
---|
1280 | * object IDs and then looking up the session object for that session ID.
|
---|
1281 | *
|
---|
1282 | * Preconditions: Caller must have locked g_pSessionsLockHandle in read mode.
|
---|
1283 | *
|
---|
1284 | * @param id Managed object reference (with combined session and object IDs).
|
---|
1285 | * @return
|
---|
1286 | */
|
---|
1287 | WebServiceSession* WebServiceSession::findSessionFromRef(const WSDLT_ID &id)
|
---|
1288 | {
|
---|
1289 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1290 |
|
---|
1291 | WebServiceSession *pSession = NULL;
|
---|
1292 | uint64_t sessid;
|
---|
1293 | if (SplitManagedObjectRef(id,
|
---|
1294 | &sessid,
|
---|
1295 | NULL))
|
---|
1296 | {
|
---|
1297 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
1298 | if (it != g_mapSessions.end())
|
---|
1299 | pSession = it->second;
|
---|
1300 | }
|
---|
1301 | return pSession;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /**
|
---|
1305 | *
|
---|
1306 | */
|
---|
1307 | const WSDLT_ID& WebServiceSession::getSessionWSDLID() const
|
---|
1308 | {
|
---|
1309 | return _pISession->getWSDLID();
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /**
|
---|
1313 | * Touches the webservice session to prevent it from timing out.
|
---|
1314 | *
|
---|
1315 | * Each webservice session has an internal timestamp that records
|
---|
1316 | * the last request made to it from the client that started it.
|
---|
1317 | * If no request was made within a configurable timeframe, then
|
---|
1318 | * the client is logged off automatically,
|
---|
1319 | * by calling IWebsessionManager::logoff()
|
---|
1320 | */
|
---|
1321 | void WebServiceSession::touch()
|
---|
1322 | {
|
---|
1323 | time(&_tLastObjectLookup);
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 |
|
---|
1327 | /****************************************************************************
|
---|
1328 | *
|
---|
1329 | * class ManagedObjectRef
|
---|
1330 | *
|
---|
1331 | ****************************************************************************/
|
---|
1332 |
|
---|
1333 | /**
|
---|
1334 | * Constructor, which assigns a unique ID to this managed object
|
---|
1335 | * reference and stores it two global hashes:
|
---|
1336 | *
|
---|
1337 | * a) G_mapManagedObjectsById, which maps ManagedObjectID's to
|
---|
1338 | * instances of this class; this hash is then used by the
|
---|
1339 | * findObjectFromRef() template function in vboxweb.h
|
---|
1340 | * to quickly retrieve the COM object from its managed
|
---|
1341 | * object ID (mostly in the context of the method mappers
|
---|
1342 | * in methodmaps.cpp, when a web service client passes in
|
---|
1343 | * a managed object ID);
|
---|
1344 | *
|
---|
1345 | * b) G_mapManagedObjectsByComPtr, which maps COM pointers to
|
---|
1346 | * instances of this class; this hash is used by
|
---|
1347 | * createRefFromObject() to quickly figure out whether an
|
---|
1348 | * instance already exists for a given COM pointer.
|
---|
1349 | *
|
---|
1350 | * This constructor calls AddRef() on the given COM object, and
|
---|
1351 | * the destructor will call Release(). We require two input pointers
|
---|
1352 | * for that COM object, one generic IUnknown* pointer which is used
|
---|
1353 | * as the map key, and a specific interface pointer (e.g. IMachine*)
|
---|
1354 | * which must support the interface given in guidInterface. All
|
---|
1355 | * three values are returned by getPtr(), which gives future callers
|
---|
1356 | * a chance to reuse the specific interface pointer without having
|
---|
1357 | * to call QueryInterface, which can be expensive.
|
---|
1358 | *
|
---|
1359 | * This does _not_ check whether another instance already
|
---|
1360 | * exists in the hash. This gets called only from the
|
---|
1361 | * createOrFindRefFromComPtr() template function in vboxweb.h, which
|
---|
1362 | * does perform that check.
|
---|
1363 | *
|
---|
1364 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1365 | *
|
---|
1366 | * @param session Session to which the MOR will be added.
|
---|
1367 | * @param pobjUnknown Pointer to IUnknown* interface for the COM object; this will be used in the hashes.
|
---|
1368 | * @param pobjInterface Pointer to a specific interface for the COM object, described by guidInterface.
|
---|
1369 | * @param guidInterface Interface which pobjInterface points to.
|
---|
1370 | * @param pcszInterface String representation of that interface (e.g. "IMachine") for readability and logging.
|
---|
1371 | */
|
---|
1372 | ManagedObjectRef::ManagedObjectRef(WebServiceSession &session,
|
---|
1373 | IUnknown *pobjUnknown,
|
---|
1374 | void *pobjInterface,
|
---|
1375 | const com::Guid &guidInterface,
|
---|
1376 | const char *pcszInterface)
|
---|
1377 | : _session(session),
|
---|
1378 | _pobjUnknown(pobjUnknown),
|
---|
1379 | _pobjInterface(pobjInterface),
|
---|
1380 | _guidInterface(guidInterface),
|
---|
1381 | _pcszInterface(pcszInterface)
|
---|
1382 | {
|
---|
1383 | Assert(pobjUnknown);
|
---|
1384 | Assert(pobjInterface);
|
---|
1385 |
|
---|
1386 | // keep both stubs alive while this MOR exists (matching Release() calls are in destructor)
|
---|
1387 | uint32_t cRefs1 = pobjUnknown->AddRef();
|
---|
1388 | uint32_t cRefs2 = ((IUnknown*)pobjInterface)->AddRef();
|
---|
1389 | _ulp = (uintptr_t)pobjUnknown;
|
---|
1390 |
|
---|
1391 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1392 | _id = ++g_iMaxManagedObjectID;
|
---|
1393 | // and count globally
|
---|
1394 | ULONG64 cTotal = ++g_cManagedObjects; // raise global count and make a copy for the debug message below
|
---|
1395 |
|
---|
1396 | char sz[34];
|
---|
1397 | MakeManagedObjectRef(sz, session._uSessionID, _id);
|
---|
1398 | _strID = sz;
|
---|
1399 |
|
---|
1400 | session._pp->_mapManagedObjectsById[_id] = this;
|
---|
1401 | session._pp->_mapManagedObjectsByPtr[_ulp] = this;
|
---|
1402 |
|
---|
1403 | session.touch();
|
---|
1404 |
|
---|
1405 | WEBDEBUG((" * %s: MOR created for %s*=0x%lX (IUnknown*=0x%lX; COM refcount now %RI32/%RI32), new ID is %llX; now %lld objects total\n",
|
---|
1406 | __FUNCTION__,
|
---|
1407 | pcszInterface,
|
---|
1408 | pobjInterface,
|
---|
1409 | pobjUnknown,
|
---|
1410 | cRefs1,
|
---|
1411 | cRefs2,
|
---|
1412 | _id,
|
---|
1413 | cTotal));
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | /**
|
---|
1417 | * Destructor; removes the instance from the global hash of
|
---|
1418 | * managed objects. Calls Release() on the contained COM object.
|
---|
1419 | *
|
---|
1420 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1421 | */
|
---|
1422 | ManagedObjectRef::~ManagedObjectRef()
|
---|
1423 | {
|
---|
1424 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1425 | ULONG64 cTotal = --g_cManagedObjects;
|
---|
1426 |
|
---|
1427 | Assert(_pobjUnknown);
|
---|
1428 | Assert(_pobjInterface);
|
---|
1429 |
|
---|
1430 | // we called AddRef() on both interfaces, so call Release() on
|
---|
1431 | // both as well, but in reverse order
|
---|
1432 | uint32_t cRefs2 = ((IUnknown*)_pobjInterface)->Release();
|
---|
1433 | uint32_t cRefs1 = _pobjUnknown->Release();
|
---|
1434 | WEBDEBUG((" * %s: deleting MOR for ID %llX (%s; COM refcount now %RI32/%RI32); now %lld objects total\n", __FUNCTION__, _id, _pcszInterface, cRefs1, cRefs2, cTotal));
|
---|
1435 |
|
---|
1436 | // if we're being destroyed from the session's destructor,
|
---|
1437 | // then that destructor is iterating over the maps, so
|
---|
1438 | // don't remove us there! (data integrity + speed)
|
---|
1439 | if (!_session._fDestructing)
|
---|
1440 | {
|
---|
1441 | WEBDEBUG((" * %s: removing from session maps\n", __FUNCTION__));
|
---|
1442 | _session._pp->_mapManagedObjectsById.erase(_id);
|
---|
1443 | if (_session._pp->_mapManagedObjectsByPtr.erase(_ulp) != 1)
|
---|
1444 | WEBDEBUG((" WARNING: could not find %llX in _mapManagedObjectsByPtr\n", _ulp));
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | /**
|
---|
1449 | * Static helper method for findObjectFromRef() template that actually
|
---|
1450 | * looks up the object from a given integer ID.
|
---|
1451 | *
|
---|
1452 | * This has been extracted into this non-template function to reduce
|
---|
1453 | * code bloat as we have the actual STL map lookup only in this function.
|
---|
1454 | *
|
---|
1455 | * This also "touches" the timestamp in the session whose ID is encoded
|
---|
1456 | * in the given integer ID, in order to prevent the session from timing
|
---|
1457 | * out.
|
---|
1458 | *
|
---|
1459 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1460 | *
|
---|
1461 | * @param strId
|
---|
1462 | * @param iter
|
---|
1463 | * @return
|
---|
1464 | */
|
---|
1465 | int ManagedObjectRef::findRefFromId(const WSDLT_ID &id,
|
---|
1466 | ManagedObjectRef **pRef,
|
---|
1467 | bool fNullAllowed)
|
---|
1468 | {
|
---|
1469 | int rc = 0;
|
---|
1470 |
|
---|
1471 | do
|
---|
1472 | {
|
---|
1473 | // allow NULL (== empty string) input reference, which should return a NULL pointer
|
---|
1474 | if (!id.length() && fNullAllowed)
|
---|
1475 | {
|
---|
1476 | *pRef = NULL;
|
---|
1477 | return 0;
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | uint64_t sessid;
|
---|
1481 | uint64_t objid;
|
---|
1482 | WEBDEBUG((" %s(): looking up objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1483 | if (!SplitManagedObjectRef(id,
|
---|
1484 | &sessid,
|
---|
1485 | &objid))
|
---|
1486 | {
|
---|
1487 | rc = VERR_WEB_INVALID_MANAGED_OBJECT_REFERENCE;
|
---|
1488 | break;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
1492 | if (it == g_mapSessions.end())
|
---|
1493 | {
|
---|
1494 | WEBDEBUG((" %s: cannot find session for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1495 | rc = VERR_WEB_INVALID_SESSION_ID;
|
---|
1496 | break;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | WebServiceSession *pSess = it->second;
|
---|
1500 | // "touch" session to prevent it from timing out
|
---|
1501 | pSess->touch();
|
---|
1502 |
|
---|
1503 | ManagedObjectsIteratorById iter = pSess->_pp->_mapManagedObjectsById.find(objid);
|
---|
1504 | if (iter == pSess->_pp->_mapManagedObjectsById.end())
|
---|
1505 | {
|
---|
1506 | WEBDEBUG((" %s: cannot find comobj for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1507 | rc = VERR_WEB_INVALID_OBJECT_ID;
|
---|
1508 | break;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | *pRef = iter->second;
|
---|
1512 |
|
---|
1513 | } while (0);
|
---|
1514 |
|
---|
1515 | return rc;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | /****************************************************************************
|
---|
1519 | *
|
---|
1520 | * interface IManagedObjectRef
|
---|
1521 | *
|
---|
1522 | ****************************************************************************/
|
---|
1523 |
|
---|
1524 | /**
|
---|
1525 | * This is the hard-coded implementation for the IManagedObjectRef::getInterfaceName()
|
---|
1526 | * that our WSDL promises to our web service clients. This method returns a
|
---|
1527 | * string describing the interface that this managed object reference
|
---|
1528 | * supports, e.g. "IMachine".
|
---|
1529 | *
|
---|
1530 | * @param soap
|
---|
1531 | * @param req
|
---|
1532 | * @param resp
|
---|
1533 | * @return
|
---|
1534 | */
|
---|
1535 | int __vbox__IManagedObjectRef_USCOREgetInterfaceName(
|
---|
1536 | struct soap *soap,
|
---|
1537 | _vbox__IManagedObjectRef_USCOREgetInterfaceName *req,
|
---|
1538 | _vbox__IManagedObjectRef_USCOREgetInterfaceNameResponse *resp)
|
---|
1539 | {
|
---|
1540 | HRESULT rc = SOAP_OK;
|
---|
1541 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1542 |
|
---|
1543 | do
|
---|
1544 | {
|
---|
1545 | // findRefFromId require the lock
|
---|
1546 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1547 |
|
---|
1548 | ManagedObjectRef *pRef;
|
---|
1549 | if (!ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false))
|
---|
1550 | resp->returnval = pRef->getInterfaceName();
|
---|
1551 |
|
---|
1552 | } while (0);
|
---|
1553 |
|
---|
1554 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1555 | if (FAILED(rc))
|
---|
1556 | return SOAP_FAULT;
|
---|
1557 | return SOAP_OK;
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 | /**
|
---|
1561 | * This is the hard-coded implementation for the IManagedObjectRef::release()
|
---|
1562 | * that our WSDL promises to our web service clients. This method releases
|
---|
1563 | * a managed object reference and removes it from our stacks.
|
---|
1564 | *
|
---|
1565 | * @param soap
|
---|
1566 | * @param req
|
---|
1567 | * @param resp
|
---|
1568 | * @return
|
---|
1569 | */
|
---|
1570 | int __vbox__IManagedObjectRef_USCORErelease(
|
---|
1571 | struct soap *soap,
|
---|
1572 | _vbox__IManagedObjectRef_USCORErelease *req,
|
---|
1573 | _vbox__IManagedObjectRef_USCOREreleaseResponse *resp)
|
---|
1574 | {
|
---|
1575 | HRESULT rc = SOAP_OK;
|
---|
1576 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1577 |
|
---|
1578 | do
|
---|
1579 | {
|
---|
1580 | // findRefFromId and the delete call below require the lock
|
---|
1581 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1582 |
|
---|
1583 | ManagedObjectRef *pRef;
|
---|
1584 | if ((rc = ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false)))
|
---|
1585 | {
|
---|
1586 | RaiseSoapInvalidObjectFault(soap, req->_USCOREthis);
|
---|
1587 | break;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | WEBDEBUG((" found reference; deleting!\n"));
|
---|
1591 | // this removes the object from all stacks; since
|
---|
1592 | // there's a ComPtr<> hidden inside the reference,
|
---|
1593 | // this should also invoke Release() on the COM
|
---|
1594 | // object
|
---|
1595 | delete pRef;
|
---|
1596 | } while (0);
|
---|
1597 |
|
---|
1598 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1599 | if (FAILED(rc))
|
---|
1600 | return SOAP_FAULT;
|
---|
1601 | return SOAP_OK;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | /****************************************************************************
|
---|
1605 | *
|
---|
1606 | * interface IWebsessionManager
|
---|
1607 | *
|
---|
1608 | ****************************************************************************/
|
---|
1609 |
|
---|
1610 | /**
|
---|
1611 | * Hard-coded implementation for IWebsessionManager::logon. As opposed to the underlying
|
---|
1612 | * COM API, this is the first method that a webservice client must call before the
|
---|
1613 | * webservice will do anything useful.
|
---|
1614 | *
|
---|
1615 | * This returns a managed object reference to the global IVirtualBox object; into this
|
---|
1616 | * reference a session ID is encoded which remains constant with all managed object
|
---|
1617 | * references returned by other methods.
|
---|
1618 | *
|
---|
1619 | * This also creates an instance of ISession, which is stored internally with the
|
---|
1620 | * webservice session and can be retrieved with IWebsessionManager::getSessionObject
|
---|
1621 | * (__vbox__IWebsessionManager_USCOREgetSessionObject). In order for the
|
---|
1622 | * VirtualBox web service to do anything useful, one usually needs both a
|
---|
1623 | * VirtualBox and an ISession object, for which these two methods are designed.
|
---|
1624 | *
|
---|
1625 | * When the webservice client is done, it should call IWebsessionManager::logoff. This
|
---|
1626 | * will clean up internally (destroy all remaining managed object references and
|
---|
1627 | * related COM objects used internally).
|
---|
1628 | *
|
---|
1629 | * After logon, an internal timeout ensures that if the webservice client does not
|
---|
1630 | * call any methods, after a configurable number of seconds, the webservice will log
|
---|
1631 | * off the client automatically. This is to ensure that the webservice does not
|
---|
1632 | * drown in managed object references and eventually deny service. Still, it is
|
---|
1633 | * a much better solution, both for performance and cleanliness, for the webservice
|
---|
1634 | * client to clean up itself.
|
---|
1635 | *
|
---|
1636 | * @param
|
---|
1637 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1638 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1639 | * @return
|
---|
1640 | */
|
---|
1641 | int __vbox__IWebsessionManager_USCORElogon(
|
---|
1642 | struct soap*,
|
---|
1643 | _vbox__IWebsessionManager_USCORElogon *req,
|
---|
1644 | _vbox__IWebsessionManager_USCORElogonResponse *resp)
|
---|
1645 | {
|
---|
1646 | HRESULT rc = SOAP_OK;
|
---|
1647 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1648 |
|
---|
1649 | do
|
---|
1650 | {
|
---|
1651 | // WebServiceSession constructor tinkers with global MOR map and requires a write lock
|
---|
1652 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1653 |
|
---|
1654 | // create new session; the constructor stores the new session
|
---|
1655 | // in the global map automatically
|
---|
1656 | WebServiceSession *pSession = new WebServiceSession();
|
---|
1657 |
|
---|
1658 | // authenticate the user
|
---|
1659 | if (!(pSession->authenticate(req->username.c_str(),
|
---|
1660 | req->password.c_str())))
|
---|
1661 | {
|
---|
1662 | // in the new session, create a managed object reference (MOR) for the
|
---|
1663 | // global VirtualBox object; this encodes the session ID in the MOR so
|
---|
1664 | // that it will be implicitly be included in all future requests of this
|
---|
1665 | // webservice client
|
---|
1666 | ComPtr<IUnknown> p2 = g_pVirtualBox;
|
---|
1667 | ManagedObjectRef *pRef = new ManagedObjectRef(*pSession,
|
---|
1668 | p2, // IUnknown *pobjUnknown
|
---|
1669 | g_pVirtualBox, // void *pobjInterface
|
---|
1670 | COM_IIDOF(IVirtualBox),
|
---|
1671 | g_pcszIVirtualBox);
|
---|
1672 | resp->returnval = pRef->getWSDLID();
|
---|
1673 | WEBDEBUG(("VirtualBox object ref is %s\n", resp->returnval.c_str()));
|
---|
1674 | }
|
---|
1675 | } while (0);
|
---|
1676 |
|
---|
1677 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1678 | if (FAILED(rc))
|
---|
1679 | return SOAP_FAULT;
|
---|
1680 | return SOAP_OK;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | /**
|
---|
1684 | * Returns the ISession object that was created for the webservice client
|
---|
1685 | * on logon.
|
---|
1686 | */
|
---|
1687 | int __vbox__IWebsessionManager_USCOREgetSessionObject(
|
---|
1688 | struct soap*,
|
---|
1689 | _vbox__IWebsessionManager_USCOREgetSessionObject *req,
|
---|
1690 | _vbox__IWebsessionManager_USCOREgetSessionObjectResponse *resp)
|
---|
1691 | {
|
---|
1692 | HRESULT rc = SOAP_OK;
|
---|
1693 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1694 |
|
---|
1695 | do
|
---|
1696 | {
|
---|
1697 | // findSessionFromRef needs lock
|
---|
1698 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1699 |
|
---|
1700 | WebServiceSession* pSession;
|
---|
1701 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1702 | resp->returnval = pSession->getSessionWSDLID();
|
---|
1703 |
|
---|
1704 | } while (0);
|
---|
1705 |
|
---|
1706 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1707 | if (FAILED(rc))
|
---|
1708 | return SOAP_FAULT;
|
---|
1709 | return SOAP_OK;
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | /**
|
---|
1713 | * hard-coded implementation for IWebsessionManager::logoff.
|
---|
1714 | *
|
---|
1715 | * @param
|
---|
1716 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1717 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1718 | * @return
|
---|
1719 | */
|
---|
1720 | int __vbox__IWebsessionManager_USCORElogoff(
|
---|
1721 | struct soap*,
|
---|
1722 | _vbox__IWebsessionManager_USCORElogoff *req,
|
---|
1723 | _vbox__IWebsessionManager_USCORElogoffResponse *resp)
|
---|
1724 | {
|
---|
1725 | HRESULT rc = SOAP_OK;
|
---|
1726 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1727 |
|
---|
1728 | do
|
---|
1729 | {
|
---|
1730 | // findSessionFromRef and the session destructor require the lock
|
---|
1731 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1732 |
|
---|
1733 | WebServiceSession* pSession;
|
---|
1734 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1735 | {
|
---|
1736 | delete pSession;
|
---|
1737 | // destructor cleans up
|
---|
1738 |
|
---|
1739 | WEBDEBUG(("session destroyed, %d sessions left open\n", g_mapSessions.size()));
|
---|
1740 | }
|
---|
1741 | } while (0);
|
---|
1742 |
|
---|
1743 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1744 | if (FAILED(rc))
|
---|
1745 | return SOAP_FAULT;
|
---|
1746 | return SOAP_OK;
|
---|
1747 | }
|
---|
1748 |
|
---|