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.

221 lines
8.1 KiB

  1. // =====================================================================================
  2. // P A S S D L G . C P P
  3. // Written by Steven J. Bailey 1/26/96
  4. // =====================================================================================
  5. #include "pch.hxx"
  6. #include "passdlg.h"
  7. #include "xpcomm.h"
  8. #include "imnact.h"
  9. #include "imnxport.h"
  10. #include "demand.h"
  11. // =====================================================================================
  12. // Prototypes
  13. // =====================================================================================
  14. INT_PTR CALLBACK PasswordDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  15. void PasswordDlgProc_OnCommand (HWND hwndDlg, int id, HWND hwndCtl, UINT codeNotify);
  16. void PasswordDlgProc_OnCancel (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode);
  17. void PasswordDlgProc_OnOk (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode);
  18. BOOL PasswordDlgProc_OnInitDialog (HWND hwndDlg, HWND hwndFocus, LPARAM lParam);
  19. // =====================================================================================
  20. // HrGetPassword
  21. // =====================================================================================
  22. HRESULT HrGetPassword (HWND hwndParent, LPPASSINFO lpPassInfo)
  23. {
  24. // Locals
  25. HRESULT hr = S_OK;
  26. // Check Params
  27. AssertSz (lpPassInfo, "NULL Parameter");
  28. AssertSz (lpPassInfo->szTitle && lpPassInfo->lpszPassword && lpPassInfo->lpszAccount && lpPassInfo->lpszServer &&
  29. (lpPassInfo->fRememberPassword == TRUE || lpPassInfo->fRememberPassword == FALSE), "PassInfo struct was not inited correctly.");
  30. // Display Dialog Box
  31. INT nResult = (INT) DialogBoxParam (g_hLocRes, MAKEINTRESOURCE (iddPassword), hwndParent, PasswordDlgProc, (LPARAM)lpPassInfo);
  32. if (nResult == IDCANCEL)
  33. hr = S_FALSE;
  34. // Done
  35. return hr;
  36. }
  37. // =====================================================================================
  38. // PasswordDlgProc
  39. // =====================================================================================
  40. INT_PTR CALLBACK PasswordDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  41. {
  42. switch (uMsg)
  43. {
  44. HANDLE_MSG (hwndDlg, WM_INITDIALOG, PasswordDlgProc_OnInitDialog);
  45. HANDLE_MSG (hwndDlg, WM_COMMAND, PasswordDlgProc_OnCommand);
  46. }
  47. return 0;
  48. }
  49. // =====================================================================================
  50. // OnInitDialog
  51. // =====================================================================================
  52. BOOL PasswordDlgProc_OnInitDialog (HWND hwndDlg, HWND hwndFocus, LPARAM lParam)
  53. {
  54. // Locals
  55. LPPASSINFO lpPassInfo = NULL;
  56. TCHAR szServer[CCHMAX_ACCOUNT_NAME];
  57. // Center
  58. CenterDialog (hwndDlg);
  59. // Make foreground
  60. SetForegroundWindow (hwndDlg);
  61. // Get Pass info struct
  62. lpPassInfo = (LPPASSINFO)lParam;
  63. if (lpPassInfo == NULL)
  64. {
  65. Assert (FALSE);
  66. return 0;
  67. }
  68. SetWndThisPtr (hwndDlg, lpPassInfo);
  69. // Set Window Title
  70. SetWindowText (hwndDlg, lpPassInfo->szTitle);
  71. // Default
  72. Edit_LimitText (GetDlgItem (hwndDlg, IDE_ACCOUNT), lpPassInfo->cbMaxAccount);
  73. Edit_LimitText (GetDlgItem (hwndDlg, IDE_PASSWORD), lpPassInfo->cbMaxPassword);
  74. // Set Defaults
  75. PszEscapeMenuStringA(lpPassInfo->lpszServer, szServer, sizeof(szServer) / sizeof(TCHAR));
  76. Edit_SetText (GetDlgItem (hwndDlg, IDS_SERVER), szServer);
  77. Edit_SetText (GetDlgItem (hwndDlg, IDE_ACCOUNT), lpPassInfo->lpszAccount);
  78. Edit_SetText (GetDlgItem (hwndDlg, IDE_PASSWORD), lpPassInfo->lpszPassword);
  79. CheckDlgButton (hwndDlg, IDCH_REMEMBER, lpPassInfo->fRememberPassword);
  80. if (lpPassInfo->fAlwaysPromptPassword)
  81. EnableWindow(GetDlgItem(hwndDlg, IDCH_REMEMBER), FALSE);
  82. // Set Focus
  83. if (!FIsStringEmpty(lpPassInfo->lpszAccount))
  84. SetFocus (GetDlgItem (hwndDlg, IDE_PASSWORD));
  85. // Done
  86. return FALSE;
  87. }
  88. // =====================================================================================
  89. // OnCommand
  90. // =====================================================================================
  91. void PasswordDlgProc_OnCommand (HWND hwndDlg, int id, HWND hwndCtl, UINT codeNotify)
  92. {
  93. switch (id)
  94. {
  95. HANDLE_COMMAND(hwndDlg, IDCANCEL, hwndCtl, codeNotify, PasswordDlgProc_OnCancel);
  96. HANDLE_COMMAND(hwndDlg, IDOK, hwndCtl, codeNotify, PasswordDlgProc_OnOk);
  97. }
  98. return;
  99. }
  100. // =====================================================================================
  101. // OnCancel
  102. // =====================================================================================
  103. void PasswordDlgProc_OnCancel (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode)
  104. {
  105. EndDialog (hwndDlg, IDCANCEL);
  106. }
  107. // =====================================================================================
  108. // OnOk
  109. // =====================================================================================
  110. void PasswordDlgProc_OnOk (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode)
  111. {
  112. // Locals
  113. LPPASSINFO lpPassInfo = NULL;
  114. lpPassInfo = (LPPASSINFO)GetWndThisPtr (hwndDlg);
  115. if (lpPassInfo == NULL)
  116. {
  117. Assert (FALSE);
  118. EndDialog (hwndDlg, IDOK);
  119. return;
  120. }
  121. Edit_GetText (GetDlgItem (hwndDlg, IDE_ACCOUNT), lpPassInfo->lpszAccount, lpPassInfo->cbMaxAccount);
  122. Edit_GetText (GetDlgItem (hwndDlg, IDE_PASSWORD), lpPassInfo->lpszPassword, lpPassInfo->cbMaxPassword);
  123. lpPassInfo->fRememberPassword = IsDlgButtonChecked (hwndDlg, IDCH_REMEMBER);
  124. EndDialog (hwndDlg, IDOK);
  125. }
  126. //***************************************************************************
  127. // Function: PromptUserForPassword
  128. //
  129. // Purpose:
  130. // This function prompts the user with a password dialog and returns the
  131. // results to the caller.
  132. //
  133. // Arguments:
  134. // LPINETSERVER pInetServer [in/out] - provides default values for username
  135. // and password, and allows us to save password to account if user asks us
  136. // to. User-supplied username and password are saved to this structure
  137. // for return to the caller.
  138. // HWND hwnd [in] - parent hwnd to be used for password dialog.
  139. //
  140. // Returns:
  141. // TRUE if user pressed "OK" on dialog, FALSE if user pressed "CANCEL".
  142. //***************************************************************************
  143. BOOL PromptUserForPassword(LPINETSERVER pInetServer, HWND hwnd)
  144. {
  145. PASSINFO pi;
  146. HRESULT hrResult;
  147. BOOL bReturn;
  148. Assert(NULL != hwnd);
  149. // Initialize variables
  150. hrResult = S_OK;
  151. bReturn = FALSE;
  152. // Setup PassInfo Struct
  153. ZeroMemory (&pi, sizeof (PASSINFO));
  154. pi.lpszAccount = pInetServer->szUserName;
  155. pi.cbMaxAccount = sizeof(pInetServer->szUserName);
  156. pi.lpszPassword = pInetServer->szPassword;
  157. pi.cbMaxPassword = sizeof(pInetServer->szPassword);
  158. pi.lpszServer = pInetServer->szAccount;
  159. pi.fRememberPassword = !ISFLAGSET(pInetServer->dwFlags, ISF_ALWAYSPROMPTFORPASSWORD);
  160. pi.fAlwaysPromptPassword = ISFLAGSET(pInetServer->dwFlags, ISF_ALWAYSPROMPTFORPASSWORD);
  161. AthLoadString(idsImapLogon, pi.szTitle, ARRAYSIZE(pi.szTitle));
  162. // Prompt for password
  163. hrResult = HrGetPassword (hwnd, &pi);
  164. if (S_OK == hrResult) {
  165. IImnAccount *pAcct;
  166. // Cache the password for this session
  167. SavePassword(pInetServer->dwPort, pInetServer->szServerName,
  168. pInetServer->szUserName, pInetServer->szPassword);
  169. // User wishes to proceed. Save account and password info
  170. hrResult = g_pAcctMan->FindAccount(AP_ACCOUNT_NAME, pInetServer->szAccount, &pAcct);
  171. if (SUCCEEDED(hrResult)) {
  172. // I'll ignore error results here, since not much we can do about 'em
  173. pAcct->SetPropSz(AP_IMAP_USERNAME, pInetServer->szUserName);
  174. if (pi.fRememberPassword)
  175. pAcct->SetPropSz(AP_IMAP_PASSWORD, pInetServer->szPassword);
  176. else
  177. pAcct->SetProp(AP_IMAP_PASSWORD, NULL, 0);
  178. pAcct->SaveChanges();
  179. pAcct->Release();
  180. }
  181. bReturn = TRUE;
  182. }
  183. Assert(SUCCEEDED(hrResult));
  184. return bReturn;
  185. } // PromptUserForPassword