summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/webservice/vboxweb.cpp
blob: eb1c44a0759bba51310c6f892332250b2c4d36cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
/* $Id: vboxweb.cpp $ */
/** @file
 * vboxweb.cpp:
 *      hand-coded parts of the webservice server. This is linked with the
 *      generated code in out/.../src/VBox/Main/webservice/methodmaps.cpp
 *      (plus static gSOAP server code) to implement the actual webservice
 *      server, to which clients can connect.
 */

/*
 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

// shared webservice header
#include "vboxweb.h"

// vbox headers
#include <VBox/com/com.h>
#include <VBox/com/array.h>
#include <VBox/com/string.h>
#include <VBox/com/ErrorInfo.h>
#include <VBox/com/errorprint.h>
#include <VBox/com/listeners.h>
#include <VBox/com/NativeEventQueue.h>
#include <VBox/VBoxAuth.h>
#include <VBox/version.h>
#include <VBox/log.h>

#include <iprt/buildconfig.h>
#include <iprt/ctype.h>
#include <iprt/getopt.h>
#include <iprt/initterm.h>
#include <iprt/ldr.h>
#include <iprt/message.h>
#include <iprt/process.h>
#include <iprt/rand.h>
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include <iprt/time.h>
#include <iprt/path.h>
#include <iprt/system.h>
#include <iprt/base64.h>
#include <iprt/stream.h>
#include <iprt/asm.h>

#ifdef WITH_OPENSSL
# include <openssl/opensslv.h>
#endif

#ifndef RT_OS_WINDOWS
# include <signal.h>
#endif

// workaround for compile problems on gcc 4.1
#ifdef __GNUC__
#pragma GCC visibility push(default)
#endif

// gSOAP headers (must come after vbox includes because it checks for conflicting defs)
#include "soapH.h"

// standard headers
#include <map>
#include <list>

#ifdef __GNUC__
#pragma GCC visibility pop
#endif

// include generated namespaces table
#include "vboxwebsrv.nsmap"

RT_C_DECLS_BEGIN

// declarations for the generated WSDL text
extern const unsigned char g_abVBoxWebWSDL[];
extern const unsigned g_cbVBoxWebWSDL;

RT_C_DECLS_END

static void WebLogSoapError(struct soap *soap);

/****************************************************************************
 *
 * private typedefs
 *
 ****************************************************************************/

typedef std::map<uint64_t, ManagedObjectRef*>   ManagedObjectsMapById;
typedef ManagedObjectsMapById::iterator         ManagedObjectsIteratorById;
typedef std::map<uintptr_t, ManagedObjectRef*>  ManagedObjectsMapByPtr;
typedef ManagedObjectsMapByPtr::iterator        ManagedObjectsIteratorByPtr;

typedef std::map<uint64_t, WebServiceSession*>  WebsessionsMap;
typedef WebsessionsMap::iterator                WebsessionsMapIterator;

typedef std::map<RTTHREAD, com::Utf8Str> ThreadsMap;

static DECLCALLBACK(int) fntWatchdog(RTTHREAD ThreadSelf, void *pvUser);

/****************************************************************************
 *
 * Read-only global variables
 *
 ****************************************************************************/

static ComPtr<IVirtualBoxClient> g_pVirtualBoxClient = NULL;

// generated strings in methodmaps.cpp
extern const char       *g_pcszISession,
                        *g_pcszIVirtualBox,
                        *g_pcszIVirtualBoxErrorInfo;

// globals for vboxweb command-line arguments
#define DEFAULT_TIMEOUT_SECS 300
#define DEFAULT_TIMEOUT_SECS_STRING "300"
static int              g_iWatchdogTimeoutSecs = DEFAULT_TIMEOUT_SECS;
static int              g_iWatchdogCheckInterval = 5;

static const char       *g_pcszBindToHost = NULL;       // host; NULL = localhost
static unsigned int     g_uBindToPort = 18083;          // port
static unsigned int     g_uBacklog = 100;               // backlog = max queue size for requests

#ifdef WITH_OPENSSL
static bool             g_fSSL = false;                 // if SSL is enabled
static const char       *g_pcszKeyFile = NULL;          // server key file
static const char       *g_pcszPassword = NULL;         // password for server key
static const char       *g_pcszCACert = NULL;           // file with trusted CA certificates
static const char       *g_pcszCAPath = NULL;           // directory with trusted CA certificates
static const char       *g_pcszDHFile = NULL;           // DH file name or DH key length in bits, NULL=use RSA
static const char       *g_pcszRandFile = NULL;         // file with random data seed
static const char       *g_pcszSID = "vboxwebsrv";      // server ID for SSL session cache
#endif /* WITH_OPENSSL */

static unsigned int     g_cMaxWorkerThreads = 100;      // max. no. of worker threads
static unsigned int     g_cMaxKeepAlive = 100;          // maximum number of soap requests in one connection

static const char       *g_pcszAuthentication = NULL;   // web service authentication

static uint32_t         g_cHistory = 10;                // enable log rotation, 10 files
static uint32_t         g_uHistoryFileTime = RT_SEC_1DAY; // max 1 day per file
static uint64_t         g_uHistoryFileSize = 100 * _1M; // max 100MB per file
bool                    g_fVerbose = false;             // be verbose

static bool             g_fDaemonize = false;           // run in background.
static volatile bool    g_fKeepRunning = true;          // controlling the exit

const WSDLT_ID          g_EmptyWSDLID;                  // for NULL MORs

/****************************************************************************
 *
 * Writeable global variables
 *
 ****************************************************************************/

// The one global SOAP queue created by main().
class SoapQ;
static SoapQ                    *g_pSoapQ = NULL;

// this mutex protects the auth lib and authentication
static util::WriteLockHandle    *g_pAuthLibLockHandle;

// this mutex protects the global VirtualBox reference below
static util::RWLockHandle       *g_pVirtualBoxLockHandle;

static ComPtr<IVirtualBox>      g_pVirtualBox = NULL;

// this mutex protects all of the below
util::WriteLockHandle           *g_pWebsessionsLockHandle;

static WebsessionsMap           g_mapWebsessions;
static ULONG64                  g_cManagedObjects = 0;

// this mutex protects g_mapThreads
static util::RWLockHandle       *g_pThreadsLockHandle;

// Threads map, so we can quickly map an RTTHREAD struct to a logger prefix
static ThreadsMap               g_mapThreads;

/****************************************************************************
 *
 *  Command line help
 *
 ****************************************************************************/

static const RTGETOPTDEF g_aOptions[]
    = {
        { "--help",             'h', RTGETOPT_REQ_NOTHING }, /* for DisplayHelp() */
#if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
        { "--background",       'b', RTGETOPT_REQ_NOTHING },
#endif
        { "--host",             'H', RTGETOPT_REQ_STRING },
        { "--port",             'p', RTGETOPT_REQ_UINT32 },
#ifdef WITH_OPENSSL
        { "--ssl",              's', RTGETOPT_REQ_NOTHING },
        { "--keyfile",          'K', RTGETOPT_REQ_STRING },
        { "--passwordfile",     'a', RTGETOPT_REQ_STRING },
        { "--cacert",           'c', RTGETOPT_REQ_STRING },
        { "--capath",           'C', RTGETOPT_REQ_STRING },
        { "--dhfile",           'D', RTGETOPT_REQ_STRING },
        { "--randfile",         'r', RTGETOPT_REQ_STRING },
#endif /* WITH_OPENSSL */
        { "--timeout",          't', RTGETOPT_REQ_UINT32 },
        { "--check-interval",   'i', RTGETOPT_REQ_UINT32 },
        { "--threads",          'T', RTGETOPT_REQ_UINT32 },
        { "--keepalive",        'k', RTGETOPT_REQ_UINT32 },
        { "--authentication",   'A', RTGETOPT_REQ_STRING },
        { "--verbose",          'v', RTGETOPT_REQ_NOTHING },
        { "--pidfile",          'P', RTGETOPT_REQ_STRING },
        { "--logfile",          'F', RTGETOPT_REQ_STRING },
        { "--logrotate",        'R', RTGETOPT_REQ_UINT32 },
        { "--logsize",          'S', RTGETOPT_REQ_UINT64 },
        { "--loginterval",      'I', RTGETOPT_REQ_UINT32 }
    };

static void DisplayHelp()
{
    RTStrmPrintf(g_pStdErr, "\nUsage: vboxwebsrv [options]\n\nSupported options (default values in brackets):\n");
    for (unsigned i = 0;
         i < RT_ELEMENTS(g_aOptions);
         ++i)
    {
        std::string str(g_aOptions[i].pszLong);
        str += ", -";
        str += g_aOptions[i].iShort;
        str += ":";

        const char *pcszDescr = "";

        switch (g_aOptions[i].iShort)
        {
            case 'h':
                pcszDescr = "Print this help message and exit.";
                break;

#if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
            case 'b':
                pcszDescr = "Run in background (daemon mode).";
                break;
#endif

            case 'H':
                pcszDescr = "The host to bind to (localhost).";
                break;

            case 'p':
                pcszDescr = "The port to bind to (18083).";
                break;

#ifdef WITH_OPENSSL
            case 's':
                pcszDescr = "Enable SSL/TLS encryption.";
                break;

            case 'K':
                pcszDescr = "Server key and certificate file, PEM format (\"\").";
                break;

            case 'a':
                pcszDescr = "File name for password to server key (\"\").";
                break;

            case 'c':
                pcszDescr = "CA certificate file, PEM format (\"\").";
                break;

            case 'C':
                pcszDescr = "CA certificate path (\"\").";
                break;

            case 'D':
                pcszDescr = "DH file name or DH key length in bits (\"\").";
                break;

            case 'r':
                pcszDescr = "File containing seed for random number generator (\"\").";
                break;
#endif /* WITH_OPENSSL */

            case 't':
                pcszDescr = "Session timeout in seconds; 0 = disable timeouts (" DEFAULT_TIMEOUT_SECS_STRING ").";
                break;

            case 'T':
                pcszDescr = "Maximum number of worker threads to run in parallel (100).";
                break;

            case 'k':
                pcszDescr = "Maximum number of requests before a socket will be closed (100).";
                break;

            case 'A':
                pcszDescr = "Authentication method for the webservice (\"\").";
                break;

            case 'i':
                pcszDescr = "Frequency of timeout checks in seconds (5).";
                break;

            case 'v':
                pcszDescr = "Be verbose.";
                break;

            case 'P':
                pcszDescr = "Name of the PID file which is created when the daemon was started.";
                break;

            case 'F':
                pcszDescr = "Name of file to write log to (no file).";
                break;

            case 'R':
                pcszDescr = "Number of log files (0 disables log rotation).";
                break;

            case 'S':
                pcszDescr = "Maximum size of a log file to trigger rotation (bytes).";
                break;

            case 'I':
                pcszDescr = "Maximum time interval to trigger log rotation (seconds).";
                break;
        }

        RTStrmPrintf(g_pStdErr, "%-23s%s\n", str.c_str(), pcszDescr);
    }
}

