2 * Credentials User Interface
4 * Copyright 2006 Robert Shearman (for CodeWeavers)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "credui_resources.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "wine/list.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(credui);
37 struct pending_credentials
45 static HINSTANCE hinstCredUI;
47 struct list pending_credentials_list = LIST_INIT(pending_credentials_list);
49 static CRITICAL_SECTION csPendingCredentials;
50 static CRITICAL_SECTION_DEBUG critsect_debug =
52 0, 0, &csPendingCredentials,
53 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": csPendingCredentials") }
56 static CRITICAL_SECTION csPendingCredentials = { &critsect_debug, -1, 0, 0, 0, 0 };
59 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
61 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
63 if (fdwReason == DLL_WINE_PREATTACH) return FALSE; /* prefer native version */
65 if (fdwReason == DLL_PROCESS_ATTACH)
67 DisableThreadLibraryCalls(hinstDLL);
68 hinstCredUI = hinstDLL;
70 else if (fdwReason == DLL_PROCESS_DETACH)
72 struct pending_credentials *entry, *cursor2;
73 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
75 list_remove(&entry->entry);
77 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
78 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
79 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
80 HeapFree(GetProcessHeap(), 0, entry);
87 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
90 FIXME("save servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
94 struct cred_dialog_params
97 PCWSTR pszMessageText;
98 PCWSTR pszCaptionText;
101 ULONG ulUsernameMaxChars;
103 ULONG ulPasswordMaxChars;
107 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
114 struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
119 SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
120 ret = CredUIParseUserNameW(params->pszUsername, user, 256, domain, 256);
121 if (ret == ERROR_SUCCESS)
123 SetDlgItemTextW(hwndDlg, IDC_USERNAME, user);
124 SetDlgItemTextW(hwndDlg, IDC_DOMAIN, domain);
126 SetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword);
128 if (ret == ERROR_SUCCESS && user[0])
129 SetFocus(GetDlgItem(hwndDlg, IDC_PASSWORD));
131 SetFocus(GetDlgItem(hwndDlg, IDC_USERNAME));
133 if (params->pszCaptionText)
134 SetWindowTextW(hwndDlg, params->pszCaptionText);
140 case MAKELONG(IDOK, BN_CLICKED):
143 struct cred_dialog_params *params =
144 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
146 domainlen = GetDlgItemTextW(hwndDlg, IDC_DOMAIN,
148 params->ulUsernameMaxChars);
149 if (domainlen && (domainlen < params->ulUsernameMaxChars))
151 params->pszUsername[domainlen++] = '\\';
152 params->pszUsername[domainlen] = '\0';
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);
161 EndDialog(hwndDlg, IDOK);
164 case MAKELONG(IDCANCEL, BN_CLICKED):
165 EndDialog(hwndDlg, IDCANCEL);
174 /******************************************************************************
175 * CredUIPromptForCredentialsW [CREDUI.@]
177 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
178 PCWSTR pszTargetName,
179 PCtxtHandle Reserved,
182 ULONG ulUsernameMaxChars,
184 ULONG ulPasswordMaxChars, PBOOL pfSave,
188 struct cred_dialog_params params;
189 DWORD result = ERROR_SUCCESS;
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);
195 if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
196 return ERROR_INVALID_FLAGS;
199 return ERROR_INVALID_PARAMETER;
201 if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
202 return ERROR_INVALID_PARAMETER;
204 params.pszTargetName = pszTargetName;
207 params.pszMessageText = pUIInfo->pszMessageText;
208 params.pszCaptionText = pUIInfo->pszCaptionText;
209 params.hbmBanner = pUIInfo->hbmBanner;
213 params.pszMessageText = NULL;
214 params.pszCaptionText = NULL;
215 params.hbmBanner = NULL;
217 params.pszUsername = pszUsername;
218 params.ulUsernameMaxChars = ulUsernameMaxChars;
219 params.pszPassword = pszPassword;
220 params.ulPasswordMaxChars = ulPasswordMaxChars;
221 params.fSave = pfSave ? *pfSave : FALSE;
223 ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
224 pUIInfo ? pUIInfo->hwndParent : NULL,
225 CredDialogProc, (LPARAM)¶ms);
227 return GetLastError();
231 TRACE("dialog cancelled\n");
232 return ERROR_CANCELLED;
236 *pfSave = params.fSave;
240 if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
243 struct pending_credentials *entry;
246 EnterCriticalSection(&csPendingCredentials);
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))
254 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
255 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
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);
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));
275 LeaveCriticalSection(&csPendingCredentials);
278 result = save_credentials(pszTargetName, pszUsername, pszPassword);
284 /******************************************************************************
285 * CredUIConfirmCredentialsW [CREDUI.@]
287 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
289 struct pending_credentials *entry;
290 DWORD result = ERROR_NOT_FOUND;
292 TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
295 return ERROR_INVALID_PARAMETER;
297 EnterCriticalSection(&csPendingCredentials);
299 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
301 if (!strcmpW(pszTargetName, entry->pszTargetName))
304 result = save_credentials(entry->pszTargetName, entry->pszUsername, entry->pszPassword);
306 result = ERROR_SUCCESS;
308 list_remove(&entry->entry);
310 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
311 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
312 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
313 HeapFree(GetProcessHeap(), 0, entry);
319 LeaveCriticalSection(&csPendingCredentials);
324 /******************************************************************************
325 * CredUIParseUserNameW [CREDUI.@]
327 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
328 ULONG ulMaxUserChars, PWSTR pszDomain,
329 ULONG ulMaxDomainChars)
333 TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
334 ulMaxUserChars, pszDomain, ulMaxDomainChars);
336 if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
338 return ERROR_INVALID_PARAMETER;
340 /* FIXME: handle marshaled credentials */
342 p = strchrW(pszUserName, '\\');
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';
353 return ERROR_SUCCESS;
356 p = strrchrW(pszUserName, '@');
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';
367 return ERROR_SUCCESS;
370 if (strlenW(pszUserName) > ulMaxUserChars - 1)
371 return ERROR_INSUFFICIENT_BUFFER;
372 strcpyW(pszUser, pszUserName);
375 return ERROR_SUCCESS;
378 /******************************************************************************
379 * CredUIStoreSSOCredA [CREDUI.@]
381 DWORD WINAPI CredUIStoreSSOCredA(PCSTR a, PCSTR b, PCSTR c, BOOL f)
383 FIXME("(%s, %s, %s, %d)\n", debugstr_a(a), debugstr_a(b), debugstr_a(c), f);
387 /******************************************************************************
388 * CredUIStoreSSOCredW [CREDUI.@]
390 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR a, PCWSTR b, PCWSTR c, BOOL f)
392 FIXME("(%s, %s, %s, %d)\n", debugstr_w(a), debugstr_w(b), debugstr_w(c), f);
396 /******************************************************************************
397 * CredUIReadSSOCredA [CREDUI.@]
399 DWORD WINAPI CredUIReadSSOCredA(PCSTR a, PSTR *b)
401 FIXME("(%s, %p)\n", debugstr_a(a), b);
407 /******************************************************************************
408 * CredUIReadSSOCredW [CREDUI.@]
410 DWORD WINAPI CredUIReadSSOCredW(PCWSTR a, PWSTR *b)
412 FIXME("(%s, %p)\n", debugstr_w(a), b);