credui: Display a warning balloon if the user has Caps Lock on.
[wine] / dlls / credui / credui_main.c
1 /*
2  * Credentials User Interface
3  *
4  * Copyright 2006 Robert Shearman (for CodeWeavers)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnt.h"
26 #include "winuser.h"
27 #include "wincred.h"
28 #include "commctrl.h"
29
30 #include "credui_resources.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "wine/list.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(credui);
37
38 #define TOOLID_INCORRECTPASSWORD    1
39 #define TOOLID_CAPSLOCKON           2
40
41 #define ID_CAPSLOCKPOP              1
42
43 struct pending_credentials
44 {
45     struct list entry;
46     PWSTR pszTargetName;
47     PWSTR pszUsername;
48     PWSTR pszPassword;
49     BOOL generic;
50 };
51
52 static HINSTANCE hinstCredUI;
53
54 static struct list pending_credentials_list = LIST_INIT(pending_credentials_list);
55
56 static CRITICAL_SECTION csPendingCredentials;
57 static CRITICAL_SECTION_DEBUG critsect_debug =
58 {
59     0, 0, &csPendingCredentials,
60     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
61     0, 0, { (DWORD_PTR)(__FILE__ ": csPendingCredentials") }
62 };
63 static CRITICAL_SECTION csPendingCredentials = { &critsect_debug, -1, 0, 0, 0, 0 };
64
65
66 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
67 {
68     TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
69
70     if (fdwReason == DLL_WINE_PREATTACH) return FALSE;  /* prefer native version */
71
72     if (fdwReason == DLL_PROCESS_ATTACH)
73     {
74         DisableThreadLibraryCalls(hinstDLL);
75         hinstCredUI = hinstDLL;
76         InitCommonControls();
77     }
78     else if (fdwReason == DLL_PROCESS_DETACH)
79     {
80         struct pending_credentials *entry, *cursor2;
81         LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
82         {
83             list_remove(&entry->entry);
84
85             HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
86             HeapFree(GetProcessHeap(), 0, entry->pszUsername);
87             ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
88             HeapFree(GetProcessHeap(), 0, entry->pszPassword);
89             HeapFree(GetProcessHeap(), 0, entry);
90         }
91     }
92
93     return TRUE;
94 }
95
96 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
97                               PCWSTR pszPassword, BOOL generic)
98 {
99     CREDENTIALW cred;
100
101     TRACE("saving servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
102
103     cred.Flags = 0;
104     cred.Type = generic ? CRED_TYPE_GENERIC : CRED_TYPE_DOMAIN_PASSWORD;
105     cred.TargetName = (LPWSTR)pszTargetName;
106     cred.Comment = NULL;
107     cred.CredentialBlobSize = strlenW(pszPassword) * sizeof(WCHAR);
108     cred.CredentialBlob = (LPBYTE)pszPassword;
109     cred.Persist = CRED_PERSIST_ENTERPRISE;
110     cred.AttributeCount = 0;
111     cred.Attributes = NULL;
112     cred.TargetAlias = NULL;
113     cred.UserName = (LPWSTR)pszUsername;
114
115     if (CredWriteW(&cred, 0))
116         return ERROR_SUCCESS;
117     else
118     {
119         DWORD ret = GetLastError();
120         ERR("CredWriteW failed with error %d\n", ret);
121         return ret;
122     }
123 }
124
125 struct cred_dialog_params
126 {
127     PCWSTR pszTargetName;
128     PCWSTR pszMessageText;
129     PCWSTR pszCaptionText;
130     HBITMAP hbmBanner;
131     PWSTR pszUsername;
132     ULONG ulUsernameMaxChars;
133     PWSTR pszPassword;
134     ULONG ulPasswordMaxChars;
135     BOOL fSave;
136     DWORD dwFlags;
137     HWND hwndBalloonTip;
138     BOOL fBalloonTipActive;
139 };
140
141 static void CredDialogFillUsernameCombo(HWND hwndUsername, struct cred_dialog_params *params)
142 {
143     DWORD count;
144     DWORD i;
145     PCREDENTIALW *credentials;
146
147     if (!CredEnumerateW(NULL, 0, &count, &credentials))
148         return;
149
150     for (i = 0; i < count; i++)
151     {
152         COMBOBOXEXITEMW comboitem;
153         DWORD j;
154         BOOL duplicate = FALSE;
155
156         if (params->dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS)
157         {
158             if ((credentials[i]->Type != CRED_TYPE_GENERIC) || !credentials[i]->UserName)
159                 continue;
160         }
161         else
162         {
163             if (credentials[i]->Type == CRED_TYPE_GENERIC)
164                 continue;
165         }
166
167         /* don't add another item with the same name if we've already added it */
168         for (j = 0; j < i; j++)
169             if (!strcmpW(credentials[i]->UserName, credentials[j]->UserName))
170             {
171                 duplicate = TRUE;
172                 break;
173             }
174
175         if (duplicate)
176             continue;
177
178         comboitem.mask = CBEIF_TEXT;
179         comboitem.iItem = -1;
180         comboitem.pszText = credentials[i]->UserName;
181         SendMessageW(hwndUsername, CBEM_INSERTITEMW, 0, (LPARAM)&comboitem);
182     }
183
184     CredFree(credentials);
185 }
186
187 static void CredDialogCreateBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
188 {
189     TTTOOLINFOW toolinfo;
190     WCHAR wszText[256];
191
192     if (params->hwndBalloonTip)
193         return;
194
195     params->hwndBalloonTip = CreateWindowExW(WS_EX_TOOLWINDOW, TOOLTIPS_CLASSW,
196         NULL, WS_POPUP | TTS_NOPREFIX | TTS_BALLOON, CW_USEDEFAULT,
197         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwndDlg, NULL,
198         hinstCredUI, NULL);
199     SetWindowPos(params->hwndBalloonTip, HWND_TOPMOST, 0, 0, 0, 0,
200                  SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
201
202     if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORD, wszText, sizeof(wszText)/sizeof(wszText[0])))
203     {
204         ERR("failed to load IDS_INCORRECTPASSWORD\n");
205         return;
206     }
207
208     toolinfo.cbSize = sizeof(toolinfo);
209     toolinfo.uFlags = TTF_TRACK;
210     toolinfo.hwnd = hwndDlg;
211     toolinfo.uId = TOOLID_INCORRECTPASSWORD;
212     memset(&toolinfo.rect, 0, sizeof(toolinfo.rect));
213     toolinfo.hinst = NULL;
214     toolinfo.lpszText = wszText;
215     toolinfo.lParam = 0;
216     toolinfo.lpReserved = NULL;
217     SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
218
219     if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKON, wszText, sizeof(wszText)/sizeof(wszText[0])))
220     {
221         ERR("failed to load IDS_CAPSLOCKON\n");
222         return;
223     }
224
225     toolinfo.uId = TOOLID_CAPSLOCKON;
226     SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
227 }
228
229 static void CredDialogShowIncorrectPasswordBalloon(HWND hwndDlg, struct cred_dialog_params *params)
230 {
231     TTTOOLINFOW toolinfo;
232     RECT rcPassword;
233     INT x;
234     INT y;
235     WCHAR wszTitle[256];
236
237     /* user name likely wrong so balloon would be confusing. focus is also
238      * not set to the password edit box, so more notification would need to be
239      * handled */
240     if (!params->pszUsername[0])
241         return;
242
243     /* don't show two balloon tips at once */
244     if (params->fBalloonTipActive)
245         return;
246
247     if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORDTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
248     {
249         ERR("failed to load IDS_INCORRECTPASSWORDTITLE\n");
250         return;
251     }
252
253     CredDialogCreateBalloonTip(hwndDlg, params);
254
255     memset(&toolinfo, 0, sizeof(toolinfo));
256     toolinfo.cbSize = sizeof(toolinfo);
257     toolinfo.hwnd = hwndDlg;
258     toolinfo.uId = TOOLID_INCORRECTPASSWORD;
259
260     SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_ERROR, (LPARAM)wszTitle);
261
262     GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
263     /* centred vertically and in the right side of the password edit control */
264     x = rcPassword.right - 12;
265     y = (rcPassword.top + rcPassword.bottom) / 2;
266     SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
267
268     SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
269
270     params->fBalloonTipActive = TRUE;
271 }
272
273 static void CredDialogShowCapsLockBalloon(HWND hwndDlg, struct cred_dialog_params *params)
274 {
275     TTTOOLINFOW toolinfo;
276     RECT rcPassword;
277     INT x;
278     INT y;
279     WCHAR wszTitle[256];
280
281     /* don't show two balloon tips at once */
282     if (params->fBalloonTipActive)
283         return;
284
285     if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKONTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
286     {
287         ERR("failed to load IDS_IDSCAPSLOCKONTITLE\n");
288         return;
289     }
290
291     CredDialogCreateBalloonTip(hwndDlg, params);
292
293     memset(&toolinfo, 0, sizeof(toolinfo));
294     toolinfo.cbSize = sizeof(toolinfo);
295     toolinfo.hwnd = hwndDlg;
296     toolinfo.uId = TOOLID_CAPSLOCKON;
297
298     SendMessageW(params->hwndBalloonTip, TTM_SETTITLEW, TTI_WARNING, (LPARAM)wszTitle);
299
300     GetWindowRect(GetDlgItem(hwndDlg, IDC_PASSWORD), &rcPassword);
301     /* just inside the left side of the password edit control */
302     x = rcPassword.left + 12;
303     y = rcPassword.bottom - 3;
304     SendMessageW(params->hwndBalloonTip, TTM_TRACKPOSITION, 0, MAKELONG(x, y));
305
306     SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&toolinfo);
307
308     SetTimer(hwndDlg, ID_CAPSLOCKPOP,
309              SendMessageW(params->hwndBalloonTip, TTM_GETDELAYTIME, TTDT_AUTOPOP, 0),
310              NULL);
311
312     params->fBalloonTipActive = TRUE;
313 }
314
315 static void CredDialogHideBalloonTip(HWND hwndDlg, struct cred_dialog_params *params)
316 {
317     TTTOOLINFOW toolinfo;
318
319     if (!params->hwndBalloonTip)
320         return;
321
322     memset(&toolinfo, 0, sizeof(toolinfo));
323
324     toolinfo.cbSize = sizeof(toolinfo);
325     toolinfo.hwnd = hwndDlg;
326     toolinfo.uId = 0;
327     SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
328     toolinfo.uId = 1;
329     SendMessageW(params->hwndBalloonTip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&toolinfo);
330
331     params->fBalloonTipActive = FALSE;
332 }
333
334 static inline BOOL CredDialogCapsLockOn(void)
335 {
336     return GetKeyState(VK_CAPITAL) & 0x1 ? TRUE : FALSE;
337 }
338
339 static LRESULT CALLBACK CredDialogPasswordSubclassProc(HWND hwnd, UINT uMsg,
340     WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
341 {
342     struct cred_dialog_params *params = (struct cred_dialog_params *)dwRefData;
343     switch (uMsg)
344     {
345     case WM_KEYDOWN:
346         if (wParam == VK_CAPITAL)
347         {
348             HWND hwndDlg = GetParent(hwnd);
349             if (CredDialogCapsLockOn())
350                 CredDialogShowCapsLockBalloon(hwndDlg, params);
351             else
352                 CredDialogHideBalloonTip(hwndDlg, params);
353         }
354         break;
355     case WM_DESTROY:
356         RemoveWindowSubclass(hwnd, CredDialogPasswordSubclassProc, uIdSubclass);
357         break;
358     }
359     return DefSubclassProc(hwnd, uMsg, wParam, lParam);
360 }
361
362 static BOOL CredDialogInit(HWND hwndDlg, struct cred_dialog_params *params)
363 {
364     HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
365     HWND hwndPassword = GetDlgItem(hwndDlg, IDC_PASSWORD);
366
367     SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
368
369     if (params->hbmBanner)
370         SendMessageW(GetDlgItem(hwndDlg, IDB_BANNER), STM_SETIMAGE,
371                      IMAGE_BITMAP, (LPARAM)params->hbmBanner);
372
373     if (params->pszMessageText)
374         SetDlgItemTextW(hwndDlg, IDC_MESSAGE, params->pszMessageText);
375     else
376     {
377         WCHAR format[256];
378         WCHAR message[256];
379         LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, sizeof(format)/sizeof(format[0]));
380         snprintfW(message, sizeof(message)/sizeof(message[0]), format, params->pszTargetName);
381         SetDlgItemTextW(hwndDlg, IDC_MESSAGE, message);
382     }
383     SetWindowTextW(hwndUsername, params->pszUsername);
384     SetWindowTextW(hwndPassword, params->pszPassword);
385
386     CredDialogFillUsernameCombo(hwndUsername, params);
387
388     if (params->pszUsername[0])
389     {
390         /* prevent showing a balloon tip here */
391         params->fBalloonTipActive = TRUE;
392         SetFocus(hwndPassword);
393         params->fBalloonTipActive = FALSE;
394     }
395     else
396         SetFocus(hwndUsername);
397
398     if (params->pszCaptionText)
399         SetWindowTextW(hwndDlg, params->pszCaptionText);
400     else
401     {
402         WCHAR format[256];
403         WCHAR title[256];
404         LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, sizeof(format)/sizeof(format[0]));
405         snprintfW(title, sizeof(title)/sizeof(title[0]), format, params->pszTargetName);
406         SetWindowTextW(hwndDlg, title);
407     }
408
409     if (params->dwFlags & (CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_PERSIST))
410         ShowWindow(GetDlgItem(hwndDlg, IDC_SAVE), SW_HIDE);
411     else if (params->fSave)
412         CheckDlgButton(hwndDlg, IDC_SAVE, BST_CHECKED);
413
414     /* setup subclassing for Caps Lock detection */
415     SetWindowSubclass(hwndPassword, CredDialogPasswordSubclassProc, 1, (DWORD_PTR)params);
416
417     if (params->dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD)
418         CredDialogShowIncorrectPasswordBalloon(hwndDlg, params);
419     else if ((GetFocus() == hwndPassword) && CredDialogCapsLockOn())
420         CredDialogShowCapsLockBalloon(hwndDlg, params);
421
422     return FALSE;
423 }
424
425 static void CredDialogCommandOk(HWND hwndDlg, struct cred_dialog_params *params)
426 {
427     HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
428     LPWSTR user;
429     INT len;
430     INT len2;
431
432     len = GetWindowTextLengthW(hwndUsername);
433     user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
434     GetWindowTextW(hwndUsername, user, len + 1);
435
436     if (!user[0])
437     {
438         HeapFree(GetProcessHeap(), 0, user);
439         return;
440     }
441
442     if (!strchrW(user, '\\') && !strchrW(user, '@'))
443     {
444         INT len_target = strlenW(params->pszTargetName);
445         memcpy(params->pszUsername, params->pszTargetName,
446                min(len_target, params->ulUsernameMaxChars) * sizeof(WCHAR));
447         if (len_target + 1 < params->ulUsernameMaxChars)
448             params->pszUsername[len_target] = '\\';
449         if (len_target + 2 < params->ulUsernameMaxChars)
450             params->pszUsername[len_target + 1] = '\0';
451     }
452     else if (params->ulUsernameMaxChars > 0)
453         params->pszUsername[0] = '\0';
454
455     len2 = strlenW(params->pszUsername);
456     memcpy(params->pszUsername + len2, user, min(len, params->ulUsernameMaxChars - len2) * sizeof(WCHAR));
457     if (params->ulUsernameMaxChars)
458         params->pszUsername[len2 + min(len, params->ulUsernameMaxChars - len2 - 1)] = '\0';
459
460     HeapFree(GetProcessHeap(), 0, user);
461
462     GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
463                     params->ulPasswordMaxChars);
464
465     EndDialog(hwndDlg, IDOK);
466 }
467
468 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
469                                        LPARAM lParam)
470 {
471     switch (uMsg)
472     {
473         case WM_INITDIALOG:
474         {
475             struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
476
477             return CredDialogInit(hwndDlg, params);
478         }
479         case WM_COMMAND:
480             switch (wParam)
481             {
482                 case MAKELONG(IDOK, BN_CLICKED):
483                 {
484                     struct cred_dialog_params *params =
485                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
486                     CredDialogCommandOk(hwndDlg, params);
487                     return TRUE;
488                 }
489                 case MAKELONG(IDCANCEL, BN_CLICKED):
490                     EndDialog(hwndDlg, IDCANCEL);
491                     return TRUE;
492                 case MAKELONG(IDC_PASSWORD, EN_SETFOCUS):
493                     if (CredDialogCapsLockOn())
494                     {
495                         struct cred_dialog_params *params =
496                             (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
497                         CredDialogShowCapsLockBalloon(hwndDlg, params);
498                     }
499                     /* don't allow another window to steal focus while the
500                      * user is typing their password */
501                     LockSetForegroundWindow(LSFW_LOCK);
502                     return TRUE;
503                 case MAKELONG(IDC_PASSWORD, EN_KILLFOCUS):
504                 {
505                     struct cred_dialog_params *params =
506                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
507                     /* the user is no longer typing their password, so allow
508                      * other windows to become foreground ones */
509                     LockSetForegroundWindow(LSFW_UNLOCK);
510                     CredDialogHideBalloonTip(hwndDlg, params);
511                     return TRUE;
512                 }
513                 case MAKELONG(IDC_PASSWORD, EN_CHANGE):
514                 {
515                     struct cred_dialog_params *params =
516                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
517                     CredDialogHideBalloonTip(hwndDlg, params);
518                     return TRUE;
519                 }
520             }
521             return FALSE;
522         case WM_TIMER:
523             if (wParam == ID_CAPSLOCKPOP)
524             {
525                 struct cred_dialog_params *params =
526                     (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
527                 CredDialogHideBalloonTip(hwndDlg, params);
528                 return TRUE;
529             }
530             return FALSE;
531         case WM_DESTROY:
532         {
533             struct cred_dialog_params *params =
534                 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
535             if (params->hwndBalloonTip) DestroyWindow(params->hwndBalloonTip);
536             return TRUE;
537         }
538         default:
539             return FALSE;
540     }
541 }
542
543 /******************************************************************************
544  *           CredUIPromptForCredentialsW [CREDUI.@]
545  */
546 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
547                                          PCWSTR pszTargetName,
548                                          PCtxtHandle Reserved,
549                                          DWORD dwAuthError,
550                                          PWSTR pszUsername,
551                                          ULONG ulUsernameMaxChars,
552                                          PWSTR pszPassword,
553                                          ULONG ulPasswordMaxChars, PBOOL pfSave,
554                                          DWORD dwFlags)
555 {
556     INT_PTR ret;
557     struct cred_dialog_params params;
558     DWORD result = ERROR_SUCCESS;
559
560     TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
561           debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
562           ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
563
564     if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
565         return ERROR_INVALID_FLAGS;
566
567     if (!pszTargetName)
568         return ERROR_INVALID_PARAMETER;
569
570     if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
571         return ERROR_INVALID_PARAMETER;
572
573     params.pszTargetName = pszTargetName;
574     if (pUIInfo)
575     {
576         params.pszMessageText = pUIInfo->pszMessageText;
577         params.pszCaptionText = pUIInfo->pszCaptionText;
578         params.hbmBanner  = pUIInfo->hbmBanner;
579     }
580     else
581     {
582         params.pszMessageText = NULL;
583         params.pszCaptionText = NULL;
584         params.hbmBanner = NULL;
585     }
586     params.pszUsername = pszUsername;
587     params.ulUsernameMaxChars = ulUsernameMaxChars;
588     params.pszPassword = pszPassword;
589     params.ulPasswordMaxChars = ulPasswordMaxChars;
590     params.fSave = pfSave ? *pfSave : FALSE;
591     params.dwFlags = dwFlags;
592     params.hwndBalloonTip = NULL;
593     params.fBalloonTipActive = FALSE;
594
595     ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
596                           pUIInfo ? pUIInfo->hwndParent : NULL,
597                           CredDialogProc, (LPARAM)&params);
598     if (ret <= 0)
599         return GetLastError();
600
601     if (ret == IDCANCEL)
602     {
603         TRACE("dialog cancelled\n");
604         return ERROR_CANCELLED;
605     }
606
607     if (pfSave)
608         *pfSave = params.fSave;
609
610     if (params.fSave)
611     {
612         if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
613         {
614             BOOL found = FALSE;
615             struct pending_credentials *entry;
616             int len;
617
618             EnterCriticalSection(&csPendingCredentials);
619
620             /* find existing pending credentials for the same target and overwrite */
621             /* FIXME: is this correct? */
622             LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
623                 if (!strcmpW(pszTargetName, entry->pszTargetName))
624                 {
625                     found = TRUE;
626                     HeapFree(GetProcessHeap(), 0, entry->pszUsername);
627                     ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
628                     HeapFree(GetProcessHeap(), 0, entry->pszPassword);
629                 }
630
631             if (!found)
632             {
633                 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
634                 list_init(&entry->entry);
635                 len = strlenW(pszTargetName);
636                 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
637                 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
638                 list_add_tail(&entry->entry, &pending_credentials_list);
639             }
640
641             len = strlenW(params.pszUsername);
642             entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
643             memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
644             len = strlenW(params.pszPassword);
645             entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
646             memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
647             entry->generic = dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE;
648
649             LeaveCriticalSection(&csPendingCredentials);
650         }
651         else
652             result = save_credentials(pszTargetName, pszUsername, pszPassword,
653                                       dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE);
654     }
655
656     return result;
657 }
658
659 /******************************************************************************
660  *           CredUIConfirmCredentialsW [CREDUI.@]
661  */
662 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
663 {
664     struct pending_credentials *entry;
665     DWORD result = ERROR_NOT_FOUND;
666
667     TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
668
669     if (!pszTargetName)
670         return ERROR_INVALID_PARAMETER;
671
672     EnterCriticalSection(&csPendingCredentials);
673
674     LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
675     {
676         if (!strcmpW(pszTargetName, entry->pszTargetName))
677         {
678             if (bConfirm)
679                 result = save_credentials(entry->pszTargetName, entry->pszUsername,
680                                           entry->pszPassword, entry->generic);
681             else
682                 result = ERROR_SUCCESS;
683
684             list_remove(&entry->entry);
685
686             HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
687             HeapFree(GetProcessHeap(), 0, entry->pszUsername);
688             ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
689             HeapFree(GetProcessHeap(), 0, entry->pszPassword);
690             HeapFree(GetProcessHeap(), 0, entry);
691
692             break;
693         }
694     }
695
696     LeaveCriticalSection(&csPendingCredentials);
697
698     return result;
699 }
700
701 /******************************************************************************
702  *           CredUIParseUserNameW [CREDUI.@]
703  */
704 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
705                                   ULONG ulMaxUserChars, PWSTR pszDomain,
706                                   ULONG ulMaxDomainChars)
707 {
708     PWSTR p;
709
710     TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
711           ulMaxUserChars, pszDomain, ulMaxDomainChars);
712
713     if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
714         !ulMaxDomainChars)
715         return ERROR_INVALID_PARAMETER;
716
717     /* FIXME: handle marshaled credentials */
718
719     p = strchrW(pszUserName, '\\');
720     if (p)
721     {
722         if (p - pszUserName > ulMaxDomainChars - 1)
723             return ERROR_INSUFFICIENT_BUFFER;
724         if (strlenW(p + 1) > ulMaxUserChars - 1)
725             return ERROR_INSUFFICIENT_BUFFER;
726         strcpyW(pszUser, p + 1);
727         memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
728         pszDomain[p - pszUserName] = '\0';
729
730         return ERROR_SUCCESS;
731     }
732
733     p = strrchrW(pszUserName, '@');
734     if (p)
735     {
736         if (p + 1 - pszUserName > ulMaxUserChars - 1)
737             return ERROR_INSUFFICIENT_BUFFER;
738         if (strlenW(p + 1) > ulMaxDomainChars - 1)
739             return ERROR_INSUFFICIENT_BUFFER;
740         strcpyW(pszDomain, p + 1);
741         memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
742         pszUser[p - pszUserName] = '\0';
743
744         return ERROR_SUCCESS;
745     }
746
747     if (strlenW(pszUserName) > ulMaxUserChars - 1)
748         return ERROR_INSUFFICIENT_BUFFER;
749     strcpyW(pszUser, pszUserName);
750     pszDomain[0] = '\0';
751
752     return ERROR_SUCCESS;
753 }
754
755 /******************************************************************************
756  *           CredUIStoreSSOCredA [CREDUI.@]
757  */
758 DWORD WINAPI CredUIStoreSSOCredA(PCSTR pszRealm, PCSTR pszUsername,
759                                  PCSTR pszPassword, BOOL bPersist)
760 {
761     FIXME("(%s, %s, %p, %d)\n", debugstr_a(pszRealm), debugstr_a(pszUsername),
762           pszPassword, bPersist);
763     return ERROR_SUCCESS;
764 }
765
766 /******************************************************************************
767  *           CredUIStoreSSOCredW [CREDUI.@]
768  */
769 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR pszRealm, PCWSTR pszUsername,
770                                  PCWSTR pszPassword, BOOL bPersist)
771 {
772     FIXME("(%s, %s, %p, %d)\n", debugstr_w(pszRealm), debugstr_w(pszUsername),
773           pszPassword, bPersist);
774     return ERROR_SUCCESS;
775 }
776
777 /******************************************************************************
778  *           CredUIReadSSOCredA [CREDUI.@]
779  */
780 DWORD WINAPI CredUIReadSSOCredA(PCSTR pszRealm, PSTR *ppszUsername)
781 {
782     FIXME("(%s, %p)\n", debugstr_a(pszRealm), ppszUsername);
783     if (ppszUsername)
784         *ppszUsername = NULL;
785     return ERROR_NOT_FOUND;
786 }
787
788 /******************************************************************************
789  *           CredUIReadSSOCredW [CREDUI.@]
790  */
791 DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
792 {
793     FIXME("(%s, %p)\n", debugstr_w(pszRealm), ppszUsername);
794     if (ppszUsername)
795         *ppszUsername = NULL;
796     return ERROR_NOT_FOUND;
797 }