VirtualBox

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

Last change on this file since 67809 was 67809, checked in by vboxsync, 7 years ago

Additions/VBoxCredProv: Renamed resetField() to setField() and implemented the ability to notify LogonUI + set a new value if needed. Fixed default value handling / resetting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/* $Id: VBoxCredProvProvider.cpp 67809 2017-07-05 18:11:47Z vboxsync $ */
2/** @file
3 * VBoxCredProvProvider - The actual credential provider class.
4 */
5
6/*
7 * Copyright (C) 2012-2016 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <new> /* For bad_alloc. */
23
24#include <iprt/win/windows.h>
25#include <credentialprovider.h>
26
27#include <iprt/err.h>
28#include <VBox/VBoxGuestLib.h>
29
30#include "VBoxCredentialProvider.h"
31#include "VBoxCredProvProvider.h"
32#include "VBoxCredProvCredential.h"
33
34
35
36VBoxCredProvProvider::VBoxCredProvProvider(void) :
37 m_cRefs(1),
38 m_pPoller(NULL),
39 m_pCred(NULL),
40 m_pEvents(NULL),
41 m_fHandleRemoteSessions(false)
42{
43 VBoxCredentialProviderAcquire();
44
45 VBoxCredProvReportStatus(VBoxGuestFacilityStatus_Init);
46}
47
48
49VBoxCredProvProvider::~VBoxCredProvProvider(void)
50{
51 VBoxCredProvVerbose(0, "VBoxCredProv: Destroying\n");
52
53 if (m_pCred)
54 {
55 m_pCred->Release();
56 m_pCred = NULL;
57 }
58
59 if (m_pPoller)
60 {
61 m_pPoller->Shutdown();
62 delete m_pPoller;
63 m_pPoller = NULL;
64 }
65
66 VBoxCredProvReportStatus(VBoxGuestFacilityStatus_Terminated);
67
68 VBoxCredentialProviderRelease();
69}
70
71
72/* IUnknown overrides. */
73ULONG
74VBoxCredProvProvider::AddRef(void)
75{
76 LONG cRefs = InterlockedIncrement(&m_cRefs);
77 VBoxCredProvVerbose(0, "VBoxCredProv: AddRef: Returning refcount=%ld\n",
78 cRefs);
79 return cRefs;
80}
81
82
83ULONG
84VBoxCredProvProvider::Release(void)
85{
86 LONG cRefs = InterlockedDecrement(&m_cRefs);
87 VBoxCredProvVerbose(0, "VBoxCredProv: Release: Returning refcount=%ld\n",
88 cRefs);
89 if (!cRefs)
90 {
91 VBoxCredProvVerbose(0, "VBoxCredProv: Calling destructor\n");
92 delete this;
93 }
94 return cRefs;
95}
96
97
98HRESULT
99VBoxCredProvProvider::QueryInterface(REFIID interfaceID, void **ppvInterface)
100{
101 HRESULT hr = S_OK;
102 if (ppvInterface)
103 {
104 if ( IID_IUnknown == interfaceID
105 || IID_ICredentialProvider == interfaceID)
106 {
107 *ppvInterface = static_cast<IUnknown*>(this);
108 reinterpret_cast<IUnknown*>(*ppvInterface)->AddRef();
109 }
110 else
111 {
112 *ppvInterface = NULL;
113 hr = E_NOINTERFACE;
114 }
115 }
116 else
117 hr = E_INVALIDARG;
118
119 return hr;
120}
121
122
123/**
124 * Loads the global configuration from registry.
125 *
126 * @return DWORD Windows error code.
127 */
128DWORD
129VBoxCredProvProvider::LoadConfiguration(void)
130{
131 HKEY hKey;
132 /** @todo Add some registry wrapper function(s) as soon as we got more values to retrieve. */
133 DWORD dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Oracle\\VirtualBox Guest Additions\\AutoLogon",
134 0L, KEY_QUERY_VALUE, &hKey);
135 if (dwRet == ERROR_SUCCESS)
136 {
137 DWORD dwValue;
138 DWORD dwType = REG_DWORD;
139 DWORD dwSize = sizeof(DWORD);
140
141 dwRet = RegQueryValueEx(hKey, L"HandleRemoteSessions", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
142 if ( dwRet == ERROR_SUCCESS
143 && dwType == REG_DWORD
144 && dwSize == sizeof(DWORD))
145 {
146 m_fHandleRemoteSessions = RT_BOOL(dwValue);
147 }
148
149 dwRet = RegQueryValueEx(hKey, L"LoggingEnabled", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
150 if ( dwRet == ERROR_SUCCESS
151 && dwType == REG_DWORD
152 && dwSize == sizeof(DWORD))
153 {
154 g_dwVerbosity = 1; /* Default logging level. */
155 }
156
157 if (g_dwVerbosity) /* Do we want logging at all? */
158 {
159 dwRet = RegQueryValueEx(hKey, L"LoggingLevel", NULL, &dwType, (LPBYTE)&dwValue, &dwSize);
160 if ( dwRet == ERROR_SUCCESS
161 && dwType == REG_DWORD
162 && dwSize == sizeof(DWORD))
163 {
164 g_dwVerbosity = dwValue;
165 }
166 }
167
168 RegCloseKey(hKey);
169 }
170 /* Do not report back an error here yet. */
171 return ERROR_SUCCESS;
172}
173
174
175/**
176 * Determines whether we should handle the current session or not.
177 *
178 * @return bool true if we should handle this session, false if not.
179 */
180bool
181VBoxCredProvProvider::HandleCurrentSession(void)
182{
183 /* Load global configuration from registry. */
184 int rc = LoadConfiguration();
185 if (RT_FAILURE(rc))
186 VBoxCredProvVerbose(0, "VBoxCredProv: Error loading global configuration, rc=%Rrc\n",
187 rc);
188
189 bool fHandle = false;
190 if (VbglR3AutoLogonIsRemoteSession())
191 {
192 if (m_fHandleRemoteSessions) /* Force remote session handling. */
193 fHandle = true;
194 }
195 else /* No remote session. */
196 fHandle = true;
197
198 VBoxCredProvVerbose(3, "VBoxCredProv: Handling current session=%RTbool\n", fHandle);
199 return fHandle;
200}
201
202
203/**
204 * Tells this provider the current usage scenario.
205 *
206 * @return HRESULT
207 * @param enmUsageScenario Current usage scenario this provider will be
208 * used in.
209 * @param dwFlags Optional flags for the usage scenario.
210 */
211HRESULT
212VBoxCredProvProvider::SetUsageScenario(CREDENTIAL_PROVIDER_USAGE_SCENARIO enmUsageScenario, DWORD dwFlags)
213{
214 HRESULT hr = S_OK;
215 DWORD dwErr;
216
217 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario: enmUsageScenario=%d, dwFlags=%ld\n",
218 enmUsageScenario, dwFlags);
219
220 m_enmUsageScenario = enmUsageScenario;
221
222 switch (m_enmUsageScenario)
223 {
224 case CPUS_LOGON:
225 case CPUS_UNLOCK_WORKSTATION:
226 {
227 VBoxCredProvReportStatus(VBoxGuestFacilityStatus_Active);
228
229 dwErr = LoadConfiguration();
230 if (dwErr != ERROR_SUCCESS)
231 VBoxCredProvVerbose(0, "VBoxCredProv: Error while loading configuration, error=%ld\n", dwErr);
232 /* Do not stop running on a misconfigured system. */
233
234 /*
235 * If we're told to not handle the current session just bail out and let the
236 * user know.
237 */
238 if (!HandleCurrentSession())
239 break;
240
241 if (!m_pPoller)
242 {
243 try
244 {
245 m_pPoller = new VBoxCredProvPoller();
246 AssertPtr(m_pPoller);
247 int rc = m_pPoller->Initialize(this);
248 if (RT_FAILURE(rc))
249 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario: Error initializing poller thread, rc=%Rrc\n", rc);
250 }
251 catch (std::bad_alloc &ex)
252 {
253 NOREF(ex);
254 hr = E_OUTOFMEMORY;
255 }
256 }
257
258 if ( SUCCEEDED(hr)
259 && !m_pCred)
260 {
261 try
262 {
263 m_pCred = new VBoxCredProvCredential();
264 AssertPtr(m_pPoller);
265 hr = m_pCred->Initialize(m_enmUsageScenario);
266 }
267 catch (std::bad_alloc &ex)
268 {
269 NOREF(ex);
270 hr = E_OUTOFMEMORY;
271 }
272 }
273 else
274 {
275 /* All set up already! Nothing to do here right now. */
276 }
277
278 /* If we failed, do some cleanup. */
279 if (FAILED(hr))
280 {
281 if (m_pCred != NULL)
282 {
283 m_pCred->Release();
284 m_pCred = NULL;
285 }
286 }
287 break;
288 }
289
290 case CPUS_CHANGE_PASSWORD: /* Asks us to provide a way to change the password. */
291 case CPUS_CREDUI: /* Displays an own UI. We don't need that. */
292 case CPUS_PLAP: /* See Pre-Logon-Access Provider. Not needed (yet). */
293
294 hr = E_NOTIMPL;
295 break;
296
297 default:
298
299 hr = E_INVALIDARG;
300 break;
301 }
302
303 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario returned hr=0x%08x\n", hr);
304 return hr;
305}
306
307
308/**
309 * Tells this provider how the serialization will be handled. Currently not used.
310 *
311 * @return STDMETHODIMP
312 * @param pcpCredentialSerialization Credentials serialization.
313 */
314STDMETHODIMP
315VBoxCredProvProvider::SetSerialization(const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION *pcpCredentialSerialization)
316{
317 NOREF(pcpCredentialSerialization);
318 return E_NOTIMPL;
319}
320
321
322/**
323 * Initializes the communication with LogonUI through callbacks events which we can later
324 * use to start re-enumeration of credentials.
325 *
326 * @return HRESULT
327 * @param pcpEvents Pointer to event interface.
328 * @param upAdviseContext The current advise context.
329 */
330HRESULT
331VBoxCredProvProvider::Advise(ICredentialProviderEvents *pcpEvents, UINT_PTR upAdviseContext)
332{
333 VBoxCredProvVerbose(0, "VBoxCredProv::Advise, pcpEvents=0x%p, upAdviseContext=%u\n",
334 pcpEvents, upAdviseContext);
335 if (m_pEvents)
336 {
337 m_pEvents->Release();
338 m_pEvents = NULL;
339 }
340
341 m_pEvents = pcpEvents;
342 if (m_pEvents)
343 m_pEvents->AddRef();
344
345 /*
346 * Save advice context for later use when binding to
347 * certain ICredentialProviderEvents events.
348 */
349 m_upAdviseContext = upAdviseContext;
350 return S_OK;
351}
352
353
354/**
355 * Uninitializes the callback events so that they're no longer valid.
356 *
357 * @return HRESULT
358 */
359HRESULT
360VBoxCredProvProvider::UnAdvise(void)
361{
362 VBoxCredProvVerbose(0, "VBoxCredProv::UnAdvise: pEvents=0x%p\n",
363 m_pEvents);
364 if (m_pEvents)
365 {
366 m_pEvents->Release();
367 m_pEvents = NULL;
368 }
369
370 return S_OK;
371}
372
373
374/**
375 * Retrieves the total count of fields we're handling (needed for field enumeration
376 * through LogonUI).
377 *
378 * @return HRESULT
379 * @param pdwCount Receives total count of fields.
380 */
381HRESULT
382VBoxCredProvProvider::GetFieldDescriptorCount(DWORD *pdwCount)
383{
384 if (pdwCount)
385 {
386 *pdwCount = VBOXCREDPROV_NUM_FIELDS;
387 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorCount: %ld\n", *pdwCount);
388 }
389 return S_OK;
390}
391
392
393/**
394 * Retrieves a descriptor of a specified field.
395 *
396 * @return HRESULT
397 * @param dwIndex ID of field to retrieve descriptor for.
398 * @param ppFieldDescriptor Pointer which receives the allocated field
399 * descriptor.
400 */
401HRESULT
402VBoxCredProvProvider::GetFieldDescriptorAt(DWORD dwIndex, CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR **ppFieldDescriptor)
403{
404 HRESULT hr = S_OK;
405 if ( dwIndex < VBOXCREDPROV_NUM_FIELDS
406 && ppFieldDescriptor)
407 {
408 PCREDENTIAL_PROVIDER_FIELD_DESCRIPTOR pcpFieldDesc =
409 (PCREDENTIAL_PROVIDER_FIELD_DESCRIPTOR)CoTaskMemAlloc(sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR));
410
411 if (pcpFieldDesc)
412 {
413 const VBOXCREDPROV_FIELD &field = s_VBoxCredProvDefaultFields[dwIndex];
414
415 RT_BZERO(pcpFieldDesc, sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR));
416
417 pcpFieldDesc->dwFieldID = field.desc.dwFieldID;
418 pcpFieldDesc->cpft = field.desc.cpft;
419 if (field.desc.pszLabel)
420 {
421 hr = SHStrDupW(field.desc.pszLabel, &pcpFieldDesc->pszLabel);
422
423 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorAt: dwIndex=%ld, pszLabel=%ls\n",
424 dwIndex,
425#ifdef DEBUG /* Don't show any (sensitive data) in release mode. */
426 field.desc.pszLabel);
427#else
428 L"XXX");
429#endif
430 }
431 pcpFieldDesc->guidFieldType = field.desc.guidFieldType;
432 }
433 else
434 hr = E_OUTOFMEMORY;
435
436 if (SUCCEEDED(hr))
437 {
438 *ppFieldDescriptor = pcpFieldDesc;
439 }
440 else if (pcpFieldDesc)
441 {
442 if (pcpFieldDesc->pszLabel)
443 {
444 CoTaskMemFree(pcpFieldDesc->pszLabel);
445 pcpFieldDesc->pszLabel = NULL;
446 }
447
448 CoTaskMemFree(pcpFieldDesc);
449 }
450 }
451 else
452 hr = E_INVALIDARG;
453
454 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorAt: dwIndex=%ld, ppDesc=0x%p, hr=0x%08x\n",
455 dwIndex, ppFieldDescriptor, hr);
456 return hr;
457}
458
459
460/**
461 * Retrieves the total number of credentials this provider can offer at the current time and
462 * if a logon attempt should be made.
463 *
464 * @return HRESULT
465 * @param pdwCount Receives number of credentials to serve.
466 * @param pdwDefault Receives the credentials index to try
467 * logging on if there is more than one
468 * credential provided. 0 is default.
469 * @param pfAutoLogonWithDefault Receives a flag indicating whether a
470 * logon attempt using the default
471 * credential should be made or not.
472 */
473HRESULT
474VBoxCredProvProvider::GetCredentialCount(DWORD *pdwCount, DWORD *pdwDefault, BOOL *pfAutoLogonWithDefault)
475{
476 AssertPtr(pdwCount);
477 AssertPtr(pdwDefault);
478 AssertPtr(pfAutoLogonWithDefault);
479
480 bool fHasCredentials = false;
481
482 /* Do we have credentials? */
483 if (m_pCred)
484 {
485 int rc = m_pCred->RetrieveCredentials();
486 fHasCredentials = rc == VINF_SUCCESS;
487 }
488
489 if (fHasCredentials)
490 {
491 *pdwCount = 1; /* This provider always has the same number of credentials (1). */
492 *pdwDefault = 0; /* The credential we provide is *always* at index 0! */
493 *pfAutoLogonWithDefault = TRUE; /* We always at least try to auto-login (if password is correct). */
494 }
495 else
496 {
497 *pdwCount = 0;
498 *pdwDefault = CREDENTIAL_PROVIDER_NO_DEFAULT;
499 *pfAutoLogonWithDefault = FALSE;
500 }
501
502 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialCount: *pdwCount=%ld, *pdwDefault=%ld, *pfAutoLogonWithDefault=%s\n",
503 *pdwCount, *pdwDefault, *pfAutoLogonWithDefault ? "true" : "false");
504 return S_OK;
505}
506
507
508/**
509 * Called by Winlogon to retrieve the interface of our current ICredentialProviderCredential interface.
510 *
511 * @return HRESULT
512 * @param dwIndex Index of credential (in case there is more than one credential at a time) to
513 * retrieve the interface for.
514 * @param ppCredProvCredential Pointer that receives the credential interface.
515 */
516HRESULT
517VBoxCredProvProvider::GetCredentialAt(DWORD dwIndex, ICredentialProviderCredential **ppCredProvCredential)
518{
519 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: Index=%ld, ppCredProvCredential=0x%p\n",
520 dwIndex, ppCredProvCredential);
521 if (!m_pCred)
522 {
523 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: No credentials available\n");
524 return E_INVALIDARG;
525 }
526
527 HRESULT hr;
528 if ( dwIndex == 0
529 && ppCredProvCredential)
530 {
531 hr = m_pCred->QueryInterface(IID_ICredentialProviderCredential,
532 reinterpret_cast<void**>(ppCredProvCredential));
533 }
534 else
535 {
536 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: More than one credential not supported!\n");
537 hr = E_INVALIDARG;
538 }
539 return hr;
540}
541
542
543/**
544 * Triggers a credential re-enumeration -- will be called by our poller thread. This then invokes
545 * GetCredentialCount() and GetCredentialAt() called by Winlogon.
546 */
547void
548VBoxCredProvProvider::OnCredentialsProvided(void)
549{
550 VBoxCredProvVerbose(0, "VBoxCredProv::OnCredentialsProvided\n");
551
552 if (m_pEvents)
553 m_pEvents->CredentialsChanged(m_upAdviseContext);
554}
555
556
557/**
558 * Creates our provider. This happens *before* CTRL-ALT-DEL was pressed!
559 */
560HRESULT
561VBoxCredProvProviderCreate(REFIID interfaceID, void **ppvInterface)
562{
563 HRESULT hr;
564
565 try
566 {
567 VBoxCredProvProvider *pProvider = new VBoxCredProvProvider();
568 AssertPtr(pProvider);
569 hr = pProvider->QueryInterface(interfaceID, ppvInterface);
570 pProvider->Release();
571 }
572 catch (std::bad_alloc &ex)
573 {
574 NOREF(ex);
575 hr = E_OUTOFMEMORY;
576 }
577
578 return hr;
579}
580
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