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.

269 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.
  64. SendDlgItemMessage(hwnd, IDC_CREDENTIALS, CRM_GETUSERNAME, (WPARAM) m_cchDomainUser - 1, (LPARAM) m_pszDomainUser);
  65. SendDlgItemMessage(hwnd, IDC_CREDENTIALS, CRM_GETPASSWORD, (WPARAM) m_cchPassword - 1, (LPARAM) m_pszPassword);
  66. }
  67. // Fall through
  68. case IDCANCEL:
  69. EndDialog(hwnd, id);
  70. return TRUE;
  71. }
  72. return FALSE;
  73. }
  74. // page implementation - used for wizards etc
  75. BOOL CPasswordPageBase::DoPasswordsMatch(HWND hwnd)
  76. {
  77. TCHAR szConfirmPW[MAX_PASSWORD + 1];
  78. TCHAR szPassword[MAX_PASSWORD + 1];
  79. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD), szPassword, ARRAYSIZE(szPassword));
  80. GetWindowText(GetDlgItem(hwnd, IDC_CONFIRMPASSWORD), szConfirmPW,ARRAYSIZE(szConfirmPW));
  81. BOOL fMatch = (StrCmp(szPassword, szConfirmPW) == 0);
  82. if (!fMatch)
  83. {
  84. // Display a message saying the passwords don't match
  85. DisplayFormatMessage(hwnd, IDS_USR_NEWUSERWIZARD_CAPTION, IDS_ERR_PWDNOMATCH, MB_OK | MB_ICONERROR);
  86. }
  87. return fMatch;
  88. }
  89. INT_PTR CPasswordWizardPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  90. {
  91. switch (uMsg)
  92. {
  93. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  94. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  95. HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  96. }
  97. return FALSE;
  98. }
  99. BOOL CPasswordWizardPage::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  100. {
  101. Edit_LimitText(GetDlgItem(hwnd, IDC_PASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  102. Edit_LimitText(GetDlgItem(hwnd, IDC_CONFIRMPASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  103. return TRUE;
  104. }
  105. BOOL CPasswordWizardPage::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  106. {
  107. switch (id)
  108. {
  109. case IDOK:
  110. // Verify that the passwords match
  111. if (DoPasswordsMatch(hwnd))
  112. {
  113. // Password is the same as confirm password - read password into user info
  114. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD), m_pUserInfo->m_szPasswordBuffer,
  115. ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer));
  116. // Hide the password
  117. m_pUserInfo->HidePassword();
  118. EndDialog(hwnd, IDOK);
  119. }
  120. else
  121. {
  122. m_pUserInfo->ZeroPassword();
  123. }
  124. break;
  125. case IDCANCEL:
  126. EndDialog(hwnd, IDCANCEL);
  127. break;
  128. default:
  129. break;
  130. }
  131. return TRUE;
  132. }
  133. BOOL CPasswordWizardPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  134. {
  135. switch (pnmh->code)
  136. {
  137. case PSN_SETACTIVE:
  138. PropSheet_SetWizButtons(pnmh->hwndFrom, PSWIZB_NEXT | PSWIZB_BACK);
  139. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, 0);
  140. return TRUE;
  141. case PSN_WIZNEXT:
  142. {
  143. // Save the data the user has entered
  144. if (DoPasswordsMatch(hwnd))
  145. {
  146. // Password is the same as confirm password - read password into user info
  147. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD),
  148. m_pUserInfo->m_szPasswordBuffer,
  149. ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer));
  150. // Hide the password
  151. m_pUserInfo->HidePassword();
  152. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, 0);
  153. }
  154. else
  155. {
  156. m_pUserInfo->ZeroPassword();
  157. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, -1);
  158. }
  159. return TRUE;
  160. }
  161. }
  162. return FALSE;
  163. }
  164. INT_PTR CChangePasswordDlg::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  165. {
  166. switch (uMsg)
  167. {
  168. HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  169. HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  170. }
  171. return FALSE;
  172. }
  173. BOOL CChangePasswordDlg::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  174. {
  175. Edit_LimitText(GetDlgItem(hwnd, IDC_PASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  176. Edit_LimitText(GetDlgItem(hwnd, IDC_CONFIRMPASSWORD), ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer) - 1);
  177. return TRUE;
  178. }
  179. BOOL CChangePasswordDlg::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  180. {
  181. switch (id)
  182. {
  183. case IDOK:
  184. if (DoPasswordsMatch(hwnd))
  185. {
  186. // Password is the same as confirm password - read password into user info
  187. GetWindowText(GetDlgItem(hwnd, IDC_PASSWORD), m_pUserInfo->m_szPasswordBuffer,
  188. ARRAYSIZE(m_pUserInfo->m_szPasswordBuffer));
  189. m_pUserInfo->HidePassword(); // Hide the password
  190. // Update the password
  191. BOOL fBadPasswordFormat;
  192. if (SUCCEEDED(m_pUserInfo->UpdatePassword(&fBadPasswordFormat)))
  193. {
  194. EndDialog(hwnd, IDOK);
  195. }
  196. else
  197. {
  198. TCHAR szDomainUser[MAX_DOMAIN + MAX_USER + 2];
  199. MakeDomainUserString(m_pUserInfo->m_szDomain, m_pUserInfo->m_szUsername,
  200. szDomainUser, ARRAYSIZE(szDomainUser));
  201. if (fBadPasswordFormat)
  202. {
  203. ::DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION,
  204. IDS_USR_UPDATE_PASSWORD_TOOSHORT_ERROR, MB_ICONERROR | MB_OK,
  205. szDomainUser);
  206. }
  207. else
  208. {
  209. ::DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION,
  210. IDS_USR_UPDATE_PASSWORD_ERROR, MB_ICONERROR | MB_OK,
  211. szDomainUser);
  212. }
  213. }
  214. }
  215. break;
  216. case IDCANCEL:
  217. EndDialog(hwnd, IDCANCEL);
  218. break;
  219. default:
  220. break;
  221. }
  222. return TRUE;
  223. }