/****************************************************************************
 *
 * SoapQ, SoapThread (multithreading)
 *
 ****************************************************************************/

class SoapQ;

class SoapThread
{
public:
    /**
     * Constructor. Creates the new thread and makes it call process() for processing the queue.
     * @param u Thread number. (So we can count from 1 and be readable.)
     * @param q SoapQ instance which has the queue to process.
     * @param soap struct soap instance from main() which we copy here.
     */
    SoapThread(size_t u,
               SoapQ &q,
               const struct soap *soap)
        : m_u(u),
          m_strThread(com::Utf8StrFmt("SQW%02d", m_u)),
          m_pQ(&q)
    {
        // make a copy of the soap struct for the new thread
        m_soap = soap_copy(soap);
        m_soap->fget = fnHttpGet;

        /* The soap.max_keep_alive value can be set to the maximum keep-alive calls allowed,
         * which is important to avoid a client from holding a thread indefinitely.
         * http://www.cs.fsu.edu/~engelen/soapdoc2.html#sec:keepalive
         *
         * Strings with 8-bit content can hold ASCII (default) or UTF8. The latter is
         * possible by enabling the SOAP_C_UTFSTRING flag.
         */
        soap_set_omode(m_soap, SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING);
        soap_set_imode(m_soap, SOAP_IO_KEEPALIVE | SOAP_C_UTFSTRING);
        m_soap->max_keep_alive = g_cMaxKeepAlive;

        int vrc = RTThreadCreate(&m_pThread,
                                 fntWrapper,
                                 this,           // pvUser
                                 0,              // cbStack
                                 RTTHREADTYPE_MAIN_HEAVY_WORKER,
                                 0,
                                 m_strThread.c_str());
        if (RT_FAILURE(vrc))
        {
            RTMsgError("Cannot start worker thread %d: %Rrc\n", u, vrc);
            exit(1);
        }
    }

    void process();

    static int fnHttpGet(struct soap *soap)
    {
        char *s = strchr(soap->path, '?');
        if (!s || strcmp(s, "?wsdl"))
            return SOAP_GET_METHOD;
        soap_response(soap, SOAP_HTML);
        soap_send_raw(soap, (const char *)g_abVBoxWebWSDL, g_cbVBoxWebWSDL);
        soap_end_send(soap);
        return SOAP_OK;
    }

    /**
     * Static function that can be passed to RTThreadCreate and that calls
     * process() on the SoapThread instance passed as the thread parameter.
     *
     * @param   hThreadSelf
     * @param   pvThread
     * @return
     */
    static DECLCALLBACK(int) fntWrapper(RTTHREAD hThreadSelf, void *pvThread)
    {
        RT_NOREF(hThreadSelf);
        SoapThread *pst = (SoapThread*)pvThread;
        pst->process();
        return VINF_SUCCESS;
    }

    size_t          m_u;            // thread number
    com::Utf8Str    m_strThread;    // thread name ("SoapQWrkXX")
    SoapQ           *m_pQ;          // the single SOAP queue that all the threads service
    struct soap     *m_soap;        // copy of the soap structure for this thread (from soap_copy())
    RTTHREAD        m_pThread;      // IPRT thread struct for this thread
};

/**
 * SOAP queue encapsulation. There is only one instance of this, to
 * which add() adds a queue item (called on the main thread),
 * and from which get() fetch items, called from each queue thread.
 */
class SoapQ
{
public:

    /**
     * Constructor. Creates the soap queue.
     * @param pSoap
     */
    SoapQ(const struct soap *pSoap)
        : m_soap(pSoap),
          m_mutex(util::LOCKCLASS_OBJECTSTATE),     // lowest lock order, no other may be held while this is held
          m_cIdleThreads(0)
    {
        RTSemEventMultiCreate(&m_event);
    }

    ~SoapQ()
    {
        /* Tell the threads to terminate. */
        RTSemEventMultiSignal(m_event);
        {
            util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);
            int i = 0;
            while (m_llAllThreads.size() && i++ <= 30)
            {
                qlock.release();
                RTThreadSleep(1000);
                RTSemEventMultiSignal(m_event);
                qlock.acquire();
            }
            LogRel(("ending queue processing (%d out of %d threads idle)\n", m_cIdleThreads, m_llAllThreads.size()));
        }

        RTSemEventMultiDestroy(m_event);
    }

    /**
     * Adds the given socket to the SOAP queue and posts the
     * member event sem to wake up the workers. Called on the main thread
     * whenever a socket has work to do. Creates a new SOAP thread on the
     * first call or when all existing threads are busy.
     * @param s Socket from soap_accept() which has work to do.
     */
    size_t add(SOAP_SOCKET s)
    {
        size_t cItems;
        util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);

        // if no threads have yet been created, or if all threads are busy,
        // create a new SOAP thread
        if (    !m_cIdleThreads
                // but only if we're not exceeding the global maximum (default is 100)
             && (m_llAllThreads.size() < g_cMaxWorkerThreads)
           )
        {
            SoapThread *pst = new SoapThread(m_llAllThreads.size() + 1,
                                             *this,
                                             m_soap);
            m_llAllThreads.push_back(pst);
            util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
            g_mapThreads[pst->m_pThread] = com::Utf8StrFmt("[%3u]", pst->m_u);
            ++m_cIdleThreads;
        }

        // enqueue the socket of this connection and post eventsem so that
        // one of the threads (possibly the one just created) can pick it up
        m_llSocketsQ.push_back(s);
        cItems = m_llSocketsQ.size();
        qlock.release();

        // unblock one of the worker threads
        RTSemEventMultiSignal(m_event);

        return cItems;
    }

    /**
     * Blocks the current thread until work comes in; then returns
     * the SOAP socket which has work to do. This reduces m_cIdleThreads
     * by one, and the caller MUST call done() when it's done processing.
     * Called from the worker threads.
     * @param cIdleThreads out: no. of threads which are currently idle (not counting the caller)
     * @param cThreads out: total no. of SOAP threads running
     * @return
     */
    SOAP_SOCKET get(size_t &cIdleThreads, size_t &cThreads)
    {
        while (g_fKeepRunning)
        {
            // wait for something to happen
            RTSemEventMultiWait(m_event, RT_INDEFINITE_WAIT);

            if (!g_fKeepRunning)
                break;

            util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);
            if (!m_llSocketsQ.empty())
            {
                SOAP_SOCKET socket = m_llSocketsQ.front();
                m_llSocketsQ.pop_front();
                cIdleThreads = --m_cIdleThreads;
                cThreads = m_llAllThreads.size();

                // reset the multi event only if the queue is now empty; otherwise
                // another thread will also wake up when we release the mutex and
                // process another one
                if (m_llSocketsQ.empty())
                    RTSemEventMultiReset(m_event);

                qlock.release();

                return socket;
            }

            // nothing to do: keep looping
        }
        return SOAP_INVALID_SOCKET;
    }

    /**
     * To be called by a worker thread after fetching an item from the
     * queue via get() and having finished its lengthy processing.
     */
    void done()
    {
        util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);
        ++m_cIdleThreads;
    }

    /**
     * To be called by a worker thread when signing off, i.e. no longer
     * willing to process requests.
     */
    void signoff(SoapThread *th)
    {
        {
            util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
            size_t c = g_mapThreads.erase(th->m_pThread);
            AssertReturnVoid(c == 1);
        }
        {
            util::AutoWriteLock qlock(m_mutex COMMA_LOCKVAL_SRC_POS);
            m_llAllThreads.remove(th);
            --m_cIdleThreads;
        }
    }

    const struct soap       *m_soap;            // soap structure created by main(), passed to constructor

    util::WriteLockHandle   m_mutex;
    RTSEMEVENTMULTI         m_event;            // posted by add(), blocked on by get()

    std::list<SoapThread*>  m_llAllThreads;     // all the threads created by the constructor
    size_t                  m_cIdleThreads;     // threads which are currently idle (statistics)

    // A std::list abused as a queue; this contains the actual jobs to do,
    // each int being a socket from soap_accept()
    std::list<SOAP_SOCKET>  m_llSocketsQ;
};

/**
 * Thread function for each of the SOAP queue worker threads. This keeps
 * running, blocks on the event semaphore in SoapThread.SoapQ and picks
 * up a socket from the queue therein, which has been put there by
 * beginProcessing().
 */
void SoapThread::process()
{
    LogRel(("New SOAP thread started\n"));

    while (g_fKeepRunning)
    {
        // wait for a socket to arrive on the queue
        size_t cIdleThreads = 0, cThreads = 0;
        m_soap->socket = m_pQ->get(cIdleThreads, cThreads);

        if (!soap_valid_socket(m_soap->socket))
            continue;

        LogRel(("Processing connection from IP=%RTnaipv4 socket=%d (%d out of %d threads idle)\n",
                RT_H2N_U32(m_soap->ip), m_soap->socket, cIdleThreads, cThreads));

        // Ensure that we don't get stuck indefinitely for connections using
        // keepalive, otherwise stale connections tie up worker threads.
        m_soap->send_timeout = 60;
        m_soap->recv_timeout = 60;
        // Limit the maximum SOAP request size to a generous amount, just to
        // be on the safe side (SOAP is quite wordy when representing arrays,
        // and some API uses need to deal with large arrays). Good that binary
        // data is no longer represented by byte arrays...
        m_soap->recv_maxlength = _16M;
        // process the request; this goes into the COM code in methodmaps.cpp
        do {
#ifdef WITH_OPENSSL
            if (g_fSSL && soap_ssl_accept(m_soap))
            {
                WebLogSoapError(m_soap);
                break;
            }
#endif /* WITH_OPENSSL */
            soap_serve(m_soap);
        } while (0);

        soap_destroy(m_soap); // clean up class instances
        soap_end(m_soap); // clean up everything and close socket

        // tell the queue we're idle again
        m_pQ->done();
    }
    m_pQ->signoff(this);
}

/****************************************************************************
 *
 * VirtualBoxClient event listener
 *
 ****************************************************************************/

class VirtualBoxClientEventListener
{
public:
    VirtualBoxClientEventListener()
    {
    }

    virtual ~VirtualBoxClientEventListener()
    {
    }

