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_GetProxyServer
63 * Determine the name of the proxy server the request is using
65 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
67 http_request_t *request;
68 http_session_t *session = NULL;
69 appinfo_t *hIC = NULL;
73 request = (http_request_t*) get_handle_object( hRequest );
77 session = request->session;
81 hIC = session->appInfo;
85 lstrcpynW(szBuf, hIC->proxy, sz);
87 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
88 p = strchrW(szBuf, ':');
95 WININET_Release( &request->hdr );
99 /***********************************************************************
102 * Determine the name of the web server
104 static BOOL WININET_GetServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
106 http_request_t *request;
107 http_session_t *session = NULL;
110 request = (http_request_t*) get_handle_object( hRequest );
114 session = request->session;
118 lstrcpynW(szBuf, session->hostName, sz);
123 WININET_Release( &request->hdr );
127 /***********************************************************************
128 * WININET_GetAuthRealm
130 * Determine the name of the (basic) Authentication realm
132 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz, BOOL proxy )
136 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
139 query = HTTP_QUERY_PROXY_AUTHENTICATE;
141 query = HTTP_QUERY_WWW_AUTHENTICATE;
143 /* extract the Realm from the response and show it */
145 if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
149 * FIXME: maybe we should check that we're
150 * dealing with 'Basic' Authentication
152 p = strchrW( szBuf, ' ' );
153 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
155 ERR("response wrong? (%s)\n", debugstr_w(szBuf));
164 q = strrchrW( p, '"' );
173 /* These two are not defined in the public headers */
174 extern DWORD WINAPI WNetCachePassword(LPSTR,WORD,LPSTR,WORD,BYTE,WORD);
175 extern DWORD WINAPI WNetGetCachedPassword(LPSTR,WORD,LPSTR,LPWORD,BYTE);
177 /***********************************************************************
178 * WININET_GetSetPassword
180 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
181 LPCWSTR szRealm, BOOL bSet )
183 WCHAR szResource[0x80], szUserPass[0x40];
185 HWND hUserItem, hPassItem;
186 DWORD r, dwMagic = 19;
189 static const WCHAR szColon[] = { ':',0 };
190 static const WCHAR szbs[] = { '/', 0 };
192 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
193 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
195 /* now try fetch the username and password */
196 lstrcpyW( szResource, szServer);
197 lstrcatW( szResource, szbs);
198 lstrcatW( szResource, szRealm);
201 * WNetCachePassword is only concerned with the length
202 * of the data stored (which we tell it) and it does
203 * not use strlen() internally so we can add WCHAR data
204 * instead of ASCII data and get it back the same way.
209 GetWindowTextW( hUserItem, szUserPass,
210 (sizeof szUserPass-1)/sizeof(WCHAR) );
211 lstrcatW(szUserPass, szColon);
212 u_len = strlenW( szUserPass );
213 GetWindowTextW( hPassItem, szUserPass+u_len,
214 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
216 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
217 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
218 r = WNetCachePassword( (CHAR*)szResource, r_len,
219 (CHAR*)szUserPass, u_len, dwMagic, 0 );
221 return ( r == WN_SUCCESS );
224 sz = sizeof szUserPass;
225 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
226 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
227 (CHAR*)szUserPass, &sz, dwMagic );
228 if( r != WN_SUCCESS )
231 p = strchrW( szUserPass, ':' );
235 SetWindowTextW( hUserItem, szUserPass );
236 SetWindowTextW( hPassItem, p+1 );
242 /***********************************************************************
243 * WININET_SetAuthorization
245 static BOOL WININET_SetAuthorization( HINTERNET hRequest, LPWSTR username,
246 LPWSTR password, BOOL proxy )
248 http_request_t *request;
249 http_session_t *session;
253 request = (http_request_t*) get_handle_object( hRequest );
257 session = request->session;
258 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
260 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
264 p = heap_strdupW(username);
268 q = heap_strdupW(password);
277 appinfo_t *hIC = session->appInfo;
279 heap_free(hIC->proxyUsername);
280 hIC->proxyUsername = p;
282 heap_free(hIC->proxyPassword);
283 hIC->proxyPassword = q;
287 heap_free(session->userName);
288 session->userName = p;
290 heap_free(session->password);
291 session->password = q;
297 WININET_Release( &request->hdr );
301 /***********************************************************************
302 * WININET_ProxyPasswordDialog
304 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
305 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
308 struct WININET_ErrorDlgParams *params;
309 WCHAR szRealm[0x80], szServer[0x80];
311 if( uMsg == WM_INITDIALOG )
313 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
315 /* save the parameter list */
316 params = (struct WININET_ErrorDlgParams*) lParam;
317 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
319 /* extract the Realm from the proxy response and show it */
320 if( WININET_GetAuthRealm( params->hRequest,
321 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
323 hitem = GetDlgItem( hdlg, IDC_REALM );
324 SetWindowTextW( hitem, szRealm );
327 /* extract the name of the proxy server */
328 if( WININET_GetProxyServer( params->hRequest,
329 szServer, sizeof szServer/sizeof(WCHAR)) )
331 hitem = GetDlgItem( hdlg, IDC_PROXY );
332 SetWindowTextW( hitem, szServer );
335 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
340 params = (struct WININET_ErrorDlgParams*)
341 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
348 WCHAR username[0x20], password[0x20];
351 hitem = GetDlgItem( hdlg, IDC_USERNAME );
353 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
356 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
358 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
360 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
362 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
363 WININET_GetAuthRealm( params->hRequest,
364 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) &&
365 WININET_GetProxyServer( params->hRequest,
366 szServer, sizeof szServer/sizeof(WCHAR)) )
368 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
370 WININET_SetAuthorization( params->hRequest, username, password, TRUE );
372 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
375 if( wParam == IDCANCEL )
377 EndDialog( hdlg, 0 );
385 /***********************************************************************
386 * WININET_PasswordDialog
388 static INT_PTR WINAPI WININET_PasswordDialog(
389 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
392 struct WININET_ErrorDlgParams *params;
393 WCHAR szRealm[0x80], szServer[0x80];
395 if( uMsg == WM_INITDIALOG )
397 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
399 /* save the parameter list */
400 params = (struct WININET_ErrorDlgParams*) lParam;
401 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
403 /* extract the Realm from the response and show it */
404 if( WININET_GetAuthRealm( params->hRequest,
405 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
407 hitem = GetDlgItem( hdlg, IDC_REALM );
408 SetWindowTextW( hitem, szRealm );
411 /* extract the name of the server */
412 if( WININET_GetServer( params->hRequest,
413 szServer, sizeof szServer/sizeof(WCHAR)) )
415 hitem = GetDlgItem( hdlg, IDC_SERVER );
416 SetWindowTextW( hitem, szServer );
419 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
424 params = (struct WININET_ErrorDlgParams*)
425 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
432 WCHAR username[0x20], password[0x20];
435 hitem = GetDlgItem( hdlg, IDC_USERNAME );
437 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
440 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
442 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
444 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
446 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
447 WININET_GetAuthRealm( params->hRequest,
448 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) &&
449 WININET_GetServer( params->hRequest,
450 szServer, sizeof szServer/sizeof(WCHAR)) )
452 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
454 WININET_SetAuthorization( params->hRequest, username, password, FALSE );
456 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
459 if( wParam == IDCANCEL )
461 EndDialog( hdlg, 0 );
469 /***********************************************************************
470 * WININET_InvalidCertificateDialog
472 static INT_PTR WINAPI WININET_InvalidCertificateDialog(
473 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
475 struct WININET_ErrorDlgParams *params;
479 if( uMsg == WM_INITDIALOG )
481 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
483 /* save the parameter list */
484 params = (struct WININET_ErrorDlgParams*) lParam;
485 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
487 switch( params->dwError )
489 case ERROR_INTERNET_INVALID_CA:
490 LoadStringW( WININET_hModule, IDS_CERT_CA_INVALID, buf, 1024 );
492 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
493 LoadStringW( WININET_hModule, IDS_CERT_DATE_INVALID, buf, 1024 );
495 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
496 LoadStringW( WININET_hModule, IDS_CERT_CN_INVALID, buf, 1024 );
498 case ERROR_INTERNET_SEC_CERT_ERRORS:
499 /* FIXME: We should fetch information about the
500 * certificate here and show all the relevant errors.
502 LoadStringW( WININET_hModule, IDS_CERT_ERRORS, buf, 1024 );
505 FIXME( "No message for error %d\n", params->dwError );
509 hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
510 SetWindowTextW( hitem, buf );
515 params = (struct WININET_ErrorDlgParams*)
516 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
525 if( params->dwFlags & FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
527 DWORD flags, size = sizeof(flags);
529 InternetQueryOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size );
530 switch( params->dwError )
532 case ERROR_INTERNET_INVALID_CA:
533 flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
535 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
536 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
538 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
539 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
541 case ERROR_INTERNET_SEC_CERT_ERRORS:
542 FIXME("Should only add ignore flags as needed.\n");
543 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
544 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
545 SECURITY_FLAG_IGNORE_UNKNOWN_CA;
546 /* FIXME: ERROR_INTERNET_SEC_CERT_ERRORS also
547 * seems to set the corresponding DLG_* flags.
551 res = InternetSetOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, size );
553 WARN("InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed.\n");
556 EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
559 if( wParam == IDCANCEL )
561 TRACE("Pressed cancel.\n");
563 EndDialog( hdlg, ERROR_CANCELLED );
572 /***********************************************************************
573 * WININET_GetConnectionStatus
575 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
577 WCHAR szStatus[0x20];
578 DWORD sz, index, dwStatus;
580 TRACE("%p\n", hRequest );
582 sz = sizeof szStatus;
584 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
585 szStatus, &sz, &index))
587 dwStatus = atoiW( szStatus );
589 TRACE("request %p status = %d\n", hRequest, dwStatus );
595 /***********************************************************************
598 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
599 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
601 struct WININET_ErrorDlgParams params;
604 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
606 if( !hWnd && !(dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI) )
607 return ERROR_INVALID_HANDLE;
610 params.hRequest = hRequest;
611 params.dwError = dwError;
612 params.dwFlags = dwFlags;
613 params.lppvData = lppvData;
618 case ERROR_INTERNET_INCORRECT_PASSWORD:
619 if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
622 dwStatus = WININET_GetConnectionStatus( hRequest );
625 case HTTP_STATUS_PROXY_AUTH_REQ:
626 return DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_PROXYDLG ),
627 hWnd, WININET_ProxyPasswordDialog, (LPARAM) ¶ms );
628 case HTTP_STATUS_DENIED:
629 return DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_AUTHDLG ),
630 hWnd, WININET_PasswordDialog, (LPARAM) ¶ms );
632 WARN("unhandled status %u\n", dwStatus);
635 case ERROR_INTERNET_SEC_CERT_ERRORS:
636 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
637 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
638 case ERROR_INTERNET_INVALID_CA:
639 if( dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI )
640 return ERROR_CANCELLED;
642 if( dwFlags & ~FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
643 FIXME("%08x contains unsupported flags.\n", dwFlags);
645 return DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_INVCERTDLG ),
646 hWnd, WININET_InvalidCertificateDialog, (LPARAM) ¶ms );
647 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
648 case ERROR_INTERNET_POST_IS_NON_SECURE:
649 FIXME("Need to display dialog for error %d\n", dwError);
650 return ERROR_SUCCESS;
653 return ERROR_NOT_SUPPORTED;