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 | /**
|
---|
720 | * Start up the webservice server. This keeps running and waits
|
---|
721 | * for incoming SOAP connections; for each request that comes in,
|
---|
722 | * it calls method implementation code, most of it in the generated
|
---|
723 | * code in methodmaps.cpp.
|
---|
724 | *
|
---|
725 | * @param argc
|
---|
726 | * @param argv[]
|
---|
727 | * @return
|
---|
728 | */
|
---|
729 | int main(int argc, char *argv[])
|
---|
730 | {
|
---|
731 | // initialize runtime
|
---|
732 | int rc = RTR3Init();
|
---|
733 | if (RT_FAILURE(rc))
|
---|
734 | return RTMsgInitFailure(rc);
|
---|
735 |
|
---|
736 | // store a log prefix for this thread
|
---|
737 | g_mapThreads[RTThreadSelf()] = "[M ]";
|
---|
738 |
|
---|
739 | RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " web service version " VBOX_VERSION_STRING "\n"
|
---|
740 | "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
741 | "All rights reserved.\n");
|
---|
742 |
|
---|
743 | int c;
|
---|
744 | const char *pszPidFile = NULL;
|
---|
745 | RTGETOPTUNION ValueUnion;
|
---|
746 | RTGETOPTSTATE GetState;
|
---|
747 | RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0 /*fFlags*/);
|
---|
748 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
749 | {
|
---|
750 | switch (c)
|
---|
751 | {
|
---|
752 | case 'H':
|
---|
753 | if (!ValueUnion.psz || !*ValueUnion.psz)
|
---|
754 | {
|
---|
755 | /* Normalize NULL/empty string to NULL, which will be
|
---|
756 | * interpreted as "localhost" below. */
|
---|
757 | g_pcszBindToHost = NULL;
|
---|
758 | }
|
---|
759 | else
|
---|
760 | g_pcszBindToHost = ValueUnion.psz;
|
---|
761 | break;
|
---|
762 |
|
---|
763 | case 'p':
|
---|
764 | g_uBindToPort = ValueUnion.u32;
|
---|
765 | break;
|
---|
766 |
|
---|
767 | case 't':
|
---|
768 | g_iWatchdogTimeoutSecs = ValueUnion.u32;
|
---|
769 | break;
|
---|
770 |
|
---|
771 | case 'i':
|
---|
772 | g_iWatchdogCheckInterval = ValueUnion.u32;
|
---|
773 | break;
|
---|
774 |
|
---|
775 | case 'F':
|
---|
776 | {
|
---|
777 | int rc2 = RTStrmOpen(ValueUnion.psz, "a", &g_pStrmLog);
|
---|
778 | if (rc2)
|
---|
779 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot open log file \"%s\" for writing: %Rrc", ValueUnion.psz, rc2);
|
---|
780 |
|
---|
781 | WebLog(VBOX_PRODUCT " Webservice Version %s\n"
|
---|
782 | "Opened log file \"%s\"\n", VBOX_VERSION_STRING, ValueUnion.psz);
|
---|
783 | break;
|
---|
784 | }
|
---|
785 |
|
---|
786 | case 'P':
|
---|
787 | pszPidFile = ValueUnion.psz;
|
---|
788 | break;
|
---|
789 |
|
---|
790 | case 'T':
|
---|
791 | g_cMaxWorkerThreads = ValueUnion.u32;
|
---|
792 | break;
|
---|
793 |
|
---|
794 | case 'k':
|
---|
795 | g_cMaxKeepAlive = ValueUnion.u32;
|
---|
796 | break;
|
---|
797 |
|
---|
798 | case 'h':
|
---|
799 | DisplayHelp();
|
---|
800 | return 0;
|
---|
801 |
|
---|
802 | case 'v':
|
---|
803 | g_fVerbose = true;
|
---|
804 | break;
|
---|
805 |
|
---|
806 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
807 | case 'b':
|
---|
808 | g_fDaemonize = true;
|
---|
809 | break;
|
---|
810 | #endif
|
---|
811 | case 'V':
|
---|
812 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
813 | return 0;
|
---|
814 |
|
---|
815 | default:
|
---|
816 | rc = RTGetOptPrintError(c, &ValueUnion);
|
---|
817 | return rc;
|
---|
818 | }
|
---|
819 | }
|
---|
820 |
|
---|
821 | #if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
|
---|
822 | if (g_fDaemonize)
|
---|
823 | {
|
---|
824 | /* prepare release logging */
|
---|
825 | char szLogFile[RTPATH_MAX];
|
---|
826 |
|
---|
827 | rc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile));
|
---|
828 | if (RT_FAILURE(rc))
|
---|
829 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory for logging: %Rrc", rc);
|
---|
830 | rc = RTPathAppend(szLogFile, sizeof(szLogFile), "vboxwebsrv.log");
|
---|
831 | if (RT_FAILURE(rc))
|
---|
832 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not construct logging path: %Rrc", rc);
|
---|
833 |
|
---|
834 | rc = RTProcDaemonizeUsingFork(false /* fNoChDir */, false /* fNoClose */, pszPidFile);
|
---|
835 | if (RT_FAILURE(rc))
|
---|
836 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to daemonize, rc=%Rrc. exiting.", rc);
|
---|
837 |
|
---|
838 | /* From now on it's a waste of CPU cycles to send logging to stdout. */
|
---|
839 | g_fStdOutLogging = false;
|
---|
840 |
|
---|
841 | /* create release logger */
|
---|
842 | PRTLOGGER loggerRelease;
|
---|
843 | static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
|
---|
844 | RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
|
---|
845 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
846 | fFlags |= RTLOGFLAGS_USECRLF;
|
---|
847 | #endif
|
---|
848 | char szError[RTPATH_MAX + 128] = "";
|
---|
849 | int vrc = RTLogCreateEx(&loggerRelease, fFlags, "all",
|
---|
850 | "VBOXWEBSRV_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
|
---|
851 | RTLOGDEST_FILE, szError, sizeof(szError), szLogFile);
|
---|
852 | if (RT_SUCCESS(vrc))
|
---|
853 | {
|
---|
854 | /* some introductory information */
|
---|
855 | RTTIMESPEC timeSpec;
|
---|
856 | char szTmp[256];
|
---|
857 | RTTimeSpecToString(RTTimeNow(&timeSpec), szTmp, sizeof(szTmp));
|
---|
858 | RTLogRelLogger(loggerRelease, 0, ~0U,
|
---|
859 | "VirtualBox web service %s r%u %s (%s %s) release log\n"
|
---|
860 | #ifdef VBOX_BLEEDING_EDGE
|
---|
861 | "EXPERIMENTAL build " VBOX_BLEEDING_EDGE "\n"
|
---|
862 | #endif
|
---|
863 | "Log opened %s\n",
|
---|
864 | VBOX_VERSION_STRING, RTBldCfgRevision(), VBOX_BUILD_TARGET,
|
---|
865 | __DATE__, __TIME__, szTmp);
|
---|
866 |
|
---|
867 | vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
|
---|
868 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
869 | RTLogRelLogger(loggerRelease, 0, ~0U, "OS Product: %s\n", szTmp);
|
---|
870 | vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
|
---|
871 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
872 | RTLogRelLogger(loggerRelease, 0, ~0U, "OS Release: %s\n", szTmp);
|
---|
873 | vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
|
---|
874 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
875 | RTLogRelLogger(loggerRelease, 0, ~0U, "OS Version: %s\n", szTmp);
|
---|
876 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
877 | RTLogRelLogger(loggerRelease, 0, ~0U, "OS Service Pack: %s\n", szTmp);
|
---|
878 |
|
---|
879 | /* the package type is interesting for Linux distributions */
|
---|
880 | char szExecName[RTPATH_MAX];
|
---|
881 | char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
|
---|
882 | RTLogRelLogger(loggerRelease, 0, ~0U,
|
---|
883 | "Executable: %s\n"
|
---|
884 | "Process ID: %u\n"
|
---|
885 | "Package type: %s"
|
---|
886 | #ifdef VBOX_OSE
|
---|
887 | " (OSE)"
|
---|
888 | #endif
|
---|
889 | "\n",
|
---|
890 | pszExecName ? pszExecName : "unknown",
|
---|
891 | RTProcSelf(),
|
---|
892 | VBOX_PACKAGE_STRING);
|
---|
893 |
|
---|
894 | /* register this logger as the release logger */
|
---|
895 | RTLogRelSetDefaultInstance(loggerRelease);
|
---|
896 |
|
---|
897 | /* Explicitly flush the log in case of VBOXWEBSRV_RELEASE_LOG=buffered. */
|
---|
898 | RTLogFlush(loggerRelease);
|
---|
899 | }
|
---|
900 | else
|
---|
901 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", szError, vrc);
|
---|
902 | }
|
---|
903 | #endif
|
---|
904 |
|
---|
905 | // initialize COM/XPCOM
|
---|
906 | HRESULT hrc = com::Initialize();
|
---|
907 | if (FAILED(hrc))
|
---|
908 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to initialize COM! hrc=%Rhrc\n", hrc);
|
---|
909 |
|
---|
910 | hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
911 | if (FAILED(hrc))
|
---|
912 | {
|
---|
913 | RTMsgError("failed to create the VirtualBoxClient object!");
|
---|
914 | com::ErrorInfo info;
|
---|
915 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
916 | {
|
---|
917 | com::GluePrintRCMessage(hrc);
|
---|
918 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
919 | }
|
---|
920 | else
|
---|
921 | com::GluePrintErrorInfo(info);
|
---|
922 | return RTEXITCODE_FAILURE;
|
---|
923 | }
|
---|
924 |
|
---|
925 | hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
|
---|
926 | if (FAILED(hrc))
|
---|
927 | {
|
---|
928 | RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", hrc);
|
---|
929 | return RTEXITCODE_FAILURE;
|
---|
930 | }
|
---|
931 |
|
---|
932 | /* VirtualBoxClient events registration. */
|
---|
933 | ComPtr<IEventListener> vboxClientListener;
|
---|
934 | {
|
---|
935 | ComPtr<IEventSource> pES;
|
---|
936 | CHECK_ERROR(g_pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam()));
|
---|
937 | ComObjPtr<VirtualBoxClientEventListenerImpl> clientListener;
|
---|
938 | clientListener.createObject();
|
---|
939 | clientListener->init(new VirtualBoxClientEventListener());
|
---|
940 | vboxClientListener = clientListener;
|
---|
941 | com::SafeArray<VBoxEventType_T> eventTypes;
|
---|
942 | eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged);
|
---|
943 | CHECK_ERROR(pES, RegisterListener(vboxClientListener, ComSafeArrayAsInParam(eventTypes), true));
|
---|
944 | }
|
---|
945 |
|
---|
946 | // create the global mutexes
|
---|
947 | g_pAuthLibLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
|
---|
948 | g_pVirtualBoxLockHandle = new util::RWLockHandle(util::LOCKCLASS_WEBSERVICE);
|
---|
949 | g_pSessionsLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
|
---|
950 | g_pThreadsLockHandle = new util::RWLockHandle(util::LOCKCLASS_OBJECTSTATE);
|
---|
951 | g_pWebLogLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
|
---|
952 |
|
---|
953 | // SOAP queue pumper thread
|
---|
954 | rc = RTThreadCreate(NULL,
|
---|
955 | fntQPumper,
|
---|
956 | NULL, // pvUser
|
---|
957 | 0, // cbStack (default)
|
---|
958 | RTTHREADTYPE_MAIN_WORKER,
|
---|
959 | 0, // flags
|
---|
960 | "SQPmp");
|
---|
961 | if (RT_FAILURE(rc))
|
---|
962 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot start SOAP queue pumper thread: %Rrc", rc);
|
---|
963 |
|
---|
964 | // watchdog thread
|
---|
965 | if (g_iWatchdogTimeoutSecs > 0)
|
---|
966 | {
|
---|
967 | // start our watchdog thread
|
---|
968 | rc = RTThreadCreate(NULL,
|
---|
969 | fntWatchdog,
|
---|
970 | NULL,
|
---|
971 | 0,
|
---|
972 | RTTHREADTYPE_MAIN_WORKER,
|
---|
973 | 0,
|
---|
974 | "Watchdog");
|
---|
975 | if (RT_FAILURE(rc))
|
---|
976 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot start watchdog thread: %Rrc", rc);
|
---|
977 | }
|
---|
978 |
|
---|
979 | com::EventQueue *pQ = com::EventQueue::getMainEventQueue();
|
---|
980 | for (;;)
|
---|
981 | {
|
---|
982 | // we have to process main event queue
|
---|
983 | WEBDEBUG(("Pumping COM event queue\n"));
|
---|
984 | rc = pQ->processEventQueue(RT_INDEFINITE_WAIT);
|
---|
985 | if (RT_FAILURE(rc))
|
---|
986 | RTMsgError("processEventQueue -> %Rrc", rc);
|
---|
987 | }
|
---|
988 |
|
---|
989 | /* VirtualBoxClient events unregistration. */
|
---|
990 | if (vboxClientListener)
|
---|
991 | {
|
---|
992 | ComPtr<IEventSource> pES;
|
---|
993 | CHECK_ERROR(g_pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam()));
|
---|
994 | if (!pES.isNull())
|
---|
995 | CHECK_ERROR(pES, UnregisterListener(vboxClientListener));
|
---|
996 | vboxClientListener.setNull();
|
---|
997 | }
|
---|
998 |
|
---|
999 | com::Shutdown();
|
---|
1000 |
|
---|
1001 | return 0;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /****************************************************************************
|
---|
1005 | *
|
---|
1006 | * Watchdog thread
|
---|
1007 | *
|
---|
1008 | ****************************************************************************/
|
---|
1009 |
|
---|
1010 | /**
|
---|
1011 | * Watchdog thread, runs in the background while the webservice is alive.
|
---|
1012 | *
|
---|
1013 | * This gets started by main() and runs in the background to check all sessions
|
---|
1014 | * for whether they have been no requests in a configurable timeout period. In
|
---|
1015 | * that case, the session is automatically logged off.
|
---|
1016 | */
|
---|
1017 | int fntWatchdog(RTTHREAD ThreadSelf, void *pvUser)
|
---|
1018 | {
|
---|
1019 | // store a log prefix for this thread
|
---|
1020 | util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1021 | g_mapThreads[RTThreadSelf()] = "[W ]";
|
---|
1022 | thrLock.release();
|
---|
1023 |
|
---|
1024 | WEBDEBUG(("Watchdog thread started\n"));
|
---|
1025 |
|
---|
1026 | while (1)
|
---|
1027 | {
|
---|
1028 | WEBDEBUG(("Watchdog: sleeping %d seconds\n", g_iWatchdogCheckInterval));
|
---|
1029 | RTThreadSleep(g_iWatchdogCheckInterval * 1000);
|
---|
1030 |
|
---|
1031 | time_t tNow;
|
---|
1032 | time(&tNow);
|
---|
1033 |
|
---|
1034 | // we're messing with sessions, so lock them
|
---|
1035 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1036 | WEBDEBUG(("Watchdog: checking %d sessions\n", g_mapSessions.size()));
|
---|
1037 |
|
---|
1038 | SessionsMap::iterator it = g_mapSessions.begin(),
|
---|
1039 | itEnd = g_mapSessions.end();
|
---|
1040 | while (it != itEnd)
|
---|
1041 | {
|
---|
1042 | WebServiceSession *pSession = it->second;
|
---|
1043 | WEBDEBUG(("Watchdog: tNow: %d, session timestamp: %d\n", tNow, pSession->getLastObjectLookup()));
|
---|
1044 | if ( tNow
|
---|
1045 | > pSession->getLastObjectLookup() + g_iWatchdogTimeoutSecs
|
---|
1046 | )
|
---|
1047 | {
|
---|
1048 | WEBDEBUG(("Watchdog: Session %llX timed out, deleting\n", pSession->getID()));
|
---|
1049 | delete pSession;
|
---|
1050 | it = g_mapSessions.begin();
|
---|
1051 | }
|
---|
1052 | else
|
---|
1053 | ++it;
|
---|
1054 | }
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | WEBDEBUG(("Watchdog thread ending\n"));
|
---|
1058 | return 0;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | /****************************************************************************
|
---|
1062 | *
|
---|
1063 | * SOAP exceptions
|
---|
1064 | *
|
---|
1065 | ****************************************************************************/
|
---|
1066 |
|
---|
1067 | /**
|
---|
1068 | * Helper function to raise a SOAP fault. Called by the other helper
|
---|
1069 | * functions, which raise specific SOAP faults.
|
---|
1070 | *
|
---|
1071 | * @param soap
|
---|
1072 | * @param str
|
---|
1073 | * @param extype
|
---|
1074 | * @param ex
|
---|
1075 | */
|
---|
1076 | void RaiseSoapFault(struct soap *soap,
|
---|
1077 | const char *pcsz,
|
---|
1078 | int extype,
|
---|
1079 | void *ex)
|
---|
1080 | {
|
---|
1081 | // raise the fault
|
---|
1082 | soap_sender_fault(soap, pcsz, NULL);
|
---|
1083 |
|
---|
1084 | struct SOAP_ENV__Detail *pDetail = (struct SOAP_ENV__Detail*)soap_malloc(soap, sizeof(struct SOAP_ENV__Detail));
|
---|
1085 |
|
---|
1086 | // without the following, gSOAP crashes miserably when sending out the
|
---|
1087 | // data because it will try to serialize all fields (stupid documentation)
|
---|
1088 | memset(pDetail, 0, sizeof(struct SOAP_ENV__Detail));
|
---|
1089 |
|
---|
1090 | // fill extended info depending on SOAP version
|
---|
1091 | if (soap->version == 2) // SOAP 1.2 is used
|
---|
1092 | {
|
---|
1093 | soap->fault->SOAP_ENV__Detail = pDetail;
|
---|
1094 | soap->fault->SOAP_ENV__Detail->__type = extype;
|
---|
1095 | soap->fault->SOAP_ENV__Detail->fault = ex;
|
---|
1096 | soap->fault->SOAP_ENV__Detail->__any = NULL; // no other XML data
|
---|
1097 | }
|
---|
1098 | else
|
---|
1099 | {
|
---|
1100 | soap->fault->detail = pDetail;
|
---|
1101 | soap->fault->detail->__type = extype;
|
---|
1102 | soap->fault->detail->fault = ex;
|
---|
1103 | soap->fault->detail->__any = NULL; // no other XML data
|
---|
1104 | }
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Raises a SOAP fault that signals that an invalid object was passed.
|
---|
1109 | *
|
---|
1110 | * @param soap
|
---|
1111 | * @param obj
|
---|
1112 | */
|
---|
1113 | void RaiseSoapInvalidObjectFault(struct soap *soap,
|
---|
1114 | WSDLT_ID obj)
|
---|
1115 | {
|
---|
1116 | _vbox__InvalidObjectFault *ex = soap_new__vbox__InvalidObjectFault(soap, 1);
|
---|
1117 | ex->badObjectID = obj;
|
---|
1118 |
|
---|
1119 | std::string str("VirtualBox error: ");
|
---|
1120 | str += "Invalid managed object reference \"" + obj + "\"";
|
---|
1121 |
|
---|
1122 | RaiseSoapFault(soap,
|
---|
1123 | str.c_str(),
|
---|
1124 | SOAP_TYPE__vbox__InvalidObjectFault,
|
---|
1125 | ex);
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | /**
|
---|
1129 | * Return a safe C++ string from the given COM string,
|
---|
1130 | * without crashing if the COM string is empty.
|
---|
1131 | * @param bstr
|
---|
1132 | * @return
|
---|
1133 | */
|
---|
1134 | std::string ConvertComString(const com::Bstr &bstr)
|
---|
1135 | {
|
---|
1136 | com::Utf8Str ustr(bstr);
|
---|
1137 | return ustr.c_str(); // @todo r=dj since the length is known, we can probably use a better std::string allocator
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /**
|
---|
1141 | * Return a safe C++ string from the given COM UUID,
|
---|
1142 | * without crashing if the UUID is empty.
|
---|
1143 | * @param bstr
|
---|
1144 | * @return
|
---|
1145 | */
|
---|
1146 | std::string ConvertComString(const com::Guid &uuid)
|
---|
1147 | {
|
---|
1148 | com::Utf8Str ustr(uuid.toString());
|
---|
1149 | return ustr.c_str(); // @todo r=dj since the length is known, we can probably use a better std::string allocator
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | /**
|
---|
1153 | * Raises a SOAP runtime fault. Implementation for the RaiseSoapRuntimeFault template
|
---|
1154 | * function in vboxweb.h.
|
---|
1155 | *
|
---|
1156 | * @param pObj
|
---|
1157 | */
|
---|
1158 | void RaiseSoapRuntimeFault2(struct soap *soap,
|
---|
1159 | HRESULT apirc,
|
---|
1160 | IUnknown *pObj,
|
---|
1161 | const com::Guid &iid)
|
---|
1162 | {
|
---|
1163 | com::ErrorInfo info(pObj, iid.ref());
|
---|
1164 |
|
---|
1165 | WEBDEBUG((" error, raising SOAP exception\n"));
|
---|
1166 |
|
---|
1167 | RTStrmPrintf(g_pStdErr, "API return code: 0x%08X (%Rhrc)\n", apirc, apirc);
|
---|
1168 | RTStrmPrintf(g_pStdErr, "COM error info result code: 0x%lX\n", info.getResultCode());
|
---|
1169 | RTStrmPrintf(g_pStdErr, "COM error info text: %ls\n", info.getText().raw());
|
---|
1170 |
|
---|
1171 | // allocated our own soap fault struct
|
---|
1172 | _vbox__RuntimeFault *ex = soap_new__vbox__RuntimeFault(soap, 1);
|
---|
1173 | // some old vbox methods return errors without setting an error in the error info,
|
---|
1174 | // so use the error info code if it's set and the HRESULT from the method otherwise
|
---|
1175 | if (S_OK == (ex->resultCode = info.getResultCode()))
|
---|
1176 | ex->resultCode = apirc;
|
---|
1177 | ex->text = ConvertComString(info.getText());
|
---|
1178 | ex->component = ConvertComString(info.getComponent());
|
---|
1179 | ex->interfaceID = ConvertComString(info.getInterfaceID());
|
---|
1180 |
|
---|
1181 | // compose descriptive message
|
---|
1182 | com::Utf8StrFmt str("VirtualBox error: %s (0x%lX)", ex->text.c_str(), ex->resultCode);
|
---|
1183 |
|
---|
1184 | RaiseSoapFault(soap,
|
---|
1185 | str.c_str(),
|
---|
1186 | SOAP_TYPE__vbox__RuntimeFault,
|
---|
1187 | ex);
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /****************************************************************************
|
---|
1191 | *
|
---|
1192 | * splitting and merging of object IDs
|
---|
1193 | *
|
---|
1194 | ****************************************************************************/
|
---|
1195 |
|
---|
1196 | uint64_t str2ulonglong(const char *pcsz)
|
---|
1197 | {
|
---|
1198 | uint64_t u = 0;
|
---|
1199 | RTStrToUInt64Full(pcsz, 16, &u);
|
---|
1200 | return u;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | /**
|
---|
1204 | * Splits a managed object reference (in string form, as
|
---|
1205 | * passed in from a SOAP method call) into two integers for
|
---|
1206 | * session and object IDs, respectively.
|
---|
1207 | *
|
---|
1208 | * @param id
|
---|
1209 | * @param sessid
|
---|
1210 | * @param objid
|
---|
1211 | * @return
|
---|
1212 | */
|
---|
1213 | bool SplitManagedObjectRef(const WSDLT_ID &id,
|
---|
1214 | uint64_t *pSessid,
|
---|
1215 | uint64_t *pObjid)
|
---|
1216 | {
|
---|
1217 | // 64-bit numbers in hex have 16 digits; hence
|
---|
1218 | // the object-ref string must have 16 + "-" + 16 characters
|
---|
1219 | std::string str;
|
---|
1220 | if ( (id.length() == 33)
|
---|
1221 | && (id[16] == '-')
|
---|
1222 | )
|
---|
1223 | {
|
---|
1224 | char psz[34];
|
---|
1225 | memcpy(psz, id.c_str(), 34);
|
---|
1226 | psz[16] = '\0';
|
---|
1227 | if (pSessid)
|
---|
1228 | *pSessid = str2ulonglong(psz);
|
---|
1229 | if (pObjid)
|
---|
1230 | *pObjid = str2ulonglong(psz + 17);
|
---|
1231 | return true;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | return false;
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | /**
|
---|
1238 | * Creates a managed object reference (in string form) from
|
---|
1239 | * two integers representing a session and object ID, respectively.
|
---|
1240 | *
|
---|
1241 | * @param sz Buffer with at least 34 bytes space to receive MOR string.
|
---|
1242 | * @param sessid
|
---|
1243 | * @param objid
|
---|
1244 | * @return
|
---|
1245 | */
|
---|
1246 | void MakeManagedObjectRef(char *sz,
|
---|
1247 | uint64_t &sessid,
|
---|
1248 | uint64_t &objid)
|
---|
1249 | {
|
---|
1250 | RTStrFormatNumber(sz, sessid, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
|
---|
1251 | sz[16] = '-';
|
---|
1252 | RTStrFormatNumber(sz + 17, objid, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | /****************************************************************************
|
---|
1256 | *
|
---|
1257 | * class WebServiceSession
|
---|
1258 | *
|
---|
1259 | ****************************************************************************/
|
---|
1260 |
|
---|
1261 | class WebServiceSessionPrivate
|
---|
1262 | {
|
---|
1263 | public:
|
---|
1264 | ManagedObjectsMapById _mapManagedObjectsById;
|
---|
1265 | ManagedObjectsMapByPtr _mapManagedObjectsByPtr;
|
---|
1266 | };
|
---|
1267 |
|
---|
1268 | /**
|
---|
1269 | * Constructor for the session object.
|
---|
1270 | *
|
---|
1271 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1272 | *
|
---|
1273 | * @param username
|
---|
1274 | * @param password
|
---|
1275 | */
|
---|
1276 | WebServiceSession::WebServiceSession()
|
---|
1277 | : _fDestructing(false),
|
---|
1278 | _pISession(NULL),
|
---|
1279 | _tLastObjectLookup(0)
|
---|
1280 | {
|
---|
1281 | _pp = new WebServiceSessionPrivate;
|
---|
1282 | _uSessionID = RTRandU64();
|
---|
1283 |
|
---|
1284 | // register this session globally
|
---|
1285 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1286 | g_mapSessions[_uSessionID] = this;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /**
|
---|
1290 | * Destructor. Cleans up and destroys all contained managed object references on the way.
|
---|
1291 | *
|
---|
1292 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1293 | */
|
---|
1294 | WebServiceSession::~WebServiceSession()
|
---|
1295 | {
|
---|
1296 | // delete us from global map first so we can't be found
|
---|
1297 | // any more while we're cleaning up
|
---|
1298 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1299 | g_mapSessions.erase(_uSessionID);
|
---|
1300 |
|
---|
1301 | // notify ManagedObjectRef destructor so it won't
|
---|
1302 | // remove itself from the maps; this avoids rebalancing
|
---|
1303 | // the map's tree on every delete as well
|
---|
1304 | _fDestructing = true;
|
---|
1305 |
|
---|
1306 | // if (_pISession)
|
---|
1307 | // {
|
---|
1308 | // delete _pISession;
|
---|
1309 | // _pISession = NULL;
|
---|
1310 | // }
|
---|
1311 |
|
---|
1312 | ManagedObjectsMapById::iterator it,
|
---|
1313 | end = _pp->_mapManagedObjectsById.end();
|
---|
1314 | for (it = _pp->_mapManagedObjectsById.begin();
|
---|
1315 | it != end;
|
---|
1316 | ++it)
|
---|
1317 | {
|
---|
1318 | ManagedObjectRef *pRef = it->second;
|
---|
1319 | delete pRef; // this frees the contained ComPtr as well
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | delete _pp;
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | /**
|
---|
1326 | * Authenticate the username and password against an authentication authority.
|
---|
1327 | *
|
---|
1328 | * @return 0 if the user was successfully authenticated, or an error code
|
---|
1329 | * otherwise.
|
---|
1330 | */
|
---|
1331 |
|
---|
1332 | int WebServiceSession::authenticate(const char *pcszUsername,
|
---|
1333 | const char *pcszPassword,
|
---|
1334 | IVirtualBox **ppVirtualBox)
|
---|
1335 | {
|
---|
1336 | int rc = VERR_WEB_NOT_AUTHENTICATED;
|
---|
1337 | ComPtr<IVirtualBox> pVirtualBox;
|
---|
1338 | {
|
---|
1339 | util::AutoReadLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1340 | pVirtualBox = g_pVirtualBox;
|
---|
1341 | }
|
---|
1342 | pVirtualBox.queryInterfaceTo(ppVirtualBox);
|
---|
1343 | if (pVirtualBox.isNull())
|
---|
1344 | return rc;
|
---|
1345 |
|
---|
1346 | util::AutoReadLock lock(g_pAuthLibLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1347 |
|
---|
1348 | static bool fAuthLibLoaded = false;
|
---|
1349 | static PAUTHENTRY pfnAuthEntry = NULL;
|
---|
1350 | static PAUTHENTRY2 pfnAuthEntry2 = NULL;
|
---|
1351 | static PAUTHENTRY3 pfnAuthEntry3 = NULL;
|
---|
1352 |
|
---|
1353 | if (!fAuthLibLoaded)
|
---|
1354 | {
|
---|
1355 | // retrieve authentication library from system properties
|
---|
1356 | ComPtr<ISystemProperties> systemProperties;
|
---|
1357 | pVirtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());
|
---|
1358 |
|
---|
1359 | com::Bstr authLibrary;
|
---|
1360 | systemProperties->COMGETTER(WebServiceAuthLibrary)(authLibrary.asOutParam());
|
---|
1361 | com::Utf8Str filename = authLibrary;
|
---|
1362 |
|
---|
1363 | WEBDEBUG(("external authentication library is '%ls'\n", authLibrary.raw()));
|
---|
1364 |
|
---|
1365 | if (filename == "null")
|
---|
1366 | // authentication disabled, let everyone in:
|
---|
1367 | fAuthLibLoaded = true;
|
---|
1368 | else
|
---|
1369 | {
|
---|
1370 | RTLDRMOD hlibAuth = 0;
|
---|
1371 | do
|
---|
1372 | {
|
---|
1373 | rc = RTLdrLoad(filename.c_str(), &hlibAuth);
|
---|
1374 | if (RT_FAILURE(rc))
|
---|
1375 | {
|
---|
1376 | WEBDEBUG(("%s() Failed to load external authentication library. Error code: %Rrc\n", __FUNCTION__, rc));
|
---|
1377 | break;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, AUTHENTRY3_NAME, (void**)&pfnAuthEntry3)))
|
---|
1381 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, AUTHENTRY3_NAME, rc));
|
---|
1382 |
|
---|
1383 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, AUTHENTRY2_NAME, (void**)&pfnAuthEntry2)))
|
---|
1384 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, AUTHENTRY2_NAME, rc));
|
---|
1385 |
|
---|
1386 | if (RT_FAILURE(rc = RTLdrGetSymbol(hlibAuth, AUTHENTRY_NAME, (void**)&pfnAuthEntry)))
|
---|
1387 | WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n", __FUNCTION__, AUTHENTRY_NAME, rc));
|
---|
1388 |
|
---|
1389 | if (pfnAuthEntry || pfnAuthEntry2 || pfnAuthEntry3)
|
---|
1390 | fAuthLibLoaded = true;
|
---|
1391 |
|
---|
1392 | } while (0);
|
---|
1393 | }
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | rc = VERR_WEB_NOT_AUTHENTICATED;
|
---|
1397 | AuthResult result;
|
---|
1398 | if (pfnAuthEntry3)
|
---|
1399 | {
|
---|
1400 | result = pfnAuthEntry3("webservice", NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
|
---|
1401 | WEBDEBUG(("%s(): result of AuthEntry(): %d\n", __FUNCTION__, result));
|
---|
1402 | if (result == AuthResultAccessGranted)
|
---|
1403 | rc = 0;
|
---|
1404 | }
|
---|
1405 | else if (pfnAuthEntry2)
|
---|
1406 | {
|
---|
1407 | result = pfnAuthEntry2(NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
|
---|
1408 | WEBDEBUG(("%s(): result of VRDPAuth2(): %d\n", __FUNCTION__, result));
|
---|
1409 | if (result == AuthResultAccessGranted)
|
---|
1410 | rc = 0;
|
---|
1411 | }
|
---|
1412 | else if (pfnAuthEntry)
|
---|
1413 | {
|
---|
1414 | result = pfnAuthEntry(NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL);
|
---|
1415 | WEBDEBUG(("%s(): result of VRDPAuth(%s, [%d]): %d\n", __FUNCTION__, pcszUsername, strlen(pcszPassword), result));
|
---|
1416 | if (result == AuthResultAccessGranted)
|
---|
1417 | rc = 0;
|
---|
1418 | }
|
---|
1419 | else if (fAuthLibLoaded)
|
---|
1420 | // fAuthLibLoaded = true but both pointers are NULL:
|
---|
1421 | // then the authlib was "null" and auth was disabled
|
---|
1422 | rc = 0;
|
---|
1423 | else
|
---|
1424 | {
|
---|
1425 | WEBDEBUG(("Could not resolve AuthEntry, VRDPAuth2 or VRDPAuth entry point"));
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | lock.release();
|
---|
1429 |
|
---|
1430 | if (!rc)
|
---|
1431 | {
|
---|
1432 | do
|
---|
1433 | {
|
---|
1434 | // now create the ISession object that this webservice session can use
|
---|
1435 | // (and of which IWebsessionManager::getSessionObject returns a managed object reference)
|
---|
1436 | ComPtr<ISession> session;
|
---|
1437 | rc = g_pVirtualBoxClient->COMGETTER(Session)(session.asOutParam());
|
---|
1438 | if (FAILED(rc))
|
---|
1439 | {
|
---|
1440 | WEBDEBUG(("ERROR: cannot create session object!"));
|
---|
1441 | break;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | ComPtr<IUnknown> p2 = session;
|
---|
1445 | _pISession = new ManagedObjectRef(*this,
|
---|
1446 | p2, // IUnknown *pobjUnknown
|
---|
1447 | session, // void *pobjInterface
|
---|
1448 | com::Guid(COM_IIDOF(ISession)),
|
---|
1449 | g_pcszISession);
|
---|
1450 |
|
---|
1451 | if (g_fVerbose)
|
---|
1452 | {
|
---|
1453 | ISession *p = session;
|
---|
1454 | WEBDEBUG((" * %s: created session object with comptr 0x%lX, MOR = %s\n", __FUNCTION__, p, _pISession->getWSDLID().c_str()));
|
---|
1455 | }
|
---|
1456 | } while (0);
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | return rc;
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | /**
|
---|
1463 | * Look up, in this session, whether a ManagedObjectRef has already been
|
---|
1464 | * created for the given COM pointer.
|
---|
1465 | *
|
---|
1466 | * Note how we require that a ComPtr<IUnknown> is passed, which causes a
|
---|
1467 | * queryInterface call when the caller passes in a different type, since
|
---|
1468 | * a ComPtr<IUnknown> will point to something different than a
|
---|
1469 | * ComPtr<IVirtualBox>, for example. As we store the ComPtr<IUnknown> in
|
---|
1470 | * our private hash table, we must search for one too.
|
---|
1471 | *
|
---|
1472 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1473 | *
|
---|
1474 | * @param pcu pointer to a COM object.
|
---|
1475 | * @return The existing ManagedObjectRef that represents the COM object, or NULL if there's none yet.
|
---|
1476 | */
|
---|
1477 | ManagedObjectRef* WebServiceSession::findRefFromPtr(const IUnknown *pObject)
|
---|
1478 | {
|
---|
1479 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1480 |
|
---|
1481 | uintptr_t ulp = (uintptr_t)pObject;
|
---|
1482 | // WEBDEBUG((" %s: looking up 0x%lX\n", __FUNCTION__, ulp));
|
---|
1483 | ManagedObjectsMapByPtr::iterator it = _pp->_mapManagedObjectsByPtr.find(ulp);
|
---|
1484 | if (it != _pp->_mapManagedObjectsByPtr.end())
|
---|
1485 | {
|
---|
1486 | ManagedObjectRef *pRef = it->second;
|
---|
1487 | WEBDEBUG((" %s: found existing ref %s (%s) for COM obj 0x%lX\n", __FUNCTION__, pRef->getWSDLID().c_str(), pRef->getInterfaceName(), ulp));
|
---|
1488 | return pRef;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | return NULL;
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | /**
|
---|
1495 | * Static method which attempts to find the session for which the given managed
|
---|
1496 | * object reference was created, by splitting the reference into the session and
|
---|
1497 | * object IDs and then looking up the session object for that session ID.
|
---|
1498 | *
|
---|
1499 | * Preconditions: Caller must have locked g_pSessionsLockHandle in read mode.
|
---|
1500 | *
|
---|
1501 | * @param id Managed object reference (with combined session and object IDs).
|
---|
1502 | * @return
|
---|
1503 | */
|
---|
1504 | WebServiceSession* WebServiceSession::findSessionFromRef(const WSDLT_ID &id)
|
---|
1505 | {
|
---|
1506 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1507 |
|
---|
1508 | WebServiceSession *pSession = NULL;
|
---|
1509 | uint64_t sessid;
|
---|
1510 | if (SplitManagedObjectRef(id,
|
---|
1511 | &sessid,
|
---|
1512 | NULL))
|
---|
1513 | {
|
---|
1514 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
1515 | if (it != g_mapSessions.end())
|
---|
1516 | pSession = it->second;
|
---|
1517 | }
|
---|
1518 | return pSession;
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | /**
|
---|
1522 | *
|
---|
1523 | */
|
---|
1524 | const WSDLT_ID& WebServiceSession::getSessionWSDLID() const
|
---|
1525 | {
|
---|
1526 | return _pISession->getWSDLID();
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | /**
|
---|
1530 | * Touches the webservice session to prevent it from timing out.
|
---|
1531 | *
|
---|
1532 | * Each webservice session has an internal timestamp that records
|
---|
1533 | * the last request made to it from the client that started it.
|
---|
1534 | * If no request was made within a configurable timeframe, then
|
---|
1535 | * the client is logged off automatically,
|
---|
1536 | * by calling IWebsessionManager::logoff()
|
---|
1537 | */
|
---|
1538 | void WebServiceSession::touch()
|
---|
1539 | {
|
---|
1540 | time(&_tLastObjectLookup);
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 |
|
---|
1544 | /****************************************************************************
|
---|
1545 | *
|
---|
1546 | * class ManagedObjectRef
|
---|
1547 | *
|
---|
1548 | ****************************************************************************/
|
---|
1549 |
|
---|
1550 | /**
|
---|
1551 | * Constructor, which assigns a unique ID to this managed object
|
---|
1552 | * reference and stores it two global hashes:
|
---|
1553 | *
|
---|
1554 | * a) G_mapManagedObjectsById, which maps ManagedObjectID's to
|
---|
1555 | * instances of this class; this hash is then used by the
|
---|
1556 | * findObjectFromRef() template function in vboxweb.h
|
---|
1557 | * to quickly retrieve the COM object from its managed
|
---|
1558 | * object ID (mostly in the context of the method mappers
|
---|
1559 | * in methodmaps.cpp, when a web service client passes in
|
---|
1560 | * a managed object ID);
|
---|
1561 | *
|
---|
1562 | * b) G_mapManagedObjectsByComPtr, which maps COM pointers to
|
---|
1563 | * instances of this class; this hash is used by
|
---|
1564 | * createRefFromObject() to quickly figure out whether an
|
---|
1565 | * instance already exists for a given COM pointer.
|
---|
1566 | *
|
---|
1567 | * This constructor calls AddRef() on the given COM object, and
|
---|
1568 | * the destructor will call Release(). We require two input pointers
|
---|
1569 | * for that COM object, one generic IUnknown* pointer which is used
|
---|
1570 | * as the map key, and a specific interface pointer (e.g. IMachine*)
|
---|
1571 | * which must support the interface given in guidInterface. All
|
---|
1572 | * three values are returned by getPtr(), which gives future callers
|
---|
1573 | * a chance to reuse the specific interface pointer without having
|
---|
1574 | * to call QueryInterface, which can be expensive.
|
---|
1575 | *
|
---|
1576 | * This does _not_ check whether another instance already
|
---|
1577 | * exists in the hash. This gets called only from the
|
---|
1578 | * createOrFindRefFromComPtr() template function in vboxweb.h, which
|
---|
1579 | * does perform that check.
|
---|
1580 | *
|
---|
1581 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1582 | *
|
---|
1583 | * @param session Session to which the MOR will be added.
|
---|
1584 | * @param pobjUnknown Pointer to IUnknown* interface for the COM object; this will be used in the hashes.
|
---|
1585 | * @param pobjInterface Pointer to a specific interface for the COM object, described by guidInterface.
|
---|
1586 | * @param guidInterface Interface which pobjInterface points to.
|
---|
1587 | * @param pcszInterface String representation of that interface (e.g. "IMachine") for readability and logging.
|
---|
1588 | */
|
---|
1589 | ManagedObjectRef::ManagedObjectRef(WebServiceSession &session,
|
---|
1590 | IUnknown *pobjUnknown,
|
---|
1591 | void *pobjInterface,
|
---|
1592 | const com::Guid &guidInterface,
|
---|
1593 | const char *pcszInterface)
|
---|
1594 | : _session(session),
|
---|
1595 | _pobjUnknown(pobjUnknown),
|
---|
1596 | _pobjInterface(pobjInterface),
|
---|
1597 | _guidInterface(guidInterface),
|
---|
1598 | _pcszInterface(pcszInterface)
|
---|
1599 | {
|
---|
1600 | Assert(pobjUnknown);
|
---|
1601 | Assert(pobjInterface);
|
---|
1602 |
|
---|
1603 | // keep both stubs alive while this MOR exists (matching Release() calls are in destructor)
|
---|
1604 | uint32_t cRefs1 = pobjUnknown->AddRef();
|
---|
1605 | uint32_t cRefs2 = ((IUnknown*)pobjInterface)->AddRef();
|
---|
1606 | _ulp = (uintptr_t)pobjUnknown;
|
---|
1607 |
|
---|
1608 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1609 | _id = ++g_iMaxManagedObjectID;
|
---|
1610 | // and count globally
|
---|
1611 | ULONG64 cTotal = ++g_cManagedObjects; // raise global count and make a copy for the debug message below
|
---|
1612 |
|
---|
1613 | char sz[34];
|
---|
1614 | MakeManagedObjectRef(sz, session._uSessionID, _id);
|
---|
1615 | _strID = sz;
|
---|
1616 |
|
---|
1617 | session._pp->_mapManagedObjectsById[_id] = this;
|
---|
1618 | session._pp->_mapManagedObjectsByPtr[_ulp] = this;
|
---|
1619 |
|
---|
1620 | session.touch();
|
---|
1621 |
|
---|
1622 | 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",
|
---|
1623 | __FUNCTION__,
|
---|
1624 | pcszInterface,
|
---|
1625 | pobjInterface,
|
---|
1626 | pobjUnknown,
|
---|
1627 | cRefs1,
|
---|
1628 | cRefs2,
|
---|
1629 | _id,
|
---|
1630 | cTotal));
|
---|
1631 | }
|
---|
1632 |
|
---|
1633 | /**
|
---|
1634 | * Destructor; removes the instance from the global hash of
|
---|
1635 | * managed objects. Calls Release() on the contained COM object.
|
---|
1636 | *
|
---|
1637 | * Preconditions: Caller must have locked g_pSessionsLockHandle.
|
---|
1638 | */
|
---|
1639 | ManagedObjectRef::~ManagedObjectRef()
|
---|
1640 | {
|
---|
1641 | Assert(g_pSessionsLockHandle->isWriteLockOnCurrentThread());
|
---|
1642 | ULONG64 cTotal = --g_cManagedObjects;
|
---|
1643 |
|
---|
1644 | Assert(_pobjUnknown);
|
---|
1645 | Assert(_pobjInterface);
|
---|
1646 |
|
---|
1647 | // we called AddRef() on both interfaces, so call Release() on
|
---|
1648 | // both as well, but in reverse order
|
---|
1649 | uint32_t cRefs2 = ((IUnknown*)_pobjInterface)->Release();
|
---|
1650 | uint32_t cRefs1 = _pobjUnknown->Release();
|
---|
1651 | WEBDEBUG((" * %s: deleting MOR for ID %llX (%s; COM refcount now %RI32/%RI32); now %lld objects total\n", __FUNCTION__, _id, _pcszInterface, cRefs1, cRefs2, cTotal));
|
---|
1652 |
|
---|
1653 | // if we're being destroyed from the session's destructor,
|
---|
1654 | // then that destructor is iterating over the maps, so
|
---|
1655 | // don't remove us there! (data integrity + speed)
|
---|
1656 | if (!_session._fDestructing)
|
---|
1657 | {
|
---|
1658 | WEBDEBUG((" * %s: removing from session maps\n", __FUNCTION__));
|
---|
1659 | _session._pp->_mapManagedObjectsById.erase(_id);
|
---|
1660 | if (_session._pp->_mapManagedObjectsByPtr.erase(_ulp) != 1)
|
---|
1661 | WEBDEBUG((" WARNING: could not find %llX in _mapManagedObjectsByPtr\n", _ulp));
|
---|
1662 | }
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | /**
|
---|
1666 | * Static helper method for findObjectFromRef() template that actually
|
---|
1667 | * looks up the object from a given integer ID.
|
---|
1668 | *
|
---|
1669 | * This has been extracted into this non-template function to reduce
|
---|
1670 | * code bloat as we have the actual STL map lookup only in this function.
|
---|
1671 | *
|
---|
1672 | * This also "touches" the timestamp in the session whose ID is encoded
|
---|
1673 | * in the given integer ID, in order to prevent the session from timing
|
---|
1674 | * out.
|
---|
1675 | *
|
---|
1676 | * Preconditions: Caller must have locked g_mutexSessions.
|
---|
1677 | *
|
---|
1678 | * @param strId
|
---|
1679 | * @param iter
|
---|
1680 | * @return
|
---|
1681 | */
|
---|
1682 | int ManagedObjectRef::findRefFromId(const WSDLT_ID &id,
|
---|
1683 | ManagedObjectRef **pRef,
|
---|
1684 | bool fNullAllowed)
|
---|
1685 | {
|
---|
1686 | int rc = 0;
|
---|
1687 |
|
---|
1688 | do
|
---|
1689 | {
|
---|
1690 | // allow NULL (== empty string) input reference, which should return a NULL pointer
|
---|
1691 | if (!id.length() && fNullAllowed)
|
---|
1692 | {
|
---|
1693 | *pRef = NULL;
|
---|
1694 | return 0;
|
---|
1695 | }
|
---|
1696 |
|
---|
1697 | uint64_t sessid;
|
---|
1698 | uint64_t objid;
|
---|
1699 | WEBDEBUG((" %s(): looking up objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1700 | if (!SplitManagedObjectRef(id,
|
---|
1701 | &sessid,
|
---|
1702 | &objid))
|
---|
1703 | {
|
---|
1704 | rc = VERR_WEB_INVALID_MANAGED_OBJECT_REFERENCE;
|
---|
1705 | break;
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | SessionsMapIterator it = g_mapSessions.find(sessid);
|
---|
1709 | if (it == g_mapSessions.end())
|
---|
1710 | {
|
---|
1711 | WEBDEBUG((" %s: cannot find session for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1712 | rc = VERR_WEB_INVALID_SESSION_ID;
|
---|
1713 | break;
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 | WebServiceSession *pSess = it->second;
|
---|
1717 | // "touch" session to prevent it from timing out
|
---|
1718 | pSess->touch();
|
---|
1719 |
|
---|
1720 | ManagedObjectsIteratorById iter = pSess->_pp->_mapManagedObjectsById.find(objid);
|
---|
1721 | if (iter == pSess->_pp->_mapManagedObjectsById.end())
|
---|
1722 | {
|
---|
1723 | WEBDEBUG((" %s: cannot find comobj for objref %s\n", __FUNCTION__, id.c_str()));
|
---|
1724 | rc = VERR_WEB_INVALID_OBJECT_ID;
|
---|
1725 | break;
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | *pRef = iter->second;
|
---|
1729 |
|
---|
1730 | } while (0);
|
---|
1731 |
|
---|
1732 | return rc;
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | /****************************************************************************
|
---|
1736 | *
|
---|
1737 | * interface IManagedObjectRef
|
---|
1738 | *
|
---|
1739 | ****************************************************************************/
|
---|
1740 |
|
---|
1741 | /**
|
---|
1742 | * This is the hard-coded implementation for the IManagedObjectRef::getInterfaceName()
|
---|
1743 | * that our WSDL promises to our web service clients. This method returns a
|
---|
1744 | * string describing the interface that this managed object reference
|
---|
1745 | * supports, e.g. "IMachine".
|
---|
1746 | *
|
---|
1747 | * @param soap
|
---|
1748 | * @param req
|
---|
1749 | * @param resp
|
---|
1750 | * @return
|
---|
1751 | */
|
---|
1752 | int __vbox__IManagedObjectRef_USCOREgetInterfaceName(
|
---|
1753 | struct soap *soap,
|
---|
1754 | _vbox__IManagedObjectRef_USCOREgetInterfaceName *req,
|
---|
1755 | _vbox__IManagedObjectRef_USCOREgetInterfaceNameResponse *resp)
|
---|
1756 | {
|
---|
1757 | HRESULT rc = S_OK;
|
---|
1758 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1759 |
|
---|
1760 | do
|
---|
1761 | {
|
---|
1762 | // findRefFromId require the lock
|
---|
1763 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1764 |
|
---|
1765 | ManagedObjectRef *pRef;
|
---|
1766 | if (!ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false))
|
---|
1767 | resp->returnval = pRef->getInterfaceName();
|
---|
1768 |
|
---|
1769 | } while (0);
|
---|
1770 |
|
---|
1771 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1772 | if (FAILED(rc))
|
---|
1773 | return SOAP_FAULT;
|
---|
1774 | return SOAP_OK;
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | /**
|
---|
1778 | * This is the hard-coded implementation for the IManagedObjectRef::release()
|
---|
1779 | * that our WSDL promises to our web service clients. This method releases
|
---|
1780 | * a managed object reference and removes it from our stacks.
|
---|
1781 | *
|
---|
1782 | * @param soap
|
---|
1783 | * @param req
|
---|
1784 | * @param resp
|
---|
1785 | * @return
|
---|
1786 | */
|
---|
1787 | int __vbox__IManagedObjectRef_USCORErelease(
|
---|
1788 | struct soap *soap,
|
---|
1789 | _vbox__IManagedObjectRef_USCORErelease *req,
|
---|
1790 | _vbox__IManagedObjectRef_USCOREreleaseResponse *resp)
|
---|
1791 | {
|
---|
1792 | HRESULT rc = S_OK;
|
---|
1793 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1794 |
|
---|
1795 | do
|
---|
1796 | {
|
---|
1797 | // findRefFromId and the delete call below require the lock
|
---|
1798 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1799 |
|
---|
1800 | ManagedObjectRef *pRef;
|
---|
1801 | if ((rc = ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false)))
|
---|
1802 | {
|
---|
1803 | RaiseSoapInvalidObjectFault(soap, req->_USCOREthis);
|
---|
1804 | break;
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | WEBDEBUG((" found reference; deleting!\n"));
|
---|
1808 | // this removes the object from all stacks; since
|
---|
1809 | // there's a ComPtr<> hidden inside the reference,
|
---|
1810 | // this should also invoke Release() on the COM
|
---|
1811 | // object
|
---|
1812 | delete pRef;
|
---|
1813 | } while (0);
|
---|
1814 |
|
---|
1815 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1816 | if (FAILED(rc))
|
---|
1817 | return SOAP_FAULT;
|
---|
1818 | return SOAP_OK;
|
---|
1819 | }
|
---|
1820 |
|
---|
1821 | /****************************************************************************
|
---|
1822 | *
|
---|
1823 | * interface IWebsessionManager
|
---|
1824 | *
|
---|
1825 | ****************************************************************************/
|
---|
1826 |
|
---|
1827 | /**
|
---|
1828 | * Hard-coded implementation for IWebsessionManager::logon. As opposed to the underlying
|
---|
1829 | * COM API, this is the first method that a webservice client must call before the
|
---|
1830 | * webservice will do anything useful.
|
---|
1831 | *
|
---|
1832 | * This returns a managed object reference to the global IVirtualBox object; into this
|
---|
1833 | * reference a session ID is encoded which remains constant with all managed object
|
---|
1834 | * references returned by other methods.
|
---|
1835 | *
|
---|
1836 | * This also creates an instance of ISession, which is stored internally with the
|
---|
1837 | * webservice session and can be retrieved with IWebsessionManager::getSessionObject
|
---|
1838 | * (__vbox__IWebsessionManager_USCOREgetSessionObject). In order for the
|
---|
1839 | * VirtualBox web service to do anything useful, one usually needs both a
|
---|
1840 | * VirtualBox and an ISession object, for which these two methods are designed.
|
---|
1841 | *
|
---|
1842 | * When the webservice client is done, it should call IWebsessionManager::logoff. This
|
---|
1843 | * will clean up internally (destroy all remaining managed object references and
|
---|
1844 | * related COM objects used internally).
|
---|
1845 | *
|
---|
1846 | * After logon, an internal timeout ensures that if the webservice client does not
|
---|
1847 | * call any methods, after a configurable number of seconds, the webservice will log
|
---|
1848 | * off the client automatically. This is to ensure that the webservice does not
|
---|
1849 | * drown in managed object references and eventually deny service. Still, it is
|
---|
1850 | * a much better solution, both for performance and cleanliness, for the webservice
|
---|
1851 | * client to clean up itself.
|
---|
1852 | *
|
---|
1853 | * @param
|
---|
1854 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1855 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1856 | * @return
|
---|
1857 | */
|
---|
1858 | int __vbox__IWebsessionManager_USCORElogon(
|
---|
1859 | struct soap *soap,
|
---|
1860 | _vbox__IWebsessionManager_USCORElogon *req,
|
---|
1861 | _vbox__IWebsessionManager_USCORElogonResponse *resp)
|
---|
1862 | {
|
---|
1863 | HRESULT rc = S_OK;
|
---|
1864 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1865 |
|
---|
1866 | do
|
---|
1867 | {
|
---|
1868 | // WebServiceSession constructor tinkers with global MOR map and requires a write lock
|
---|
1869 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1870 |
|
---|
1871 | // create new session; the constructor stores the new session
|
---|
1872 | // in the global map automatically
|
---|
1873 | WebServiceSession *pSession = new WebServiceSession();
|
---|
1874 | ComPtr<IVirtualBox> pVirtualBox;
|
---|
1875 |
|
---|
1876 | // authenticate the user
|
---|
1877 | if (!(pSession->authenticate(req->username.c_str(),
|
---|
1878 | req->password.c_str(),
|
---|
1879 | pVirtualBox.asOutParam())))
|
---|
1880 | {
|
---|
1881 | // in the new session, create a managed object reference (MOR) for the
|
---|
1882 | // global VirtualBox object; this encodes the session ID in the MOR so
|
---|
1883 | // that it will be implicitly be included in all future requests of this
|
---|
1884 | // webservice client
|
---|
1885 | ComPtr<IUnknown> p2 = pVirtualBox;
|
---|
1886 | if (pVirtualBox.isNull() || p2.isNull())
|
---|
1887 | {
|
---|
1888 | rc = E_FAIL;
|
---|
1889 | break;
|
---|
1890 | }
|
---|
1891 | ManagedObjectRef *pRef = new ManagedObjectRef(*pSession,
|
---|
1892 | p2, // IUnknown *pobjUnknown
|
---|
1893 | pVirtualBox, // void *pobjInterface
|
---|
1894 | COM_IIDOF(IVirtualBox),
|
---|
1895 | g_pcszIVirtualBox);
|
---|
1896 | resp->returnval = pRef->getWSDLID();
|
---|
1897 | WEBDEBUG(("VirtualBox object ref is %s\n", resp->returnval.c_str()));
|
---|
1898 | }
|
---|
1899 | else
|
---|
1900 | rc = E_FAIL;
|
---|
1901 | } while (0);
|
---|
1902 |
|
---|
1903 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1904 | if (FAILED(rc))
|
---|
1905 | return SOAP_FAULT;
|
---|
1906 | return SOAP_OK;
|
---|
1907 | }
|
---|
1908 |
|
---|
1909 | /**
|
---|
1910 | * Returns the ISession object that was created for the webservice client
|
---|
1911 | * on logon.
|
---|
1912 | */
|
---|
1913 | int __vbox__IWebsessionManager_USCOREgetSessionObject(
|
---|
1914 | struct soap*,
|
---|
1915 | _vbox__IWebsessionManager_USCOREgetSessionObject *req,
|
---|
1916 | _vbox__IWebsessionManager_USCOREgetSessionObjectResponse *resp)
|
---|
1917 | {
|
---|
1918 | HRESULT rc = S_OK;
|
---|
1919 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1920 |
|
---|
1921 | do
|
---|
1922 | {
|
---|
1923 | // findSessionFromRef needs lock
|
---|
1924 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1925 |
|
---|
1926 | WebServiceSession* pSession;
|
---|
1927 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1928 | resp->returnval = pSession->getSessionWSDLID();
|
---|
1929 |
|
---|
1930 | } while (0);
|
---|
1931 |
|
---|
1932 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1933 | if (FAILED(rc))
|
---|
1934 | return SOAP_FAULT;
|
---|
1935 | return SOAP_OK;
|
---|
1936 | }
|
---|
1937 |
|
---|
1938 | /**
|
---|
1939 | * hard-coded implementation for IWebsessionManager::logoff.
|
---|
1940 | *
|
---|
1941 | * @param
|
---|
1942 | * @param vbox__IWebsessionManager_USCORElogon
|
---|
1943 | * @param vbox__IWebsessionManager_USCORElogonResponse
|
---|
1944 | * @return
|
---|
1945 | */
|
---|
1946 | int __vbox__IWebsessionManager_USCORElogoff(
|
---|
1947 | struct soap*,
|
---|
1948 | _vbox__IWebsessionManager_USCORElogoff *req,
|
---|
1949 | _vbox__IWebsessionManager_USCORElogoffResponse *resp)
|
---|
1950 | {
|
---|
1951 | HRESULT rc = S_OK;
|
---|
1952 | WEBDEBUG(("-- entering %s\n", __FUNCTION__));
|
---|
1953 |
|
---|
1954 | do
|
---|
1955 | {
|
---|
1956 | // findSessionFromRef and the session destructor require the lock
|
---|
1957 | util::AutoWriteLock lock(g_pSessionsLockHandle COMMA_LOCKVAL_SRC_POS);
|
---|
1958 |
|
---|
1959 | WebServiceSession* pSession;
|
---|
1960 | if ((pSession = WebServiceSession::findSessionFromRef(req->refIVirtualBox)))
|
---|
1961 | {
|
---|
1962 | delete pSession;
|
---|
1963 | // destructor cleans up
|
---|
1964 |
|
---|
1965 | WEBDEBUG(("session destroyed, %d sessions left open\n", g_mapSessions.size()));
|
---|
1966 | }
|
---|
1967 | } while (0);
|
---|
1968 |
|
---|
1969 | WEBDEBUG(("-- leaving %s, rc: 0x%lX\n", __FUNCTION__, rc));
|
---|
1970 | if (FAILED(rc))
|
---|
1971 | return SOAP_FAULT;
|
---|
1972 | return SOAP_OK;
|
---|
1973 | }
|
---|