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.

272 lines
8.5 KiB

  1. #include "stdafx.h"
  2. #include "password.h"
  3. #pragma hdrstop
  4. // password prompt dialog
  5. INT_PTR CPasswordDialog::DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  6. {
  7. switch (uMsg)
  8. {
  9. HANDLE_MSG(hwnd, WM_INITDIALOG, OnInitDialog);
  10. HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
  11. default:
  12. break;
  13. }
  14. return FALSE;
  15. }
  16. BOOL CPasswordDialog::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  17. {
  18. TCHAR szMessage[MAX_PATH + MAX_DOMAIN + MAX_USER + 256 + 2]; szMessage[0] = 0;
  19. //
  20. // Limit the size of the edit controls + Set username/password
  21. //
  22. HWND hwndCredential = GetDlgItem(hwnd, IDC_CREDENTIALS);
  23. SendMessage(hwndCredential, CRM_SETUSERNAME, NULL, (LPARAM) m_pszDomainUser);
  24. SendMessage(hwndCredential, CRM_SETPASSWORD, NULL, (LPARAM) m_pszPassword);
  25. SendMessage(hwndCredential, CRM_SETUSERNAMEMAX, m_cchDomainUser - 1, NULL);
  26. SendMessage(hwndCredential, CRM_SETPASSWORDMAX, m_cchPassword - 1, NULL);
  27. // We may need to generate a user name here to use if no user name was
  28. // passed in
  29. TCHAR szDomainUser[MAX_DOMAIN + MAX_USER + 2];
  30. LPTSTR pszUserNameToUse;
  31. if (*m_pszDomainUser)
  32. {
  33. pszUserNameToUse = m_pszDomainUser;
  34. }
  35. else
  36. {
  37. szDomainUser[0] = 0;
  38. TCHAR szUser[MAX_USER + 1];
  39. DWORD cchUser = ARRAYSIZE(szUser);
  40. TCHAR szDomain[MAX_DOMAIN + 1];
  41. DWORD cchDomain = ARRAYSIZE(szDomain);
  42. GetCurrentUserAndDomainName(szUser, &cchUser, szDomain, &cchDomain);
  43. MakeDomainUserString(szDomain, szUser, szDomainUser, ARRAYSIZE(szDomainUser));
  44. pszUserNameToUse = szDomainUser;
  45. }
  46. FormatMessageString(IDS_PWD_STATIC, szMessage, ARRAYSIZE(szMessage), m_pszResourceName, pszUserNameToUse);
  47. SetDlgItemText(hwnd, IDC_MESSAGE, szMessage);
  48. // Now set the error message description
  49. TCHAR szError[512];
  50. if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, m_dwError, 0, szError, ARRAYSIZE(szError), NULL))
  51. {
  52. LoadString(g_hinst, IDS_ERR_UNEXPECTED, szError, ARRAYSIZE(szError));
  53. }
  54. SetDlgItemText(hwnd, IDC_ERROR, szError);
  55. return TRUE;
  56. }
  57. BOOL CPasswordDialog::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  58. {
  59. switch(id)
  60. {
  61. case IDOK:
  62. {
  63. // Read the username and password from the dialog. Note
  64. // that we don't call FetchText for the password since it
  65. // strips leading and trailing whitespace and, for all we
  66. // know, that could be an important part of the password
  67. SendDlgItemMessage(hwnd, IDC_CREDENTIALS, CRM_GETUSERNAME, (WPARAM) m_cchDomainUser - 1, (LPARAM) m_pszDomainUser);
  68. SendDlgItemMessage(hwnd, IDC_CREDENTIALS, CRM_GETPASSWORD, (WPARAM) m_cchPassword - 1, (LPARAM) m_pszPassword);
  69. }
  70. // Fall through
  71. case IDCANCEL:
  72. EndDialog(hwnd, id);
  73. return TRUE;
  74. }
  75. return FALSE;
  76. }
  77. // page implementation - used for wizards etc
  78. BOOL CPasswordPageBase::DoPasswordsMatch(HWND hwnd)
  79. {
  80. TCHAR szConfirmPW[MAX_PASSWORD + 1];
  81. TCHAR szPassword[MAX_PASSWORD + 1];
  82. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD), szPassword, ARRAYSIZE(szPassword));
  83. GetWindowText(GetDlgItem(hwnd, IDC_CONFIRMPASSWORD), szConfirmPW,ARRAYSIZE(szConfirmPW));
  84. BOOL fMatch = (StrCmp(szPassword, szConfirmPW) == 0);
  85. if (!fMatch)
  86. {
  87. // Display a message saying the passwords don't match
  88. DisplayFormatMessage(hwnd, IDS_USR_NEWUSERWIZARD_CAPTION, IDS_ERR_PWDNOMATCH, MB_OK | MB_ICONERROR);
  89. }
  90. return fMatch;
  91. }
  92. INT_PTR CPasswordWizardPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  93. {
  94. switch (uMsg)
  95. {
  96. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  97. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  98. HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  99. }
  100. return FALSE;
  101. }
  102. BOOL CPasswordWizardPage::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  103. {
  104. Edit_LimitText(GetDlgItem(hwnd, IDC_PASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  105. Edit_LimitText(GetDlgItem(hwnd, IDC_CONFIRMPASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  106. return TRUE;
  107. }
  108. BOOL CPasswordWizardPage::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  109. {
  110. switch (id)
  111. {
  112. case IDOK:
  113. // Verify that the passwords match
  114. if (DoPasswordsMatch(hwnd))
  115. {
  116. // Password is the same as confirm password - read password into user info
  117. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD), m_pUserInfo->m_szPasswordBuffer,
  118. ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer));
  119. // Hide the password
  120. m_pUserInfo->HidePassword();
  121. EndDialog(hwnd, IDOK);
  122. }
  123. else
  124. {
  125. m_pUserInfo->ZeroPassword();
  126. }
  127. break;
  128. case IDCANCEL:
  129. EndDialog(hwnd, IDCANCEL);
  130. break;
  131. default:
  132. break;
  133. }
  134. return TRUE;
  135. }
  136. BOOL CPasswordWizardPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  137. {
  138. switch (pnmh->code)
  139. {
  140. case PSN_SETACTIVE:
  141. PropSheet_SetWizButtons(pnmh->hwndFrom, PSWIZB_NEXT | PSWIZB_BACK);
  142. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, 0);
  143. return TRUE;
  144. case PSN_WIZNEXT:
  145. {
  146. // Save the data the user has entered
  147. if (DoPasswordsMatch(hwnd))
  148. {
  149. // Password is the same as confirm password - read password into user info
  150. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD),
  151. m_pUserInfo->m_szPasswordBuffer,
  152. ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer));
  153. // Hide the password
  154. m_pUserInfo->HidePassword();
  155. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, 0);
  156. }
  157. else
  158. {
  159. m_pUserInfo->ZeroPassword();
  160. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, -1);
  161. }
  162. return TRUE;
  163. }
  164. }
  165. return FALSE;
  166. }
  167. INT_PTR CChangePasswordDlg::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  168. {
  169. switch (uMsg)
  170. {
  171. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  172. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  173. }
  174. return FALSE;
  175. }
  176. BOOL CChangePasswordDlg::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  177. {
  178. Edit_LimitText(GetDlgItem(hwnd, IDC_PASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  179. Edit_LimitText(GetDlgItem(hwnd, IDC_CONFIRMPASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  180. return TRUE;
  181. }
  182. BOOL CChangePasswordDlg::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  183. {
  184. switch (id)
  185. {
  186. case IDOK:
  187. if (DoPasswordsMatch(hwnd))
  188. {
  189. // Password is the same as confirm password - read password into user info
  190. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD), m_pUserInfo->m_szPasswordBuffer,
  191. ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer));
  192. m_pUserInfo->HidePassword(); // Hide the password
  193. // Update the password
  194. BOOL fBadPasswordFormat;
  195. if (SUCCEEDED(m_pUserInfo->UpdatePassword(&fBadPasswordFormat)))
  196. {
  197. EndDialog(hwnd, IDOK);
  198. }
  199. else
  200. {
  201. TCHAR szDomainUser[MAX_DOMAIN + MAX_USER + 2];
  202. MakeDomainUserString(m_pUserInfo->m_szDomain, m_pUserInfo->m_szUsername,
  203. szDomainUser, ARRAYSIZE(szDomainUser));
  204. if (fBadPasswordFormat)
  205. {
  206. ::DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION,
  207. IDS_USR_UPDATE_PASSWORD_TOOSHORT_ERROR, MB_ICONERROR | MB_OK,
  208. szDomainUser);
  209. }
  210. else
  211. {
  212. ::DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION,
  213. IDS_USR_UPDATE_PASSWORD_ERROR, MB_ICONERROR | MB_OK,
  214. szDomainUser);
  215. }
  216. }
  217. }
  218. break;
  219. case IDCANCEL:
  220. EndDialog(hwnd, IDCANCEL);
  221. break;
  222. default:
  223. break;
  224. }
  225. return TRUE;
  226. }