VirtualBox

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

最後變更 在這個檔案從95890是 95890,由 vboxsync 提交於 3 年 前

Add/Nt/VBoxCredProv: Make it compile in VBOX_WITH_NOCRT_STATIC mode. bugref:10261

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 17.5 KB
 
1/* $Id: VBoxCredProvProvider.cpp 95890 2022-07-28 01:49:20Z vboxsync $ */
2/** @file
3 * VBoxCredProvProvider - The actual credential provider class.
4 */
5
6/*
7 * Copyright (C) 2012-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 <iprt/win/credentialprovider.h>
26
27#include <iprt/errcore.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_pCred(NULL)
39 , m_pPoller(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
216 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario: enmUsageScenario=%d, dwFlags=%ld\n",
217 enmUsageScenario, dwFlags);
218
219 m_enmUsageScenario = enmUsageScenario;
220
221 switch (m_enmUsageScenario)
222 {
223 case CPUS_LOGON:
224 case CPUS_UNLOCK_WORKSTATION:
225 {
226 VBoxCredProvReportStatus(VBoxGuestFacilityStatus_Active);
227
228 DWORD dwErr = LoadConfiguration();
229 if (dwErr != ERROR_SUCCESS)
230 VBoxCredProvVerbose(0, "VBoxCredProv: Error while loading configuration, error=%ld\n", dwErr);
231 /* Do not stop running on a misconfigured system. */
232
233 /*
234 * If we're told to not handle the current session just bail out and let the
235 * user know.
236 */
237 if (!HandleCurrentSession())
238 break;
239
240 hr = S_OK;
241 if (!m_pPoller)
242 {
243#ifdef RT_EXCEPTIONS_ENABLED
244 try { m_pPoller = new VBoxCredProvPoller(); }
245 catch (std::bad_alloc &) { hr = E_OUTOFMEMORY; }
246#else
247 m_pPoller = new VBoxCredProvPoller();
248 AssertStmt(m_pPoller, hr = E_OUTOFMEMORY);
249#endif
250 if (SUCCEEDED(hr))
251 {
252 int rc = m_pPoller->Initialize(this);
253 if (RT_FAILURE(rc))
254 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario: Error initializing poller thread, rc=%Rrc\n", rc);
255/** @todo r=bird: Why is the initialize failure ignored here? */
256 }
257 }
258
259 if ( SUCCEEDED(hr)
260 && !m_pCred)
261 {
262#ifdef RT_EXCEPTIONS_ENABLED
263 try { m_pCred = new VBoxCredProvCredential(); }
264 catch (std::bad_alloc &) { hr = E_OUTOFMEMORY; }
265#else
266 m_pCred = new VBoxCredProvCredential();
267 AssertStmt(m_pCred, hr = E_OUTOFMEMORY);
268#endif
269 if (SUCCEEDED(hr))
270 hr = m_pCred->Initialize(m_enmUsageScenario);
271 }
272 else
273 {
274 /* All set up already! Nothing to do here right now. */
275 }
276
277 /* If we failed, do some cleanup. */
278/** @todo r=bird: Why aren't we cleaning up m_pPoller too? Very confusing given
279 * that m_pCred wasn't necessarily even created above. Always explain the WHY
280 * when doing something that isn't logical like here! */
281 if (FAILED(hr))
282 {
283 if (m_pCred != NULL)
284 {
285 m_pCred->Release();
286 m_pCred = NULL;
287 }
288 }
289 break;
290 }
291
292 case CPUS_CHANGE_PASSWORD: /* Asks us to provide a way to change the password. */
293 case CPUS_CREDUI: /* Displays an own UI. We don't need that. */
294 case CPUS_PLAP: /* See Pre-Logon-Access Provider. Not needed (yet). */
295 hr = E_NOTIMPL;
296 break;
297
298 default:
299
300 hr = E_INVALIDARG;
301 break;
302 }
303
304 VBoxCredProvVerbose(0, "VBoxCredProv::SetUsageScenario returned hr=0x%08x\n", hr);
305 return hr;
306}
307
308
309/**
310 * Tells this provider how the serialization will be handled. Currently not used.
311 *
312 * @return STDMETHODIMP
313 * @param pcpCredentialSerialization Credentials serialization.
314 */
315STDMETHODIMP
316VBoxCredProvProvider::SetSerialization(const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION *pcpCredentialSerialization)
317{
318 NOREF(pcpCredentialSerialization);
319 return E_NOTIMPL;
320}
321
322
323/**
324 * Initializes the communication with LogonUI through callbacks events which we can later
325 * use to start re-enumeration of credentials.
326 *
327 * @return HRESULT
328 * @param pcpEvents Pointer to event interface.
329 * @param upAdviseContext The current advise context.
330 */
331HRESULT
332VBoxCredProvProvider::Advise(ICredentialProviderEvents *pcpEvents, UINT_PTR upAdviseContext)
333{
334 VBoxCredProvVerbose(0, "VBoxCredProv::Advise, pcpEvents=0x%p, upAdviseContext=%u\n",
335 pcpEvents, upAdviseContext);
336 if (m_pEvents)
337 {
338 m_pEvents->Release();
339 m_pEvents = NULL;
340 }
341
342 m_pEvents = pcpEvents;
343 if (m_pEvents)
344 m_pEvents->AddRef();
345
346 /*
347 * Save advice context for later use when binding to
348 * certain ICredentialProviderEvents events.
349 */
350 m_upAdviseContext = upAdviseContext;
351 return S_OK;
352}
353
354
355/**
356 * Uninitializes the callback events so that they're no longer valid.
357 *
358 * @return HRESULT
359 */
360HRESULT
361VBoxCredProvProvider::UnAdvise(void)
362{
363 VBoxCredProvVerbose(0, "VBoxCredProv::UnAdvise: pEvents=0x%p\n",
364 m_pEvents);
365 if (m_pEvents)
366 {
367 m_pEvents->Release();
368 m_pEvents = NULL;
369 }
370
371 return S_OK;
372}
373
374
375/**
376 * Retrieves the total count of fields we're handling (needed for field enumeration
377 * through LogonUI).
378 *
379 * @return HRESULT
380 * @param pdwCount Receives total count of fields.
381 */
382HRESULT
383VBoxCredProvProvider::GetFieldDescriptorCount(DWORD *pdwCount)
384{
385 if (pdwCount)
386 {
387 *pdwCount = VBOXCREDPROV_NUM_FIELDS;
388 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorCount: %ld\n", *pdwCount);
389 }
390 return S_OK;
391}
392
393
394/**
395 * Retrieves a descriptor of a specified field.
396 *
397 * @return HRESULT
398 * @param dwIndex ID of field to retrieve descriptor for.
399 * @param ppFieldDescriptor Pointer which receives the allocated field
400 * descriptor.
401 */
402HRESULT
403VBoxCredProvProvider::GetFieldDescriptorAt(DWORD dwIndex, CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR **ppFieldDescriptor)
404{
405 HRESULT hr = S_OK;
406 if ( dwIndex < VBOXCREDPROV_NUM_FIELDS
407 && ppFieldDescriptor)
408 {
409 PCREDENTIAL_PROVIDER_FIELD_DESCRIPTOR pcpFieldDesc =
410 (PCREDENTIAL_PROVIDER_FIELD_DESCRIPTOR)CoTaskMemAlloc(sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR));
411
412 if (pcpFieldDesc)
413 {
414 const VBOXCREDPROV_FIELD &field = s_VBoxCredProvDefaultFields[dwIndex];
415
416 RT_BZERO(pcpFieldDesc, sizeof(CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR));
417
418 pcpFieldDesc->dwFieldID = field.desc.dwFieldID;
419 pcpFieldDesc->cpft = field.desc.cpft;
420
421 PCRTUTF16 pcwszField = NULL;
422
423 if (dwIndex != VBOXCREDPROV_FIELDID_PASSWORD) /* Don't ever get any password. Never ever, ever. */
424 {
425 if (m_pCred) /* If we have retrieved credentials, get the actual (current) value. */
426 pcwszField = m_pCred->getField(dwIndex);
427 else /* Otherwise get the default value. */
428 pcwszField = field.desc.pszLabel;
429 }
430
431 hr = SHStrDupW(pcwszField ? pcwszField : L"", &pcpFieldDesc->pszLabel);
432
433 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorAt: dwIndex=%ld, pszLabel=%ls, hr=0x%08x\n",
434 dwIndex,
435#ifdef DEBUG /* Don't show any (sensitive data) in release mode. */
436 pcwszField ? pcwszField : L"",
437#else
438 L"XXX",
439#endif
440 hr);
441
442 pcpFieldDesc->guidFieldType = field.desc.guidFieldType;
443 }
444 else
445 hr = E_OUTOFMEMORY;
446
447 if (SUCCEEDED(hr))
448 {
449 *ppFieldDescriptor = pcpFieldDesc;
450 }
451 else if (pcpFieldDesc)
452 {
453 if (pcpFieldDesc->pszLabel)
454 {
455 CoTaskMemFree(pcpFieldDesc->pszLabel);
456 pcpFieldDesc->pszLabel = NULL;
457 }
458
459 CoTaskMemFree(pcpFieldDesc);
460 }
461 }
462 else
463 hr = E_INVALIDARG;
464
465 VBoxCredProvVerbose(0, "VBoxCredProv::GetFieldDescriptorAt: dwIndex=%ld, ppDesc=0x%p, hr=0x%08x\n",
466 dwIndex, ppFieldDescriptor, hr);
467 return hr;
468}
469
470
471/**
472 * Retrieves the total number of credentials this provider can offer at the current time and
473 * if a logon attempt should be made.
474 *
475 * @return HRESULT
476 * @param pdwCount Receives number of credentials to serve.
477 * @param pdwDefault Receives the credentials index to try
478 * logging on if there is more than one
479 * credential provided. 0 is default.
480 * @param pfAutoLogonWithDefault Receives a flag indicating whether a
481 * logon attempt using the default
482 * credential should be made or not.
483 */
484HRESULT
485VBoxCredProvProvider::GetCredentialCount(DWORD *pdwCount, DWORD *pdwDefault, BOOL *pfAutoLogonWithDefault)
486{
487 AssertPtr(pdwCount);
488 AssertPtr(pdwDefault);
489 AssertPtr(pfAutoLogonWithDefault);
490
491 bool fHasCredentials = false;
492
493 /* Do we have credentials? */
494 if (m_pCred)
495 {
496 int rc = m_pCred->RetrieveCredentials();
497 fHasCredentials = rc == VINF_SUCCESS;
498 }
499
500 if (fHasCredentials)
501 {
502 *pdwCount = 1; /* This provider always has the same number of credentials (1). */
503 *pdwDefault = 0; /* The credential we provide is *always* at index 0! */
504 *pfAutoLogonWithDefault = TRUE; /* We always at least try to auto-login (if password is correct). */
505 }
506 else
507 {
508 *pdwCount = 0;
509 *pdwDefault = CREDENTIAL_PROVIDER_NO_DEFAULT;
510 *pfAutoLogonWithDefault = FALSE;
511 }
512
513 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialCount: *pdwCount=%ld, *pdwDefault=%ld, *pfAutoLogonWithDefault=%s\n",
514 *pdwCount, *pdwDefault, *pfAutoLogonWithDefault ? "true" : "false");
515 return S_OK;
516}
517
518
519/**
520 * Called by Winlogon to retrieve the interface of our current ICredentialProviderCredential interface.
521 *
522 * @return HRESULT
523 * @param dwIndex Index of credential (in case there is more than one credential at a time) to
524 * retrieve the interface for.
525 * @param ppCredProvCredential Pointer that receives the credential interface.
526 */
527HRESULT
528VBoxCredProvProvider::GetCredentialAt(DWORD dwIndex, ICredentialProviderCredential **ppCredProvCredential)
529{
530 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: Index=%ld, ppCredProvCredential=0x%p\n",
531 dwIndex, ppCredProvCredential);
532 if (!m_pCred)
533 {
534 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: No credentials available\n");
535 return E_INVALIDARG;
536 }
537
538 HRESULT hr;
539 if ( dwIndex == 0
540 && ppCredProvCredential)
541 {
542 hr = m_pCred->QueryInterface(IID_ICredentialProviderCredential,
543 reinterpret_cast<void**>(ppCredProvCredential));
544 }
545 else
546 {
547 VBoxCredProvVerbose(0, "VBoxCredProv::GetCredentialAt: More than one credential not supported!\n");
548 hr = E_INVALIDARG;
549 }
550 return hr;
551}
552
553
554/**
555 * Triggers a credential re-enumeration -- will be called by our poller thread. This then invokes
556 * GetCredentialCount() and GetCredentialAt() called by Winlogon.
557 */
558void
559VBoxCredProvProvider::OnCredentialsProvided(void)
560{
561 VBoxCredProvVerbose(0, "VBoxCredProv::OnCredentialsProvided\n");
562
563 if (m_pEvents)
564 m_pEvents->CredentialsChanged(m_upAdviseContext);
565}
566
567
568/**
569 * Creates our provider. This happens *before* CTRL-ALT-DEL was pressed!
570 */
571HRESULT
572VBoxCredProvProviderCreate(REFIID interfaceID, void **ppvInterface)
573{
574 VBoxCredProvProvider *pProvider;
575#ifdef RT_EXCEPTIONS_ENABLED
576 try { pProvider = new VBoxCredProvProvider(); }
577 catch (std::bad_alloc &) { AssertFailedReturn(E_OUTOFMEMORY); }
578#else
579 pProvider = new VBoxCredProvProvider();
580 AssertReturn(pProvider, E_OUTOFMEMORY);
581#endif
582
583 HRESULT hr = pProvider->QueryInterface(interfaceID, ppvInterface);
584 pProvider->Release();
585
586 return hr;
587}
588
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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