Revert "winex11: Refuse to set the pixel format for HWND_MESSAGE windows."
[wine.git] / dlls / credui / credui_main.c
blob6e8d45229bab86f505966b0389899b08c15a4cc8
1 /*
2 * Credentials User Interface
4 * Copyright 2006 Robert Shearman (for CodeWeavers)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnt.h"
26 #include "winuser.h"
27 #include "wincred.h"
28 #include "commctrl.h"
30 #include "credui_resources.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "wine/list.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(credui);
38 #define TOOLID_INCORRECTPASSWORD 1
39 #define TOOLID_CAPSLOCKON 2
41 #define ID_CAPSLOCKPOP 1
43 struct pending_credentials
45 struct list entry;
46 PWSTR pszTargetName;
47 PWSTR pszUsername;
48 PWSTR pszPassword;
49 BOOL generic;
52 static HINSTANCE hinstCredUI;
54 static struct list pending_credentials_list = LIST_INIT(pending_credentials_list);
56 static CRITICAL_SECTION csPendingCredentials;
57 static CRITICAL_SECTION_DEBUG critsect_debug =
59 0, 0, &csPendingCredentials,
60 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
61 0, 0, { (DWORD_PTR)(__FILE__ ": csPendingCredentials") }
63 static CRITICAL_SECTION csPendingCredentials = { &critsect_debug, -1, 0, 0, 0, 0 };
66 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
68 struct pending_credentials *entry, *cursor2;
69 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
71 switch (fdwReason)
73 case DLL_WINE_PREATTACH:
74 return FALSE; /* prefer native version */
76 case DLL_PROCESS_ATTACH:
77 DisableThreadLibraryCalls(hinstDLL);
78 hinstCredUI = hinstDLL;
79 InitCommonControls();
80 break;
82 case DLL_PROCESS_DETACH:
83 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
85 list_remove(&entry->entry);
87 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
88 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
89 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
90 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
91 HeapFree(GetProcessHeap(), 0, entry);
93 DeleteCriticalSection(&csPendingCredentials);
94 break;
97 return TRUE;
100 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
101 PCWSTR pszPassword, BOOL generic)
103 CREDENTIALW cred;
105 TRACE("saving servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
107 cred.Flags = 0;
108 cred.Type = generic ? CRED_TYPE_GENERIC : CRED_TYPE_DOMAIN_PASSWORD;
109 cred.TargetName = (LPWSTR)pszTargetName;
110 cred.Comment = NULL;
111 cred.CredentialBlobSize = strlenW(pszPassword) * sizeof(WCHAR);
112 cred.CredentialBlob = (LPBYTE)pszPassword;
113 cred.Persist = CRED_PERSIST_ENTERPRISE;
114 cred.AttributeCount = 0;
115 cred.Attributes = NULL;
116 cred.TargetAlias = NULL;
117 cred.UserName = (LPWSTR)pszUsername;
119 if (CredWriteW(&cred, 0))
120 return ERROR_SUCCESS;
121 else
123 DWORD ret = GetLastError();
124 ERR("CredWriteW failed with error %d\n", ret);
125 return ret;
129 struct cred_dialog_params
131 PCWSTR pszTargetName;
132 PCWSTR pszMessageText;
133 PCWSTR pszCaptionText;
134 HBITMAP hbmBanner;
135 PWSTR pszUsername;
136 ULONG ulUsernameMaxChars;
137 PWSTR pszPassword;
138 ULONG ulPasswordMaxChars;
139 BOOL fSave;
140 DWORD dwFlags;
141 HWND hwndBalloonTip;
142 BOOL fBalloonTipActive;
145 static void CredDialogFillUsernameCombo(HWND hwndUsername, const struct cred_dialog_params *params)
147 DWORD count;
148 DWORD i;
149 PCREDENTIALW *credentials;
151 if (!CredEnumerateW(NULL, 0, &count, &credentials))
152 return;
154 for (i = 0; i < count; i++)
156 COMBOBOXEXITEMW comboitem;
157 DWORD j;
158 BOOL duplicate = FALSE;
160 if (params->dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS)
162 if ((credentials[i]->Type != CRED_TYPE_GENERIC) || !credentials[i]->UserName)
163 continue;
165 else
167 if (credentials[i]->Type == CRED_TYPE_GENERIC)
168 continue;
171 /* don't add another item with the same name if we've already added it */
172 for (j = 0; j < i; j++)
173 if (!strcmpW(credentials[i]->UserName, credentials[j]->UserName))
175 duplicate = TRUE;
176 break;
179 if (duplicate)
180 continue;
182 comboitem.mask = CBEIF_TEXT;
183 comboitem.iItem = -1;
184 comboitem.pszText = credentials[i]->UserName;
185 SendMessageW(hwndUsername, CBEM_INSERTITEMW, 0, (LPARAM)&comboitem);
188 CredFree(credentials);
191 static void CredDialogCreateBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
193 TTTOOLINFOW toolinfo;
194 WCHAR wszText[256];
196 if (params->hwndBalloonTip)
197 return;
199 params->hwndBalloonTip = CreateWindowExW(WS_EX_TOOLWINDOW, TOOLTIPS_CLASSW,
200 NULL, WS_POPUP | TTS_NOPREFIX | TTS_BALLOON, CW_USEDEFAULT,
201 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwndDlg, NULL,
202 hinstCredUI, NULL);
203 SetWindowPos(params->hwndBalloonTip, HWND_TOPMOST, 0, 0, 0, 0,
204 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
206 if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORD, wszText, sizeof(wszText)/sizeof(wszText[0])))
208 ERR("failed to load IDS_INCORRECTPASSWORD\n");
209 return;
212 toolinfo.cbSize = sizeof(toolinfo);
213 toolinfo.uFlags = TTF_TRACK;
214 toolinfo.hwnd = hwndDlg;
215 toolinfo.uId = TOOLID_INCORRECTPASSWORD;
216 memset(&toolinfo.rect, 0, sizeof(toolinfo.rect));
217 toolinfo.hinst = NULL;
218 toolinfo.lpszText = wszText;
219 toolinfo.lParam = 0;
220 toolinfo.lpReserved = NULL;
221 SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
223 if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKON, wszText, sizeof(wszText)/sizeof(wszText[0])))
225 ERR("failed to load IDS_CAPSLOCKON\n");
226 return;
229 toolinfo.uId = TOOLID_CAPSLOCKON;
230 SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
233 static void CredDialogShowIncorrectPasswordBalloon(HWND hwndDlg, struct cred_dialog_params *params)
235 TTTOOLINFOW toolinfo;
236 RECT rcPassword;
237 INT x;
238 INT y;
239 WCHAR wszTitle[256];
241 /* user name likely wrong so balloon would be confusing. focus is also
242 * not set to the password edit box, so more notification would need to be
243 * handled */
244 if (!params->pszUsername[0])
245 return;
247 /* don't show two balloon tips at once */
248 if (params->fBalloonTipActive)
249 return;
251 if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORDTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
253 ERR("failed to load IDS_INCORRECTPASSWORDTITLE\n");
254 return;
257 CredDialogCreateBalloonTip(hwndDlg, params);
259 memset(&toolinfo, 0, sizeof(toolinfo));
260 toolinfo.cbSize = sizeof(toolinfo);
261 toolinfo.hwnd = hwndDlg;
262 toolinfo.uId = TOOLID_INCORRECTPASSWORD;
264 SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_ERROR, (LPARAM)wszTitle);
266 GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
267 /* centered vertically and in the right side of the password edit control */
268 x = rcPassword.right - 12;
269 y = (rcPassword.top + rcPassword.bottom) / 2;
270 SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
272 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
274 params->fBalloonTipActive = TRUE;
277 static void CredDialogShowCapsLockBalloon(HWND hwndDlg, struct cred_dialog_params *params)
279 TTTOOLINFOW toolinfo;
280 RECT rcPassword;
281 INT x;
282 INT y;
283 WCHAR wszTitle[256];
285 /* don't show two balloon tips at once */
286 if (params->fBalloonTipActive)
287 return;
289 if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKONTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
291 ERR("failed to load IDS_IDSCAPSLOCKONTITLE\n");
292 return;
295 CredDialogCreateBalloonTip(hwndDlg, params);
297 memset(&toolinfo, 0, sizeof(toolinfo));
298 toolinfo.cbSize = sizeof(toolinfo);
299 toolinfo.hwnd = hwndDlg;
300 toolinfo.uId = TOOLID_CAPSLOCKON;
302 SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_WARNING, (LPARAM)wszTitle);
304 GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
305 /* just inside the left side of the password edit control */
306 x = rcPassword.left + 12;
307 y = rcPassword.bottom - 3;
308 SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
310 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
312 SetTimer(hwndDlg, ID_CAPSLOCKPOP,
313 SendMessageW(params->hwndBalloonTip, TTM_GETDELAYTIME, TTDT_AUTOPOP, 0),
314 NULL);
316 params->fBalloonTipActive = TRUE;
319 static void CredDialogHideBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
321 TTTOOLINFOW toolinfo;
323 if (!params->hwndBalloonTip)
324 return;
326 memset(&toolinfo, 0, sizeof(toolinfo));
328 toolinfo.cbSize = sizeof(toolinfo);
329 toolinfo.hwnd = hwndDlg;
330 toolinfo.uId = 0;
331 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
332 toolinfo.uId = 1;
333 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
335 params->fBalloonTipActive = FALSE;
338 static inline BOOL CredDialogCapsLockOn(void)
340 return (GetKeyState(VK_CAPITAL) & 0x1) != 0;
343 static LRESULT CALLBACK CredDialogPasswordSubclassProc(HWND hwnd, UINT uMsg,
344 WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
346 struct cred_dialog_params *params = (struct cred_dialog_params *)dwRefData;
347 switch (uMsg)
349 case WM_KEYDOWN:
350 if (wParam == VK_CAPITAL)
352 HWND hwndDlg = GetParent(hwnd);
353 if (CredDialogCapsLockOn())
354 CredDialogShowCapsLockBalloon(hwndDlg, params);
355 else
356 CredDialogHideBalloonTip(hwndDlg, params);
358 break;
359 case WM_DESTROY:
360 RemoveWindowSubclass(hwnd, CredDialogPasswordSubclassProc, uIdSubclass);
361 break;
363 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
366 static BOOL CredDialogInit(HWND hwndDlg, struct cred_dialog_params *params)
368 HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
369 HWND hwndPassword = GetDlgItem(hwndDlg, IDC_PASSWORD);
371 SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
373 if (params->hbmBanner)
374 SendMessageW(GetDlgItem(hwndDlg, IDB_BANNER), STM_SETIMAGE,
375 IMAGE_BITMAP, (LPARAM)params->hbmBanner);
377 if (params->pszMessageText)
378 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, params->pszMessageText);
379 else
381 WCHAR format[256];
382 WCHAR message[256];
383 LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, sizeof(format)/sizeof(format[0]));
384 snprintfW(message, sizeof(message)/sizeof(message[0]), format, params->pszTargetName);
385 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, message);
387 SetWindowTextW(hwndUsername, params->pszUsername);
388 SetWindowTextW(hwndPassword, params->pszPassword);
390 CredDialogFillUsernameCombo(hwndUsername, params);
392 if (params->pszUsername[0])
394 /* prevent showing a balloon tip here */
395 params->fBalloonTipActive = TRUE;
396 SetFocus(hwndPassword);
397 params->fBalloonTipActive = FALSE;
399 else
400 SetFocus(hwndUsername);
402 if (params->pszCaptionText)
403 SetWindowTextW(hwndDlg, params->pszCaptionText);
404 else
406 WCHAR format[256];
407 WCHAR title[256];
408 LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, sizeof(format)/sizeof(format[0]));
409 snprintfW(title, sizeof(title)/sizeof(title[0]), format, params->pszTargetName);
410 SetWindowTextW(hwndDlg, title);
413 if (params->dwFlags & CREDUI_FLAGS_PERSIST ||
414 (params->dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST &&
415 !(params->dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX)))
416 ShowWindow(GetDlgItem(hwndDlg, IDC_SAVE), SW_HIDE);
417 else if (params->fSave)
418 CheckDlgButton(hwndDlg, IDC_SAVE, BST_CHECKED);
420 /* setup subclassing for Caps Lock detection */
421 SetWindowSubclass(hwndPassword, CredDialogPasswordSubclassProc, 1, (DWORD_PTR)params);
423 if (params->dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD)
424 CredDialogShowIncorrectPasswordBalloon(hwndDlg, params);
425 else if ((GetFocus() == hwndPassword) && CredDialogCapsLockOn())
426 CredDialogShowCapsLockBalloon(hwndDlg, params);
428 return FALSE;
431 static void CredDialogCommandOk(HWND hwndDlg, struct cred_dialog_params *params)
433 HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
434 LPWSTR user;
435 INT len;
436 INT len2;
438 len = GetWindowTextLengthW(hwndUsername);
439 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
440 GetWindowTextW(hwndUsername, user, len + 1);
442 if (!user[0])
444 HeapFree(GetProcessHeap(), 0, user);
445 return;
448 if (!strchrW(user, '\\') && !strchrW(user, '@'))
450 ULONG len_target = strlenW(params->pszTargetName);
451 memcpy(params->pszUsername, params->pszTargetName,
452 min(len_target, params->ulUsernameMaxChars) * sizeof(WCHAR));
453 if (len_target + 1 < params->ulUsernameMaxChars)
454 params->pszUsername[len_target] = '\\';
455 if (len_target + 2 < params->ulUsernameMaxChars)
456 params->pszUsername[len_target + 1] = '\0';
458 else if (params->ulUsernameMaxChars > 0)
459 params->pszUsername[0] = '\0';
461 len2 = strlenW(params->pszUsername);
462 memcpy(params->pszUsername + len2, user, min(len, params->ulUsernameMaxChars - len2) * sizeof(WCHAR));
463 if (params->ulUsernameMaxChars)
464 params->pszUsername[len2 + min(len, params->ulUsernameMaxChars - len2 - 1)] = '\0';
466 HeapFree(GetProcessHeap(), 0, user);
468 GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
469 params->ulPasswordMaxChars);
471 params->fSave = IsDlgButtonChecked(hwndDlg, IDC_SAVE) == BST_CHECKED;
473 EndDialog(hwndDlg, IDOK);
476 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
477 LPARAM lParam)
479 switch (uMsg)
481 case WM_INITDIALOG:
483 struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
485 return CredDialogInit(hwndDlg, params);
487 case WM_COMMAND:
488 switch (wParam)
490 case MAKELONG(IDOK, BN_CLICKED):
492 struct cred_dialog_params *params =
493 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
494 CredDialogCommandOk(hwndDlg, params);
495 return TRUE;
497 case MAKELONG(IDCANCEL, BN_CLICKED):
498 EndDialog(hwndDlg, IDCANCEL);
499 return TRUE;
500 case MAKELONG(IDC_PASSWORD, EN_SETFOCUS):
501 if (CredDialogCapsLockOn())
503 struct cred_dialog_params *params =
504 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
505 CredDialogShowCapsLockBalloon(hwndDlg, params);
507 /* don't allow another window to steal focus while the
508 * user is typing their password */
509 LockSetForegroundWindow(LSFW_LOCK);
510 return TRUE;
511 case MAKELONG(IDC_PASSWORD, EN_KILLFOCUS):
513 struct cred_dialog_params *params =
514 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
515 /* the user is no longer typing their password, so allow
516 * other windows to become foreground ones */
517 LockSetForegroundWindow(LSFW_UNLOCK);
518 CredDialogHideBalloonTip(hwndDlg, params);
519 return TRUE;
521 case MAKELONG(IDC_PASSWORD, EN_CHANGE):
523 struct cred_dialog_params *params =
524 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
525 CredDialogHideBalloonTip(hwndDlg, params);
526 return TRUE;
529 return FALSE;
530 case WM_TIMER:
531 if (wParam == ID_CAPSLOCKPOP)
533 struct cred_dialog_params *params =
534 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
535 CredDialogHideBalloonTip(hwndDlg, params);
536 return TRUE;
538 return FALSE;
539 case WM_DESTROY:
541 struct cred_dialog_params *params =
542 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
543 if (params->hwndBalloonTip) DestroyWindow(params->hwndBalloonTip);
544 return TRUE;
546 default:
547 return FALSE;
551 static BOOL find_existing_credential(const WCHAR *target, WCHAR *username, ULONG len_username,
552 WCHAR *password, ULONG len_password)
554 DWORD count, i;
555 CREDENTIALW **credentials;
557 if (!CredEnumerateW(target, 0, &count, &credentials)) return FALSE;
558 for (i = 0; i < count; i++)
560 if (credentials[i]->Type != CRED_TYPE_DOMAIN_PASSWORD)
562 FIXME("no support for type %u credentials\n", credentials[i]->Type);
563 continue;
565 if ((!*username || !strcmpW(username, credentials[i]->UserName)) &&
566 strlenW(credentials[i]->UserName) < len_username &&
567 credentials[i]->CredentialBlobSize / sizeof(WCHAR) < len_password)
569 TRACE("found existing credential for %s\n", debugstr_w(credentials[i]->UserName));
571 strcpyW(username, credentials[i]->UserName);
572 memcpy(password, credentials[i]->CredentialBlob, credentials[i]->CredentialBlobSize);
573 password[credentials[i]->CredentialBlobSize / sizeof(WCHAR)] = 0;
575 CredFree(credentials);
576 return TRUE;
579 CredFree(credentials);
580 return FALSE;
583 /******************************************************************************
584 * CredUIPromptForCredentialsW [CREDUI.@]
586 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
587 PCWSTR pszTargetName,
588 PCtxtHandle Reserved,
589 DWORD dwAuthError,
590 PWSTR pszUsername,
591 ULONG ulUsernameMaxChars,
592 PWSTR pszPassword,
593 ULONG ulPasswordMaxChars, PBOOL pfSave,
594 DWORD dwFlags)
596 INT_PTR ret;
597 struct cred_dialog_params params;
598 DWORD result = ERROR_SUCCESS;
600 TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
601 debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
602 ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
604 if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
605 return ERROR_INVALID_FLAGS;
607 if (!pszTargetName)
608 return ERROR_INVALID_PARAMETER;
610 if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
611 return ERROR_INVALID_PARAMETER;
613 if (!(dwFlags & CREDUI_FLAGS_ALWAYS_SHOW_UI) &&
614 !(dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD) &&
615 find_existing_credential(pszTargetName, pszUsername, ulUsernameMaxChars, pszPassword, ulPasswordMaxChars))
616 return ERROR_SUCCESS;
618 params.pszTargetName = pszTargetName;
619 if (pUIInfo)
621 params.pszMessageText = pUIInfo->pszMessageText;
622 params.pszCaptionText = pUIInfo->pszCaptionText;
623 params.hbmBanner = pUIInfo->hbmBanner;
625 else
627 params.pszMessageText = NULL;
628 params.pszCaptionText = NULL;
629 params.hbmBanner = NULL;
631 params.pszUsername = pszUsername;
632 params.ulUsernameMaxChars = ulUsernameMaxChars;
633 params.pszPassword = pszPassword;
634 params.ulPasswordMaxChars = ulPasswordMaxChars;
635 params.fSave = pfSave ? *pfSave : FALSE;
636 params.dwFlags = dwFlags;
637 params.hwndBalloonTip = NULL;
638 params.fBalloonTipActive = FALSE;
640 ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
641 pUIInfo ? pUIInfo->hwndParent : NULL,
642 CredDialogProc, (LPARAM)&params);
643 if (ret <= 0)
644 return GetLastError();
646 if (ret == IDCANCEL)
648 TRACE("dialog cancelled\n");
649 return ERROR_CANCELLED;
652 if (pfSave)
653 *pfSave = params.fSave;
655 if (params.fSave)
657 if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
659 BOOL found = FALSE;
660 struct pending_credentials *entry;
661 int len;
663 EnterCriticalSection(&csPendingCredentials);
665 /* find existing pending credentials for the same target and overwrite */
666 /* FIXME: is this correct? */
667 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
668 if (!strcmpW(pszTargetName, entry->pszTargetName))
670 found = TRUE;
671 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
672 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
673 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
676 if (!found)
678 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
679 len = strlenW(pszTargetName);
680 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
681 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
682 list_add_tail(&pending_credentials_list, &entry->entry);
685 len = strlenW(params.pszUsername);
686 entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
687 memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
688 len = strlenW(params.pszPassword);
689 entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
690 memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
691 entry->generic = (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0;
693 LeaveCriticalSection(&csPendingCredentials);
695 else if (!(dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST))
696 result = save_credentials(pszTargetName, pszUsername, pszPassword,
697 (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0);
700 return result;
703 /******************************************************************************
704 * CredUIConfirmCredentialsW [CREDUI.@]
706 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
708 struct pending_credentials *entry;
709 DWORD result = ERROR_NOT_FOUND;
711 TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
713 if (!pszTargetName)
714 return ERROR_INVALID_PARAMETER;
716 EnterCriticalSection(&csPendingCredentials);
718 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
720 if (!strcmpW(pszTargetName, entry->pszTargetName))
722 if (bConfirm)
723 result = save_credentials(entry->pszTargetName, entry->pszUsername,
724 entry->pszPassword, entry->generic);
725 else
726 result = ERROR_SUCCESS;
728 list_remove(&entry->entry);
730 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
731 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
732 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
733 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
734 HeapFree(GetProcessHeap(), 0, entry);
736 break;
740 LeaveCriticalSection(&csPendingCredentials);
742 return result;
745 /******************************************************************************
746 * CredUIParseUserNameW [CREDUI.@]
748 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
749 ULONG ulMaxUserChars, PWSTR pszDomain,
750 ULONG ulMaxDomainChars)
752 PWSTR p;
754 TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
755 ulMaxUserChars, pszDomain, ulMaxDomainChars);
757 if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
758 !ulMaxDomainChars)
759 return ERROR_INVALID_PARAMETER;
761 /* FIXME: handle marshaled credentials */
763 p = strchrW(pszUserName, '\\');
764 if (p)
766 if (p - pszUserName > ulMaxDomainChars - 1)
767 return ERROR_INSUFFICIENT_BUFFER;
768 if (strlenW(p + 1) > ulMaxUserChars - 1)
769 return ERROR_INSUFFICIENT_BUFFER;
770 strcpyW(pszUser, p + 1);
771 memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
772 pszDomain[p - pszUserName] = '\0';
774 return ERROR_SUCCESS;
777 p = strrchrW(pszUserName, '@');
778 if (p)
780 if (p + 1 - pszUserName > ulMaxUserChars - 1)
781 return ERROR_INSUFFICIENT_BUFFER;
782 if (strlenW(p + 1) > ulMaxDomainChars - 1)
783 return ERROR_INSUFFICIENT_BUFFER;
784 strcpyW(pszDomain, p + 1);
785 memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
786 pszUser[p - pszUserName] = '\0';
788 return ERROR_SUCCESS;
791 if (strlenW(pszUserName) > ulMaxUserChars - 1)
792 return ERROR_INSUFFICIENT_BUFFER;
793 strcpyW(pszUser, pszUserName);
794 pszDomain[0] = '\0';
796 return ERROR_SUCCESS;
799 /******************************************************************************
800 * CredUIStoreSSOCredA [CREDUI.@]
802 DWORD WINAPI CredUIStoreSSOCredA(PCSTR pszRealm, PCSTR pszUsername,
803 PCSTR pszPassword, BOOL bPersist)
805 FIXME("(%s, %s, %p, %d)\n", debugstr_a(pszRealm), debugstr_a(pszUsername),
806 pszPassword, bPersist);
807 return ERROR_SUCCESS;
810 /******************************************************************************
811 * CredUIStoreSSOCredW [CREDUI.@]
813 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR pszRealm, PCWSTR pszUsername,
814 PCWSTR pszPassword, BOOL bPersist)
816 FIXME("(%s, %s, %p, %d)\n", debugstr_w(pszRealm), debugstr_w(pszUsername),
817 pszPassword, bPersist);
818 return ERROR_SUCCESS;
821 /******************************************************************************
822 * CredUIReadSSOCredA [CREDUI.@]
824 DWORD WINAPI CredUIReadSSOCredA(PCSTR pszRealm, PSTR *ppszUsername)
826 FIXME("(%s, %p)\n", debugstr_a(pszRealm), ppszUsername);
827 if (ppszUsername)
828 *ppszUsername = NULL;
829 return ERROR_NOT_FOUND;
832 /******************************************************************************
833 * CredUIReadSSOCredW [CREDUI.@]
835 DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
837 FIXME("(%s, %p)\n", debugstr_w(pszRealm), ppszUsername);
838 if (ppszUsername)
839 *ppszUsername = NULL;
840 return ERROR_NOT_FOUND;
843 /******************************************************************************
844 * CredUIInitControls [CREDUI.@]
846 BOOL WINAPI CredUIInitControls(void)
848 FIXME("() stub\n");
849 return TRUE;