    HRESULT init()
    {
       return S_OK;
    }

    void uninit()
    {
    }


    STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
    {
        switch (aType)
        {
            case VBoxEventType_OnVBoxSVCAvailabilityChanged:
            {
                ComPtr<IVBoxSVCAvailabilityChangedEvent> pVSACEv = aEvent;
                Assert(pVSACEv);
                BOOL fAvailable = FALSE;
                pVSACEv->COMGETTER(Available)(&fAvailable);
                if (!fAvailable)
                {
                    LogRel(("VBoxSVC became unavailable\n"));
                    {
                        util::AutoWriteLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS);
                        g_pVirtualBox.setNull();
                    }
                    {
                        // we're messing with websessions, so lock them
                        util::AutoWriteLock lock(g_pWebsessionsLockHandle COMMA_LOCKVAL_SRC_POS);
                        WEBDEBUG(("SVC unavailable: deleting %d websessions\n", g_mapWebsessions.size()));

                        WebsessionsMapIterator it = g_mapWebsessions.begin(),
                                               itEnd = g_mapWebsessions.end();
                        while (it != itEnd)
                        {
                            WebServiceSession *pWebsession = it->second;
                            WEBDEBUG(("SVC unavailable: websession %#llx stale, deleting\n", pWebsession->getID()));
                            delete pWebsession;
                            it = g_mapWebsessions.begin();
                        }
                    }
                }
                else
                {
                    LogRel(("VBoxSVC became available\n"));
                    util::AutoWriteLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS);
                    HRESULT hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
                    AssertComRC(hrc);
                }
                break;
            }
            default:
                AssertFailed();
        }

        return S_OK;
    }

private:
};

typedef ListenerImpl<VirtualBoxClientEventListener> VirtualBoxClientEventListenerImpl;

VBOX_LISTENER_DECLARE(VirtualBoxClientEventListenerImpl)

/**
 * Helper for printing SOAP error messages.
 * @param soap
 */
/*static*/
void WebLogSoapError(struct soap *soap)
{
    if (soap_check_state(soap))
    {
        LogRel(("Error: soap struct not initialized\n"));
        return;
    }

    const char *pcszFaultString = *soap_faultstring(soap);
    const char **ppcszDetail = soap_faultcode(soap);
    LogRel(("#### SOAP FAULT: %s [%s]\n",
            pcszFaultString ? pcszFaultString : "[no fault string available]",
            (ppcszDetail && *ppcszDetail) ? *ppcszDetail : "no details available"));
}

/**
 * Helper for decoding AuthResult.
 * @param result AuthResult
 */
static const char * decodeAuthResult(AuthResult result)
{
    switch (result)
    {
        case AuthResultAccessDenied:    return "access DENIED";
        case AuthResultAccessGranted:   return "access granted";
        case AuthResultDelegateToGuest: return "delegated to guest";
        default:                        return "unknown AuthResult";
    }
}

#if defined(WITH_OPENSSL) && (OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_NUMBER))
/****************************************************************************
 *
 * OpenSSL convenience functions for multithread support.
 * Not required for OpenSSL 1.1+
 *
 ****************************************************************************/

static RTCRITSECT *g_pSSLMutexes = NULL;

struct CRYPTO_dynlock_value
{
    RTCRITSECT mutex;
};

static unsigned long CRYPTO_id_function()
{
    return (unsigned long)RTThreadNativeSelf();
}

static void CRYPTO_locking_function(int mode, int n, const char * /*file*/, int /*line*/)
{
    if (mode & CRYPTO_LOCK)
        RTCritSectEnter(&g_pSSLMutexes[n]);
    else
        RTCritSectLeave(&g_pSSLMutexes[n]);
}

static struct CRYPTO_dynlock_value *CRYPTO_dyn_create_function(const char * /*file*/, int /*line*/)
{
    static uint32_t s_iCritSectDynlock = 0;
    struct CRYPTO_dynlock_value *value = (struct CRYPTO_dynlock_value *)RTMemAlloc(sizeof(struct CRYPTO_dynlock_value));
    if (value)
        RTCritSectInitEx(&value->mutex, RTCRITSECT_FLAGS_NO_LOCK_VAL,
                         NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE,
                         "openssl-dyn-%u", ASMAtomicIncU32(&s_iCritSectDynlock) - 1);

    return value;
}

static void CRYPTO_dyn_lock_function(int mode, struct CRYPTO_dynlock_value *value, const char * /*file*/, int /*line*/)
{
    if (mode & CRYPTO_LOCK)
        RTCritSectEnter(&value->mutex);
    else
        RTCritSectLeave(&value->mutex);
}

static void CRYPTO_dyn_destroy_function(struct CRYPTO_dynlock_value *value, const char * /*file*/, int /*line*/)
{
    if (value)
    {
        RTCritSectDelete(&value->mutex);
        free(value);
    }
}

static int CRYPTO_thread_setup()
{
    int num_locks = CRYPTO_num_locks();
    g_pSSLMutexes = (RTCRITSECT *)RTMemAlloc(num_locks * sizeof(RTCRITSECT));
    if (!g_pSSLMutexes)
        return SOAP_EOM;

    for (int i = 0; i < num_locks; i++)
    {
        int vrc = RTCritSectInitEx(&g_pSSLMutexes[i], RTCRITSECT_FLAGS_NO_LOCK_VAL,
                                   NIL_RTLOCKVALCLASS, RTLOCKVAL_SUB_CLASS_NONE,
                                   "openssl-%d", i);
        if (RT_FAILURE(vrc))
        {
            for ( ; i >= 0; i--)
                RTCritSectDelete(&g_pSSLMutexes[i]);
            RTMemFree(g_pSSLMutexes);
            g_pSSLMutexes = NULL;
            return SOAP_EOM;
        }
    }

    CRYPTO_set_id_callback(CRYPTO_id_function);
    CRYPTO_set_locking_callback(CRYPTO_locking_function);
    CRYPTO_set_dynlock_create_callback(CRYPTO_dyn_create_function);
    CRYPTO_set_dynlock_lock_callback(CRYPTO_dyn_lock_function);
    CRYPTO_set_dynlock_destroy_callback(CRYPTO_dyn_destroy_function);

    return SOAP_OK;
}

static void CRYPTO_thread_cleanup()
{
    if (!g_pSSLMutexes)
        return;

    CRYPTO_set_id_callback(NULL);
    CRYPTO_set_locking_callback(NULL);
    CRYPTO_set_dynlock_create_callback(NULL);
    CRYPTO_set_dynlock_lock_callback(NULL);
    CRYPTO_set_dynlock_destroy_callback(NULL);

    int num_locks = CRYPTO_num_locks();
    for (int i = 0; i < num_locks; i++)
        RTCritSectDelete(&g_pSSLMutexes[i]);

    RTMemFree(g_pSSLMutexes);
    g_pSSLMutexes = NULL;
}
#endif /* WITH_OPENSSL && (OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_NUMBER)) */

/****************************************************************************
 *
 * SOAP queue pumper thread
 *
 ****************************************************************************/

static void doQueuesLoop()
{
#if defined(WITH_OPENSSL) && (OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_NUMBER))
    if (g_fSSL && CRYPTO_thread_setup())
    {
        LogRel(("Failed to set up OpenSSL thread mutex!"));
        exit(RTEXITCODE_FAILURE);
    }
#endif

    // set up gSOAP
    struct soap soap;
    soap_init(&soap);

#ifdef WITH_OPENSSL
    if (g_fSSL && soap_ssl_server_context(&soap, SOAP_SSL_REQUIRE_SERVER_AUTHENTICATION | SOAP_TLSv1, g_pcszKeyFile,
                                         g_pcszPassword, g_pcszCACert, g_pcszCAPath,
                                         g_pcszDHFile, g_pcszRandFile, g_pcszSID))
    {
        WebLogSoapError(&soap);
        exit(RTEXITCODE_FAILURE);
    }
#endif /* WITH_OPENSSL */

    soap.bind_flags |= SO_REUSEADDR;
            // avoid EADDRINUSE on bind()

    SOAP_SOCKET m, s; // master and slave sockets
    m = soap_bind(&soap,
                  g_pcszBindToHost ? g_pcszBindToHost : "localhost",    // safe default host
                  g_uBindToPort,    // port
                  g_uBacklog);      // backlog = max queue size for requests
    if (m == SOAP_INVALID_SOCKET)
        WebLogSoapError(&soap);
    else
    {
#ifdef WITH_OPENSSL
        const char *pszSsl = g_fSSL ? "SSL, " : "";
#else /* !WITH_OPENSSL */
        const char *pszSsl = "";
#endif /*!WITH_OPENSSL */
        LogRel(("Socket connection successful: host = %s, port = %u, %smaster socket = %d\n",
                (g_pcszBindToHost) ? g_pcszBindToHost : "default (localhost)",
                g_uBindToPort, pszSsl, m));

        // initialize thread queue, mutex and eventsem
        g_pSoapQ = new SoapQ(&soap);

        uint64_t cAccepted = 1;
        while (g_fKeepRunning)
        {
            struct timeval timeout;
            fd_set ReadFds, WriteFds, XcptFds;
            int rv;
            for (;;)
            {
                timeout.tv_sec = 60;
                timeout.tv_usec = 0;
                FD_ZERO(&ReadFds);
                FD_SET(soap.master, &ReadFds);
                FD_ZERO(&WriteFds);
                FD_SET(soap.master, &WriteFds);
                FD_ZERO(&XcptFds);
                FD_SET(soap.master, &XcptFds);
                rv = select((int)soap.master + 1, &ReadFds, &WriteFds, &XcptFds, &timeout);
                if (rv > 0)
                    break; // work is waiting
                if (rv == 0)
                    continue; // timeout, not necessary to bother gsoap
                // r < 0, errno
#if GSOAP_VERSION >= 208103
                if (soap_socket_errno == SOAP_EINTR)
#else
                if (soap_socket_errno(soap.master) == SOAP_EINTR)
#endif
                    rv = 0; // re-check if we should terminate
                break;
            }
            if (rv == 0)
                continue;

            // call gSOAP to handle incoming SOAP connection
            soap.accept_timeout = -1; // 1usec timeout, actual waiting is above
            s = soap_accept(&soap);
            if (!soap_valid_socket(s))
            {
                if (soap.errnum)
                    WebLogSoapError(&soap);
                continue;
            }

            // add the socket to the queue and tell worker threads to
            // pick up the job
            size_t cItemsOnQ = g_pSoapQ->add(s);
            LogRel(("Request %llu on socket %d queued for processing (%d items on Q)\n", cAccepted, s, cItemsOnQ));
            cAccepted++;
        }

        delete g_pSoapQ;
        g_pSoapQ = NULL;

        LogRel(("ending SOAP request handling\n"));

        delete g_pSoapQ;
        g_pSoapQ = NULL;

    }
    soap_done(&soap); // close master socket and detach environment

#if defined(WITH_OPENSSL) && (OPENSSL_VERSION_NUMBER < 0x10100000 || defined(LIBRESSL_VERSION_NUMBER))
    if (g_fSSL)
        CRYPTO_thread_cleanup();
#endif
}

/**
 * Thread function for the "queue pumper" thread started from main(). This implements
 * the loop that takes SOAP calls from HTTP and serves them by handing sockets to the
 * SOAP queue worker threads.
 */
static DECLCALLBACK(int) fntQPumper(RTTHREAD hThreadSelf, void *pvUser)
{
    RT_NOREF(hThreadSelf, pvUser);

    // store a log prefix for this thread
    util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
    g_mapThreads[RTThreadSelf()] = "[ P ]";
    thrLock.release();

    doQueuesLoop();

    thrLock.acquire();
    g_mapThreads.erase(RTThreadSelf());
    return VINF_SUCCESS;
}

#ifdef RT_OS_WINDOWS
/**
 * "Signal" handler for cleanly terminating the event loop.
 */
static BOOL WINAPI websrvSignalHandler(DWORD dwCtrlType)
{
    bool fEventHandled = FALSE;
    switch (dwCtrlType)
    {
        /* User pressed CTRL+C or CTRL+BREAK or an external event was sent
         * via GenerateConsoleCtrlEvent(). */
        case CTRL_BREAK_EVENT:
        case CTRL_CLOSE_EVENT:
        case CTRL_C_EVENT:
        case CTRL_LOGOFF_EVENT:
        case CTRL_SHUTDOWN_EVENT:
        {
            ASMAtomicWriteBool(&g_fKeepRunning, false);
            com::NativeEventQueue *pQ = com::NativeEventQueue::getMainEventQueue();
            pQ->interruptEventQueueProcessing();
            fEventHandled = TRUE;
            break;
        }
        default:
            break;
    }
    return fEventHandled;
}
#else
/**
 * Signal handler for cleanly terminating the event loop.
 */
static void websrvSignalHandler(int iSignal)
{
    NOREF(iSignal);
    ASMAtomicWriteBool(&g_fKeepRunning, false);
    com::NativeEventQueue *pQ = com::NativeEventQueue::getMainEventQueue();
    pQ->interruptEventQueueProcessing();
}
#endif


/**
 * Start up the webservice server. This keeps running and waits
 * for incoming SOAP connections; for each request that comes in,
 * it calls method implementation code, most of it in the generated
 * code in methodmaps.cpp.
 *
 * @param argc
 * @param argv[]
 * @return
 */
int main(int argc, char *argv[])
{
    // initialize runtime
    int vrc = RTR3InitExe(argc, &argv, 0);
    if (RT_FAILURE(vrc))
        return RTMsgInitFailure(vrc);
#ifdef RT_OS_WINDOWS
    ATL::CComModule _Module; /* Required internally by ATL (constructor records instance in global variable). */
#endif

    // store a log prefix for this thread
    g_mapThreads[RTThreadSelf()] = "[M  ]";

    RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " web service Version " VBOX_VERSION_STRING "\n"
                            "Copyright (C) 2007-" VBOX_C_YEAR " " VBOX_VENDOR "\n");

    int c;
    const char *pszLogFile = NULL;
    const char *pszPidFile = NULL;
    RTGETOPTUNION ValueUnion;
    RTGETOPTSTATE GetState;
    RTGetOptInit(&GetState, argc, argv, g_aOptions, RT_ELEMENTS(g_aOptions), 1, 0 /*fFlags*/);
    while ((c = RTGetOpt(&GetState, &ValueUnion)))
    {
        switch (c)
        {
            case 'H':
                if (!ValueUnion.psz || !*ValueUnion.psz)
                {
                    /* Normalize NULL/empty string to NULL, which will be
                     * interpreted as "localhost" below. */
                    g_pcszBindToHost = NULL;
                }
                else
                    g_pcszBindToHost = ValueUnion.psz;
                break;

            case 'p':
                g_uBindToPort = ValueUnion.u32;
                break;

#ifdef WITH_OPENSSL
            case 's':
                g_fSSL = true;
                break;

            case 'K':
                g_pcszKeyFile = ValueUnion.psz;
                break;

            case 'a':
                if (ValueUnion.psz[0] == '\0')
                    g_pcszPassword = NULL;
                else
                {
                    PRTSTREAM StrmIn;
                    if (!strcmp(ValueUnion.psz, "-"))
                        StrmIn = g_pStdIn;
                    else
                    {
                        vrc = RTStrmOpen(ValueUnion.psz, "r", &StrmIn);
                        if (RT_FAILURE(vrc))
                            return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open password file (%s, %Rrc)", ValueUnion.psz, vrc);
                    }
                    char szPasswd[512];
                    vrc = RTStrmGetLine(StrmIn, szPasswd, sizeof(szPasswd));
                    if (RT_FAILURE(vrc))
                        return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to read password (%s, %Rrc)", ValueUnion.psz, vrc);
                    g_pcszPassword = RTStrDup(szPasswd);
                    memset(szPasswd, '\0', sizeof(szPasswd));
                    if (StrmIn != g_pStdIn)
                        RTStrmClose(StrmIn);
                }
                break;

            case 'c':
                g_pcszCACert = ValueUnion.psz;
                break;

            case 'C':
                g_pcszCAPath = ValueUnion.psz;
                break;

            case 'D':
                g_pcszDHFile = ValueUnion.psz;
                break;

            case 'r':
                g_pcszRandFile = ValueUnion.psz;
                break;
#endif /* WITH_OPENSSL */

            case 't':
                g_iWatchdogTimeoutSecs = ValueUnion.u32;
                break;

            case 'i':
                g_iWatchdogCheckInterval = ValueUnion.u32;
                break;

            case 'F':
                pszLogFile = ValueUnion.psz;
                break;

            case 'R':
                g_cHistory = ValueUnion.u32;
                break;

            case 'S':
                g_uHistoryFileSize = ValueUnion.u64;
                break;

            case 'I':
                g_uHistoryFileTime = ValueUnion.u32;
                break;

            case 'P':
                pszPidFile = ValueUnion.psz;
                break;

            case 'T':
                g_cMaxWorkerThreads = ValueUnion.u32;
                break;

            case 'k':
                g_cMaxKeepAlive = ValueUnion.u32;
                break;

            case 'A':
                g_pcszAuthentication = ValueUnion.psz;
                break;

            case 'h':
                DisplayHelp();
                return 0;

            case 'v':
                g_fVerbose = true;
                break;

#if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
            case 'b':
                g_fDaemonize = true;
                break;
#endif
            case 'V':
                RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
                return 0;

            default:
                return RTGetOptPrintError(c, &ValueUnion);
        }
    }

    /* create release logger, to stdout */
    RTERRINFOSTATIC ErrInfo;
    vrc = com::VBoxLogRelCreate("web service", g_fDaemonize ? NULL : pszLogFile,
                                RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
                                "all", "VBOXWEBSRV_RELEASE_LOG",
                                RTLOGDEST_STDOUT, UINT32_MAX /* cMaxEntriesPerGroup */,
                                g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
                                RTErrInfoInitStatic(&ErrInfo));
    if (RT_FAILURE(vrc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", ErrInfo.Core.pszMsg, vrc);

#if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
    if (g_fDaemonize)
    {
        /* prepare release logging */
        char szLogFile[RTPATH_MAX];

        if (!pszLogFile || !*pszLogFile)
        {
            vrc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile));
            if (RT_FAILURE(vrc))
                 return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory for logging: %Rrc", vrc);
            vrc = RTPathAppend(szLogFile, sizeof(szLogFile), "vboxwebsrv.log");
            if (RT_FAILURE(vrc))
                 return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not construct logging path: %Rrc", vrc);
            pszLogFile = szLogFile;
        }

        vrc = RTProcDaemonizeUsingFork(false /* fNoChDir */, false /* fNoClose */, pszPidFile);
        if (RT_FAILURE(vrc))
            return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to daemonize, vrc=%Rrc. exiting.", vrc);

        /* create release logger, to file */
        vrc = com::VBoxLogRelCreate("web service", pszLogFile,
                                    RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
                                    "all", "VBOXWEBSRV_RELEASE_LOG",
                                    RTLOGDEST_FILE, UINT32_MAX /* cMaxEntriesPerGroup */,
                                    g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
                                    RTErrInfoInitStatic(&ErrInfo));
        if (RT_FAILURE(vrc))
            return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", ErrInfo.Core.pszMsg, vrc);
    }
#endif

    // initialize SOAP SSL support if enabled
#ifdef WITH_OPENSSL
    if (g_fSSL)
        soap_ssl_init();
#endif /* WITH_OPENSSL */

    // initialize COM/XPCOM
    HRESULT hrc = com::Initialize();
