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.

286 lines
11 KiB

  1. #define COBJMACROS
  2. #include <_apipch.h>
  3. #include <wab.h>
  4. #define COBJMACROS
  5. #include "resource.h"
  6. #include "objbase.h"
  7. #include "ui_pwd.h"
  8. #include "commctrl.h"
  9. #include "winuser.h"
  10. #include "windowsx.h"
  11. #include "imnxport.h"
  12. // =====================================================================================
  13. // Prototypes
  14. // =====================================================================================
  15. INT_PTR CALLBACK PasswordDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  16. void PasswordDlgProc_OnCommand (HWND hwndDlg, int id, HWND hwndCtl, UINT codeNotify);
  17. void PasswordDlgProc_OnCancel (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode);
  18. void PasswordDlgProc_OnOk (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode);
  19. BOOL PasswordDlgProc_OnInitDialog (HWND hwndDlg, HWND hwndFocus, LPARAM lParam);
  20. extern VOID CenterDialog(HWND hwndDlg);
  21. #define FIsStringEmpty(s) (*s == 0)
  22. #define ISFLAGSET(_dw, _f) (BOOL)(((_dw) & (_f)) == (_f))
  23. // --------------------------------------------------------------------------------
  24. // HANDLE_COMMAND - Used in a WindowProc to simplify handling of WM_COMMAND messages
  25. // --------------------------------------------------------------------------------
  26. #define HANDLE_COMMAND(hwnd, id, hwndCtl, codeNotify, fn) \
  27. case (id): { (fn)((HWND)(hwnd), (HWND)(hwndCtl), (UINT)(codeNotify)); break; }
  28. // =====================================================================================
  29. // HrGetPassword
  30. // =====================================================================================
  31. HRESULT HrGetPassword (HWND hwndParent, LPPASSINFO lpPassInfo)
  32. {
  33. // Locals
  34. HRESULT hr = S_OK;
  35. INT nResult;
  36. // Check Params
  37. AssertSz (lpPassInfo, TEXT("NULL Parameter"));
  38. AssertSz (lpPassInfo->lpszPassword && lpPassInfo->lpszAccount && lpPassInfo->lpszServer &&
  39. (lpPassInfo->fRememberPassword == TRUE || lpPassInfo->fRememberPassword == FALSE), TEXT("PassInfo struct was not inited correctly."));
  40. // Display Dialog Box
  41. nResult = (INT) DialogBoxParam (hinstMapiX, MAKEINTRESOURCE (iddPassword), hwndParent, PasswordDlgProc, (LPARAM)lpPassInfo);
  42. if (nResult == IDCANCEL)
  43. hr = S_FALSE;
  44. // Done
  45. return hr;
  46. }
  47. // =====================================================================================
  48. // PasswordDlgProc
  49. // =====================================================================================
  50. INT_PTR CALLBACK PasswordDlgProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  51. {
  52. switch (uMsg)
  53. {
  54. HANDLE_MSG (hwndDlg, WM_INITDIALOG, PasswordDlgProc_OnInitDialog);
  55. HANDLE_MSG (hwndDlg, WM_COMMAND, PasswordDlgProc_OnCommand);
  56. }
  57. return 0;
  58. }
  59. // =====================================================================================
  60. // OnInitDialog
  61. // =====================================================================================
  62. BOOL PasswordDlgProc_OnInitDialog (HWND hwndDlg, HWND hwndFocus, LPARAM lParam)
  63. {
  64. // Locals
  65. LPPASSINFO lpPassInfo = NULL;
  66. TCHAR szServer[CCHMAX_ACCOUNT_NAME];
  67. // Center
  68. CenterDialog (hwndDlg);
  69. // Make foreground
  70. SetForegroundWindow (hwndDlg);
  71. // Get Pass info struct
  72. lpPassInfo = (LPPASSINFO)lParam;
  73. if (lpPassInfo == NULL)
  74. {
  75. Assert (FALSE);
  76. return 0;
  77. }
  78. SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)lpPassInfo);
  79. // Default
  80. Edit_LimitText (GetDlgItem (hwndDlg, IDE_ACCOUNT), lpPassInfo->cbMaxAccount);
  81. Edit_LimitText (GetDlgItem (hwndDlg, IDE_PASSWORD), lpPassInfo->cbMaxPassword);
  82. // Set Defaults
  83. Edit_SetText (GetDlgItem (hwndDlg, IDS_SERVER), lpPassInfo->lpszServer);
  84. Edit_SetText (GetDlgItem (hwndDlg, IDE_ACCOUNT), lpPassInfo->lpszAccount);
  85. Edit_SetText (GetDlgItem (hwndDlg, IDE_PASSWORD), lpPassInfo->lpszPassword);
  86. CheckDlgButton (hwndDlg, IDCH_REMEMBER, lpPassInfo->fRememberPassword);
  87. if (lpPassInfo->fAlwaysPromptPassword)
  88. EnableWindow(GetDlgItem(hwndDlg, IDCH_REMEMBER), FALSE);
  89. // Set Focus
  90. if (!FIsStringEmpty(lpPassInfo->lpszAccount))
  91. SetFocus (GetDlgItem (hwndDlg, IDE_PASSWORD));
  92. // Done
  93. return FALSE;
  94. }
  95. // =====================================================================================
  96. // OnCommand
  97. // =====================================================================================
  98. void PasswordDlgProc_OnCommand (HWND hwndDlg, int id, HWND hwndCtl, UINT codeNotify)
  99. {
  100. switch (id)
  101. {
  102. HANDLE_COMMAND(hwndDlg, IDCANCEL, hwndCtl, codeNotify, PasswordDlgProc_OnCancel);
  103. HANDLE_COMMAND(hwndDlg, IDOK, hwndCtl, codeNotify, PasswordDlgProc_OnOk);
  104. }
  105. return;
  106. }
  107. // =====================================================================================
  108. // OnCancel
  109. // =====================================================================================
  110. void PasswordDlgProc_OnCancel (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode)
  111. {
  112. EndDialog (hwndDlg, IDCANCEL);
  113. }
  114. // =====================================================================================
  115. // OnOk
  116. // =====================================================================================
  117. void PasswordDlgProc_OnOk (HWND hwndDlg, HWND hwndCtl, UINT uNotifyCode)
  118. {
  119. // Locals
  120. LPPASSINFO lpPassInfo = NULL;
  121. lpPassInfo = (LPPASSINFO)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  122. if (lpPassInfo == NULL)
  123. {
  124. Assert (FALSE);
  125. EndDialog (hwndDlg, IDOK);
  126. return;
  127. }
  128. Edit_GetText (GetDlgItem (hwndDlg, IDE_ACCOUNT), lpPassInfo->lpszAccount, lpPassInfo->cbMaxAccount);
  129. Edit_GetText (GetDlgItem (hwndDlg, IDE_PASSWORD), lpPassInfo->lpszPassword, lpPassInfo->cbMaxPassword);
  130. lpPassInfo->fRememberPassword = IsDlgButtonChecked (hwndDlg, IDCH_REMEMBER);
  131. EndDialog (hwndDlg, IDOK);
  132. }
  133. //***************************************************************************
  134. // Function: PromptUserForPassword
  135. //
  136. // Purpose:
  137. // This function prompts the user with a password dialog and returns the
  138. // results to the caller.
  139. //
  140. // Arguments:
  141. // LPINETSERVER pInetServer [in/out] - provides default values for username
  142. // and password, and allows us to save password to account if user asks us
  143. // to. User-supplied username and password are saved to this structure
  144. // for return to the caller.
  145. // HWND hwnd [in] - parent hwnd to be used for password dialog.
  146. //
  147. // Returns:
  148. // TRUE if user pressed TEXT("OK") on dialog, FALSE if user pressed TEXT("CANCEL").
  149. //***************************************************************************
  150. BOOL PromptUserForPassword(LPINETSERVER pInetServer, HWND hwnd)
  151. {
  152. PASSINFO pi = {0};
  153. HRESULT hrResult;
  154. BOOL bReturn;
  155. Assert(NULL != hwnd);
  156. // Initialize variables
  157. hrResult = S_OK;
  158. bReturn = FALSE;
  159. // Setup PassInfo Struct
  160. ZeroMemory (&pi, sizeof (PASSINFO));
  161. pi.cbMaxAccount = sizeof(pInetServer->szUserName);
  162. pi.cbMaxPassword = sizeof(pInetServer->szPassword);
  163. pi.lpszServer = ConvertAtoW(pInetServer->szAccount); // We don't modify this in the dialog
  164. {
  165. LPWSTR lpwszAccount;
  166. LPWSTR lpwszPassword;
  167. pi.lpszAccount = LocalAlloc(LMEM_ZEROINIT, pi.cbMaxAccount);
  168. pi.lpszPassword = LocalAlloc(LMEM_ZEROINIT, pi.cbMaxPassword);
  169. // Convert to Unicode strings
  170. lpwszAccount = ConvertAtoW(pInetServer->szUserName);
  171. lpwszPassword = ConvertAtoW(pInetServer->szPassword);
  172. if (lpwszAccount && pi.lpszAccount)
  173. StrCpyN((pi.lpszAccount), lpwszAccount, (pi.cbMaxAccount / sizeof(WCHAR)));
  174. if (lpwszPassword && pi.lpszPassword)
  175. {
  176. StrCpyN((pi.lpszPassword), lpwszPassword, (pi.cbMaxPassword / sizeof(WCHAR)));
  177. ZeroMemory(lpwszPassword, (lstrlenW(lpwszPassword) * sizeof(lpwszPassword[0])));
  178. }
  179. LocalFreeAndNull(&lpwszAccount);
  180. LocalFreeAndNull(&lpwszPassword);
  181. }
  182. pi.fRememberPassword = !ISFLAGSET(pInetServer->dwFlags, ISF_ALWAYSPROMPTFORPASSWORD);
  183. pi.fAlwaysPromptPassword = ISFLAGSET(pInetServer->dwFlags, ISF_ALWAYSPROMPTFORPASSWORD);
  184. // Prompt for password
  185. hrResult = HrGetPassword (hwnd, &pi);
  186. if (S_OK == hrResult)
  187. {
  188. IImnAccount *pAcct;
  189. IImnAccountManager2 *pAcctMgr = NULL;
  190. // Update the INET server structure. Must convert back to ANSI
  191. {
  192. LPSTR lpszAccount = ConvertWtoA(pi.lpszAccount);
  193. LPSTR lpszPassword = ConvertWtoA(pi.lpszPassword);
  194. // If the conversion from Wide to ANSI overflows the pInetServer string
  195. // buffers then we must fail.
  196. if (lpszAccount)
  197. {
  198. if (lstrlenA(lpszAccount) < (int)(pi.cbMaxAccount))
  199. StrCpyNA(pInetServer->szUserName, lpszAccount, ARRAYSIZE(pInetServer->szUserName));
  200. else
  201. hrResult = TYPE_E_BUFFERTOOSMALL;
  202. }
  203. if (lpszPassword)
  204. {
  205. if (lstrlenA(lpszPassword) < (int)(pi.cbMaxPassword))
  206. StrCpyNA(pInetServer->szPassword, lpszPassword, ARRAYSIZE(pInetServer->szPassword));
  207. else
  208. hrResult = TYPE_E_BUFFERTOOSMALL;
  209. ZeroMemory(lpszPassword, (lstrlenA(lpszPassword) * sizeof(lpszPassword[0])));
  210. }
  211. LocalFreeAndNull(&lpszAccount);
  212. LocalFreeAndNull(&lpszPassword);
  213. }
  214. if (SUCCEEDED(hrResult = InitAccountManager(NULL, &pAcctMgr, NULL)))
  215. {
  216. // User wishes to proceed. Save account and password info
  217. hrResult = pAcctMgr->lpVtbl->FindAccount(pAcctMgr, AP_ACCOUNT_NAME, pInetServer->szAccount, &pAcct);
  218. if (SUCCEEDED(hrResult))
  219. {
  220. // I'll ignore error results here, since not much we can do about 'em
  221. pAcct->lpVtbl->SetPropSz(pAcct, AP_HTTPMAIL_USERNAME, pInetServer->szUserName);
  222. if (pi.fRememberPassword)
  223. pAcct->lpVtbl->SetPropSz(pAcct, AP_HTTPMAIL_PASSWORD, pInetServer->szPassword);
  224. else
  225. pAcct->lpVtbl->SetProp(pAcct, AP_HTTPMAIL_PASSWORD, NULL, 0);
  226. pAcct->lpVtbl->SaveChanges(pAcct);
  227. pAcct->lpVtbl->Release(pAcct);
  228. }
  229. // don't release the lpAcctMgr since the WAB maintains a global reference.
  230. }
  231. bReturn = TRUE;
  232. }
  233. Assert(SUCCEEDED(hrResult));
  234. if (pi.lpszPassword && pi.cbMaxPassword)
  235. ZeroMemory(pi.lpszPassword, pi.cbMaxPassword);
  236. LocalFreeAndNull(&(pi.lpszPassword));
  237. LocalFreeAndNull(&(pi.lpszAccount));
  238. LocalFreeAndNull(&(pi.lpszServer));
  239. ZeroMemory(&pi, sizeof(pi));
  240. return bReturn;
  241. } // PromptUserForPassword