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
|
/* $Id: VBoxCredentialProvider.cpp $ */
/** @file
* VBoxCredentialProvider - Main file of the VirtualBox Credential Provider.
*/
/*
* Copyright (C) 2012-2014 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <windows.h>
#include <initguid.h>
#include <new> /* For bad_alloc. */
#ifdef VBOX_WITH_SENS
# include <eventsys.h>
# include <sens.h>
# include <Sensevts.h>
#endif
#include <iprt/buildconfig.h>
#include <iprt/initterm.h>
#ifdef VBOX_WITH_SENS
# include <iprt/string.h>
#endif
#include <VBox/VBoxGuestLib.h>
#include "VBoxCredentialProvider.h"
#include "VBoxCredProvFactory.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static LONG g_cDllRefs = 0; /**< Global DLL reference count. */
static HINSTANCE g_hDllInst = NULL; /**< Global DLL hInstance. */
#ifdef VBOX_WITH_SENS
static bool g_fSENSEnabled = false;
static IEventSystem *g_pIEventSystem = NULL; /**< Pointer to IEventSystem interface. */
/**
* Subscribed SENS events.
*/
static struct VBOXCREDPROVSENSEVENTS
{
/** The actual method name the subscription is for. */
char *pszMethod;
/** A friendly name for the subscription. */
char *pszSubscriptionName;
/** The actual subscription UUID.
* Should not be changed. */
char *pszSubscriptionUUID;
} g_aSENSEvents[] = {
{ "Logon", "VBoxCredProv SENS Logon", "{561D0791-47C0-4BC3-87C0-CDC2621EA653}" },
{ "Logoff", "VBoxCredProv SENS Logoff", "{12B618B1-F2E0-4390-BADA-7EB1DC31A70A}" },
{ "StartShell", "VBoxCredProv SENS StartShell", "{5941931D-015A-4F91-98DA-81AAE262D090}" },
{ "DisplayLock", "VBoxCredProv SENS DisplayLock", "{B7E2C510-501A-4961-938F-A458970930D7}" },
{ "DisplayUnlock", "VBoxCredProv SENS DisplayUnlock", "{11305987-8FFC-41AD-A264-991BD5B7488A}" },
{ "StartScreenSaver", "VBoxCredProv SENS StartScreenSaver", "{6E2D26DF-0095-4EC4-AE00-2395F09AF7F2}" },
{ "StopScreenSaver", "VBoxCredProv SENS StopScreenSaver", "{F53426BC-412F-41E8-9A5F-E5FA8A164BD6}" }
};
/**
* Implementation of the ISensLogon interface for getting
* SENS (System Event Notification Service) events. SENS must be up
* and running on this OS!
*/
interface VBoxCredProvSensLogon : public ISensLogon
{
public:
VBoxCredProvSensLogon(void) :
m_cRefs(1)
{
}
STDMETHODIMP QueryInterface(REFIID interfaceID, void **ppvInterface)
{
if ( IsEqualIID(interfaceID, IID_IUnknown)
|| IsEqualIID(interfaceID, IID_IDispatch)
|| IsEqualIID(interfaceID, IID_ISensLogon))
{
*ppvInterface = this;
AddRef();
return S_OK;
}
*ppvInterface = NULL;
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef(void)
{
return InterlockedIncrement(&m_cRefs);
}
ULONG STDMETHODCALLTYPE Release(void)
{
ULONG ulTemp = InterlockedDecrement(&m_cRefs);
return ulTemp;
}
HRESULT STDMETHODCALLTYPE GetTypeInfoCount(unsigned int FAR* pctinfo)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE GetTypeInfo(unsigned int iTInfo, LCID lcid, ITypeInfo FAR* FAR* ppTInfo)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE GetIDsOfNames(REFIID riid,
OLECHAR FAR* FAR* rgszNames, unsigned int cNames,
LCID lcid, DISPID FAR* rgDispId)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags,
DISPPARAMS FAR* pDispParams, VARIANT FAR* parResult, EXCEPINFO FAR* pExcepInfo,
unsigned int FAR* puArgErr)
{
return E_NOTIMPL;
}
/* ISensLogon methods */
STDMETHODIMP Logon(BSTR bstrUserName)
{
VBoxCredProvVerbose(0, "VBoxCredProvSensLogon: Logon\n");
return S_OK;
}
STDMETHODIMP Logoff(BSTR bstrUserName)
{
VBoxCredProvVerbose(0, "VBoxCredProvSensLogon: Logoff\n");
return S_OK;
}
STDMETHODIMP StartShell(BSTR bstrUserName)
{
VBoxCredProvVerbose(0, "VBoxCredProvSensLogon: Logon\n");
return S_OK;
}
STDMETHODIMP DisplayLock(BSTR bstrUserName)
{
VBoxCredProvVerbose(0, "VBoxCredProvSensLogon: DisplayLock\n");
return S_OK;
}
STDMETHODIMP DisplayUnlock(BSTR bstrUserName)
{
VBoxCredProvVerbose(0, "VBoxCredProvSensLogon: DisplayUnlock\n");
return S_OK;
}
STDMETHODIMP StartScreenSaver(BSTR bstrUserName)
{
VBoxCredProvVerbose(0, "VBoxCredProvSensLogon: StartScreenSaver\n");
return S_OK;
}
STDMETHODIMP StopScreenSaver(BSTR bstrUserName)
{
VBoxCredProvVerbose(0, "VBoxCredProvSensLogon: StopScreenSaver\n");
return S_OK;
}
protected:
LONG m_cRefs;
};
static VBoxCredProvSensLogon *g_pISensLogon = NULL;
/**
* Register events to be called by SENS.
*
* @return HRESULT
*/
static HRESULT VBoxCredentialProviderRegisterSENS(void)
{
VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS\n");
HRESULT hr = CoCreateInstance(CLSID_CEventSystem, 0, CLSCTX_SERVER, IID_IEventSystem, (void**)&g_pIEventSystem);
if (FAILED(hr))
{
VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Could not connect to CEventSystem, hr=%Rhrc\n",
hr);
return hr;
}
try
{
g_pISensLogon = new VBoxCredProvSensLogon();
AssertPtr(g_pISensLogon);
}
catch (std::bad_alloc &ex)
{
NOREF(ex);
hr = E_OUTOFMEMORY;
}
if ( SUCCEEDED(hr)
&& g_pIEventSystem)
{
IEventSubscription *pIEventSubscription;
int i;
for (i = 0; i < RT_ELEMENTS(g_aSENSEvents); i++)
{
VBoxCredProvVerbose(0, "VBoxCredProv: Registering \"%s\" (%s) ...\n",
g_aSENSEvents[i].pszMethod, g_aSENSEvents[i].pszSubscriptionName);
hr = CoCreateInstance(CLSID_CEventSubscription, 0, CLSCTX_SERVER, IID_IEventSubscription, (LPVOID*)&pIEventSubscription);
if (FAILED(hr))
continue;
hr = pIEventSubscription->put_EventClassID(L"{d5978630-5b9f-11d1-8dd2-00aa004abd5e}" /* SENSGUID_EVENTCLASS_LOGON */);
if (FAILED(hr))
break;
hr = pIEventSubscription->put_SubscriberInterface((IUnknown*)g_pISensLogon);
if (FAILED(hr))
break;
PRTUTF16 pwszTemp;
int rc = RTStrToUtf16(g_aSENSEvents[i].pszMethod, &pwszTemp);
if (RT_SUCCESS(rc))
{
hr = pIEventSubscription->put_MethodName(pwszTemp);
RTUtf16Free(pwszTemp);
}
else
hr = ERROR_OUTOFMEMORY;
if (FAILED(hr))
break;
rc = RTStrToUtf16(g_aSENSEvents[i].pszSubscriptionName, &pwszTemp);
if (RT_SUCCESS(rc))
{
hr = pIEventSubscription->put_SubscriptionName(pwszTemp);
RTUtf16Free(pwszTemp);
}
else
hr = ERROR_OUTOFMEMORY;
if (FAILED(hr))
break;
rc = RTStrToUtf16(g_aSENSEvents[i].pszSubscriptionUUID, &pwszTemp);
if (RT_SUCCESS(rc))
{
hr = pIEventSubscription->put_SubscriptionID(pwszTemp);
RTUtf16Free(pwszTemp);
}
else
hr = ERROR_OUTOFMEMORY;
if (FAILED(hr))
break;
hr = pIEventSubscription->put_PerUser(TRUE);
if (FAILED(hr))
break;
hr = g_pIEventSystem->Store(PROGID_EventSubscription, (IUnknown*)pIEventSubscription);
if (FAILED(hr))
break;
pIEventSubscription->Release();
pIEventSubscription = NULL;
}
if (FAILED(hr))
VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Could not register \"%s\" (%s), hr=%Rhrc\n",
g_aSENSEvents[i].pszMethod, g_aSENSEvents[i].pszSubscriptionName, hr);
if (pIEventSubscription != NULL)
pIEventSubscription->Release();
}
if (FAILED(hr))
{
VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Error registering SENS provider, hr=%Rhrc\n", hr);
if (g_pIEventSystem)
{
g_pIEventSystem->Release();
g_pIEventSystem = NULL;
}
}
VBoxCredProvVerbose(0, "VBoxCredentialProviderRegisterSENS: Returning hr=%Rhrc\n", hr);
return hr;
}
/**
* Unregisters registered SENS events.
*/
static void VBoxCredentialProviderUnregisterSENS(void)
{
if (g_pIEventSystem)
{
g_pIEventSystem->Release();
g_pIEventSystem = NULL;
}
/* We need to reconnecto to the event system because we can be called
* in a different context COM can't handle. */
HRESULT hr = CoCreateInstance(CLSID_CEventSystem, 0,
CLSCTX_SERVER, IID_IEventSystem, (void**)&g_pIEventSystem);
if ( SUCCEEDED(hr)
&& g_pIEventSystem)
{
VBoxCredProvVerbose(0, "VBoxCredentialProviderUnregisterSENS\n");
HRESULT hr;
for (int i = 0; i < RT_ELEMENTS(g_aSENSEvents); i++)
{
int iErrorIdX;
char *pszSubToRemove;
if (!RTStrAPrintf(&pszSubToRemove, "SubscriptionID=%s",
g_aSENSEvents[i].pszSubscriptionUUID))
{
continue; /* Keep going. */
}
PRTUTF16 pwszTemp;
int rc2 = RTStrToUtf16(pszSubToRemove, &pwszTemp);
if (RT_SUCCESS(rc2))
{
hr = g_pIEventSystem->Remove(PROGID_EventSubscription, pwszTemp,
&iErrorIdX);
RTUtf16Free(pwszTemp);
}
else
hr = ERROR_OUTOFMEMORY;
if (FAILED(hr))
VBoxCredProvVerbose(0, "VBoxCredentialProviderUnregisterSENS: Could not unregister \"%s\" (query: %s), hr=%Rhrc (index: %d)\n",
g_aSENSEvents[i].pszMethod, pszSubToRemove, hr, iErrorIdX);
/* Keep going. */
RTStrFree(pszSubToRemove);
}
g_pIEventSystem->Release();
g_pIEventSystem = NULL;
}
if (g_pISensLogon)
{
delete g_pISensLogon;
g_pISensLogon = NULL;
}
VBoxCredProvVerbose(0, "VBoxCredentialProviderUnregisterSENS: Returning hr=%Rhrc\n", hr);
}
#endif /* VBOX_WITH_SENS */
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID pReserved)
{
NOREF(pReserved);
g_hDllInst = hInst;
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
{
int rc = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
if (RT_SUCCESS(rc))
rc = VbglR3Init();
if (RT_SUCCESS(rc))
{
VBoxCredProvVerbose(0, "VBoxCredProv: v%s r%s (%s %s) loaded (refs=%ld)\n",
RTBldCfgVersion(), RTBldCfgRevisionStr(),
__DATE__, __TIME__, g_cDllRefs);
}
DisableThreadLibraryCalls(hInst);
break;
}
case DLL_PROCESS_DETACH:
VBoxCredProvVerbose(0, "VBoxCredProv: Unloaded (refs=%ld)\n", g_cDllRefs);
if (!g_cDllRefs)
VbglR3Term();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
default:
break;
}
return TRUE;
}
/**
* Increments the reference count by one. Must be released
* with VBoxCredentialProviderRelease() when finished.
*/
void VBoxCredentialProviderAcquire(void)
{
LONG cRefCount = InterlockedIncrement(&g_cDllRefs);
VBoxCredProvVerbose(0, "VBoxCredentialProviderAcquire: Increasing global refcount to %ld\n",
cRefCount);
}
/**
* Decrements the reference count by one.
*/
void VBoxCredentialProviderRelease(void)
{
LONG cRefCount = InterlockedDecrement(&g_cDllRefs);
VBoxCredProvVerbose(0, "VBoxCredentialProviderRelease: Decreasing global refcount to %ld\n",
cRefCount);
}
/**
* Returns the current DLL reference count.
*
* @return LONG The current reference count.
*/
LONG VBoxCredentialProviderRefCount(void)
{
return g_cDllRefs;
}
/**
* Entry point for determining whether the credential
* provider DLL can be unloaded or not.
*
* @return HRESULT
*/
HRESULT __stdcall DllCanUnloadNow(void)
{
VBoxCredProvVerbose(0, "DllCanUnloadNow (refs=%ld)\n",
g_cDllRefs);
#ifdef VBOX_WITH_SENS
if (!g_cDllRefs)
{
if (g_fSENSEnabled)
VBoxCredentialProviderUnregisterSENS();
CoUninitialize();
}
#endif
return (g_cDllRefs > 0) ? S_FALSE : S_OK;
}
/**
* Create the VirtualBox credential provider by creating
* its factory which then in turn can create instances of the
* provider itself.
*
* @return HRESULT
* @param classID The class ID.
* @param interfaceID The interface ID.
* @param ppvInterface Receives the interface pointer on successful
* object creation.
*/
HRESULT VBoxCredentialProviderCreate(REFCLSID classID, REFIID interfaceID,
void **ppvInterface)
{
HRESULT hr;
if (classID == CLSID_VBoxCredProvider)
{
try
{
VBoxCredProvFactory* pFactory = new VBoxCredProvFactory();
AssertPtr(pFactory);
hr = pFactory->QueryInterface(interfaceID,
ppvInterface);
pFactory->Release();
#ifdef VBOX_WITH_SENS
g_fSENSEnabled = true; /* By default SENS support is enabled. */
HKEY hKey;
/** @todo Add some registry wrapper function(s) as soon as we got more values to retrieve. */
DWORD dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Oracle\\VirtualBox Guest Additions\\AutoLogon",
0L, KEY_QUERY_VALUE, &hKey);
if (dwRet == ERROR_SUCCESS)
{
DWORD dwValue;
DWORD dwType = REG_DWORD;
DWORD dwSize = sizeof(DWORD);
dwRet = RegQueryValueEx(hKey, L"HandleSENS", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
if ( dwRet == ERROR_SUCCESS
&& dwType == REG_DWORD
&& dwSize == sizeof(DWORD))
{
g_fSENSEnabled = RT_BOOL(dwValue);
}
RegCloseKey(hKey);
}
VBoxCredProvVerbose(0, "VBoxCredentialProviderCreate: g_fSENSEnabled=%RTbool\n",
g_fSENSEnabled);
if ( SUCCEEDED(hr)
&& g_fSENSEnabled)
{
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
VBoxCredentialProviderRegisterSENS();
}
#else
VBoxCredProvVerbose(0, "VBoxCredentialProviderCreate: SENS support is disabled\n");
#endif
}
catch (std::bad_alloc &ex)
{
NOREF(ex);
hr = E_OUTOFMEMORY;
}
}
else
hr = CLASS_E_CLASSNOTAVAILABLE;
return hr;
}
/**
* Entry point for getting the actual credential provider
* class object.
*
* @return HRESULT
* @param classID The class ID.
* @param interfaceID The interface ID.
* @param ppvInterface Receives the interface pointer on successful
* object creation.
*/
HRESULT __stdcall DllGetClassObject(REFCLSID classID, REFIID interfaceID,
void **ppvInterface)
{
VBoxCredProvVerbose(0, "DllGetClassObject (refs=%ld)\n",
g_cDllRefs);
return VBoxCredentialProviderCreate(classID, interfaceID, ppvInterface);
}
|