Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1319 lines
42 KiB

  1. /*---------------------------------------------------------------------------
  2. File: PwdRpc.cpp
  3. Comments: RPC interface for Password Migration Lsa Notification Package
  4. and other internal functions.
  5. REVISION LOG ENTRY
  6. Revision By: Paul Thompson
  7. Revised on 09/04/00
  8. ---------------------------------------------------------------------------
  9. */
  10. #include "Pwd.h"
  11. #include <lmcons.h>
  12. #include <comdef.h>
  13. #include <malloc.h>
  14. #include "PwdSvc.h"
  15. #include "McsDmMsg.h"
  16. #include "AdmtCrypt2.h"
  17. #include "pwdfuncs.h"
  18. #include "TReg.hpp"
  19. #include "IsAdmin.hpp"
  20. #include "ResStr.h"
  21. #include "TxtSid.h"
  22. #include "resource.h"
  23. #include <MsPwdMig.h>
  24. /* global definitions */
  25. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  26. #define STATUS_NULL_LM_PASSWORD ((NTSTATUS)0x4000000DL)
  27. #define LM_BUFFER_LENGTH (LM20_PWLEN + 1)
  28. typedef NTSTATUS (CALLBACK * LSAIWRITEAUDITEVENT)(PSE_ADT_PARAMETER_ARRAY, ULONG);
  29. /* global variables */
  30. CRITICAL_SECTION csADMTCriticalSection; //critical sectio to protect concurrent first-time access
  31. SAMPR_HANDLE hgDomainHandle = NULL; //domain handle used in password calls
  32. LM_OWF_PASSWORD NullLmOwfPassword; //NULL representation of an LM Owf Password
  33. NT_OWF_PASSWORD NullNtOwfPassword; //NULL representation of an NT Owf Password
  34. HCRYPTPROV g_hProvider = 0;
  35. HCRYPTKEY g_hSessionKey = 0;
  36. HANDLE hEventSource;
  37. HMODULE hLsaDLL = NULL;
  38. LSAIWRITEAUDITEVENT LsaIWriteAuditEvent = NULL;
  39. PWCHAR pDomain = NULL;
  40. BOOL LsapCrashOnAuditFail = TRUE;
  41. int nOSVer = 4;
  42. BOOL bWhistlerDC = FALSE;
  43. /* Checks if this machine is running Whistler OS or something even newer and the OS major verison number, sets global variables accordingly */
  44. void GetOS()
  45. {
  46. /* local constants */
  47. const int WINDOWS_2000_BUILD_NUMBER = 2195;
  48. /* local variables */
  49. TRegKey verKey, regComputer;
  50. DWORD rc = 0;
  51. WCHAR sBuildNum[MAX_PATH];
  52. /* function body */
  53. //connect to the DC's HKLM registry key
  54. rc = regComputer.Connect(HKEY_LOCAL_MACHINE, NULL);
  55. if (rc == ERROR_SUCCESS)
  56. {
  57. //see if this machine is running Windows XP or newer by checking the
  58. //build number in the registry. If not, then we don't need to check
  59. //for the new security option
  60. rc = verKey.OpenRead(L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",&regComputer);
  61. if (rc == ERROR_SUCCESS)
  62. {
  63. //get the CurrentBuildNumber string
  64. rc = verKey.ValueGetStr(L"CurrentBuildNumber", sBuildNum, MAX_PATH);
  65. if (rc == ERROR_SUCCESS)
  66. {
  67. int nBuild = _wtoi(sBuildNum);
  68. if (nBuild <= WINDOWS_2000_BUILD_NUMBER)
  69. bWhistlerDC = FALSE;
  70. else
  71. bWhistlerDC = TRUE;
  72. }
  73. //get the Version Number
  74. rc = verKey.ValueGetStr(L"CurrentVersion", sBuildNum, MAX_PATH);
  75. if (rc == ERROR_SUCCESS)
  76. nOSVer = _wtoi(sBuildNum);
  77. }
  78. }
  79. return;
  80. }
  81. _bstr_t GetString(DWORD dwID)
  82. {
  83. /* local variables */
  84. HINSTANCE m_hInstance = NULL;
  85. WCHAR sBuffer[1000];
  86. int len;
  87. _bstr_t bstrRet;
  88. /* function body */
  89. m_hInstance = LoadLibrary(L"PwMig.dll");
  90. len = LoadString(m_hInstance, dwID, sBuffer, 1000);
  91. bstrRet = sBuffer;
  92. if (m_hInstance)
  93. FreeLibrary(m_hInstance);
  94. return bstrRet;
  95. }
  96. /***************************
  97. * Event Logging Functions *
  98. ***************************/
  99. /*++
  100. Routine Description:
  101. Implements current policy of how to deal with a failed audit.
  102. Arguments:
  103. None.
  104. Return Value:
  105. None.
  106. --*/
  107. void LsapAuditFailed(NTSTATUS AuditStatus)
  108. {
  109. /* local variables */
  110. NTSTATUS Status;
  111. ULONG Response;
  112. ULONG_PTR HardErrorParam;
  113. BOOLEAN PrivWasEnabled;
  114. TRegKey verKey, regComputer;
  115. DWORD rc = 0;
  116. WCHAR sBuildNum[MAX_PATH];
  117. DWORD crashVal;
  118. BOOL bRaiseError = FALSE;
  119. /* function body */
  120. //connect to this machine's HKLM registry key
  121. rc = regComputer.Connect(HKEY_LOCAL_MACHINE, NULL);
  122. if (rc == ERROR_SUCCESS)
  123. {
  124. //open the LSA key and see if crash on audit failed is turned on
  125. rc = verKey.Open(L"SYSTEM\\CurrentControlSet\\Control\\Lsa",&regComputer);
  126. if (rc == ERROR_SUCCESS)
  127. {
  128. //get the CrashOnAuditFail value
  129. rc = verKey.ValueGetDWORD(CRASH_ON_AUDIT_FAIL_VALUE, &crashVal);
  130. if (rc == ERROR_SUCCESS)
  131. {
  132. //if crash on audit fail is set, turn off the flag
  133. if (crashVal == LSAP_CRASH_ON_AUDIT_FAIL)
  134. {
  135. bRaiseError = TRUE; //set flag to raise hard error
  136. rc = verKey.ValueSetDWORD(CRASH_ON_AUDIT_FAIL_VALUE, LSAP_ALLOW_ADIMIN_LOGONS_ONLY);
  137. if (rc == ERROR_SUCCESS)
  138. {
  139. //flush the key to disk
  140. do
  141. {
  142. Status = NtFlushKey(verKey.KeyGet());
  143. } while ((Status == STATUS_INSUFFICIENT_RESOURCES) || (Status == STATUS_NO_MEMORY));
  144. ASSERT(NT_SUCCESS(Status));
  145. }
  146. }
  147. }
  148. }
  149. }
  150. //if needed, raise a hard error
  151. if (bRaiseError)
  152. {
  153. HardErrorParam = AuditStatus;
  154. // enable the shutdown privilege so that we can bugcheck
  155. Status = RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &PrivWasEnabled);
  156. Status = NtRaiseHardError(
  157. STATUS_AUDIT_FAILED,
  158. 1,
  159. 0,
  160. &HardErrorParam,
  161. OptionShutdownSystem,
  162. &Response);
  163. }
  164. return;
  165. }
  166. /*Routine Description:
  167. Find out if auditing is enabled for a certain event category and
  168. event success/failure case.
  169. Arguments:
  170. AuditCategory - Category of event to be audited.
  171. e.g. AuditCategoryPolicyChange
  172. AuditEventType - status type of event
  173. e.g. EVENTLOG_AUDIT_SUCCESS or EVENTLOG_AUDIT_FAILURE
  174. Return Value:
  175. TRUE or FALSE
  176. */
  177. BOOL LsapAdtIsAuditingEnabledForCategory(POLICY_AUDIT_EVENT_TYPE AuditCategory,
  178. UINT AuditEventType)
  179. {
  180. BOOL bSuccess = FALSE;
  181. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  182. NTSTATUS status = 0;
  183. LSA_HANDLE hPolicy;
  184. ASSERT((AuditEventType == EVENTLOG_AUDIT_SUCCESS) ||
  185. (AuditEventType == EVENTLOG_AUDIT_FAILURE));
  186. //attempt to open the policy.
  187. ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));//object attributes are reserved, so initalize to zeroes.
  188. status = LsaOpenPolicy( NULL,
  189. &ObjectAttributes,
  190. POLICY_ALL_ACCESS,
  191. &hPolicy); //recieves the policy handle
  192. if (NT_SUCCESS(status))
  193. {
  194. //ask for audit event policy information
  195. PPOLICY_AUDIT_EVENTS_INFO info;
  196. status = LsaQueryInformationPolicy(hPolicy, PolicyAuditEventsInformation, (PVOID *)&info);
  197. if (NT_SUCCESS(status))
  198. {
  199. //if auditing is enabled, see if enable for this type
  200. if (info->AuditingMode)
  201. {
  202. POLICY_AUDIT_EVENT_OPTIONS EventAuditingOptions;
  203. EventAuditingOptions = info->EventAuditingOptions[AuditCategory];
  204. bSuccess = (AuditEventType == EVENTLOG_AUDIT_SUCCESS) ?
  205. (BOOL) (EventAuditingOptions & POLICY_AUDIT_EVENT_SUCCESS):
  206. (BOOL) (EventAuditingOptions & POLICY_AUDIT_EVENT_FAILURE);
  207. }
  208. LsaFreeMemory((PVOID) info); //free policy info structure
  209. }
  210. LsaClose(hPolicy); //Freeing the policy object handle
  211. }
  212. return bSuccess;
  213. }
  214. /*++
  215. Routine Description:
  216. This routine impersonates our client, opens the thread token, and
  217. extracts the User Sid. It puts the Sid in memory allocated via
  218. LsapAllocateLsaHeap, which must be freed by the caller.
  219. Arguments:
  220. None.
  221. Return Value:
  222. Returns a pointer to heap memory containing a copy of the Sid, or
  223. NULL.
  224. --*/
  225. NTSTATUS LsapQueryClientInfo(PTOKEN_USER *UserSid, PLUID AuthenticationId)
  226. {
  227. NTSTATUS Status = STATUS_SUCCESS;
  228. HANDLE TokenHandle;
  229. ULONG ReturnLength;
  230. TOKEN_STATISTICS TokenStats;
  231. //impersonate the caller
  232. Status = I_RpcMapWin32Status(RpcImpersonateClient(NULL));
  233. if (!NT_SUCCESS(Status))
  234. return( Status );
  235. //open the thread token
  236. Status = NtOpenThreadToken(
  237. NtCurrentThread(),
  238. TOKEN_QUERY,
  239. TRUE, // OpenAsSelf
  240. &TokenHandle);
  241. if (!NT_SUCCESS(Status))
  242. {
  243. I_RpcMapWin32Status(RpcRevertToSelf());
  244. return( Status );
  245. }
  246. //revert to self
  247. Status = I_RpcMapWin32Status(RpcRevertToSelf());
  248. ASSERT(NT_SUCCESS(Status));
  249. //get the size of the token information
  250. Status = NtQueryInformationToken (
  251. TokenHandle,
  252. TokenUser,
  253. NULL,
  254. 0,
  255. &ReturnLength);
  256. if (Status != STATUS_BUFFER_TOO_SMALL)
  257. {
  258. NtClose(TokenHandle);
  259. return( Status );
  260. }
  261. //allocate memory to hold the token info
  262. *UserSid = (PTOKEN_USER)malloc(ReturnLength);
  263. if (*UserSid == NULL)
  264. {
  265. NtClose(TokenHandle);
  266. return( STATUS_INSUFFICIENT_RESOURCES );
  267. }
  268. //get the token info
  269. Status = NtQueryInformationToken (
  270. TokenHandle,
  271. TokenUser,
  272. *UserSid,
  273. ReturnLength,
  274. &ReturnLength);
  275. if (!NT_SUCCESS(Status))
  276. {
  277. NtClose(TokenHandle);
  278. free(*UserSid);
  279. *UserSid = NULL;
  280. return( Status );
  281. }
  282. //get the authentication ID
  283. ReturnLength = 0;
  284. Status = NtQueryInformationToken (
  285. TokenHandle,
  286. TokenStatistics,
  287. (PVOID)&TokenStats,
  288. sizeof(TOKEN_STATISTICS),
  289. &ReturnLength);
  290. NtClose(TokenHandle);
  291. if (!NT_SUCCESS(Status))
  292. {
  293. free(*UserSid);
  294. *UserSid = NULL;
  295. return( Status );
  296. }
  297. *AuthenticationId = TokenStats.AuthenticationId;
  298. return Status;
  299. }
  300. /*********************************************************************
  301. * *
  302. * Written by: Paul Thompson *
  303. * Date: 23 APR 2001 *
  304. * *
  305. * This function is responsible for generating a *
  306. * SE_AUDITID_PASSWORD_HASH_ACCESS event in the security log. This *
  307. * function is called to generate that message when a user password *
  308. * hash is retrieved by the ADMT password filter DLL. *
  309. * All these event logging functions are copied and modified from LSA*
  310. * code written by others. *
  311. * *
  312. * Parameters: *
  313. * EventType - EVENTLOG_AUDIT_SUCCESS or EVENTLOG_AUDIT_FAILURE *
  314. * pszTargetUserName - name of user whose password is being retrieved*
  315. * pszTargetUserDomain - domain of user whose password is being *
  316. * retrieved *
  317. * *
  318. * Return Value: *
  319. * HRESULT - Standard Return Result *
  320. * *
  321. *********************************************************************/
  322. //BEGIN LsaAuditPasswordAccessEvent
  323. HRESULT LsaAuditPasswordAccessEvent(USHORT EventType,
  324. PCWSTR pszTargetUserName,
  325. PCWSTR pszTargetUserDomain)
  326. {
  327. /* local constants */
  328. const int W2K_VERSION_NUMBER = 5;
  329. /* local variables */
  330. HRESULT hr = S_OK;
  331. NTSTATUS Status = STATUS_SUCCESS;
  332. LUID ClientAuthenticationId;
  333. PTOKEN_USER TokenUserInformation=NULL;
  334. SE_ADT_PARAMETER_ARRAY AuditParameters = { 0 };
  335. PSE_ADT_PARAMETER_ARRAY_ENTRY Parameter;
  336. UNICODE_STRING TargetUser;
  337. UNICODE_STRING TargetDomain;
  338. UNICODE_STRING SubsystemName;
  339. UNICODE_STRING Explanation;
  340. _bstr_t sExplainText;
  341. /* function body */
  342. //if parameters are invalid, return
  343. if ( !((EventType == EVENTLOG_AUDIT_SUCCESS) ||
  344. (EventType == EVENTLOG_AUDIT_FAILURE)) ||
  345. !pszTargetUserName || !pszTargetUserDomain ||
  346. !*pszTargetUserName || !*pszTargetUserDomain )
  347. {
  348. return (HRESULT_FROM_WIN32(LsaNtStatusToWinError(STATUS_INVALID_PARAMETER)));
  349. }
  350. //if auditing is not enabled, return asap
  351. if (!LsapAdtIsAuditingEnabledForCategory(AuditCategoryAccountManagement, EventType))
  352. return S_OK;
  353. // get caller info from the thread token
  354. Status = LsapQueryClientInfo( &TokenUserInformation, &ClientAuthenticationId );
  355. if (!NT_SUCCESS( Status ))
  356. {
  357. LsapAuditFailed(Status);
  358. return (HRESULT_FROM_WIN32(LsaNtStatusToWinError(Status)));
  359. }
  360. //If Whistler, init parameters and write event
  361. if (bWhistlerDC)
  362. {
  363. //init UNICODE_STRINGS
  364. RtlInitUnicodeString(&TargetUser, pszTargetUserName);
  365. RtlInitUnicodeString(&TargetDomain, pszTargetUserDomain);
  366. RtlInitUnicodeString(&SubsystemName, L"Security");
  367. //set the audit paramter header information
  368. RtlZeroMemory((PVOID) &AuditParameters, sizeof(AuditParameters));
  369. // AuditParameters.CategoryId = SE_CATEGID_ACCOUNT_MANAGEMENT;
  370. AuditParameters.CategoryId = AuditCategoryAccountManagement;
  371. AuditParameters.AuditId = SE_AUDITID_PASSWORD_HASH_ACCESS;
  372. AuditParameters.Type = EventType;
  373. //now set the audit parameters for this OS. Parameters are added to the structure using macros
  374. //defined in LsaParamMacros.h
  375. AuditParameters.ParameterCount = 0;
  376. LsapSetParmTypeSid(AuditParameters, AuditParameters.ParameterCount, TokenUserInformation->User.Sid);
  377. AuditParameters.ParameterCount++;
  378. LsapSetParmTypeString(AuditParameters, AuditParameters.ParameterCount, &SubsystemName);
  379. AuditParameters.ParameterCount++;
  380. LsapSetParmTypeString(AuditParameters, AuditParameters.ParameterCount, &TargetUser);
  381. AuditParameters.ParameterCount++;
  382. LsapSetParmTypeString(AuditParameters, AuditParameters.ParameterCount, &TargetDomain);
  383. AuditParameters.ParameterCount++;
  384. LsapSetParmTypeLogonId(AuditParameters, AuditParameters.ParameterCount, ClientAuthenticationId);
  385. AuditParameters.ParameterCount++;
  386. //Write to the security log
  387. Status = LsaIWriteAuditEvent(&AuditParameters, 0);
  388. if (!NT_SUCCESS(Status))
  389. LsapAuditFailed(Status);
  390. //do not free the TargetUser string since it is still being used
  391. //do not free the TargetDomain string since it is a global variable
  392. RtlFreeUnicodeString(&SubsystemName);
  393. }//end if Whistler
  394. //else if W2K, init parameters and write event
  395. else if ((!bWhistlerDC) && (nOSVer == W2K_VERSION_NUMBER))
  396. {
  397. //init UNICODE_STRINGS
  398. RtlInitUnicodeString(&TargetUser, pszTargetUserName);
  399. RtlInitUnicodeString(&TargetDomain, pszTargetUserDomain);
  400. RtlInitUnicodeString(&SubsystemName, L"Security");
  401. //if not Whistler the audit message will be vague as to its intent, therefore we will add some
  402. //explanation text
  403. sExplainText = GetString(IDS_EVENT_PWD_HASH_W2K_EXPLAIN);
  404. RtlInitUnicodeString(&Explanation, (WCHAR*)sExplainText);
  405. //set the audit paramter header information
  406. RtlZeroMemory((PVOID) &AuditParameters, sizeof(AuditParameters));
  407. // AuditParameters.CategoryId = SE_CATEGID_ACCOUNT_MANAGEMENT;
  408. AuditParameters.CategoryId = AuditCategoryAccountManagement;
  409. AuditParameters.AuditId = SE_AUDITID_PASSWORD_HASH_ACCESS;
  410. AuditParameters.Type = EventType;
  411. //now set the audit parameters for this OS. Parameters are added to the structure using macros
  412. //defined in LsaParamMacros.h
  413. AuditParameters.ParameterCount = 0;
  414. LsapSetParmTypeSid(AuditParameters, AuditParameters.ParameterCount, TokenUserInformation->User.Sid);
  415. AuditParameters.ParameterCount++;
  416. LsapSetParmTypeString(AuditParameters, AuditParameters.ParameterCount, &SubsystemName);
  417. AuditParameters.ParameterCount++;
  418. LsapSetParmTypeString(AuditParameters, AuditParameters.ParameterCount, &TargetUser);
  419. AuditParameters.ParameterCount++;
  420. LsapSetParmTypeString(AuditParameters, AuditParameters.ParameterCount, &TargetDomain);
  421. AuditParameters.ParameterCount++;
  422. LsapSetParmTypeLogonId(AuditParameters, AuditParameters.ParameterCount, ClientAuthenticationId);
  423. AuditParameters.ParameterCount++;
  424. LsapSetParmTypeString(AuditParameters, AuditParameters.ParameterCount, &Explanation);
  425. AuditParameters.ParameterCount++;
  426. //Write to the security log
  427. Status = LsaIWriteAuditEvent(&AuditParameters, 0);
  428. if (!NT_SUCCESS(Status))
  429. LsapAuditFailed(Status);
  430. //do not free the TargetUser string since it is still being used
  431. //do not free the TargetDomain string since it is a global variable
  432. RtlFreeUnicodeString(&SubsystemName);
  433. RtlFreeUnicodeString(&Explanation);
  434. }//end if Whistler
  435. if (TokenUserInformation != NULL)
  436. free(TokenUserInformation);
  437. return (HRESULT_FROM_WIN32(LsaNtStatusToWinError(Status)));
  438. }
  439. /*********************************************************************
  440. * *
  441. * Written by: Paul Thompson *
  442. * Date: 8 SEPT 2000 *
  443. * *
  444. * This function is responsible for retrieving the caller's sid. *
  445. * We will use this prior to logging an event log. *
  446. * *
  447. *********************************************************************/
  448. //BEGIN GetCallerSid
  449. DWORD GetCallerSid(PSID *pCallerSid)
  450. {
  451. /* local variables */
  452. DWORD rc, rc2;
  453. HANDLE hToken = NULL;
  454. TOKEN_USER tUser[10];
  455. ULONG len;
  456. /* function body */
  457. rc = (DWORD)RpcImpersonateClient(NULL);
  458. if (!rc)
  459. {
  460. if ( OpenThreadToken(GetCurrentThread(), TOKEN_READ, TRUE, &hToken) )
  461. {
  462. if ( GetTokenInformation(hToken,TokenUser,tUser,10*(sizeof TOKEN_USER),&len) )
  463. *pCallerSid = (SID*)tUser[0].User.Sid;
  464. else
  465. rc = GetLastError();
  466. CloseHandle(hToken);
  467. }
  468. else
  469. rc = GetLastError();
  470. rc2 = (DWORD)RpcRevertToSelf();
  471. if (rc2)
  472. rc = rc2;
  473. }
  474. return rc;
  475. }
  476. //END GetCallerSid
  477. /*********************************************************************
  478. * *
  479. * Written by: Paul Thompson *
  480. * Date: 19 SEPT 2000 *
  481. * *
  482. * This function is responsible for logging major events in Event*
  483. * Viewer. *
  484. * *
  485. *********************************************************************/
  486. //BEGIN LogEvent
  487. void LogPwdEvent(const WCHAR* srcName, bool bAuditSuccess)
  488. {
  489. /* local variables */
  490. USHORT wType;
  491. DWORD rc = 0;
  492. BOOL rcBool;
  493. /* function body */
  494. if (bAuditSuccess)
  495. wType = EVENTLOG_AUDIT_SUCCESS;
  496. else
  497. wType = EVENTLOG_AUDIT_FAILURE;
  498. //if NT4.0, write to the Security Event Log as you would any log
  499. if (nOSVer == 4)
  500. {
  501. PSID pCallerSid = NULL;
  502. GetCallerSid(&pCallerSid); //get the caller's SID
  503. if ((pCallerSid) && (hEventSource))
  504. {
  505. LPTSTR pStringArray[1];
  506. WCHAR msg[2000];
  507. WCHAR txtSid[MAX_PATH];
  508. DWORD lenTxt = MAX_PATH;
  509. //prepare the msg to display
  510. if (!GetTextualSid(pCallerSid,txtSid,&lenTxt))
  511. wcscpy(txtSid, L"");
  512. swprintf(msg, GetString(IDS_EVENT_PWD_HASH_RETRIEVAL), srcName, pDomain, txtSid);
  513. pStringArray[0] = msg;
  514. //log the event
  515. rcBool = ReportEventW(hEventSource, // handle of event source
  516. wType, // event type
  517. SE_CATEGID_ACCOUNT_MANAGEMENT,// event category
  518. SE_AUDITID_PASSWORD_HASH_ACCESS,// event ID
  519. NULL, // current user's SID
  520. 1, // strings in lpszStrings
  521. 0, // no bytes of raw data
  522. (LPCTSTR *)pStringArray, // array of error strings
  523. NULL ); // no raw data
  524. if ( !rcBool )
  525. rc = GetLastError();
  526. }
  527. }
  528. else //else write the event by requesting LSA to do it for us
  529. {
  530. //if not already done, late bind to LsaIWriteAuditEvent since it is not present on an NT 4.0 box
  531. if (!LsaIWriteAuditEvent)
  532. {
  533. hLsaDLL = LoadLibrary(L"LsaSrv.dll");
  534. if ( hLsaDLL )
  535. LsaIWriteAuditEvent = (LSAIWRITEAUDITEVENT)GetProcAddress(hLsaDLL, "LsaIWriteAuditEvent");
  536. }
  537. if (LsaIWriteAuditEvent)
  538. LsaAuditPasswordAccessEvent(wType, srcName, pDomain);
  539. }
  540. }
  541. //END LogEvent
  542. /*******************************
  543. * Event Logging Functions End *
  544. *******************************/
  545. /*********************************************************************
  546. * *
  547. * Written by: Paul Thompson *
  548. * Date: 8 SEPT 2000 *
  549. * *
  550. * This function is responsible for obtaining the account domain *
  551. * sid. This sid will be later used to Open the domain via SAM. *
  552. * *
  553. *********************************************************************/
  554. //BEGIN GetDomainSid
  555. NTSTATUS GetDomainSid(PSID * pDomainSid)
  556. {
  557. /* local variables */
  558. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  559. NTSTATUS status = 0;
  560. LSA_HANDLE hPolicy;
  561. HRESULT hr = 0;
  562. /* function body */
  563. //object attributes are reserved, so initalize to zeroes.
  564. ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
  565. //attempt to open the policy.
  566. status = LsaOpenPolicy(
  567. NULL,
  568. &ObjectAttributes,
  569. POLICY_ALL_ACCESS,
  570. &hPolicy //recieves the policy handle
  571. );
  572. if (NT_SUCCESS(status))
  573. {
  574. //ask for account domain policy information
  575. PPOLICY_ACCOUNT_DOMAIN_INFO info;
  576. status = LsaQueryInformationPolicy(hPolicy, PolicyAccountDomainInformation, (PVOID *)&info);
  577. if (NT_SUCCESS(status))
  578. {
  579. //save the domain sid
  580. DWORD sidLen = GetLengthSid(info->DomainSid);
  581. *pDomainSid = new BYTE[sidLen];
  582. if (*pDomainSid)
  583. CopySid(sidLen, *pDomainSid, info->DomainSid);
  584. else
  585. status = STATUS_INSUFFICIENT_RESOURCES;
  586. //save the domain name
  587. USHORT uLen = info->DomainName.Length / sizeof(WCHAR);
  588. pDomain = new WCHAR[uLen + sizeof(WCHAR)];
  589. if (pDomain)
  590. {
  591. wcsncpy(pDomain, info->DomainName.Buffer, uLen);
  592. pDomain[uLen] = L'\0';
  593. }
  594. //free policy info structure
  595. LsaFreeMemory((PVOID) info);
  596. }
  597. //Freeing the policy object handle
  598. LsaClose(hPolicy);
  599. }
  600. return status;
  601. }
  602. //END GetDomainSid
  603. /*********************************************************************
  604. * *
  605. * Written by: Paul Thompson *
  606. * Date: 8 SEPT 2000 *
  607. * *
  608. * This function is responsible for obtaining a domain handle *
  609. * used repeatedly by our interface function CopyPassword. *
  610. * For optimization, this function should only be called once per*
  611. * the life of this dll. *
  612. * This function also gets an Event Handle to the event log. *
  613. * *
  614. *********************************************************************/
  615. //BEGIN GetDomainHandle
  616. NTSTATUS GetDomainHandle(SAMPR_HANDLE *pDomainHandle)
  617. {
  618. /* local variables */
  619. PSID pDomainSid;
  620. NTSTATUS status;
  621. SAMPR_HANDLE hServerHandle;
  622. SAMPR_HANDLE hDomainHandle;
  623. /* function body */
  624. //get the account domain sid
  625. status = GetDomainSid(&pDomainSid);
  626. if (NT_SUCCESS(status))
  627. {
  628. //connect to the Sam and get a server handle
  629. status = SamIConnect(NULL,
  630. &hServerHandle,
  631. POLICY_ALL_ACCESS,
  632. TRUE);
  633. if (NT_SUCCESS(status))
  634. {
  635. //get the account domain handle
  636. status = SamrOpenDomain(hServerHandle,
  637. POLICY_ALL_ACCESS,
  638. (PRPC_SID)pDomainSid,
  639. &hDomainHandle);
  640. if (NT_SUCCESS(status))
  641. *pDomainHandle = hDomainHandle;
  642. //close the SamIConnect server handle
  643. SamrCloseHandle(&hServerHandle);
  644. }
  645. FreeSid(pDomainSid);
  646. }
  647. return status;
  648. }
  649. //END GetDomainHandle
  650. /*********************************************************************
  651. * *
  652. * Written by: Paul Thompson *
  653. * Date: 8 SEPT 2000 *
  654. * *
  655. * This function is responsible for retrieving the global domain *
  656. * handle. If we don't have the handle yet, it calls the externally *
  657. * defined GetDomainHandle funtion to get the handle. The handle *
  658. * retrieval code is placed in a critical section. Subsequent *
  659. * calls to this functin merely return the handle. *
  660. * I will also use this function to fill the global NULL *
  661. * LmOwfPassword structure for possible use. This should be done *
  662. * one time only. *
  663. * *
  664. *********************************************************************/
  665. //BEGIN RetrieveDomainHandle
  666. HRESULT RetrieveDomainHandle(SAMPR_HANDLE *pDomainHandle)
  667. {
  668. /* local constants */
  669. const WCHAR * svcName = L"Security";
  670. /* local variables */
  671. NTSTATUS status = 0;
  672. HRESULT hr = ERROR_SUCCESS;
  673. /* function body */
  674. __try
  675. {
  676. //enter the critical section
  677. EnterCriticalSection(&csADMTCriticalSection);
  678. //if not yet retrieved, get the global handle and fill the NULL
  679. //LmOwfPassword structure
  680. if (hgDomainHandle == NULL)
  681. {
  682. //get the domain handle
  683. status = GetDomainHandle(&hgDomainHandle);
  684. if (NT_SUCCESS(status))
  685. pDomainHandle = &hgDomainHandle;
  686. GetOS(); //set global variable as to whether this DC's OS
  687. //if NT4.0 OS on this DC, then set the event handle for logging events
  688. if (nOSVer == 4)
  689. {
  690. NTSTATUS Status;
  691. BOOLEAN PrivWasEnabled;
  692. //make sure we have audit and debug privileges
  693. RtlAdjustPrivilege( SE_SECURITY_PRIVILEGE, TRUE, FALSE, &PrivWasEnabled );
  694. RtlAdjustPrivilege( SE_DEBUG_PRIVILEGE, TRUE, FALSE, &PrivWasEnabled );
  695. RtlAdjustPrivilege( SE_AUDIT_PRIVILEGE, TRUE, FALSE, &PrivWasEnabled );
  696. //register this dll with the eventlog, get a handle, and store globally
  697. hEventSource = RegisterEventSourceW(NULL, svcName);
  698. if (!hEventSource)
  699. DWORD rc = GetLastError();
  700. }
  701. //fill a global NULL LmOwfPassword in case we need it later
  702. WCHAR sNtPwd[MAX_PATH] = L"";
  703. UNICODE_STRING UnicodePwd;
  704. ANSI_STRING LmPassword;
  705. CHAR sBuf[LM_BUFFER_LENGTH];
  706. RtlInitUnicodeString(&UnicodePwd, sNtPwd);
  707. //fill LmOwf NULL password
  708. LmPassword.Buffer = (PCHAR)&sBuf;
  709. LmPassword.MaximumLength = LmPassword.Length = LM_BUFFER_LENGTH;
  710. RtlZeroMemory( LmPassword.Buffer, LM_BUFFER_LENGTH );
  711. status = RtlUpcaseUnicodeStringToOemString( &LmPassword, &UnicodePwd, FALSE );
  712. if ( !NT_SUCCESS(status) )
  713. {
  714. //the password is longer than the max LM password length
  715. status = STATUS_NULL_LM_PASSWORD;
  716. RtlZeroMemory( LmPassword.Buffer, LM_BUFFER_LENGTH );
  717. RtlCalculateLmOwfPassword((PLM_PASSWORD)&LmPassword, &NullLmOwfPassword);
  718. }
  719. else
  720. {
  721. RtlCalculateLmOwfPassword((PLM_PASSWORD)&LmPassword, &NullLmOwfPassword);
  722. RtlFreeOemString(&LmPassword);
  723. }
  724. //fill NtOwf NULL password
  725. RtlCalculateNtOwfPassword((PNT_PASSWORD)&UnicodePwd, &NullNtOwfPassword);
  726. RtlFreeUnicodeString(&UnicodePwd);
  727. }
  728. }
  729. __finally
  730. {
  731. // Release ownership and delete of the critical section
  732. LeaveCriticalSection(&csADMTCriticalSection);
  733. }
  734. //convert any error to a win error
  735. if (!NT_SUCCESS(status))
  736. hr = LsaNtStatusToWinError(status);
  737. return hr;
  738. }
  739. //END RetrieveDomainHandle
  740. /*********************************************************************
  741. * *
  742. * Written by: Paul Thompson *
  743. * Date: 11 SEPT 2000 *
  744. * *
  745. * This function is responsible for retrieving the passwords for *
  746. * the given user's source domain account. We use SAM APIs to *
  747. * retrieve the LmOwf and NtOwf formats of the password. *
  748. * *
  749. *********************************************************************/
  750. //BEGIN RetrieveEncrytedSourcePasswords
  751. HRESULT RetrieveEncrytedSourcePasswords(const WCHAR* srcName,
  752. PLM_OWF_PASSWORD pSrcLmOwfPwd,
  753. PNT_OWF_PASSWORD pSrcNtOwfPwd)
  754. {
  755. /* local variables */
  756. NTSTATUS status = 0;
  757. HRESULT hr = ERROR_SUCCESS;
  758. SAMPR_HANDLE hUserHandle = NULL;
  759. ULONG ulCount = 1;
  760. ULONG userID;
  761. RPC_UNICODE_STRING sNames[1];
  762. SAMPR_ULONG_ARRAY ulIDs;
  763. SAMPR_ULONG_ARRAY ulUse;
  764. PSAMPR_USER_INFO_BUFFER pInfoBuf = NULL;
  765. WCHAR * pName;
  766. /* function body */
  767. pName = new WCHAR[wcslen(srcName)+1];
  768. if (!pName)
  769. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  770. //get the user's ID
  771. sNames[0].Length = sNames[0].MaximumLength = (USHORT)((wcslen(srcName)) * sizeof(WCHAR));
  772. wcscpy(pName, srcName);
  773. sNames[0].Buffer = pName;
  774. ulIDs.Element = NULL;
  775. ulUse.Element = NULL;
  776. status = SamrLookupNamesInDomain(hgDomainHandle,
  777. ulCount,
  778. sNames,
  779. &ulIDs,
  780. &ulUse);
  781. delete [] pName;
  782. if (!NT_SUCCESS(status))
  783. return HRESULT_FROM_WIN32(LsaNtStatusToWinError(status));
  784. userID = *(ulIDs.Element);
  785. //get a user handle
  786. status = SamrOpenUser(hgDomainHandle,
  787. POLICY_ALL_ACCESS,
  788. userID,
  789. &hUserHandle);
  790. if (!NT_SUCCESS(status))
  791. {
  792. SamIFree_SAMPR_ULONG_ARRAY(&ulIDs);
  793. SamIFree_SAMPR_ULONG_ARRAY(&ulUse);
  794. return HRESULT_FROM_WIN32(LsaNtStatusToWinError(status));
  795. }
  796. //get the user's password
  797. status = SamrQueryInformationUser(hUserHandle,
  798. UserInternal3Information,
  799. &pInfoBuf);
  800. if (NT_SUCCESS(status)) //if success, get LmOwf and NtOwf versions of the password
  801. {
  802. if (pInfoBuf->Internal3.I1.NtPasswordPresent)
  803. memcpy(pSrcNtOwfPwd, pInfoBuf->Internal3.I1.NtOwfPassword.Buffer, sizeof(NT_OWF_PASSWORD));
  804. else
  805. memcpy(pSrcNtOwfPwd, &NullNtOwfPassword, sizeof(NT_OWF_PASSWORD));
  806. if (pInfoBuf->Internal3.I1.LmPasswordPresent)
  807. memcpy(pSrcLmOwfPwd, pInfoBuf->Internal3.I1.LmOwfPassword.Buffer, sizeof(LM_OWF_PASSWORD));
  808. else //else we need to use the global NULL LmOwfPassword
  809. memcpy(pSrcLmOwfPwd, &NullLmOwfPassword, sizeof(LM_OWF_PASSWORD));
  810. SamIFree_SAMPR_USER_INFO_BUFFER (pInfoBuf, UserInternal3Information);
  811. LogPwdEvent(srcName, true);
  812. }
  813. else
  814. LogPwdEvent(srcName, false);
  815. SamIFree_SAMPR_ULONG_ARRAY(&ulIDs);
  816. SamIFree_SAMPR_ULONG_ARRAY(&ulUse);
  817. SamrCloseHandle(&hUserHandle);
  818. if (!NT_SUCCESS(status))
  819. hr = HRESULT_FROM_WIN32(LsaNtStatusToWinError(status));
  820. return hr;
  821. }
  822. //END RetrieveEncrytedSourcePasswords
  823. /*********************************************************************
  824. * *
  825. * Written by: Paul Thompson *
  826. * Date: 11 SEPT 2000 *
  827. * *
  828. * This function is responsible for using the MSCHAP dll to *
  829. * change the given target user's password. *
  830. * *
  831. *********************************************************************/
  832. //BEGIN SetTargetPassword
  833. HRESULT SetTargetPassword(handle_t hBinding, const WCHAR* tgtServer,
  834. const WCHAR* tgtName, WCHAR* currentPwd,
  835. LM_OWF_PASSWORD newLmOwfPwd, NT_OWF_PASSWORD newNtOwfPwd)
  836. {
  837. /* local variables */
  838. NTSTATUS status;
  839. HRESULT hr = ERROR_SUCCESS;
  840. RPC_STATUS rcpStatus;
  841. UNICODE_STRING UnicodePwd;
  842. OEM_STRING oemString;
  843. LM_OWF_PASSWORD OldLmOwfPassword;
  844. NT_OWF_PASSWORD OldNtOwfPassword;
  845. BOOLEAN LmOldPresent = TRUE;
  846. int nConvert;
  847. char oldLmPwd[MAX_PATH];
  848. WCHAR * pTemp;
  849. /* function body */
  850. pTemp = new WCHAR[wcslen(currentPwd)+1];
  851. if (!pTemp)
  852. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  853. //convert the old LmOwf password
  854. wcscpy(pTemp, currentPwd);
  855. _wcsupr(pTemp);
  856. RtlInitUnicodeString(&UnicodePwd, pTemp);
  857. status = RtlUpcaseUnicodeStringToOemString(&oemString, &UnicodePwd, TRUE);
  858. delete [] pTemp;
  859. if (NT_SUCCESS(status))
  860. {
  861. if (status == STATUS_NULL_LM_PASSWORD)
  862. LmOldPresent = FALSE;
  863. else
  864. {
  865. strcpy(oldLmPwd, oemString.Buffer);
  866. status = RtlCalculateLmOwfPassword((PLM_PASSWORD)oldLmPwd, &OldLmOwfPassword);
  867. }
  868. RtlFreeOemString(&oemString);
  869. }
  870. //convert the old NtOwf password
  871. // RtlFreeUnicodeString(&UnicodePwd);
  872. RtlInitUnicodeString(&UnicodePwd, currentPwd);
  873. status = RtlCalculateNtOwfPassword(&UnicodePwd, &OldNtOwfPassword);
  874. // RtlFreeUnicodeString(&UnicodePwd);
  875. if (!NT_SUCCESS(status)) //if failed, leave
  876. return HRESULT_FROM_WIN32(LsaNtStatusToWinError(status));
  877. //impersonate the caller when setting the password
  878. rcpStatus = RpcImpersonateClient(hBinding);
  879. //change the Password!
  880. status = MSChapSrvChangePassword(const_cast<WCHAR*>(tgtServer),
  881. const_cast<WCHAR*>(tgtName),
  882. LmOldPresent,
  883. &OldLmOwfPassword,
  884. &newLmOwfPwd,
  885. &OldNtOwfPassword,
  886. &newNtOwfPwd);
  887. if (rcpStatus == RPC_S_OK)
  888. {
  889. rcpStatus = RpcRevertToSelf();
  890. hr = HRESULT_FROM_WIN32(rcpStatus);
  891. }
  892. if (!NT_SUCCESS(status))
  893. hr = HRESULT_FROM_WIN32(LsaNtStatusToWinError(status));
  894. return hr;
  895. }
  896. //END SetTargetPassword
  897. /*********************************************************************
  898. * *
  899. * Written by: Paul Thompson *
  900. * Date: 8 SEPT 2000 *
  901. * *
  902. * This function is responsible for checking to make sure that *
  903. * the calling client has the proper access on this machine and *
  904. * domain to change someone's password. We use a helper function to *
  905. * do the actual check. *
  906. * *
  907. *********************************************************************/
  908. //BEGIN AuthenticateClient
  909. DWORD
  910. AuthenticateClient(
  911. handle_t hBinding // in - binding for client call
  912. )
  913. {
  914. /* local variables */
  915. DWORD rc;
  916. DWORD rc2;
  917. /* function body */
  918. rc = (DWORD)RpcImpersonateClient(hBinding);
  919. if (!rc)
  920. {
  921. rc = IsAdminLocal();
  922. rc2 = (DWORD)RpcRevertToSelf();
  923. if (rc2)
  924. rc = rc2;
  925. }
  926. return rc;
  927. }
  928. //END AuthenticateClient
  929. /*********************************************************************
  930. * *
  931. * Written by: Paul Thompson *
  932. * Date: 6 SEPT 2000 *
  933. * *
  934. * This function is responsible for migrating the given user's *
  935. * password from the source domain, in which this dll is running, to *
  936. * the given migrated target domain account. We will retrieve the *
  937. * old user's current password and set the new user's password to *
  938. * match. *
  939. * *
  940. *********************************************************************/
  941. //BEGIN CopyPassword
  942. DWORD __stdcall
  943. CopyPassword(
  944. /* [in] */ handle_t hBinding,
  945. /* [string][in] */ const WCHAR __RPC_FAR *tgtServer,
  946. /* [string][in] */ const WCHAR __RPC_FAR *srcName,
  947. /* [string][in] */ const WCHAR __RPC_FAR *tgtName,
  948. /* [in] */ unsigned long dwPwd,
  949. /* [size_is][in] */const char __RPC_FAR *currentPwd
  950. )
  951. {
  952. HRESULT hr = ERROR_SUCCESS;
  953. SAMPR_HANDLE hDomain = NULL;
  954. LM_OWF_PASSWORD NewLmOwfPassword;
  955. NT_OWF_PASSWORD NewNtOwfPassword;
  956. NTSTATUS status;
  957. DWORD rc=0;
  958. PSID pCallerSid = NULL;
  959. _variant_t varPwd;
  960. _bstr_t bstrPwd;
  961. // validate parameters
  962. if ((tgtServer == NULL) || (srcName == NULL) || (tgtName == NULL) ||
  963. (currentPwd == NULL) || (dwPwd <= 0))
  964. {
  965. return E_INVALIDARG;
  966. }
  967. //validate the buffer and the reported size
  968. if (IsBadReadPtr(currentPwd, dwPwd))
  969. return E_INVALIDARG;
  970. //make sure the client is an admin on the local machine, otherwise, forget it
  971. rc = AuthenticateClient(hBinding);
  972. if ( rc )
  973. return HRESULT_FROM_WIN32(rc);
  974. try
  975. {
  976. //convert the incoming byte array into a variant
  977. varPwd = SetVariantWithBinaryArray(const_cast<char*>(currentPwd), dwPwd);
  978. if ((varPwd.vt != (VT_UI1|VT_ARRAY)) || (varPwd.parray == NULL))
  979. return E_INVALIDARG;
  980. //try to decrypt the password
  981. bstrPwd = AdmtDecrypt(g_hSessionKey, varPwd);
  982. if (!bstrPwd)
  983. return GetLastError();
  984. }
  985. catch (_com_error& ce)
  986. {
  987. return ce.Error();
  988. }
  989. catch (...)
  990. {
  991. return E_FAIL;
  992. }
  993. //get the domain handle
  994. hr = RetrieveDomainHandle(&hDomain);
  995. if (hr == ERROR_SUCCESS)
  996. {
  997. //get the user's password from the source domain
  998. hr = RetrieveEncrytedSourcePasswords(srcName, &NewLmOwfPassword, &NewNtOwfPassword);
  999. if (hr == ERROR_SUCCESS)
  1000. {
  1001. //set the target user's password to the source user's
  1002. hr = SetTargetPassword(hBinding, tgtServer, tgtName, (WCHAR*)bstrPwd,
  1003. NewLmOwfPassword, NewNtOwfPassword);
  1004. }
  1005. }
  1006. return hr;
  1007. }
  1008. //END CopyPassword
  1009. /*********************************************************************
  1010. * *
  1011. * Written by: Paul Thompson *
  1012. * Date: 6 SEPT 2000 *
  1013. * *
  1014. * This function is responsible for checking a registry value to *
  1015. * make sure that the ADMT password migration Lsa notification *
  1016. * package is installed, running, and ready to migrate passwords. *
  1017. * *
  1018. *********************************************************************/
  1019. //BEGIN CheckConfig
  1020. DWORD __stdcall
  1021. CheckConfig(
  1022. /* [in] */ handle_t hBinding,
  1023. /* [in] */ unsigned long dwSession,
  1024. /* [size_is][in] */const char __RPC_FAR *aSession,
  1025. /* [in] */ unsigned long dwPwd,
  1026. /* [size_is][in] */const char __RPC_FAR *aTestPwd,
  1027. /* [out] */ WCHAR __RPC_FAR tempPwd[PASSWORD_BUFFER_SIZE]
  1028. )
  1029. {
  1030. DWORD rc;
  1031. DWORD rval;
  1032. DWORD type; // type of value
  1033. DWORD len = sizeof rval; // value length
  1034. HKEY hKey;
  1035. _variant_t varPwd;
  1036. _variant_t varSession;
  1037. _bstr_t bstrPwd = L"";
  1038. // validate parameters
  1039. if ((aSession == NULL) || (aTestPwd == NULL) || (tempPwd == NULL) ||
  1040. (dwSession <= 0) || (dwPwd <= 0))
  1041. {
  1042. return E_INVALIDARG;
  1043. }
  1044. //validate the buffer and the reported size
  1045. if ((IsBadReadPtr(aSession, dwSession)) || (IsBadReadPtr(aTestPwd, dwPwd)) ||
  1046. (IsBadWritePtr((LPVOID)tempPwd, PASSWORD_BUFFER_SIZE * sizeof(WCHAR))))
  1047. {
  1048. return E_INVALIDARG;
  1049. }
  1050. //make sure the client is an admin on the local machine, otherwise, forget it
  1051. rc = AuthenticateClient(hBinding);
  1052. if ( rc )
  1053. return HRESULT_FROM_WIN32(rc);
  1054. //make sure the registry value is set for password migration
  1055. rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  1056. L"System\\CurrentControlSet\\Control\\Lsa",
  1057. 0,
  1058. KEY_READ,
  1059. &hKey);
  1060. if (rc == ERROR_SUCCESS)
  1061. {
  1062. rc = RegQueryValueEx(hKey, L"AllowPasswordExport", NULL, &type, (BYTE *)&rval, &len);
  1063. RegCloseKey(hKey);
  1064. if ((rc == ERROR_SUCCESS) && (type == REG_DWORD) && (rval == 1))
  1065. rc = ERROR_SUCCESS;
  1066. else
  1067. return PM_E_PASSWORD_MIGRATION_NOT_ENABLED;
  1068. }
  1069. try
  1070. {
  1071. //convert the incoming byte arrays into variants
  1072. varSession = SetVariantWithBinaryArray(const_cast<char*>(aSession), dwSession);
  1073. varPwd = SetVariantWithBinaryArray(const_cast<char*>(aTestPwd), dwPwd);
  1074. if ((varSession.vt != (VT_UI1|VT_ARRAY)) || (varSession.parray == NULL) ||
  1075. (varPwd.vt != (VT_UI1|VT_ARRAY)) || (varPwd.parray == NULL))
  1076. return E_INVALIDARG;
  1077. // acquire cryptographic service provider context
  1078. if (g_hProvider == 0)
  1079. {
  1080. g_hProvider = AdmtAcquireContext();
  1081. }
  1082. // destroy any existing session key
  1083. if (g_hSessionKey)
  1084. {
  1085. AdmtDestroyKey(g_hSessionKey);
  1086. g_hSessionKey = 0;
  1087. }
  1088. // import new session key
  1089. g_hSessionKey = AdmtImportSessionKey(g_hProvider, varSession);
  1090. // decrypt password
  1091. if (g_hSessionKey)
  1092. {
  1093. bstrPwd = AdmtDecrypt(g_hSessionKey, varPwd);
  1094. if (!bstrPwd)
  1095. return GetLastError();
  1096. }
  1097. else
  1098. return GetLastError();
  1099. //send back the decrypted password
  1100. if (bstrPwd.length() > 0)
  1101. {
  1102. wcsncpy(tempPwd, bstrPwd, PASSWORD_BUFFER_SIZE);
  1103. tempPwd[PASSWORD_BUFFER_SIZE - 1] = L'\0';
  1104. }
  1105. else
  1106. {
  1107. tempPwd[0] = L'\0';
  1108. }
  1109. }
  1110. catch (_com_error& ce)
  1111. {
  1112. return ce.Error();
  1113. }
  1114. catch (...)
  1115. {
  1116. return E_FAIL;
  1117. }
  1118. return rc;
  1119. }
  1120. //END CheckConfig