advapi32/tests: Reopen the main handle if needed.
[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
34 WINE_DEFAULT_DEBUG_CHANNEL(credui);
35
36 static HINSTANCE hinstCredUI;
37
38 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
39 {
40         TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
41
42         if (fdwReason == DLL_WINE_PREATTACH) return FALSE;      /* prefer native version */
43
44         if (fdwReason == DLL_PROCESS_ATTACH)
45         {
46                 DisableThreadLibraryCalls(hinstDLL);
47                 hinstCredUI = hinstDLL;
48         }
49
50         return TRUE;
51 }
52
53 struct cred_dialog_params
54 {
55     PCWSTR pszTargetName;
56     PCWSTR pszMessageText;
57     PCWSTR pszCaptionText;
58     HBITMAP hbmBanner;
59     PWSTR pszUsername;
60     ULONG ulUsernameMaxChars;
61     PWSTR pszPassword;
62     ULONG ulPasswordMaxChars;
63     BOOL fSave;
64 };
65
66 static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
67                                        LPARAM lParam)
68 {
69     switch (uMsg)
70     {
71         case WM_INITDIALOG:
72         {
73             struct cred_dialog_params *params = (struct cred_dialog_params *)lParam;
74             DWORD ret;
75             WCHAR user[256];
76             WCHAR domain[256];
77
78             SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)params);
79             ret = CredUIParseUserNameW(params->pszUsername, user, 256, domain, 256);
80             if (ret == ERROR_SUCCESS)
81             {
82                 SetDlgItemTextW(hwndDlg, IDC_USERNAME, user);
83                 SetDlgItemTextW(hwndDlg, IDC_DOMAIN, domain);
84             }
85             SetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword);
86
87             if (ret == ERROR_SUCCESS && user[0])
88                 SetFocus(GetDlgItem(hwndDlg, IDC_PASSWORD));
89             else
90                 SetFocus(GetDlgItem(hwndDlg, IDC_USERNAME));
91
92             if (params->pszCaptionText)
93                 SetWindowTextW(hwndDlg, params->pszCaptionText);
94             return FALSE;
95         }
96         case WM_COMMAND:
97             switch (wParam)
98             {
99                 case MAKELONG(IDOK, BN_CLICKED):
100                 {
101                     ULONG domainlen;
102                     struct cred_dialog_params *params =
103                         (struct cred_dialog_params *)GetWindowLongPtrW(hwndDlg, DWLP_USER);
104
105                     domainlen = GetDlgItemTextW(hwndDlg, IDC_DOMAIN,
106                                                 params->pszUsername,
107                                                 params->ulUsernameMaxChars);
108                     if (domainlen && (domainlen < params->ulUsernameMaxChars))
109                     {
110                         params->pszUsername[domainlen++] = '\\';
111                         params->pszUsername[domainlen] = '\0';
112                     }
113                     if (domainlen < params->ulUsernameMaxChars)
114                         GetDlgItemTextW(hwndDlg, IDC_USERNAME,
115                                         params->pszUsername + domainlen,
116                                         params->ulUsernameMaxChars - domainlen);
117                     GetDlgItemTextW(hwndDlg, IDC_PASSWORD, params->pszPassword,
118                                     params->ulPasswordMaxChars);
119
120                     EndDialog(hwndDlg, IDOK);
121                     return TRUE;
122                 }
123                 case MAKELONG(IDCANCEL, BN_CLICKED):
124                     EndDialog(hwndDlg, IDCANCEL);
125                     return TRUE;
126             }
127             /* fall through */
128         default:
129             return FALSE;
130     }
131 }
132
133 /******************************************************************************
134  *           CredUIPromptForCredentialsW [CREDUI.@]
135  */
136 DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
137                                          PCWSTR pszTargetName,
138                                          PCtxtHandle Reserved,
139                                          DWORD dwAuthError,
140                                          PWSTR pszUsername,
141                                          ULONG ulUsernameMaxChars,
142                                          PWSTR pszPassword,
143                                          ULONG ulPasswordMaxChars, PBOOL pfSave,
144                                          DWORD dwFlags)
145 {
146     INT_PTR ret;
147     struct cred_dialog_params params;
148
149     TRACE("(%p, %s, %p, %d, %p, %d, %p, %d, %p, 0x%08x)\n", pUIInfo,
150           debugstr_w(pszTargetName), Reserved, dwAuthError, pszUsername,
151           ulUsernameMaxChars, pszPassword, ulPasswordMaxChars, pfSave, dwFlags);
152
153     params.pszTargetName = pszTargetName;
154     if (pUIInfo)
155     {
156         params.pszMessageText = pUIInfo->pszMessageText;
157         params.pszCaptionText = pUIInfo->pszCaptionText;
158         params.hbmBanner  = pUIInfo->hbmBanner;
159     }
160     else
161     {
162         params.pszMessageText = NULL;
163         params.pszCaptionText = NULL;
164         params.hbmBanner = NULL;
165     }
166     params.pszUsername = pszUsername;
167     params.ulUsernameMaxChars = ulUsernameMaxChars;
168     params.pszPassword = pszPassword;
169     params.ulPasswordMaxChars = ulPasswordMaxChars;
170     params.fSave = pfSave ? *pfSave : FALSE;
171
172     ret = DialogBoxParamW(hinstCredUI, MAKEINTRESOURCEW(IDD_CREDDIALOG),
173                           pUIInfo->hwndParent, CredDialogProc, (LPARAM)&params);
174     if (ret <= 0)
175         return GetLastError();
176
177     if (ret == IDCANCEL)
178     {
179         TRACE("dialog cancelled\n");
180         return ERROR_CANCELLED;
181     }
182
183     if (pfSave)
184         *pfSave = params.fSave;
185
186     return ERROR_SUCCESS;
187 }
188
189 /******************************************************************************
190  *           CredUIConfirmCredentialsW [CREDUI.@]
191  */
192 DWORD WINAPI CredUIConfirmCredentialsW(PCWSTR pszTargetName, BOOL bConfirm)
193 {
194     FIXME("(%s, %s): stub\n", debugstr_w(pszTargetName),
195           bConfirm ? "TRUE" : "FALSE");
196     return ERROR_SUCCESS;
197 }
198
199 /******************************************************************************
200  *           CredUIParseUserNameW [CREDUI.@]
201  */
202 DWORD WINAPI CredUIParseUserNameW(PCWSTR pszUserName, PWSTR pszUser,
203                                   ULONG ulMaxUserChars, PWSTR pszDomain,
204                                   ULONG ulMaxDomainChars)
205 {
206     PWSTR p;
207
208     TRACE("(%s, %p, %d, %p, %d)\n", debugstr_w(pszUserName), pszUser,
209           ulMaxUserChars, pszDomain, ulMaxDomainChars);
210
211     if (!pszUserName || !pszUser || !ulMaxUserChars || !pszDomain ||
212         !ulMaxDomainChars)
213         return ERROR_INVALID_PARAMETER;
214
215     /* FIXME: handle marshaled credentials */
216
217     p = strchrW(pszUserName, '\\');
218     if (p)
219     {
220         if (p - pszUserName > ulMaxDomainChars - 1)
221             return ERROR_INSUFFICIENT_BUFFER;
222         if (strlenW(p + 1) > ulMaxUserChars - 1)
223             return ERROR_INSUFFICIENT_BUFFER;
224         strcpyW(pszUser, p + 1);
225         memcpy(pszDomain, pszUserName, (p - pszUserName)*sizeof(WCHAR));
226         pszDomain[p - pszUserName] = '\0';
227
228         return ERROR_SUCCESS;
229     }
230
231     p = strrchrW(pszUserName, '@');
232     if (p)
233     {
234         if (p + 1 - pszUserName > ulMaxUserChars - 1)
235             return ERROR_INSUFFICIENT_BUFFER;
236         if (strlenW(p + 1) > ulMaxDomainChars - 1)
237             return ERROR_INSUFFICIENT_BUFFER;
238         strcpyW(pszDomain, p + 1);
239         memcpy(pszUser, pszUserName, (p - pszUserName)*sizeof(WCHAR));
240         pszUser[p - pszUserName] = '\0';
241
242         return ERROR_SUCCESS;
243     }
244
245     if (strlenW(pszUserName) > ulMaxUserChars - 1)
246         return ERROR_INSUFFICIENT_BUFFER;
247     strcpyW(pszUser, pszUserName);
248     pszDomain[0] = '\0';
249
250     return ERROR_SUCCESS;
251 }