Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / wininet / dialogs.c
blob2c4a52785696078ab2318f69687b6f4afec277e4
1 /*
2 * Wininet
4 * Copyright 2003 Mike McCormack for CodeWeavers Inc.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #if defined(__MINGW32__) || defined (_MSC_VER)
25 #include <ws2tcpip.h>
26 #endif
28 #include <stdarg.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winreg.h"
34 #include "wininet.h"
35 #include "winnetwk.h"
36 #include "wine/debug.h"
37 #include "winerror.h"
38 #define NO_SHLWAPI_STREAM
39 #include "shlwapi.h"
41 #include "internet.h"
43 #include "wine/unicode.h"
45 #include "resource.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
49 struct WININET_ErrorDlgParams
51 HWND hWnd;
52 HINTERNET hRequest;
53 DWORD dwError;
54 DWORD dwFlags;
55 LPVOID* lppvData;
58 /***********************************************************************
59 * WININET_GetProxyServer
61 * Determine the name of the proxy server the request is using
63 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
65 http_request_t *lpwhr;
66 http_session_t *lpwhs = NULL;
67 appinfo_t *hIC = NULL;
68 BOOL ret = FALSE;
69 LPWSTR p;
71 lpwhr = (http_request_t*) WININET_GetObject( hRequest );
72 if (NULL == lpwhr)
73 return FALSE;
75 lpwhs = lpwhr->lpHttpSession;
76 if (NULL == lpwhs)
77 goto done;
79 hIC = lpwhs->lpAppInfo;
80 if (NULL == hIC)
81 goto done;
83 lstrcpynW(szBuf, hIC->lpszProxy, sz);
85 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
86 p = strchrW(szBuf, ':');
87 if (p)
88 *p = 0;
90 ret = TRUE;
92 done:
93 WININET_Release( &lpwhr->hdr );
94 return ret;
97 /***********************************************************************
98 * WININET_GetServer
100 * Determine the name of the web server
102 static BOOL WININET_GetServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
104 http_request_t *lpwhr;
105 http_session_t *lpwhs = NULL;
106 BOOL ret = FALSE;
108 lpwhr = (http_request_t*) WININET_GetObject( hRequest );
109 if (NULL == lpwhr)
110 return FALSE;
112 lpwhs = lpwhr->lpHttpSession;
113 if (NULL == lpwhs)
114 goto done;
116 lstrcpynW(szBuf, lpwhs->lpszHostName, sz);
118 ret = TRUE;
120 done:
121 WININET_Release( &lpwhr->hdr );
122 return ret;
125 /***********************************************************************
126 * WININET_GetAuthRealm
128 * Determine the name of the (basic) Authentication realm
130 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz, BOOL proxy )
132 LPWSTR p, q;
133 DWORD index, query;
134 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
136 if (proxy)
137 query = HTTP_QUERY_PROXY_AUTHENTICATE;
138 else
139 query = HTTP_QUERY_WWW_AUTHENTICATE;
141 /* extract the Realm from the response and show it */
142 index = 0;
143 if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
144 return FALSE;
147 * FIXME: maybe we should check that we're
148 * dealing with 'Basic' Authentication
150 p = strchrW( szBuf, ' ' );
151 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
153 ERR("response wrong? (%s)\n", debugstr_w(szBuf));
154 return FALSE;
157 /* remove quotes */
158 p += 7;
159 if( *p == '"' )
161 p++;
162 q = strrchrW( p, '"' );
163 if( q )
164 *q = 0;
166 strcpyW( szBuf, p );
168 return TRUE;
171 /***********************************************************************
172 * WININET_GetSetPassword
174 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
175 LPCWSTR szRealm, BOOL bSet )
177 WCHAR szResource[0x80], szUserPass[0x40];
178 LPWSTR p;
179 HWND hUserItem, hPassItem;
180 DWORD r, dwMagic = 19;
181 UINT r_len, u_len;
182 WORD sz;
183 static const WCHAR szColon[] = { ':',0 };
184 static const WCHAR szbs[] = { '/', 0 };
186 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
187 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
189 /* now try fetch the username and password */
190 lstrcpyW( szResource, szServer);
191 lstrcatW( szResource, szbs);
192 lstrcatW( szResource, szRealm);
195 * WNetCachePassword is only concerned with the length
196 * of the data stored (which we tell it) and it does
197 * not use strlen() internally so we can add WCHAR data
198 * instead of ASCII data and get it back the same way.
200 if( bSet )
202 szUserPass[0] = 0;
203 GetWindowTextW( hUserItem, szUserPass,
204 (sizeof szUserPass-1)/sizeof(WCHAR) );
205 lstrcatW(szUserPass, szColon);
206 u_len = strlenW( szUserPass );
207 GetWindowTextW( hPassItem, szUserPass+u_len,
208 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
210 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
211 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
212 r = WNetCachePassword( (CHAR*)szResource, r_len,
213 (CHAR*)szUserPass, u_len, dwMagic, 0 );
215 return ( r == WN_SUCCESS );
218 sz = sizeof szUserPass;
219 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
220 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
221 (CHAR*)szUserPass, &sz, dwMagic );
222 if( r != WN_SUCCESS )
223 return FALSE;
225 p = strchrW( szUserPass, ':' );
226 if( p )
228 *p = 0;
229 SetWindowTextW( hUserItem, szUserPass );
230 SetWindowTextW( hPassItem, p+1 );
233 return TRUE;
236 /***********************************************************************
237 * WININET_SetAuthorization
239 static BOOL WININET_SetAuthorization( HINTERNET hRequest, LPWSTR username,
240 LPWSTR password, BOOL proxy )
242 http_request_t *lpwhr;
243 http_session_t *lpwhs;
244 BOOL ret = FALSE;
245 LPWSTR p, q;
247 lpwhr = (http_request_t*) WININET_GetObject( hRequest );
248 if( !lpwhr )
249 return FALSE;
251 lpwhs = lpwhr->lpHttpSession;
252 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
254 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
255 goto done;
258 p = heap_strdupW(username);
259 if( !p )
260 goto done;
262 q = heap_strdupW(password);
263 if( !q )
265 HeapFree(GetProcessHeap(), 0, username);
266 goto done;
269 if (proxy)
271 appinfo_t *hIC = lpwhs->lpAppInfo;
273 HeapFree(GetProcessHeap(), 0, hIC->lpszProxyUsername);
274 hIC->lpszProxyUsername = p;
276 HeapFree(GetProcessHeap(), 0, hIC->lpszProxyPassword);
277 hIC->lpszProxyPassword = q;
279 else
281 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
282 lpwhs->lpszUserName = p;
284 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
285 lpwhs->lpszPassword = q;
288 ret = TRUE;
290 done:
291 WININET_Release( &lpwhr->hdr );
292 return ret;
295 /***********************************************************************
296 * WININET_ProxyPasswordDialog
298 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
299 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
301 HWND hitem;
302 struct WININET_ErrorDlgParams *params;
303 WCHAR szRealm[0x80], szServer[0x80];
305 if( uMsg == WM_INITDIALOG )
307 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
309 /* save the parameter list */
310 params = (struct WININET_ErrorDlgParams*) lParam;
311 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
313 /* extract the Realm from the proxy response and show it */
314 if( WININET_GetAuthRealm( params->hRequest,
315 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
317 hitem = GetDlgItem( hdlg, IDC_REALM );
318 SetWindowTextW( hitem, szRealm );
321 /* extract the name of the proxy server */
322 if( WININET_GetProxyServer( params->hRequest,
323 szServer, sizeof szServer/sizeof(WCHAR)) )
325 hitem = GetDlgItem( hdlg, IDC_PROXY );
326 SetWindowTextW( hitem, szServer );
329 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
331 return TRUE;
334 params = (struct WININET_ErrorDlgParams*)
335 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
337 switch( uMsg )
339 case WM_COMMAND:
340 if( wParam == IDOK )
342 WCHAR username[0x20], password[0x20];
344 username[0] = 0;
345 hitem = GetDlgItem( hdlg, IDC_USERNAME );
346 if( hitem )
347 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
349 password[0] = 0;
350 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
351 if( hitem )
352 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
354 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
355 if( hitem &&
356 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
357 WININET_GetAuthRealm( params->hRequest,
358 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) &&
359 WININET_GetProxyServer( params->hRequest,
360 szServer, sizeof szServer/sizeof(WCHAR)) )
362 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
364 WININET_SetAuthorization( params->hRequest, username, password, TRUE );
366 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
367 return TRUE;
369 if( wParam == IDCANCEL )
371 EndDialog( hdlg, 0 );
372 return TRUE;
374 break;
376 return FALSE;
379 /***********************************************************************
380 * WININET_PasswordDialog
382 static INT_PTR WINAPI WININET_PasswordDialog(
383 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
385 HWND hitem;
386 struct WININET_ErrorDlgParams *params;
387 WCHAR szRealm[0x80], szServer[0x80];
389 if( uMsg == WM_INITDIALOG )
391 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
393 /* save the parameter list */
394 params = (struct WININET_ErrorDlgParams*) lParam;
395 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
397 /* extract the Realm from the response and show it */
398 if( WININET_GetAuthRealm( params->hRequest,
399 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
401 hitem = GetDlgItem( hdlg, IDC_REALM );
402 SetWindowTextW( hitem, szRealm );
405 /* extract the name of the server */
406 if( WININET_GetServer( params->hRequest,
407 szServer, sizeof szServer/sizeof(WCHAR)) )
409 hitem = GetDlgItem( hdlg, IDC_SERVER );
410 SetWindowTextW( hitem, szServer );
413 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
415 return TRUE;
418 params = (struct WININET_ErrorDlgParams*)
419 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
421 switch( uMsg )
423 case WM_COMMAND:
424 if( wParam == IDOK )
426 WCHAR username[0x20], password[0x20];
428 username[0] = 0;
429 hitem = GetDlgItem( hdlg, IDC_USERNAME );
430 if( hitem )
431 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
433 password[0] = 0;
434 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
435 if( hitem )
436 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
438 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
439 if( hitem &&
440 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
441 WININET_GetAuthRealm( params->hRequest,
442 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) &&
443 WININET_GetServer( params->hRequest,
444 szServer, sizeof szServer/sizeof(WCHAR)) )
446 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
448 WININET_SetAuthorization( params->hRequest, username, password, FALSE );
450 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
451 return TRUE;
453 if( wParam == IDCANCEL )
455 EndDialog( hdlg, 0 );
456 return TRUE;
458 break;
460 return FALSE;
463 /***********************************************************************
464 * WININET_GetConnectionStatus
466 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
468 WCHAR szStatus[0x20];
469 DWORD sz, index, dwStatus;
471 TRACE("%p\n", hRequest );
473 sz = sizeof szStatus;
474 index = 0;
475 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
476 szStatus, &sz, &index))
477 return -1;
478 dwStatus = atoiW( szStatus );
480 TRACE("request %p status = %d\n", hRequest, dwStatus );
482 return dwStatus;
486 /***********************************************************************
487 * InternetErrorDlg
489 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
490 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
492 struct WININET_ErrorDlgParams params;
493 HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
494 INT dwStatus;
496 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
498 params.hWnd = hWnd;
499 params.hRequest = hRequest;
500 params.dwError = dwError;
501 params.dwFlags = dwFlags;
502 params.lppvData = lppvData;
504 switch( dwError )
506 case ERROR_SUCCESS:
507 case ERROR_INTERNET_INCORRECT_PASSWORD:
508 if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
509 return 0;
511 dwStatus = WININET_GetConnectionStatus( hRequest );
512 switch (dwStatus)
514 case HTTP_STATUS_PROXY_AUTH_REQ:
515 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
516 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
517 case HTTP_STATUS_DENIED:
518 return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_AUTHDLG ),
519 hWnd, WININET_PasswordDialog, (LPARAM) &params );
520 default:
521 WARN("unhandled status %u\n", dwStatus);
522 return 0;
525 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
526 case ERROR_INTERNET_INVALID_CA:
527 case ERROR_INTERNET_POST_IS_NON_SECURE:
528 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
529 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
530 FIXME("Need to display dialog for error %d\n", dwError);
531 return ERROR_SUCCESS;
533 return ERROR_INVALID_PARAMETER;