secur32: Basic implementation of schannel AcquireCredentialsHandle/FreeCredentialsHandle.
[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         ULONG 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     params->fSave = IsDlgButtonChecked(hwndDlg, IDC_SAVE) == BST_CHECKED;
466
467     EndDialog(hwndDlg, IDOK);
468 }
469
470 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
471                                        LPARAM lParam)
472 {
473     switch (uMsg)
474     {
475         case WM_INITDIALOG:
476         {
477             struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
478
479             return CredDialogInit(hwndDlg, params);
480         }
481         case WM_COMMAND:
482             switch (wParam)
483             {
484                 case MAKELONG(IDOK, BN_CLICKED):
485                 {
486                     struct cred_dialog_params *params =
487                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
488                     CredDialogCommandOk(hwndDlg, params);
489                     return TRUE;
490                 }
491                 case MAKELONG(IDCANCEL, BN_CLICKED):
492                     EndDialog(hwndDlg, IDCANCEL);
493                     return TRUE;
494                 case MAKELONG(IDC_PASSWORD, EN_SETFOCUS):
495                     if (CredDialogCapsLockOn())
496                     {
497                         struct cred_dialog_params *params =
498                             (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
499                         CredDialogShowCapsLockBalloon(hwndDlg, params);
500                     }
501                     /* don't allow another window to steal focus while the
502                      * user is typing their password */
503                     LockSetForegroundWindow(LSFW_LOCK);
504                     return TRUE;
505                 case MAKELONG(IDC_PASSWORD, EN_KILLFOCUS):
506                 {
507                     struct cred_dialog_params *params =
508                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
509                     /* the user is no longer typing their password, so allow
510                      * other windows to become foreground ones */
511                     LockSetForegroundWindow(LSFW_UNLOCK);
512                     CredDialogHideBalloonTip(hwndDlg, params);
513                     return TRUE;
514                 }
515                 case MAKELONG(IDC_PASSWORD, EN_CHANGE):
516                 {
517                     struct cred_dialog_params *params =
518                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
519                     CredDialogHideBalloonTip(hwndDlg, params);
520                     return TRUE;
521                 }
522             }
523             return FALSE;
524         case WM_TIMER:
525             if (wParam == ID_CAPSLOCKPOP)
526             {
527                 struct cred_dialog_params *params =
528                     (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
529                 CredDialogHideBalloonTip(hwndDlg, params);
530                 return TRUE;
531             }
532             return FALSE;
533         case WM_DESTROY:
534         {
535             struct cred_dialog_params *params =
536                 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
537             if (params->hwndBalloonTip) DestroyWindow(params->hwndBalloonTip);
538             return TRUE;
539         }
540         default:
541             return FALSE;
542     }
543 }
544
545 /******************************************************************************
546  *           CredUIPromptForCredentialsW [CREDUI.@]
547  */
548 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
549                                          PCWSTR pszTargetName,
550                                          PCtxtHandle Reserved,
551                                          DWORD dwAuthError,
552                                          PWSTR pszUsername,
553                                          ULONG ulUsernameMaxChars,
554                                          PWSTR pszPassword,
555                                          ULONG ulPasswordMaxChars, PBOOL pfSave,
556                                          DWORD dwFlags)
557 {
558     INT_PTR ret;
559     struct cred_dialog_params params;
560     DWORD result = ERROR_SUCCESS;
561
562     TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
563           debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
564           ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
565
566     if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
567         return ERROR_INVALID_FLAGS;
568
569     if (!pszTargetName)
570         return ERROR_INVALID_PARAMETER;
571
572     if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
573         return ERROR_INVALID_PARAMETER;
574
575     params.pszTargetName = pszTargetName;
576     if (pUIInfo)
577     {
578         params.pszMessageText = pUIInfo->pszMessageText;
579         params.pszCaptionText = pUIInfo->pszCaptionText;
580         params.hbmBanner  = pUIInfo->hbmBanner;
581     }
582     else
583     {
584         params.pszMessageText = NULL;
585         params.pszCaptionText = NULL;
586         params.hbmBanner = NULL;
587     }
588     params.pszUsername = pszUsername;
589     params.ulUsernameMaxChars = ulUsernameMaxChars;
590     params.pszPassword = pszPassword;
591     params.ulPasswordMaxChars = ulPasswordMaxChars;
592     params.fSave = pfSave ? *pfSave : FALSE;
593     params.dwFlags = dwFlags;
594     params.hwndBalloonTip = NULL;
595     params.fBalloonTipActive = FALSE;
596
597     ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
598                           pUIInfo ? pUIInfo->hwndParent : NULL,
599                           CredDialogProc, (LPARAM)&params);
600     if (ret <= 0)
601         return GetLastError();
602
603     if (ret == IDCANCEL)
604     {
605         TRACE("dialog cancelled\n");
606         return ERROR_CANCELLED;
607     }
608
609     if (pfSave)
610         *pfSave = params.fSave;
611
612     if (params.fSave)
613     {
614         if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
615         {
616             BOOL found = FALSE;
617             struct pending_credentials *entry;
618             int len;
619
620             EnterCriticalSection(&csPendingCredentials);
621
622             /* find existing pending credentials for the same target and overwrite */
623             /* FIXME: is this correct? */
624             LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
625                 if (!strcmpW(pszTargetName, entry->pszTargetName))
626                 {
627                     found = TRUE;
628                     HeapFree(GetProcessHeap(), 0, entry->pszUsername);
629                     ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
630                     HeapFree(GetProcessHeap(), 0, entry->pszPassword);
631                 }
632
633             if (!found)
634             {
635                 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
636                 len = strlenW(pszTargetName);
637                 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
638                 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
639                 list_add_tail(&pending_credentials_list, &entry->entry);
640             }
641
642             len = strlenW(params.pszUsername);
643             entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
644             memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
645             len = strlenW(params.pszPassword);
646             entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
647             memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
648             entry->generic = dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE;
649
650             LeaveCriticalSection(&csPendingCredentials);
651         }
652         else
653             result = save_credentials(pszTargetName, pszUsername, pszPassword,
654                                       dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE);
655     }
656
657     return result;
658 }
659
660 /******************************************************************************
661  *           CredUIConfirmCredentialsW [CREDUI.@]
662  */
663 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
664 {
665     struct pending_credentials *entry;
666     DWORD result = ERROR_NOT_FOUND;
667
668     TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
669
670     if (!pszTargetName)
671         return ERROR_INVALID_PARAMETER;
672
673     EnterCriticalSection(&csPendingCredentials);
674
675     LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
676     {
677         if (!strcmpW(pszTargetName, entry->pszTargetName))
678         {
679             if (bConfirm)
680                 result = save_credentials(entry->pszTargetName, entry->pszUsername,
681                                           entry->pszPassword, entry->generic);
682             else
683                 result = ERROR_SUCCESS;
684
685             list_remove(&entry->entry);
686
687             HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
688             HeapFree(GetProcessHeap(), 0, entry->pszUsername);
689             ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
690             HeapFree(GetProcessHeap(), 0, entry->pszPassword);
691             HeapFree(GetProcessHeap(), 0, entry);
692
693             break;
694         }
695     }
696
697     LeaveCriticalSection(&csPendingCredentials);
698
699     return result;
700 }
701
702 /******************************************************************************
703  *           CredUIParseUserNameW [CREDUI.@]
704  */
705 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
706                                   ULONG ulMaxUserChars, PWSTR pszDomain,
707                                   ULONG ulMaxDomainChars)
708 {
709     PWSTR p;
710
711     TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
712           ulMaxUserChars, pszDomain, ulMaxDomainChars);
713
714     if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
715         !ulMaxDomainChars)
716         return ERROR_INVALID_PARAMETER;
717
718     /* FIXME: handle marshaled credentials */
719
720     p = strchrW(pszUserName, '\\');
721     if (p)
722     {
723         if (p - pszUserName > ulMaxDomainChars - 1)
724             return ERROR_INSUFFICIENT_BUFFER;
725         if (strlenW(p + 1) > ulMaxUserChars - 1)
726             return ERROR_INSUFFICIENT_BUFFER;
727         strcpyW(pszUser, p + 1);
728         memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
729         pszDomain[p - pszUserName] = '\0';
730
731         return ERROR_SUCCESS;
732     }
733
734     p = strrchrW(pszUserName, '@');
735     if (p)
736     {
737         if (p + 1 - pszUserName > ulMaxUserChars - 1)
738             return ERROR_INSUFFICIENT_BUFFER;
739         if (strlenW(p + 1) > ulMaxDomainChars - 1)
740             return ERROR_INSUFFICIENT_BUFFER;
741         strcpyW(pszDomain, p + 1);
742         memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
743         pszUser[p - pszUserName] = '\0';
744
745         return ERROR_SUCCESS;
746     }
747
748     if (strlenW(pszUserName) > ulMaxUserChars - 1)
749         return ERROR_INSUFFICIENT_BUFFER;
750     strcpyW(pszUser, pszUserName);
751     pszDomain[0] = '\0';
752
753     return ERROR_SUCCESS;
754 }
755
756 /******************************************************************************
757  *           CredUIStoreSSOCredA [CREDUI.@]
758  */
759 DWORD WINAPI CredUIStoreSSOCredA(PCSTR pszRealm, PCSTR pszUsername,
760                                  PCSTR pszPassword, BOOL bPersist)
761 {
762     FIXME("(%s, %s, %p, %d)\n", debugstr_a(pszRealm), debugstr_a(pszUsername),
763           pszPassword, bPersist);
764     return ERROR_SUCCESS;
765 }
766
767 /******************************************************************************
768  *           CredUIStoreSSOCredW [CREDUI.@]
769  */
770 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR pszRealm, PCWSTR pszUsername,
771                                  PCWSTR pszPassword, BOOL bPersist)
772 {
773     FIXME("(%s, %s, %p, %d)\n", debugstr_w(pszRealm), debugstr_w(pszUsername),
774           pszPassword, bPersist);
775     return ERROR_SUCCESS;
776 }
777
778 /******************************************************************************
779  *           CredUIReadSSOCredA [CREDUI.@]
780  */
781 DWORD WINAPI CredUIReadSSOCredA(PCSTR pszRealm, PSTR *ppszUsername)
782 {
783     FIXME("(%s, %p)\n", debugstr_a(pszRealm), ppszUsername);
784     if (ppszUsername)
785         *ppszUsername = NULL;
786     return ERROR_NOT_FOUND;
787 }
788
789 /******************************************************************************
790  *           CredUIReadSSOCredW [CREDUI.@]
791  */
792 DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
793 {
794     FIXME("(%s, %p)\n", debugstr_w(pszRealm), ppszUsername);
795     if (ppszUsername)
796         *ppszUsername = NULL;
797     return ERROR_NOT_FOUND;
798 }