move more dlls
[reactos.git] / reactos / lib / wininet / dialogs.c
1 /*
2 * Wininet
3 *
4 * Copyright 2003 Mike McCormack for CodeWeavers Inc.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "wininet.h"
31 #include "winnetwk.h"
32 #include "winnls.h"
33 #include "wine/debug.h"
34 #include "winerror.h"
35 #define NO_SHLWAPI_STREAM
36 #include "shlwapi.h"
37
38 #include "internet.h"
39
40 #include "wine/unicode.h"
41
42 #include "resource.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
45
46 struct WININET_ErrorDlgParams
47 {
48 HWND hWnd;
49 HINTERNET hRequest;
50 DWORD dwError;
51 DWORD dwFlags;
52 LPVOID* lppvData;
53 };
54
55 /***********************************************************************
56 * WININET_GetProxyServer
57 *
58 * Determine the name of the proxy server the request is using
59 */
60 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
61 {
62 LPWININETHTTPREQW lpwhr;
63 LPWININETHTTPSESSIONW lpwhs = NULL;
64 LPWININETAPPINFOW hIC = NULL;
65 LPWSTR p;
66
67 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
68 if (NULL == lpwhr)
69 return FALSE;
70
71 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
72 if (NULL == lpwhs)
73 return FALSE;
74
75 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
76 if (NULL == hIC)
77 return FALSE;
78
79 lstrcpynW(szBuf, hIC->lpszProxy, sz);
80
81 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
82 p = strchrW(szBuf, ':');
83 if(*p)
84 *p = 0;
85
86 return TRUE;
87 }
88
89 /***********************************************************************
90 * WININET_GetAuthRealm
91 *
92 * Determine the name of the (basic) Authentication realm
93 */
94 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
95 {
96 LPWSTR p, q;
97 DWORD index;
98 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
99
100 /* extract the Realm from the proxy response and show it */
101 index = 0;
102 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
103 szBuf, &sz, &index) )
104 return FALSE;
105
106 /*
107 * FIXME: maybe we should check that we're
108 * dealing with 'Basic' Authentication
109 */
110 p = strchrW( szBuf, ' ' );
111 if( p && !strncmpW( p+1, szRealm, strlenW(szRealm) ) )
112 {
113 /* remove quotes */
114 p += 7;
115 if( *p == '"' )
116 {
117 p++;
118 q = strrchrW( p, '"' );
119 if( q )
120 *q = 0;
121 }
122 }
123
124 strcpyW( szBuf, p );
125
126 return TRUE;
127 }
128
129 /***********************************************************************
130 * WININET_GetSetPassword
131 */
132 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
133 LPCWSTR szRealm, BOOL bSet )
134 {
135 WCHAR szResource[0x80], szUserPass[0x40];
136 LPWSTR p;
137 HWND hUserItem, hPassItem;
138 DWORD r, dwMagic = 19;
139 UINT r_len, u_len;
140 WORD sz;
141 static const WCHAR szColon[] = { ':',0 };
142 static const WCHAR szbs[] = { '/', 0 };
143
144 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
145 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
146
147 /* now try fetch the username and password */
148 lstrcpyW( szResource, szServer);
149 lstrcatW( szResource, szbs);
150 lstrcatW( szResource, szRealm);
151
152 /*
153 * WNetCachePassword is only concerned with the length
154 * of the data stored (which we tell it) and it does
155 * not use strlen() internally so we can add WCHAR data
156 * instead of ASCII data and get it back the same way.
157 */
158 if( bSet )
159 {
160 szUserPass[0] = 0;
161 GetWindowTextW( hUserItem, szUserPass,
162 (sizeof szUserPass-1)/sizeof(WCHAR) );
163 lstrcatW(szUserPass, szColon);
164 u_len = strlenW( szUserPass );
165 GetWindowTextW( hPassItem, szUserPass+u_len,
166 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
167
168 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
169 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
170 r = WNetCachePassword( (CHAR*)szResource, r_len,
171 (CHAR*)szUserPass, u_len, dwMagic, 0 );
172
173 return ( r == WN_SUCCESS );
174 }
175
176 sz = sizeof szUserPass;
177 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
178 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
179 (CHAR*)szUserPass, &sz, dwMagic );
180 if( r != WN_SUCCESS )
181 return FALSE;
182
183 p = strchrW( szUserPass, ':' );
184 if( p )
185 {
186 *p = 0;
187 SetWindowTextW( hUserItem, szUserPass );
188 SetWindowTextW( hPassItem, p+1 );
189 }
190
191 return TRUE;
192 }
193
194 /***********************************************************************
195 * WININET_SetProxyAuthorization
196 */
197 static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
198 LPWSTR username, LPWSTR password )
199 {
200 LPWININETHTTPREQW lpwhr;
201 LPWININETHTTPSESSIONW lpwhs;
202 LPWININETAPPINFOW hIC;
203 LPWSTR p;
204
205 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
206 if( !lpwhr )
207 return FALSE;
208
209 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
210 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
211 {
212 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
213 return FALSE;
214 }
215
216 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
217
218 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
219 if( !p )
220 return FALSE;
221
222 lstrcpyW( p, username );
223 hIC->lpszProxyUsername = p;
224
225 p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
226 if( !p )
227 return FALSE;
228
229 lstrcpyW( p, password );
230 hIC->lpszProxyPassword = p;
231
232 return TRUE;
233 }
234
235 /***********************************************************************
236 * WININET_ProxyPasswordDialog
237 */
238 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
239 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
240 {
241 HWND hitem;
242 struct WININET_ErrorDlgParams *params;
243 WCHAR szRealm[0x80], szServer[0x80];
244
245 if( uMsg == WM_INITDIALOG )
246 {
247 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
248
249 /* save the parameter list */
250 params = (struct WININET_ErrorDlgParams*) lParam;
251 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
252
253 /* extract the Realm from the proxy response and show it */
254 if( WININET_GetAuthRealm( params->hRequest,
255 szRealm, sizeof szRealm/sizeof(WCHAR)) )
256 {
257 hitem = GetDlgItem( hdlg, IDC_REALM );
258 SetWindowTextW( hitem, szRealm );
259 }
260
261 /* extract the name of the proxy server */
262 if( WININET_GetProxyServer( params->hRequest,
263 szServer, sizeof szServer/sizeof(WCHAR)) )
264 {
265 hitem = GetDlgItem( hdlg, IDC_PROXY );
266 SetWindowTextW( hitem, szServer );
267 }
268
269 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
270
271 return TRUE;
272 }
273
274 params = (struct WININET_ErrorDlgParams*)
275 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
276
277 switch( uMsg )
278 {
279 case WM_COMMAND:
280 if( wParam == IDOK )
281 {
282 WCHAR username[0x20], password[0x20];
283
284 username[0] = 0;
285 hitem = GetDlgItem( hdlg, IDC_USERNAME );
286 if( hitem )
287 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
288
289 password[0] = 0;
290 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
291 if( hitem )
292 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
293
294 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
295 if( hitem &&
296 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
297 WININET_GetAuthRealm( params->hRequest,
298 szRealm, sizeof szRealm/sizeof(WCHAR)) &&
299 WININET_GetProxyServer( params->hRequest,
300 szServer, sizeof szServer/sizeof(WCHAR)) )
301 {
302 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
303 }
304 WININET_SetProxyAuthorization( params->hRequest, username, password );
305
306 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
307 return TRUE;
308 }
309 if( wParam == IDCANCEL )
310 {
311 EndDialog( hdlg, 0 );
312 return TRUE;
313 }
314 break;
315 }
316 return FALSE;
317 }
318
319 /***********************************************************************
320 * WININET_GetConnectionStatus
321 */
322 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
323 {
324 WCHAR szStatus[0x20];
325 DWORD sz, index, dwStatus;
326
327 TRACE("%p\n", hRequest );
328
329 sz = sizeof szStatus;
330 index = 0;
331 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
332 szStatus, &sz, &index))
333 return -1;
334 dwStatus = atoiW( szStatus );
335
336 TRACE("request %p status = %ld\n", hRequest, dwStatus );
337
338 return dwStatus;
339 }
340
341
342 /***********************************************************************
343 * InternetErrorDlg
344 */
345 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
346 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
347 {
348 struct WININET_ErrorDlgParams params;
349 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
350 INT dwStatus;
351
352 TRACE("%p %p %ld %08lx %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
353
354 params.hWnd = hWnd;
355 params.hRequest = hRequest;
356 params.dwError = dwError;
357 params.dwFlags = dwFlags;
358 params.lppvData = lppvData;
359
360 switch( dwError )
361 {
362 case ERROR_SUCCESS:
363 if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
364 return 0;
365 dwStatus = WININET_GetConnectionStatus( hRequest );
366 if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
367 return ERROR_SUCCESS;
368 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
369 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
370
371 case ERROR_INTERNET_INCORRECT_PASSWORD:
372 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
373 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
374
375 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
376 case ERROR_INTERNET_INVALID_CA:
377 case ERROR_INTERNET_POST_IS_NON_SECURE:
378 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
379 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
380 FIXME("Need to display dialog for error %ld\n", dwError);
381 return ERROR_SUCCESS;
382 }
383 return ERROR_INVALID_PARAMETER;
384 }