#ifdef VBOX_WITH_XPCOM
    if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
    {
        char szHome[RTPATH_MAX] = "";
        com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
        return RTMsgErrorExit(RTEXITCODE_FAILURE,
               "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
    }
#endif
    if (FAILED(hrc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to initialize COM! hrc=%Rhrc\n", hrc);

    hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
    if (FAILED(hrc))
    {
        RTMsgError("failed to create the VirtualBoxClient object!");
        com::ErrorInfo info;
        if (!info.isFullAvailable() && !info.isBasicAvailable())
        {
            com::GluePrintRCMessage(hrc);
            RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
        }
        else
            com::GluePrintErrorInfo(info);
        return RTEXITCODE_FAILURE;
    }

    hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
    if (FAILED(hrc))
    {
        RTMsgError("Failed to get VirtualBox object (hrc=%Rhrc)!", hrc);
        return RTEXITCODE_FAILURE;
    }

    // set the authentication method if requested
    if (g_pVirtualBox && g_pcszAuthentication && g_pcszAuthentication[0])
    {
        ComPtr<ISystemProperties> pSystemProperties;
        g_pVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
        if (pSystemProperties)
            pSystemProperties->COMSETTER(WebServiceAuthLibrary)(com::Bstr(g_pcszAuthentication).raw());
    }

    /* VirtualBoxClient events registration. */
    ComPtr<IEventListener> vboxClientListener;
    {
        ComPtr<IEventSource> pES;
        CHECK_ERROR(g_pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam()));
        ComObjPtr<VirtualBoxClientEventListenerImpl> clientListener;
        clientListener.createObject();
        clientListener->init(new VirtualBoxClientEventListener());
        vboxClientListener = clientListener;
        com::SafeArray<VBoxEventType_T> eventTypes;
        eventTypes.push_back(VBoxEventType_OnVBoxSVCAvailabilityChanged);
        CHECK_ERROR(pES, RegisterListener(vboxClientListener, ComSafeArrayAsInParam(eventTypes), true));
    }

    // create the global mutexes
    g_pAuthLibLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
    g_pVirtualBoxLockHandle = new util::RWLockHandle(util::LOCKCLASS_WEBSERVICE);
    g_pWebsessionsLockHandle = new util::WriteLockHandle(util::LOCKCLASS_WEBSERVICE);
    g_pThreadsLockHandle = new util::RWLockHandle(util::LOCKCLASS_OBJECTSTATE);

    // SOAP queue pumper thread
    RTTHREAD threadQPumper;
    vrc = RTThreadCreate(&threadQPumper,
                         fntQPumper,
                         NULL,        // pvUser
                         0,           // cbStack (default)
                         RTTHREADTYPE_MAIN_WORKER,
                         RTTHREADFLAGS_WAITABLE,
                         "SQPmp");
    if (RT_FAILURE(vrc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot start SOAP queue pumper thread: %Rrc", vrc);

    // watchdog thread
    RTTHREAD threadWatchdog = NIL_RTTHREAD;
    if (g_iWatchdogTimeoutSecs > 0)
    {
        // start our watchdog thread
        vrc = RTThreadCreate(&threadWatchdog,
                             fntWatchdog,
                             NULL,
                             0,
                             RTTHREADTYPE_MAIN_WORKER,
                             RTTHREADFLAGS_WAITABLE,
                             "Watchdog");
        if (RT_FAILURE(vrc))
            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot start watchdog thread: %Rrc", vrc);
    }

#ifdef RT_OS_WINDOWS
    if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)websrvSignalHandler, TRUE /* Add handler */))
    {
        vrc = RTErrConvertFromWin32(GetLastError());
        RTMsgError("Unable to install console control handler, vrc=%Rrc\n", vrc);
    }
#else
    signal(SIGINT,   websrvSignalHandler);
    signal(SIGTERM,  websrvSignalHandler);
# ifdef SIGBREAK
    signal(SIGBREAK, websrvSignalHandler);
# endif
#endif

    com::NativeEventQueue *pQ = com::NativeEventQueue::getMainEventQueue();
    while (g_fKeepRunning)
    {
        // we have to process main event queue
        WEBDEBUG(("Pumping COM event queue\n"));
        vrc = pQ->processEventQueue(RT_INDEFINITE_WAIT);
        if (RT_FAILURE(vrc))
            RTMsgError("processEventQueue -> %Rrc", vrc);
    }

    LogRel(("requested termination, cleaning up\n"));

#ifdef RT_OS_WINDOWS
    if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)websrvSignalHandler, FALSE /* Remove handler */))
    {
        vrc = RTErrConvertFromWin32(GetLastError());
        RTMsgError("Unable to remove console control handler, vrc=%Rrc\n", vrc);
    }
#else
    signal(SIGINT,   SIG_DFL);
    signal(SIGTERM,  SIG_DFL);
# ifdef SIGBREAK
    signal(SIGBREAK, SIG_DFL);
# endif
#endif

#ifndef RT_OS_WINDOWS
    RTThreadPoke(threadQPumper);
#endif
    RTThreadWait(threadQPumper, 30000, NULL);
    if (threadWatchdog != NIL_RTTHREAD)
    {
#ifndef RT_OS_WINDOWS
        RTThreadPoke(threadWatchdog);
#endif
        RTThreadWait(threadWatchdog, g_iWatchdogCheckInterval * 1000 + 10000, NULL);
    }

    /* VirtualBoxClient events unregistration. */
    if (vboxClientListener)
    {
        ComPtr<IEventSource> pES;
        CHECK_ERROR(g_pVirtualBoxClient, COMGETTER(EventSource)(pES.asOutParam()));
        if (!pES.isNull())
            CHECK_ERROR(pES, UnregisterListener(vboxClientListener));
        vboxClientListener.setNull();
    }

    {
        util::AutoWriteLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS);
        g_pVirtualBox.setNull();
    }
    {
        util::AutoWriteLock lock(g_pWebsessionsLockHandle COMMA_LOCKVAL_SRC_POS);
        WebsessionsMapIterator it = g_mapWebsessions.begin(),
                               itEnd = g_mapWebsessions.end();
        while (it != itEnd)
        {
            WebServiceSession *pWebsession = it->second;
            WEBDEBUG(("SVC unavailable: websession %#llx stale, deleting\n", pWebsession->getID()));
            delete pWebsession;
            it = g_mapWebsessions.begin();
        }
    }
    g_pVirtualBoxClient.setNull();

    com::Shutdown();

    return 0;
}

/****************************************************************************
 *
 * Watchdog thread
 *
 ****************************************************************************/

/**
 * Watchdog thread, runs in the background while the webservice is alive.
 *
 * This gets started by main() and runs in the background to check all websessions
 * for whether they have been no requests in a configurable timeout period. In
 * that case, the websession is automatically logged off.
 */
static DECLCALLBACK(int) fntWatchdog(RTTHREAD hThreadSelf, void *pvUser)
{
    RT_NOREF(hThreadSelf, pvUser);

    // store a log prefix for this thread
    util::AutoWriteLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
    g_mapThreads[RTThreadSelf()] = "[W  ]";
    thrLock.release();

    WEBDEBUG(("Watchdog thread started\n"));

    uint32_t tNextStat = 0;

    while (g_fKeepRunning)
    {
        WEBDEBUG(("Watchdog: sleeping %d seconds\n", g_iWatchdogCheckInterval));
        RTThreadSleep(g_iWatchdogCheckInterval * 1000);

        uint32_t tNow = RTTimeProgramSecTS();

        // we're messing with websessions, so lock them
        util::AutoWriteLock lock(g_pWebsessionsLockHandle COMMA_LOCKVAL_SRC_POS);
        WEBDEBUG(("Watchdog: checking %d websessions\n", g_mapWebsessions.size()));

        WebsessionsMapIterator it = g_mapWebsessions.begin(),
                               itEnd = g_mapWebsessions.end();
        while (it != itEnd)
        {
            WebServiceSession *pWebsession = it->second;
            WEBDEBUG(("Watchdog: tNow: %d, websession timestamp: %d\n", tNow, pWebsession->getLastObjectLookup()));
            if (tNow > pWebsession->getLastObjectLookup() + g_iWatchdogTimeoutSecs)
            {
                WEBDEBUG(("Watchdog: websession %#llx timed out, deleting\n", pWebsession->getID()));
                delete pWebsession;
                it = g_mapWebsessions.begin();
            }
            else
                ++it;
        }

        // re-set the authentication method in case it has been changed
        if (g_pVirtualBox && g_pcszAuthentication && g_pcszAuthentication[0])
        {
            ComPtr<ISystemProperties> pSystemProperties;
            g_pVirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
            if (pSystemProperties)
                pSystemProperties->COMSETTER(WebServiceAuthLibrary)(com::Bstr(g_pcszAuthentication).raw());
        }

        // Log some MOR usage statistics every 5 minutes, but only if there's
        // something worth logging (at least one reference or a transition to
        // zero references). Avoids useless log spamming in idle webservice.
        if (tNow >= tNextStat)
        {
            size_t cMOR = 0;
            it = g_mapWebsessions.begin();
            itEnd = g_mapWebsessions.end();
            while (it != itEnd)
            {
                cMOR += it->second->CountRefs();
                ++it;
            }
            static bool fLastZero = false;
            if (cMOR || !fLastZero)
                LogRel(("Statistics: %zu websessions, %zu references\n",
                        g_mapWebsessions.size(), cMOR));
            fLastZero = (cMOR == 0);
            while (tNextStat <= tNow)
                tNextStat += 5 * 60; /* 5 minutes */
        }
    }

    thrLock.acquire();
    g_mapThreads.erase(RTThreadSelf());

    LogRel(("ending Watchdog thread\n"));
    return 0;
}

/****************************************************************************
 *
 * SOAP exceptions
 *
 ****************************************************************************/

/**
 * Helper function to raise a SOAP fault. Called by the other helper
 * functions, which raise specific SOAP faults.
 *
 * @param soap
 * @param pcsz
 * @param extype
 * @param ex
 */
static void RaiseSoapFault(struct soap *soap,
                           const char *pcsz,
                           int extype,
                           void *ex)
{
    // raise the fault
    soap_sender_fault(soap, pcsz, NULL);

    struct SOAP_ENV__Detail *pDetail = (struct SOAP_ENV__Detail*)soap_malloc(soap, sizeof(struct SOAP_ENV__Detail));

    // without the following, gSOAP crashes miserably when sending out the
    // data because it will try to serialize all fields (stupid documentation)
    memset(pDetail, 0, sizeof(struct SOAP_ENV__Detail));

    // fill extended info depending on SOAP version
    if (soap->version == 2) // SOAP 1.2 is used
    {
        soap->fault->SOAP_ENV__Detail = pDetail;
        soap->fault->SOAP_ENV__Detail->__type = extype;
        soap->fault->SOAP_ENV__Detail->fault = ex;
        soap->fault->SOAP_ENV__Detail->__any = NULL; // no other XML data
    }
    else
    {
        soap->fault->detail = pDetail;
        soap->fault->detail->__type = extype;
        soap->fault->detail->fault = ex;
        soap->fault->detail->__any = NULL; // no other XML data
    }
}

/**
 * Raises a SOAP fault that signals that an invalid object was passed.
 *
 * @param soap
 * @param obj
 */
void RaiseSoapInvalidObjectFault(struct soap *soap,
                                 WSDLT_ID obj)
{
    _vbox__InvalidObjectFault *ex = soap_new__vbox__InvalidObjectFault(soap, 1);
    ex->badObjectID = obj;

    std::string str("VirtualBox error: ");
    str += "Invalid managed object reference \"" + obj + "\"";

    RaiseSoapFault(soap,
                   str.c_str(),
                   SOAP_TYPE__vbox__InvalidObjectFault,
                   ex);
}

/**
 * Return a safe C++ string from the given COM string,
 * without crashing if the COM string is empty.
 * @param bstr
 * @return
 */
std::string ConvertComString(const com::Bstr &bstr)
{
    com::Utf8Str ustr(bstr);
    return ustr.c_str();        /// @todo r=dj since the length is known, we can probably use a better std::string allocator
}

