-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSampleCredential.cpp
580 lines (495 loc) · 16.1 KB
/
CSampleCredential.cpp
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
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
#ifndef WIN32_NO_STATUS
#include <ntstatus.h>
#define WIN32_NO_STATUS
#endif
#include <unknwn.h>
#include "CSampleCredential.h"
#include "CWrappedCredentialEvents.h"
#include "guid.h"
// CSampleCredential ////////////////////////////////////////////////////////
// NOTE: Please read the readme.txt file to understand when it's appropriate to
// wrap an another credential provider and when it's not. If you have questions
// about whether your scenario is an appropriate use of wrapping another credprov,
// please contact [email protected]
CSampleCredential::CSampleCredential():
_cRef(1)
{
DllAddRef();
ZeroMemory(_rgCredProvFieldDescriptors, sizeof(_rgCredProvFieldDescriptors));
ZeroMemory(_rgFieldStatePairs, sizeof(_rgFieldStatePairs));
ZeroMemory(_rgFieldStrings, sizeof(_rgFieldStrings));
_pWrappedCredential = NULL;
_pWrappedCredentialEvents = NULL;
_pCredProvCredentialEvents = NULL;
_dwWrappedDescriptorCount = 0;
_dwDatabaseIndex = 0;
}
CSampleCredential::~CSampleCredential()
{
for (int i = 0; i < ARRAYSIZE(_rgFieldStrings); i++)
{
CoTaskMemFree(_rgFieldStrings[i]);
CoTaskMemFree(_rgCredProvFieldDescriptors[i].pszLabel);
}
_CleanupEvents();
if (_pWrappedCredential)
{
_pWrappedCredential->Release();
}
DllRelease();
}
// Initializes one credential with the field information passed in. We also keep track
// of our wrapped credential and how many fields it has.
HRESULT CSampleCredential::Initialize(
__in const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd,
__in const FIELD_STATE_PAIR* rgfsp,
__in ICredentialProviderCredential *pWrappedCredential,
__in DWORD dwWrappedDescriptorCount
)
{
HRESULT hr = S_OK;
// Grab the credential we're wrapping for future reference.
if (_pWrappedCredential != NULL)
{
_pWrappedCredential->Release();
}
_pWrappedCredential = pWrappedCredential;
_pWrappedCredential->AddRef();
// We also need to remember how many fields the inner credential has.
_dwWrappedDescriptorCount = dwWrappedDescriptorCount;
// Copy the field descriptors for each field. This is useful if you want to vary the field
// descriptors based on what Usage scenario the credential was created for.
for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++)
{
_rgFieldStatePairs[i] = rgfsp[i];
hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]);
}
// Initialize the String value of all of our fields.
if (SUCCEEDED(hr))
{
hr = SHStrDupW(L"Reboot System", &_rgFieldStrings[SFI_REBOOT_LINK]);
}
return hr;
}
// LogonUI calls this in order to give us a callback in case we need to notify it of
// anything. We'll also provide it to the wrapped credential.
HRESULT CSampleCredential::Advise(
__in ICredentialProviderCredentialEvents* pcpce
)
{
HRESULT hr = S_OK;
_CleanupEvents();
// We keep a strong reference on the real ICredentialProviderCredentialEvents
// to ensure that the weak reference held by the CWrappedCredentialEvents is valid.
_pCredProvCredentialEvents = pcpce;
_pCredProvCredentialEvents->AddRef();
_pWrappedCredentialEvents = new CWrappedCredentialEvents();
if (_pWrappedCredentialEvents != NULL)
{
_pWrappedCredentialEvents->Initialize(this, pcpce);
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->Advise(_pWrappedCredentialEvents);
}
}
else
{
hr = E_OUTOFMEMORY;
}
return hr;
}
// LogonUI calls this to tell us to release the callback.
// We'll also provide it to the wrapped credential.
HRESULT CSampleCredential::UnAdvise()
{
HRESULT hr = S_OK;
if (_pWrappedCredential != NULL)
{
_pWrappedCredential->UnAdvise();
}
_CleanupEvents();
return hr;
}
// LogonUI calls this function when our tile is selected (zoomed)
// If you simply want fields to show/hide based on the selected state,
// there's no need to do anything here - you can set that up in the
// field definitions. In fact, we're just going to hand it off to the
// wrapped credential in case it wants to do something.
HRESULT CSampleCredential::SetSelected(__out BOOL* pbAutoLogon)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->SetSelected(pbAutoLogon);
}
return hr;
}
// Similarly to SetSelected, LogonUI calls this when your tile was selected
// and now no longer is. We'll let the wrapped credential do anything it needs.
HRESULT CSampleCredential::SetDeselected()
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->SetDeselected();
}
return hr;
}
// Get info for a particular field of a tile. Called by logonUI to get information to
// display the tile. We'll check to see if it's for us or the wrapped credential, and then
// handle or route it as appropriate.
HRESULT CSampleCredential::GetFieldState(
__in DWORD dwFieldID,
__out CREDENTIAL_PROVIDER_FIELD_STATE* pcpfs,
__out CREDENTIAL_PROVIDER_FIELD_INTERACTIVE_STATE* pcpfis
)
{
HRESULT hr = E_UNEXPECTED;
// Make sure we have a wrapped credential.
if (_pWrappedCredential != NULL)
{
// Validate parameters.
if ((pcpfs != NULL) && (pcpfis != NULL))
{
// If the field is in the wrapped credential, hand it off.
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->GetFieldState(dwFieldID, pcpfs, pcpfis);
}
// Otherwise, we need to see if it's one of ours.
else
{
FIELD_STATE_PAIR *pfsp = _LookupLocalFieldStatePair(dwFieldID);
// If the field ID is valid, give it info it needs.
if (pfsp != NULL)
{
*pcpfs = pfsp->cpfs;
*pcpfis = pfsp->cpfis;
hr = S_OK;
}
else
{
hr = E_INVALIDARG;
}
}
}
else
{
hr = E_INVALIDARG;
}
}
return hr;
}
// Sets ppwsz to the string value of the field at the index dwFieldID. We'll check to see if
// it's for us or the wrapped credential, and then handle or route it as appropriate.
HRESULT CSampleCredential::GetStringValue(
__in DWORD dwFieldID,
__deref_out PWSTR* ppwsz
)
{
HRESULT hr = E_UNEXPECTED;
// Make sure we have a wrapped credential.
if (_pWrappedCredential != NULL)
{
// If this field belongs to the wrapped credential, hand it off.
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->GetStringValue(dwFieldID, ppwsz);
}
// Otherwise determine if we need to handle it.
else
{
FIELD_STATE_PAIR *pfsp = _LookupLocalFieldStatePair(dwFieldID);
if (pfsp != NULL)
{
hr = SHStrDupW(_rgFieldStrings[SFI_REBOOT_LINK], ppwsz);
}
else
{
hr = E_INVALIDARG;
}
}
}
return hr;
}
// Returns the number of items to be included in the combobox (pcItems), as well as the
// currently selected item (pdwSelectedItem). We'll check to see if it's for us or the
// wrapped credential, and then handle or route it as appropriate.
HRESULT CSampleCredential::GetComboBoxValueCount(
__in DWORD dwFieldID,
__out DWORD* pcItems,
__out_range(<,*pcItems) DWORD* pdwSelectedItem
)
{
HRESULT hr = E_UNEXPECTED;
// Make sure we have a wrapped credential.
if (_pWrappedCredential != NULL)
{
// If this field belongs to the wrapped credential, hand it off.
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->GetComboBoxValueCount(dwFieldID, pcItems, pdwSelectedItem);
}
}
return hr;
}
// Called iteratively to fill the combobox with the string (ppwszItem) at index dwItem.
// We'll check to see if it's for us or the wrapped credential, and then handle or route
// it as appropriate.
HRESULT CSampleCredential::GetComboBoxValueAt(
__in DWORD dwFieldID,
__in DWORD dwItem,
__deref_out PWSTR* ppwszItem
)
{
HRESULT hr = E_UNEXPECTED;
// Make sure we have a wrapped credential.
if (_pWrappedCredential != NULL)
{
// If this field belongs to the wrapped credential, hand it off.
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->GetComboBoxValueAt(dwFieldID, dwItem, ppwszItem);
}
}
return hr;
}
// Called when the user changes the selected item in the combobox. We'll check to see if
// it's for us or the wrapped credential, and then handle or route it as appropriate.
HRESULT CSampleCredential::SetComboBoxSelectedValue(
__in DWORD dwFieldID,
__in DWORD dwSelectedItem
)
{
HRESULT hr = E_UNEXPECTED;
// Make sure we have a wrapped credential.
if (_pWrappedCredential != NULL)
{
// If this field belongs to the wrapped credential, hand it off.
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->SetComboBoxSelectedValue(dwFieldID, dwSelectedItem);
}
}
return hr;
}
//-------------
// The following methods are for logonUI to get the values of various UI elements and
// then communicate to the credential about what the user did in that field. Even though
// we don't offer these field types ourselves, we need to pass along the request to the
// wrapped credential.
HRESULT CSampleCredential::GetBitmapValue(
__in DWORD dwFieldID,
__out HBITMAP* phbmp
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->GetBitmapValue(dwFieldID, phbmp);
}
return hr;
}
HRESULT CSampleCredential::GetSubmitButtonValue(
__in DWORD dwFieldID,
__out DWORD* pdwAdjacentTo
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->GetSubmitButtonValue(dwFieldID, pdwAdjacentTo);
}
return hr;
}
HRESULT CSampleCredential::SetStringValue(
__in DWORD dwFieldID,
__in PCWSTR pwz
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
// If this field belongs to the wrapped credential, hand it off.
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->SetStringValue(dwFieldID, pwz);
}
// Otherwise determine if we need to handle it.
else
{
FIELD_STATE_PAIR *pfsp = _LookupLocalFieldStatePair(dwFieldID);
if ((pfsp != NULL))
{
PWSTR* ppwszStored = &_rgFieldStrings[dwFieldID];
CoTaskMemFree(*ppwszStored);
hr = SHStrDupW(pwz, ppwszStored);
}
else
{
hr = E_INVALIDARG;
}
}
}
return hr;
}
HRESULT CSampleCredential::GetCheckboxValue(
__in DWORD dwFieldID,
__out BOOL* pbChecked,
__deref_out PWSTR* ppwszLabel
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->GetCheckboxValue(dwFieldID, pbChecked, ppwszLabel);
}
}
return hr;
}
HRESULT CSampleCredential::SetCheckboxValue(
__in DWORD dwFieldID,
__in BOOL bChecked
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->SetCheckboxValue(dwFieldID, bChecked);
}
return hr;
}
HRESULT CSampleCredential::CommandLinkClicked(__in DWORD dwFieldID)
{
HRESULT hr = E_UNEXPECTED;
// Make sure we have a wrapped credential.
if (_pWrappedCredential != NULL)
{
// If this field belongs to the wrapped credential, hand it off.
if (_IsFieldInWrappedCredential(dwFieldID))
{
hr = _pWrappedCredential->CommandLinkClicked(dwFieldID);
}
// Otherwise determine if we need to handle it.
else
{
STARTUPINFO si;
memset(&si, 0, sizeof (STARTUPINFOW));
si.cb = sizeof (STARTUPINFOW);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = FALSE;
PROCESS_INFORMATION pi;
memset(&pi, 0, sizeof (PROCESS_INFORMATION));
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// start the program up
CreateProcess( L"C:\\Windows\\System32\\shutdown.exe", // the path
L"shutdown.exe /r /t 0 /f", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ); // Pointer to PROCESS_INFORMATION structure
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
if (dwFieldID < ARRAYSIZE(_rgCredProvFieldDescriptors) &&
(CPFT_COMMAND_LINK == _rgCredProvFieldDescriptors[dwFieldID].cpft))
{
hr = S_OK;
}
else
{
hr = E_INVALIDARG;
}
}
}
return hr;
}
//------ end of methods for controls we don't have ourselves ----//
//
// Collect the username and password into a serialized credential for the correct usage scenario
// (logon/unlock is what's demonstrated in this sample). LogonUI then passes these credentials
// back to the system to log on.
//
HRESULT CSampleCredential::GetSerialization(
__out CREDENTIAL_PROVIDER_GET_SERIALIZATION_RESPONSE* pcpgsr,
__out CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION* pcpcs,
__deref_out_opt PWSTR* ppwszOptionalStatusText,
__out CREDENTIAL_PROVIDER_STATUS_ICON* pcpsiOptionalStatusIcon
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->GetSerialization(pcpgsr, pcpcs, ppwszOptionalStatusText, pcpsiOptionalStatusIcon);
}
return hr;
}
// ReportResult is completely optional. However, we will hand it off to the wrapped
// credential in case they want to handle it.
HRESULT CSampleCredential::ReportResult(
__in NTSTATUS ntsStatus,
__in NTSTATUS ntsSubstatus,
__deref_out_opt PWSTR* ppwszOptionalStatusText,
__out CREDENTIAL_PROVIDER_STATUS_ICON* pcpsiOptionalStatusIcon
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedCredential != NULL)
{
hr = _pWrappedCredential->ReportResult(ntsStatus, ntsSubstatus, ppwszOptionalStatusText, pcpsiOptionalStatusIcon);
}
return hr;
}
BOOL CSampleCredential::_IsFieldInWrappedCredential(
__in DWORD dwFieldID
)
{
return (dwFieldID < _dwWrappedDescriptorCount);
}
FIELD_STATE_PAIR *CSampleCredential::_LookupLocalFieldStatePair(
__in DWORD dwFieldID
)
{
// Offset into the ID to account for the wrapped fields.
dwFieldID -= _dwWrappedDescriptorCount;
// If the index if valid, give it the info it wants.
if (dwFieldID < SFI_NUM_FIELDS)
{
return &(_rgFieldStatePairs[dwFieldID]);
}
return NULL;
}
void CSampleCredential::_CleanupEvents()
{
// Call Uninitialize before releasing our reference on the real
// ICredentialProviderCredentialEvents to avoid having an
// invalid reference.
if (_pWrappedCredentialEvents != NULL)
{
_pWrappedCredentialEvents->Uninitialize();
_pWrappedCredentialEvents->Release();
_pWrappedCredentialEvents = NULL;
}
if (_pCredProvCredentialEvents != NULL)
{
_pCredProvCredentialEvents->Release();
_pCredProvCredentialEvents = NULL;
}
}