mshtml: Fixed handling channels without container and necko channel.
[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 struct pending_credentials
39 {
40     struct list entry;
41     PWSTR pszTargetName;
42     PWSTR pszUsername;
43     PWSTR pszPassword;
44     BOOL generic;
45 };
46
47 static HINSTANCE hinstCredUI;
48
49 static struct list pending_credentials_list = LIST_INIT(pending_credentials_list);
50
51 static CRITICAL_SECTION csPendingCredentials;
52 static CRITICAL_SECTION_DEBUG critsect_debug =
53 {
54     0, 0, &csPendingCredentials,
55     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
56     0, 0, { (DWORD_PTR)(__FILE__ ": csPendingCredentials") }
57 };
58 static CRITICAL_SECTION csPendingCredentials = { &critsect_debug, -1, 0, 0, 0, 0 };
59
60
61 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
62 {
63     TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
64
65     if (fdwReason == DLL_WINE_PREATTACH) return FALSE;  /* prefer native version */
66
67     if (fdwReason == DLL_PROCESS_ATTACH)
68     {
69         DisableThreadLibraryCalls(hinstDLL);
70         hinstCredUI = hinstDLL;
71         InitCommonControls();
72     }
73     else if (fdwReason == DLL_PROCESS_DETACH)
74     {
75         struct pending_credentials *entry, *cursor2;
76         LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
77         {
78             list_remove(&entry->entry);
79
80             HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
81             HeapFree(GetProcessHeap(), 0, entry->pszUsername);
82             ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
83             HeapFree(GetProcessHeap(), 0, entry->pszPassword);
84             HeapFree(GetProcessHeap(), 0, entry);
85         }
86     }
87
88     return TRUE;
89 }
90
91 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
92                               PCWSTR pszPassword, BOOL generic)
93 {
94     CREDENTIALW cred;
95
96     TRACE("saving servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
97
98     cred.Flags = 0;
99     cred.Type = generic ? CRED_TYPE_GENERIC : CRED_TYPE_DOMAIN_PASSWORD;
100     cred.TargetName = (LPWSTR)pszTargetName;
101     cred.Comment = NULL;
102     cred.CredentialBlobSize = strlenW(pszPassword) * sizeof(WCHAR);
103     cred.CredentialBlob = (LPBYTE)pszPassword;
104     cred.Persist = CRED_PERSIST_ENTERPRISE;
105     cred.AttributeCount = 0;
106     cred.Attributes = NULL;
107     cred.TargetAlias = NULL;
108     cred.UserName = (LPWSTR)pszUsername;
109
110     if (CredWriteW(&cred, 0))
111         return ERROR_SUCCESS;
112     else
113     {
114         DWORD ret = GetLastError();
115         ERR("CredWriteW failed with error %d\n", ret);
116         return ret;
117     }
118 }
119
120 struct cred_dialog_params
121 {
122     PCWSTR pszTargetName;
123     PCWSTR pszMessageText;
124     PCWSTR pszCaptionText;
125     HBITMAP hbmBanner;
126     PWSTR pszUsername;
127     ULONG ulUsernameMaxChars;
128     PWSTR pszPassword;
129     ULONG ulPasswordMaxChars;
130     BOOL fSave;
131     DWORD dwFlags;
132 };
133
134 static void CredDialogFillUsernameCombo(HWND hwndUsername, struct cred_dialog_params *params)
135 {
136     DWORD count;
137     DWORD i;
138     PCREDENTIALW *credentials;
139
140     if (!CredEnumerateW(NULL, 0, &count, &credentials))
141         return;
142
143     for (i = 0; i < count; i++)
144     {
145         COMBOBOXEXITEMW comboitem;
146         DWORD j;
147         BOOL duplicate = FALSE;
148
149         if (params->dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS)
150         {
151             if ((credentials[i]->Type != CRED_TYPE_GENERIC) || !credentials[i]->UserName)
152                 continue;
153         }
154         else
155         {
156             if (credentials[i]->Type == CRED_TYPE_GENERIC)
157                 continue;
158         }
159
160         /* don't add another item with the same name if we've already added it */
161         for (j = 0; j < i; j++)
162             if (!strcmpW(credentials[i]->UserName, credentials[j]->UserName))
163             {
164                 duplicate = TRUE;
165                 break;
166             }
167
168         if (duplicate)
169             continue;
170
171         comboitem.mask = CBEIF_TEXT;
172         comboitem.iItem = -1;
173         comboitem.pszText = credentials[i]->UserName;
174         SendMessageW(hwndUsername, CBEM_INSERTITEMW, 0, (LPARAM)&comboitem);
175     }
176
177     CredFree(credentials);
178 }
179
180 static BOOL CredDialogInit(HWND hwndDlg, struct cred_dialog_params *params)
181 {
182     HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
183     HWND hwndPassword = GetDlgItem(hwndDlg, IDC_PASSWORD);
184
185     SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
186
187     if (params->hbmBanner)
188         SendMessageW(GetDlgItem(hwndDlg, IDB_BANNER), STM_SETIMAGE,
189                      IMAGE_BITMAP, (LPARAM)params->hbmBanner);
190
191     if (params->pszMessageText)
192         SetDlgItemTextW(hwndDlg, IDC_MESSAGE, params->pszMessageText);
193     else
194     {
195         WCHAR format[256];
196         WCHAR message[256];
197         LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, sizeof(format)/sizeof(format[0]));
198         snprintfW(message, sizeof(message)/sizeof(message[0]), format, params->pszTargetName);
199         SetDlgItemTextW(hwndDlg, IDC_MESSAGE, message);
200     }
201     SetWindowTextW(hwndUsername, params->pszUsername);
202     SetWindowTextW(hwndPassword, params->pszPassword);
203
204     CredDialogFillUsernameCombo(hwndUsername, params);
205
206     if (params->pszUsername[0])
207         SetFocus(hwndPassword);
208     else
209         SetFocus(hwndUsername);
210
211     if (params->pszCaptionText)
212         SetWindowTextW(hwndDlg, params->pszCaptionText);
213     else
214     {
215         WCHAR format[256];
216         WCHAR title[256];
217         LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, sizeof(format)/sizeof(format[0]));
218         snprintfW(title, sizeof(title)/sizeof(title[0]), format, params->pszTargetName);
219         SetWindowTextW(hwndDlg, title);
220     }
221
222     if (params->dwFlags & (CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_PERSIST))
223         ShowWindow(GetDlgItem(hwndDlg, IDC_SAVE), SW_HIDE);
224     else if (params->fSave)
225         CheckDlgButton(hwndDlg, IDC_SAVE, BST_CHECKED);
226
227     return FALSE;
228 }
229
230 static void CredDialogCommandOk(HWND hwndDlg, struct cred_dialog_params *params)
231 {
232     HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
233     LPWSTR user;
234     INT len;
235     INT len2;
236
237     len = GetWindowTextLengthW(hwndUsername);
238     user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
239     GetWindowTextW(hwndUsername, user, len + 1);
240
241     if (!user[0])
242     {
243         HeapFree(GetProcessHeap(), 0, user);
244         return;
245     }
246
247     if (!strchrW(user, '\\') && !strchrW(user, '@'))
248     {
249         INT len_target = strlenW(params->pszTargetName);
250         memcpy(params->pszUsername, params->pszTargetName,
251                min(len_target, params->ulUsernameMaxChars) * sizeof(WCHAR));
252         if (len_target + 1 < params->ulUsernameMaxChars)
253             params->pszUsername[len_target] = '\\';
254         if (len_target + 2 < params->ulUsernameMaxChars)
255             params->pszUsername[len_target + 1] = '\0';
256     }
257     else if (params->ulUsernameMaxChars > 0)
258         params->pszUsername[0] = '\0';
259
260     len2 = strlenW(params->pszUsername);
261     memcpy(params->pszUsername + len2, user, min(len, params->ulUsernameMaxChars - len2) * sizeof(WCHAR));
262     if (params->ulUsernameMaxChars)
263         params->pszUsername[len2 + min(len, params->ulUsernameMaxChars - len2 - 1)] = '\0';
264
265     HeapFree(GetProcessHeap(), 0, user);
266
267     GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
268                     params->ulPasswordMaxChars);
269
270     EndDialog(hwndDlg, IDOK);
271 }
272
273 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
274                                        LPARAM lParam)
275 {
276     switch (uMsg)
277     {
278         case WM_INITDIALOG:
279         {
280             struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
281
282             return CredDialogInit(hwndDlg, params);
283         }
284         case WM_COMMAND:
285             switch (wParam)
286             {
287                 case MAKELONG(IDOK, BN_CLICKED):
288                 {
289                     struct cred_dialog_params *params =
290                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
291                     CredDialogCommandOk(hwndDlg, params);
292                     return TRUE;
293                 }
294                 case MAKELONG(IDCANCEL, BN_CLICKED):
295                     EndDialog(hwndDlg, IDCANCEL);
296                     return TRUE;
297                 case MAKELONG(IDC_PASSWORD, EN_SETFOCUS):
298                     /* don't allow another window to steal focus while the
299                      * user is typing their password */
300                     LockSetForegroundWindow(LSFW_LOCK);
301                     return TRUE;
302                 case MAKELONG(IDC_PASSWORD, EN_KILLFOCUS):
303                     /* the user is no longer typing their password, so allow
304                      * other windows to become foreground ones */
305                     LockSetForegroundWindow(LSFW_UNLOCK);
306                     return TRUE;
307             }
308             /* fall through */
309         default:
310             return FALSE;
311     }
312 }
313
314 /******************************************************************************
315  *           CredUIPromptForCredentialsW [CREDUI.@]
316  */
317 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
318                                          PCWSTR pszTargetName,
319                                          PCtxtHandle Reserved,
320                                          DWORD dwAuthError,
321                                          PWSTR pszUsername,
322                                          ULONG ulUsernameMaxChars,
323                                          PWSTR pszPassword,
324                                          ULONG ulPasswordMaxChars, PBOOL pfSave,
325                                          DWORD dwFlags)
326 {
327     INT_PTR ret;
328     struct cred_dialog_params params;
329     DWORD result = ERROR_SUCCESS;
330
331     TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
332           debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
333           ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
334
335     if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
336         return ERROR_INVALID_FLAGS;
337
338     if (!pszTargetName)
339         return ERROR_INVALID_PARAMETER;
340
341     if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
342         return ERROR_INVALID_PARAMETER;
343
344     params.pszTargetName = pszTargetName;
345     if (pUIInfo)
346     {
347         params.pszMessageText = pUIInfo->pszMessageText;
348         params.pszCaptionText = pUIInfo->pszCaptionText;
349         params.hbmBanner  = pUIInfo->hbmBanner;
350     }
351     else
352     {
353         params.pszMessageText = NULL;
354         params.pszCaptionText = NULL;
355         params.hbmBanner = NULL;
356     }
357     params.pszUsername = pszUsername;
358     params.ulUsernameMaxChars = ulUsernameMaxChars;
359     params.pszPassword = pszPassword;
360     params.ulPasswordMaxChars = ulPasswordMaxChars;
361     params.fSave = pfSave ? *pfSave : FALSE;
362     params.dwFlags = dwFlags;
363
364     ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
365                           pUIInfo ? pUIInfo->hwndParent : NULL,
366                           CredDialogProc, (LPARAM)&params);
367     if (ret <= 0)
368         return GetLastError();
369
370     if (ret == IDCANCEL)
371     {
372         TRACE("dialog cancelled\n");
373         return ERROR_CANCELLED;
374     }
375
376     if (pfSave)
377         *pfSave = params.fSave;
378
379     if (params.fSave)
380     {
381         if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
382         {
383             BOOL found = FALSE;
384             struct pending_credentials *entry;
385             int len;
386
387             EnterCriticalSection(&csPendingCredentials);
388
389             /* find existing pending credentials for the same target and overwrite */
390             /* FIXME: is this correct? */
391             LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
392                 if (!strcmpW(pszTargetName, entry->pszTargetName))
393                 {
394                     found = TRUE;
395                     HeapFree(GetProcessHeap(), 0, entry->pszUsername);
396                     ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
397                     HeapFree(GetProcessHeap(), 0, entry->pszPassword);
398                 }
399
400             if (!found)
401             {
402                 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
403                 list_init(&entry->entry);
404                 len = strlenW(pszTargetName);
405                 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
406                 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
407                 list_add_tail(&entry->entry, &pending_credentials_list);
408             }
409
410             len = strlenW(params.pszUsername);
411             entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
412             memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
413             len = strlenW(params.pszPassword);
414             entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
415             memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
416             entry->generic = dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE;
417
418             LeaveCriticalSection(&csPendingCredentials);
419         }
420         else
421             result = save_credentials(pszTargetName, pszUsername, pszPassword,
422                                       dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE);
423     }
424
425     return result;
426 }
427
428 /******************************************************************************
429  *           CredUIConfirmCredentialsW [CREDUI.@]
430  */
431 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
432 {
433     struct pending_credentials *entry;
434     DWORD result = ERROR_NOT_FOUND;
435
436     TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
437
438     if (!pszTargetName)
439         return ERROR_INVALID_PARAMETER;
440
441     EnterCriticalSection(&csPendingCredentials);
442
443     LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
444     {
445         if (!strcmpW(pszTargetName, entry->pszTargetName))
446         {
447             if (bConfirm)
448                 result = save_credentials(entry->pszTargetName, entry->pszUsername,
449                                           entry->pszPassword, entry->generic);
450             else
451                 result = ERROR_SUCCESS;
452
453             list_remove(&entry->entry);
454
455             HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
456             HeapFree(GetProcessHeap(), 0, entry->pszUsername);
457             ZeroMemory(entry->pszPassword, (strlenW(entry->pszPassword) + 1) * sizeof(WCHAR));
458             HeapFree(GetProcessHeap(), 0, entry->pszPassword);
459             HeapFree(GetProcessHeap(), 0, entry);
460
461             break;
462         }
463     }
464
465     LeaveCriticalSection(&csPendingCredentials);
466
467     return result;
468 }
469
470 /******************************************************************************
471  *           CredUIParseUserNameW [CREDUI.@]
472  */
473 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
474                                   ULONG ulMaxUserChars, PWSTR pszDomain,
475                                   ULONG ulMaxDomainChars)
476 {
477     PWSTR p;
478
479     TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
480           ulMaxUserChars, pszDomain, ulMaxDomainChars);
481
482     if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
483         !ulMaxDomainChars)
484         return ERROR_INVALID_PARAMETER;
485
486     /* FIXME: handle marshaled credentials */
487
488     p = strchrW(pszUserName, '\\');
489     if (p)
490     {
491         if (p - pszUserName > ulMaxDomainChars - 1)
492             return ERROR_INSUFFICIENT_BUFFER;
493         if (strlenW(p + 1) > ulMaxUserChars - 1)
494             return ERROR_INSUFFICIENT_BUFFER;
495         strcpyW(pszUser, p + 1);
496         memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
497         pszDomain[p - pszUserName] = '\0';
498
499         return ERROR_SUCCESS;
500     }
501
502     p = strrchrW(pszUserName, '@');
503     if (p)
504     {
505         if (p + 1 - pszUserName > ulMaxUserChars - 1)
506             return ERROR_INSUFFICIENT_BUFFER;
507         if (strlenW(p + 1) > ulMaxDomainChars - 1)
508             return ERROR_INSUFFICIENT_BUFFER;
509         strcpyW(pszDomain, p + 1);
510         memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
511         pszUser[p - pszUserName] = '\0';
512
513         return ERROR_SUCCESS;
514     }
515
516     if (strlenW(pszUserName) > ulMaxUserChars - 1)
517         return ERROR_INSUFFICIENT_BUFFER;
518     strcpyW(pszUser, pszUserName);
519     pszDomain[0] = '\0';
520
521     return ERROR_SUCCESS;
522 }
523
524 /******************************************************************************
525  *           CredUIStoreSSOCredA [CREDUI.@]
526  */
527 DWORD WINAPI CredUIStoreSSOCredA(PCSTR pszRealm, PCSTR pszUsername,
528                                  PCSTR pszPassword, BOOL bPersist)
529 {
530     FIXME("(%s, %s, %p, %d)\n", debugstr_a(pszRealm), debugstr_a(pszUsername),
531           pszPassword, bPersist);
532     return ERROR_SUCCESS;
533 }
534
535 /******************************************************************************
536  *           CredUIStoreSSOCredW [CREDUI.@]
537  */
538 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR pszRealm, PCWSTR pszUsername,
539                                  PCWSTR pszPassword, BOOL bPersist)
540 {
541     FIXME("(%s, %s, %p, %d)\n", debugstr_w(pszRealm), debugstr_w(pszUsername),
542           pszPassword, bPersist);
543     return ERROR_SUCCESS;
544 }
545
546 /******************************************************************************
547  *           CredUIReadSSOCredA [CREDUI.@]
548  */
549 DWORD WINAPI CredUIReadSSOCredA(PCSTR pszRealm, PSTR *ppszUsername)
550 {
551     FIXME("(%s, %p)\n", debugstr_a(pszRealm), ppszUsername);
552     if (ppszUsername)
553         *ppszUsername = NULL;
554     return ERROR_NOT_FOUND;
555 }
556
557 /******************************************************************************
558  *           CredUIReadSSOCredW [CREDUI.@]
559  */
560 DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
561 {
562     FIXME("(%s, %p)\n", debugstr_w(pszRealm), ppszUsername);
563     if (ppszUsername)
564         *ppszUsername = NULL;
565     return ERROR_NOT_FOUND;
566 }