/**
 * Return a safe C++ string from the given COM UUID,
 * without crashing if the UUID is empty.
 * @param uuid
 * @return
 */
std::string ConvertComString(const com::Guid &uuid)
{
    com::Utf8Str ustr(uuid.toString());
    return ustr.c_str();        /// @todo r=dj since the length is known, we can probably use a better std::string allocator
}

/** Code to handle string <-> byte arrays base64 conversion. */
std::string Base64EncodeByteArray(ComSafeArrayIn(BYTE, aData))
{

    com::SafeArray<BYTE> sfaData(ComSafeArrayInArg(aData));
    ssize_t cbData = sfaData.size();

    if (cbData == 0)
        return "";

    ssize_t cchOut = RTBase64EncodedLength(cbData);

    RTCString aStr;

    aStr.reserve(cchOut+1);
    int vrc = RTBase64Encode(sfaData.raw(), cbData, aStr.mutableRaw(), aStr.capacity(), NULL);
    AssertRC(vrc);
    aStr.jolt();

    return aStr.c_str();
}

#define DECODE_STR_MAX _1M
void Base64DecodeByteArray(struct soap *soap, const std::string& aStr, ComSafeArrayOut(BYTE, aData), const WSDLT_ID &idThis, const char *pszMethodName, IUnknown *pObj, const com::Guid &iid)
{
    const char* pszStr = aStr.c_str();
    ssize_t cbOut = RTBase64DecodedSize(pszStr, NULL);

    if (cbOut > DECODE_STR_MAX)
    {
        LogRel(("Decode string too long.\n"));
        RaiseSoapRuntimeFault(soap, idThis, pszMethodName, E_INVALIDARG, pObj, iid);
    }

    com::SafeArray<BYTE> result(cbOut);
    int vrc = RTBase64Decode(pszStr, result.raw(), cbOut, NULL, NULL);
    if (FAILED(vrc))
    {
        LogRel(("String Decoding Failed. Error code: %Rrc\n", vrc));
        RaiseSoapRuntimeFault(soap, idThis, pszMethodName, E_INVALIDARG, pObj, iid);
    }

    result.detachTo(ComSafeArrayOutArg(aData));
}

/**
 * Raises a SOAP runtime fault.
 *
 * @param soap
 * @param idThis
 * @param pcszMethodName
 * @param apirc
 * @param pObj
 * @param iid
 */
void RaiseSoapRuntimeFault(struct soap *soap,
                           const WSDLT_ID &idThis,
                           const char *pcszMethodName,
                           HRESULT apirc,
                           IUnknown *pObj,
                           const com::Guid &iid)
{
    com::ErrorInfo info(pObj, iid.ref());

    WEBDEBUG(("   error, raising SOAP exception\n"));

    LogRel(("API method name:            %s\n", pcszMethodName));
    LogRel(("API return code:            %#10lx (%Rhrc)\n", apirc, apirc));
    if (info.isFullAvailable() || info.isBasicAvailable())
    {
        const com::ErrorInfo *pInfo = &info;
        do
        {
            LogRel(("COM error info result code: %#10lx (%Rhrc)\n", pInfo->getResultCode(), pInfo->getResultCode()));
            LogRel(("COM error info text:        %ls\n", pInfo->getText().raw()));

            pInfo = pInfo->getNext();
        }
        while (pInfo);
    }

    // compose descriptive message
    com::Utf8StrFmt str("VirtualBox error: apirc=%#lx", apirc);
    if (info.isFullAvailable() || info.isBasicAvailable())
    {
        const com::ErrorInfo *pInfo = &info;
        do
        {
            str += com::Utf8StrFmt(" %ls (%#lx)", pInfo->getText().raw(), pInfo->getResultCode());
            pInfo = pInfo->getNext();
        }
        while (pInfo);
    }

    // allocate our own soap fault struct
    _vbox__RuntimeFault *ex = soap_new__vbox__RuntimeFault(soap, 1);
    ComPtr<IVirtualBoxErrorInfo> pVirtualBoxErrorInfo;
    info.getVirtualBoxErrorInfo(pVirtualBoxErrorInfo);
    ex->resultCode = apirc;
    ex->returnval = createOrFindRefFromComPtr(idThis, g_pcszIVirtualBoxErrorInfo, pVirtualBoxErrorInfo);

    RaiseSoapFault(soap,
                   str.c_str(),
                   SOAP_TYPE__vbox__RuntimeFault,
                   ex);
}

/****************************************************************************
 *
 *  splitting and merging of object IDs
 *
 ****************************************************************************/

/**
 * Splits a managed object reference (in string form, as passed in from a SOAP
 * method call) into two integers for websession and object IDs, respectively.
 *
 * @param id
 * @param pWebsessId
 * @param pObjId
 * @return
 */
static bool SplitManagedObjectRef(const WSDLT_ID &id,
                                  uint64_t *pWebsessId,
                                  uint64_t *pObjId)
{
    // 64-bit numbers in hex have 16 digits; hence
    // the object-ref string must have 16 + "-" + 16 characters
    if (    id.length() == 33
         && id[16] == '-'
       )
    {
        char psz[34];
        memcpy(psz, id.c_str(), 34);
        psz[16] = '\0';
        if (pWebsessId)
            RTStrToUInt64Full(psz, 16, pWebsessId);
        if (pObjId)
            RTStrToUInt64Full(psz + 17, 16, pObjId);
        return true;
    }

    return false;
}

/**
 * Creates a managed object reference (in string form) from
 * two integers representing a websession and object ID, respectively.
 *
 * @param sz Buffer with at least 34 bytes space to receive MOR string.
 * @param websessId
 * @param objId
 */
static void MakeManagedObjectRef(char *sz,
                                 uint64_t websessId,
                                 uint64_t objId)
{
    RTStrFormatNumber(sz, websessId, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
    sz[16] = '-';
    RTStrFormatNumber(sz + 17, objId, 16, 16, 0, RTSTR_F_64BIT | RTSTR_F_ZEROPAD);
}

/****************************************************************************
 *
 *  class WebServiceSession
 *
 ****************************************************************************/

class WebServiceSessionPrivate
{
    public:
        ManagedObjectsMapById       _mapManagedObjectsById;
        ManagedObjectsMapByPtr      _mapManagedObjectsByPtr;
};

/**
 * Constructor for the websession object.
 *
 * Preconditions: Caller must have locked g_pWebsessionsLockHandle.
 */
WebServiceSession::WebServiceSession()
    : _uNextObjectID(1),        // avoid 0 for no real reason
      _fDestructing(false),
      _tLastObjectLookup(0)
{
    _pp = new WebServiceSessionPrivate;
    _uWebsessionID = RTRandU64();

    // register this websession globally
    Assert(g_pWebsessionsLockHandle->isWriteLockOnCurrentThread());
    g_mapWebsessions[_uWebsessionID] = this;
}

/**
 * Destructor. Cleans up and destroys all contained managed object references on the way.
 *
 * Preconditions: Caller must have locked g_pWebsessionsLockHandle.
 */
WebServiceSession::~WebServiceSession()
{
    // delete us from global map first so we can't be found
    // any more while we're cleaning up
    Assert(g_pWebsessionsLockHandle->isWriteLockOnCurrentThread());
    g_mapWebsessions.erase(_uWebsessionID);

    // notify ManagedObjectRef destructor so it won't
    // remove itself from the maps; this avoids rebalancing
    // the map's tree on every delete as well
    _fDestructing = true;

    ManagedObjectsIteratorById it,
                               end = _pp->_mapManagedObjectsById.end();
    for (it = _pp->_mapManagedObjectsById.begin();
         it != end;
         ++it)
    {
        ManagedObjectRef *pRef = it->second;
        delete pRef;        // this frees the contained ComPtr as well
    }

    delete _pp;
}

/**
 *  Authenticate the username and password against an authentication authority.
 *
 *  @return 0 if the user was successfully authenticated, or an error code
 *  otherwise.
 */
int WebServiceSession::authenticate(const char *pcszUsername,
                                    const char *pcszPassword,
                                    IVirtualBox **ppVirtualBox)
{
    int vrc = VERR_WEB_NOT_AUTHENTICATED;
    ComPtr<IVirtualBox> pVirtualBox;
    {
        util::AutoReadLock vlock(g_pVirtualBoxLockHandle COMMA_LOCKVAL_SRC_POS);
        pVirtualBox = g_pVirtualBox;
    }
    if (pVirtualBox.isNull())
        return vrc;
    pVirtualBox.queryInterfaceTo(ppVirtualBox);

    util::AutoReadLock lock(g_pAuthLibLockHandle COMMA_LOCKVAL_SRC_POS);

    static bool fAuthLibLoaded = false;
    static PAUTHENTRY pfnAuthEntry = NULL;
    static PAUTHENTRY2 pfnAuthEntry2 = NULL;
    static PAUTHENTRY3 pfnAuthEntry3 = NULL;

    if (!fAuthLibLoaded)
    {
        // retrieve authentication library from system properties
        ComPtr<ISystemProperties> systemProperties;
        pVirtualBox->COMGETTER(SystemProperties)(systemProperties.asOutParam());

        com::Bstr authLibrary;
        systemProperties->COMGETTER(WebServiceAuthLibrary)(authLibrary.asOutParam());
        com::Utf8Str filename = authLibrary;

        LogRel(("External authentication library is '%ls'\n", authLibrary.raw()));

        if (filename == "null")
            // authentication disabled, let everyone in:
            fAuthLibLoaded = true;
        else
        {
            RTLDRMOD hlibAuth = 0;
            do
            {
                if (RTPathHavePath(filename.c_str()))
                    vrc = RTLdrLoad(filename.c_str(), &hlibAuth);
                else
                    vrc = RTLdrLoadAppPriv(filename.c_str(), &hlibAuth);
                if (RT_FAILURE(vrc))
                {
                    WEBDEBUG(("%s() Failed to load external authentication library '%s'. Error code: %Rrc\n",
                              __FUNCTION__, filename.c_str(), vrc));
                    break;
                }

                if (RT_FAILURE(vrc = RTLdrGetSymbol(hlibAuth, AUTHENTRY3_NAME, (void**)&pfnAuthEntry3)))
                {
                    WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n",
                              __FUNCTION__, AUTHENTRY3_NAME, vrc));

                    if (RT_FAILURE(vrc = RTLdrGetSymbol(hlibAuth, AUTHENTRY2_NAME, (void**)&pfnAuthEntry2)))
                    {
                        WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n",
                                  __FUNCTION__, AUTHENTRY2_NAME, vrc));

                        if (RT_FAILURE(vrc = RTLdrGetSymbol(hlibAuth, AUTHENTRY_NAME, (void**)&pfnAuthEntry)))
                            WEBDEBUG(("%s(): Could not resolve import '%s'. Error code: %Rrc\n",
                                      __FUNCTION__, AUTHENTRY_NAME, vrc));
                    }
                }

                if (pfnAuthEntry || pfnAuthEntry2 || pfnAuthEntry3)
                    fAuthLibLoaded = true;

            } while (0);
        }
    }

    if (strlen(pcszUsername) >= _1K)
    {
        LogRel(("Access denied, excessive username length: %zu\n", strlen(pcszUsername)));
        vrc = VERR_WEB_NOT_AUTHENTICATED;
    }
    else if (strlen(pcszPassword) >= _1K)
    {
        LogRel(("Access denied, excessive password length: %zu\n", strlen(pcszPassword)));
        vrc = VERR_WEB_NOT_AUTHENTICATED;
    }
    else if (pfnAuthEntry3 || pfnAuthEntry2 || pfnAuthEntry)
    {
        const char *pszFn;
        AuthResult result;
        if (pfnAuthEntry3)
        {
            result = pfnAuthEntry3("webservice", NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
            pszFn = AUTHENTRY3_NAME;
        }
        else if (pfnAuthEntry2)
        {
            result = pfnAuthEntry2(NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL, true, 0);
            pszFn = AUTHENTRY2_NAME;
        }
        else
        {
            result = pfnAuthEntry(NULL, AuthGuestNotAsked, pcszUsername, pcszPassword, NULL);
            pszFn = AUTHENTRY_NAME;
        }
        WEBDEBUG(("%s(): result of %s('%s', [%d]): %d (%s)\n",
                  __FUNCTION__, pszFn, pcszUsername, strlen(pcszPassword), result, decodeAuthResult(result)));
        if (result == AuthResultAccessGranted)
        {
            LogRel(("Access for user '%s' granted\n", pcszUsername));
            vrc = VINF_SUCCESS;
        }
        else
        {
            if (result == AuthResultAccessDenied)
                LogRel(("Access for user '%s' denied\n", pcszUsername));
            vrc = VERR_WEB_NOT_AUTHENTICATED;
        }
    }
    else if (fAuthLibLoaded)
    {
        // fAuthLibLoaded = true but all pointers are NULL:
        // The authlib was "null" and auth was disabled
        vrc = VINF_SUCCESS;
    }
    else
    {
        WEBDEBUG(("Could not resolve AuthEntry, VRDPAuth2 or VRDPAuth entry point"));
        vrc = VERR_WEB_NOT_AUTHENTICATED;
    }

    lock.release();

    return vrc;
}

