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.

3984 lines
117 KiB

  1. //=============================================================================
  2. // Copyright (c) 2000 Microsoft Corporation
  3. //
  4. // dialogs.cpp
  5. //
  6. // Credential manager user interface classes used to get credentials.
  7. //
  8. // Created 02/29/2000 johnstep (John Stephens)
  9. //=============================================================================
  10. #include "precomp.hpp"
  11. #include "shellapi.h"
  12. #include "dialogs.hpp"
  13. #include "resource.h"
  14. #include "utils.hpp"
  15. #include "shpriv.h"
  16. #include "shlobj.h"
  17. #include "shlobjp.h"
  18. // compile switch to allow credui to update wildcard creds
  19. #define SETWILDCARD
  20. NET_API_STATUS NetUserChangePasswordEy(LPCWSTR domainname, LPCWSTR username, LPCWSTR oldpassword, LPCWSTR newpassword);
  21. // .NET logo sizes
  22. #define BRANDLEFT_PIXEL_WIDTH 3
  23. #define BRANDLEFT_PIXEL_HEIGHT 4
  24. #define BRANDMID_PIXEL_HEIGHT 4
  25. #define BRANDRIGHT_PIXEL_WIDTH 144
  26. #define BRANDRIGHT_PIXEL_HEIGHT 37
  27. //-----------------------------------------------------------------------------
  28. // Values
  29. //-----------------------------------------------------------------------------
  30. #define CREDUI_MAX_WELCOME_TEXT_LINES 8
  31. #define CREDUI_MAX_LOGO_HEIGHT 80
  32. #define CREDUI_MAX_CMDLINE_MSG_LENGTH 256
  33. //-----------------------------------------------------------------------------
  34. // Global Variables
  35. //-----------------------------------------------------------------------------
  36. extern BOOL gbWaitingForSSOCreds;
  37. extern WCHAR gszSSOUserName[CREDUI_MAX_USERNAME_LENGTH];
  38. extern WCHAR gszSSOPassword[CREDUI_MAX_PASSWORD_LENGTH];
  39. // Balloon tip infos for change password dialog:
  40. CONST CREDUI_BALLOON_TIP_INFO CreduiWrongOldTipInfo =
  41. {
  42. CreduiStrings.WrongOldTipTitle,
  43. CreduiStrings.WrongOldTipText,
  44. TTI_ERROR, 96, 76
  45. };
  46. CONST CREDUI_BALLOON_TIP_INFO CreduiNotSameTipInfo =
  47. {
  48. CreduiStrings.NotSameTipTitle,
  49. CreduiStrings.NotSameTipText,
  50. TTI_ERROR, 96, 76
  51. };
  52. CONST CREDUI_BALLOON_TIP_INFO CreduiTooShortTipInfo =
  53. {
  54. CreduiStrings.TooShortTipTitle,
  55. CreduiStrings.TooShortTipText,
  56. TTI_ERROR, 96, 76
  57. };
  58. // Control balloons for password dialog:
  59. CONST CREDUI_BALLOON CreduiUserNameBalloon =
  60. {
  61. 1, CREDUI_CONTROL_USERNAME, CREDUI_BALLOON_ICON_INFO,
  62. CreduiStrings.UserNameTipTitle,
  63. CreduiStrings.UserNameTipText
  64. };
  65. CONST CREDUI_BALLOON CreduiEmailNameBalloon =
  66. {
  67. 1, CREDUI_CONTROL_USERNAME, CREDUI_BALLOON_ICON_INFO,
  68. CreduiStrings.EmailNameTipTitle,
  69. CreduiStrings.EmailNameTipText
  70. };
  71. CONST CREDUI_BALLOON CreduiDowngradeBalloon =
  72. {
  73. 1, CREDUI_CONTROL_USERNAME, CREDUI_BALLOON_ICON_ERROR,
  74. CreduiStrings.LogonTipTitle,
  75. CreduiStrings.DowngradeTipText
  76. };
  77. CONST CREDUI_BALLOON CreduiLogonBalloon =
  78. {
  79. 1, CREDUI_CONTROL_PASSWORD, CREDUI_BALLOON_ICON_ERROR,
  80. CreduiStrings.LogonTipTitle,
  81. CreduiStrings.LogonTipText
  82. };
  83. CONST CREDUI_BALLOON CreduiLogonCapsBalloon =
  84. {
  85. 1, CREDUI_CONTROL_PASSWORD, CREDUI_BALLOON_ICON_ERROR,
  86. CreduiStrings.LogonTipTitle,
  87. CreduiStrings.LogonTipCaps
  88. };
  89. // Placehold for known password:
  90. CONST WCHAR CreduiKnownPassword[] = L"********";
  91. //-----------------------------------------------------------------------------
  92. // Functions
  93. //-----------------------------------------------------------------------------
  94. void DemoteOldDefaultSSOCred (
  95. PCREDENTIAL_TARGET_INFORMATION pTargetInfo, // target info of new cred
  96. DWORD Flags
  97. );
  98. //=============================================================================
  99. // CreduiChangePasswordCallback
  100. //
  101. // This callback handles changing a domain password.
  102. //
  103. // Arguments:
  104. // changePasswordWindow (in)
  105. // message (in)
  106. // wParam (in)
  107. // lParam (in) - on WM_INITDIALOG, this is the info structure
  108. //
  109. // Returns TRUE if we handled the message, otherwise FALSE.
  110. //
  111. // Created 04/26/2000 johnstep (John Stephens)
  112. //=============================================================================
  113. INT_PTR
  114. CALLBACK
  115. CreduiChangePasswordCallback(
  116. HWND changePasswordWindow,
  117. UINT message,
  118. WPARAM wParam,
  119. LPARAM lParam
  120. )
  121. {
  122. CREDUI_CHANGE_PASSWORD_INFO *info =
  123. reinterpret_cast<CREDUI_CHANGE_PASSWORD_INFO *>(
  124. GetWindowLongPtr(changePasswordWindow, GWLP_USERDATA));
  125. if (message == WM_INITDIALOG)
  126. {
  127. info = reinterpret_cast<CREDUI_CHANGE_PASSWORD_INFO *>(lParam);
  128. if (info != NULL)
  129. {
  130. // Store this object's pointer in the user data window long:
  131. SetWindowLongPtr(changePasswordWindow,
  132. GWLP_USERDATA,
  133. reinterpret_cast<LONG_PTR>(info));
  134. SetWindowText(
  135. GetDlgItem(changePasswordWindow, IDC_USERNAME),
  136. info->UserName);
  137. info->BalloonTip.Init(CreduiInstance, changePasswordWindow);
  138. info->OldPasswordBox.Init(
  139. GetDlgItem(changePasswordWindow, IDC_PASSWORD),
  140. &info->BalloonTip,
  141. &CreduiCapsLockTipInfo);
  142. info->NewPasswordBox.Init(
  143. GetDlgItem(changePasswordWindow, IDC_NEW_PASSWORD),
  144. &info->BalloonTip,
  145. &CreduiCapsLockTipInfo);
  146. info->ConfirmPasswordBox.Init(
  147. GetDlgItem(changePasswordWindow, IDC_CONFIRM_PASSWORD),
  148. &info->BalloonTip,
  149. &CreduiCapsLockTipInfo);
  150. return TRUE;
  151. }
  152. else
  153. {
  154. EndDialog(changePasswordWindow, IDCANCEL);
  155. }
  156. }
  157. else switch (message)
  158. {
  159. case WM_COMMAND:
  160. switch (LOWORD(wParam))
  161. {
  162. case IDC_PASSWORD:
  163. case IDC_NEW_PASSWORD:
  164. if ((HIWORD(wParam) == EN_KILLFOCUS) ||
  165. (HIWORD(wParam) == EN_CHANGE))
  166. {
  167. if (info->BalloonTip.IsVisible())
  168. {
  169. info->BalloonTip.Hide();
  170. }
  171. }
  172. break;
  173. case IDOK:
  174. WCHAR userDomain[CRED_MAX_USERNAME_LENGTH + 1];
  175. WCHAR userName[CRED_MAX_USERNAME_LENGTH + 1];
  176. WCHAR oldPassword[CRED_MAX_STRING_LENGTH + 1];
  177. WCHAR newPassword[CRED_MAX_STRING_LENGTH + 1];
  178. WCHAR confirmPassword[CRED_MAX_STRING_LENGTH + 1];
  179. oldPassword[0] = L'\0';
  180. newPassword[0] = L'\0';
  181. confirmPassword[0] = L'\0';
  182. GetWindowText(
  183. GetDlgItem(changePasswordWindow, IDC_NEW_PASSWORD),
  184. newPassword,
  185. CRED_MAX_STRING_LENGTH);
  186. GetWindowText(
  187. GetDlgItem(changePasswordWindow, IDC_CONFIRM_PASSWORD),
  188. confirmPassword,
  189. CRED_MAX_STRING_LENGTH);
  190. if (lstrcmp(newPassword, confirmPassword) != 0)
  191. {
  192. SetWindowText(
  193. GetDlgItem(changePasswordWindow, IDC_NEW_PASSWORD),
  194. NULL);
  195. SetWindowText(
  196. GetDlgItem(changePasswordWindow, IDC_CONFIRM_PASSWORD),
  197. NULL);
  198. info->BalloonTip.SetInfo(
  199. GetDlgItem(changePasswordWindow, IDC_NEW_PASSWORD),
  200. &CreduiNotSameTipInfo);
  201. info->BalloonTip.Show();
  202. ZeroMemory( newPassword, sizeof(newPassword) );
  203. ZeroMemory( confirmPassword, sizeof(confirmPassword) );
  204. break;
  205. }
  206. // Confirm password is no longer needed
  207. ZeroMemory( confirmPassword, sizeof(confirmPassword) );
  208. GetWindowText(
  209. GetDlgItem(changePasswordWindow, IDC_PASSWORD),
  210. oldPassword,
  211. CRED_MAX_STRING_LENGTH);
  212. if (CredUIParseUserName(
  213. info->UserName,
  214. userName,
  215. sizeof(userName)/sizeof(WCHAR),
  216. userDomain,
  217. sizeof(userDomain)/sizeof(WCHAR)) == ERROR_SUCCESS)
  218. {
  219. NET_API_STATUS netStatus =
  220. NetUserChangePasswordEy(userDomain,
  221. userName,
  222. oldPassword,
  223. newPassword);
  224. // Old password is no longer needed
  225. ZeroMemory(oldPassword, sizeof oldPassword);
  226. switch (netStatus)
  227. {
  228. case NERR_Success:
  229. // Once we make it this far, the password has been
  230. // changed. If the following call fails to update the
  231. // credential, we really cannot do much about it except
  232. // maybe notify the user.
  233. lstrcpyn(info->Password,
  234. newPassword,
  235. info->PasswordMaxChars+1);
  236. // Scrub the passwords on the stack and clear the
  237. // controls:
  238. ZeroMemory(newPassword, sizeof newPassword);
  239. // NOTE: We may want to first set the controls to some
  240. // pattern to fill the memory, then clear it.
  241. SetWindowText(
  242. GetDlgItem(changePasswordWindow, IDC_PASSWORD),
  243. NULL);
  244. SetWindowText(
  245. GetDlgItem(changePasswordWindow, IDC_NEW_PASSWORD),
  246. NULL);
  247. SetWindowText(
  248. GetDlgItem(changePasswordWindow,
  249. IDC_CONFIRM_PASSWORD),
  250. NULL);
  251. // NOTE: We may want to notify the user that the password
  252. // has been successfully changed.
  253. break;
  254. default:
  255. // NOTE: If we got an unknown error, just handle it the
  256. // same way as an invalid password, for now:
  257. case ERROR_INVALID_PASSWORD:
  258. // New password is no longer needed
  259. ZeroMemory(newPassword, sizeof newPassword);
  260. SetWindowText(
  261. GetDlgItem(changePasswordWindow, IDC_PASSWORD),
  262. NULL);
  263. info->BalloonTip.SetInfo(
  264. GetDlgItem(changePasswordWindow, IDC_PASSWORD),
  265. &CreduiWrongOldTipInfo);
  266. info->BalloonTip.Show();
  267. return TRUE;
  268. case ERROR_ACCESS_DENIED:
  269. // NOTE: One use of this return value is when the new
  270. // password and old are the same. Would any
  271. // configuration ever allow them to match? If not,
  272. // we could just compare before calling the API.
  273. case NERR_PasswordTooShort:
  274. // New password is no longer needed
  275. ZeroMemory(newPassword, sizeof newPassword);
  276. SetWindowText(
  277. GetDlgItem(changePasswordWindow, IDC_NEW_PASSWORD),
  278. NULL);
  279. SetWindowText(
  280. GetDlgItem(changePasswordWindow,
  281. IDC_CONFIRM_PASSWORD),
  282. NULL);
  283. info->BalloonTip.SetInfo(
  284. GetDlgItem(changePasswordWindow, IDC_NEW_PASSWORD),
  285. &CreduiTooShortTipInfo);
  286. info->BalloonTip.Show();
  287. return TRUE;
  288. }
  289. }
  290. else
  291. {
  292. break;
  293. }
  294. // Fall through...
  295. case IDCANCEL:
  296. EndDialog(changePasswordWindow, LOWORD(wParam));
  297. return TRUE;
  298. }
  299. break;
  300. }
  301. return FALSE;
  302. }
  303. //=============================================================================
  304. // CreduiChangeDomainPassword
  305. //
  306. // Displays a dialog allowing the user to change the domain password.
  307. //
  308. // Arguments:
  309. // parentWindow (in)
  310. // userName (in)
  311. // password (out)
  312. // passwordMaxChars (in) - on WM_INITDIALOG, this is the info structure
  313. //
  314. // Returns TRUE if we handled the message, otherwise FALSE.
  315. //
  316. // Created 06/06/2000 johnstep (John Stephens)
  317. //=============================================================================
  318. BOOL
  319. CreduiChangeDomainPassword(
  320. HWND parentWindow,
  321. CONST WCHAR *userName,
  322. WCHAR *password,
  323. ULONG passwordMaxChars
  324. )
  325. {
  326. CREDUI_CHANGE_PASSWORD_INFO info;
  327. info.UserName = userName;
  328. info.Password = password;
  329. info.PasswordMaxChars = passwordMaxChars;
  330. return
  331. DialogBoxParam(
  332. CreduiInstance,
  333. MAKEINTRESOURCE(IDD_CHANGE_PASSWORD),
  334. parentWindow,
  335. CreduiChangePasswordCallback,
  336. reinterpret_cast<LPARAM>(&info)) == IDOK;
  337. }
  338. //-----------------------------------------------------------------------------
  339. // CreduiPasswordDialog Class Implementation
  340. //-----------------------------------------------------------------------------
  341. LONG CreduiPasswordDialog::Registered = FALSE;
  342. //=============================================================================
  343. // CreduiPasswordDialog::SetCredTargetFromInfo()
  344. //
  345. // Created 04/03/2001 georgema
  346. //=============================================================================
  347. void CreduiPasswordDialog::SetCredTargetFromInfo()
  348. {
  349. BOOL serverOnly = TRUE;
  350. NewCredential.Type = (CredCategory == GENERIC_CATEGORY) ?
  351. CRED_TYPE_GENERIC :
  352. CRED_TYPE_DOMAIN_PASSWORD;
  353. if ( TargetInfo != NULL )
  354. {
  355. if ( TargetInfo->CredTypeCount == 1 && *(TargetInfo->CredTypes) == CRED_TYPE_DOMAIN_VISIBLE_PASSWORD ) {
  356. NewCredential.Type = CRED_TYPE_DOMAIN_VISIBLE_PASSWORD;
  357. MaximumPersist = MaximumPersistSso;
  358. serverOnly = (Flags & CREDUI_FLAGS_SERVER_CREDENTIAL);
  359. }
  360. }
  361. if ( CredCategory == DOMAIN_CATEGORY &&
  362. (TargetInfo != NULL))
  363. {
  364. SelectBestTargetName(serverOnly);
  365. NewCredential.TargetName = NewTargetName;
  366. }
  367. else
  368. {
  369. NewCredential.TargetName = const_cast<WCHAR *>(UserOrTargetName);
  370. }
  371. }
  372. //=============================================================================
  373. // CreduiPasswordDialog::CreduiPasswordDialog
  374. //
  375. // Created 02/25/2000 johnstep (John Stephens)
  376. //=============================================================================
  377. CreduiPasswordDialog::CreduiPasswordDialog(
  378. IN BOOL doingCommandLine,
  379. IN BOOL delayCredentialWrite,
  380. IN DWORD credCategory,
  381. CREDUI_INFO *uiInfo,
  382. CONST WCHAR *targetName,
  383. WCHAR *userName,
  384. ULONG userNameMaxChars,
  385. WCHAR *password,
  386. ULONG passwordMaxChars,
  387. BOOL *save,
  388. DWORD flags,
  389. CtxtHandle *securityContext,
  390. DWORD authError,
  391. DWORD *result
  392. )
  393. /*++
  394. Routine Description:
  395. This routine implements the GUI prompt for credentials
  396. Arguments:
  397. DoingCommandLine - TRUE if prompting is to be done via the command line
  398. FALSE if prompting is to be done via GUI
  399. delayCredentialWrite - TRUE if the credential is to be written only upon confirmation.
  400. FALSE, if the credential is to be written now as a session credential then
  401. morphed to a more persistent credential upon confirmation.
  402. This field is ignored if Flags doesn't specify CREDUI_FLAGS_EXPECT_CONFIRMATION.
  403. credCategory - This is the subset of the "flags" parameter defining the category of
  404. the credential.
  405. ...
  406. Return Values:
  407. None
  408. --*/
  409. {
  410. Result = ERROR_CANCELLED;
  411. hBrandBmp = NULL;
  412. // Initialize the result out argument for failure:
  413. if ( result != NULL )
  414. *result = Result;
  415. // Turn on the following chatter always for debug builds
  416. #if DBG
  417. // ensure that we don't miss uninitialized members in test...
  418. memset(this,0xcc,sizeof(CreduiPasswordDialog));
  419. OutputDebugString(L"CreduiPasswordDialog: Constructor\n" );
  420. OutputDebugString(L"Incoming targetname = ");
  421. if (targetName) OutputDebugString(targetName);
  422. OutputDebugString(L"\n");
  423. OutputDebugString(L"Incoming username = ");
  424. if (userName) OutputDebugString(userName);
  425. OutputDebugString(L"\n");
  426. #endif
  427. CREDENTIAL **credentialSet = NULL;
  428. SecPkgContext_CredentialName credentialName = { 0, NULL };
  429. ZeroMemory(&NewCredential, sizeof(NewCredential));
  430. fPassedUsername = FALSE;
  431. fPasswordOnly = FALSE;
  432. //FIX 399728
  433. if ((userName != NULL) &&
  434. (wcslen(userName) != 0)) fPassedUsername = TRUE;
  435. // Set most of the class members to valid initial values. The window
  436. // handles will be initialized later if everything succeeds:
  437. DoingCommandLine = doingCommandLine;
  438. DelayCredentialWrite = delayCredentialWrite;
  439. CredCategory = credCategory;
  440. UserName = userName;
  441. UserNameMaxChars = userNameMaxChars;
  442. Password = password;
  443. PasswordMaxChars = passwordMaxChars;
  444. Flags = flags;
  445. AuthError = authError;
  446. Save = (Flags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) ? *save : FALSE;
  447. rcBrand.top = 0;
  448. rcBrand.left = 0;
  449. rcBrand.right = 0;
  450. rcBrand.bottom = 0;
  451. if (targetName != NULL)
  452. {
  453. TargetName = const_cast<WCHAR *>(targetName);
  454. SecurityContext = NULL;
  455. }
  456. else if ( CredCategory == DOMAIN_CATEGORY )
  457. {
  458. if( securityContext == NULL) {
  459. CreduiDebugLog("CreduiPasswordDialog: Need to pass target name for domain creds.\n" );
  460. if ( result != NULL )
  461. *result = ERROR_INVALID_PARAMETER;
  462. return;
  463. }
  464. SecurityContext = securityContext;
  465. // Get the credential name, which includes the type, from the
  466. // security context:
  467. if (QueryContextAttributes(
  468. SecurityContext,
  469. SECPKG_ATTR_CREDENTIAL_NAME,
  470. static_cast<VOID *>(&credentialName)) != SEC_E_OK)
  471. {
  472. // This is an invalid security context for this function:
  473. CreduiDebugLog("CreduiPasswordDialog: Cannot QueryContextAttributes.\n" );
  474. if ( result != NULL )
  475. *result = ERROR_INVALID_PARAMETER;
  476. return;
  477. }
  478. TargetName = credentialName.sCredentialName;
  479. }
  480. else TargetName = NULL;
  481. if (uiInfo != NULL)
  482. {
  483. UiInfo = *uiInfo;
  484. }
  485. else
  486. {
  487. UiInfo.cbSize = sizeof(UiInfo);
  488. UiInfo.hwndParent = NULL;
  489. UiInfo.pszMessageText = NULL;
  490. UiInfo.pszCaptionText = NULL;
  491. UiInfo.hbmBanner = NULL;
  492. }
  493. PasswordCredential = NULL;
  494. OldCredential = NULL;
  495. TargetInfo = NULL;
  496. EncryptedVisiblePassword = TRUE;
  497. FirstPaint = TRUE;
  498. CredBalloon = NULL;
  499. NewTargetName[0] = L'\0';
  500. NewTargetAlias[0] = L'\0';
  501. DisabledControlMask = 0;
  502. PasswordState = PASSWORD_UNINIT;
  503. ResizeTop = 0;
  504. ResizeDelta = 0;
  505. DWORD MaximumPersistArray[CRED_TYPE_MAXIMUM];
  506. if ( LocalCredGetSessionTypes( CRED_TYPE_MAXIMUM, MaximumPersistArray)) {
  507. //
  508. // Maintain "MaximumPersist" as the maximum persistence to use.
  509. // We don't know yet whether this is an SSO cred, so keep it in a separate location
  510. // until we know for sure.
  511. //
  512. // Be careful to not use MaximumPersist until we do know for sure
  513. //
  514. if ( CredCategory == DOMAIN_CATEGORY || CredCategory == USERNAME_TARGET_CATEGORY ) {
  515. MaximumPersist = MaximumPersistArray[CRED_TYPE_DOMAIN_PASSWORD];
  516. } else {
  517. MaximumPersist = MaximumPersistArray[CRED_TYPE_GENERIC];
  518. }
  519. MaximumPersistSso = MaximumPersistArray[CRED_TYPE_DOMAIN_VISIBLE_PASSWORD];
  520. } else {
  521. MaximumPersist = CRED_PERSIST_NONE;
  522. MaximumPersistSso = CRED_PERSIST_NONE;
  523. }
  524. fInitialSaveState = FALSE;
  525. //
  526. // USERNAME_TARGET creds have two concepts of TargetName
  527. // The target name on the peristed credential needs to be the UserName
  528. // The target name everywhere else is the prompt text
  529. // UserOrTargetName is used in all the former cases.
  530. if ( CredCategory == USERNAME_TARGET_CATEGORY ) {
  531. UserOrTargetName = UserName;
  532. } else {
  533. UserOrTargetName = TargetName;
  534. }
  535. //
  536. // Get the target information for the target
  537. //
  538. // Only attempt to get target information if the target name is not a wildcard name.
  539. //
  540. // Do this regardless of whether the credential will be persisted.
  541. // Target info is used for completing the username.
  542. //
  543. if ( CredCategory == DOMAIN_CATEGORY &&
  544. SecurityContext == NULL &&
  545. !CreduiIsWildcardTargetName(TargetName) &&
  546. LocalCredGetTargetInfoW( TargetName,
  547. CRED_ALLOW_NAME_RESOLUTION,
  548. &TargetInfo) )
  549. {
  550. //
  551. // Check out the target info to ensure it matches the flag bits
  552. // passed by the caller.
  553. //
  554. // If not, ignore the target info.
  555. // We'll assume that the caller is using a different auth package than
  556. // the one matching the cached info.
  557. //
  558. if ( TargetInfo->CredTypeCount != 0 ) {
  559. ULONG AuthPackageStyle;
  560. BOOL CertOk = FALSE;
  561. BOOL PasswordOk = FALSE;
  562. AuthPackageStyle = 0;
  563. //
  564. // Loop through the supported cred types seeing what style the auth package supports
  565. //
  566. for (UINT i = 0; i < TargetInfo->CredTypeCount; ++i)
  567. {
  568. switch ( TargetInfo->CredTypes[i] ) {
  569. case CRED_TYPE_DOMAIN_CERTIFICATE:
  570. CertOk = TRUE;
  571. break;
  572. case CRED_TYPE_DOMAIN_PASSWORD:
  573. case CRED_TYPE_DOMAIN_VISIBLE_PASSWORD:
  574. PasswordOk = TRUE;
  575. break;
  576. }
  577. }
  578. //
  579. // Adjust that for what the caller requires
  580. //
  581. if (Flags & (CREDUI_FLAGS_REQUIRE_SMARTCARD|CREDUI_FLAGS_REQUIRE_CERTIFICATE) ) {
  582. PasswordOk = FALSE;
  583. } else if (Flags & CREDUI_FLAGS_EXCLUDE_CERTIFICATES) {
  584. CertOk = FALSE;
  585. }
  586. //
  587. // If nothing to supported,
  588. // ignore the target info
  589. //
  590. if ( !CertOk && !PasswordOk ) {
  591. LocalCredFree(static_cast<VOID *>(TargetInfo));
  592. TargetInfo = NULL;
  593. }
  594. }
  595. }
  596. //
  597. // If the credential might be peristed,
  598. // determine the existing credential and
  599. // build a template for the credential to be persisted.
  600. //
  601. if (!(Flags & CREDUI_FLAGS_DO_NOT_PERSIST))
  602. {
  603. // Read the existing credential for this target:
  604. CREDENTIAL *credential = NULL;
  605. Result = ERROR_SUCCESS;
  606. if ( CredCategory == GENERIC_CATEGORY )
  607. {
  608. if (LocalCredReadW(TargetName,
  609. CRED_TYPE_GENERIC,
  610. 0,
  611. &PasswordCredential))
  612. {
  613. OldCredential = PasswordCredential;
  614. }
  615. else
  616. {
  617. Result = GetLastError();
  618. }
  619. }
  620. else
  621. {
  622. DWORD count;
  623. //
  624. // If a TargetInfo was found,
  625. // use it to read the matching credentials.
  626. //
  627. if ( TargetInfo != NULL ) {
  628. if (LocalCredReadDomainCredentialsW(TargetInfo, 0, &count,
  629. &credentialSet))
  630. {
  631. for (DWORD i = 0; i < count; ++i)
  632. {
  633. #ifndef SETWILDCARD
  634. //
  635. // Ignore RAS and wildcard credentials,
  636. // we never want credUI to change such a credential.
  637. //
  638. if ( CreduiIsSpecialCredential(credentialSet[i]) ) {
  639. continue;
  640. }
  641. #endif
  642. //
  643. // If the caller needs a server credential,
  644. // ignore wildcard credentials.
  645. //
  646. if ((Flags & CREDUI_FLAGS_SERVER_CREDENTIAL) &&
  647. CreduiIsWildcardTargetName( credentialSet[i]->TargetName)) {
  648. continue;
  649. }
  650. //
  651. // If the caller wants a certificate,
  652. // ignore non certificates.
  653. //
  654. if ( Flags & (CREDUI_FLAGS_REQUIRE_CERTIFICATE|CREDUI_FLAGS_REQUIRE_SMARTCARD) ) {
  655. if ( credentialSet[i]->Type != CRED_TYPE_DOMAIN_CERTIFICATE ) {
  656. continue;
  657. }
  658. }
  659. //
  660. // If the caller wants to avoid certificates,
  661. // ignore certificates.
  662. //
  663. if ( Flags & CREDUI_FLAGS_EXCLUDE_CERTIFICATES ) {
  664. if ( credentialSet[i]->Type == CRED_TYPE_DOMAIN_CERTIFICATE ) {
  665. continue;
  666. }
  667. }
  668. //
  669. // CredReadDomain domain credentials returns creds in preference
  670. // order as specified by the TargetInfo.
  671. // So use the first valid one.
  672. //
  673. if ( OldCredential == NULL ) {
  674. OldCredential = credentialSet[i];
  675. }
  676. //
  677. // Remember the PasswordCredential in case we need to fall back to it
  678. //
  679. if ( credentialSet[i]->Type == CRED_TYPE_DOMAIN_PASSWORD ) {
  680. PasswordCredential = credentialSet[i];
  681. }
  682. }
  683. if (OldCredential == NULL)
  684. {
  685. Result = ERROR_NOT_FOUND;
  686. }
  687. else
  688. {
  689. Result = ERROR_SUCCESS;
  690. }
  691. }
  692. else
  693. {
  694. Result = GetLastError();
  695. }
  696. }
  697. //
  698. // We don't have a target info
  699. // read each of the possible credential types
  700. //
  701. else
  702. {
  703. if (!(Flags & CREDUI_FLAGS_EXCLUDE_CERTIFICATES) &&
  704. ((SecurityContext == NULL) ||
  705. (credentialName.CredentialType ==
  706. CRED_TYPE_DOMAIN_CERTIFICATE)) &&
  707. *UserOrTargetName != '\0' &&
  708. LocalCredReadW(UserOrTargetName,
  709. CRED_TYPE_DOMAIN_CERTIFICATE,
  710. 0,
  711. &credential))
  712. {
  713. if (CreduiIsSpecialCredential(credential))
  714. {
  715. LocalCredFree(static_cast<VOID *>(credential));
  716. credential = NULL;
  717. }
  718. else
  719. {
  720. OldCredential = credential;
  721. }
  722. }
  723. if ( ( Flags & (CREDUI_FLAGS_REQUIRE_CERTIFICATE|CREDUI_FLAGS_REQUIRE_SMARTCARD)) == 0 ) {
  724. if ( OldCredential == NULL &&
  725. ((SecurityContext == NULL) ||
  726. (credentialName.CredentialType ==
  727. CRED_TYPE_DOMAIN_PASSWORD)) &&
  728. *UserOrTargetName != '\0' &&
  729. LocalCredReadW(UserOrTargetName,
  730. CRED_TYPE_DOMAIN_PASSWORD,
  731. 0,
  732. &credential))
  733. {
  734. if (CreduiIsSpecialCredential(credential))
  735. {
  736. LocalCredFree(static_cast<VOID *>(credential));
  737. credential = NULL;
  738. }
  739. else
  740. {
  741. PasswordCredential = credential;
  742. OldCredential = credential;
  743. Result = ERROR_SUCCESS;
  744. }
  745. }
  746. if ( OldCredential == NULL &&
  747. ((SecurityContext == NULL) ||
  748. (credentialName.CredentialType ==
  749. CRED_TYPE_DOMAIN_VISIBLE_PASSWORD)) &&
  750. *UserOrTargetName != '\0' &&
  751. LocalCredReadW(UserOrTargetName,
  752. CRED_TYPE_DOMAIN_VISIBLE_PASSWORD,
  753. 0,
  754. &credential))
  755. {
  756. if (CreduiIsSpecialCredential(credential))
  757. {
  758. LocalCredFree(static_cast<VOID *>(credential));
  759. credential = NULL;
  760. }
  761. else
  762. {
  763. OldCredential = credential;
  764. Result = ERROR_SUCCESS;
  765. }
  766. }
  767. }
  768. if (OldCredential == NULL)
  769. {
  770. Result = GetLastError();
  771. }
  772. else
  773. {
  774. fInitialSaveState = TRUE;
  775. Result = ERROR_SUCCESS;
  776. }
  777. }
  778. }
  779. if (Result == ERROR_SUCCESS)
  780. {
  781. NewCredential = *OldCredential;
  782. // if we have an existing cred, set the save state
  783. if (OldCredential != NULL)
  784. {
  785. fInitialSaveState = TRUE;
  786. }
  787. // If a user name was not passed, copy the user name and password
  788. // from the existing credential:
  789. if (UserName[0] == L'\0')
  790. {
  791. if (OldCredential->UserName != NULL)
  792. {
  793. lstrcpyn(UserName,
  794. OldCredential->UserName,
  795. UserNameMaxChars + 1);
  796. }
  797. if (Password[0] == L'\0')
  798. {
  799. if ((OldCredential->Type == CRED_TYPE_GENERIC) )
  800. {
  801. CopyMemory(
  802. Password,
  803. OldCredential->CredentialBlob,
  804. OldCredential->CredentialBlobSize
  805. );
  806. Password[OldCredential->
  807. CredentialBlobSize >> 1] = L'\0';
  808. }
  809. else if (OldCredential->Type == CRED_TYPE_DOMAIN_VISIBLE_PASSWORD)
  810. {
  811. // check to see if the new one should be encrypted, but in any case, we can't prefill
  812. // the password box
  813. EncryptedVisiblePassword = IsPasswordEncrypted(OldCredential->CredentialBlob,
  814. OldCredential->CredentialBlobSize);
  815. PasswordState = PASSWORD_CHANGED;
  816. }
  817. else
  818. {
  819. // If prompt is required now, or if we're rerturning the password, then we can't prefill the
  820. // password box:
  821. fInitialSaveState = TRUE;
  822. if (OldCredential->Flags & CRED_FLAGS_PROMPT_NOW || DelayCredentialWrite )
  823. {
  824. PasswordState = PASSWORD_CHANGED;
  825. }
  826. else
  827. {
  828. lstrcpyn(Password,
  829. CreduiKnownPassword,
  830. PasswordMaxChars + 1);
  831. }
  832. }
  833. }
  834. }
  835. }
  836. else
  837. {
  838. if (Result != ERROR_NO_SUCH_LOGON_SESSION)
  839. {
  840. Result = ERROR_SUCCESS;
  841. }
  842. OldCredential = NULL;
  843. SetCredTargetFromInfo();
  844. PasswordState = PASSWORD_CHANGED;
  845. }
  846. NewCredential.UserName = UserName;
  847. NewCredential.CredentialBlob = reinterpret_cast<BYTE *>(Password);
  848. //
  849. // Since the old cred is an SSO cred,
  850. // use the SSO maximum persistence.
  851. //
  852. // Wait till now since NewCredential.Type is updated from either the old credential
  853. // or from the target info above.
  854. //
  855. // BUGBUG: If there is no old credential or target info,
  856. // we won't know this until later.
  857. //
  858. if ( NewCredential.Type == CRED_TYPE_DOMAIN_VISIBLE_PASSWORD ) {
  859. MaximumPersist = MaximumPersistSso;
  860. }
  861. //
  862. // If the session type isn't supported on this machine,
  863. // fail if the caller needs to save the credential.
  864. //
  865. if ( MaximumPersist == CRED_PERSIST_NONE ) {
  866. if ( DelayCredentialWrite )
  867. {
  868. // no credmgr available, and user asked to persist. We can't persist, but we
  869. // can continue with UI and return values via the api - add CREDUI_FLAGS_DO_NOT_PERSIST
  870. Flags |= CREDUI_FLAGS_DO_NOT_PERSIST;
  871. }
  872. else
  873. {
  874. // we can't do anything without credmgr, return error
  875. if ( result != NULL )
  876. *result = ERROR_NO_SUCH_LOGON_SESSION;
  877. return;
  878. }
  879. }
  880. }
  881. //
  882. // If the cred may not be persisted,
  883. // clear the NewCredential.
  884. //
  885. if ( Flags & CREDUI_FLAGS_DO_NOT_PERSIST)
  886. {
  887. // NEED TO Initialize NewCred here
  888. ZeroMemory(&NewCredential, sizeof NewCredential);
  889. SetCredTargetFromInfo();
  890. PasswordState = PASSWORD_CHANGED;
  891. NewCredential.UserName = UserName;
  892. NewCredential.CredentialBlob = reinterpret_cast<BYTE *>(Password);
  893. Result = ERROR_SUCCESS;
  894. }
  895. if (Result == ERROR_SUCCESS)
  896. {
  897. HWND parentWindow = UiInfo.hwndParent;
  898. CreduiIconParentWindow iconWindow;
  899. if ((parentWindow == NULL) || !IsWindow(parentWindow))
  900. {
  901. if (iconWindow.Init(CreduiInstance, IDI_DEFAULT))
  902. {
  903. parentWindow = iconWindow.GetWindow();
  904. }
  905. }
  906. BOOL doPasswordDialog = TRUE;
  907. // Before doing the dialog, check for special errors:
  908. if ( CredCategory == DOMAIN_CATEGORY &&
  909. !DoingCommandLine &&
  910. CREDUIP_IS_EXPIRED_ERROR( authError ) &&
  911. (OldCredential != NULL))
  912. {
  913. if (CreduiChangeDomainPassword(
  914. parentWindow,
  915. UserName,
  916. Password,
  917. PasswordMaxChars))
  918. {
  919. doPasswordDialog = FALSE;
  920. // Attempt to write the new credential, first get the length.
  921. // The blob does not include the terminating NULL:
  922. NewCredential.CredentialBlobSize =
  923. lstrlen(Password) * sizeof (WCHAR);
  924. // If the password is empty, do not write a credential blob:
  925. if (NewCredential.CredentialBlobSize == 0)
  926. {
  927. NewCredential.CredentialBlob = NULL;
  928. }
  929. else
  930. {
  931. NewCredential.CredentialBlob =
  932. reinterpret_cast<BYTE *>(Password);
  933. }
  934. Result = FinishHandleOk();
  935. }
  936. }
  937. // Check to see if we can skip the UI:
  938. if ( CredCategory == GENERIC_CATEGORY &&
  939. !(Flags & CREDUI_FLAGS_ALWAYS_SHOW_UI) &&
  940. (OldCredential != NULL) &&
  941. !(OldCredential->Flags & CRED_FLAGS_PROMPT_NOW))
  942. {
  943. doPasswordDialog = FALSE;
  944. if ((Flags & CREDUI_FLAGS_REQUIRE_CERTIFICATE) &&
  945. !LocalCredIsMarshaledCredentialW(OldCredential->UserName))
  946. {
  947. doPasswordDialog = TRUE;
  948. }
  949. if ((Flags & CREDUI_FLAGS_EXCLUDE_CERTIFICATES) &&
  950. LocalCredIsMarshaledCredentialW(OldCredential->UserName))
  951. {
  952. doPasswordDialog = TRUE;
  953. }
  954. }
  955. // Do the dialog box:
  956. // check to see if this is an SSO cred
  957. if ( GetSSOPackageInfo( TargetInfo, &SSOPackage ) )
  958. {
  959. // it's an sso cred
  960. dwIDDResource = IDD_BRANDEDPASSWORD;
  961. // we never set initial save state on these
  962. fInitialSaveState = FALSE;
  963. // check to see if we already have a cred for this
  964. if (!CheckForSSOCred( NULL ))
  965. {
  966. // check to see if we should run the wizard
  967. if ( !(SSOPackage.dwRegistrationCompleted) && (SSOPackage.dwNumRegistrationRuns < 5) )
  968. {
  969. doPasswordDialog = !TryLauchRegWizard ( &SSOPackage, UiInfo.hwndParent, (MaximumPersistSso > CRED_PERSIST_SESSION),
  970. userName, userNameMaxChars,
  971. password, passwordMaxChars,
  972. &Result );
  973. }
  974. }
  975. }
  976. else
  977. {
  978. // it's not an sso cred
  979. dwIDDResource = IDD_PASSWORD;
  980. }
  981. // save a copy of the current working user name for later
  982. if (NewCredential.UserName != NULL)
  983. {
  984. wcscpy(OldUserName,NewCredential.UserName);
  985. }
  986. else
  987. {
  988. OldUserName[0] = 0;
  989. }
  990. if (doPasswordDialog)
  991. {
  992. if ( DoingCommandLine )
  993. {
  994. //
  995. // Do the command line version of a dialog box
  996. //
  997. Result = CmdLineDialog();
  998. }
  999. else
  1000. {
  1001. LinkWindow_RegisterClass();
  1002. if (DialogBoxParam(
  1003. CreduiInstance,
  1004. MAKEINTRESOURCE(dwIDDResource),
  1005. parentWindow,
  1006. DialogMessageHandlerCallback,
  1007. reinterpret_cast<LPARAM>(this)) == IDOK)
  1008. {
  1009. if ((Result != ERROR_SUCCESS) &&
  1010. (Result != ERROR_NO_SUCH_LOGON_SESSION))
  1011. {
  1012. Result = ERROR_CANCELLED;
  1013. }
  1014. }
  1015. else
  1016. {
  1017. Result = ERROR_CANCELLED;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. else
  1023. {
  1024. Result = ERROR_NO_SUCH_LOGON_SESSION;
  1025. }
  1026. // kill outbound username if share-level access
  1027. if (fPasswordOnly)
  1028. {
  1029. CreduiDebugLog("CUIPD: Share level credentials\n");
  1030. userName[0] = 0;
  1031. }
  1032. // Make sure other processes can set foreground window once again:
  1033. LockSetForegroundWindow(LSFW_UNLOCK);
  1034. AllowSetForegroundWindow(ASFW_ANY);
  1035. if (TargetInfo != NULL)
  1036. {
  1037. LocalCredFree(static_cast<VOID *>(TargetInfo));
  1038. }
  1039. // If we read a credential set, PasswordCredential
  1040. // is a pointer into this set. Otherwise, they were
  1041. // read separately:
  1042. if (credentialSet != NULL)
  1043. {
  1044. LocalCredFree(static_cast<VOID *>(credentialSet));
  1045. }
  1046. else
  1047. {
  1048. if (PasswordCredential != NULL)
  1049. {
  1050. LocalCredFree(static_cast<VOID *>(PasswordCredential));
  1051. }
  1052. }
  1053. if ( result != NULL )
  1054. *result = Result;
  1055. if ( save != NULL &&
  1056. (Result == ERROR_SUCCESS))
  1057. {
  1058. *save = Save;
  1059. }
  1060. }
  1061. //=============================================================================
  1062. // CreduiPasswordDialog::~CreduiPasswordDialog
  1063. //
  1064. // The constructor cleans up after itself, and since that is the only way to
  1065. // use this class, there's nothing to do in the destructor.
  1066. //
  1067. // Created 02/25/2000 johnstep (John Stephens)
  1068. //=============================================================================
  1069. CreduiPasswordDialog::~CreduiPasswordDialog()
  1070. {
  1071. WCHAR ClassName[] = {L"CreduiCmdLineHelperWindow"};
  1072. LONG lRet;
  1073. if ( hBrandBmp != NULL )
  1074. DeleteObject ( hBrandBmp );
  1075. // If 'Registered' is nonzero, unregister the class
  1076. if (InterlockedCompareExchange(&Registered, FALSE, TRUE))
  1077. {
  1078. lRet = UnregisterClass(ClassName, 0);
  1079. // Put the class handle back if deregister failed
  1080. if (0 == lRet)
  1081. {
  1082. // If the unregister fails, allow other instances to retry the unregister
  1083. CreduiDebugLog( "CreduiPasswordDialog::~CreduiPasswordDialog UnregisterClass failed.\n" );
  1084. // put back the flag value that we had destroyed
  1085. InterlockedExchange(&Registered,TRUE);
  1086. }
  1087. }
  1088. }
  1089. #define MAX_TEMP_TARGETNAME 64
  1090. DWORD
  1091. CreduiPasswordDialog::CmdLineDialog(
  1092. VOID
  1093. )
  1094. /*++
  1095. Routine Description:
  1096. This routine implements the command line prompt for credentials
  1097. Arguments:
  1098. None
  1099. Return Values:
  1100. Win 32 status of the operation
  1101. --*/
  1102. {
  1103. DWORD WinStatus;
  1104. HWND Window;
  1105. MSG msg;
  1106. //
  1107. // Create the window class if it doesn't already exist
  1108. //
  1109. if (!InterlockedCompareExchange(&Registered, TRUE, FALSE))
  1110. {
  1111. WNDCLASS windowClass;
  1112. ZeroMemory(&windowClass, sizeof windowClass);
  1113. windowClass.style = CS_GLOBALCLASS;
  1114. windowClass.cbWndExtra = 0;
  1115. windowClass.lpfnWndProc = CmdLineMessageHandlerCallback;
  1116. windowClass.hInstance = CreduiInstance;
  1117. windowClass.hIcon = NULL;
  1118. windowClass.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
  1119. windowClass.lpszClassName = L"CreduiCmdLineHelperWindow";
  1120. InterlockedExchange(&Registered, RegisterClass(&windowClass) != 0);
  1121. if (!InterlockedCompareExchange(&Registered, FALSE, FALSE))
  1122. {
  1123. WinStatus = GetLastError();
  1124. goto Cleanup;
  1125. }
  1126. }
  1127. //
  1128. // Create a window of that class
  1129. //
  1130. Window = CreateWindow(
  1131. L"CreduiCmdLineHelperWindow",
  1132. NULL,
  1133. WS_POPUP,
  1134. 0, 0, 0, 0,
  1135. NULL, NULL, CreduiInstance,(LPVOID) this);
  1136. if ( Window == NULL )
  1137. {
  1138. WinStatus = GetLastError();
  1139. goto Cleanup;
  1140. }
  1141. //
  1142. // Run the message loop
  1143. //
  1144. CreduiDebugLog( "Entering message loop\n" );
  1145. for (;;) {
  1146. BOOL GmStatus;
  1147. GmStatus = GetMessage(&msg, NULL, 0, 0 );
  1148. if ( GmStatus == -1 )
  1149. {
  1150. WinStatus = GetLastError();
  1151. break;
  1152. }
  1153. else if ( GmStatus == 0 )
  1154. {
  1155. WinStatus = Result;
  1156. break;
  1157. }
  1158. DispatchMessage(&msg);
  1159. }
  1160. //
  1161. // Get the status left from the message loop.
  1162. WinStatus = (DWORD) msg.wParam;
  1163. CreduiDebugLog( "Got Quit Message: %ld\n", WinStatus );
  1164. //
  1165. // Process the data as though the user hit OK to the GUI
  1166. //
  1167. if ( WinStatus == NO_ERROR ) {
  1168. PasswordState = PASSWORD_CHANGED;
  1169. WinStatus = HandleOk();
  1170. if ( WinStatus == ERROR_NO_SUCH_LOGON_SESSION)
  1171. {
  1172. WinStatus = ERROR_CANCELLED;
  1173. }
  1174. }
  1175. CreduiDebugLog( "Calling destroy\n" );
  1176. DestroyWindow( Window );
  1177. Cleanup:
  1178. return WinStatus;
  1179. }
  1180. //=============================================================================
  1181. // CreduiPasswordDialog::FinishHandleOk
  1182. //
  1183. // This function writes the credential for domain credentials.
  1184. //
  1185. // Created 04/09/2000 johnstep (John Stephens)
  1186. //=============================================================================
  1187. DWORD
  1188. CreduiPasswordDialog::FinishHandleOk()
  1189. {
  1190. DWORD error = ERROR_SUCCESS;
  1191. PCREDENTIAL_TARGET_INFORMATION pTargetInfo = TargetInfo;
  1192. if (Flags & CREDUI_FLAGS_KEEP_USERNAME )
  1193. return error;
  1194. DWORD dwCredWriteFlags = 0;
  1195. BOOL bDoSave = TRUE;
  1196. if (PasswordState == PASSWORD_INIT)
  1197. {
  1198. dwCredWriteFlags |= CRED_PRESERVE_CREDENTIAL_BLOB;
  1199. }
  1200. if ( dwIDDResource == IDD_BRANDEDPASSWORD )
  1201. {
  1202. // we need to reinterpret what Save means. Save in this case means to save as the default
  1203. // for the SSO realm, while !Save means rename the cred target as the username and
  1204. // save without the password
  1205. // never delay writing these creds
  1206. DelayCredentialWrite = FALSE;
  1207. if ( Save )
  1208. {
  1209. NewCredential.Persist = CRED_PERSIST_ENTERPRISE;
  1210. if ( !(Flags & CREDUI_FLAGS_KEEP_USERNAME ))
  1211. DemoteOldDefaultSSOCred ( TargetInfo, Flags );
  1212. }
  1213. else
  1214. {
  1215. // save this under the username if it doesn't already match NewCred's username
  1216. if ( (OldCredential != NULL && OldCredential->UserName != NULL &&
  1217. _wcsicmp ( OldCredential->UserName, UserName ) == 0 )||
  1218. (Flags & CREDUI_FLAGS_KEEP_USERNAME ) )
  1219. {
  1220. // don't do this save if the username matches the currently saved cred - we don't want a duplicate
  1221. bDoSave = FALSE;
  1222. }
  1223. // alter the TargetName
  1224. TargetName = UserName;
  1225. TargetInfo->TargetName = TargetName;
  1226. // don't set a TargetInfo for this
  1227. pTargetInfo = NULL;
  1228. NewCredential.Persist = CRED_PERSIST_ENTERPRISE;
  1229. NewCredential.CredentialBlob = NULL;
  1230. NewCredential.CredentialBlobSize = 0;
  1231. NewCredential.TargetName = TargetName;
  1232. NewCredential.Flags = CRED_FLAGS_USERNAME_TARGET;
  1233. DelayCredentialWrite = FALSE;
  1234. EncryptedVisiblePassword = FALSE;
  1235. dwCredWriteFlags = 0;
  1236. }
  1237. }
  1238. //
  1239. // Write the credential to credential manager
  1240. //
  1241. // Don't delay actually writing the credential to cred man since we're not
  1242. // returning the credential to the caller.
  1243. //
  1244. if ( bDoSave )
  1245. {
  1246. error = WriteCred( TargetName,
  1247. Flags,
  1248. pTargetInfo,
  1249. &NewCredential,
  1250. dwCredWriteFlags,
  1251. DelayCredentialWrite,
  1252. EncryptedVisiblePassword);
  1253. }
  1254. else
  1255. {
  1256. error = ERROR_SUCCESS;
  1257. }
  1258. if ( error != NO_ERROR ) {
  1259. CreduiDebugLog("CreduiPasswordDialog::HandleOk: "
  1260. "WriteCred failed: "
  1261. "%u\n", error);
  1262. }
  1263. if ((SecurityContext != NULL) && (error == ERROR_SUCCESS))
  1264. {
  1265. BOOL isValidated = TRUE;
  1266. if (!SetContextAttributes(
  1267. SecurityContext,
  1268. SECPKG_ATTR_USE_VALIDATED,
  1269. &isValidated,
  1270. sizeof isValidated))
  1271. {
  1272. error = ERROR_GEN_FAILURE;
  1273. }
  1274. }
  1275. // Clear any password from memory, and also make sure the credential blob
  1276. // points to Password once again (in case it was set to NULL due to a zero
  1277. // length blob):
  1278. NewCredential.CredentialBlob = reinterpret_cast<BYTE *>(Password);
  1279. NewCredential.CredentialBlobSize = 0;
  1280. return error;
  1281. }
  1282. //=============================================================================
  1283. // CreduiPasswordDialog::Enable
  1284. //
  1285. // Enables or disables all the user controls in the dialog box. This allows us
  1286. // to maintain dialog responsiveness, while waiting for some potentially
  1287. // lengthy (network usually) operation to complete.
  1288. //
  1289. // Most controls are always enabled normally, but we need to track the state
  1290. // of IDC_CRED and IDOK. Do this with a simple DWORD bitmask.
  1291. //
  1292. // NOTE: Allow Cancel to remain enabled, and use it to abort the current
  1293. // lookup? This means we have to somehow kill the thread, though, or
  1294. // maybe just leave it, and close our handle to it?
  1295. //
  1296. // Arguments:
  1297. // enable (in) - TRUE to enable the controls, FALSE to disable.
  1298. //
  1299. // Created 04/07/2000 johnstep (John Stephens)
  1300. //=============================================================================
  1301. VOID
  1302. CreduiPasswordDialog::Enable(
  1303. BOOL enable
  1304. )
  1305. {
  1306. if (enable && (DisabledControlMask & DISABLED_DIALOG))
  1307. {
  1308. DisabledControlMask &= ~DISABLED_DIALOG;
  1309. EnableWindow(CredControlWindow, TRUE);
  1310. if ( DialogWindow) {
  1311. EnableWindow(GetDlgItem(DialogWindow, IDCANCEL), TRUE);
  1312. if (!(DisabledControlMask & DISABLED_CONTROL_OK))
  1313. {
  1314. EnableWindow(GetDlgItem(DialogWindow, IDOK), TRUE);
  1315. }
  1316. }
  1317. }
  1318. else if (!(DisabledControlMask & DISABLED_DIALOG))
  1319. {
  1320. // Hide the balloon tip before disabling the window:
  1321. DisabledControlMask |= DISABLED_DIALOG;
  1322. EnableWindow(CredControlWindow, FALSE);
  1323. if ( DialogWindow ) {
  1324. EnableWindow(GetDlgItem(DialogWindow, IDCANCEL), FALSE);
  1325. EnableWindow(GetDlgItem(DialogWindow, IDOK), FALSE);
  1326. }
  1327. }
  1328. }
  1329. //=============================================================================
  1330. // CreduiPasswordDialog::SelectAndSetWindowCaption
  1331. //
  1332. // Created 03/10/2000 johnstep (John Stephens)
  1333. //=============================================================================
  1334. VOID
  1335. CreduiPasswordDialog::SelectAndSetWindowCaption()
  1336. {
  1337. //
  1338. // Command line doesn't have a caption
  1339. //
  1340. if ( !DialogWindow ) {
  1341. return;
  1342. }
  1343. if (UiInfo.pszCaptionText != NULL)
  1344. {
  1345. SetWindowText(DialogWindow, UiInfo.pszCaptionText);
  1346. }
  1347. else
  1348. {
  1349. WCHAR captionText[256];
  1350. captionText[255] = L'\0';
  1351. if ( CredCategory == DOMAIN_CATEGORY )
  1352. {
  1353. if (TargetInfo != NULL)
  1354. {
  1355. if ((TargetInfo->DnsServerName != NULL) ||
  1356. (TargetInfo->NetbiosServerName == NULL))
  1357. {
  1358. _snwprintf(captionText, 255,
  1359. CreduiStrings.DnsCaption,
  1360. (TargetInfo->DnsServerName != NULL) ?
  1361. TargetInfo->DnsServerName :
  1362. TargetInfo->TargetName);
  1363. }
  1364. else if ((TargetInfo->NetbiosServerName != NULL) &&
  1365. (TargetInfo->NetbiosDomainName != NULL))
  1366. {
  1367. _snwprintf(captionText, 255,
  1368. CreduiStrings.NetbiosCaption,
  1369. TargetInfo->NetbiosServerName,
  1370. TargetInfo->NetbiosDomainName);
  1371. }
  1372. else
  1373. {
  1374. _snwprintf(captionText, 255,
  1375. CreduiStrings.DnsCaption, TargetName);
  1376. }
  1377. }
  1378. else
  1379. {
  1380. _snwprintf(captionText, 255,
  1381. CreduiStrings.DnsCaption, TargetName);
  1382. }
  1383. }
  1384. else
  1385. {
  1386. _snwprintf(captionText, 255,
  1387. CreduiStrings.GenericCaption, TargetName);
  1388. }
  1389. SetWindowText(DialogWindow, captionText);
  1390. }
  1391. }
  1392. //=============================================================================
  1393. // CreduiPasswordDialog::ResizeDialogCallback
  1394. //
  1395. // Created 04/12/2000 johnstep (John Stephens)
  1396. //=============================================================================
  1397. BOOL
  1398. CALLBACK
  1399. CreduiPasswordDialog::ResizeDialogCallback(
  1400. HWND childWindow,
  1401. LPARAM lParam
  1402. )
  1403. {
  1404. CreduiPasswordDialog *that =
  1405. reinterpret_cast<CreduiPasswordDialog *>(lParam);
  1406. ASSERT(that != NULL);
  1407. //
  1408. // Command line doesn't care about window size
  1409. //
  1410. if ( that->DoingCommandLine ) {
  1411. return TRUE;
  1412. }
  1413. HWND dialogWindow = GetParent(childWindow);
  1414. if (dialogWindow == NULL)
  1415. {
  1416. // Stop enumeration because our window must have been destroyed or
  1417. // something:
  1418. return FALSE;
  1419. }
  1420. else if (dialogWindow == that->DialogWindow)
  1421. {
  1422. RECT childRect;
  1423. GetWindowRect(childWindow, &childRect);
  1424. // If this child window is below the message window, move it down:
  1425. if (childRect.top >= that->ResizeTop)
  1426. {
  1427. MapWindowPoints ( NULL, dialogWindow,
  1428. reinterpret_cast<POINT *>(&childRect.left), 2 );
  1429. SetWindowPos(childWindow,
  1430. NULL,
  1431. childRect.left,
  1432. childRect.top + that->ResizeDelta,
  1433. 0,
  1434. 0,
  1435. SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOOWNERZORDER |
  1436. SWP_NOZORDER);
  1437. }
  1438. }
  1439. return TRUE;
  1440. }
  1441. //=============================================================================
  1442. // CreduiPasswordDialog::SelectAndSetWindowMessage
  1443. //
  1444. // This function will also resize the dialog to accomodate very long message
  1445. // text. The maximum number of lines allowed at once is
  1446. // CREDUI_MAX_WELCOME_TEXT_LINES, and, beyond that, a scrollbar is added.
  1447. //
  1448. // Created 03/10/2000 johnstep (John Stephens)
  1449. //=============================================================================
  1450. VOID
  1451. CreduiPasswordDialog::SelectAndSetWindowMessage()
  1452. {
  1453. HWND welcomeTextWindow = GetDlgItem(DialogWindow, IDC_WELCOME_TEXT);
  1454. //
  1455. // Command line doesn't have welcome text
  1456. //
  1457. if ( DoingCommandLine ) {
  1458. return;
  1459. }
  1460. ASSERT(welcomeTextWindow != NULL);
  1461. if (UiInfo.pszMessageText != NULL)
  1462. {
  1463. SetWindowText(welcomeTextWindow, UiInfo.pszMessageText);
  1464. }
  1465. else
  1466. {
  1467. WCHAR messageText[256];
  1468. messageText[255] = L'\0';
  1469. if ( CredCategory == GENERIC_CATEGORY )
  1470. {
  1471. if (UserName[0] == L'\0')
  1472. {
  1473. _snwprintf(messageText, 255,
  1474. CreduiStrings.Welcome, TargetName);
  1475. }
  1476. else
  1477. {
  1478. _snwprintf(messageText, 255,
  1479. CreduiStrings.WelcomeBack, TargetName);
  1480. }
  1481. }
  1482. else
  1483. {
  1484. _snwprintf(messageText, 255,
  1485. CreduiStrings.Connecting, TargetName);
  1486. }
  1487. SetWindowText(welcomeTextWindow, messageText);
  1488. }
  1489. ULONG lineCount = (ULONG) SendMessage(welcomeTextWindow,
  1490. EM_GETLINECOUNT,
  1491. 0,
  1492. 0);
  1493. if (lineCount > 1)
  1494. {
  1495. if ( dwIDDResource == IDD_BRANDEDPASSWORD )
  1496. {
  1497. // Different layout (text to side of graphic)
  1498. RECT messageRect; // RECT for welcomeTextWindow
  1499. ULONG lineHeight = 13; // Height of line in welcomeTextWindow
  1500. DWORD lineIndex = 0; // Character index of first char in second line of welcomeTextWindow
  1501. DWORD linePos = 0; // Position of first char in second line of welcomeTextWindow
  1502. GetWindowRect(welcomeTextWindow, &messageRect);
  1503. // We don't want to resize anything for the branded password dialog
  1504. ResizeDelta = 0;
  1505. ResizeTop = messageRect.bottom;
  1506. // Get the first character index of the second line
  1507. lineIndex = (DWORD)SendMessage(welcomeTextWindow, EM_LINEINDEX, 1, 0);
  1508. if (lineIndex > -1)
  1509. {
  1510. // Get the position of the first character of the second line
  1511. linePos = (ULONG)SendMessage(welcomeTextWindow, EM_POSFROMCHAR, lineIndex, 0);
  1512. if (linePos)
  1513. {
  1514. // This is our line height
  1515. lineHeight = (ULONG)HIWORD(linePos);
  1516. }
  1517. }
  1518. if ((lineCount * lineHeight) > CREDUI_MAX_LOGO_HEIGHT)
  1519. {
  1520. // Add the scrollbar. Consider adjusting the formatting rectangle
  1521. // of the edit control because, by default, it is adjusted to
  1522. // allow just exactly enough room for the scrollbar. This means
  1523. // text could potentially "touch" the scrollbar, which would look
  1524. // bad.
  1525. LONG_PTR style = GetWindowLongPtr(welcomeTextWindow, GWL_STYLE);
  1526. SetWindowLongPtr(welcomeTextWindow,
  1527. GWL_STYLE,
  1528. style |= WS_VSCROLL);
  1529. }
  1530. SetWindowPos(welcomeTextWindow,
  1531. NULL,
  1532. 0,
  1533. 0,
  1534. messageRect.right - messageRect.left,
  1535. messageRect.bottom - messageRect.top,
  1536. SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE |
  1537. SWP_NOOWNERZORDER | SWP_NOZORDER);
  1538. }
  1539. else
  1540. {
  1541. // normal layout (text below graphic)
  1542. RECT messageRect;
  1543. GetWindowRect(welcomeTextWindow, &messageRect);
  1544. ULONG lineHeight = messageRect.bottom - messageRect.top;
  1545. ResizeTop = messageRect.bottom;
  1546. ResizeDelta = lineHeight *
  1547. (min(CREDUI_MAX_WELCOME_TEXT_LINES, lineCount) - 1);
  1548. messageRect.bottom += ResizeDelta;
  1549. if (lineCount > CREDUI_MAX_WELCOME_TEXT_LINES)
  1550. {
  1551. // Add the scrollbar. Consider adjusting the formatting rectangle
  1552. // of the edit control because, by default, it is adjusted to
  1553. // allow just exactly enough room for the scrollbar. This means
  1554. // text could potentially "touch" the scrollbar, which would look
  1555. // bad.
  1556. LONG_PTR style = GetWindowLongPtr(welcomeTextWindow, GWL_STYLE);
  1557. SetWindowLongPtr(welcomeTextWindow,
  1558. GWL_STYLE,
  1559. style |= WS_VSCROLL);
  1560. }
  1561. SetWindowPos(welcomeTextWindow,
  1562. NULL,
  1563. 0,
  1564. 0,
  1565. messageRect.right - messageRect.left,
  1566. messageRect.bottom - messageRect.top,
  1567. SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE |
  1568. SWP_NOOWNERZORDER | SWP_NOZORDER);
  1569. }
  1570. // Reposition all the other controls:
  1571. EnumChildWindows(DialogWindow,
  1572. ResizeDialogCallback,
  1573. reinterpret_cast<LPARAM>(this));
  1574. // Resize the dialog box now
  1575. RECT dialogRect;
  1576. GetWindowRect(DialogWindow, &dialogRect);
  1577. SetWindowPos(DialogWindow,
  1578. NULL,
  1579. dialogRect.left,
  1580. dialogRect.top - (ResizeDelta / 2),
  1581. dialogRect.right - dialogRect.left,
  1582. (dialogRect.bottom - dialogRect.top) + ResizeDelta,
  1583. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  1584. ResizeTop = 0;
  1585. ResizeDelta = 0;
  1586. }
  1587. }
  1588. //=============================================================================
  1589. // CreduiPasswordDialog::InitWindow
  1590. //
  1591. // Created 02/25/2000 johnstep (John Stephens)
  1592. //
  1593. // Initialize the window
  1594. //
  1595. // dialogWindow - Window handle
  1596. //=============================================================================
  1597. BOOL
  1598. CreduiPasswordDialog::InitWindow(HWND dialogWindow)
  1599. {
  1600. //
  1601. // Store this object's pointer in the user data window long:
  1602. //
  1603. SetWindowLongPtr(dialogWindow,
  1604. GWLP_USERDATA,
  1605. reinterpret_cast<LONG_PTR>(this));
  1606. //
  1607. // In the GUI case, remember the dialog box window
  1608. //
  1609. if ( !DoingCommandLine ) {
  1610. DialogWindow = dialogWindow;
  1611. // Get the window handles for the various controls:
  1612. CredControlWindow = GetDlgItem(DialogWindow, IDC_CRED);
  1613. //
  1614. // In the command line case, remember the popup window
  1615. //
  1616. } else {
  1617. CmdLineWindow = dialogWindow;
  1618. //
  1619. // Create a CredControlWindow
  1620. // It isn't visible, but the existence of the window allows the command line
  1621. // code to share logic with the GUI implementation
  1622. //
  1623. CredControlWindow =
  1624. CreateWindowEx(
  1625. WS_EX_NOPARENTNOTIFY,
  1626. WC_CREDENTIAL,
  1627. NULL, // No window name
  1628. WS_CHILD | WS_GROUP,
  1629. 0, 0, 0, 0, // No coordinates on the screen
  1630. CmdLineWindow, // Parent window
  1631. NULL, // No menu window
  1632. CreduiInstance,
  1633. NULL);
  1634. if ( CredControlWindow == NULL) {
  1635. return FALSE;
  1636. }
  1637. }
  1638. ASSERT(CredControlWindow != NULL);
  1639. //
  1640. // First, initialize the Credential control window:
  1641. //
  1642. // Set the default style
  1643. //
  1644. if (Flags & CREDUI_FLAGS_REQUIRE_SMARTCARD)
  1645. {
  1646. CredControlStyle = CRS_SMARTCARDS;
  1647. }
  1648. else if (Flags & CREDUI_FLAGS_REQUIRE_CERTIFICATE)
  1649. {
  1650. CredControlStyle = CRS_CERTIFICATES | CRS_SMARTCARDS;
  1651. }
  1652. else if (Flags & CREDUI_FLAGS_EXCLUDE_CERTIFICATES)
  1653. {
  1654. CredControlStyle = CRS_USERNAMES;
  1655. }
  1656. else
  1657. {
  1658. CredControlStyle = CRS_USERNAMES | CRS_CERTIFICATES | CRS_SMARTCARDS;
  1659. }
  1660. if (Flags & CREDUI_FLAGS_KEEP_USERNAME )
  1661. {
  1662. CredControlStyle |= CRS_KEEPUSERNAME;
  1663. }
  1664. //
  1665. // If we have a target info,
  1666. // refine the style to match match what the auth package needs
  1667. //
  1668. // If the auth package has no opinion of its needs, allow everything.
  1669. //
  1670. if ( CredCategory == DOMAIN_CATEGORY &&
  1671. TargetInfo != NULL &&
  1672. TargetInfo->CredTypeCount != 0 )
  1673. {
  1674. ULONG AuthPackageStyle;
  1675. AuthPackageStyle = 0;
  1676. //
  1677. // Loop through the supported cred types seeing what style the auth package supports
  1678. //
  1679. for (UINT i = 0; i < TargetInfo->CredTypeCount; ++i)
  1680. {
  1681. switch ( TargetInfo->CredTypes[i] ) {
  1682. case CRED_TYPE_DOMAIN_CERTIFICATE:
  1683. AuthPackageStyle |= CRS_CERTIFICATES | CRS_SMARTCARDS;
  1684. break;
  1685. case CRED_TYPE_DOMAIN_PASSWORD:
  1686. case CRED_TYPE_DOMAIN_VISIBLE_PASSWORD:
  1687. AuthPackageStyle |= CRS_USERNAMES;
  1688. break;
  1689. }
  1690. }
  1691. CredControlStyle &= (AuthPackageStyle | CRS_KEEPUSERNAME );
  1692. ASSERT( CredControlStyle != 0);
  1693. }
  1694. //
  1695. // If the caller wants only administrators,
  1696. // include that request in the style.
  1697. //
  1698. if (Flags & CREDUI_FLAGS_REQUEST_ADMINISTRATOR)
  1699. {
  1700. CredControlStyle |= CRS_ADMINISTRATORS;
  1701. }
  1702. //
  1703. // If the caller asked to see the save checkbox,
  1704. // and we're running in a session that supports saving,
  1705. // include the save checkbox in the style
  1706. if ( (Flags & (CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_PERSIST)) == 0 &&
  1707. !(Flags & CREDUI_FLAGS_KEEP_USERNAME ) &&
  1708. MaximumPersist != CRED_PERSIST_NONE )
  1709. {
  1710. CredControlStyle |= CRS_SAVECHECK;
  1711. //
  1712. // If the caller wants us to prompt for saving and return the result to him,
  1713. // include the save checkbox in the style
  1714. //
  1715. }
  1716. else if ( (Flags & (CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX)) ==
  1717. (CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) &&
  1718. !(Flags & CREDUI_FLAGS_KEEP_USERNAME ))
  1719. {
  1720. CredControlStyle |= CRS_SAVECHECK;
  1721. }
  1722. //
  1723. // If we're not displaying the save checkbox,
  1724. // adust the size of the dialog box.
  1725. //
  1726. if ( (CredControlStyle & CRS_SAVECHECK) == 0 &&
  1727. DialogWindow != NULL ) {
  1728. RECT rect;
  1729. SetRect(&rect, 0, 0, 0, 0);
  1730. rect.bottom = CREDUI_CONTROL_ADD_SAVE;
  1731. MapDialogRect(DialogWindow, &rect);
  1732. ResizeDelta = -rect.bottom;
  1733. GetWindowRect(CredControlWindow, &rect);
  1734. ResizeTop = rect.bottom;
  1735. // Reposition all the other controls:
  1736. EnumChildWindows(DialogWindow,
  1737. ResizeDialogCallback,
  1738. reinterpret_cast<LPARAM>(this));
  1739. // Resize the dialog box now:
  1740. GetWindowRect(DialogWindow, &rect);
  1741. SetWindowPos(DialogWindow,
  1742. NULL,
  1743. rect.left,
  1744. rect.top - (ResizeDelta / 2),
  1745. rect.right - rect.left,
  1746. (rect.bottom - rect.top) + ResizeDelta,
  1747. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  1748. GetClientRect(CredControlWindow, &rect);
  1749. SetWindowPos(CredControlWindow,
  1750. NULL,
  1751. 0,
  1752. 0,
  1753. rect.right - rect.left,
  1754. (rect.bottom - rect.top) + ResizeDelta,
  1755. SWP_NOMOVE | SWP_NOACTIVATE |
  1756. SWP_NOOWNERZORDER | SWP_NOZORDER);
  1757. ResizeTop = 0;
  1758. ResizeDelta = 0;
  1759. }
  1760. if ( dwIDDResource == IDD_BRANDEDPASSWORD )
  1761. {
  1762. // size the parts of the brand
  1763. RECT rect;
  1764. RECT rectleft;
  1765. RECT rectright;
  1766. RECT rectmid;
  1767. HWND hwndRight = GetDlgItem(DialogWindow, IDC_BRANDRIGHT);
  1768. HWND hwndMid = GetDlgItem(DialogWindow, IDC_BRANDMID);
  1769. HWND hwndLeft = GetDlgItem(DialogWindow, IDC_BRANDLEFT);
  1770. HBITMAP hBmp;
  1771. GetWindowRect(hwndRight, &rect);
  1772. // Load the images from files
  1773. // We can't load them in the .rc because then we lose transparency
  1774. hBmp = (HBITMAP)LoadImage(CreduiInstance, MAKEINTRESOURCE(IDB_BRANDRIGHT), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  1775. if (hBmp)
  1776. {
  1777. SendMessage(hwndRight, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
  1778. }
  1779. hBmp = (HBITMAP)LoadImage(CreduiInstance, MAKEINTRESOURCE(IDB_BRANDMID), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  1780. if (hBmp)
  1781. {
  1782. if (hwndMid)
  1783. {
  1784. SendMessage(hwndMid, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
  1785. }
  1786. }
  1787. hBmp = (HBITMAP)LoadImage(CreduiInstance, MAKEINTRESOURCE(IDB_BRANDLEFT), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  1788. if (hBmp)
  1789. {
  1790. if (hwndLeft)
  1791. {
  1792. SendMessage(hwndLeft, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
  1793. }
  1794. }
  1795. rectright.bottom = rect.bottom;
  1796. rectright.top = rect.bottom - BRANDRIGHT_PIXEL_HEIGHT;
  1797. rectright.right = rect.right;
  1798. rectright.left = rect.right - BRANDRIGHT_PIXEL_WIDTH;
  1799. rectleft.bottom = rect.bottom;
  1800. rectleft.top = rect.bottom - BRANDLEFT_PIXEL_HEIGHT;
  1801. rectleft.left = rect.left;
  1802. rectleft.right = rect.left + BRANDLEFT_PIXEL_WIDTH;
  1803. MapWindowPoints(NULL, DialogWindow, (LPPOINT)&rectleft, 2);
  1804. MapWindowPoints(NULL, DialogWindow, (LPPOINT)&rectright, 2);
  1805. if ( rectleft.right >= rectright.left )
  1806. {
  1807. // dialog is really narrow, just use rectright
  1808. // hide the other two
  1809. ShowWindow ( hwndMid, SW_HIDE );
  1810. ShowWindow ( hwndLeft, SW_HIDE );
  1811. }
  1812. else
  1813. {
  1814. // calculate rectmid
  1815. rectmid.bottom = rectleft.bottom;
  1816. rectmid.top = rectleft.bottom - BRANDMID_PIXEL_HEIGHT;
  1817. rectmid.left = rectleft.right;
  1818. rectmid.right = rectright.left;
  1819. // No need to MapWindowsPoints for rectmid, since it's base off of the already-mapped rects.
  1820. SetWindowPos(hwndMid,
  1821. NULL,
  1822. rectmid.left,
  1823. rectmid.top,
  1824. rectmid.right - rectmid.left,
  1825. rectmid.bottom - rectmid.top,
  1826. SWP_NOACTIVATE | SWP_NOOWNERZORDER |
  1827. SWP_NOZORDER);
  1828. SetWindowPos(hwndLeft,
  1829. NULL,
  1830. rectleft.left,
  1831. rectleft.top,
  1832. rectleft.right - rectleft.left,
  1833. rectleft.bottom - rectleft.top,
  1834. SWP_NOACTIVATE | SWP_NOOWNERZORDER |
  1835. SWP_NOZORDER);
  1836. }
  1837. SetWindowPos(hwndRight,
  1838. NULL,
  1839. rectright.left,
  1840. rectright.top,
  1841. rectright.right - rectright.left,
  1842. rectright.bottom - rectright.top,
  1843. SWP_NOACTIVATE | SWP_NOOWNERZORDER |
  1844. SWP_NOZORDER);
  1845. }
  1846. //
  1847. // Tell the control window the style flags and whether we're doing command line
  1848. //
  1849. if (!SendMessage(CredControlWindow, CRM_INITSTYLE, (WPARAM)(CredControlStyle), DoingCommandLine ) )
  1850. {
  1851. return FALSE;
  1852. }
  1853. // If a custom banner bitmap was provided, set it now, and free the
  1854. // default bitmap:
  1855. if ( DialogWindow != NULL && UiInfo.hbmBanner != NULL)
  1856. {
  1857. HWND bannerControlWindow = GetDlgItem(DialogWindow, IDC_BANNER);
  1858. ASSERT(bannerControlWindow != NULL);
  1859. HBITMAP oldBitmap =
  1860. reinterpret_cast<HBITMAP>(
  1861. SendMessage(
  1862. bannerControlWindow,
  1863. STM_SETIMAGE,
  1864. IMAGE_BITMAP,
  1865. reinterpret_cast<LPARAM>(UiInfo.hbmBanner)));
  1866. ASSERT(oldBitmap != NULL);
  1867. DeleteObject(static_cast<HGDIOBJ>(oldBitmap));
  1868. }
  1869. else if ( DialogWindow != NULL && dwIDDResource == IDD_BRANDEDPASSWORD )
  1870. {
  1871. // if it's branded and there's no banner, hide the banner control
  1872. HWND bannerControlWindow = GetDlgItem(DialogWindow, IDC_BANNER);
  1873. if (bannerControlWindow != NULL)
  1874. {
  1875. ShowWindow ( bannerControlWindow, SW_HIDE );
  1876. }
  1877. }
  1878. // Limit the number of characters entered into the user name and password
  1879. // edit controls:
  1880. Credential_SetUserNameMaxChars(CredControlWindow, UserNameMaxChars);
  1881. Credential_SetPasswordMaxChars(CredControlWindow, PasswordMaxChars);
  1882. SelectAndSetWindowCaption();
  1883. SelectAndSetWindowMessage();
  1884. //
  1885. // Set the default check box states
  1886. // Default to don't save. That ensures the user has to take an action to
  1887. // change global state.
  1888. //
  1889. //
  1890. // If the caller is specifying the default value of the checkbox,
  1891. // grab that default
  1892. //
  1893. fInitialSaveState = FALSE;
  1894. if (Flags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX)
  1895. {
  1896. if (dwIDDResource != IDD_BRANDEDPASSWORD)
  1897. {
  1898. fInitialSaveState = Save;
  1899. }
  1900. else
  1901. {
  1902. fInitialSaveState = FALSE;
  1903. }
  1904. //
  1905. // Only save if credman is available
  1906. //
  1907. } else if ( MaximumPersist != CRED_PERSIST_NONE ) {
  1908. //
  1909. // If the caller is forcing the save flag on,
  1910. // default the save flag to TRUE.
  1911. //
  1912. if ( Flags & CREDUI_FLAGS_PERSIST ) {
  1913. fInitialSaveState = TRUE;
  1914. }
  1915. }
  1916. Credential_CheckSave(CredControlWindow, fInitialSaveState);
  1917. // Make sure the new password window is the foreground window, if
  1918. // possible:
  1919. if ( DialogWindow != NULL ) {
  1920. AllowSetForegroundWindow(GetCurrentProcessId());
  1921. SetForegroundWindow(DialogWindow);
  1922. AllowSetForegroundWindow(ASFW_ANY);
  1923. }
  1924. // NOTE: Do we really need the guest user name here?
  1925. if ((TargetInfo != NULL) && (UserName[0] == L'\0'))
  1926. {
  1927. if (TargetInfo->Flags & CRED_TI_ONLY_PASSWORD_REQUIRED)
  1928. {
  1929. LPWSTR ServerName = NULL;
  1930. if ( TargetInfo->NetbiosServerName != NULL ) {
  1931. ServerName = TargetInfo->NetbiosServerName;
  1932. } else if ( TargetInfo->DnsServerName != NULL &&
  1933. (TargetInfo->Flags & CRED_TI_SERVER_FORMAT_UNKNOWN) != 0 ) {
  1934. ServerName = TargetInfo->DnsServerName;
  1935. }
  1936. if ( ServerName != NULL )
  1937. {
  1938. LPWSTR GuestName;
  1939. if ( !CreduiLookupLocalNameFromRid( DOMAIN_USER_RID_GUEST, &GuestName ) ) {
  1940. GuestName = NULL;
  1941. }
  1942. _snwprintf(UserName,
  1943. CREDUI_MAX_USERNAME_LENGTH,
  1944. L"%s\\%s",
  1945. ServerName,
  1946. GuestName == NULL ? L"Guest" : GuestName );
  1947. if ( GuestName != NULL ) {
  1948. delete GuestName;
  1949. }
  1950. }
  1951. }
  1952. else
  1953. {
  1954. if ( DialogWindow ) {
  1955. EnableWindow(GetDlgItem(DialogWindow, IDOK), FALSE);
  1956. }
  1957. DisabledControlMask |= DISABLED_CONTROL_OK;
  1958. }
  1959. }
  1960. if ((UserName[0] != L'\0') || (Password[0] != L'\0'))
  1961. {
  1962. // If this fails, it may be because the certificate was not found in
  1963. // the store, so if this is a domain credential, use the password one,
  1964. // if available:
  1965. BOOL isMarshaled = LocalCredIsMarshaledCredentialW(UserName);
  1966. if (Credential_SetUserName(CredControlWindow, UserName))
  1967. {
  1968. if (!isMarshaled)
  1969. {
  1970. Credential_SetPassword(CredControlWindow, Password);
  1971. }
  1972. if (TargetInfo)
  1973. {
  1974. if (TargetInfo->Flags & CRED_TI_ONLY_PASSWORD_REQUIRED)
  1975. {
  1976. Credential_DisableUserName(CredControlWindow);
  1977. // when disabling the username field, prepare to return no username to the caller
  1978. if (Flags & CREDUI_FLAGS_PASSWORD_ONLY_OK)
  1979. {
  1980. fPasswordOnly = TRUE;
  1981. CreduiDebugLog("CreduiPasswordDialog::InitWindow - Password only share\n");
  1982. }
  1983. }
  1984. }
  1985. }
  1986. else
  1987. {
  1988. if ( CredCategory == DOMAIN_CATEGORY &&
  1989. isMarshaled &&
  1990. (PasswordCredential != NULL))
  1991. {
  1992. OldCredential = PasswordCredential;
  1993. // Change as little as possible about the new credential
  1994. // because we already selected the target name, etc.:
  1995. if (OldCredential->UserName != NULL)
  1996. {
  1997. lstrcpyn(UserName,
  1998. OldCredential->UserName,
  1999. UserNameMaxChars + 1);
  2000. }
  2001. else
  2002. {
  2003. UserName[0] = L'\0';
  2004. }
  2005. Password[0] = L'\0';
  2006. Credential_SetPassword(CredControlWindow, Password);
  2007. }
  2008. else
  2009. {
  2010. UserName[0] = L'\0';
  2011. Password[0] = L'\0';
  2012. }
  2013. }
  2014. if (PasswordState == PASSWORD_UNINIT)
  2015. {
  2016. PasswordState = PASSWORD_INIT;
  2017. }
  2018. if (UserName[0] != L'\0')
  2019. {
  2020. Credential_SetPasswordFocus(CredControlWindow);
  2021. if (Flags & CREDUI_FLAGS_INCORRECT_PASSWORD)
  2022. {
  2023. if (CreduiIsCapsLockOn())
  2024. {
  2025. CredBalloon = &CreduiLogonCapsBalloon;
  2026. }
  2027. else
  2028. {
  2029. CredBalloon = &CreduiLogonBalloon;
  2030. }
  2031. }
  2032. }
  2033. }
  2034. if (Flags & CREDUI_FLAGS_KEEP_USERNAME )
  2035. {
  2036. // okay button should always be enabled for this case
  2037. EnableWindow(GetDlgItem(DialogWindow, IDOK), TRUE);
  2038. DisabledControlMask &= ~DISABLED_CONTROL_OK;
  2039. }
  2040. return TRUE;
  2041. }
  2042. //=============================================================================
  2043. // CreduiPasswordDialog::CompleteUserName
  2044. //
  2045. // Searches the user name for a domain name, and determines whether this
  2046. // specifies the target server or a domain. If a domain is not present in the
  2047. // user name, add it if this is a workstation or no target information is
  2048. // available.
  2049. //
  2050. // Returns TRUE if a domain was already present in the user name, or if we
  2051. // added one. Otherwise, return FALSE.
  2052. //
  2053. // Created 03/10/2000 johnstep (John Stephens)
  2054. //=============================================================================
  2055. BOOL
  2056. CreduiPasswordDialog::CompleteUserName()
  2057. {
  2058. return ::CompleteUserName(
  2059. UserName,
  2060. UserNameMaxChars,
  2061. TargetInfo,
  2062. TargetName,
  2063. Flags);
  2064. }
  2065. //=============================================================================
  2066. // CreduiPasswordDialog::SelectBestTargetName
  2067. //
  2068. // Given TargetInfo, this function determines the most general target name
  2069. // possible, and stores the result in NewTargetName. If a target alias is
  2070. // available (NetBIOS name for a DNS name), this will be stored in
  2071. // NewTargetAlias. If the wildcard won't fit within the required string
  2072. // length, then it is not used.
  2073. //
  2074. // If serverOnly then, use the target information form of the target name,
  2075. // with the user-typed name as the alias. If unavailable, just use the user-
  2076. // typed form with a NULL alias.
  2077. //
  2078. // Created 03/10/2000 johnstep (John Stephens)
  2079. //=============================================================================
  2080. VOID
  2081. CreduiPasswordDialog::SelectBestTargetName(
  2082. BOOL serverOnly
  2083. )
  2084. {
  2085. NET_API_STATUS NetStatus;
  2086. BOOL usePrefixWildcard = TRUE;
  2087. WCHAR *credName = NULL;
  2088. LPWSTR DomainName = NULL;
  2089. LPWSTR DnsDomainName = NULL;
  2090. LPWSTR DnsForestName = NULL;
  2091. LPWSTR ComputerName = NULL;
  2092. BOOLEAN IsWorkgroupName;
  2093. BOOLEAN AddWildcard = TRUE;
  2094. // If serverOnly is passed, always use the new server-only behavior.
  2095. if (serverOnly)
  2096. {
  2097. if (TargetInfo->TargetName != NULL)
  2098. {
  2099. credName = TargetInfo->TargetName;
  2100. NewCredential.TargetAlias = TargetName;
  2101. }
  2102. else
  2103. {
  2104. credName = TargetName;
  2105. NewCredential.TargetAlias = NULL;
  2106. }
  2107. ASSERT(credName != NULL);
  2108. lstrcpyn(NewTargetName, credName, CRED_MAX_STRING_LENGTH);
  2109. return;
  2110. }
  2111. // Determine the maximum length name we can use for the wildcard case:
  2112. const ULONG maxChars = ((sizeof NewTargetName) /
  2113. (sizeof NewTargetName[0])) - 3;
  2114. if (TargetInfo->Flags & CRED_TI_ONLY_PASSWORD_REQUIRED )
  2115. {
  2116. serverOnly = TRUE;
  2117. }
  2118. //
  2119. // If the auth package requires us to create a target info by an explicit name,
  2120. // only use that name.
  2121. //
  2122. if ( TargetInfo->Flags & CRED_TI_CREATE_EXPLICIT_CRED )
  2123. {
  2124. credName = TargetInfo->TargetName;
  2125. AddWildcard = FALSE;
  2126. }
  2127. //
  2128. // If this machine is a member of a domain,
  2129. // and the target machine is a member of the same forest,
  2130. // and the user is logged onto an account in the forest,
  2131. // then this prompt must be because of an authorization issue.
  2132. //
  2133. // Avoid this check if a username was passed into the call
  2134. //
  2135. // Special case this and create a server specific credential
  2136. //
  2137. if ( credName == NULL &&
  2138. !serverOnly &&
  2139. UserName[0] == '\0' ) {
  2140. BOOL MachineInSameForest = FALSE;
  2141. BOOL UserUsingDomainAccount = FALSE;
  2142. //
  2143. // First determine if this machine is in the same forest as the target machine
  2144. //
  2145. NetStatus = NetpGetDomainNameExExEx (
  2146. &DomainName,
  2147. &DnsDomainName,
  2148. &DnsForestName,
  2149. NULL, // Don't need the GUID
  2150. &IsWorkgroupName );
  2151. if ( NetStatus == NO_ERROR &&
  2152. !IsWorkgroupName ) {
  2153. //
  2154. // check if the netbios domain names match
  2155. //
  2156. if ( TargetInfo->NetbiosDomainName != NULL &&
  2157. DomainName != NULL &&
  2158. lstrcmpi( TargetInfo->NetbiosDomainName, DomainName ) == 0 ) {
  2159. MachineInSameForest = TRUE;
  2160. //
  2161. // check if the DNS domain names match
  2162. //
  2163. } else if ( TargetInfo->DnsDomainName != NULL &&
  2164. DnsDomainName != NULL &&
  2165. lstrcmpi( TargetInfo->DnsDomainName, DnsDomainName ) == 0 ) {
  2166. MachineInSameForest = TRUE;
  2167. //
  2168. // handle the special domain format unknown case
  2169. //
  2170. } else if ( TargetInfo->DnsDomainName != NULL &&
  2171. DomainName != NULL &&
  2172. (TargetInfo->Flags & CRED_TI_DOMAIN_FORMAT_UNKNOWN) != 0 &&
  2173. lstrcmpi( TargetInfo->DnsDomainName, DomainName ) == 0 ) {
  2174. MachineInSameForest = TRUE;
  2175. //
  2176. // handle the forest name
  2177. // (Too bad this doesn't work for the other trees in the forest.)
  2178. //
  2179. } else if ( TargetInfo->DnsTreeName != NULL &&
  2180. DnsForestName != NULL &&
  2181. lstrcmpi( TargetInfo->DnsTreeName, DnsForestName ) == 0 ) {
  2182. MachineInSameForest = TRUE;
  2183. }
  2184. }
  2185. //
  2186. // If this machine and target machine are in the same forest,
  2187. // see if the user is logged onto an account that should have worked.
  2188. //
  2189. if ( MachineInSameForest ) {
  2190. WCHAR UserNameBuffer[CRED_MAX_USERNAME_LENGTH+1];
  2191. ULONG UserNameLength = CRED_MAX_USERNAME_LENGTH+1;
  2192. if ( GetUserNameEx( NameSamCompatible,
  2193. UserNameBuffer,
  2194. &UserNameLength ) ) {
  2195. //
  2196. // Parse off the netbios account domain name
  2197. //
  2198. LPWSTR SlashPointer;
  2199. SlashPointer = wcsrchr( UserNameBuffer, L'\\' );
  2200. if ( SlashPointer != NULL ) {
  2201. *SlashPointer = '\0';
  2202. //
  2203. // Get the computer name of this machine
  2204. //
  2205. NetStatus = NetpGetComputerName( &ComputerName );
  2206. if ( NetStatus == NO_ERROR ) {
  2207. //
  2208. // If the netbios account domain doesn't match the local computer name,
  2209. // then the user is logged onto an account in the forest.
  2210. // Since trust within the forest is transitive,
  2211. // these creds should have worked
  2212. //
  2213. if ( lstrcmpi( ComputerName, UserNameBuffer ) != 0 ) {
  2214. UserUsingDomainAccount = TRUE;
  2215. }
  2216. }
  2217. }
  2218. }
  2219. }
  2220. //
  2221. // If both conditions are true,
  2222. // we should create a server only cred.
  2223. //
  2224. if ( MachineInSameForest && UserUsingDomainAccount ) {
  2225. serverOnly = TRUE;
  2226. }
  2227. }
  2228. //
  2229. // Compute the most generic form of the target name
  2230. //
  2231. if ( credName == NULL && !serverOnly)
  2232. {
  2233. // Look for the most general name possible first, selecting DNS over
  2234. // NetBIOS:
  2235. if ((TargetInfo->DnsServerName != NULL) &&
  2236. (TargetInfo->DnsTreeName != NULL) &&
  2237. CreduiIsPostfixString(TargetInfo->DnsServerName,
  2238. TargetInfo->DnsTreeName) &&
  2239. (lstrlen(TargetInfo->DnsTreeName) <= maxChars))
  2240. {
  2241. // Credential form: *.DnsTreeName
  2242. credName = TargetInfo->DnsTreeName;
  2243. }
  2244. else if ((TargetInfo->DnsServerName != NULL) &&
  2245. (TargetInfo->DnsDomainName != NULL) &&
  2246. !(TargetInfo->Flags & CRED_TI_DOMAIN_FORMAT_UNKNOWN) &&
  2247. CreduiIsPostfixString(TargetInfo->DnsServerName,
  2248. TargetInfo->DnsDomainName) &&
  2249. (lstrlen(TargetInfo->DnsDomainName) <= maxChars))
  2250. {
  2251. // Credential form: *.DnsDomainName
  2252. credName = TargetInfo->DnsDomainName;
  2253. }
  2254. else
  2255. {
  2256. usePrefixWildcard = FALSE;
  2257. // If we have a DNS domain name, and it is different from the
  2258. // target name and different from the NetBIOS server, if it
  2259. // exists, then use it:
  2260. if ((TargetInfo->DnsDomainName != NULL) &&
  2261. (lstrlen(TargetInfo->DnsDomainName) <= maxChars))
  2262. {
  2263. // Credential form: DnsDomainName\*
  2264. credName = TargetInfo->DnsDomainName;
  2265. // Set the target alias, if we have one. Don't change the
  2266. // field otherwise, because it may already have been stored in
  2267. // the old credential which was copied over:
  2268. if (TargetInfo->NetbiosDomainName != NULL)
  2269. {
  2270. ULONG append = lstrlen(TargetInfo->NetbiosDomainName);
  2271. if (append <= maxChars)
  2272. {
  2273. lstrcpy(NewTargetAlias,
  2274. TargetInfo->NetbiosDomainName);
  2275. NewTargetAlias[append + 0] = L'\\';
  2276. NewTargetAlias[append + 1] = L'*';
  2277. NewTargetAlias[append + 2] = L'\0';
  2278. NewCredential.TargetAlias = NewTargetAlias;
  2279. }
  2280. }
  2281. }
  2282. else if ((TargetInfo->NetbiosDomainName != NULL) &&
  2283. (lstrlen(TargetInfo->NetbiosDomainName) <= maxChars))
  2284. {
  2285. // Credential form: NetbiosDomainName\*
  2286. credName = TargetInfo->NetbiosDomainName;
  2287. }
  2288. }
  2289. }
  2290. //
  2291. // If we still don't have a target name, select a server target name:
  2292. //
  2293. if (credName == NULL)
  2294. {
  2295. if (TargetInfo->DnsServerName != NULL)
  2296. {
  2297. // Credential form: DnsServerName
  2298. credName = TargetInfo->DnsServerName;
  2299. // Set the target alias, if we have one. Don't change the field
  2300. // otherwise, because it may already have been stored in the old
  2301. // credential which was copied over:
  2302. if (TargetInfo->NetbiosServerName != NULL)
  2303. {
  2304. NewCredential.TargetAlias = TargetInfo->NetbiosServerName;
  2305. }
  2306. }
  2307. else if (TargetInfo->NetbiosServerName != NULL)
  2308. {
  2309. // Credential form: NetbiosServerName
  2310. credName = TargetInfo->NetbiosServerName;
  2311. }
  2312. else
  2313. {
  2314. // Credential form: TargetName
  2315. credName = TargetName;
  2316. }
  2317. AddWildcard = FALSE;
  2318. }
  2319. ASSERT( credName != NULL );
  2320. //
  2321. // If the target name should be wildcarded,
  2322. // add the wildcard
  2323. //
  2324. if ( AddWildcard )
  2325. {
  2326. // Add the wildcard in the required format:
  2327. if (usePrefixWildcard)
  2328. {
  2329. NewTargetName[0] = L'*';
  2330. NewTargetName[1] = L'.';
  2331. lstrcpy(NewTargetName + 2, credName);
  2332. }
  2333. else
  2334. {
  2335. lstrcpy(NewTargetName, credName);
  2336. ULONG append = lstrlen(NewTargetName);
  2337. ASSERT(append <= maxChars);
  2338. NewTargetName[append + 0] = L'\\';
  2339. NewTargetName[append + 1] = L'*';
  2340. NewTargetName[append + 2] = L'\0';
  2341. }
  2342. } else {
  2343. lstrcpy(NewTargetName, credName);
  2344. }
  2345. if ( DomainName != NULL ) {
  2346. NetApiBufferFree( DomainName );
  2347. }
  2348. if ( DnsDomainName != NULL ) {
  2349. NetApiBufferFree( DnsDomainName );
  2350. }
  2351. if ( DnsForestName != NULL ) {
  2352. NetApiBufferFree( DnsForestName );
  2353. }
  2354. if ( ComputerName != NULL ) {
  2355. NetApiBufferFree( ComputerName );
  2356. }
  2357. }
  2358. //=============================================================================
  2359. // CreduiPasswordDialog::UsernameHandleOk
  2360. //
  2361. // Do the username part of "HandleOk". The caller should already have filled in
  2362. // UserName as typed by the user. On succes, UserName may be modified to reflect
  2363. // completion flags.
  2364. //
  2365. // This part of HandleOk is split out because command line is considered to have two
  2366. // OKs. The first is when the user hits 'enter' after typing the username. So
  2367. // username validation happens then by calling this routine.
  2368. //
  2369. // Returns:
  2370. // NO_ERROR: if the user name is OK.
  2371. // ERROR_BAD_USERNAME: if the username syntax is bad
  2372. // ERROR_DOWNGRADE_DETECTED: if the username doesn't match downgrade requirements
  2373. //
  2374. //=============================================================================
  2375. DWORD
  2376. CreduiPasswordDialog::UsernameHandleOk()
  2377. {
  2378. DWORD WinStatus;
  2379. BOOL isMarshaled = LocalCredIsMarshaledCredentialW(UserName);
  2380. CreduiDebugLog("UsernameHandleOK: Flags = %x\n",Flags);
  2381. //
  2382. // Compute the credential type based on the new username
  2383. //
  2384. if ( CredCategory != GENERIC_CATEGORY )
  2385. {
  2386. if (isMarshaled)
  2387. {
  2388. NewCredential.Type = CRED_TYPE_DOMAIN_CERTIFICATE;
  2389. }
  2390. else
  2391. {
  2392. SSOPACKAGE SSOPackage;
  2393. // look in registry
  2394. if ( GetSSOPackageInfo( TargetInfo, &SSOPackage ) )
  2395. {
  2396. // it's an sso cred
  2397. NewCredential.Type = CRED_TYPE_DOMAIN_VISIBLE_PASSWORD;
  2398. MaximumPersist = MaximumPersistSso;
  2399. }
  2400. else
  2401. {
  2402. // just a regular domain cred
  2403. NewCredential.Type = CRED_TYPE_DOMAIN_PASSWORD;
  2404. }
  2405. }
  2406. // Set the flag for username target cred
  2407. if ( CredCategory == USERNAME_TARGET_CATEGORY ) {
  2408. NewCredential.Flags |= CRED_FLAGS_USERNAME_TARGET;
  2409. }
  2410. }
  2411. else
  2412. {
  2413. NewCredential.Type = CRED_TYPE_GENERIC;
  2414. }
  2415. //
  2416. // If the caller wanted the username validated,
  2417. // do so now.
  2418. //
  2419. WinStatus = NO_ERROR;
  2420. if ( NewCredential.Type == CRED_TYPE_DOMAIN_PASSWORD ||
  2421. (Flags & CREDUI_FLAGS_COMPLETE_USERNAME) != 0 ) {
  2422. if ( !CompleteUserName() ) {
  2423. WinStatus = ERROR_BAD_USERNAME;
  2424. }
  2425. } else if ( Flags & CREDUI_FLAGS_VALIDATE_USERNAME ) {
  2426. WCHAR user[CREDUI_MAX_USERNAME_LENGTH+1];
  2427. WCHAR domain[CREDUI_MAX_USERNAME_LENGTH+1];
  2428. WinStatus = CredUIParseUserNameW( UserName,
  2429. user,
  2430. sizeof(user)/sizeof(WCHAR),
  2431. domain,
  2432. sizeof(domain)/sizeof(WCHAR) );
  2433. if (WinStatus != NO_ERROR) {
  2434. WinStatus = ERROR_BAD_USERNAME;
  2435. }
  2436. }
  2437. //
  2438. // If all is good so far,
  2439. // and we were originally called because of a downgrade detection,
  2440. // and the user typed his logon creds,
  2441. // fail so that the downgrade attacker doesn't "win".
  2442. //
  2443. if ( WinStatus == NO_ERROR &&
  2444. CREDUIP_IS_DOWNGRADE_ERROR( AuthError ) ) {
  2445. WCHAR UserNameBuffer[CRED_MAX_USERNAME_LENGTH+1];
  2446. ULONG UserNameLength;
  2447. //
  2448. // If the typed name is the sam compatible name,
  2449. // fail.
  2450. //
  2451. UserNameLength = sizeof(UserNameBuffer)/sizeof(WCHAR);
  2452. if ( GetUserNameEx( NameSamCompatible,
  2453. UserNameBuffer,
  2454. &UserNameLength ) ) {
  2455. if ( _wcsicmp( UserNameBuffer, UserName ) == 0 ) {
  2456. WinStatus = ERROR_DOWNGRADE_DETECTED;
  2457. }
  2458. }
  2459. //
  2460. // If the typed name is the UPN,
  2461. // fail.
  2462. //
  2463. UserNameLength = sizeof(UserNameBuffer)/sizeof(WCHAR);
  2464. if ( WinStatus == NO_ERROR &&
  2465. wcsrchr( UserName, L'@' ) != NULL &&
  2466. GetUserNameEx( NameUserPrincipal,
  2467. UserNameBuffer,
  2468. &UserNameLength ) ) {
  2469. if ( _wcsicmp( UserNameBuffer, UserName ) == 0 ) {
  2470. WinStatus = ERROR_DOWNGRADE_DETECTED;
  2471. }
  2472. }
  2473. }
  2474. // if (NULL == UserName) CreduiDebugLog("Exit UsernameHandleOK: Username NULL, Flags = %x\n",Flags);
  2475. // else CreduiDebugLog("Exit UsernameHandleOK: UserName = %S, Flags = %x\n", UserName, Flags);
  2476. return WinStatus;
  2477. }
  2478. //=============================================================================
  2479. // CreduiPasswordDialog::HandleOk
  2480. //
  2481. // Returns ERROR_SUCCESS on success, ERROR_NO_SUCH_LOGON_SESSION if credential
  2482. // manager cannot be used to write the credential, or an error from CredWrite
  2483. // or FinishHandleOk.
  2484. //
  2485. // Created 02/25/2000 johnstep (John Stephens)
  2486. //=============================================================================
  2487. DWORD
  2488. CreduiPasswordDialog::HandleOk()
  2489. {
  2490. // Get the user name, password, and other settings from the dialog:
  2491. if (!Credential_GetUserName(CredControlWindow,
  2492. UserName,
  2493. UserNameMaxChars))
  2494. {
  2495. return ERROR_GEN_FAILURE;
  2496. }
  2497. #ifdef SETWILDCARD
  2498. // if is wildcard cred, see if username entered in control matches the
  2499. // cred username. If not, override the old credential.
  2500. if ( CreduiIsSpecialCredential(&NewCredential) )
  2501. {
  2502. if (0 != _wcsicmp(OldUserName,UserName) )
  2503. {
  2504. // Change the TargetName from special cred to explicit target passed by caller
  2505. ZeroMemory(&NewCredential,sizeof NewCredential);
  2506. SetCredTargetFromInfo();
  2507. NewCredential.UserName = UserName;
  2508. }
  2509. }
  2510. #endif
  2511. // If we cannot get the password, then this is probably a certificate
  2512. // so just set a blank password, which will result in no credential
  2513. // blob:
  2514. BOOL gotPassword =
  2515. Credential_GetPassword(CredControlWindow,
  2516. Password,
  2517. PasswordMaxChars);
  2518. if (!gotPassword)
  2519. {
  2520. Password[0] = L'\0';
  2521. }
  2522. //
  2523. // Get the state of the Save checkbox
  2524. //
  2525. if ( CredControlStyle & CRS_SAVECHECK ) {
  2526. Save = Credential_IsSaveChecked(CredControlWindow);
  2527. } else {
  2528. Save = fInitialSaveState;
  2529. }
  2530. DWORD error = ERROR_SUCCESS;
  2531. if (!(Flags & CREDUI_FLAGS_DO_NOT_PERSIST))
  2532. {
  2533. //
  2534. // Only save the credential if the user checked the 'save' checkbox
  2535. //
  2536. if ( Save ) {
  2537. //
  2538. // Keep any existing persistence or use the maximum persistance available.
  2539. //
  2540. if (OldCredential != NULL) {
  2541. NewCredential.Persist = OldCredential->Persist;
  2542. } else {
  2543. NewCredential.Persist = MaximumPersist;
  2544. }
  2545. //
  2546. // The credential might still be saved if the caller didn't asked for the creds
  2547. // to be returned.
  2548. //
  2549. // ISSUE-2000/12/04-CliffV: Such callers are buggy. We should assign bugs and get them fixed.
  2550. //
  2551. } else {
  2552. NewCredential.Persist = CRED_PERSIST_SESSION;
  2553. }
  2554. if ( CredCategory == GENERIC_CATEGORY || PasswordState == PASSWORD_CHANGED )
  2555. {
  2556. // Attempt to write the new credential, first get the length. The blob
  2557. // does not include the terminating NULL:
  2558. NewCredential.CredentialBlobSize = lstrlen(Password) * sizeof (WCHAR);
  2559. }
  2560. else
  2561. {
  2562. NewCredential.CredentialBlobSize = 0;
  2563. }
  2564. // If the password is empty, there is nothing to write:
  2565. if (NewCredential.CredentialBlobSize == 0)
  2566. {
  2567. NewCredential.CredentialBlob = NULL;
  2568. }
  2569. else
  2570. {
  2571. NewCredential.CredentialBlob = reinterpret_cast<BYTE *>(Password);
  2572. }
  2573. }
  2574. //
  2575. // Validate the username
  2576. //
  2577. error = UsernameHandleOk();
  2578. //
  2579. // Save the credentials as required
  2580. //
  2581. if (!(Flags & CREDUI_FLAGS_DO_NOT_PERSIST) &&
  2582. error == ERROR_SUCCESS ) {
  2583. //
  2584. // Only save the credential if the user checked the 'save' checkbox
  2585. //
  2586. // Also store them for callers that didn't ask for creds to be returned
  2587. //
  2588. // Also store them for Branded credenials
  2589. //
  2590. if ( Save ||
  2591. !DelayCredentialWrite ||
  2592. dwIDDResource == IDD_BRANDEDPASSWORD ) {
  2593. error = FinishHandleOk();
  2594. // Bug 230648: CredWrite could come back with ERROR_INVALID_PASSWORD here
  2595. }
  2596. else if ( fInitialSaveState && !Save )
  2597. {
  2598. // user unchecked the save box, delete the cred
  2599. LocalCredDeleteW ( NewCredential.TargetName,
  2600. NewCredential.Type,
  2601. 0 );
  2602. } else {
  2603. error = ERROR_SUCCESS;
  2604. }
  2605. if (error == ERROR_SUCCESS)
  2606. {
  2607. if ( DialogWindow ) {
  2608. EndDialog(DialogWindow, IDOK);
  2609. }
  2610. return error;
  2611. }
  2612. else if (error == ERROR_NO_SUCH_LOGON_SESSION)
  2613. {
  2614. if ( DialogWindow ) {
  2615. EndDialog(DialogWindow, IDCANCEL);
  2616. }
  2617. return error;
  2618. }
  2619. // can still fall out of this block with ERROR_INVALID_PASSWORD
  2620. Credential_SetUserName(CredControlWindow, UserName);
  2621. }
  2622. //
  2623. // If the credential couldn't be written,
  2624. // let the user type a better one.
  2625. //
  2626. if ((error != ERROR_SUCCESS) &&
  2627. (error != ERROR_NO_SUCH_LOGON_SESSION))
  2628. {
  2629. // For some reason we failed to write the credential:
  2630. if ( error == ERROR_DOWNGRADE_DETECTED ) {
  2631. SendMessage(CredControlWindow,
  2632. CRM_SHOWBALLOON,
  2633. 0,
  2634. reinterpret_cast<LPARAM>(&CreduiDowngradeBalloon));
  2635. } else if ( error == ERROR_INVALID_PASSWORD ) {
  2636. // Handler for 230648 !
  2637. //EDITBALLOONTIP structBt = {0};
  2638. //structBt.cbStruct = sizeof(structBt);
  2639. //structBt.pszTitle = PasswordTipTitle;
  2640. //structBt.pszText = PasswordTipText;
  2641. //structBt.ttiIcon = TTI_INFO;
  2642. //Edit_ShowBalloonTip(hwndPassword,&structBt);
  2643. // for now, use preexisting credui mechanism, though that may go away in
  2644. // lieu of shell's global implementation using EM_SHOWBALLOONTIP
  2645. Credential_ShowPasswordBalloon(CredControlWindow,
  2646. TTI_INFO,
  2647. CreduiStrings.PasswordTipTitle,
  2648. CreduiStrings.PasswordTipText);
  2649. } else {
  2650. if ( dwIDDResource == IDD_BRANDEDPASSWORD )
  2651. {
  2652. SendMessage(CredControlWindow,
  2653. CRM_SHOWBALLOON,
  2654. 0,
  2655. reinterpret_cast<LPARAM>(&CreduiEmailNameBalloon));
  2656. }
  2657. else
  2658. {
  2659. SendMessage(CredControlWindow,
  2660. CRM_SHOWBALLOON,
  2661. 0,
  2662. reinterpret_cast<LPARAM>(&CreduiUserNameBalloon));
  2663. }
  2664. }
  2665. }
  2666. return error;
  2667. }
  2668. DWORD CreduiPasswordDialog::CmdlinePasswordPrompt()
  2669. /*++
  2670. Routine Description:
  2671. Command line code get username and password as needed from the user.
  2672. UserName and Password strings set in their respective controls.
  2673. Arguments:
  2674. None
  2675. Return Values:
  2676. Status of the operation.
  2677. --*/
  2678. {
  2679. DWORD WinStatus;
  2680. WCHAR szMsg[CREDUI_MAX_CMDLINE_MSG_LENGTH + 1];
  2681. ULONG LocalUserNameLength = 0;
  2682. WCHAR LocalPassword[CREDUI_MAX_PASSWORD_LENGTH + 1];
  2683. WCHAR LocalUserName[CREDUI_MAX_USERNAME_LENGTH + 1] = {0};
  2684. BOOL bNeedUserNamePrompt = FALSE;
  2685. //
  2686. // Get the username passed into the API
  2687. //
  2688. if (!Credential_GetUserName(CredControlWindow,
  2689. UserName,
  2690. UserNameMaxChars))
  2691. {
  2692. UserName[0] = '\0';
  2693. }
  2694. // FIX352582 - allow update of wildcard creds. Matched wildcard cred presents
  2695. // username. User should see it and be able to override it.
  2696. //bNeedUserNamePrompt = (UserName[0] == '\0');
  2697. // FIX 399728 - prevent username prompting if the name was passed explicitly
  2698. if (fPassedUsername) bNeedUserNamePrompt = FALSE;
  2699. else if (!(Flags & USERNAME_TARGET_CATEGORY)) bNeedUserNamePrompt = TRUE;
  2700. //
  2701. // Loop getting the username until the user types a valid one
  2702. //
  2703. while ( bNeedUserNamePrompt ) {
  2704. //
  2705. // Prompt for username
  2706. //
  2707. if (UserName[0] != 0)
  2708. {
  2709. WCHAR *rgsz[2];
  2710. rgsz[0] = UserName;
  2711. rgsz[1] = TargetName;
  2712. szMsg[0] = 0;
  2713. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2714. CreduiInstance,
  2715. IDS_PASSEDNAME_PROMPT,
  2716. 0,
  2717. szMsg,
  2718. CREDUI_MAX_CMDLINE_MSG_LENGTH,
  2719. (va_list *) rgsz);
  2720. }
  2721. else
  2722. {
  2723. WCHAR *rgsz[2];
  2724. rgsz[0] = TargetName;
  2725. rgsz[1] = 0;
  2726. szMsg[0] = 0;
  2727. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2728. CreduiInstance,
  2729. IDS_USERNAME_PROMPT,
  2730. 0,
  2731. szMsg,
  2732. CREDUI_MAX_CMDLINE_MSG_LENGTH,
  2733. (va_list *) rgsz);
  2734. }
  2735. szMsg[CREDUI_MAX_CMDLINE_MSG_LENGTH] = L'\0';
  2736. CredPutStdout( szMsg );
  2737. CredGetStdin( LocalUserName, UserNameMaxChars, TRUE );
  2738. LocalUserNameLength = wcslen( LocalUserName );
  2739. // If nothing entered and nothing passed, bail.
  2740. // If nothing entered and a previous value exists, use previous value unchanged
  2741. // else use overwrite old value with new value
  2742. if ( LocalUserNameLength == 0 ) {
  2743. if (UserName[0] == '\0')
  2744. {
  2745. WinStatus = ERROR_CANCELLED;
  2746. goto Cleanup;
  2747. }
  2748. }
  2749. else
  2750. wcscpy(UserName, LocalUserName);
  2751. CreduiDebugLog("CreduiCredentialControl::CmdlinePasswordPrompt: "
  2752. "Username : %s\n",
  2753. UserName );
  2754. //
  2755. // See if the username is valid (and optional complete the new name)
  2756. //
  2757. WinStatus = UsernameHandleOk();
  2758. if ( WinStatus != NO_ERROR ) {
  2759. // invalid username, put up message and try again
  2760. if ( WinStatus == ERROR_DOWNGRADE_DETECTED ) {
  2761. szMsg[0] = 0;
  2762. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2763. CreduiInstance,
  2764. IDS_DOWNGRADE_CMD_TEXT,
  2765. 0,
  2766. szMsg,
  2767. CREDUI_MAX_CMDLINE_MSG_LENGTH,
  2768. NULL);
  2769. CredPutStdout(szMsg);
  2770. } else {
  2771. szMsg[0] = 0;
  2772. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2773. CreduiInstance,
  2774. IDS_INVALID_USERNAME,
  2775. 0,
  2776. szMsg,
  2777. CREDUI_MAX_CMDLINE_MSG_LENGTH,
  2778. NULL);
  2779. CredPutStdout(szMsg);
  2780. }
  2781. CredPutStdout( L"\n" );
  2782. continue;
  2783. }
  2784. //
  2785. // Save the username in the control as though the GUI prompted for it
  2786. //
  2787. if ( !Credential_SetUserName( CredControlWindow, UserName ) ) {
  2788. WinStatus = GetLastError();
  2789. CreduiDebugLog("CreduiCredentialControl::CmdlinePasswordPrompt: "
  2790. "OnSetUserName failed: %u\n",
  2791. WinStatus );
  2792. goto Cleanup;
  2793. }
  2794. break;
  2795. }
  2796. //
  2797. // Prompt for a password
  2798. //
  2799. //FIX216477 detect marshalled name and change the prompt string
  2800. // to a generic one for any certificate
  2801. if (CredIsMarshaledCredentialW( UserName )) {
  2802. {
  2803. WCHAR *rgsz[2];
  2804. rgsz[0] = TargetName;
  2805. szMsg[0] = 0;
  2806. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2807. CreduiInstance,
  2808. IDS_CERTIFICATE_PROMPT,
  2809. 0,
  2810. szMsg,
  2811. CREDUI_MAX_CMDLINE_MSG_LENGTH,
  2812. (va_list *) rgsz);
  2813. }
  2814. }
  2815. else if ((Flags & USERNAME_TARGET_CATEGORY) || (LocalUserNameLength != 0))
  2816. {
  2817. WCHAR *rgsz[2];
  2818. rgsz[0] = TargetName;
  2819. szMsg[0] = 0;
  2820. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2821. CreduiInstance,
  2822. IDS_SIMPLEPASSWORD_PROMPT,
  2823. 0,
  2824. szMsg,
  2825. CREDUI_MAX_CMDLINE_MSG_LENGTH,
  2826. (va_list *) rgsz);
  2827. } else {
  2828. WCHAR *rgsz[2];
  2829. rgsz[0] = UserName;
  2830. rgsz[1] = TargetName;
  2831. szMsg[0] = 0;
  2832. FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2833. CreduiInstance,
  2834. IDS_PASSWORD_PROMPT,
  2835. 0,
  2836. szMsg,
  2837. CREDUI_MAX_CMDLINE_MSG_LENGTH,
  2838. (va_list *) rgsz);
  2839. szMsg[CREDUI_MAX_CMDLINE_MSG_LENGTH] = L'\0';
  2840. }
  2841. CredPutStdout( szMsg );
  2842. CredGetStdin( LocalPassword, CREDUI_MAX_PASSWORD_LENGTH, FALSE );
  2843. //
  2844. // Save the password in the control as though the GUI prompted for it
  2845. //
  2846. if ( !Credential_SetPassword( CredControlWindow, LocalPassword ) ) {
  2847. WinStatus = GetLastError();
  2848. CreduiDebugLog("CreduiCredentialControl::CmdlinePasswordPrompt: "
  2849. "OnSetPassword failed: %u\n",
  2850. WinStatus );
  2851. goto Cleanup;
  2852. }
  2853. WinStatus = NO_ERROR;
  2854. //
  2855. // Tell our parent window that we're done prompting
  2856. //
  2857. Cleanup:
  2858. ZeroMemory( LocalPassword, sizeof(LocalPassword) );
  2859. return WinStatus;
  2860. }
  2861. //=============================================================================
  2862. // CreduiPasswordDialog::CmdLineMessageHandlerCallback
  2863. //
  2864. //
  2865. // The command line message handler function
  2866. //
  2867. // Arguments:
  2868. // Window (in)
  2869. // message (in)
  2870. // wParam (in)
  2871. // lParam (in)
  2872. //
  2873. //=============================================================================
  2874. LRESULT
  2875. CALLBACK
  2876. CreduiPasswordDialog::CmdLineMessageHandlerCallback(
  2877. HWND window,
  2878. UINT message,
  2879. WPARAM wParam,
  2880. LPARAM lParam
  2881. )
  2882. {
  2883. // CreduiDebugLog( "CmdLine Callback: %8.8lx %8.8lx %8.8lx\n", message, wParam, lParam );
  2884. // on window message, retrieve object ptr from window data ptr
  2885. // call class object handler function.
  2886. CreduiPasswordDialog *that =
  2887. reinterpret_cast<CreduiPasswordDialog *>(
  2888. GetWindowLongPtr(window, GWLP_USERDATA));
  2889. if (that != NULL) {
  2890. ASSERT(window == that->CmdLineWindow);
  2891. return that->CmdLineMessageHandler(message, wParam, lParam);
  2892. }
  2893. if (message == WM_CREATE)
  2894. {
  2895. DWORD WinStatus;
  2896. LPCREATESTRUCT lpCreateStruct = (LPCREATESTRUCT)lParam;
  2897. that = (CreduiPasswordDialog *)lpCreateStruct->lpCreateParams;
  2898. if (that != NULL) {
  2899. //
  2900. // Initialize the window
  2901. //
  2902. if (!that->InitWindow( window )) {
  2903. PostQuitMessage( ERROR_CANCELLED );
  2904. return 0;
  2905. }
  2906. SetWindowPos ( window, HWND_BOTTOM, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED );
  2907. //
  2908. // For passwords,
  2909. // prompt here.
  2910. //
  2911. // It is better to prompt here than in the control window since
  2912. // command line has to do user name completion and that is done at this layer.
  2913. //
  2914. if ( (that->Flags & CREDUI_FLAGS_REQUIRE_SMARTCARD) == 0 ) {
  2915. WinStatus = that->CmdlinePasswordPrompt();
  2916. if ( WinStatus != NO_ERROR ) {
  2917. PostQuitMessage( WinStatus );
  2918. return 0;
  2919. }
  2920. }
  2921. //
  2922. // For smartcards,
  2923. // Prompt in the control window since it supports smart card enumeration
  2924. // For password,
  2925. // prompt where to save the cred.
  2926. //
  2927. WinStatus = (DWORD) SendMessage(that->CredControlWindow, CRM_DOCMDLINE, 0, (LPARAM)that->TargetName );
  2928. if ( WinStatus != NO_ERROR ) {
  2929. PostQuitMessage( WinStatus );
  2930. return 0;
  2931. }
  2932. }
  2933. return 0;
  2934. }
  2935. return DefWindowProc(window, message, wParam, lParam);
  2936. }
  2937. //=============================================================================
  2938. // CreduiPasswordDialog::CmdLineMessageHandler
  2939. //
  2940. // Called from the control window callback to handle the window messages.
  2941. //
  2942. // Arguments:
  2943. // message (in)
  2944. // wParam (in)
  2945. // lParam (in)
  2946. //
  2947. //=============================================================================
  2948. LRESULT
  2949. CreduiPasswordDialog::CmdLineMessageHandler(
  2950. UINT message,
  2951. WPARAM wParam,
  2952. LPARAM lParam
  2953. )
  2954. {
  2955. return SendMessage( CredControlWindow, message, wParam, lParam );
  2956. }
  2957. //=============================================================================
  2958. // CreduiPasswordDialog::DialogMessageHandlerCallback
  2959. //
  2960. // This is the actual callback function for the dialog window. On the intial
  2961. // create, this handles WM_INITDIALOG. After that, it is only responsible for
  2962. // getting the this pointer and calling DialogMessageHandler.
  2963. //
  2964. // Arguments:
  2965. // dialogWindow (in)
  2966. // message (in)
  2967. // wParam (in)
  2968. // lParam (in)
  2969. //
  2970. // Returns TRUE if the message was handled or FALSE otherwise. FALSE is also
  2971. // returned in special cases such as WM_INITDIALOG.
  2972. //
  2973. // Created 02/25/2000 johnstep (John Stephens)
  2974. //=============================================================================
  2975. INT_PTR
  2976. CALLBACK
  2977. CreduiPasswordDialog::DialogMessageHandlerCallback(
  2978. HWND dialogWindow,
  2979. UINT message,
  2980. WPARAM wParam,
  2981. LPARAM lParam
  2982. )
  2983. {
  2984. //CreduiDebugLog( "Dialog Callback: %8.8lx %8.8lx %8.8lx\n", message, wParam, lParam );
  2985. CreduiPasswordDialog *that =
  2986. reinterpret_cast<CreduiPasswordDialog *>(
  2987. GetWindowLongPtr(dialogWindow, GWLP_USERDATA));
  2988. if (that != NULL)
  2989. {
  2990. ASSERT(dialogWindow == that->DialogWindow);
  2991. return that->DialogMessageHandler(message, wParam, lParam);
  2992. }
  2993. if (message == WM_INITDIALOG)
  2994. {
  2995. that = reinterpret_cast<CreduiPasswordDialog *>(lParam);
  2996. if (that != NULL)
  2997. {
  2998. if (!that->InitWindow(dialogWindow))
  2999. {
  3000. EndDialog(dialogWindow, IDCANCEL);
  3001. }
  3002. }
  3003. }
  3004. return FALSE;
  3005. }
  3006. //=============================================================================
  3007. // CreduiPasswordDialog::DialogMessageHandler
  3008. //
  3009. // Called from the dialog window callback to handle the window messages.
  3010. //
  3011. // Arguments:
  3012. // message (in)
  3013. // wParam (in)
  3014. // lParam (in)
  3015. //
  3016. // Returns TRUE if the message was handled or FALSE otherwise.
  3017. //
  3018. // Created 02/25/2000 johnstep (John Stephens)
  3019. //=============================================================================
  3020. INT_PTR
  3021. CreduiPasswordDialog::DialogMessageHandler(
  3022. UINT message,
  3023. WPARAM wParam,
  3024. LPARAM lParam
  3025. )
  3026. {
  3027. switch (message)
  3028. {
  3029. case WM_NOTIFY:
  3030. {
  3031. int idCtrl = (int)wParam;
  3032. LPNMHDR pnmh = (LPNMHDR)lParam;
  3033. switch (pnmh->code)
  3034. {
  3035. case NM_CLICK:
  3036. case NM_RETURN:
  3037. switch (idCtrl)
  3038. {
  3039. case IDC_GETALINK:
  3040. {
  3041. DWORD dwResult;
  3042. if ( TryLauchRegWizard ( &SSOPackage, DialogWindow, TRUE /* HasLogonSession */,
  3043. UserName, UserNameMaxChars,
  3044. Password, PasswordMaxChars,
  3045. &dwResult ))
  3046. {
  3047. // end dialog, Result
  3048. if ( dwResult == ERROR_SUCCESS )
  3049. {
  3050. EndDialog(DialogWindow, IDOK);
  3051. return TRUE;
  3052. }
  3053. }
  3054. else
  3055. {
  3056. // if we couldn't launch the wizard, try the web page.
  3057. if ( wcslen(SSOPackage.szRegURL) > 0 )
  3058. {
  3059. ShellExecute ( NULL,
  3060. NULL,
  3061. SSOPackage.szRegURL,
  3062. NULL,
  3063. NULL,
  3064. SW_SHOWNORMAL );
  3065. }
  3066. }
  3067. }
  3068. break;
  3069. case IDC_HELPLINK:
  3070. if ( wcslen(SSOPackage.szHelpURL) > 0 )
  3071. {
  3072. ShellExecute ( NULL,
  3073. NULL,
  3074. SSOPackage.szHelpURL,
  3075. NULL,
  3076. NULL,
  3077. SW_SHOWNORMAL );
  3078. }
  3079. break;
  3080. }
  3081. }
  3082. }
  3083. break;
  3084. case WM_COMMAND:
  3085. switch (LOWORD(wParam))
  3086. {
  3087. case IDOK:
  3088. Result = HandleOk();
  3089. if (Result != ERROR_SUCCESS)
  3090. {
  3091. if (Result == ERROR_NO_SUCH_LOGON_SESSION)
  3092. {
  3093. wParam = IDCANCEL;
  3094. }
  3095. else
  3096. {
  3097. return TRUE;
  3098. }
  3099. }
  3100. // Fall through...
  3101. case IDCANCEL:
  3102. EndDialog(DialogWindow, LOWORD(wParam));
  3103. return TRUE;
  3104. case IDC_CRED:
  3105. if (HIWORD(wParam) == CRN_USERNAMECHANGE)
  3106. {
  3107. BOOL enable = TRUE;
  3108. LONG length = Credential_GetUserNameLength(CredControlWindow);
  3109. // Validate the user name:
  3110. if ( CredCategory == DOMAIN_CATEGORY &&
  3111. ((TargetInfo == NULL) ||
  3112. !(TargetInfo->Flags & CRED_TI_ONLY_PASSWORD_REQUIRED)))
  3113. {
  3114. enable = length > 0;
  3115. }
  3116. else
  3117. {
  3118. enable = length != -1;
  3119. }
  3120. if (enable)
  3121. {
  3122. EnableWindow(GetDlgItem(DialogWindow, IDOK), TRUE);
  3123. DisabledControlMask &= ~DISABLED_CONTROL_OK;
  3124. }
  3125. else
  3126. {
  3127. EnableWindow(GetDlgItem(DialogWindow, IDOK), FALSE);
  3128. DisabledControlMask |= DISABLED_CONTROL_OK;
  3129. }
  3130. }
  3131. else if (HIWORD(wParam) == CRN_PASSWORDCHANGE)
  3132. {
  3133. if (PasswordState == PASSWORD_INIT)
  3134. {
  3135. PasswordState = PASSWORD_CHANGED;
  3136. }
  3137. }
  3138. return TRUE;
  3139. }
  3140. break;
  3141. case WM_ENTERSIZEMOVE:
  3142. Credential_HideBalloon(CredControlWindow);
  3143. return TRUE;
  3144. case WM_PAINT:
  3145. if (FirstPaint && GetUpdateRect(DialogWindow, NULL, FALSE))
  3146. {
  3147. FirstPaint = FALSE;
  3148. if (CredBalloon != NULL)
  3149. {
  3150. SendMessage(CredControlWindow,
  3151. CRM_SHOWBALLOON,
  3152. 0,
  3153. reinterpret_cast<LPARAM>(CredBalloon));
  3154. }
  3155. CredBalloon = NULL;
  3156. }
  3157. break;
  3158. }
  3159. return FALSE;
  3160. }
  3161. // looks for an existing cred with the same name as that of pNewCredential and demotes it to
  3162. // username only
  3163. void DemoteOldDefaultSSOCred (
  3164. PCREDENTIAL_TARGET_INFORMATION pTargetInfo, // target info of new cred
  3165. DWORD Flags
  3166. )
  3167. {
  3168. CREDENTIALW **credentialSet = NULL;
  3169. DWORD count;
  3170. if ( pTargetInfo == NULL )
  3171. return;
  3172. if (LocalCredReadDomainCredentialsW( pTargetInfo, 0, &count,
  3173. &credentialSet))
  3174. {
  3175. PCREDENTIAL pOldCredential = NULL;
  3176. for ( DWORD i = 0; i < count; i++ )
  3177. {
  3178. #ifndef SETWILDCARD
  3179. //
  3180. // Ignore RAS and wildcard credentials,
  3181. // we never want credUI to change such a credential.
  3182. //
  3183. if ( CreduiIsSpecialCredential(credentialSet[i]) )
  3184. {
  3185. continue;
  3186. }
  3187. #endif
  3188. //
  3189. // CredReadDomain domain credentials returns creds in preference
  3190. // order as specified by the TargetInfo.
  3191. // So use the first valid one.
  3192. //
  3193. if ( credentialSet[i]->Type == CRED_TYPE_DOMAIN_VISIBLE_PASSWORD )
  3194. {
  3195. pOldCredential = credentialSet[i];
  3196. break;
  3197. }
  3198. }
  3199. if ( pOldCredential != NULL )
  3200. {
  3201. // save this under the username
  3202. pOldCredential->Persist = CRED_PERSIST_ENTERPRISE;
  3203. pOldCredential->CredentialBlob = NULL;
  3204. pOldCredential->CredentialBlobSize = 0;
  3205. pOldCredential->TargetName = pOldCredential->UserName;
  3206. pOldCredential->Flags = CRED_FLAGS_USERNAME_TARGET;
  3207. WriteCred( pOldCredential->UserName,
  3208. Flags,
  3209. NULL,
  3210. pOldCredential,
  3211. 0,
  3212. FALSE,
  3213. FALSE);
  3214. }
  3215. }
  3216. if (credentialSet != NULL)
  3217. {
  3218. LocalCredFree(static_cast<VOID *>(credentialSet));
  3219. }
  3220. }