mshtml: Clean up InvokeEx implementation by using get_dispid_type.
[wine/multimedia.git] / dlls / wininet / dialogs.c
bloba3235a472602b61a6b86cf4bc5f0bbc1fb5b8602
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 #define MAX_STRING_LEN 1024
49 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
51 struct WININET_ErrorDlgParams
53 http_request_t *req;
54 HWND hWnd;
55 DWORD dwError;
56 DWORD dwFlags;
57 LPVOID* lppvData;
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 )
67 LPWSTR p, q;
68 DWORD index, query;
69 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
71 if (proxy)
72 query = HTTP_QUERY_PROXY_AUTHENTICATE;
73 else
74 query = HTTP_QUERY_WWW_AUTHENTICATE;
76 /* extract the Realm from the response and show it */
77 index = 0;
78 if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
79 return FALSE;
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));
89 return FALSE;
92 /* remove quotes */
93 p += 7;
94 if( *p == '"' )
96 p++;
97 q = strrchrW( p, '"' );
98 if( q )
99 *q = 0;
101 strcpyW( szBuf, p );
103 return TRUE;
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];
117 LPWSTR p;
118 HWND hUserItem, hPassItem;
119 DWORD r, dwMagic = 19;
120 UINT r_len, u_len;
121 WORD sz;
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.
139 if( bSet )
141 szUserPass[0] = 0;
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 )
162 return FALSE;
164 p = strchrW( szUserPass, ':' );
165 if( p )
167 *p = 0;
168 SetWindowTextW( hUserItem, szUserPass );
169 SetWindowTextW( hPassItem, p+1 );
172 return TRUE;
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;
182 LPWSTR p, q;
184 p = heap_strdupW(username);
185 if( !p )
186 return FALSE;
188 q = heap_strdupW(password);
189 if( !q )
191 heap_free(username);
192 return FALSE;
195 if (proxy)
197 appinfo_t *hIC = session->appInfo;
199 heap_free(hIC->proxyUsername);
200 hIC->proxyUsername = p;
202 heap_free(hIC->proxyPassword);
203 hIC->proxyPassword = q;
205 else
207 heap_free(session->userName);
208 session->userName = p;
210 heap_free(session->password);
211 session->password = q;
214 return TRUE;
217 /***********************************************************************
218 * WININET_ProxyPasswordDialog
220 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
221 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
223 HWND hitem;
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 );
248 return TRUE;
251 params = (struct WININET_ErrorDlgParams*)
252 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
254 switch( uMsg )
256 case WM_COMMAND:
257 if( wParam == IDOK )
259 WCHAR username[0x20], password[0x20];
261 username[0] = 0;
262 hitem = GetDlgItem( hdlg, IDC_USERNAME );
263 if( hitem )
264 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
266 password[0] = 0;
267 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
268 if( hitem )
269 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
271 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
272 if( hitem &&
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 );
280 return TRUE;
282 if( wParam == IDCANCEL )
284 EndDialog( hdlg, 0 );
285 return TRUE;
287 break;
289 return FALSE;
292 /***********************************************************************
293 * WININET_PasswordDialog
295 static INT_PTR WINAPI WININET_PasswordDialog(
296 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
298 HWND hitem;
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 );
323 return TRUE;
326 params = (struct WININET_ErrorDlgParams*)
327 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
329 switch( uMsg )
331 case WM_COMMAND:
332 if( wParam == IDOK )
334 WCHAR username[0x20], password[0x20];
336 username[0] = 0;
337 hitem = GetDlgItem( hdlg, IDC_USERNAME );
338 if( hitem )
339 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
341 password[0] = 0;
342 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
343 if( hitem )
344 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
346 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
347 if( hitem &&
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 );
357 return TRUE;
359 if( wParam == IDCANCEL )
361 EndDialog( hdlg, 0 );
362 return TRUE;
364 break;
366 return FALSE;
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;
376 HWND hitem;
377 WCHAR buf[1024];
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 );
391 break;
392 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
393 LoadStringW( WININET_hModule, IDS_CERT_DATE_INVALID, buf, 1024 );
394 break;
395 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
396 LoadStringW( WININET_hModule, IDS_CERT_CN_INVALID, buf, 1024 );
397 break;
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 );
403 break;
404 default:
405 FIXME( "No message for error %d\n", params->dwError );
406 buf[0] = '\0';
409 hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
410 SetWindowTextW( hitem, buf );
412 return TRUE;
415 params = (struct WININET_ErrorDlgParams*)
416 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
418 switch( uMsg )
420 case WM_COMMAND:
421 if( wParam == IDOK )
423 BOOL res = TRUE;
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;
435 break;
436 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
437 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
438 break;
439 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
440 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
441 break;
442 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
443 flags |= SECURITY_FLAG_IGNORE_REVOCATION;
444 break;
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;
454 break;
456 /* FIXME: Use helper function */
457 flags |= SECURITY_FLAG_SECURE;
458 req->security_flags |= flags;
459 if(req->netconn)
460 req->netconn->security_flags |= flags;
463 EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
464 return TRUE;
466 if( wParam == IDCANCEL )
468 TRACE("Pressed cancel.\n");
470 EndDialog( hdlg, ERROR_CANCELLED );
471 return TRUE;
473 break;
476 return FALSE;
479 /***********************************************************************
480 * InternetErrorDlg
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;
494 if(hRequest) {
495 req = (http_request_t*)get_handle_object(hRequest);
496 if(!req)
497 return ERROR_INVALID_HANDLE;
498 if(req->hdr.htype != WH_HHTTPREQ)
499 return ERROR_SUCCESS; /* Yes, that was tested */
502 params.req = req;
503 params.hWnd = hWnd;
504 params.dwError = dwError;
505 params.dwFlags = dwFlags;
506 params.lppvData = lppvData;
508 switch( dwError )
510 case ERROR_SUCCESS:
511 case ERROR_INTERNET_INCORRECT_PASSWORD: {
512 if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
513 break;
514 if(!req)
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) &params );
521 break;
522 case HTTP_STATUS_DENIED:
523 res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_AUTHDLG ),
524 hWnd, WININET_PasswordDialog, (LPARAM) &params );
525 break;
526 default:
527 WARN("unhandled status %u\n", req->status_code);
529 break;
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;
538 break;
540 if(!req)
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) &params );
549 break;
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);
553 res = ERROR_SUCCESS;
554 break;
555 default:
556 res = ERROR_NOT_SUPPORTED;
559 if(req)
560 WININET_Release(&req->hdr);
561 return res;