VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxCredProv/VBoxCredProvProvider.cpp@ 40272

Last change on this file since 40272 was 40271, checked in by vboxsync, 13 years ago

VBoxCredProv: fixed svn properties

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 KB
Line 
1/* $Id: VBoxCredProvProvider.cpp 40271 2012-02-28 11:22:04Z vboxsync $ */
2/** @file
3 * VBoxCredProvProvider - The actual credential provider class.
4 */
5
6/*
7 * Copyright (C) 2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <credentialprovider.h>
19
20#include <iprt/err.h>
21
22#include <VBox/VBoxGuestLib.h>
23
24#include "VBoxCredentialProvider.h"
25#include "VBoxCredProvProvider.h"
26#include "VBoxCredProvCredential.h"
27
28VBoxCredProvProvider::VBoxCredProvProvider(void) :
29 m_cRefCount(1),
30 m_pPoller(NULL),
31 m_pCred(NULL),
32 m_pCredProvEvents(NULL),
33 m_fGotCredentials(false),
34 m_fHandleRemoteSessions(false)
35{
36 LONG cRefCount = VBoxCredentialProviderAcquire();
37
38 VBoxCredProvReportStatus(VBoxGuestFacilityStatus_Init);
39}
40
41
42VBoxCredProvProvider::~VBoxCredProvProvider(void)
43{
44 if (m_pCred)
45 {
46 m_pCred->Release();
47 m_pCred = NULL;
48 }
49
50 if (m_pPoller)
51 {
52 m_pPoller->Shutdown();
53 delete m_pPoller;
54 m_pPoller = NULL;
55 }
56
57 ULONG cRefCount = VBoxCredentialProviderRefCount();
58 VBoxCredProvVerbose(0, "VBoxCredProv: Destroying (global DLL refcount=%ld)\n", cRefCount);
59
60 VBoxCredProvReportStatus(VBoxGuestFacilityStatus_Terminated);
61
62 VBoxCredentialProviderRelease();
63}
64
65
66/* IUnknown overrides. */
67ULONG VBoxCredProvProvider::AddRef(void)
68{
69 VBoxCredProvVerbose(0, "VBoxCredProv: Increasing reference from %ld to %ld\n",
70 m_cRefCount, m_cRefCount + 1);
71
72 return m_cRefCount++;
73}
74
75
76ULONG VBoxCredProvProvider::Release(void)
77{
78 Assert(m_cRefCount);
79
80 VBoxCredProvVerbose(0, "VBoxCredProv: Decreasing reference from %ld to %ld\n",
81 m_cRefCount, m_cRefCount - 1);
82
83 ULONG cRefCount = --m_cRefCount;
84 if (!cRefCount)
85 {
86 VBoxCredProvVerbose(0, "VBoxCredProv: Calling destructor\n");
87 delete this;
88 }
89 return cRefCount;
90}
91
92
93HRESULT VBoxCredProvProvider::QueryInterface(REFIID interfaceID, void **ppvInterface)
94{
95 HRESULT hr = S_OK;;
96 if (ppvInterface)
97 {
98 if ( IID_IUnknown == interfaceID
99 || IID_ICredentialProvider == interfaceID)
100 {
101 *ppvInterface = static_cast<IUnknown*>(this);
102 reinterpret_cast<IUnknown*>(*ppvInterface)->AddRef();
103 }
104 else
105 {
106 *ppvInterface = NULL;
107 hr = E_NOINTERFACE;
108 }
109 }
110 else
111 hr = E_INVALIDARG;
112
113 return hr;
114}
115
116
117/**
118 * Loads the global configuration from registry.
119 *
120 * @return DWORD Windows error code.
121 */
122DWORD VBoxCredProvProvider::LoadConfiguration(void)
123{
124 HKEY hKey;
125 /** @todo Add some registry wrapper function(s) as soon as we got more values to retrieve. */
126 DWORD dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Oracle\\VirtualBox Guest Additions\\AutoLogon",
127 0L, KEY_QUERY_VALUE, &hKey);
128 if (dwRet == ERROR_SUCCESS)
129 {
130 DWORD dwValue;
131 DWORD dwType = REG_DWORD;
132 DWORD dwSize = sizeof(DWORD);
133
134 dwRet = RegQueryValueEx(hKey, L"HandleRemoteSessions", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
135 if ( dwRet == ERROR_SUCCESS
136 && dwType == REG_DWORD
137 && dwSize == sizeof(DWORD))
138 {
139 m_fHandleRemoteSessions = true;
140 }
141
142 dwRet = RegQueryValueEx(hKey, L"LoggingEnabled", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
143 if ( dwRet == ERROR_SUCCESS
144 && dwType == REG_DWORD
145 && dwSize == sizeof(DWORD))
146 {
147 g_dwVerbosity = 1; /* Default logging level. */
148 }
149
150 if (g_dwVerbosity) /* Do we want logging at all? */
151 {
152 dwRet = RegQueryValueEx(hKey, L"LoggingLevel", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
153 if ( dwRet == ERROR_SUCCESS
154 && dwType == REG_DWORD
155 && dwSize == sizeof(DWORD))
156 {
157 g_dwVerbosity = dwValue;
158 }
159 }
160
161 RegCloseKey(hKey);
162 }
163 /* Do not report back an error here yet. */
164 return ERROR_SUCCESS;
165}
166
167
168/**
169 * Determines whether we should handle the current session or not.
170 *
171 * @return bool true if we should handle this session, false if not.
172 */
173bool VBoxCredProvProvider::HandleCurrentSession(void)
174{
175 /* Load global configuration from registry. */
176 int rc = LoadConfiguration();
177 if (RT_FAILURE(rc))
178 VBoxCredProvVerbose(0, "VBoxCredProv: Error loading global configuration, rc=%Rrc\n",
179 rc);
180
181 bool fHandle = false;
182 if (VbglR3AutoLogonIsRemoteSession())
183 {
184 if (m_fHandleRemoteSessions) /* Force remote session handling. */
185 fHandle = true;
186 }
187 else /* No remote session. */
188 fHandle = true;
189
190 VBoxCredProvVerbose(3, "VBoxCredProv: Handling current session=%RTbool\n", fHandle);
191 return fHandle;
192}
193
194
195/*
196 * SetUsageScenario is the provider's cue that it's going to be asked for tiles
197 * in a subsequent call. This call happens after the user pressed CTRL+ALT+DEL
198 * and we need to handle the CPUS_LOGON event.
199 */
200HRESULT VBoxCredProvProvider::SetUsageScenario(CREDENTIAL_PROVIDER_USAGE_SCENARIO cpUsageScenario,
201 DWORD dwFlags)
202{
203 HRESULT hr = S_OK;
204 DWORD dwErr;
205
206 m_cpUsageScenario = cpUsageScenario;
207
208 /* Decide which scenarios to support here. Returning E_NOTIMPL simply tells the caller
209 * that we're not designed for that scenario. */
210 switch (m_cpUsageScenario)
211 {
212 case CPUS_LOGON:
213 case CPUS_UNLOCK_WORKSTATION:
214
215 VBoxCredProvReportStatus(VBoxGuestFacilityStatus_Active);
216
217 dwErr = LoadConfiguration();
218 if (dwErr != ERROR_SUCCESS)
219 VBoxCredProvVerbose(0, "VBoxCredProv: Error while loading configuration, error=%ld\n", dwErr);
220 /* Do not stop running on a misconfigured system. */
221
222 /*
223 * If we're told to not handle the current session just bail out and let the
224 * user know.
225 */
226 if (!HandleCurrentSession())
227 break;
228
229 if (!m_pPoller)
230 {
231 m_pPoller = new VBoxCredProvPoller();
232 AssertPtr(m_pPoller);
233 int rc = m_pPoller->Initialize(this);
234 if (RT_FAILURE(rc))
235 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario: Error initializing poller thread, rc=%Rrc\n", rc);
236 }
237
238 if (!m_pCred)
239 {
240 m_pCred = new VBoxCredProvCredential(this);
241 if (m_pCred)
242 {
243 hr = m_pCred->Initialize(m_cpUsageScenario);
244 }
245 else
246 hr = E_OUTOFMEMORY;
247 }
248 else
249 {
250 /* All set up already! Nothing to do here right now. */
251 }
252
253 /* If we did fail -> cleanup */
254 if (FAILED(hr))
255 {
256 if (m_pCred != NULL)
257 {
258 m_pCred->Release();
259 m_pCred = NULL;
260 }
261 }
262 break;
263
264 case CPUS_CHANGE_PASSWORD:
265 case CPUS_CREDUI:
266 case CPUS_PLAP:
267
268 hr = E_NOTIMPL;
269 break;
270
271 default:
272
273 hr = E_INVALIDARG;
274 break;
275 }
276
277 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario returned hr=0x%08x (cpUS=%d, dwFlags=%ld)\n",
278 hr, cpUsageScenario, dwFlags);
279 return hr;
280}
281
282
283/*
284 * SetSerialization takes the kind of buffer that you would normally return to LogonUI for
285 * an authentication attempt. It's the opposite of ICredentialProviderCredential::GetSerialization.
286 * GetSerialization is implement by a credential and serializes that credential. Instead,
287 * SetSerialization takes the serialization and uses it to create a credential.
288 *
289 * SetSerialization is called for two main scenarios. The first scenario is in the credUI case
290 * where it is prepopulating a tile with credentials that the user chose to store in the OS.
291 * The second situation is in a remote logon case where the remote client may wish to
292 * prepopulate a tile with a username, or in some cases, completely populate the tile and
293 * use it to logon without showing any UI.
294 */
295STDMETHODIMP VBoxCredProvProvider::SetSerialization(const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION *pcpCredentialSerialization)
296{
297 NOREF(pcpCredentialSerialization);
298 return E_NOTIMPL;
299}
300
301
302/*
303 * Called by LogonUI to give you a callback. Providers often use the callback if they
304 * some event would cause them to need to change the set of tiles (visible UI elements)
305 * that they enumerated.
306 */
307HRESULT VBoxCredProvProvider::Advise(ICredentialProviderEvents *pcpEvents,
308 UINT_PTR upAdviseContext)
309{
310 VBoxCredProvVerbose(0, "VBoxCredProv::Advise\n");
311
312 if (m_pCredProvEvents != NULL)
313 m_pCredProvEvents->Release();
314
315 m_pCredProvEvents = pcpEvents;
316 AssertPtr(m_pCredProvEvents);
317 m_pCredProvEvents->AddRef();
318
319 /*
320 * Save advice context for later use when binding to
321 * certain ICredentialProviderEvents events.
322 */
323 m_upAdviseContext = upAdviseContext;
324 return S_OK;
325}
326
327
328/* Called by LogonUI when the ICredentialProviderEvents callback is no longer valid. */
329HRESULT VBoxCredProvProvider::UnAdvise(void)
330{
331 VBoxCredProvVerbose(0, "VBoxCredProv::UnAdvise\n");
332 if (m_pCredProvEvents != NULL)
333 {
334 m_pCredProvEvents->Release();
335 m_pCredProvEvents = NULL;
336 }
337
338 return S_OK;
339}
340
341
342/*
343 * Called by LogonUI to determine the number of fields in your tiles. This
344 * does mean that all your tiles must have the same number of fields.
345 * This number must include both visible and invisible fields. If you want a tile
346 * to have different fields from the other tiles you enumerate for a given usage
347 * scenario you must include them all in this count and then hide/show them as desired
348 * using the field descriptors.
349 */
350HRESULT VBoxCredProvProvider::GetFieldDescriptorCount(DWORD *pdwCount)
351{
352 Assert(pdwCount);
353 *pdwCount = VBOXCREDPROV_NUM_FIELDS;
354
355 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorCount: %ld\n", *pdwCount);
356 return S_OK;
357}
358
359
360/* Returns the field descriptor for a particular field. */
361HRESULT VBoxCredProvProvider::GetFieldDescriptorAt(DWORD dwIndex,
362 CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR **ppcpFieldDescriptor)
363{
364 /* Verify dwIndex is a valid field. */
365 HRESULT hr = S_OK;
366 if ( dwIndex < VBOXCREDPROV_NUM_FIELDS
367 && ppcpFieldDescriptor)
368 {
369 PCREDENTIAL_PROVIDER_FIELD_DESCRIPTOR pcpFieldDesc =
370 (PCREDENTIAL_PROVIDER_FIELD_DESCRIPTOR)CoTaskMemAlloc(sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR));
371
372 if (pcpFieldDesc)
373 {
374 const VBOXCREDPROV_FIELD &field = s_VBoxCredProvFields[dwIndex];
375
376 ZeroMemory(pcpFieldDesc, sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR));
377
378 pcpFieldDesc->dwFieldID = field.desc.dwFieldID;
379 pcpFieldDesc->cpft = field.desc.cpft;
380 if (field.desc.pszLabel)
381 hr = SHStrDupW(field.desc.pszLabel, &pcpFieldDesc->pszLabel);
382 }
383 else
384 hr = E_OUTOFMEMORY;
385
386 if (SUCCEEDED(hr))
387 {
388 *ppcpFieldDescriptor = pcpFieldDesc;
389 }
390 else
391 CoTaskMemFree(pcpFieldDesc);
392 }
393 else
394 hr = E_INVALIDARG;
395
396 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorAt: dwIndex=%ld, ppDesc=0x%p, hr=0x%08x\n",
397 dwIndex, ppcpFieldDescriptor, hr);
398 return hr;
399}
400
401
402/*
403 * Sets pdwCount to the number of tiles that we wish to show at this time.
404 * Sets pdwDefault to the index of the tile which should be used as the default.
405 *
406 * The default tile is the tile which will be shown in the zoomed view by default. If
407 * more than one provider specifies a default tile the behavior is the last used cred
408 * prov gets to specify the default tile to be displayed
409 *
410 * If *pbAutoLogonWithDefault is TRUE, LogonUI will immediately call GetSerialization
411 * on the credential you've specified as the default and will submit that credential
412 * for authentication without showing any further UI.
413 */
414HRESULT VBoxCredProvProvider::GetCredentialCount(DWORD *pdwCount,
415 DWORD *pdwDefault,
416 BOOL *pbAutoLogonWithDefault)
417{
418 AssertPtr(pdwCount);
419 AssertPtr(pdwDefault);
420 AssertPtr(pbAutoLogonWithDefault);
421
422 bool fHasCredentials = false;
423
424 /* Do we have credentials? */
425 if (m_pCred)
426 {
427 int rc = m_pCred->RetrieveCredentials();
428 fHasCredentials = rc == VINF_SUCCESS;
429 }
430
431 if (fHasCredentials)
432 {
433 *pdwCount = 1; /* This provider always has the same number of credentials (1). */
434 *pdwDefault = 0; /* The credential we provide is *always* at index 0! */
435 *pbAutoLogonWithDefault = TRUE; /* We always at least try to auto-login (if password is correct). */
436 }
437 else
438 {
439 *pdwCount = 0;
440 *pdwDefault = CREDENTIAL_PROVIDER_NO_DEFAULT;
441 *pbAutoLogonWithDefault = FALSE;
442 }
443
444 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialCount: *pdwCount=%ld, *pdwDefault=%ld, *pbAutoLogonWithDefault=%s\n",
445 *pdwCount, *pdwDefault, *pbAutoLogonWithDefault ? "true" : "false");
446 return S_OK;
447}
448
449
450/*
451 * Returns the credential at the index specified by dwIndex. This function is called by logonUI to enumerate
452 * the tiles.
453 */
454HRESULT VBoxCredProvProvider::GetCredentialAt(DWORD dwIndex,
455 ICredentialProviderCredential **ppCredProvCredential)
456{
457 HRESULT hr;
458
459 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: Index=%ld, ppCredProvCredential=%p\n", dwIndex, ppCredProvCredential);
460
461 if (!m_pCred)
462 {
463 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: No credentials available\n");
464 return E_INVALIDARG;
465 }
466
467 /* Validate parameters (we only have one credential). */
468 if( dwIndex == 0
469 && ppCredProvCredential)
470 {
471 hr = m_pCred->QueryInterface(IID_ICredentialProviderCredential,
472 reinterpret_cast<void**>(ppCredProvCredential));
473 }
474 else
475 {
476 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: More than one credential not supported!\n");
477 hr = E_INVALIDARG;
478 }
479 return hr;
480}
481
482
483/* Do a credential re-enumeration if we got the event to do so. This then invokes
484 * GetCredentialCount() and GetCredentialAt() called by Winlogon. */
485void VBoxCredProvProvider::OnCredentialsProvided(void)
486{
487 VBoxCredProvVerbose(0, "VBoxCredProv::OnCredentialsProvided\n");
488
489 if (m_pCredProvEvents != NULL)
490 m_pCredProvEvents->CredentialsChanged(m_upAdviseContext);
491}
492
493
494/* Creates our provider. This happens *before* CTRL-ALT-DEL was pressed! */
495HRESULT VBoxCredProvProviderCreate(REFIID interfaceID, void **ppvInterface)
496{
497 HRESULT hr;
498
499 VBoxCredProvProvider* pProvider = new VBoxCredProvProvider();
500 if (pProvider)
501 {
502 hr = pProvider->QueryInterface(interfaceID, ppvInterface);
503 pProvider->Release();
504 }
505 else
506 hr = E_OUTOFMEMORY;
507
508 return hr;
509}
510
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette