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