Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.44 KB | None | 0 0
  1. /* * * * * * * * * * * * * * * * * * * * *
  2. **
  3. ** Copyright 2012 Dominik Pretzsch
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. **
  17. ** * * * * * * * * * * * * * * * * * * */
  18.  
  19. #include <credentialprovider.h>
  20. #include "CProvider.h"
  21.  
  22. // CProvider ////////////////////////////////////////////////////////
  23.  
  24. CProvider::CProvider() :
  25. _cRef(1),
  26. _pkiulSetSerialization(NULL),
  27. _pccCredential(NULL),
  28. //_dwNumCreds(0),
  29. _bAutoSubmitSetSerializationCred(false),
  30. _dwSetSerializationCred(CREDENTIAL_PROVIDER_NO_DEFAULT)
  31. {
  32. DllAddRef();
  33.  
  34. //ZeroMemory(_rgpCredentials, sizeof(_rgpCredentials));
  35.  
  36. ////
  37.  
  38. Data::Provider::Init();
  39. //Data::Provider::Get()->pProvider = this;
  40. }
  41.  
  42. CProvider::~CProvider()
  43. {
  44. /*
  45. for (size_t i = 0; i < _dwNumCreds; i++)
  46. {
  47. if (_rgpCredentials[i] != NULL)
  48. {
  49. _rgpCredentials[i]->Release();
  50. }
  51. }
  52. */
  53.  
  54. /*
  55. if (_rgpCredentials[0] != NULL)
  56. {
  57. _rgpCredentials[0]->Release();
  58. }
  59. */
  60.  
  61. if (_pccCredential != NULL)
  62. {
  63. _pccCredential->Release();
  64. }
  65.  
  66. Data::Provider::Deinit();
  67.  
  68. DllRelease();
  69. }
  70.  
  71. void CProvider::_CleanupSetSerialization()
  72. {
  73. DebugPrintLn(__FUNCTION__);
  74.  
  75. if (_pkiulSetSerialization)
  76. {
  77. KERB_INTERACTIVE_LOGON* pkil = &_pkiulSetSerialization->Logon;
  78. SecureZeroMemory(_pkiulSetSerialization,
  79. sizeof(*_pkiulSetSerialization) +
  80. pkil->LogonDomainName.MaximumLength +
  81. pkil->UserName.MaximumLength +
  82. pkil->Password.MaximumLength);
  83. HeapFree(GetProcessHeap(), 0, _pkiulSetSerialization);
  84. }
  85. }
  86.  
  87. /*
  88. void CProvider::Redraw()
  89. {
  90. DebugPrintLn(__FUNCTION__);
  91.  
  92. if (_pcpe != NULL)
  93. {
  94. _pcpe->CredentialsChanged(_upAdviseContext);
  95. }
  96. }
  97. */
  98.  
  99. // SetUsageScenario is the provider's cue that it's going to be asked for tiles
  100. // in a subsequent call.
  101. //
  102. // This sample only handles the logon and unlock scenarios as those are the most common.
  103. HRESULT CProvider::SetUsageScenario(
  104. __in CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
  105. __in DWORD dwFlags
  106. )
  107. {
  108. DebugPrintLn(__FUNCTION__);
  109.  
  110. HRESULT hr = E_INVALIDARG;
  111.  
  112. Data::Provider::Get()->credPackFlags = dwFlags;
  113. Data::Provider::Get()->usage_scenario = cpus;
  114. //_cpus = cpus;
  115.  
  116. //static bool s_bCredsEnumeratedLogon = false;
  117. //static bool s_bCredsEnumeratedUnlock = false;
  118.  
  119. // Decide which scenarios to support here. Returning E_NOTIMPL simply tells the caller
  120. // that we're not designed for that scenario.
  121. switch (General::Fields::GetCurrentUsageScenario())
  122. {
  123. case CPUS_LOGON:
  124. case CPUS_CREDUI:
  125. /*
  126. if (!s_bCredsEnumeratedLogon)
  127. {
  128. PWSTR szDomainName = NULL;
  129. NETSETUP_JOIN_STATUS join_status;
  130.  
  131. if (!NetGetJoinInformation(
  132. NULL,
  133. &szDomainName,
  134. &join_status) == NERR_Success || join_status == NetSetupUnjoined || join_status == NetSetupUnknownStatus) szDomainName = NULL;
  135.  
  136. hr = this->_EnumerateCredentials(NULL, szDomainName);
  137.  
  138. s_bCredsEnumeratedLogon = true;
  139. s_bCredsEnumeratedUnlock = false;
  140. }
  141. else
  142. hr = S_OK;
  143. break;
  144. */
  145.  
  146. case CPUS_CHANGE_PASSWORD:
  147. case CPUS_UNLOCK_WORKSTATION:
  148. //case CPUS_CREDUI:
  149. /*
  150. if (!s_bCredsEnumeratedUnlock)
  151. {
  152. PWSTR szUserName = NULL;
  153. PWSTR szDomainName = NULL;
  154. DWORD dwLen;
  155.  
  156. if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
  157. WTS_CURRENT_SESSION,
  158. WTSUserName,
  159. &szUserName,
  160. &dwLen)) szUserName = NULL;
  161.  
  162. if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
  163. WTS_CURRENT_SESSION,
  164. WTSDomainName,
  165. &szDomainName,
  166. &dwLen)) szDomainName = NULL;
  167.  
  168. hr = this->_EnumerateCredentials(szUserName, szDomainName);
  169.  
  170. s_bCredsEnumeratedUnlock = true;
  171. s_bCredsEnumeratedLogon = false;
  172. }
  173. else
  174. hr = S_OK;
  175. break;
  176. */
  177.  
  178. hr = S_OK;
  179. break;
  180.  
  181. //case CPUS_CREDUI: // Though, we are prepared
  182. case CPUS_PLAP:
  183. case CPUS_INVALID:
  184. hr = E_NOTIMPL;
  185. break;
  186.  
  187. default:
  188. hr = E_INVALIDARG;
  189. }
  190.  
  191. DebugPrintLn(General::Fields::GetCurrentUsageScenario());
  192. DebugPrintLn(hr);
  193.  
  194. return hr;
  195. }
  196.  
  197. void CProvider::PrefillCredentials(const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION* pcps){
  198.  
  199. if (pcps && pcps->cbSerialization > 0 && pcps->rgbSerialization){
  200.  
  201. //fill the username and password in
  202. KERB_INTERACTIVE_UNLOCK_LOGON* pkil = (KERB_INTERACTIVE_UNLOCK_LOGON*)pcps->rgbSerialization;
  203.  
  204. if (pkil){
  205.  
  206. //check to see if the username is ok
  207. if (pkil->Logon.UserName.Length && pkil->Logon.UserName.Buffer){
  208.  
  209. //copy the username
  210. this->Prefilled_UserName = wstring(pkil->Logon.UserName.Buffer, pkil->Logon.UserName.Length / sizeof(WCHAR));
  211. Helper::Debug::WriteLogFile(pkil->Logon.UserName.Buffer);
  212. }
  213.  
  214. if (pkil->Logon.Password.Length && pkil->Logon.Password.Buffer){
  215.  
  216. this->Prefilled_Password = wstring(pkil->Logon.Password.Buffer, pkil->Logon.Password.Length / sizeof(WCHAR));
  217. }
  218.  
  219. }
  220. }
  221.  
  222. }
  223.  
  224. // SetSerialization takes the kind of buffer that you would normally return to LogonUI for
  225. // an authentication attempt. It's the opposite of ICredentialProviderCredential::GetSerialization.
  226. // GetSerialization is implement by a credential and serializes that credential. Instead,
  227. // SetSerialization takes the serialization and uses it to create a credential.
  228. //
  229. // SetSerialization is called for two main scenarios. The first scenario is in the credui case
  230. // where it is prepopulating a tile with credentials that the user chose to store in the OS.
  231. // The second situation is in a remote logon case where the remote client may wish to
  232. // prepopulate a tile with a username, or in some cases, completely populate the tile and
  233. // use it to logon without showing any UI.
  234. //
  235. // Since this sample doesn't support CPUS_CREDUI, we have not implemented the credui specific
  236. // pieces of this function. For information on that, please see the credUI sample.
  237. HRESULT CProvider::SetSerialization(
  238. __in const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION* pcpcs
  239. )
  240. {
  241. DebugPrintLn(__FUNCTION__);
  242.  
  243. HRESULT result = E_NOTIMPL;
  244.  
  245. // retrieve authPackage
  246. ULONG authPackage = NULL;
  247. result = RetrieveNegotiateAuthPackage(&authPackage);
  248.  
  249. //prefill credentials
  250. this->PrefillCredentials(pcpcs);
  251.  
  252. if (!SUCCEEDED(result))
  253. {
  254. DebugPrintLn("Failed to retrieve authPackage");
  255. return result;
  256. }
  257.  
  258. if (General::Fields::GetCurrentUsageScenario() == CPUS_CREDUI)
  259. {
  260. DebugPrintLn("CPUS_CREDUI");
  261.  
  262. if (((Data::Provider::Get()->credPackFlags & CREDUIWIN_IN_CRED_ONLY) || (Data::Provider::Get()->credPackFlags & CREDUIWIN_AUTHPACKAGE_ONLY)) && authPackage != pcpcs->ulAuthenticationPackage)
  263. {
  264. DebugPrintLn("authPackage invalid");
  265. return E_INVALIDARG;
  266. }
  267.  
  268. if (Data::Provider::Get()->credPackFlags & CREDUIWIN_AUTHPACKAGE_ONLY)
  269. {
  270. DebugPrintLn("CPUS_CREDUI but not CREDUIWIN_AUTHPACKAGE_ONLY");
  271. result = S_FALSE;
  272. }
  273. }
  274.  
  275. if (authPackage == pcpcs->ulAuthenticationPackage && pcpcs->cbSerialization > 0 && pcpcs->rgbSerialization)
  276. {
  277. KERB_INTERACTIVE_UNLOCK_LOGON* pkil = (KERB_INTERACTIVE_UNLOCK_LOGON*)pcpcs->rgbSerialization;
  278. if (pkil->Logon.MessageType == KerbInteractiveLogon)
  279. {
  280. if (pkil->Logon.UserName.Length && pkil->Logon.UserName.Buffer)
  281. {
  282. BYTE * nativeSerialization = NULL;
  283. DWORD nativeSerializationSize = 0;
  284.  
  285. if (Data::Provider::Get()->credPackFlags == CPUS_CREDUI && (Data::Provider::Get()->credPackFlags & CREDUIWIN_PACK_32_WOW))
  286. {
  287. if (!SUCCEEDED(KerbInteractiveUnlockLogonRepackNative(pcpcs->rgbSerialization, pcpcs->cbSerialization,
  288. &nativeSerialization, &nativeSerializationSize)))
  289. {
  290. return result;
  291. }
  292. }
  293. else
  294. {
  295. nativeSerialization = (BYTE*)LocalAlloc(LMEM_ZEROINIT, pcpcs->cbSerialization);
  296. nativeSerializationSize = pcpcs->cbSerialization;
  297.  
  298. if (!nativeSerialization)
  299. return E_OUTOFMEMORY;
  300.  
  301. CopyMemory(nativeSerialization, pcpcs->rgbSerialization, pcpcs->cbSerialization);
  302. }
  303.  
  304. KerbInteractiveUnlockLogonUnpackInPlace((KERB_INTERACTIVE_UNLOCK_LOGON *)nativeSerialization, nativeSerializationSize);
  305.  
  306. if (_pkiulSetSerialization)
  307. LocalFree(_pkiulSetSerialization);
  308.  
  309. _pkiulSetSerialization = (KERB_INTERACTIVE_UNLOCK_LOGON *)nativeSerialization;
  310.  
  311. result = S_OK;
  312. }
  313. }
  314. }
  315.  
  316. DebugPrintLn(result);
  317.  
  318. return result;
  319. }
  320.  
  321. // Called by LogonUI to give you a callback. Providers often use the callback if they
  322. // some event would cause them to need to change the set of tiles that they enumerated
  323. HRESULT CProvider::Advise(
  324. __in ICredentialProviderEvents* pcpe,
  325. __in UINT_PTR upAdviseContext
  326. )
  327. {
  328. DebugPrintLn(__FUNCTION__);
  329.  
  330. //UNREFERENCED_PARAMETER(pcpe);
  331. //UNREFERENCED_PARAMETER(upAdviseContext);
  332.  
  333. if (Data::Provider::Get()->_pcpe != NULL)
  334. {
  335. Data::Provider::Get()->_pcpe->Release();
  336. }
  337.  
  338. Data::Provider::Get()->_pcpe = pcpe;
  339. Data::Provider::Get()->_pcpe->AddRef();
  340.  
  341. Data::Provider::Get()->_upAdviseContext = upAdviseContext;
  342.  
  343. return S_OK;
  344. }
  345.  
  346. // Called by LogonUI when the ICredentialProviderEvents callback is no longer valid.
  347. HRESULT CProvider::UnAdvise()
  348. {
  349. DebugPrintLn(__FUNCTION__);
  350.  
  351. if (Data::Provider::Get()->_pcpe != NULL)
  352. {
  353. Data::Provider::Get()->_pcpe->Release();
  354. }
  355.  
  356. Data::Provider::Get()->_pcpe = NULL;
  357. Data::Provider::Get()->_upAdviseContext = NULL;
  358.  
  359. return S_OK;
  360. }
  361.  
  362. // Called by LogonUI to determine the number of fields in your tiles. This
  363. // does mean that all your tiles must have the same number of fields.
  364. // This number must include both visible and invisible fields. If you want a tile
  365. // to have different fields from the other tiles you enumerate for a given usage
  366. // scenario you must include them all in this count and then hide/show them as desired
  367. // using the field descriptors.
  368. HRESULT CProvider::GetFieldDescriptorCount(
  369. __out DWORD* pdwCount
  370. )
  371. {
  372. DebugPrintLn(__FUNCTION__);
  373.  
  374. *pdwCount = General::Fields::GetCurrentNumFields();
  375.  
  376. return S_OK;
  377. }
  378.  
  379. // Gets the field descriptor for a particular field
  380. HRESULT CProvider::GetFieldDescriptorAt(
  381. __in DWORD dwIndex,
  382. __deref_out CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR** ppcpfd
  383. )
  384. {
  385. DebugPrintLn(__FUNCTION__);
  386.  
  387. HRESULT hr;
  388.  
  389. // Verify dwIndex is a valid field.
  390. if ((dwIndex < General::Fields::GetCurrentNumFields()) && ppcpfd)
  391. {
  392. hr = FieldDescriptorCoAllocCopy(s_rgCredProvFieldDescriptorsFor[General::Fields::GetCurrentUsageScenario()][dwIndex], ppcpfd);
  393. }
  394. else
  395. {
  396. hr = E_INVALIDARG;
  397. }
  398.  
  399. return hr;
  400. }
  401.  
  402. // Sets pdwCount to the number of tiles that we wish to show at this time.
  403. // Sets pdwDefault to the index of the tile which should be used as the default.
  404. //
  405. // The default tile is the tile which will be shown in the zoomed view by default. If
  406. // more than one provider specifies a default tile the behavior is the last used cred
  407. // prov gets to specify the default tile to be displayed
  408. //
  409. // If *pbAutoLogonWithDefault is TRUE, LogonUI will immediately call GetSerialization
  410. // on the credential you've specified as the default and will submit that credential
  411. // for authentication without showing any further UI.
  412. HRESULT CProvider::GetCredentialCount(
  413. __out DWORD* pdwCount,
  414. __out_range(<, *pdwCount) DWORD* pdwDefault,
  415. __out BOOL* pbAutoLogonWithDefault
  416. )
  417. {
  418. DebugPrintLn(__FUNCTION__);
  419.  
  420. HRESULT hr = S_OK;
  421.  
  422. *pdwCount = 1; //_dwNumCreds;
  423. *pdwDefault = 0; //CREDENTIAL_PROVIDER_NO_DEFAULT;
  424. *pbAutoLogonWithDefault = FALSE;
  425.  
  426. // if serialized creds are available, try using them to logon
  427. if (_SerializationAvailable(SAF_USERNAME) && _SerializationAvailable(SAF_PASSWORD))
  428. {
  429. *pdwDefault = 0;
  430. *pbAutoLogonWithDefault = TRUE;
  431. }
  432.  
  433. DebugPrintLn(hr);
  434.  
  435. return hr;
  436. }
  437.  
  438. // Returns the credential at the index specified by dwIndex. This function is called by logonUI to enumerate
  439. // the tiles.
  440. HRESULT CProvider::GetCredentialAt(
  441. __in DWORD dwIndex,
  442. __deref_out ICredentialProviderCredential** ppcpc
  443. )
  444. {
  445. DebugPrintLn(__FUNCTION__);
  446.  
  447. HRESULT hr = E_FAIL;
  448.  
  449. if (!_pccCredential)
  450. {
  451. DebugPrintLn("Checking for serialized credentials");
  452. PWSTR serializedUser, serializedPass, serializedDomain;
  453. _GetSerializedCredentials(&serializedUser, &serializedPass, &serializedDomain);
  454.  
  455. DebugPrintLn("Checking for missing credentials");
  456.  
  457. if (General::Fields::GetCurrentUsageScenario() == CPUS_UNLOCK_WORKSTATION && serializedUser == NULL)
  458. {
  459. if (serializedUser == NULL)
  460. {
  461. DebugPrintLn("Looking-up missing user name from session");
  462.  
  463. DWORD dwLen;
  464.  
  465. if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
  466. WTS_CURRENT_SESSION,
  467. WTSUserName,
  468. &serializedUser,
  469. &dwLen))
  470. {
  471. serializedUser = NULL;
  472. }
  473. }
  474.  
  475. if (serializedDomain == NULL)
  476. {
  477. DebugPrintLn("Looking-up missing domain name from session");
  478.  
  479. DWORD dwLen;
  480.  
  481. if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
  482. WTS_CURRENT_SESSION,
  483. WTSDomainName,
  484. &serializedDomain,
  485. &dwLen))
  486. {
  487. serializedDomain = NULL;
  488. }
  489. }
  490. }
  491. else if (General::Fields::GetCurrentUsageScenario() == CPUS_LOGON)
  492. {
  493. if (serializedDomain == NULL)
  494. {
  495. DebugPrintLn("Looking-up missing domain name from computer");
  496.  
  497. NETSETUP_JOIN_STATUS join_status;
  498.  
  499. if (!NetGetJoinInformation(
  500. NULL,
  501. &serializedDomain,
  502. &join_status) == NERR_Success || join_status == NetSetupUnjoined || join_status == NetSetupUnknownStatus)
  503. {
  504. serializedDomain = NULL;
  505. }
  506. }
  507. }
  508.  
  509. DebugPrintLn("Initializing CCredential");
  510.  
  511. _pccCredential = new CCredential();
  512.  
  513. /*
  514. switch (General::Fields::GetCurrentUsageScenario())
  515. {
  516. case CPUS_UNLOCK_WORKSTATION:
  517. hr = _pccCredential->Initialize(s_rgLogonUnlockCredProvFieldDescriptors, s_rgLogonUnlockFieldStatePairsUnlock, serializedUser, serializedDomain, serializedPass);
  518. break;
  519. case CPUS_LOGON:
  520. hr = _pccCredential->Initialize(s_rgLogonUnlockCredProvFieldDescriptors, s_rgLogonUnlockFieldStatePairs, serializedUser, serializedDomain, serializedPass);
  521. break;
  522. case CPUS_CHANGE_PASSWORD:
  523. hr = _pccCredential->Initialize(s_rgChangePasswordCredProvFieldDescriptors, s_rgChangePasswordFieldStatePairs, serializedUser, serializedDomain, serializedPass);
  524. break;
  525. default:
  526. hr = E_INVALIDARG;
  527. }
  528. */
  529.  
  530.  
  531. hr = _pccCredential->Initialize(s_rgCredProvFieldDescriptorsFor[General::Fields::GetCurrentUsageScenario()]
  532. , s_rgCredProvBaseFieldStatePairsFor[General::Fields::GetCurrentUsageScenario()]
  533. , serializedUser
  534. , serializedDomain
  535. , serializedPass);
  536.  
  537.  
  538.  
  539.  
  540. }
  541. else
  542. {
  543. hr = S_OK;
  544. }
  545.  
  546. DebugPrintLn("Checking for successful initialization");
  547.  
  548. if (FAILED(hr))
  549. {
  550. DebugPrintLn("Initialization failed");
  551. return hr;
  552. }
  553.  
  554. DebugPrintLn("Checking for successful instantiation");
  555.  
  556. if (!_pccCredential)
  557. {
  558. DebugPrintLn("Instantiation failed");
  559. return E_OUTOFMEMORY;
  560. }
  561.  
  562. DebugPrintLn("Returning interface to credential");
  563.  
  564. // Validate parameters.
  565. //if((dwIndex < _dwNumCreds) && ppcpc)
  566. if ((dwIndex == 0) && ppcpc)
  567. {
  568. if (General::Fields::GetCurrentUsageScenario() == CPUS_CREDUI)
  569. {
  570. DebugPrintLn("CredUI: returning an IID_ICredentialProviderCredential");
  571. hr = _pccCredential->QueryInterface(IID_ICredentialProviderCredential, reinterpret_cast<void **>(ppcpc));
  572. }
  573. else
  574. {
  575. DebugPrintLn("Non-CredUI: returning an IID_IConnectableCredentialProviderCredential");
  576. hr = _pccCredential->QueryInterface(IID_IConnectableCredentialProviderCredential, reinterpret_cast<void **>(ppcpc));
  577. //hr = _pccCredential->QueryInterface(IID_ICredentialProviderCredential, reinterpret_cast<void **>(ppcpc));
  578. }
  579. }
  580. else
  581. {
  582. hr = E_INVALIDARG;
  583. }
  584.  
  585. DebugPrintLn(hr);
  586.  
  587. return hr;
  588. }
  589.  
  590. // Sets up all the credentials for this provider. Since we always show the same tiles,
  591. // we just set it up once.
  592. /*
  593. HRESULT CProvider::_EnumerateCredentials(
  594. __in_opt PWSTR user_name,
  595. __in_opt PWSTR domain_name
  596. )
  597. {
  598. UNREFERENCED_PARAMETER(user_name);
  599. UNREFERENCED_PARAMETER(domain_name);
  600.  
  601. DebugPrintLn(__FUNCTION__);
  602.  
  603. HRESULT hr;
  604.  
  605. // Allocate memory for the new credential.
  606. CCredential* ppc = new CCredential();
  607.  
  608. if (ppc)
  609. {
  610. // Set the Field State Pair and Field Descriptors for ppc's fields
  611. // to the defaults (s_rgLogonUnlockCredProvFieldDescriptors, and s_rgLogonUnlockFieldStatePairs).
  612. /*
  613. if (General::Fields::GetCurrentUsageScenario() == CPUS_UNLOCK_WORKSTATION)
  614. hr = ppc->Initialize(s_rgLogonUnlockCredProvFieldDescriptors, s_rgLogonUnlockFieldStatePairsUnlock, user_name, domain_name);
  615. else
  616. hr = ppc->Initialize(s_rgLogonUnlockCredProvFieldDescriptors, s_rgLogonUnlockFieldStatePairs, user_name, domain_name);
  617. /
  618.  
  619. PWSTR serializedUser, serializedPass, serializedDomain;
  620. _GetSerializedCredentials(&serializedUser, &serializedPass, &serializedDomain);
  621.  
  622. switch (General::Fields::GetCurrentUsageScenario())
  623. {
  624. case CPUS_UNLOCK_WORKSTATION:
  625. hr = ppc->Initialize(s_rgLogonUnlockCredProvFieldDescriptors, s_rgLogonUnlockFieldStatePairsUnlock, serializedUser, serializedDomain, serializedPass);
  626. break;
  627. case CPUS_LOGON:
  628. hr = ppc->Initialize(s_rgLogonUnlockCredProvFieldDescriptors, s_rgLogonUnlockFieldStatePairs, serializedUser, serializedDomain, serializedPass);
  629. break;
  630. case CPUS_CHANGE_PASSWORD:
  631. hr = ppc->Initialize(s_rgChangePasswordCredProvFieldDescriptors, s_rgChangePasswordFieldStatePairs, serializedUser, serializedDomain, serializedPass);
  632. break;
  633. default:
  634. hr = E_INVALIDARG;
  635. }
  636.  
  637. if (SUCCEEDED(hr))
  638. {
  639. _rgpCredentials[0] = ppc;
  640. //_dwNumCreds++;
  641. }
  642. else
  643. {
  644. // Release the pointer to account for the local reference.
  645. ppc->Release();
  646. }
  647. }
  648. else
  649. {
  650. hr = E_OUTOFMEMORY;
  651. }
  652.  
  653. return hr;
  654. }
  655. */
  656.  
  657. // Boilerplate code to create our provider.
  658. HRESULT CSample_CreateInstance(__in REFIID riid, __deref_out void** ppv)
  659. {
  660. DebugPrintLn(__FUNCTION__);
  661.  
  662. HRESULT hr;
  663.  
  664. CProvider* pProvider = new CProvider();
  665.  
  666. if (pProvider)
  667. {
  668. hr = pProvider->QueryInterface(riid, ppv);
  669. pProvider->Release();
  670. }
  671. else
  672. {
  673. hr = E_OUTOFMEMORY;
  674. }
  675.  
  676. DebugPrintLn(hr);
  677.  
  678. return hr;
  679. }
  680.  
  681. // This enumerates a tile for the info in _pkiulSetSerialization. See the SetSerialization function comment for
  682. // more information.
  683. /*
  684. HRESULT CProvider::_EnumerateSetSerialization()
  685. {
  686. return E_NOTIMPL;
  687. }
  688. */
  689.  
  690. void CProvider::_GetSerializedCredentials(PWSTR *username, PWSTR *password, PWSTR *domain)
  691. {
  692. DebugPrintLn(__FUNCTION__);
  693.  
  694. if (username)
  695. {
  696. if (_SerializationAvailable(SAF_USERNAME))
  697. {
  698. *username = (PWSTR)LocalAlloc(LMEM_ZEROINIT, _pkiulSetSerialization->Logon.UserName.Length + sizeof(wchar_t));
  699. CopyMemory(*username, _pkiulSetSerialization->Logon.UserName.Buffer, _pkiulSetSerialization->Logon.UserName.Length);
  700. }
  701. else
  702. *username = NULL;
  703. }
  704.  
  705. if (password)
  706. {
  707. if (_SerializationAvailable(SAF_PASSWORD))
  708. {
  709. *password = (PWSTR)LocalAlloc(LMEM_ZEROINIT, _pkiulSetSerialization->Logon.Password.Length + sizeof(wchar_t));
  710. CopyMemory(*password, _pkiulSetSerialization->Logon.Password.Buffer, _pkiulSetSerialization->Logon.Password.Length);
  711. }
  712. else
  713. *password = NULL;
  714. }
  715.  
  716. if (domain)
  717. {
  718. if (_SerializationAvailable(SAF_DOMAIN))
  719. {
  720. *domain = (PWSTR)LocalAlloc(LMEM_ZEROINIT, _pkiulSetSerialization->Logon.LogonDomainName.Length + sizeof(wchar_t));
  721. CopyMemory(*domain, _pkiulSetSerialization->Logon.LogonDomainName.Buffer, _pkiulSetSerialization->Logon.LogonDomainName.Length);
  722. }
  723. else
  724. *domain = NULL;
  725. }
  726. }
  727.  
  728. bool CProvider::_SerializationAvailable(SERIALIZATION_AVAILABLE_FOR checkFor)
  729. {
  730. DebugPrintLn(__FUNCTION__);
  731.  
  732. bool result = false;
  733.  
  734. if (!_pkiulSetSerialization)
  735. {
  736. DebugPrintLn("No serialized creds set");
  737. }
  738. else {
  739. switch (checkFor)
  740. {
  741. case SAF_USERNAME:
  742. result = _pkiulSetSerialization->Logon.UserName.Length && _pkiulSetSerialization->Logon.UserName.Buffer;
  743. break;
  744. case SAF_PASSWORD:
  745. result = _pkiulSetSerialization->Logon.Password.Length && _pkiulSetSerialization->Logon.Password.Buffer;
  746. break;
  747. case SAF_DOMAIN:
  748. result = _pkiulSetSerialization->Logon.LogonDomainName.Length && _pkiulSetSerialization->Logon.LogonDomainName.Buffer;
  749. break;
  750. }
  751. }
  752.  
  753. return result;
  754. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement