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