/**
 *  Look up, in this websession, whether a ManagedObjectRef has already been
 *  created for the given COM pointer.
 *
 *  Note how we require that a ComPtr<IUnknown> is passed, which causes a
 *  queryInterface call when the caller passes in a different type, since
 *  a ComPtr<IUnknown> will point to something different than a
 *  ComPtr<IVirtualBox>, for example. As we store the ComPtr<IUnknown> in
 *  our private hash table, we must search for one too.
 *
 * Preconditions: Caller must have locked g_pWebsessionsLockHandle.
 *
 * @param pObject pointer to a COM object.
 * @return The existing ManagedObjectRef that represents the COM object, or NULL if there's none yet.
 */
ManagedObjectRef* WebServiceSession::findRefFromPtr(const IUnknown *pObject)
{
    Assert(g_pWebsessionsLockHandle->isWriteLockOnCurrentThread());

    uintptr_t ulp = (uintptr_t)pObject;
    // WEBDEBUG(("   %s: looking up %#lx\n", __FUNCTION__, ulp));
    ManagedObjectsIteratorByPtr it = _pp->_mapManagedObjectsByPtr.find(ulp);
    if (it != _pp->_mapManagedObjectsByPtr.end())
    {
        ManagedObjectRef *pRef = it->second;
        WEBDEBUG(("   %s: found existing ref %s (%s) for COM obj %#lx\n", __FUNCTION__, pRef->getWSDLID().c_str(), pRef->getInterfaceName(), ulp));
        return pRef;
    }

    return NULL;
}

/**
 * Static method which attempts to find the websession for which the given
 * managed object reference was created, by splitting the reference into the
 * websession and object IDs and then looking up the websession object.
 *
 * Preconditions: Caller must have locked g_pWebsessionsLockHandle in read mode.
 *
 * @param id Managed object reference (with combined websession and object IDs).
 * @return
 */
WebServiceSession *WebServiceSession::findWebsessionFromRef(const WSDLT_ID &id)
{
    Assert(g_pWebsessionsLockHandle->isWriteLockOnCurrentThread());

    WebServiceSession *pWebsession = NULL;
    uint64_t websessId;
    if (SplitManagedObjectRef(id,
                              &websessId,
                              NULL))
    {
        WebsessionsMapIterator it = g_mapWebsessions.find(websessId);
        if (it != g_mapWebsessions.end())
            pWebsession = it->second;
    }
    return pWebsession;
}

/**
 * Touches the websession to prevent it from timing out.
 *
 * Each websession has an internal timestamp that records the last request made
 * to it from the client that started it. If no request was made within a
 * configurable timeframe, then the client is logged off automatically,
 * by calling IWebsessionManager::logoff()
 */
void WebServiceSession::touch()
{
    _tLastObjectLookup = RTTimeProgramSecTS();
}

/**
 * Counts the number of managed object references in this websession.
 */
size_t WebServiceSession::CountRefs()
{
    return _pp->_mapManagedObjectsById.size();
}


/****************************************************************************
 *
 *  class ManagedObjectRef
 *
 ****************************************************************************/

/**
 *  Constructor, which assigns a unique ID to this managed object
 *  reference and stores it in two hashes (living in the associated
 *  WebServiceSession object):
 *
 *   a) _mapManagedObjectsById, which maps ManagedObjectID's to
 *      instances of this class; this hash is then used by the
 *      findObjectFromRef() template function in vboxweb.h
 *      to quickly retrieve the COM object from its managed
 *      object ID (mostly in the context of the method mappers
 *      in methodmaps.cpp, when a web service client passes in
 *      a managed object ID);
 *
 *   b) _mapManagedObjectsByPtr, which maps COM pointers to
 *      instances of this class; this hash is used by
 *      createRefFromObject() to quickly figure out whether an
 *      instance already exists for a given COM pointer.
 *
 *  This constructor calls AddRef() on the given COM object, and
 *  the destructor will call Release(). We require two input pointers
 *  for that COM object, one generic IUnknown* pointer which is used
 *  as the map key, and a specific interface pointer (e.g. IMachine*)
 *  which must support the interface given in guidInterface. All
 *  three values are returned by getPtr(), which gives future callers
 *  a chance to reuse the specific interface pointer without having
 *  to call QueryInterface, which can be expensive.
 *
 *  This does _not_ check whether another instance already
 *  exists in the hash. This gets called only from the
 *  createOrFindRefFromComPtr() template function in vboxweb.h, which
 *  does perform that check.
 *
 * Preconditions: Caller must have locked g_pWebsessionsLockHandle.
 *
 * @param websession Websession to which the MOR will be added.
 * @param pobjUnknown Pointer to IUnknown* interface for the COM object; this will be used in the hashes.
 * @param pobjInterface Pointer to a specific interface for the COM object, described by guidInterface.
 * @param guidInterface Interface which pobjInterface points to.
 * @param pcszInterface String representation of that interface (e.g. "IMachine") for readability and logging.
 */
ManagedObjectRef::ManagedObjectRef(WebServiceSession &websession,
                                   IUnknown *pobjUnknown,
                                   void *pobjInterface,
                                   const com::Guid &guidInterface,
                                   const char *pcszInterface)
    : _websession(websession),
      _pobjUnknown(pobjUnknown),
      _pobjInterface(pobjInterface),
      _guidInterface(guidInterface),
      _pcszInterface(pcszInterface)
{
    Assert(pobjUnknown);
    Assert(pobjInterface);

    // keep both stubs alive while this MOR exists (matching Release() calls are in destructor)
    uint32_t cRefs1 = pobjUnknown->AddRef();
    uint32_t cRefs2 = ((IUnknown*)pobjInterface)->AddRef();
    _ulp = (uintptr_t)pobjUnknown;

    Assert(g_pWebsessionsLockHandle->isWriteLockOnCurrentThread());
    _id = websession.createObjectID();
    // and count globally
    ULONG64 cTotal = ++g_cManagedObjects;           // raise global count and make a copy for the debug message below

    char sz[34];
    MakeManagedObjectRef(sz, websession._uWebsessionID, _id);
    _strID = sz;

    websession._pp->_mapManagedObjectsById[_id] = this;
    websession._pp->_mapManagedObjectsByPtr[_ulp] = this;

    websession.touch();

    WEBDEBUG(("   * %s: MOR created for %s*=%#p (IUnknown*=%#p; COM refcount now %RI32/%RI32), new ID is %#llx; now %lld objects total\n",
              __FUNCTION__,
              pcszInterface,
              pobjInterface,
              pobjUnknown,
              cRefs1,
              cRefs2,
              _id,
              cTotal));
}

/**
 * Destructor; removes the instance from the global hash of
 * managed objects. Calls Release() on the contained COM object.
 *
 * Preconditions: Caller must have locked g_pWebsessionsLockHandle.
 */
ManagedObjectRef::~ManagedObjectRef()
{
    Assert(g_pWebsessionsLockHandle->isWriteLockOnCurrentThread());
    ULONG64 cTotal = --g_cManagedObjects;

    Assert(_pobjUnknown);
    Assert(_pobjInterface);

    // we called AddRef() on both interfaces, so call Release() on
    // both as well, but in reverse order
    uint32_t cRefs2 = ((IUnknown*)_pobjInterface)->Release();
    uint32_t cRefs1 = _pobjUnknown->Release();
    WEBDEBUG(("   * %s: deleting MOR for ID %#llx (%s; COM refcount now %RI32/%RI32); now %lld objects total\n", __FUNCTION__, _id, _pcszInterface, cRefs1, cRefs2, cTotal));

    // if we're being destroyed from the websession's destructor,
    // then that destructor is iterating over the maps, so
    // don't remove us there! (data integrity + speed)
    if (!_websession._fDestructing)
    {
        WEBDEBUG(("   * %s: removing from websession maps\n", __FUNCTION__));
        _websession._pp->_mapManagedObjectsById.erase(_id);
        if (_websession._pp->_mapManagedObjectsByPtr.erase(_ulp) != 1)
            WEBDEBUG(("   WARNING: could not find %#llx in _mapManagedObjectsByPtr\n", _ulp));
    }
}

