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