Leaked source code of windows server 2003
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.

178 lines
4.8 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1998.
  5. //
  6. // File: password.cxx
  7. //
  8. // Contents: Implementation of class used to prompt user for credentials.
  9. //
  10. // Classes: CPasswordDialog
  11. //
  12. // History: 02-09-1998 DavidMun Created
  13. //
  14. //---------------------------------------------------------------------------
  15. #include "headers.hxx"
  16. #include <wincred.h>
  17. #include <wincrui.h>
  18. #pragma hdrstop
  19. //+--------------------------------------------------------------------------
  20. //
  21. // Member: CPasswordDialog::DoModalDialog
  22. //
  23. // Synopsis: Invoke the name and password dialog as a modal dialog.
  24. //
  25. // Arguments: [hwndParent] - dialog parent.
  26. //
  27. // Returns: S_OK - user entered name & password and hit OK
  28. // S_FALSE - user hit cancel
  29. //
  30. // History: 02-09-1998 DavidMun Created
  31. //
  32. //---------------------------------------------------------------------------
  33. HRESULT
  34. CPasswordDialog::DoModalDialog(
  35. HWND hwndParent)
  36. {
  37. TRACE_METHOD(CPasswordDialog, DoModalDialog);
  38. HRESULT hr = S_OK;
  39. //
  40. // If the target is being accessed via WinNT provider, show the example
  41. // with just the nt4 style user name, otherwise show
  42. // the example with both UPN and NT4 style user names.
  43. //
  44. String strExample;
  45. if (m_flProvider != PROVIDER_WINNT)
  46. {
  47. strExample = String::load(IDS_EXAMPLE_UPN_NT4, g_hinst);
  48. }
  49. else
  50. {
  51. strExample = String::load(IDS_EXAMPLE_NT4, g_hinst);
  52. }
  53. //
  54. //Form the credui message
  55. //
  56. String strFormat = String::load((int)IDS_CREDUI_MESSAGE, g_hinst);
  57. String strMessage = String::format(strFormat, m_wzTarget.c_str(), strExample.c_str());
  58. String strTitle = String::load(IDS_CREDUI_TITLE, g_hinst);
  59. //
  60. //Init uiInfo
  61. //
  62. CREDUI_INFO uiInfo;
  63. //REVIEWED-2002-02-21-lucios.
  64. ::ZeroMemory( &uiInfo, sizeof(CREDUI_INFO) );
  65. uiInfo.cbSize = sizeof(uiInfo);
  66. uiInfo.hwndParent = hwndParent;
  67. uiInfo.pszMessageText = strMessage.c_str();
  68. uiInfo.pszCaptionText = strTitle.c_str();
  69. TCHAR achUserName[CREDUI_MAX_USERNAME_LENGTH + 1];
  70. TCHAR achPassword[CREDUI_MAX_PASSWORD_LENGTH + 1];
  71. //REVIEWED-2002-02-21-lucios.
  72. ::ZeroMemory(achUserName,sizeof(achUserName));
  73. ::SecureZeroMemory(achPassword,sizeof(achPassword));
  74. do
  75. {
  76. //
  77. //Show the password dialog box
  78. //
  79. DWORD dwErr = CredUIPromptForCredentials(&uiInfo,
  80. NULL,
  81. NULL,
  82. NO_ERROR,
  83. achUserName,
  84. CREDUI_MAX_USERNAME_LENGTH,
  85. achPassword,
  86. CREDUI_MAX_PASSWORD_LENGTH,
  87. NULL,
  88. CREDUI_FLAGS_DO_NOT_PERSIST | CREDUI_FLAGS_GENERIC_CREDENTIALS);
  89. if (NO_ERROR != dwErr) // e.g. S_FALSE
  90. {
  91. if(dwErr == ERROR_CANCELLED)
  92. hr = S_FALSE;
  93. else
  94. {
  95. hr = HRESULT_FROM_WIN32(dwErr);
  96. Dbg(DEB_ERROR,
  97. "CredUIPromptForCredentials Failed\n");
  98. DBG_OUT_HRESULT(hr);
  99. }
  100. break;
  101. }
  102. }while(!_ValidateName(hwndParent, achUserName));
  103. if(hr == S_OK)
  104. {
  105. // NTRAID#NTBUG9-548215-2002/02/20-lucios.
  106. *m_userName=achUserName;
  107. m_password->Encrypt(achPassword);
  108. }
  109. //REVIEWED-2002-02-21-lucios.
  110. ::ZeroMemory(achUserName,sizeof(achUserName));
  111. ::SecureZeroMemory(achPassword,sizeof(achPassword));
  112. return hr;
  113. }
  114. //+--------------------------------------------------------------------------
  115. //
  116. // Member: CPasswordDialog::_ValidateName
  117. //
  118. // Synopsis: Ensure that the form of the name the user entered is valid
  119. // for the provider being used to access the resource.
  120. //
  121. // Returns: TRUE if name valid
  122. // FALSE if name not valid
  123. //
  124. // History: 01-11-2000 davidmun Created
  125. //
  126. // Notes: Displays error if name not valid
  127. //
  128. //---------------------------------------------------------------------------
  129. BOOL
  130. CPasswordDialog::_ValidateName(HWND hwnd, LPWSTR pwzUserName)
  131. {
  132. if (pwzUserName && !*pwzUserName)
  133. {
  134. return FALSE; // bug if we get here
  135. }
  136. //
  137. // If provider is not WinNT, any nonempty name is valid
  138. //
  139. if (m_flProvider != PROVIDER_WINNT)
  140. {
  141. return TRUE;
  142. }
  143. // NTRAID#NTBUG9-506139-2002/02/04-lucios
  144. // Removed the checking for UPN format names
  145. // for WinNT providers, since smartcards
  146. // can have '@'. Also, checking only for '@'
  147. // doesn't garantee that the name is UPN.
  148. // We let the WinNT provider fail.
  149. return TRUE;
  150. }