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/VRDPAuth.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 creatd) 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 | // intialize 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 | // intialize 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);
|
---|
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 authentification 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 PVRDPAUTHENTRY pfnAuthEntry = NULL;
|
---|
1134 | static PVRDPAUTHENTRY2 pfnAuthEntry2 = NULL;
|
---|
1135 |
|
---|
1136 | if (!fAuthLibLoaded)
|
---|
1137 | {
|
---|
1138 | // retrieve authentication library from system properties
|
---|
1139 | ComPtr<ISystemProperties> systemProperties;
|
---|
1140 | g_pVirtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
|
---|
1141 |
|
---|
1142 | com::Bstr authLibrary;
|
---|
1143 | systemProperties->COMGETTER(WebServiceAuthLibrary)(authLibrary.asOutParam());
|
---|
1144 | com::Utf8Str filename = authLibrary;
|
---|
1145 |
|
---|
1146 | WEBDEBUG(("external authentication library is '%ls'\n", authLibrary.raw()));
|
---|
1147 |
|
---|
1148 | if (filename == "null")
|
---|
1149 | // authentication disabled, let everyone in:
|
---|
1150 | fAuthLibLoaded = true;
|
---|
1151 | else
|
---|
1152 | {
|
---|
1153 | RTLDRMOD hlibAuth = 0;
|
---|
1154 | do
|
---|
1155 | {
|
---|
1156 | rc = RTLdrLoad(filename.c_str(), &hlibAuth);
|
---|
1157 | if (RT_FAILURE(rc))
|
---|
1158 | {
|
---|
1159 | WEBDEBUG(("%s() Failed to load external authentication library. Error code: %Rrc\n", __FUNCTION__, rc));
|
---|
1160 | break;
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, "VRDPAuth2", (void**)&pfnAuthEntry2)))
|
---|
1164 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, "VRDPAuth2", rc));
|
---|
1165 |
|
---|
1166 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, "VRDPAuth", (void**)&pfnAuthEntry)))
|
---|
1167 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, "VRDPAuth", rc));
|
---|
1168 |
|
---|
1169 | if (pfnAuthEntry || pfnAuthEntry2)
|
---|
1170 | fAuthLibLoaded = true;
|
---|
1171 |
|
---|
1172 | } while (0);
|
---|
1173 | }
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | rc = VERR_WEB_NOT_AUTHENTICATED;
|
---|
1177 | VRDPAuthResult result;
|
---|
1178 | if (pfnAuthEntry2)
|
---|
1179 | {
|
---|
1180 | result = pfnAuthEntry2(NULL, VRDPAuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
|
---|
1181 | WEBDEBUG(("%s(): result of VRDPAuth2(): %d\n", __FUNCTION__, result));
|
---|
1182 | if (result == VRDPAuthAccessGranted)
|
---|
1183 | rc = 0;
|
---|
1184 | }
|
---|
1185 | else if (pfnAuthEntry)
|
---|
1186 | {
|
---|
1187 | result = pfnAuthEntry(NULL, VRDPAuthGuestNotAsked, pcszUsername, pcszPassword, NULL);
|
---|
1188 | WEBDEBUG(("%s(): result of VRDPAuth(%s, [%d]): %d\n", __FUNCTION__, pcszUsername, strlen(pcszPassword), result));
|
---|
1189 | if (result == VRDPAuthAccessGranted)
|
---|
1190 | rc = 0;
|
---|
1191 | }
|
---|
1192 | else if (fAuthLibLoaded)
|
---|
1193 | // fAuthLibLoaded = true but both pointers are NULL:
|
---|
1194 | // then the authlib was "null" and auth was disabled
|
---|
1195 | rc = 0;
|
---|
1196 | else
|
---|
1197 | {
|
---|
1198 | WEBDEBUG(("Could not resolve VRDPAuth2 or VRDPAuth entry point"));
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | lock.release();
|
---|
1202 |
|
---|
1203 | if (!rc)
|
---|
1204 | {
|
---|
1205 | do
|
---|
1206 | {
|
---|
1207 | // now create the ISession object that this webservice session can use
|
---|
1208 | // (and of which IWebsessionManager::getSessionObject returns a managed object reference)
|
---|
1209 | ComPtr<ISession> session;
|
---|
1210 | if (FAILED(rc = session.createInprocObject(CLSID_Session)))
|
---|
1211 | {
|
---|
1212 | WEBDEBUG(("ERROR: cannot create session object!"));
|
---|
1213 | break;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | ComPtr<IUnknown> p2 = session;
|
---|
1217 | _pISession = new ManagedObjectRef(*this,
|
---|
1218 | p2, // IUnknown *pobjUnknown
|
---|
1219 | session, // void *pobjInterface
|
---|
1220 | com::Guid(COM_IIDOF(ISession)),
|
---|
1221 | g_pcszISession);
|
---|
1222 |
|
---|
1223 | if (g_fVerbose)
|
---|
1224 | {
|
---|
1225 | ISession *p = session;
|
---|
1226 | WEBDEBUG((" * %s: created session object with comptr 0x%lX, MOR = %s\n", __FUNCTION__, p, _pISession->getWSDLID().c_str()));
|
---|
1227 | }
|
---|
1228 | } while (0);
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | return rc;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /**
|
---|
1235 | * Look up, in this session, whether a ManagedObjectRef has already been
|
---|
1236 | * created for the given COM pointer.
|
---|
1237 | *
|
---|
1238 | * Note how we require that a ComPtr<IUnknown> is passed, which causes a
|
---|
1239 | * queryInterface call when the caller passes in a different type, since
|
---|
1240 | * a ComPtr<IUnknown> will point to something different than a
|
---|
1241 | * ComPtr<IVirtualBox>, for example. As we store the ComPtr<IUnknown> in
|
---|
1242 | * our private hash table, we must search for one too.
|
---|
1243 | *
|
---|
1244 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1245 | *
|
---|
1246 | * @param pcu pointer to a COM object.
|
---|
1247 | * @return The existing ManagedObjectRef that represents the COM object, or NULL if there's none yet.
|
---|
1248 | */
|
---|
1249 | ManagedObjectRef* WebServiceSession::findRefFromPtr(const IUnknown *pObject)
|
---|
1250 | {
|
---|
1251 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1252 |
|
---|
1253 | uintptr_t ulp = (uintptr_t)pObject;
|
---|
1254 | // WEBDEBUG((" %s: looking up 0x%lX\n", __FUNCTION__, ulp));
|
---|
1255 | ManagedObjectsMapByPtr::iterator it = _pp->_mapManagedObjectsByPtr.find(ulp);
|
---|
1256 | if (it != _pp->_mapManagedObjectsByPtr.end())
|
---|
1257 | {
|
---|
1258 | ManagedObjectRef *pRef = it->second;
|
---|
1259 | WEBDEBUG((" %s: found existing ref %s (%s) for COM obj 0x%lX\n", __FUNCTION__, pRef->getWSDLID().c_str(), pRef->getInterfaceName(), ulp));
|
---|
1260 | return pRef;
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | return NULL;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | /**
|
---|
1267 | * Static method which attempts to find the session for which the given managed
|
---|
1268 | * object reference was created, by splitting the reference into the session and
|
---|
1269 | * object IDs and then looking up the session object for that session ID.
|
---|
1270 | *
|
---|
1271 | * Preconditions: Caller must have locked g_pSessionsLockHandle in read mode.
|
---|
1272 | *
|
---|
1273 | * @param id Managed object reference (with combined session and object IDs).
|
---|
1274 | * @return
|
---|
1275 | */
|
---|
1276 | WebServiceSession* WebServiceSession::findSessionFromRef(const WSDLT_ID &id)
|
---|
1277 | {
|
---|
1278 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1279 |
|
---|
1280 | WebServiceSession *pSession = NULL;
|
---|
1281 | uint64_t sessid;
|
---|
1282 | if (SplitManagedObjectRef(id,
|
---|
1283 | &sessid,
|
---|
1284 | NULL))
|
---|
1285 | {
|
---|
1286 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
1287 | if (it != g_mapSessions.end())
|
---|
1288 | pSession = it->second;
|
---|
1289 | }
|
---|
1290 | return pSession;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /**
|
---|
1294 | *
|
---|
1295 | */
|
---|
1296 | const WSDLT_ID& WebServiceSession::getSessionWSDLID() const
|
---|
1297 | {
|
---|
1298 | return _pISession->getWSDLID();
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /**
|
---|
1302 | * Touches the webservice session to prevent it from timing out.
|
---|
1303 | *
|
---|
1304 | * Each webservice session has an internal timestamp that records
|
---|
1305 | * the last request made to it from the client that started it.
|
---|
1306 | * If no request was made within a configurable timeframe, then
|
---|
1307 | * the client is logged off automatically,
|
---|
1308 | * by calling IWebsessionManager::logoff()
|
---|
1309 | */
|
---|
1310 | void WebServiceSession::touch()
|
---|
1311 | {
|
---|
1312 | time(&_tLastObjectLookup);
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 |
|
---|
1316 | /****************************************************************************
|
---|
1317 | *
|
---|
1318 | * class ManagedObjectRef
|
---|
1319 | *
|
---|
1320 | ****************************************************************************/
|
---|
1321 |
|
---|
1322 | /**
|
---|
1323 | * Constructor, which assigns a unique ID to this managed object
|
---|
1324 | * reference and stores it two global hashes:
|
---|
1325 | *
|
---|
1326 | * a) G_mapManagedObjectsById, which maps ManagedObjectID's to
|
---|
1327 | * instances of this class; this hash is then used by the
|
---|
1328 | * findObjectFromRef() template function in vboxweb.h
|
---|
1329 | * to quickly retrieve the COM object from its managed
|
---|
1330 | * object ID (mostly in the context of the method mappers
|
---|
1331 | * in methodmaps.cpp, when a web service client passes in
|
---|
1332 | * a managed object ID);
|
---|
1333 | *
|
---|
1334 | * b) G_mapManagedObjectsByComPtr, which maps COM pointers to
|
---|
1335 | * instances of this class; this hash is used by
|
---|
1336 | * createRefFromObject() to quickly figure out whether an
|
---|
1337 | * instance already exists for a given COM pointer.
|
---|
1338 | *
|
---|
1339 | * This constructor calls AddRef() on the given COM object, and
|
---|
1340 | * the destructor will call Release(). We require two input pointers
|
---|
1341 | * for that COM object, one generic IUnknown* pointer which is used
|
---|
1342 | * as the map key, and a specific interface pointer (e.g. IMachine*)
|
---|
1343 | * which must support the interface given in guidInterface. All
|
---|
1344 | * three values are returned by getPtr(), which gives future callers
|
---|
1345 | * a chance to reuse the specific interface pointer without having
|
---|
1346 | * to call QueryInterface, which can be expensive.
|
---|
1347 | *
|
---|
1348 | * This does _not_ check whether another instance already
|
---|
1349 | * exists in the hash. This gets called only from the
|
---|
1350 | * createOrFindRefFromComPtr() template function in vboxweb.h, which
|
---|
1351 | * does perform that check.
|
---|
1352 | *
|
---|
1353 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1354 | *
|
---|
1355 | * @param session Session to which the MOR will be added.
|
---|
1356 | * @param pobjUnknown Pointer to IUnknown* interface for the COM object; this will be used in the hashes.
|
---|
1357 | * @param pobjInterface Pointer to a specific interface for the COM object, described by guidInterface.
|
---|
1358 | * @param guidInterface Interface which pobjInterface points to.
|
---|
1359 | * @param pcszInterface String representation of that interface (e.g. "IMachine") for readability and logging.
|
---|
1360 | */
|
---|
1361 | ManagedObjectRef::ManagedObjectRef(WebServiceSession &session,
|
---|
1362 | IUnknown *pobjUnknown,
|
---|
1363 | void *pobjInterface,
|
---|
1364 | const com::Guid &guidInterface,
|
---|
1365 | const char *pcszInterface)
|
---|
1366 | : _session(session),
|
---|
1367 | _pobjUnknown(pobjUnknown),
|
---|
1368 | _pobjInterface(pobjInterface),
|
---|
1369 | _guidInterface(guidInterface),
|
---|
1370 | _pcszInterface(pcszInterface)
|
---|
1371 | {
|
---|
1372 | Assert(pobjUnknown);
|
---|
1373 | Assert(pobjInterface);
|
---|
1374 |
|
---|
1375 | // keep both stubs alive while this MOR exists (matching Release() calls are in destructor)
|
---|
1376 | uint32_t cRefs1 = pobjUnknown->AddRef();
|
---|
1377 | uint32_t cRefs2 = ((IUnknown*)pobjInterface)->AddRef();
|
---|
1378 | _ulp = (uintptr_t)pobjUnknown;
|
---|
1379 |
|
---|
1380 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1381 | _id = ++g_iMaxManagedObjectID;
|
---|
1382 | // and count globally
|
---|
1383 | ULONG64 cTotal = ++g_cManagedObjects; // raise global count and make a copy for the debug message below
|
---|
1384 |
|
---|
1385 | char sz[34];
|
---|
1386 | MakeManagedObjectRef(sz, session._uSessionID, _id);
|
---|
1387 | _strID = sz;
|
---|
1388 |
|
---|
1389 | session._pp->_mapManagedObjectsById[_id] = this;
|
---|
1390 | session._pp->_mapManagedObjectsByPtr[_ulp] = this;
|
---|
1391 |
|
---|
1392 | session.touch();
|
---|
1393 |
|
---|
1394 | 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",
|
---|
1395 | __FUNCTION__,
|
---|
1396 | pcszInterface,
|
---|
1397 | pobjInterface,
|
---|
1398 | pobjUnknown,
|
---|
1399 | cRefs1,
|
---|
1400 | cRefs2,
|
---|
1401 | _id,
|
---|
1402 | cTotal));
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | /**
|
---|
1406 | * Destructor; removes the instance from the global hash of
|
---|
1407 | * managed objects. Calls Release() on the contained COM object.
|
---|
1408 | *
|
---|
1409 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1410 | */
|
---|
1411 | ManagedObjectRef::~ManagedObjectRef()
|
---|
1412 | {
|
---|
1413 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1414 | ULONG64 cTotal = --g_cManagedObjects;
|
---|
1415 |
|
---|
1416 | Assert(_pobjUnknown);
|
---|
1417 | Assert(_pobjInterface);
|
---|
1418 |
|
---|
1419 | // we called AddRef() on both interfaces, so call Release() on
|
---|
1420 | // both as well, but in reverse order
|
---|
1421 | uint32_t cRefs2 = ((IUnknown*)_pobjInterface)->Release();
|
---|
1422 | uint32_t cRefs1 = _pobjUnknown->Release();
|
---|
1423 | WEBDEBUG((" * %s: deleting MOR for ID %llX (%s; COM refcount now %RI32/%RI32); now %lld objects total\n", __FUNCTION__, _id, _pcszInterface, cRefs1, cRefs2, cTotal));
|
---|
1424 |
|
---|
1425 | // if we're being destroyed from the session's destructor,
|
---|
1426 | // then that destructor is iterating over the maps, so
|
---|
1427 | // don't remove us there! (data integrity + speed)
|
---|
1428 | if (!_session._fDestructing)
|
---|
1429 | {
|
---|
1430 | WEBDEBUG((" * %s: removing from session maps\n", __FUNCTION__));
|
---|
1431 | _session._pp->_mapManagedObjectsById.erase(_id);
|
---|
1432 | if (_session._pp->_mapManagedObjectsByPtr.erase(_ulp) != 1)
|
---|
1433 | WEBDEBUG((" WARNING: could not find %llX in _mapManagedObjectsByPtr\n", _ulp));
|
---|
1434 | }
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | /**
|
---|
1438 | * Static helper method for findObjectFromRef() template that actually
|
---|
1439 | * looks up the object from a given integer ID.
|
---|
1440 | *
|
---|
1441 | * This has been extracted into this non-template function to reduce
|
---|
1442 | * code bloat as we have the actual STL map lookup only in this function.
|
---|
1443 | *
|
---|
1444 | * This also "touches" the timestamp in the session whose ID is encoded
|
---|
1445 | * in the given integer ID, in order to prevent the session from timing
|
---|
1446 | * out.
|
---|
1447 | *
|
---|
1448 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1449 | *
|
---|
1450 | * @param strId
|
---|
1451 | * @param iter
|
---|
1452 | * @return
|
---|
1453 | */
|
---|
1454 | int ManagedObjectRef::findRefFromId(const WSDLT_ID &id,
|
---|
1455 | ManagedObjectRef **pRef,
|
---|
1456 | bool fNullAllowed)
|
---|
1457 | {
|
---|
1458 | int rc = 0;
|
---|
1459 |
|
---|
1460 | do
|
---|
1461 | {
|
---|
1462 | // allow NULL (== empty string) input reference, which should return a NULL pointer
|
---|
1463 | if (!id.length() && fNullAllowed)
|
---|
1464 | {
|
---|
1465 | *pRef = NULL;
|
---|
1466 | return 0;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | uint64_t sessid;
|
---|
1470 | uint64_t objid;
|
---|
1471 | WEBDEBUG((" %s(): looking up objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1472 | if (!SplitManagedObjectRef(id,
|
---|
1473 | &sessid,
|
---|
1474 | &objid))
|
---|
1475 | {
|
---|
1476 | rc = VERR_WEB_INVALID_MANAGED_OBJECT_REFERENCE;
|
---|
1477 | break;
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
1481 | if (it == g_mapSessions.end())
|
---|
1482 | {
|
---|
1483 | WEBDEBUG((" %s: cannot find session for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1484 | rc = VERR_WEB_INVALID_SESSION_ID;
|
---|
1485 | break;
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | WebServiceSession *pSess = it->second;
|
---|
1489 | // "touch" session to prevent it from timing out
|
---|
1490 | pSess->touch();
|
---|
1491 |
|
---|
1492 | ManagedObjectsIteratorById iter = pSess->_pp->_mapManagedObjectsById.find(objid);
|
---|
1493 | if (iter == pSess->_pp->_mapManagedObjectsById.end())
|
---|
1494 | {
|
---|
1495 | WEBDEBUG((" %s: cannot find comobj for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1496 | rc = VERR_WEB_INVALID_OBJECT_ID;
|
---|
1497 | break;
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | *pRef = iter->second;
|
---|
1501 |
|
---|
1502 | } while (0);
|
---|
1503 |
|
---|
1504 | return rc;
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | /****************************************************************************
|
---|
1508 | *
|
---|
1509 | * interface IManagedObjectRef
|
---|
1510 | *
|
---|
1511 | ****************************************************************************/
|
---|
1512 |
|
---|
1513 | /**
|
---|
1514 | * This is the hard-coded implementation for the IManagedObjectRef::getInterfaceName()
|
---|
1515 | * that our WSDL promises to our web service clients. This method returns a
|
---|
1516 | * string describing the interface that this managed object reference
|
---|
1517 | * supports, e.g. "IMachine".
|
---|
1518 | *
|
---|
1519 | * @param soap
|
---|
1520 | * @param req
|
---|
1521 | * @param resp
|
---|
1522 | * @return
|
---|
1523 | */
|
---|
1524 | int __vbox__IManagedObjectRef_USCOREgetInterfaceName(
|
---|
1525 | struct soap *soap,
|
---|
1526 | _vbox__IManagedObjectRef_USCOREgetInterfaceName *req,
|
---|
1527 | _vbox__IManagedObjectRef_USCOREgetInterfaceNameResponse *resp)
|
---|
1528 | {
|
---|
1529 | HRESULT rc = SOAP_OK;
|
---|
1530 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1531 |
|
---|
1532 | do
|
---|
1533 | {
|
---|
1534 | // findRefFromId require the lock
|
---|
1535 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1536 |
|
---|
1537 | ManagedObjectRef *pRef;
|
---|
1538 | if (!ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false))
|
---|
1539 | resp->returnval = pRef->getInterfaceName();
|
---|
1540 |
|
---|
1541 | } while (0);
|
---|
1542 |
|
---|
1543 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1544 | if (FAILED(rc))
|
---|
1545 | return SOAP_FAULT;
|
---|
1546 | return SOAP_OK;
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /**
|
---|
1550 | * This is the hard-coded implementation for the IManagedObjectRef::release()
|
---|
1551 | * that our WSDL promises to our web service clients. This method releases
|
---|
1552 | * a managed object reference and removes it from our stacks.
|
---|
1553 | *
|
---|
1554 | * @param soap
|
---|
1555 | * @param req
|
---|
1556 | * @param resp
|
---|
1557 | * @return
|
---|
1558 | */
|
---|
1559 | int __vbox__IManagedObjectRef_USCORErelease(
|
---|
1560 | struct soap *soap,
|
---|
1561 | _vbox__IManagedObjectRef_USCORErelease *req,
|
---|
1562 | _vbox__IManagedObjectRef_USCOREreleaseResponse *resp)
|
---|
1563 | {
|
---|
1564 | HRESULT rc = SOAP_OK;
|
---|
1565 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1566 |
|
---|
1567 | do
|
---|
1568 | {
|
---|
1569 | // findRefFromId and the delete call below require the lock
|
---|
1570 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1571 |
|
---|
1572 | ManagedObjectRef *pRef;
|
---|
1573 | if ((rc = ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false)))
|
---|
1574 | {
|
---|
1575 | RaiseSoapInvalidObjectFault(soap, req->_USCOREthis);
|
---|
1576 | break;
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | WEBDEBUG((" found reference; deleting!\n"));
|
---|
1580 | // this removes the object from all stacks; since
|
---|
1581 | // there's a ComPtr<> hidden inside the reference,
|
---|
1582 | // this should also invoke Release() on the COM
|
---|
1583 | // object
|
---|
1584 | delete pRef;
|
---|
1585 | } while (0);
|
---|
1586 |
|
---|
1587 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1588 | if (FAILED(rc))
|
---|
1589 | return SOAP_FAULT;
|
---|
1590 | return SOAP_OK;
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | /****************************************************************************
|
---|
1594 | *
|
---|
1595 | * interface IWebsessionManager
|
---|
1596 | *
|
---|
1597 | ****************************************************************************/
|
---|
1598 |
|
---|
1599 | /**
|
---|
1600 | * Hard-coded implementation for IWebsessionManager::logon. As opposed to the underlying
|
---|
1601 | * COM API, this is the first method that a webservice client must call before the
|
---|
1602 | * webservice will do anything useful.
|
---|
1603 | *
|
---|
1604 | * This returns a managed object reference to the global IVirtualBox object; into this
|
---|
1605 | * reference a session ID is encoded which remains constant with all managed object
|
---|
1606 | * references returned by other methods.
|
---|
1607 | *
|
---|
1608 | * This also creates an instance of ISession, which is stored internally with the
|
---|
1609 | * webservice session and can be retrieved with IWebsessionManager::getSessionObject
|
---|
1610 | * (__vbox__IWebsessionManager_USCOREgetSessionObject). In order for the
|
---|
1611 | * VirtualBox web service to do anything useful, one usually needs both a
|
---|
1612 | * VirtualBox and an ISession object, for which these two methods are designed.
|
---|
1613 | *
|
---|
1614 | * When the webservice client is done, it should call IWebsessionManager::logoff. This
|
---|
1615 | * will clean up internally (destroy all remaining managed object references and
|
---|
1616 | * related COM objects used internally).
|
---|
1617 | *
|
---|
1618 | * After logon, an internal timeout ensures that if the webservice client does not
|
---|
1619 | * call any methods, after a configurable number of seconds, the webservice will log
|
---|
1620 | * off the client automatically. This is to ensure that the webservice does not
|
---|
1621 | * drown in managed object references and eventually deny service. Still, it is
|
---|
1622 | * a much better solution, both for performance and cleanliness, for the webservice
|
---|
1623 | * client to clean up itself.
|
---|
1624 | *
|
---|
1625 | * @param
|
---|
1626 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1627 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1628 | * @return
|
---|
1629 | */
|
---|
1630 | int __vbox__IWebsessionManager_USCORElogon(
|
---|
1631 | struct soap*,
|
---|
1632 | _vbox__IWebsessionManager_USCORElogon *req,
|
---|
1633 | _vbox__IWebsessionManager_USCORElogonResponse *resp)
|
---|
1634 | {
|
---|
1635 | HRESULT rc = SOAP_OK;
|
---|
1636 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1637 |
|
---|
1638 | do
|
---|
1639 | {
|
---|
1640 | // WebServiceSession constructor tinkers with global MOR map and requires a write lock
|
---|
1641 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1642 |
|
---|
1643 | // create new session; the constructor stores the new session
|
---|
1644 | // in the global map automatically
|
---|
1645 | WebServiceSession *pSession = new WebServiceSession();
|
---|
1646 |
|
---|
1647 | // authenticate the user
|
---|
1648 | if (!(pSession->authenticate(req->username.c_str(),
|
---|
1649 | req->password.c_str())))
|
---|
1650 | {
|
---|
1651 | // in the new session, create a managed object reference (MOR) for the
|
---|
1652 | // global VirtualBox object; this encodes the session ID in the MOR so
|
---|
1653 | // that it will be implicitly be included in all future requests of this
|
---|
1654 | // webservice client
|
---|
1655 | ComPtr<IUnknown> p2 = g_pVirtualBox;
|
---|
1656 | ManagedObjectRef *pRef = new ManagedObjectRef(*pSession,
|
---|
1657 | p2, // IUnknown *pobjUnknown
|
---|
1658 | g_pVirtualBox, // void *pobjInterface
|
---|
1659 | COM_IIDOF(IVirtualBox),
|
---|
1660 | g_pcszIVirtualBox);
|
---|
1661 | resp->returnval = pRef->getWSDLID();
|
---|
1662 | WEBDEBUG(("VirtualBox object ref is %s\n", resp->returnval.c_str()));
|
---|
1663 | }
|
---|
1664 | } while (0);
|
---|
1665 |
|
---|
1666 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1667 | if (FAILED(rc))
|
---|
1668 | return SOAP_FAULT;
|
---|
1669 | return SOAP_OK;
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | /**
|
---|
1673 | * Returns the ISession object that was created for the webservice client
|
---|
1674 | * on logon.
|
---|
1675 | */
|
---|
1676 | int __vbox__IWebsessionManager_USCOREgetSessionObject(
|
---|
1677 | struct soap*,
|
---|
1678 | _vbox__IWebsessionManager_USCOREgetSessionObject *req,
|
---|
1679 | _vbox__IWebsessionManager_USCOREgetSessionObjectResponse *resp)
|
---|
1680 | {
|
---|
1681 | HRESULT rc = SOAP_OK;
|
---|
1682 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1683 |
|
---|
1684 | do
|
---|
1685 | {
|
---|
1686 | // findSessionFromRef needs lock
|
---|
1687 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1688 |
|
---|
1689 | WebServiceSession* pSession;
|
---|
1690 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1691 | resp->returnval = pSession->getSessionWSDLID();
|
---|
1692 |
|
---|
1693 | } while (0);
|
---|
1694 |
|
---|
1695 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1696 | if (FAILED(rc))
|
---|
1697 | return SOAP_FAULT;
|
---|
1698 | return SOAP_OK;
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | /**
|
---|
1702 | * hard-coded implementation for IWebsessionManager::logoff.
|
---|
1703 | *
|
---|
1704 | * @param
|
---|
1705 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1706 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1707 | * @return
|
---|
1708 | */
|
---|
1709 | int __vbox__IWebsessionManager_USCORElogoff(
|
---|
1710 | struct soap*,
|
---|
1711 | _vbox__IWebsessionManager_USCORElogoff *req,
|
---|
1712 | _vbox__IWebsessionManager_USCORElogoffResponse *resp)
|
---|
1713 | {
|
---|
1714 | HRESULT rc = SOAP_OK;
|
---|
1715 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1716 |
|
---|
1717 | do
|
---|
1718 | {
|
---|
1719 | // findSessionFromRef and the session destructor require the lock
|
---|
1720 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1721 |
|
---|
1722 | WebServiceSession* pSession;
|
---|
1723 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1724 | {
|
---|
1725 | delete pSession;
|
---|
1726 | // destructor cleans up
|
---|
1727 |
|
---|
1728 | WEBDEBUG(("session destroyed, %d sessions left open\n", g_mapSessions.size()));
|
---|
1729 | }
|
---|
1730 | } while (0);
|
---|
1731 |
|
---|
1732 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1733 | if (FAILED(rc))
|
---|
1734 | return SOAP_FAULT;
|
---|
1735 | return SOAP_OK;
|
---|
1736 | }
|
---|
1737 |
|
---|