/**
 * Static helper method for findObjectFromRef() template that actually
 * looks up the object from a given integer ID.
 *
 * This has been extracted into this non-template function to reduce
 * code bloat as we have the actual STL map lookup only in this function.
 *
 * This also "touches" the timestamp in the websession whose ID is encoded
 * in the given integer ID, in order to prevent the websession from timing
 * out.
 *
 * Preconditions: Caller must have locked g_pWebsessionsLockHandle.
 *
 * @param   id
 * @param   pRef
 * @param   fNullAllowed
 * @return
 */
int ManagedObjectRef::findRefFromId(const WSDLT_ID &id,
                                    ManagedObjectRef **pRef,
                                    bool fNullAllowed)
{
    int vrc = VINF_SUCCESS;

    do
    {
        // allow NULL (== empty string) input reference, which should return a NULL pointer
        if (!id.length() && fNullAllowed)
        {
            *pRef = NULL;
            return 0;
        }

        uint64_t websessId;
        uint64_t objId;
        WEBDEBUG(("   %s(): looking up objref %s\n", __FUNCTION__, id.c_str()));
        if (!SplitManagedObjectRef(id,
                                   &websessId,
                                   &objId))
        {
            vrc = VERR_WEB_INVALID_MANAGED_OBJECT_REFERENCE;
            break;
        }

        WebsessionsMapIterator it = g_mapWebsessions.find(websessId);
        if (it == g_mapWebsessions.end())
        {
            WEBDEBUG(("   %s: cannot find websession for objref %s\n", __FUNCTION__, id.c_str()));
            vrc = VERR_WEB_INVALID_SESSION_ID;
            break;
        }

        WebServiceSession *pWebsession = it->second;
        // "touch" websession to prevent it from timing out
        pWebsession->touch();

        ManagedObjectsIteratorById iter = pWebsession->_pp->_mapManagedObjectsById.find(objId);
        if (iter == pWebsession->_pp->_mapManagedObjectsById.end())
        {
            WEBDEBUG(("   %s: cannot find comobj for objref %s\n", __FUNCTION__, id.c_str()));
            vrc = VERR_WEB_INVALID_OBJECT_ID;
            break;
        }

        *pRef = iter->second;

    } while (0);

    return vrc;
}

/****************************************************************************
 *
 * interface IManagedObjectRef
 *
 ****************************************************************************/

/**
 * This is the hard-coded implementation for the IManagedObjectRef::getInterfaceName()
 * that our WSDL promises to our web service clients. This method returns a
 * string describing the interface that this managed object reference
 * supports, e.g. "IMachine".
 *
 * @param soap
 * @param req
 * @param resp
 * @return
 */
int __vbox__IManagedObjectRef_USCOREgetInterfaceName(
    struct soap *soap,
    _vbox__IManagedObjectRef_USCOREgetInterfaceName *req,
    _vbox__IManagedObjectRef_USCOREgetInterfaceNameResponse *resp)
{
    RT_NOREF(soap);
    HRESULT hrc = S_OK; /** @todo r=bird: hrc is not set.... */
    WEBDEBUG(("-- entering %s\n", __FUNCTION__));

    do
    {
        // findRefFromId require the lock
        util::AutoWriteLock lock(g_pWebsessionsLockHandle COMMA_LOCKVAL_SRC_POS);

        ManagedObjectRef *pRef;
        if (!ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false))
            resp->returnval = pRef->getInterfaceName();

    } while (0);

    WEBDEBUG(("-- leaving %s, hrc: %#lx\n", __FUNCTION__, hrc));
    if (FAILED(hrc))
        return SOAP_FAULT;
    return SOAP_OK;
}

/**
 * This is the hard-coded implementation for the IManagedObjectRef::release()
 * that our WSDL promises to our web service clients. This method releases
 * a managed object reference and removes it from our stacks.
 *
 * @param soap
 * @param req
 * @param resp
 * @return
 */
int __vbox__IManagedObjectRef_USCORErelease(
    struct soap *soap,
    _vbox__IManagedObjectRef_USCORErelease *req,
    _vbox__IManagedObjectRef_USCOREreleaseResponse *resp)
{
    RT_NOREF(resp);
    HRESULT hrc;
    WEBDEBUG(("-- entering %s\n", __FUNCTION__));

    {
        // findRefFromId and the delete call below require the lock
        util::AutoWriteLock lock(g_pWebsessionsLockHandle COMMA_LOCKVAL_SRC_POS);

        ManagedObjectRef *pRef;
        hrc = ManagedObjectRef::findRefFromId(req->_USCOREthis, &pRef, false);
        if (hrc == S_OK)
        {
            WEBDEBUG(("   found reference; deleting!\n"));
            // this removes the object from all stacks; since
            // there's a ComPtr<> hidden inside the reference,
            // this should also invoke Release() on the COM
            // object
            delete pRef;
        }
        else
            RaiseSoapInvalidObjectFault(soap, req->_USCOREthis);
    }

    WEBDEBUG(("-- leaving %s, hrc: %#lx\n", __FUNCTION__, hrc));
    if (FAILED(hrc))
        return SOAP_FAULT;
    return SOAP_OK;
}

/****************************************************************************
 *
 * interface IWebsessionManager
 *
 ****************************************************************************/

/**
 * Hard-coded implementation for IWebsessionManager::logon. As opposed to the underlying
 * COM API, this is the first method that a webservice client must call before the
 * webservice will do anything useful.
 *
 * This returns a managed object reference to the global IVirtualBox object; into this
 * reference a websession ID is encoded which remains constant with all managed object
 * references returned by other methods.
 *
 * When the webservice client is done, it should call IWebsessionManager::logoff. This
 * will clean up internally (destroy all remaining managed object references and
 * related COM objects used internally).
 *
 * After logon, an internal timeout ensures that if the webservice client does not
 * call any methods, after a configurable number of seconds, the webservice will log
 * off the client automatically. This is to ensure that the webservice does not
 * drown in managed object references and eventually deny service. Still, it is
 * a much better solution, both for performance and cleanliness, for the webservice
 * client to clean up itself.
 *
 * @param soap
 * @param req
 * @param resp
 * @return
 */
int __vbox__IWebsessionManager_USCORElogon(
        struct soap *soap,
        _vbox__IWebsessionManager_USCORElogon *req,
        _vbox__IWebsessionManager_USCORElogonResponse *resp)
{
    RT_NOREF(soap);
    HRESULT hrc = S_OK;
    WEBDEBUG(("-- entering %s\n", __FUNCTION__));

    do
    {
        // WebServiceSession constructor tinkers with global MOR map and requires a write lock
        util::AutoWriteLock lock(g_pWebsessionsLockHandle COMMA_LOCKVAL_SRC_POS);

        // create new websession; the constructor stores the new websession
        // in the global map automatically
        WebServiceSession *pWebsession = new WebServiceSession();
        ComPtr<IVirtualBox> pVirtualBox;

        // authenticate the user
        if (!(pWebsession->authenticate(req->username.c_str(),
                                        req->password.c_str(),
                                        pVirtualBox.asOutParam())))
        {
            // fake up a "root" MOR for this websession
            char sz[34];
            MakeManagedObjectRef(sz, pWebsession->getID(), 0ULL);
            WSDLT_ID id = sz;

            // in the new websession, create a managed object reference (MOR) for the
            // global VirtualBox object; this encodes the websession ID in the MOR so
            // that it will be implicitly be included in all future requests of this
            // webservice client
            resp->returnval = createOrFindRefFromComPtr(id, g_pcszIVirtualBox, pVirtualBox);
            WEBDEBUG(("VirtualBox object ref is %s\n", resp->returnval.c_str()));
        }
        else
            hrc = E_FAIL;
    } while (0);

    WEBDEBUG(("-- leaving %s, hrc: %#lx\n", __FUNCTION__, hrc));
    if (FAILED(hrc))
        return SOAP_FAULT;
    return SOAP_OK;
}

/**
 * Returns a new ISession object every time.
 *
 * No longer connected in any way to logons, one websession can easily
 * handle multiple sessions.
 */
int __vbox__IWebsessionManager_USCOREgetSessionObject(
        struct soap*,
        _vbox__IWebsessionManager_USCOREgetSessionObject *req,
        _vbox__IWebsessionManager_USCOREgetSessionObjectResponse *resp)
{
    HRESULT hrc = S_OK;
    WEBDEBUG(("-- entering %s\n", __FUNCTION__));

    do
    {
        // create a new ISession object
        ComPtr<ISession> pSession;
        hrc = g_pVirtualBoxClient->COMGETTER(Session)(pSession.asOutParam());
        if (FAILED(hrc))
        {
            WEBDEBUG(("ERROR: cannot create session object!"));
            break;
        }

        // return its MOR
        resp->returnval = createOrFindRefFromComPtr(req->refIVirtualBox, g_pcszISession, pSession);
        WEBDEBUG(("Session object ref is %s\n", resp->returnval.c_str()));
    } while (0);

    WEBDEBUG(("-- leaving %s, hrc: %#lx\n", __FUNCTION__, hrc));
    if (FAILED(hrc))
        return SOAP_FAULT;
    return SOAP_OK;
}

/**
 * hard-coded implementation for IWebsessionManager::logoff.
 *
 * @param req
 * @param resp
 * @return
 */
int __vbox__IWebsessionManager_USCORElogoff(
        struct soap*,
        _vbox__IWebsessionManager_USCORElogoff *req,
        _vbox__IWebsessionManager_USCORElogoffResponse *resp)
{
    RT_NOREF(resp);
    HRESULT hrc = S_OK;
    WEBDEBUG(("-- entering %s\n", __FUNCTION__));

    {
        // findWebsessionFromRef and the websession destructor require the lock
        util::AutoWriteLock lock(g_pWebsessionsLockHandle COMMA_LOCKVAL_SRC_POS);

        WebServiceSession *pWebsession = WebServiceSession::findWebsessionFromRef(req->refIVirtualBox);
        if (pWebsession)
        {
            WEBDEBUG(("websession logoff, deleting websession %#llx\n", pWebsession->getID()));
            delete pWebsession;
                // destructor cleans up

            WEBDEBUG(("websession destroyed, %d websessions left open\n", g_mapWebsessions.size()));
        }
    }

    WEBDEBUG(("-- leaving %s, hrc: %#lx\n", __FUNCTION__, hrc));
    if (FAILED(hrc))
        return SOAP_FAULT;
    return SOAP_OK;
}