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