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 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
49 struct WININET_ErrorDlgParams
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
;
71 lpwhr
= (http_request_t
*) WININET_GetObject( hRequest
);
75 lpwhs
= lpwhr
->lpHttpSession
;
79 hIC
= lpwhs
->lpAppInfo
;
83 lstrcpynW(szBuf
, hIC
->lpszProxy
, sz
);
85 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
86 p
= strchrW(szBuf
, ':');
93 WININET_Release( &lpwhr
->hdr
);
97 /***********************************************************************
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
;
108 lpwhr
= (http_request_t
*) WININET_GetObject( hRequest
);
112 lpwhs
= lpwhr
->lpHttpSession
;
116 lstrcpynW(szBuf
, lpwhs
->lpszHostName
, sz
);
121 WININET_Release( &lpwhr
->hdr
);
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
)
134 static const WCHAR szRealm
[] = { 'r','e','a','l','m','=',0 };
137 query
= HTTP_QUERY_PROXY_AUTHENTICATE
;
139 query
= HTTP_QUERY_WWW_AUTHENTICATE
;
141 /* extract the Realm from the response and show it */
143 if( !HttpQueryInfoW( hRequest
, query
, szBuf
, &sz
, &index
) )
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
));
162 q
= strrchrW( p
, '"' );
171 /***********************************************************************
172 * WININET_GetSetPassword
174 static BOOL
WININET_GetSetPassword( HWND hdlg
, LPCWSTR szServer
,
175 LPCWSTR szRealm
, BOOL bSet
)
177 WCHAR szResource
[0x80], szUserPass
[0x40];
179 HWND hUserItem
, hPassItem
;
180 DWORD r
, dwMagic
= 19;
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.
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
)
225 p
= strchrW( szUserPass
, ':' );
229 SetWindowTextW( hUserItem
, szUserPass
);
230 SetWindowTextW( hPassItem
, p
+1 );
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
;
247 lpwhr
= (http_request_t
*) WININET_GetObject( hRequest
);
251 lpwhs
= lpwhr
->lpHttpSession
;
252 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
254 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
258 p
= heap_strdupW(username
);
262 q
= heap_strdupW(password
);
265 HeapFree(GetProcessHeap(), 0, username
);
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
;
281 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
282 lpwhs
->lpszUserName
= p
;
284 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszPassword
);
285 lpwhs
->lpszPassword
= q
;
291 WININET_Release( &lpwhr
->hdr
);
295 /***********************************************************************
296 * WININET_ProxyPasswordDialog
298 static INT_PTR WINAPI
WININET_ProxyPasswordDialog(
299 HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
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
);
334 params
= (struct WININET_ErrorDlgParams
*)
335 GetWindowLongPtrW( hdlg
, GWLP_USERDATA
);
342 WCHAR username
[0x20], password
[0x20];
345 hitem
= GetDlgItem( hdlg
, IDC_USERNAME
);
347 GetWindowTextW( hitem
, username
, sizeof username
/sizeof(WCHAR
) );
350 hitem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
352 GetWindowTextW( hitem
, password
, sizeof password
/sizeof(WCHAR
) );
354 hitem
= GetDlgItem( hdlg
, IDC_SAVEPASSWORD
);
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
);
369 if( wParam
== IDCANCEL
)
371 EndDialog( hdlg
, 0 );
379 /***********************************************************************
380 * WININET_PasswordDialog
382 static INT_PTR WINAPI
WININET_PasswordDialog(
383 HWND hdlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
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
);
418 params
= (struct WININET_ErrorDlgParams
*)
419 GetWindowLongPtrW( hdlg
, GWLP_USERDATA
);
426 WCHAR username
[0x20], password
[0x20];
429 hitem
= GetDlgItem( hdlg
, IDC_USERNAME
);
431 GetWindowTextW( hitem
, username
, sizeof username
/sizeof(WCHAR
) );
434 hitem
= GetDlgItem( hdlg
, IDC_PASSWORD
);
436 GetWindowTextW( hitem
, password
, sizeof password
/sizeof(WCHAR
) );
438 hitem
= GetDlgItem( hdlg
, IDC_SAVEPASSWORD
);
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
);
453 if( wParam
== IDCANCEL
)
455 EndDialog( hdlg
, 0 );
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
;
475 if( !HttpQueryInfoW( hRequest
, HTTP_QUERY_STATUS_CODE
,
476 szStatus
, &sz
, &index
))
478 dwStatus
= atoiW( szStatus
);
480 TRACE("request %p status = %d\n", hRequest
, dwStatus
);
486 /***********************************************************************
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" );
496 TRACE("%p %p %d %08x %p\n", hWnd
, hRequest
, dwError
, dwFlags
, lppvData
);
499 params
.hRequest
= hRequest
;
500 params
.dwError
= dwError
;
501 params
.dwFlags
= dwFlags
;
502 params
.lppvData
= lppvData
;
507 case ERROR_INTERNET_INCORRECT_PASSWORD
:
508 if( !dwError
&& !(dwFlags
& FLAGS_ERROR_UI_FILTER_FOR_ERRORS
) )
511 dwStatus
= WININET_GetConnectionStatus( hRequest
);
514 case HTTP_STATUS_PROXY_AUTH_REQ
:
515 return DialogBoxParamW( hwininet
, MAKEINTRESOURCEW( IDD_PROXYDLG
),
516 hWnd
, WININET_ProxyPasswordDialog
, (LPARAM
) ¶ms
);
517 case HTTP_STATUS_DENIED
:
518 return DialogBoxParamW( hwininet
, MAKEINTRESOURCEW( IDD_AUTHDLG
),
519 hWnd
, WININET_PasswordDialog
, (LPARAM
) ¶ms
);
521 WARN("unhandled status %u\n", dwStatus
);
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
;