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
30 #include "credui_resources.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "wine/list.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(credui);
38 struct pending_credentials
46 static HINSTANCE hinstCredUI;
48 static struct list pending_credentials_list = LIST_INIT(pending_credentials_list);
50 static CRITICAL_SECTION csPendingCredentials;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &csPendingCredentials,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": csPendingCredentials") }
57 static CRITICAL_SECTION csPendingCredentials = { &critsect_debug, -1, 0, 0, 0, 0 };
60 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
62 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
64 if (fdwReason == DLL_WINE_PREATTACH) return FALSE; /* prefer native version */
66 if (fdwReason == DLL_PROCESS_ATTACH)
68 DisableThreadLibraryCalls(hinstDLL);
69 hinstCredUI = hinstDLL;
72 else if (fdwReason == DLL_PROCESS_DETACH)
74 struct pending_credentials *entry, *cursor2;
75 LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
77 list_remove(&entry->entry);
79 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
80 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
81 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
82 HeapFree(GetProcessHeap(), 0, entry);
89 static DWORD save_credentials(PCWSTR pszTargetName, PCWSTR pszUsername,
92 FIXME("save servername %s with username %s\n", debugstr_w(pszTargetName), debugstr_w(pszUsername));
96 struct cred_dialog_params
99 PCWSTR pszMessageText;
100 PCWSTR pszCaptionText;
103 ULONG ulUsernameMaxChars;
105 ULONG ulPasswordMaxChars;
110 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
117 struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
119 SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
120 if (params->pszMessageText)
121 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, params->pszMessageText);
126 LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, sizeof(format)/sizeof(format[0]));
127 snprintfW(message, sizeof(message)/sizeof(message[0]), format, params->pszTargetName);
128 SetDlgItemTextW(hwndDlg, IDC_MESSAGE, message);
130 SetDlgItemTextW(hwndDlg, IDC_USERNAME, params->pszUsername);
131 SetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword);
133 if (params->pszUsername[0])
134 SetFocus(GetDlgItem(hwndDlg, IDC_PASSWORD));
136 SetFocus(GetDlgItem(hwndDlg, IDC_USERNAME));
138 if (params->pszCaptionText)
139 SetWindowTextW(hwndDlg, params->pszCaptionText);
144 LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, sizeof(format)/sizeof(format[0]));
145 snprintfW(title, sizeof(title)/sizeof(title[0]), format, params->pszTargetName);
146 SetWindowTextW(hwndDlg, title);
149 if (params->dwFlags & (CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_PERSIST))
150 ShowWindow(GetDlgItem(hwndDlg, IDC_SAVE), SW_HIDE);
151 else if (params->fSave)
152 CheckDlgButton(hwndDlg, IDC_SAVE, BST_CHECKED);
159 case MAKELONG(IDOK, BN_CLICKED):
161 struct cred_dialog_params *params =
162 (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
163 HWND hwndUsername = GetDlgItem(hwndDlg, IDC_USERNAME);
168 len = GetWindowTextLengthW(hwndUsername);
169 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
170 GetWindowTextW(hwndUsername, user, len + 1);
174 HeapFree(GetProcessHeap(), 0, user);
178 if (!strchrW(user, '\\') && !strchrW(user, '@'))
180 INT len_target = strlenW(params->pszTargetName);
181 memcpy(params->pszUsername, params->pszTargetName,
182 min(len_target, params->ulUsernameMaxChars) * sizeof(WCHAR));
183 if (len_target + 1 < params->ulUsernameMaxChars)
184 params->pszUsername[len_target] = '\\';
185 if (len_target + 2 < params->ulUsernameMaxChars)
186 params->pszUsername[len_target + 1] = '\0';
188 else if (params->ulUsernameMaxChars > 0)
189 params->pszUsername[0] = '\0';
191 len2 = strlenW(params->pszUsername);
192 memcpy(params->pszUsername + len2, user, min(len, params->ulUsernameMaxChars - len2) * sizeof(WCHAR));
193 if (params->ulUsernameMaxChars)
194 params->pszUsername[len2 + min(len, params->ulUsernameMaxChars - len2 - 1)] = '\0';
196 HeapFree(GetProcessHeap(), 0, user);
198 GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
199 params->ulPasswordMaxChars);
201 EndDialog(hwndDlg, IDOK);
204 case MAKELONG(IDCANCEL, BN_CLICKED):
205 EndDialog(hwndDlg, IDCANCEL);
214 /******************************************************************************
215 * CredUIPromptForCredentialsW [CREDUI.@]
217 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
218 PCWSTR pszTargetName,
219 PCtxtHandle Reserved,
222 ULONG ulUsernameMaxChars,
224 ULONG ulPasswordMaxChars, PBOOL pfSave,
228 struct cred_dialog_params params;
229 DWORD result = ERROR_SUCCESS;
231 TRACE("(%p, %s, %p, %d, %s, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
232 debugstr_w(pszTargetName), Reserved, dwAuthError, debugstr_w(pszUsername),
233 ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
235 if ((dwFlags & (CREDUI_FLAGS_ALWAYS_SHOW_UI|CREDUI_FLAGS_GENERIC_CREDENTIALS)) == CREDUI_FLAGS_ALWAYS_SHOW_UI)
236 return ERROR_INVALID_FLAGS;
239 return ERROR_INVALID_PARAMETER;
241 if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
242 return ERROR_INVALID_PARAMETER;
244 params.pszTargetName = pszTargetName;
247 params.pszMessageText = pUIInfo->pszMessageText;
248 params.pszCaptionText = pUIInfo->pszCaptionText;
249 params.hbmBanner = pUIInfo->hbmBanner;
253 params.pszMessageText = NULL;
254 params.pszCaptionText = NULL;
255 params.hbmBanner = NULL;
257 params.pszUsername = pszUsername;
258 params.ulUsernameMaxChars = ulUsernameMaxChars;
259 params.pszPassword = pszPassword;
260 params.ulPasswordMaxChars = ulPasswordMaxChars;
261 params.fSave = pfSave ? *pfSave : FALSE;
262 params.dwFlags = dwFlags;
264 ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
265 pUIInfo ? pUIInfo->hwndParent : NULL,
266 CredDialogProc, (LPARAM)¶ms);
268 return GetLastError();
272 TRACE("dialog cancelled\n");
273 return ERROR_CANCELLED;
277 *pfSave = params.fSave;
281 if (dwFlags & CREDUI_FLAGS_EXPECT_CONFIRMATION)
284 struct pending_credentials *entry;
287 EnterCriticalSection(&csPendingCredentials);
289 /* find existing pending credentials for the same target and overwrite */
290 /* FIXME: is this correct? */
291 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
292 if (!strcmpW(pszTargetName, entry->pszTargetName))
295 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
296 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
301 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
302 list_init(&entry->entry);
303 len = strlenW(pszTargetName);
304 entry->pszTargetName = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
305 memcpy(entry->pszTargetName, pszTargetName, (len + 1)*sizeof(WCHAR));
306 list_add_tail(&entry->entry, &pending_credentials_list);
309 len = strlenW(params.pszUsername);
310 entry->pszUsername = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
311 memcpy(entry->pszUsername, params.pszUsername, (len + 1)*sizeof(WCHAR));
312 len = strlenW(params.pszPassword);
313 entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
314 memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
316 LeaveCriticalSection(&csPendingCredentials);
319 result = save_credentials(pszTargetName, pszUsername, pszPassword);
325 /******************************************************************************
326 * CredUIConfirmCredentialsW [CREDUI.@]
328 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
330 struct pending_credentials *entry;
331 DWORD result = ERROR_NOT_FOUND;
333 TRACE("(%s, %s)\n", debugstr_w(pszTargetName), bConfirm ? "TRUE" : "FALSE");
336 return ERROR_INVALID_PARAMETER;
338 EnterCriticalSection(&csPendingCredentials);
340 LIST_FOR_EACH_ENTRY(entry, &pending_credentials_list, struct pending_credentials, entry)
342 if (!strcmpW(pszTargetName, entry->pszTargetName))
345 result = save_credentials(entry->pszTargetName, entry->pszUsername, entry->pszPassword);
347 result = ERROR_SUCCESS;
349 list_remove(&entry->entry);
351 HeapFree(GetProcessHeap(), 0, entry->pszTargetName);
352 HeapFree(GetProcessHeap(), 0, entry->pszUsername);
353 HeapFree(GetProcessHeap(), 0, entry->pszPassword);
354 HeapFree(GetProcessHeap(), 0, entry);
360 LeaveCriticalSection(&csPendingCredentials);
365 /******************************************************************************
366 * CredUIParseUserNameW [CREDUI.@]
368 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
369 ULONG ulMaxUserChars, PWSTR pszDomain,
370 ULONG ulMaxDomainChars)
374 TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
375 ulMaxUserChars, pszDomain, ulMaxDomainChars);
377 if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
379 return ERROR_INVALID_PARAMETER;
381 /* FIXME: handle marshaled credentials */
383 p = strchrW(pszUserName, '\\');
386 if (p - pszUserName > ulMaxDomainChars - 1)
387 return ERROR_INSUFFICIENT_BUFFER;
388 if (strlenW(p + 1) > ulMaxUserChars - 1)
389 return ERROR_INSUFFICIENT_BUFFER;
390 strcpyW(pszUser, p + 1);
391 memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
392 pszDomain[p - pszUserName] = '\0';
394 return ERROR_SUCCESS;
397 p = strrchrW(pszUserName, '@');
400 if (p + 1 - pszUserName > ulMaxUserChars - 1)
401 return ERROR_INSUFFICIENT_BUFFER;
402 if (strlenW(p + 1) > ulMaxDomainChars - 1)
403 return ERROR_INSUFFICIENT_BUFFER;
404 strcpyW(pszDomain, p + 1);
405 memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
406 pszUser[p - pszUserName] = '\0';
408 return ERROR_SUCCESS;
411 if (strlenW(pszUserName) > ulMaxUserChars - 1)
412 return ERROR_INSUFFICIENT_BUFFER;
413 strcpyW(pszUser, pszUserName);
416 return ERROR_SUCCESS;
419 /******************************************************************************
420 * CredUIStoreSSOCredA [CREDUI.@]
422 DWORD WINAPI CredUIStoreSSOCredA(PCSTR pszRealm, PCSTR pszUsername,
423 PCSTR pszPassword, BOOL bPersist)
425 FIXME("(%s, %s, %p, %d)\n", debugstr_a(pszRealm), debugstr_a(pszUsername),
426 pszPassword, bPersist);
427 return ERROR_SUCCESS;
430 /******************************************************************************
431 * CredUIStoreSSOCredW [CREDUI.@]
433 DWORD WINAPI CredUIStoreSSOCredW(PCWSTR pszRealm, PCWSTR pszUsername,
434 PCWSTR pszPassword, BOOL bPersist)
436 FIXME("(%s, %s, %p, %d)\n", debugstr_w(pszRealm), debugstr_w(pszUsername),
437 pszPassword, bPersist);
438 return ERROR_SUCCESS;
441 /******************************************************************************
442 * CredUIReadSSOCredA [CREDUI.@]
444 DWORD WINAPI CredUIReadSSOCredA(PCSTR pszRealm, PSTR *ppszUsername)
446 FIXME("(%s, %p)\n", debugstr_a(pszRealm), ppszUsername);
448 *ppszUsername = NULL;
449 return ERROR_NOT_FOUND;
452 /******************************************************************************
453 * CredUIReadSSOCredW [CREDUI.@]
455 DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
457 FIXME("(%s, %p)\n", debugstr_w(pszRealm), ppszUsername);
459 *ppszUsername = NULL;
460 return ERROR_NOT_FOUND;