credui: Verify some of the parameters to CredUIPromptForCredentials, like native...
[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
29 #include "credui_resources.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "wine/list.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(credui);
36
37 struct pending_credentials
38 {
39     struct list entry;
40     PWSTR pszTargetName;
41     PWSTR pszUsername;
42     PWSTR pszPassword;
43 };
44
45 static HINSTANCE hinstCredUI;
46
47 struct list pending_credentials_list = LIST_INIT(pending_credentials_list);
48
49 static CRITICAL_SECTION csPendingCredentials;
50 static CRITICAL_SECTION_DEBUG critsect_debug =
51 {
52     0, 0, &csPendingCredentials,
53     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
54     0, 0, { (DWORD_PTR)(__FILE__ ": csPendingCredentials") }
55 };
56 static CRITICAL_SECTION csPendingCredentials = { &critsect_debug, -1, 0, 0, 0, 0 };
57
58
59 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
60 {
61     TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
62
63     if (fdwReason == DLL_WINE_PREATTACH) return FALSE;  /* prefer native version */
64
65     if (fdwReason == DLL_PROCESS_ATTACH)
66     {
67         DisableThreadLibraryCalls(hinstDLL);
68         hinstCredUI = hinstDLL;
69     }
70     else if (fdwReason == DLL_PROCESS_DETACH)
71     {
72         struct pending_credentials *entry, *cursor2;
73         LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
74         {
75             list_remove(&entry->entry);
76
77             HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
78             HeapFree(GetProcessHeap(), 0, entry->pszUsername);
79             HeapFree(GetProcessHeap(), 0, entry->pszPassword);
80             HeapFree(GetProcessHeap(), 0, entry);
81         }
82     }
83
84     return TRUE;
85 }
86
87 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
88                               PCWSTR pszPassword)
89 {
90     FIXME("save servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
91     return ERROR_SUCCESS;
92 }
93
94 struct cred_dialog_params
95 {
96     PCWSTR pszTargetName;
97     PCWSTR pszMessageText;
98     PCWSTR pszCaptionText;
99     HBITMAP hbmBanner;
100     PWSTR pszUsername;
101     ULONG ulUsernameMaxChars;
102     PWSTR pszPassword;
103     ULONG ulPasswordMaxChars;
104     BOOL fSave;
105 };
106
107 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
108                                        LPARAM lParam)
109 {
110     switch (uMsg)
111     {
112         case WM_INITDIALOG:
113         {
114             struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
115             DWORD ret;
116             WCHAR user[256];
117             WCHAR domain[256];
118
119             SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
120             ret = CredUIParseUserNameW(params->pszUsername, user, 256, domain, 256);
121             if (ret == ERROR_SUCCESS)
122             {
123                 SetDlgItemTextW(hwndDlg, IDC_USERNAME, user);
124                 SetDlgItemTextW(hwndDlg, IDC_DOMAIN, domain);
125             }
126             SetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword);
127
128             if (ret == ERROR_SUCCESS && user[0])
129                 SetFocus(GetDlgItem(hwndDlg, IDC_PASSWORD));
130             else
131                 SetFocus(GetDlgItem(hwndDlg, IDC_USERNAME));
132
133             if (params->pszCaptionText)
134                 SetWindowTextW(hwndDlg, params->pszCaptionText);
135             return FALSE;
136         }
137         case WM_COMMAND:
138             switch (wParam)
139             {
140                 case MAKELONG(IDOK, BN_CLICKED):
141                 {
142                     ULONG domainlen;
143                     struct cred_dialog_params *params =
144                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
145
146                     domainlen = GetDlgItemTextW(hwndDlg, IDC_DOMAIN,
147                                                 params->pszUsername,
148                                                 params->ulUsernameMaxChars);
149                     if (domainlen && (domainlen < params->ulUsernameMaxChars))
150                     {
151                         params->pszUsername[domainlen++] = '\\';
152                         params->pszUsername[domainlen] = '\0';
153                     }
154                     if (domainlen < params->ulUsernameMaxChars)
155                         GetDlgItemTextW(hwndDlg, IDC_USERNAME,
156                                         params->pszUsername + domainlen,
157                                         params->ulUsernameMaxChars - domainlen);
158                     GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
159                                     params->ulPasswordMaxChars);
160
161                     EndDialog(hwndDlg, IDOK);
162                     return TRUE;
163                 }
164                 case MAKELONG(IDCANCEL, BN_CLICKED):
165                     EndDialog(hwndDlg, IDCANCEL);
166                     return TRUE;
167             }
168             /* fall through */
169         default:
170             return FALSE;
171     }
172 }
173
174 /******************************************************************************
175  *           CredUIPromptForCredentialsW [CREDUI.@]
176  */
177 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
178                                          PCWSTR pszTargetName,
179                                          PCtxtHandle Reserved,
180                                          DWORD dwAuthError,
181                                          PWSTR pszUsername,
182                                          ULONG ulUsernameMaxChars,
183                                          PWSTR pszPassword,
184                                          ULONG ulPasswordMaxChars, PBOOL pfSave,
185                                          DWORD dwFlags)
186 {
187     INT_PTR ret;
188     struct cred_dialog_params params;
189     DWORD result = ERROR_SUCCESS;
190
191     TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
192           debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
193           ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
194
195     if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
196         return ERROR_INVALID_FLAGS;
197
198     if (!pszTargetName)
199         return ERROR_INVALID_PARAMETER;
200
201     if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
202         return ERROR_INVALID_PARAMETER;
203
204     params.pszTargetName = pszTargetName;
205     if (pUIInfo)
206     {
207         params.pszMessageText = pUIInfo->pszMessageText;
208         params.pszCaptionText = pUIInfo->pszCaptionText;
209         params.hbmBanner  = pUIInfo->hbmBanner;
210     }
211     else
212     {
213         params.pszMessageText = NULL;
214         params.pszCaptionText = NULL;
215         params.hbmBanner = NULL;
216     }
217     params.pszUsername = pszUsername;
218     params.ulUsernameMaxChars = ulUsernameMaxChars;
219     params.pszPassword = pszPassword;
220     params.ulPasswordMaxChars = ulPasswordMaxChars;
221     params.fSave = pfSave ? *pfSave : FALSE;
222
223     ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
224                           pUIInfo ? pUIInfo->hwndParent : NULL,
225                           CredDialogProc, (LPARAM)&params);
226     if (ret <= 0)
227         return GetLastError();
228
229     if (ret == IDCANCEL)
230     {
231         TRACE("dialog cancelled\n");
232         return ERROR_CANCELLED;
233     }
234
235     if (pfSave)
236         *pfSave = params.fSave;
237
238     if (params.fSave)
239     {
240         if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
241         {
242             BOOL found = FALSE;
243             struct pending_credentials *entry;
244             int len;
245
246             EnterCriticalSection(&csPendingCredentials);
247
248             /* find existing pending credentials for the same target and overwrite */
249             /* FIXME: is this correct? */
250             LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
251                 if (!strcmpW(pszTargetName, entry->pszTargetName))
252                 {
253                     found = TRUE;
254                     HeapFree(GetProcessHeap(), 0, entry->pszUsername);
255                     HeapFree(GetProcessHeap(), 0, entry->pszPassword);
256                 }
257
258             if (!found)
259             {
260                 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
261                 list_init(&entry->entry);
262                 len = strlenW(pszTargetName);
263                 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
264                 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
265                 list_add_tail(&entry->entry, &pending_credentials_list);
266             }
267
268             len = strlenW(params.pszUsername);
269             entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
270             memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
271             len = strlenW(params.pszPassword);
272             entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
273             memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
274
275             LeaveCriticalSection(&csPendingCredentials);
276         }
277         else
278             result = save_credentials(pszTargetName, pszUsername, pszPassword);
279     }
280
281     return result;
282 }
283
284 /******************************************************************************
285  *           CredUIConfirmCredentialsW [CREDUI.@]
286  */
287 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
288 {
289     struct pending_credentials *entry;
290     DWORD result = ERROR_NOT_FOUND;
291
292     TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
293
294     if (!pszTargetName)
295         return ERROR_INVALID_PARAMETER;
296
297     EnterCriticalSection(&csPendingCredentials);
298
299     LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
300     {
301         if (!strcmpW(pszTargetName, entry->pszTargetName))
302         {
303             if (bConfirm)
304                 result = save_credentials(entry->pszTargetName, entry->pszUsername, entry->pszPassword);
305             else
306                 result = ERROR_SUCCESS;
307
308             list_remove(&entry->entry);
309
310             HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
311             HeapFree(GetProcessHeap(), 0, entry->pszUsername);
312             HeapFree(GetProcessHeap(), 0, entry->pszPassword);
313             HeapFree(GetProcessHeap(), 0, entry);
314
315             break;
316         }
317     }
318
319     LeaveCriticalSection(&csPendingCredentials);
320
321     return result;
322 }
323
324 /******************************************************************************
325  *           CredUIParseUserNameW [CREDUI.@]
326  */
327 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
328                                   ULONG ulMaxUserChars, PWSTR pszDomain,
329                                   ULONG ulMaxDomainChars)
330 {
331     PWSTR p;
332
333     TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
334           ulMaxUserChars, pszDomain, ulMaxDomainChars);
335
336     if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
337         !ulMaxDomainChars)
338         return ERROR_INVALID_PARAMETER;
339
340     /* FIXME: handle marshaled credentials */
341
342     p = strchrW(pszUserName, '\\');
343     if (p)
344     {
345         if (p - pszUserName > ulMaxDomainChars - 1)
346             return ERROR_INSUFFICIENT_BUFFER;
347         if (strlenW(p + 1) > ulMaxUserChars - 1)
348             return ERROR_INSUFFICIENT_BUFFER;
349         strcpyW(pszUser, p + 1);
350         memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
351         pszDomain[p - pszUserName] = '\0';
352
353         return ERROR_SUCCESS;
354     }
355
356     p = strrchrW(pszUserName, '@');
357     if (p)
358     {
359         if (p + 1 - pszUserName > ulMaxUserChars - 1)
360             return ERROR_INSUFFICIENT_BUFFER;
361         if (strlenW(p + 1) > ulMaxDomainChars - 1)
362             return ERROR_INSUFFICIENT_BUFFER;
363         strcpyW(pszDomain, p + 1);
364         memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
365         pszUser[p - pszUserName] = '\0';
366
367         return ERROR_SUCCESS;
368     }
369
370     if (strlenW(pszUserName) > ulMaxUserChars - 1)
371         return ERROR_INSUFFICIENT_BUFFER;
372     strcpyW(pszUser, pszUserName);
373     pszDomain[0] = '\0';
374
375     return ERROR_SUCCESS;
376 }
377
378 /******************************************************************************
379  *           CredUIStoreSSOCredA [CREDUI.@]
380  */
381 DWORD WINAPI CredUIStoreSSOCredA(PCSTR a, PCSTR b, PCSTR c, BOOL f)
382 {
383     FIXME("(%s, %s, %s, %d)\n", debugstr_a(a), debugstr_a(b), debugstr_a(c), f);
384     return 0;
385 }
386
387 /******************************************************************************
388  *           CredUIStoreSSOCredW [CREDUI.@]
389  */
390 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR a, PCWSTR b, PCWSTR c, BOOL f)
391 {
392     FIXME("(%s, %s, %s, %d)\n", debugstr_w(a), debugstr_w(b), debugstr_w(c), f);
393     return 0;
394 }
395
396 /******************************************************************************
397  *           CredUIReadSSOCredA [CREDUI.@]
398  */
399 DWORD WINAPI CredUIReadSSOCredA(PCSTR a, PSTR *b)
400 {
401     FIXME("(%s, %p)\n", debugstr_a(a), b);
402     if (b)
403         *b = NULL;
404     return 0;
405 }
406
407 /******************************************************************************
408  *           CredUIReadSSOCredW [CREDUI.@]
409  */
410 DWORD WINAPI CredUIReadSSOCredW(PCWSTR a, PWSTR *b)
411 {
412     FIXME("(%s, %p)\n", debugstr_w(a), b);
413     if (b)
414         *b = NULL;
415     return 0;
416 }