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 /***********************************************************************
174 * WININET_GetSetPassword
176 static BOOL
WININET_GetSetPassword( HWND hdlg
, LPCWSTR szServer
,
177 LPCWSTR szRealm
, BOOL bSet
)
179 WCHAR szResource
[0x80], szUserPass
[0x40];
181 HWND hUserItem
, hPassItem
;
182 DWORD r
, dwMagic
= 19;
185 static const WCHAR szColon
[] = { ':',0 };
186 static const WCHAR szbs
[] = { '/', 0 };
188 hUserItem
= GetDlgItem( hdlg
, IDC_USERNAME
);
189 hPassItem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
191 /* now try fetch the username and password */
192 lstrcpyW( szResource
, szServer
);
193 lstrcatW( szResource
, szbs
);
194 lstrcatW( szResource
, szRealm
);
197 * WNetCachePassword is only concerned with the length
198 * of the data stored (which we tell it) and it does
199 * not use strlen() internally so we can add WCHAR data
200 * instead of ASCII data and get it back the same way.
205 GetWindowTextW( hUserItem
, szUserPass
,
206 (sizeof szUserPass
-1)/sizeof(WCHAR
) );
207 lstrcatW(szUserPass
, szColon
);
208 u_len
= strlenW( szUserPass
);
209 GetWindowTextW( hPassItem
, szUserPass
+u_len
,
210 (sizeof szUserPass
)/sizeof(WCHAR
)-u_len
);
212 r_len
= (strlenW( szResource
) + 1)*sizeof(WCHAR
);
213 u_len
= (strlenW( szUserPass
) + 1)*sizeof(WCHAR
);
214 r
= WNetCachePassword( (CHAR
*)szResource
, r_len
,
215 (CHAR
*)szUserPass
, u_len
, dwMagic
, 0 );
217 return ( r
== WN_SUCCESS
);
220 sz
= sizeof szUserPass
;
221 r_len
= (strlenW( szResource
) + 1)*sizeof(WCHAR
);
222 r
= WNetGetCachedPassword( (CHAR
*)szResource
, r_len
,
223 (CHAR
*)szUserPass
, &sz
, dwMagic
);
224 if( r
!= WN_SUCCESS
)
227 p
= strchrW( szUserPass
, ':' );
231 SetWindowTextW( hUserItem
, szUserPass
);
232 SetWindowTextW( hPassItem
, p
+1 );
238 /***********************************************************************
239 * WININET_SetAuthorization
241 static BOOL
WININET_SetAuthorization( HINTERNET hRequest
, LPWSTR username
,
242 LPWSTR password
, BOOL proxy
)
244 http_request_t
*request
;
245 http_session_t
*session
;
249 request
= (http_request_t
*) get_handle_object( hRequest
);
253 session
= request
->session
;
254 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
256 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
260 p
= heap_strdupW(username
);
264 q
= heap_strdupW(password
);
267 HeapFree(GetProcessHeap(), 0, username
);
273 appinfo_t
*hIC
= session
->appInfo
;
275 HeapFree(GetProcessHeap(), 0, hIC
->proxyUsername
);
276 hIC
->proxyUsername
= p
;
278 HeapFree(GetProcessHeap(), 0, hIC
->proxyPassword
);
279 hIC
->proxyPassword
= q
;
283 HeapFree(GetProcessHeap(), 0, session
->userName
);
284 session
->userName
= p
;
286 HeapFree(GetProcessHeap(), 0, session
->password
);
287 session
->password
= q
;
293 WININET_Release( &request
->hdr
);
297 /***********************************************************************
298 * WININET_ProxyPasswordDialog
300 static INT_PTR WINAPI
WININET_ProxyPasswordDialog(
301 HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
304 struct WININET_ErrorDlgParams
*params
;
305 WCHAR szRealm
[0x80], szServer
[0x80];
307 if( uMsg
== WM_INITDIALOG
)
309 TRACE("WM_INITDIALOG (%08lx)\n", lParam
);
311 /* save the parameter list */
312 params
= (struct WININET_ErrorDlgParams
*) lParam
;
313 SetWindowLongPtrW( hdlg
, GWLP_USERDATA
, lParam
);
315 /* extract the Realm from the proxy response and show it */
316 if( WININET_GetAuthRealm( params
->hRequest
,
317 szRealm
, sizeof szRealm
/sizeof(WCHAR
), TRUE
) )
319 hitem
= GetDlgItem( hdlg
, IDC_REALM
);
320 SetWindowTextW( hitem
, szRealm
);
323 /* extract the name of the proxy server */
324 if( WININET_GetProxyServer( params
->hRequest
,
325 szServer
, sizeof szServer
/sizeof(WCHAR
)) )
327 hitem
= GetDlgItem( hdlg
, IDC_PROXY
);
328 SetWindowTextW( hitem
, szServer
);
331 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, FALSE
);
336 params
= (struct WININET_ErrorDlgParams
*)
337 GetWindowLongPtrW( hdlg
, GWLP_USERDATA
);
344 WCHAR username
[0x20], password
[0x20];
347 hitem
= GetDlgItem( hdlg
, IDC_USERNAME
);
349 GetWindowTextW( hitem
, username
, sizeof username
/sizeof(WCHAR
) );
352 hitem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
354 GetWindowTextW( hitem
, password
, sizeof password
/sizeof(WCHAR
) );
356 hitem
= GetDlgItem( hdlg
, IDC_SAVEPASSWORD
);
358 SendMessageW( hitem
, BM_GETSTATE
, 0, 0 ) &&
359 WININET_GetAuthRealm( params
->hRequest
,
360 szRealm
, sizeof szRealm
/sizeof(WCHAR
), TRUE
) &&
361 WININET_GetProxyServer( params
->hRequest
,
362 szServer
, sizeof szServer
/sizeof(WCHAR
)) )
364 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, TRUE
);
366 WININET_SetAuthorization( params
->hRequest
, username
, password
, TRUE
);
368 EndDialog( hdlg
, ERROR_INTERNET_FORCE_RETRY
);
371 if( wParam
== IDCANCEL
)
373 EndDialog( hdlg
, 0 );
381 /***********************************************************************
382 * WININET_PasswordDialog
384 static INT_PTR WINAPI
WININET_PasswordDialog(
385 HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
388 struct WININET_ErrorDlgParams
*params
;
389 WCHAR szRealm
[0x80], szServer
[0x80];
391 if( uMsg
== WM_INITDIALOG
)
393 TRACE("WM_INITDIALOG (%08lx)\n", lParam
);
395 /* save the parameter list */
396 params
= (struct WININET_ErrorDlgParams
*) lParam
;
397 SetWindowLongPtrW( hdlg
, GWLP_USERDATA
, lParam
);
399 /* extract the Realm from the response and show it */
400 if( WININET_GetAuthRealm( params
->hRequest
,
401 szRealm
, sizeof szRealm
/sizeof(WCHAR
), FALSE
) )
403 hitem
= GetDlgItem( hdlg
, IDC_REALM
);
404 SetWindowTextW( hitem
, szRealm
);
407 /* extract the name of the server */
408 if( WININET_GetServer( params
->hRequest
,
409 szServer
, sizeof szServer
/sizeof(WCHAR
)) )
411 hitem
= GetDlgItem( hdlg
, IDC_SERVER
);
412 SetWindowTextW( hitem
, szServer
);
415 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, FALSE
);
420 params
= (struct WININET_ErrorDlgParams
*)
421 GetWindowLongPtrW( hdlg
, GWLP_USERDATA
);
428 WCHAR username
[0x20], password
[0x20];
431 hitem
= GetDlgItem( hdlg
, IDC_USERNAME
);
433 GetWindowTextW( hitem
, username
, sizeof username
/sizeof(WCHAR
) );
436 hitem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
438 GetWindowTextW( hitem
, password
, sizeof password
/sizeof(WCHAR
) );
440 hitem
= GetDlgItem( hdlg
, IDC_SAVEPASSWORD
);
442 SendMessageW( hitem
, BM_GETSTATE
, 0, 0 ) &&
443 WININET_GetAuthRealm( params
->hRequest
,
444 szRealm
, sizeof szRealm
/sizeof(WCHAR
), FALSE
) &&
445 WININET_GetServer( params
->hRequest
,
446 szServer
, sizeof szServer
/sizeof(WCHAR
)) )
448 WININET_GetSetPassword( hdlg
, szServer
, szRealm
, TRUE
);
450 WININET_SetAuthorization( params
->hRequest
, username
, password
, FALSE
);
452 EndDialog( hdlg
, ERROR_INTERNET_FORCE_RETRY
);
455 if( wParam
== IDCANCEL
)
457 EndDialog( hdlg
, 0 );
465 /***********************************************************************
466 * WININET_InvalidCertificateDialog
468 static INT_PTR WINAPI
WININET_InvalidCertificateDialog(
469 HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
471 struct WININET_ErrorDlgParams
*params
;
475 if( uMsg
== WM_INITDIALOG
)
477 TRACE("WM_INITDIALOG (%08lx)\n", lParam
);
479 /* save the parameter list */
480 params
= (struct WININET_ErrorDlgParams
*) lParam
;
481 SetWindowLongPtrW( hdlg
, GWLP_USERDATA
, lParam
);
483 switch( params
->dwError
)
485 case ERROR_INTERNET_INVALID_CA
:
486 LoadStringW( WININET_hModule
, IDS_CERT_CA_INVALID
, buf
, 1024 );
488 case ERROR_INTERNET_SEC_CERT_DATE_INVALID
:
489 LoadStringW( WININET_hModule
, IDS_CERT_DATE_INVALID
, buf
, 1024 );
491 case ERROR_INTERNET_SEC_CERT_CN_INVALID
:
492 LoadStringW( WININET_hModule
, IDS_CERT_CN_INVALID
, buf
, 1024 );
494 case ERROR_INTERNET_SEC_CERT_ERRORS
:
495 /* FIXME: We should fetch information about the
496 * certificate here and show all the relevant errors.
498 LoadStringW( WININET_hModule
, IDS_CERT_ERRORS
, buf
, 1024 );
501 FIXME( "No message for error %d\n", params
->dwError
);
505 hitem
= GetDlgItem( hdlg
, IDC_CERT_ERROR
);
506 SetWindowTextW( hitem
, buf
);
511 params
= (struct WININET_ErrorDlgParams
*)
512 GetWindowLongPtrW( hdlg
, GWLP_USERDATA
);
521 if( params
->dwFlags
& FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS
)
523 DWORD flags
, size
= sizeof(flags
);
525 InternetQueryOptionW( params
->hRequest
, INTERNET_OPTION_SECURITY_FLAGS
, &flags
, &size
);
526 switch( params
->dwError
)
528 case ERROR_INTERNET_INVALID_CA
:
529 flags
|= SECURITY_FLAG_IGNORE_UNKNOWN_CA
;
531 case ERROR_INTERNET_SEC_CERT_DATE_INVALID
:
532 flags
|= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
;
534 case ERROR_INTERNET_SEC_CERT_CN_INVALID
:
535 flags
|= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
;
537 case ERROR_INTERNET_SEC_CERT_ERRORS
:
538 FIXME("Should only add ignore flags as needed.\n");
539 flags
|= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
540 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
541 SECURITY_FLAG_IGNORE_UNKNOWN_CA
;
542 /* FIXME: ERROR_INTERNET_SEC_CERT_ERRORS also
543 * seems to set the corresponding DLG_* flags.
547 res
= InternetSetOptionW( params
->hRequest
, INTERNET_OPTION_SECURITY_FLAGS
, &flags
, size
);
549 WARN("InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed.\n");
552 EndDialog( hdlg
, res
? ERROR_SUCCESS
: ERROR_NOT_SUPPORTED
);
555 if( wParam
== IDCANCEL
)
557 TRACE("Pressed cancel.\n");
559 EndDialog( hdlg
, ERROR_CANCELLED
);
568 /***********************************************************************
569 * WININET_GetConnectionStatus
571 static INT
WININET_GetConnectionStatus( HINTERNET hRequest
)
573 WCHAR szStatus
[0x20];
574 DWORD sz
, index
, dwStatus
;
576 TRACE("%p\n", hRequest
);
578 sz
= sizeof szStatus
;
580 if( !HttpQueryInfoW( hRequest
, HTTP_QUERY_STATUS_CODE
,
581 szStatus
, &sz
, &index
))
583 dwStatus
= atoiW( szStatus
);
585 TRACE("request %p status = %d\n", hRequest
, dwStatus
);
591 /***********************************************************************
594 DWORD WINAPI
InternetErrorDlg(HWND hWnd
, HINTERNET hRequest
,
595 DWORD dwError
, DWORD dwFlags
, LPVOID
* lppvData
)
597 struct WININET_ErrorDlgParams params
;
600 TRACE("%p %p %d %08x %p\n", hWnd
, hRequest
, dwError
, dwFlags
, lppvData
);
602 if( !hWnd
&& !(dwFlags
& FLAGS_ERROR_UI_FLAGS_NO_UI
) )
603 return ERROR_INVALID_HANDLE
;
606 params
.hRequest
= hRequest
;
607 params
.dwError
= dwError
;
608 params
.dwFlags
= dwFlags
;
609 params
.lppvData
= lppvData
;
614 case ERROR_INTERNET_INCORRECT_PASSWORD
:
615 if( !dwError
&& !(dwFlags
& FLAGS_ERROR_UI_FILTER_FOR_ERRORS
) )
618 dwStatus
= WININET_GetConnectionStatus( hRequest
);
621 case HTTP_STATUS_PROXY_AUTH_REQ
:
622 return DialogBoxParamW( WININET_hModule
, MAKEINTRESOURCEW( IDD_PROXYDLG
),
623 hWnd
, WININET_ProxyPasswordDialog
, (LPARAM
) ¶ms
);
624 case HTTP_STATUS_DENIED
:
625 return DialogBoxParamW( WININET_hModule
, MAKEINTRESOURCEW( IDD_AUTHDLG
),
626 hWnd
, WININET_PasswordDialog
, (LPARAM
) ¶ms
);
628 WARN("unhandled status %u\n", dwStatus
);
631 case ERROR_INTERNET_SEC_CERT_ERRORS
:
632 case ERROR_INTERNET_SEC_CERT_CN_INVALID
:
633 case ERROR_INTERNET_SEC_CERT_DATE_INVALID
:
634 case ERROR_INTERNET_INVALID_CA
:
635 if( dwFlags
& FLAGS_ERROR_UI_FLAGS_NO_UI
)
636 return ERROR_CANCELLED
;
638 if( dwFlags
& ~FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS
)
639 FIXME("%08x contains unsupported flags.\n", dwFlags
);
641 return DialogBoxParamW( WININET_hModule
, MAKEINTRESOURCEW( IDD_INVCERTDLG
),
642 hWnd
, WININET_InvalidCertificateDialog
, (LPARAM
) ¶ms
);
643 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR
:
644 case ERROR_INTERNET_POST_IS_NON_SECURE
:
645 FIXME("Need to display dialog for error %d\n", dwError
);
646 return ERROR_SUCCESS
;
649 return ERROR_NOT_SUPPORTED
;