msi: Don't defer custom actions in the UI sequence if they match the currently runnin...
[wine/multimedia.git] / dlls / credui / credui_main.c
blobc7b2da440395fdd9428dbf49f8c46f9a68cd2b8a
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 if (lpvReserved) break;
84 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
86 list_remove(&entry->entry);
88 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
89 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
90 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
91 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
92 HeapFree(GetProcessHeap(), 0, entry);
94 DeleteCriticalSection(&csPendingCredentials);
95 break;
98 return TRUE;
101 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
102 PCWSTR pszPassword, BOOL generic)
104 CREDENTIALW cred;
106 TRACE("saving servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
108 cred.Flags = 0;
109 cred.Type = generic ? CRED_TYPE_GENERIC : CRED_TYPE_DOMAIN_PASSWORD;
110 cred.TargetName = (LPWSTR)pszTargetName;
111 cred.Comment = NULL;
112 cred.CredentialBlobSize = strlenW(pszPassword) * sizeof(WCHAR);
113 cred.CredentialBlob = (LPBYTE)pszPassword;
114 cred.Persist = CRED_PERSIST_ENTERPRISE;
115 cred.AttributeCount = 0;
116 cred.Attributes = NULL;
117 cred.TargetAlias = NULL;
118 cred.UserName = (LPWSTR)pszUsername;
120 if (CredWriteW(&cred, 0))
121 return ERROR_SUCCESS;
122 else
124 DWORD ret = GetLastError();
125 ERR("CredWriteW failed with error %d\n", ret);
126 return ret;
130 struct cred_dialog_params
132 PCWSTR pszTargetName;
133 PCWSTR pszMessageText;
134 PCWSTR pszCaptionText;
135 HBITMAP hbmBanner;
136 PWSTR pszUsername;
137 ULONG ulUsernameMaxChars;
138 PWSTR pszPassword;
139 ULONG ulPasswordMaxChars;
140 BOOL fSave;
141 DWORD dwFlags;
142 HWND hwndBalloonTip;
143 BOOL fBalloonTipActive;
146 static void CredDialogFillUsernameCombo(HWND hwndUsername, const struct cred_dialog_params *params)
148 DWORD count;
149 DWORD i;
150 PCREDENTIALW *credentials;
152 if (!CredEnumerateW(NULL, 0, &count, &credentials))
153 return;
155 for (i = 0; i < count; i++)
157 COMBOBOXEXITEMW comboitem;
158 DWORD j;
159 BOOL duplicate = FALSE;
161 if (params->dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS)
163 if ((credentials[i]->Type != CRED_TYPE_GENERIC) || !credentials[i]->UserName)
164 continue;
166 else
168 if (credentials[i]->Type == CRED_TYPE_GENERIC)
169 continue;
172 /* don't add another item with the same name if we've already added it */
173 for (j = 0; j < i; j++)
174 if (!strcmpW(credentials[i]->UserName, credentials[j]->UserName))
176 duplicate = TRUE;
177 break;
180 if (duplicate)
181 continue;
183 comboitem.mask = CBEIF_TEXT;
184 comboitem.iItem = -1;
185 comboitem.pszText = credentials[i]->UserName;
186 SendMessageW(hwndUsername, CBEM_INSERTITEMW, 0, (LPARAM)&comboitem);
189 CredFree(credentials);
192 static void CredDialogCreateBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
194 TTTOOLINFOW toolinfo;
195 WCHAR wszText[256];
197 if (params->hwndBalloonTip)
198 return;
200 params->hwndBalloonTip = CreateWindowExW(WS_EX_TOOLWINDOW, TOOLTIPS_CLASSW,
201 NULL, WS_POPUP | TTS_NOPREFIX | TTS_BALLOON, CW_USEDEFAULT,
202 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwndDlg, NULL,
203 hinstCredUI, NULL);
204 SetWindowPos(params->hwndBalloonTip, HWND_TOPMOST, 0, 0, 0, 0,
205 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
207 if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORD, wszText, sizeof(wszText)/sizeof(wszText[0])))
209 ERR("failed to load IDS_INCORRECTPASSWORD\n");
210 return;
213 toolinfo.cbSize = sizeof(toolinfo);
214 toolinfo.uFlags = TTF_TRACK;
215 toolinfo.hwnd = hwndDlg;
216 toolinfo.uId = TOOLID_INCORRECTPASSWORD;
217 memset(&toolinfo.rect, 0, sizeof(toolinfo.rect));
218 toolinfo.hinst = NULL;
219 toolinfo.lpszText = wszText;
220 toolinfo.lParam = 0;
221 toolinfo.lpReserved = NULL;
222 SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
224 if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKON, wszText, sizeof(wszText)/sizeof(wszText[0])))
226 ERR("failed to load IDS_CAPSLOCKON\n");
227 return;
230 toolinfo.uId = TOOLID_CAPSLOCKON;
231 SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
234 static void CredDialogShowIncorrectPasswordBalloon(HWND hwndDlg, struct cred_dialog_params *params)
236 TTTOOLINFOW toolinfo;
237 RECT rcPassword;
238 INT x;
239 INT y;
240 WCHAR wszTitle[256];
242 /* user name likely wrong so balloon would be confusing. focus is also
243 * not set to the password edit box, so more notification would need to be
244 * handled */
245 if (!params->pszUsername[0])
246 return;
248 /* don't show two balloon tips at once */
249 if (params->fBalloonTipActive)
250 return;
252 if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORDTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
254 ERR("failed to load IDS_INCORRECTPASSWORDTITLE\n");
255 return;
258 CredDialogCreateBalloonTip(hwndDlg, params);
260 memset(&toolinfo, 0, sizeof(toolinfo));
261 toolinfo.cbSize = sizeof(toolinfo);
262 toolinfo.hwnd = hwndDlg;
263 toolinfo.uId = TOOLID_INCORRECTPASSWORD;
265 SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_ERROR, (LPARAM)wszTitle);
267 GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
268 /* centered vertically and in the right side of the password edit control */
269 x = rcPassword.right - 12;
270 y = (rcPassword.top + rcPassword.bottom) / 2;
271 SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
273 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
275 params->fBalloonTipActive = TRUE;
278 static void CredDialogShowCapsLockBalloon(HWND hwndDlg, struct cred_dialog_params *params)
280 TTTOOLINFOW toolinfo;
281 RECT rcPassword;
282 INT x;
283 INT y;
284 WCHAR wszTitle[256];
286 /* don't show two balloon tips at once */
287 if (params->fBalloonTipActive)
288 return;
290 if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKONTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
292 ERR("failed to load IDS_IDSCAPSLOCKONTITLE\n");
293 return;
296 CredDialogCreateBalloonTip(hwndDlg, params);
298 memset(&toolinfo, 0, sizeof(toolinfo));
299 toolinfo.cbSize = sizeof(toolinfo);
300 toolinfo.hwnd = hwndDlg;
301 toolinfo.uId = TOOLID_CAPSLOCKON;
303 SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_WARNING, (LPARAM)wszTitle);
305 GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
306 /* just inside the left side of the password edit control */
307 x = rcPassword.left + 12;
308 y = rcPassword.bottom - 3;
309 SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
311 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
313 SetTimer(hwndDlg, ID_CAPSLOCKPOP,
314 SendMessageW(params->hwndBalloonTip, TTM_GETDELAYTIME, TTDT_AUTOPOP, 0),
315 NULL);
317 params->fBalloonTipActive = TRUE;
320 static void CredDialogHideBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
322 TTTOOLINFOW toolinfo;
324 if (!params->hwndBalloonTip)
325 return;
327 memset(&toolinfo, 0, sizeof(toolinfo));
329 toolinfo.cbSize = sizeof(toolinfo);
330 toolinfo.hwnd = hwndDlg;
331 toolinfo.uId = 0;
332 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
333 toolinfo.uId = 1;
334 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
336 params->fBalloonTipActive = FALSE;
339 static inline BOOL CredDialogCapsLockOn(void)
341 return (GetKeyState(VK_CAPITAL) & 0x1) != 0;
344 static LRESULT CALLBACK CredDialogPasswordSubclassProc(HWND hwnd, UINT uMsg,
345 WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
347 struct cred_dialog_params *params = (struct cred_dialog_params *)dwRefData;
348 switch (uMsg)
350 case WM_KEYDOWN:
351 if (wParam == VK_CAPITAL)
353 HWND hwndDlg = GetParent(hwnd);
354 if (CredDialogCapsLockOn())
355 CredDialogShowCapsLockBalloon(hwndDlg, params);
356 else
357 CredDialogHideBalloonTip(hwndDlg, params);
359 break;
360 case WM_DESTROY:
361 RemoveWindowSubclass(hwnd, CredDialogPasswordSubclassProc, uIdSubclass);
362 break;
364 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
367 static BOOL CredDialogInit(HWND hwndDlg, struct cred_dialog_params *params)
369 HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
370 HWND hwndPassword = GetDlgItem(hwndDlg, IDC_PASSWORD);
372 SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
374 if (params->hbmBanner)
375 SendMessageW(GetDlgItem(hwndDlg, IDB_BANNER), STM_SETIMAGE,
376 IMAGE_BITMAP, (LPARAM)params->hbmBanner);
378 if (params->pszMessageText)
379 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, params->pszMessageText);
380 else
382 WCHAR format[256];
383 WCHAR message[256];
384 LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, sizeof(format)/sizeof(format[0]));
385 snprintfW(message, sizeof(message)/sizeof(message[0]), format, params->pszTargetName);
386 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, message);
388 SetWindowTextW(hwndUsername, params->pszUsername);
389 SetWindowTextW(hwndPassword, params->pszPassword);
391 CredDialogFillUsernameCombo(hwndUsername, params);
393 if (params->pszUsername[0])
395 /* prevent showing a balloon tip here */
396 params->fBalloonTipActive = TRUE;
397 SetFocus(hwndPassword);
398 params->fBalloonTipActive = FALSE;
400 else
401 SetFocus(hwndUsername);
403 if (params->pszCaptionText)
404 SetWindowTextW(hwndDlg, params->pszCaptionText);
405 else
407 WCHAR format[256];
408 WCHAR title[256];
409 LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, sizeof(format)/sizeof(format[0]));
410 snprintfW(title, sizeof(title)/sizeof(title[0]), format, params->pszTargetName);
411 SetWindowTextW(hwndDlg, title);
414 if (params->dwFlags & CREDUI_FLAGS_PERSIST ||
415 (params->dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST &&
416 !(params->dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX)))
417 ShowWindow(GetDlgItem(hwndDlg, IDC_SAVE), SW_HIDE);
418 else if (params->fSave)
419 CheckDlgButton(hwndDlg, IDC_SAVE, BST_CHECKED);
421 /* setup subclassing for Caps Lock detection */
422 SetWindowSubclass(hwndPassword, CredDialogPasswordSubclassProc, 1, (DWORD_PTR)params);
424 if (params->dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD)
425 CredDialogShowIncorrectPasswordBalloon(hwndDlg, params);
426 else if ((GetFocus() == hwndPassword) && CredDialogCapsLockOn())
427 CredDialogShowCapsLockBalloon(hwndDlg, params);
429 return FALSE;
432 static void CredDialogCommandOk(HWND hwndDlg, struct cred_dialog_params *params)
434 HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
435 LPWSTR user;
436 INT len;
437 INT len2;
439 len = GetWindowTextLengthW(hwndUsername);
440 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
441 GetWindowTextW(hwndUsername, user, len + 1);
443 if (!user[0])
445 HeapFree(GetProcessHeap(), 0, user);
446 return;
449 if (!strchrW(user, '\\') && !strchrW(user, '@'))
451 ULONG len_target = strlenW(params->pszTargetName);
452 memcpy(params->pszUsername, params->pszTargetName,
453 min(len_target, params->ulUsernameMaxChars) * sizeof(WCHAR));
454 if (len_target + 1 < params->ulUsernameMaxChars)
455 params->pszUsername[len_target] = '\\';
456 if (len_target + 2 < params->ulUsernameMaxChars)
457 params->pszUsername[len_target + 1] = '\0';
459 else if (params->ulUsernameMaxChars > 0)
460 params->pszUsername[0] = '\0';
462 len2 = strlenW(params->pszUsername);
463 memcpy(params->pszUsername + len2, user, min(len, params->ulUsernameMaxChars - len2) * sizeof(WCHAR));
464 if (params->ulUsernameMaxChars)
465 params->pszUsername[len2 + min(len, params->ulUsernameMaxChars - len2 - 1)] = '\0';
467 HeapFree(GetProcessHeap(), 0, user);
469 GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
470 params->ulPasswordMaxChars);
472 params->fSave = IsDlgButtonChecked(hwndDlg, IDC_SAVE) == BST_CHECKED;
474 EndDialog(hwndDlg, IDOK);
477 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
478 LPARAM lParam)
480 switch (uMsg)
482 case WM_INITDIALOG:
484 struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
486 return CredDialogInit(hwndDlg, params);
488 case WM_COMMAND:
489 switch (wParam)
491 case MAKELONG(IDOK, BN_CLICKED):
493 struct cred_dialog_params *params =
494 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
495 CredDialogCommandOk(hwndDlg, params);
496 return TRUE;
498 case MAKELONG(IDCANCEL, BN_CLICKED):
499 EndDialog(hwndDlg, IDCANCEL);
500 return TRUE;
501 case MAKELONG(IDC_PASSWORD, EN_SETFOCUS):
502 if (CredDialogCapsLockOn())
504 struct cred_dialog_params *params =
505 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
506 CredDialogShowCapsLockBalloon(hwndDlg, params);
508 /* don't allow another window to steal focus while the
509 * user is typing their password */
510 LockSetForegroundWindow(LSFW_LOCK);
511 return TRUE;
512 case MAKELONG(IDC_PASSWORD, EN_KILLFOCUS):
514 struct cred_dialog_params *params =
515 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
516 /* the user is no longer typing their password, so allow
517 * other windows to become foreground ones */
518 LockSetForegroundWindow(LSFW_UNLOCK);
519 CredDialogHideBalloonTip(hwndDlg, params);
520 return TRUE;
522 case MAKELONG(IDC_PASSWORD, EN_CHANGE):
524 struct cred_dialog_params *params =
525 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
526 CredDialogHideBalloonTip(hwndDlg, params);
527 return TRUE;
530 return FALSE;
531 case WM_TIMER:
532 if (wParam == ID_CAPSLOCKPOP)
534 struct cred_dialog_params *params =
535 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
536 CredDialogHideBalloonTip(hwndDlg, params);
537 return TRUE;
539 return FALSE;
540 case WM_DESTROY:
542 struct cred_dialog_params *params =
543 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
544 if (params->hwndBalloonTip) DestroyWindow(params->hwndBalloonTip);
545 return TRUE;
547 default:
548 return FALSE;
552 static BOOL find_existing_credential(const WCHAR *target, WCHAR *username, ULONG len_username,
553 WCHAR *password, ULONG len_password)
555 DWORD count, i;
556 CREDENTIALW **credentials;
558 if (!CredEnumerateW(target, 0, &count, &credentials)) return FALSE;
559 for (i = 0; i < count; i++)
561 if (credentials[i]->Type != CRED_TYPE_DOMAIN_PASSWORD)
563 FIXME("no support for type %u credentials\n", credentials[i]->Type);
564 continue;
566 if ((!*username || !strcmpW(username, credentials[i]->UserName)) &&
567 strlenW(credentials[i]->UserName) < len_username &&
568 credentials[i]->CredentialBlobSize / sizeof(WCHAR) < len_password)
570 TRACE("found existing credential for %s\n", debugstr_w(credentials[i]->UserName));
572 strcpyW(username, credentials[i]->UserName);
573 memcpy(password, credentials[i]->CredentialBlob, credentials[i]->CredentialBlobSize);
574 password[credentials[i]->CredentialBlobSize / sizeof(WCHAR)] = 0;
576 CredFree(credentials);
577 return TRUE;
580 CredFree(credentials);
581 return FALSE;
584 /******************************************************************************
585 * CredUIPromptForCredentialsW [CREDUI.@]
587 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
588 PCWSTR pszTargetName,
589 PCtxtHandle Reserved,
590 DWORD dwAuthError,
591 PWSTR pszUsername,
592 ULONG ulUsernameMaxChars,
593 PWSTR pszPassword,
594 ULONG ulPasswordMaxChars, PBOOL pfSave,
595 DWORD dwFlags)
597 INT_PTR ret;
598 struct cred_dialog_params params;
599 DWORD result = ERROR_SUCCESS;
601 TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
602 debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
603 ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
605 if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
606 return ERROR_INVALID_FLAGS;
608 if (!pszTargetName)
609 return ERROR_INVALID_PARAMETER;
611 if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
612 return ERROR_INVALID_PARAMETER;
614 if (!(dwFlags & CREDUI_FLAGS_ALWAYS_SHOW_UI) &&
615 !(dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD) &&
616 find_existing_credential(pszTargetName, pszUsername, ulUsernameMaxChars, pszPassword, ulPasswordMaxChars))
617 return ERROR_SUCCESS;
619 params.pszTargetName = pszTargetName;
620 if (pUIInfo)
622 params.pszMessageText = pUIInfo->pszMessageText;
623 params.pszCaptionText = pUIInfo->pszCaptionText;
624 params.hbmBanner = pUIInfo->hbmBanner;
626 else
628 params.pszMessageText = NULL;
629 params.pszCaptionText = NULL;
630 params.hbmBanner = NULL;
632 params.pszUsername = pszUsername;
633 params.ulUsernameMaxChars = ulUsernameMaxChars;
634 params.pszPassword = pszPassword;
635 params.ulPasswordMaxChars = ulPasswordMaxChars;
636 params.fSave = pfSave ? *pfSave : FALSE;
637 params.dwFlags = dwFlags;
638 params.hwndBalloonTip = NULL;
639 params.fBalloonTipActive = FALSE;
641 ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
642 pUIInfo ? pUIInfo->hwndParent : NULL,
643 CredDialogProc, (LPARAM)&params);
644 if (ret <= 0)
645 return GetLastError();
647 if (ret == IDCANCEL)
649 TRACE("dialog cancelled\n");
650 return ERROR_CANCELLED;
653 if (pfSave)
654 *pfSave = params.fSave;
656 if (params.fSave)
658 if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
660 BOOL found = FALSE;
661 struct pending_credentials *entry;
662 int len;
664 EnterCriticalSection(&csPendingCredentials);
666 /* find existing pending credentials for the same target and overwrite */
667 /* FIXME: is this correct? */
668 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
669 if (!strcmpW(pszTargetName, entry->pszTargetName))
671 found = TRUE;
672 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
673 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
674 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
677 if (!found)
679 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
680 len = strlenW(pszTargetName);
681 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
682 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
683 list_add_tail(&pending_credentials_list, &entry->entry);
686 len = strlenW(params.pszUsername);
687 entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
688 memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
689 len = strlenW(params.pszPassword);
690 entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
691 memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
692 entry->generic = (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0;
694 LeaveCriticalSection(&csPendingCredentials);
696 else if (!(dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST))
697 result = save_credentials(pszTargetName, pszUsername, pszPassword,
698 (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0);
701 return result;
704 /******************************************************************************
705 * CredUIConfirmCredentialsW [CREDUI.@]
707 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
709 struct pending_credentials *entry;
710 DWORD result = ERROR_NOT_FOUND;
712 TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
714 if (!pszTargetName)
715 return ERROR_INVALID_PARAMETER;
717 EnterCriticalSection(&csPendingCredentials);
719 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
721 if (!strcmpW(pszTargetName, entry->pszTargetName))
723 if (bConfirm)
724 result = save_credentials(entry->pszTargetName, entry->pszUsername,
725 entry->pszPassword, entry->generic);
726 else
727 result = ERROR_SUCCESS;
729 list_remove(&entry->entry);
731 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
732 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
733 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
734 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
735 HeapFree(GetProcessHeap(), 0, entry);
737 break;
741 LeaveCriticalSection(&csPendingCredentials);
743 return result;
746 /******************************************************************************
747 * CredUIParseUserNameW [CREDUI.@]
749 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
750 ULONG ulMaxUserChars, PWSTR pszDomain,
751 ULONG ulMaxDomainChars)
753 PWSTR p;
755 TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
756 ulMaxUserChars, pszDomain, ulMaxDomainChars);
758 if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
759 !ulMaxDomainChars)
760 return ERROR_INVALID_PARAMETER;
762 /* FIXME: handle marshaled credentials */
764 p = strchrW(pszUserName, '\\');
765 if (p)
767 if (p - pszUserName > ulMaxDomainChars - 1)
768 return ERROR_INSUFFICIENT_BUFFER;
769 if (strlenW(p + 1) > ulMaxUserChars - 1)
770 return ERROR_INSUFFICIENT_BUFFER;
771 strcpyW(pszUser, p + 1);
772 memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
773 pszDomain[p - pszUserName] = '\0';
775 return ERROR_SUCCESS;
778 p = strrchrW(pszUserName, '@');
779 if (p)
781 if (p + 1 - pszUserName > ulMaxUserChars - 1)
782 return ERROR_INSUFFICIENT_BUFFER;
783 if (strlenW(p + 1) > ulMaxDomainChars - 1)
784 return ERROR_INSUFFICIENT_BUFFER;
785 strcpyW(pszDomain, p + 1);
786 memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
787 pszUser[p - pszUserName] = '\0';
789 return ERROR_SUCCESS;
792 if (strlenW(pszUserName) > ulMaxUserChars - 1)
793 return ERROR_INSUFFICIENT_BUFFER;
794 strcpyW(pszUser, pszUserName);
795 pszDomain[0] = '\0';
797 return ERROR_SUCCESS;
800 /******************************************************************************
801 * CredUIStoreSSOCredA [CREDUI.@]
803 DWORD WINAPI CredUIStoreSSOCredA(PCSTR pszRealm, PCSTR pszUsername,
804 PCSTR pszPassword, BOOL bPersist)
806 FIXME("(%s, %s, %p, %d)\n", debugstr_a(pszRealm), debugstr_a(pszUsername),
807 pszPassword, bPersist);
808 return ERROR_SUCCESS;
811 /******************************************************************************
812 * CredUIStoreSSOCredW [CREDUI.@]
814 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR pszRealm, PCWSTR pszUsername,
815 PCWSTR pszPassword, BOOL bPersist)
817 FIXME("(%s, %s, %p, %d)\n", debugstr_w(pszRealm), debugstr_w(pszUsername),
818 pszPassword, bPersist);
819 return ERROR_SUCCESS;
822 /******************************************************************************
823 * CredUIReadSSOCredA [CREDUI.@]
825 DWORD WINAPI CredUIReadSSOCredA(PCSTR pszRealm, PSTR *ppszUsername)
827 FIXME("(%s, %p)\n", debugstr_a(pszRealm), ppszUsername);
828 if (ppszUsername)
829 *ppszUsername = NULL;
830 return ERROR_NOT_FOUND;
833 /******************************************************************************
834 * CredUIReadSSOCredW [CREDUI.@]
836 DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
838 FIXME("(%s, %p)\n", debugstr_w(pszRealm), ppszUsername);
839 if (ppszUsername)
840 *ppszUsername = NULL;
841 return ERROR_NOT_FOUND;
844 /******************************************************************************
845 * CredUIInitControls [CREDUI.@]
847 BOOL WINAPI CredUIInitControls(void)
849 FIXME("() stub\n");
850 return TRUE;