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