bcrypt: Require macOS version 10.7 or newer for encryption support.
[wine.git] / dlls / credui / credui_main.c
bloba4df9fe43302de8115f039c26c4ff0b67de81dc9
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 "rpc.h"
29 #include "sspi.h"
30 #include "commctrl.h"
32 #include "credui_resources.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(credui);
40 #define TOOLID_INCORRECTPASSWORD 1
41 #define TOOLID_CAPSLOCKON 2
43 #define ID_CAPSLOCKPOP 1
45 struct pending_credentials
47 struct list entry;
48 PWSTR pszTargetName;
49 PWSTR pszUsername;
50 PWSTR pszPassword;
51 BOOL generic;
54 static HINSTANCE hinstCredUI;
56 static struct list pending_credentials_list = LIST_INIT(pending_credentials_list);
58 static CRITICAL_SECTION csPendingCredentials;
59 static CRITICAL_SECTION_DEBUG critsect_debug =
61 0, 0, &csPendingCredentials,
62 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": csPendingCredentials") }
65 static CRITICAL_SECTION csPendingCredentials = { &critsect_debug, -1, 0, 0, 0, 0 };
68 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
70 struct pending_credentials *entry, *cursor2;
71 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
73 switch (fdwReason)
75 case DLL_WINE_PREATTACH:
76 return FALSE; /* prefer native version */
78 case DLL_PROCESS_ATTACH:
79 DisableThreadLibraryCalls(hinstDLL);
80 hinstCredUI = hinstDLL;
81 InitCommonControls();
82 break;
84 case DLL_PROCESS_DETACH:
85 if (lpvReserved) break;
86 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
88 list_remove(&entry->entry);
90 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
91 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
92 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
93 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
94 HeapFree(GetProcessHeap(), 0, entry);
96 DeleteCriticalSection(&csPendingCredentials);
97 break;
100 return TRUE;
103 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
104 PCWSTR pszPassword, BOOL generic)
106 CREDENTIALW cred;
108 TRACE("saving servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
110 cred.Flags = 0;
111 cred.Type = generic ? CRED_TYPE_GENERIC : CRED_TYPE_DOMAIN_PASSWORD;
112 cred.TargetName = (LPWSTR)pszTargetName;
113 cred.Comment = NULL;
114 cred.CredentialBlobSize = strlenW(pszPassword) * sizeof(WCHAR);
115 cred.CredentialBlob = (LPBYTE)pszPassword;
116 cred.Persist = CRED_PERSIST_ENTERPRISE;
117 cred.AttributeCount = 0;
118 cred.Attributes = NULL;
119 cred.TargetAlias = NULL;
120 cred.UserName = (LPWSTR)pszUsername;
122 if (CredWriteW(&cred, 0))
123 return ERROR_SUCCESS;
124 else
126 DWORD ret = GetLastError();
127 ERR("CredWriteW failed with error %d\n", ret);
128 return ret;
132 struct cred_dialog_params
134 PCWSTR pszTargetName;
135 PCWSTR pszMessageText;
136 PCWSTR pszCaptionText;
137 HBITMAP hbmBanner;
138 PWSTR pszUsername;
139 ULONG ulUsernameMaxChars;
140 PWSTR pszPassword;
141 ULONG ulPasswordMaxChars;
142 BOOL fSave;
143 DWORD dwFlags;
144 HWND hwndBalloonTip;
145 BOOL fBalloonTipActive;
148 static void CredDialogFillUsernameCombo(HWND hwndUsername, const struct cred_dialog_params *params)
150 DWORD count;
151 DWORD i;
152 PCREDENTIALW *credentials;
154 if (!CredEnumerateW(NULL, 0, &count, &credentials))
155 return;
157 for (i = 0; i < count; i++)
159 COMBOBOXEXITEMW comboitem;
160 DWORD j;
161 BOOL duplicate = FALSE;
163 if (params->dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS)
165 if ((credentials[i]->Type != CRED_TYPE_GENERIC) || !credentials[i]->UserName)
166 continue;
168 else
170 if (credentials[i]->Type == CRED_TYPE_GENERIC)
171 continue;
174 /* don't add another item with the same name if we've already added it */
175 for (j = 0; j < i; j++)
176 if (!strcmpW(credentials[i]->UserName, credentials[j]->UserName))
178 duplicate = TRUE;
179 break;
182 if (duplicate)
183 continue;
185 comboitem.mask = CBEIF_TEXT;
186 comboitem.iItem = -1;
187 comboitem.pszText = credentials[i]->UserName;
188 SendMessageW(hwndUsername, CBEM_INSERTITEMW, 0, (LPARAM)&comboitem);
191 CredFree(credentials);
194 static void CredDialogCreateBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
196 TTTOOLINFOW toolinfo;
197 WCHAR wszText[256];
199 if (params->hwndBalloonTip)
200 return;
202 params->hwndBalloonTip = CreateWindowExW(WS_EX_TOOLWINDOW, TOOLTIPS_CLASSW,
203 NULL, WS_POPUP | TTS_NOPREFIX | TTS_BALLOON, CW_USEDEFAULT,
204 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwndDlg, NULL,
205 hinstCredUI, NULL);
206 SetWindowPos(params->hwndBalloonTip, HWND_TOPMOST, 0, 0, 0, 0,
207 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
209 if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORD, wszText, sizeof(wszText)/sizeof(wszText[0])))
211 ERR("failed to load IDS_INCORRECTPASSWORD\n");
212 return;
215 toolinfo.cbSize = sizeof(toolinfo);
216 toolinfo.uFlags = TTF_TRACK;
217 toolinfo.hwnd = hwndDlg;
218 toolinfo.uId = TOOLID_INCORRECTPASSWORD;
219 SetRectEmpty(&toolinfo.rect);
220 toolinfo.hinst = NULL;
221 toolinfo.lpszText = wszText;
222 toolinfo.lParam = 0;
223 toolinfo.lpReserved = NULL;
224 SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
226 if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKON, wszText, sizeof(wszText)/sizeof(wszText[0])))
228 ERR("failed to load IDS_CAPSLOCKON\n");
229 return;
232 toolinfo.uId = TOOLID_CAPSLOCKON;
233 SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
236 static void CredDialogShowIncorrectPasswordBalloon(HWND hwndDlg, struct cred_dialog_params *params)
238 TTTOOLINFOW toolinfo;
239 RECT rcPassword;
240 INT x;
241 INT y;
242 WCHAR wszTitle[256];
244 /* user name likely wrong so balloon would be confusing. focus is also
245 * not set to the password edit box, so more notification would need to be
246 * handled */
247 if (!params->pszUsername[0])
248 return;
250 /* don't show two balloon tips at once */
251 if (params->fBalloonTipActive)
252 return;
254 if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORDTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
256 ERR("failed to load IDS_INCORRECTPASSWORDTITLE\n");
257 return;
260 CredDialogCreateBalloonTip(hwndDlg, params);
262 memset(&toolinfo, 0, sizeof(toolinfo));
263 toolinfo.cbSize = sizeof(toolinfo);
264 toolinfo.hwnd = hwndDlg;
265 toolinfo.uId = TOOLID_INCORRECTPASSWORD;
267 SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_ERROR, (LPARAM)wszTitle);
269 GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
270 /* centered vertically and in the right side of the password edit control */
271 x = rcPassword.right - 12;
272 y = (rcPassword.top + rcPassword.bottom) / 2;
273 SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
275 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
277 params->fBalloonTipActive = TRUE;
280 static void CredDialogShowCapsLockBalloon(HWND hwndDlg, struct cred_dialog_params *params)
282 TTTOOLINFOW toolinfo;
283 RECT rcPassword;
284 INT x;
285 INT y;
286 WCHAR wszTitle[256];
288 /* don't show two balloon tips at once */
289 if (params->fBalloonTipActive)
290 return;
292 if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKONTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
294 ERR("failed to load IDS_IDSCAPSLOCKONTITLE\n");
295 return;
298 CredDialogCreateBalloonTip(hwndDlg, params);
300 memset(&toolinfo, 0, sizeof(toolinfo));
301 toolinfo.cbSize = sizeof(toolinfo);
302 toolinfo.hwnd = hwndDlg;
303 toolinfo.uId = TOOLID_CAPSLOCKON;
305 SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_WARNING, (LPARAM)wszTitle);
307 GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
308 /* just inside the left side of the password edit control */
309 x = rcPassword.left + 12;
310 y = rcPassword.bottom - 3;
311 SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
313 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
315 SetTimer(hwndDlg, ID_CAPSLOCKPOP,
316 SendMessageW(params->hwndBalloonTip, TTM_GETDELAYTIME, TTDT_AUTOPOP, 0),
317 NULL);
319 params->fBalloonTipActive = TRUE;
322 static void CredDialogHideBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
324 TTTOOLINFOW toolinfo;
326 if (!params->hwndBalloonTip)
327 return;
329 memset(&toolinfo, 0, sizeof(toolinfo));
331 toolinfo.cbSize = sizeof(toolinfo);
332 toolinfo.hwnd = hwndDlg;
333 toolinfo.uId = 0;
334 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
335 toolinfo.uId = 1;
336 SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
338 params->fBalloonTipActive = FALSE;
341 static inline BOOL CredDialogCapsLockOn(void)
343 return (GetKeyState(VK_CAPITAL) & 0x1) != 0;
346 static LRESULT CALLBACK CredDialogPasswordSubclassProc(HWND hwnd, UINT uMsg,
347 WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
349 struct cred_dialog_params *params = (struct cred_dialog_params *)dwRefData;
350 switch (uMsg)
352 case WM_KEYDOWN:
353 if (wParam == VK_CAPITAL)
355 HWND hwndDlg = GetParent(hwnd);
356 if (CredDialogCapsLockOn())
357 CredDialogShowCapsLockBalloon(hwndDlg, params);
358 else
359 CredDialogHideBalloonTip(hwndDlg, params);
361 break;
362 case WM_DESTROY:
363 RemoveWindowSubclass(hwnd, CredDialogPasswordSubclassProc, uIdSubclass);
364 break;
366 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
369 static BOOL CredDialogInit(HWND hwndDlg, struct cred_dialog_params *params)
371 HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
372 HWND hwndPassword = GetDlgItem(hwndDlg, IDC_PASSWORD);
374 SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
376 if (params->hbmBanner)
377 SendMessageW(GetDlgItem(hwndDlg, IDB_BANNER), STM_SETIMAGE,
378 IMAGE_BITMAP, (LPARAM)params->hbmBanner);
380 if (params->pszMessageText)
381 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, params->pszMessageText);
382 else
384 WCHAR format[256];
385 WCHAR message[256];
386 LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, sizeof(format)/sizeof(format[0]));
387 snprintfW(message, sizeof(message)/sizeof(message[0]), format, params->pszTargetName);
388 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, message);
390 SetWindowTextW(hwndUsername, params->pszUsername);
391 SetWindowTextW(hwndPassword, params->pszPassword);
393 CredDialogFillUsernameCombo(hwndUsername, params);
395 if (params->pszUsername[0])
397 /* prevent showing a balloon tip here */
398 params->fBalloonTipActive = TRUE;
399 SetFocus(hwndPassword);
400 params->fBalloonTipActive = FALSE;
402 else
403 SetFocus(hwndUsername);
405 if (params->pszCaptionText)
406 SetWindowTextW(hwndDlg, params->pszCaptionText);
407 else
409 WCHAR format[256];
410 WCHAR title[256];
411 LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, sizeof(format)/sizeof(format[0]));
412 snprintfW(title, sizeof(title)/sizeof(title[0]), format, params->pszTargetName);
413 SetWindowTextW(hwndDlg, title);
416 if (params->dwFlags & CREDUI_FLAGS_PERSIST ||
417 (params->dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST &&
418 !(params->dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX)))
419 ShowWindow(GetDlgItem(hwndDlg, IDC_SAVE), SW_HIDE);
420 else if (params->fSave)
421 CheckDlgButton(hwndDlg, IDC_SAVE, BST_CHECKED);
423 /* setup subclassing for Caps Lock detection */
424 SetWindowSubclass(hwndPassword, CredDialogPasswordSubclassProc, 1, (DWORD_PTR)params);
426 if (params->dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD)
427 CredDialogShowIncorrectPasswordBalloon(hwndDlg, params);
428 else if ((GetFocus() == hwndPassword) && CredDialogCapsLockOn())
429 CredDialogShowCapsLockBalloon(hwndDlg, params);
431 return FALSE;
434 static void CredDialogCommandOk(HWND hwndDlg, struct cred_dialog_params *params)
436 HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
437 LPWSTR user;
438 INT len;
439 INT len2;
441 len = GetWindowTextLengthW(hwndUsername);
442 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
443 GetWindowTextW(hwndUsername, user, len + 1);
445 if (!user[0])
447 HeapFree(GetProcessHeap(), 0, user);
448 return;
451 if (!strchrW(user, '\\') && !strchrW(user, '@'))
453 ULONG len_target = strlenW(params->pszTargetName);
454 memcpy(params->pszUsername, params->pszTargetName,
455 min(len_target, params->ulUsernameMaxChars) * sizeof(WCHAR));
456 if (len_target + 1 < params->ulUsernameMaxChars)
457 params->pszUsername[len_target] = '\\';
458 if (len_target + 2 < params->ulUsernameMaxChars)
459 params->pszUsername[len_target + 1] = '\0';
461 else if (params->ulUsernameMaxChars > 0)
462 params->pszUsername[0] = '\0';
464 len2 = strlenW(params->pszUsername);
465 memcpy(params->pszUsername + len2, user, min(len, params->ulUsernameMaxChars - len2) * sizeof(WCHAR));
466 if (params->ulUsernameMaxChars)
467 params->pszUsername[len2 + min(len, params->ulUsernameMaxChars - len2 - 1)] = '\0';
469 HeapFree(GetProcessHeap(), 0, user);
471 GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
472 params->ulPasswordMaxChars);
474 params->fSave = IsDlgButtonChecked(hwndDlg, IDC_SAVE) == BST_CHECKED;
476 EndDialog(hwndDlg, IDOK);
479 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
480 LPARAM lParam)
482 switch (uMsg)
484 case WM_INITDIALOG:
486 struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
488 return CredDialogInit(hwndDlg, params);
490 case WM_COMMAND:
491 switch (wParam)
493 case MAKELONG(IDOK, BN_CLICKED):
495 struct cred_dialog_params *params =
496 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
497 CredDialogCommandOk(hwndDlg, params);
498 return TRUE;
500 case MAKELONG(IDCANCEL, BN_CLICKED):
501 EndDialog(hwndDlg, IDCANCEL);
502 return TRUE;
503 case MAKELONG(IDC_PASSWORD, EN_SETFOCUS):
504 if (CredDialogCapsLockOn())
506 struct cred_dialog_params *params =
507 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
508 CredDialogShowCapsLockBalloon(hwndDlg, params);
510 /* don't allow another window to steal focus while the
511 * user is typing their password */
512 LockSetForegroundWindow(LSFW_LOCK);
513 return TRUE;
514 case MAKELONG(IDC_PASSWORD, EN_KILLFOCUS):
516 struct cred_dialog_params *params =
517 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
518 /* the user is no longer typing their password, so allow
519 * other windows to become foreground ones */
520 LockSetForegroundWindow(LSFW_UNLOCK);
521 CredDialogHideBalloonTip(hwndDlg, params);
522 return TRUE;
524 case MAKELONG(IDC_PASSWORD, EN_CHANGE):
526 struct cred_dialog_params *params =
527 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
528 CredDialogHideBalloonTip(hwndDlg, params);
529 return TRUE;
532 return FALSE;
533 case WM_TIMER:
534 if (wParam == ID_CAPSLOCKPOP)
536 struct cred_dialog_params *params =
537 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
538 CredDialogHideBalloonTip(hwndDlg, params);
539 return TRUE;
541 return FALSE;
542 case WM_DESTROY:
544 struct cred_dialog_params *params =
545 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
546 if (params->hwndBalloonTip) DestroyWindow(params->hwndBalloonTip);
547 return TRUE;
549 default:
550 return FALSE;
554 static BOOL find_existing_credential(const WCHAR *target, WCHAR *username, ULONG len_username,
555 WCHAR *password, ULONG len_password)
557 DWORD count, i;
558 CREDENTIALW **credentials;
560 if (!CredEnumerateW(target, 0, &count, &credentials)) return FALSE;
561 for (i = 0; i < count; i++)
563 if (credentials[i]->Type != CRED_TYPE_DOMAIN_PASSWORD &&
564 credentials[i]->Type != CRED_TYPE_GENERIC)
566 FIXME("no support for type %u credentials\n", credentials[i]->Type);
567 continue;
569 if ((!*username || !strcmpW(username, credentials[i]->UserName)) &&
570 strlenW(credentials[i]->UserName) < len_username &&
571 credentials[i]->CredentialBlobSize / sizeof(WCHAR) < len_password)
573 TRACE("found existing credential for %s\n", debugstr_w(credentials[i]->UserName));
575 strcpyW(username, credentials[i]->UserName);
576 memcpy(password, credentials[i]->CredentialBlob, credentials[i]->CredentialBlobSize);
577 password[credentials[i]->CredentialBlobSize / sizeof(WCHAR)] = 0;
579 CredFree(credentials);
580 return TRUE;
583 CredFree(credentials);
584 return FALSE;
587 /******************************************************************************
588 * CredUIPromptForCredentialsW [CREDUI.@]
590 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
591 PCWSTR pszTargetName,
592 PCtxtHandle Reserved,
593 DWORD dwAuthError,
594 PWSTR pszUsername,
595 ULONG ulUsernameMaxChars,
596 PWSTR pszPassword,
597 ULONG ulPasswordMaxChars, PBOOL pfSave,
598 DWORD dwFlags)
600 INT_PTR ret;
601 struct cred_dialog_params params;
602 DWORD result = ERROR_SUCCESS;
604 TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
605 debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
606 ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
608 if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
609 return ERROR_INVALID_FLAGS;
611 if (!pszTargetName)
612 return ERROR_INVALID_PARAMETER;
614 if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
615 return ERROR_INVALID_PARAMETER;
617 if (!(dwFlags & CREDUI_FLAGS_ALWAYS_SHOW_UI) &&
618 !(dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD) &&
619 find_existing_credential(pszTargetName, pszUsername, ulUsernameMaxChars, pszPassword, ulPasswordMaxChars))
620 return ERROR_SUCCESS;
622 params.pszTargetName = pszTargetName;
623 if (pUIInfo)
625 params.pszMessageText = pUIInfo->pszMessageText;
626 params.pszCaptionText = pUIInfo->pszCaptionText;
627 params.hbmBanner = pUIInfo->hbmBanner;
629 else
631 params.pszMessageText = NULL;
632 params.pszCaptionText = NULL;
633 params.hbmBanner = NULL;
635 params.pszUsername = pszUsername;
636 params.ulUsernameMaxChars = ulUsernameMaxChars;
637 params.pszPassword = pszPassword;
638 params.ulPasswordMaxChars = ulPasswordMaxChars;
639 params.fSave = pfSave ? *pfSave : FALSE;
640 params.dwFlags = dwFlags;
641 params.hwndBalloonTip = NULL;
642 params.fBalloonTipActive = FALSE;
644 ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
645 pUIInfo ? pUIInfo->hwndParent : NULL,
646 CredDialogProc, (LPARAM)&params);
647 if (ret <= 0)
648 return GetLastError();
650 if (ret == IDCANCEL)
652 TRACE("dialog cancelled\n");
653 return ERROR_CANCELLED;
656 if (pfSave)
657 *pfSave = params.fSave;
659 if (params.fSave)
661 if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
663 BOOL found = FALSE;
664 struct pending_credentials *entry;
665 int len;
667 EnterCriticalSection(&csPendingCredentials);
669 /* find existing pending credentials for the same target and overwrite */
670 /* FIXME: is this correct? */
671 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
672 if (!strcmpW(pszTargetName, entry->pszTargetName))
674 found = TRUE;
675 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
676 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
677 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
680 if (!found)
682 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
683 len = strlenW(pszTargetName);
684 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
685 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
686 list_add_tail(&pending_credentials_list, &entry->entry);
689 len = strlenW(params.pszUsername);
690 entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
691 memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
692 len = strlenW(params.pszPassword);
693 entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
694 memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
695 entry->generic = (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0;
697 LeaveCriticalSection(&csPendingCredentials);
699 else if (!(dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST))
700 result = save_credentials(pszTargetName, pszUsername, pszPassword,
701 (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0);
704 return result;
707 /******************************************************************************
708 * CredUIConfirmCredentialsW [CREDUI.@]
710 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
712 struct pending_credentials *entry;
713 DWORD result = ERROR_NOT_FOUND;
715 TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
717 if (!pszTargetName)
718 return ERROR_INVALID_PARAMETER;
720 EnterCriticalSection(&csPendingCredentials);
722 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
724 if (!strcmpW(pszTargetName, entry->pszTargetName))
726 if (bConfirm)
727 result = save_credentials(entry->pszTargetName, entry->pszUsername,
728 entry->pszPassword, entry->generic);
729 else
730 result = ERROR_SUCCESS;
732 list_remove(&entry->entry);
734 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
735 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
736 ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
737 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
738 HeapFree(GetProcessHeap(), 0, entry);
740 break;
744 LeaveCriticalSection(&csPendingCredentials);
746 return result;
749 /******************************************************************************
750 * CredUIParseUserNameW [CREDUI.@]
752 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
753 ULONG ulMaxUserChars, PWSTR pszDomain,
754 ULONG ulMaxDomainChars)
756 PWSTR p;
758 TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
759 ulMaxUserChars, pszDomain, ulMaxDomainChars);
761 if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
762 !ulMaxDomainChars)
763 return ERROR_INVALID_PARAMETER;
765 /* FIXME: handle marshaled credentials */
767 p = strchrW(pszUserName, '\\');
768 if (p)
770 if (p - pszUserName > ulMaxDomainChars - 1)
771 return ERROR_INSUFFICIENT_BUFFER;
772 if (strlenW(p + 1) > ulMaxUserChars - 1)
773 return ERROR_INSUFFICIENT_BUFFER;
774 strcpyW(pszUser, p + 1);
775 memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
776 pszDomain[p - pszUserName] = '\0';
778 return ERROR_SUCCESS;
781 p = strrchrW(pszUserName, '@');
782 if (p)
784 if (p + 1 - pszUserName > ulMaxUserChars - 1)
785 return ERROR_INSUFFICIENT_BUFFER;
786 if (strlenW(p + 1) > ulMaxDomainChars - 1)
787 return ERROR_INSUFFICIENT_BUFFER;
788 strcpyW(pszDomain, p + 1);
789 memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
790 pszUser[p - pszUserName] = '\0';
792 return ERROR_SUCCESS;
795 if (strlenW(pszUserName) > ulMaxUserChars - 1)
796 return ERROR_INSUFFICIENT_BUFFER;
797 strcpyW(pszUser, pszUserName);
798 pszDomain[0] = '\0';
800 return ERROR_SUCCESS;
803 /******************************************************************************
804 * CredUIStoreSSOCredA [CREDUI.@]
806 DWORD WINAPI CredUIStoreSSOCredA(PCSTR pszRealm, PCSTR pszUsername,
807 PCSTR pszPassword, BOOL bPersist)
809 FIXME("(%s, %s, %p, %d)\n", debugstr_a(pszRealm), debugstr_a(pszUsername),
810 pszPassword, bPersist);
811 return ERROR_SUCCESS;
814 /******************************************************************************
815 * CredUIStoreSSOCredW [CREDUI.@]
817 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR pszRealm, PCWSTR pszUsername,
818 PCWSTR pszPassword, BOOL bPersist)
820 FIXME("(%s, %s, %p, %d)\n", debugstr_w(pszRealm), debugstr_w(pszUsername),
821 pszPassword, bPersist);
822 return ERROR_SUCCESS;
825 /******************************************************************************
826 * CredUIReadSSOCredA [CREDUI.@]
828 DWORD WINAPI CredUIReadSSOCredA(PCSTR pszRealm, PSTR *ppszUsername)
830 FIXME("(%s, %p)\n", debugstr_a(pszRealm), ppszUsername);
831 if (ppszUsername)
832 *ppszUsername = NULL;
833 return ERROR_NOT_FOUND;
836 /******************************************************************************
837 * CredUIReadSSOCredW [CREDUI.@]
839 DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
841 FIXME("(%s, %p)\n", debugstr_w(pszRealm), ppszUsername);
842 if (ppszUsername)
843 *ppszUsername = NULL;
844 return ERROR_NOT_FOUND;
847 /******************************************************************************
848 * CredUIInitControls [CREDUI.@]
850 BOOL WINAPI CredUIInitControls(void)
852 FIXME("() stub\n");
853 return TRUE;
856 /******************************************************************************
857 * SspiPromptForCredentialsW [CREDUI.@]
859 ULONG SEC_ENTRY SspiPromptForCredentialsW( PCWSTR target, void *info,
860 DWORD error, PCWSTR package,
861 PSEC_WINNT_AUTH_IDENTITY_OPAQUE input_id,
862 PSEC_WINNT_AUTH_IDENTITY_OPAQUE *output_id,
863 BOOL *save, DWORD sspi_flags )
865 static const WCHAR basicW[] = {'B','a','s','i','c',0};
866 static const WCHAR ntlmW[] = {'N','T','L','M',0};
867 static const WCHAR negotiateW[] = {'N','e','g','o','t','i','a','t','e',0};
868 WCHAR username[CREDUI_MAX_USERNAME_LENGTH + 1] = {0};
869 WCHAR password[CREDUI_MAX_PASSWORD_LENGTH + 1] = {0};
870 DWORD len_username = sizeof(username) / sizeof(username[0]);
871 DWORD len_password = sizeof(password) / sizeof(password[0]);
872 DWORD ret, flags;
873 CREDUI_INFOW *cred_info = info;
874 SEC_WINNT_AUTH_IDENTITY_W *id = input_id;
876 FIXME( "(%s, %p, %u, %s, %p, %p, %p, %x) stub\n", debugstr_w(target), info,
877 error, debugstr_w(package), input_id, output_id, save, sspi_flags );
879 if (!target) return ERROR_INVALID_PARAMETER;
880 if (!package || (strcmpiW( package, basicW ) && strcmpiW( package, ntlmW ) &&
881 strcmpiW( package, negotiateW )))
883 FIXME( "package %s not supported\n", debugstr_w(package) );
884 return ERROR_NO_SUCH_PACKAGE;
887 flags = CREDUI_FLAGS_ALWAYS_SHOW_UI | CREDUI_FLAGS_GENERIC_CREDENTIALS;
889 if (sspi_flags & SSPIPFC_CREDPROV_DO_NOT_SAVE)
890 flags |= CREDUI_FLAGS_DO_NOT_PERSIST;
892 if (!(sspi_flags & SSPIPFC_NO_CHECKBOX))
893 flags |= CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX;
895 if (!id) find_existing_credential( target, username, len_username, password, len_password );
896 else
898 if (id->User && id->UserLength > 0 && id->UserLength <= CREDUI_MAX_USERNAME_LENGTH)
900 memcpy( username, id->User, id->UserLength * sizeof(WCHAR) );
901 username[id->UserLength] = 0;
903 if (id->Password && id->PasswordLength > 0 && id->PasswordLength <= CREDUI_MAX_PASSWORD_LENGTH)
905 memcpy( password, id->Password, id->PasswordLength * sizeof(WCHAR) );
906 password[id->PasswordLength] = 0;
910 if (!(ret = CredUIPromptForCredentialsW( cred_info, target, NULL, error, username,
911 len_username, password, len_password, save, flags )))
913 DWORD size = sizeof(*id), len_domain = 0;
914 WCHAR *ptr, *user = username, *domain = NULL;
916 if ((ptr = strchrW( username, '\\' )))
918 user = ptr + 1;
919 len_username = strlenW( user );
920 if (!strcmpiW( package, ntlmW ) || !strcmpiW( package, negotiateW ))
922 domain = username;
923 len_domain = ptr - username;
925 *ptr = 0;
927 else len_username = strlenW( username );
928 len_password = strlenW( password );
930 size += (len_username + 1) * sizeof(WCHAR);
931 size += (len_domain + 1) * sizeof(WCHAR);
932 size += (len_password + 1) * sizeof(WCHAR);
933 if (!(id = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
934 ptr = (WCHAR *)(id + 1);
936 memcpy( ptr, user, (len_username + 1) * sizeof(WCHAR) );
937 id->User = ptr;
938 id->UserLength = len_username;
939 ptr += len_username + 1;
940 if (len_domain)
942 memcpy( ptr, domain, (len_domain + 1) * sizeof(WCHAR) );
943 id->Domain = ptr;
944 id->DomainLength = len_domain;
945 ptr += len_domain + 1;
947 else
949 id->Domain = NULL;
950 id->DomainLength = 0;
952 memcpy( ptr, password, (len_password + 1) * sizeof(WCHAR) );
953 id->Password = ptr;
954 id->PasswordLength = len_password;
955 id->Flags = 0;
957 *output_id = id;
960 return ret;
963 /******************************************************************************
964 * CredUIPromptForWindowsCredentialsW [CREDUI.@]
966 DWORD WINAPI CredUIPromptForWindowsCredentialsW( CREDUI_INFOW *info, DWORD error, ULONG *package,
967 const void *in_buf, ULONG in_buf_size, void **out_buf,
968 ULONG *out_buf_size, BOOL *save, DWORD flags )
970 FIXME( "(%p, %u, %p, %p, %u, %p, %p, %p, %08x) stub\n", info, error, package, in_buf, in_buf_size,
971 out_buf, out_buf_size, save, flags );
972 return ERROR_CALL_NOT_IMPLEMENTED;
975 /******************************************************************************
976 * CredPackAuthenticationBufferW [CREDUI.@]
978 BOOL WINAPI CredPackAuthenticationBufferW( DWORD flags, WCHAR *username, WCHAR *password, BYTE *buf,
979 DWORD *size )
981 FIXME( "(%08x, %s, %p, %p, %p) stub\n", flags, debugstr_w(username), password, buf, size );
982 return ERROR_CALL_NOT_IMPLEMENTED;
985 /******************************************************************************
986 * CredUnPackAuthenticationBufferW [CREDUI.@]
988 BOOL WINAPI CredUnPackAuthenticationBufferW( DWORD flags, void *buf, DWORD size, WCHAR *username,
989 DWORD *len_username, WCHAR *domain, DWORD *len_domain,
990 WCHAR *password, DWORD *len_password )
992 FIXME( "(%08x, %p, %u, %p, %p, %p, %p, %p, %p) stub\n", flags, buf, size, username, len_username,
993 domain, len_domain, password, len_password );
994 return ERROR_CALL_NOT_IMPLEMENTED;