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
22 #include "wine/port.h"
24 #if defined(__MINGW32__) || defined (_MSC_VER)
36 #include "wine/debug.h"
38 #define NO_SHLWAPI_STREAM
43 #include "wine/unicode.h"
47 #define MAX_STRING_LEN 1024
49 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
51 struct WININET_ErrorDlgParams
60 /***********************************************************************
61 * WININET_GetAuthRealm
63 * Determine the name of the (basic) Authentication realm
65 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz, BOOL proxy )
69 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
72 query = HTTP_QUERY_PROXY_AUTHENTICATE;
74 query = HTTP_QUERY_WWW_AUTHENTICATE;
76 /* extract the Realm from the response and show it */
78 if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
82 * FIXME: maybe we should check that we're
83 * dealing with 'Basic' Authentication
85 p = strchrW( szBuf, ' ' );
86 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
88 ERR("response wrong? (%s)\n", debugstr_w(szBuf));
97 q = strrchrW( p, '"' );
106 /* These two are not defined in the public headers */
107 extern DWORD WINAPI WNetCachePassword(LPSTR,WORD,LPSTR,WORD,BYTE,WORD);
108 extern DWORD WINAPI WNetGetCachedPassword(LPSTR,WORD,LPSTR,LPWORD,BYTE);
110 /***********************************************************************
111 * WININET_GetSetPassword
113 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
114 LPCWSTR szRealm, BOOL bSet )
116 WCHAR szResource[0x80], szUserPass[0x40];
118 HWND hUserItem, hPassItem;
119 DWORD r, dwMagic = 19;
122 static const WCHAR szColon[] = { ':',0 };
123 static const WCHAR szbs[] = { '/', 0 };
125 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
126 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
128 /* now try fetch the username and password */
129 lstrcpyW( szResource, szServer);
130 lstrcatW( szResource, szbs);
131 lstrcatW( szResource, szRealm);
134 * WNetCachePassword is only concerned with the length
135 * of the data stored (which we tell it) and it does
136 * not use strlen() internally so we can add WCHAR data
137 * instead of ASCII data and get it back the same way.
142 GetWindowTextW( hUserItem, szUserPass,
143 (sizeof szUserPass-1)/sizeof(WCHAR) );
144 lstrcatW(szUserPass, szColon);
145 u_len = strlenW( szUserPass );
146 GetWindowTextW( hPassItem, szUserPass+u_len,
147 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
149 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
150 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
151 r = WNetCachePassword( (CHAR*)szResource, r_len,
152 (CHAR*)szUserPass, u_len, dwMagic, 0 );
154 return ( r == WN_SUCCESS );
157 sz = sizeof szUserPass;
158 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
159 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
160 (CHAR*)szUserPass, &sz, dwMagic );
161 if( r != WN_SUCCESS )
164 p = strchrW( szUserPass, ':' );
168 SetWindowTextW( hUserItem, szUserPass );
169 SetWindowTextW( hPassItem, p+1 );
175 /***********************************************************************
176 * WININET_SetAuthorization
178 static BOOL WININET_SetAuthorization( http_request_t *request, LPWSTR username,
179 LPWSTR password, BOOL proxy )
181 http_session_t *session = request->session;
184 p = heap_strdupW(username);
188 q = heap_strdupW(password);
197 appinfo_t *hIC = session->appInfo;
199 heap_free(hIC->proxyUsername);
200 hIC->proxyUsername = p;
202 heap_free(hIC->proxyPassword);
203 hIC->proxyPassword = q;
207 heap_free(session->userName);
208 session->userName = p;
210 heap_free(session->password);
211 session->password = q;
217 /***********************************************************************
218 * WININET_ProxyPasswordDialog
220 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
221 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
224 struct WININET_ErrorDlgParams *params;
225 WCHAR szRealm[0x80], szServer[0x80];
227 if( uMsg == WM_INITDIALOG )
229 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
231 /* save the parameter list */
232 params = (struct WININET_ErrorDlgParams*) lParam;
233 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
235 /* extract the Realm from the proxy response and show it */
236 if( WININET_GetAuthRealm( params->req->hdr.hInternet,
237 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
239 hitem = GetDlgItem( hdlg, IDC_REALM );
240 SetWindowTextW( hitem, szRealm );
243 hitem = GetDlgItem( hdlg, IDC_PROXY );
244 SetWindowTextW( hitem, params->req->session->appInfo->proxy );
246 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
251 params = (struct WININET_ErrorDlgParams*)
252 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
259 WCHAR username[0x20], password[0x20];
262 hitem = GetDlgItem( hdlg, IDC_USERNAME );
264 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
267 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
269 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
271 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
273 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
274 WININET_GetAuthRealm( params->req->hdr.hInternet,
275 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE) )
276 WININET_GetSetPassword( hdlg, params->req->session->appInfo->proxy, szRealm, TRUE );
277 WININET_SetAuthorization( params->req, username, password, TRUE );
279 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
282 if( wParam == IDCANCEL )
284 EndDialog( hdlg, 0 );
292 /***********************************************************************
293 * WININET_PasswordDialog
295 static INT_PTR WINAPI WININET_PasswordDialog(
296 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
299 struct WININET_ErrorDlgParams *params;
300 WCHAR szRealm[0x80], szServer[0x80];
302 if( uMsg == WM_INITDIALOG )
304 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
306 /* save the parameter list */
307 params = (struct WININET_ErrorDlgParams*) lParam;
308 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
310 /* extract the Realm from the response and show it */
311 if( WININET_GetAuthRealm( params->req->hdr.hInternet,
312 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
314 hitem = GetDlgItem( hdlg, IDC_REALM );
315 SetWindowTextW( hitem, szRealm );
318 hitem = GetDlgItem( hdlg, IDC_SERVER );
319 SetWindowTextW( hitem, params->req->session->hostName );
321 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
326 params = (struct WININET_ErrorDlgParams*)
327 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
334 WCHAR username[0x20], password[0x20];
337 hitem = GetDlgItem( hdlg, IDC_USERNAME );
339 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
342 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
344 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
346 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
348 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
349 WININET_GetAuthRealm( params->req->hdr.hInternet,
350 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ))
352 WININET_GetSetPassword( hdlg, params->req->session->hostName, szRealm, TRUE );
354 WININET_SetAuthorization( params->req, username, password, FALSE );
356 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
359 if( wParam == IDCANCEL )
361 EndDialog( hdlg, 0 );
369 /***********************************************************************
370 * WININET_InvalidCertificateDialog
372 static INT_PTR WINAPI WININET_InvalidCertificateDialog(
373 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
375 struct WININET_ErrorDlgParams *params;
379 if( uMsg == WM_INITDIALOG )
381 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
383 /* save the parameter list */
384 params = (struct WININET_ErrorDlgParams*) lParam;
385 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
387 switch( params->dwError )
389 case ERROR_INTERNET_INVALID_CA:
390 LoadStringW( WININET_hModule, IDS_CERT_CA_INVALID, buf, 1024 );
392 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
393 LoadStringW( WININET_hModule, IDS_CERT_DATE_INVALID, buf, 1024 );
395 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
396 LoadStringW( WININET_hModule, IDS_CERT_CN_INVALID, buf, 1024 );
398 case ERROR_INTERNET_SEC_CERT_ERRORS:
399 /* FIXME: We should fetch information about the
400 * certificate here and show all the relevant errors.
402 LoadStringW( WININET_hModule, IDS_CERT_ERRORS, buf, 1024 );
405 FIXME( "No message for error %d\n", params->dwError );
409 hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
410 SetWindowTextW( hitem, buf );
415 params = (struct WININET_ErrorDlgParams*)
416 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
425 if( params->dwFlags & FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
427 http_request_t *req = params->req;
428 DWORD flags, size = sizeof(flags);
430 InternetQueryOptionW( req->hdr.hInternet, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size );
431 switch( params->dwError )
433 case ERROR_INTERNET_INVALID_CA:
434 flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
436 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
437 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
439 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
440 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
442 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
443 flags |= SECURITY_FLAG_IGNORE_REVOCATION;
445 case ERROR_INTERNET_SEC_CERT_ERRORS:
446 if(flags & _SECURITY_FLAG_CERT_REV_FAILED)
447 flags |= SECURITY_FLAG_IGNORE_REVOCATION;
448 if(flags & _SECURITY_FLAG_CERT_INVALID_CA)
449 flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
450 if(flags & _SECURITY_FLAG_CERT_INVALID_CN)
451 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
452 if(flags & _SECURITY_FLAG_CERT_INVALID_DATE)
453 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
456 /* FIXME: Use helper function */
457 flags |= SECURITY_FLAG_SECURE;
458 req->security_flags |= flags;
460 req->netconn->security_flags |= flags;
463 EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
466 if( wParam == IDCANCEL )
468 TRACE("Pressed cancel.\n");
470 EndDialog( hdlg, ERROR_CANCELLED );
479 /***********************************************************************
482 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
483 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
485 struct WININET_ErrorDlgParams params;
486 http_request_t *req = NULL;
487 DWORD res = ERROR_SUCCESS;
489 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
491 if( !hWnd && !(dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI) )
492 return ERROR_INVALID_HANDLE;
495 req = (http_request_t*)get_handle_object(hRequest);
497 return ERROR_INVALID_HANDLE;
498 if(req->hdr.htype != WH_HHTTPREQ)
499 return ERROR_SUCCESS; /* Yes, that was tested */
504 params.dwError = dwError;
505 params.dwFlags = dwFlags;
506 params.lppvData = lppvData;
511 case ERROR_INTERNET_INCORRECT_PASSWORD: {
512 if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
515 return ERROR_INVALID_HANDLE;
517 switch(req->status_code) {
518 case HTTP_STATUS_PROXY_AUTH_REQ:
519 res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_PROXYDLG ),
520 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
522 case HTTP_STATUS_DENIED:
523 res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_AUTHDLG ),
524 hWnd, WININET_PasswordDialog, (LPARAM) ¶ms );
527 WARN("unhandled status %u\n", req->status_code);
531 case ERROR_INTERNET_SEC_CERT_ERRORS:
532 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
533 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
534 case ERROR_INTERNET_INVALID_CA:
535 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
536 if( dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI ) {
537 res = ERROR_CANCELLED;
541 return ERROR_INVALID_HANDLE;
544 if( dwFlags & ~FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
545 FIXME("%08x contains unsupported flags.\n", dwFlags);
547 res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_INVCERTDLG ),
548 hWnd, WININET_InvalidCertificateDialog, (LPARAM) ¶ms );
550 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
551 case ERROR_INTERNET_POST_IS_NON_SECURE:
552 FIXME("Need to display dialog for error %d\n", dwError);
556 res = ERROR_NOT_SUPPORTED;
560 WININET_Release(&req->hdr);