comctl32: Fix a typo in comment.
[wine.git] / dlls / wininet / dialogs.c
blob938daa8b8f76be228a355f937e5ed67a6c6373a3
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 "ws2tcpip.h"
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "wininet.h"
30 #include "winnetwk.h"
31 #include "wine/debug.h"
32 #include "winerror.h"
33 #define NO_SHLWAPI_STREAM
34 #include "shlwapi.h"
35 #include "cryptuiapi.h"
37 #include "internet.h"
39 #include "wine/unicode.h"
41 #include "resource.h"
43 #define MAX_STRING_LEN 1024
45 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
47 struct WININET_ErrorDlgParams
49 http_request_t *req;
50 HWND hWnd;
51 DWORD dwError;
52 DWORD dwFlags;
53 LPVOID* lppvData;
56 /***********************************************************************
57 * WININET_GetAuthRealm
59 * Determine the name of the (basic) Authentication realm
61 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz, BOOL proxy )
63 LPWSTR p, q;
64 DWORD index, query;
65 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
67 if (proxy)
68 query = HTTP_QUERY_PROXY_AUTHENTICATE;
69 else
70 query = HTTP_QUERY_WWW_AUTHENTICATE;
72 /* extract the Realm from the response and show it */
73 index = 0;
74 if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
75 return FALSE;
78 * FIXME: maybe we should check that we're
79 * dealing with 'Basic' Authentication
81 p = strchrW( szBuf, ' ' );
82 if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
84 ERR("response wrong? (%s)\n", debugstr_w(szBuf));
85 return FALSE;
88 /* remove quotes */
89 p += 7;
90 if( *p == '"' )
92 p++;
93 q = strrchrW( p, '"' );
94 if( q )
95 *q = 0;
97 strcpyW( szBuf, p );
99 return TRUE;
102 /* These two are not defined in the public headers */
103 extern DWORD WINAPI WNetCachePassword(LPSTR,WORD,LPSTR,WORD,BYTE,WORD);
104 extern DWORD WINAPI WNetGetCachedPassword(LPSTR,WORD,LPSTR,LPWORD,BYTE);
106 /***********************************************************************
107 * WININET_GetSetPassword
109 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
110 LPCWSTR szRealm, BOOL bSet )
112 WCHAR szResource[0x80], szUserPass[0x40];
113 LPWSTR p;
114 HWND hUserItem, hPassItem;
115 DWORD r, dwMagic = 19;
116 UINT r_len, u_len;
117 WORD sz;
118 static const WCHAR szColon[] = { ':',0 };
119 static const WCHAR szbs[] = { '/', 0 };
121 hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
122 hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
124 /* now try fetch the username and password */
125 lstrcpyW( szResource, szServer);
126 lstrcatW( szResource, szbs);
127 lstrcatW( szResource, szRealm);
130 * WNetCachePassword is only concerned with the length
131 * of the data stored (which we tell it) and it does
132 * not use strlen() internally so we can add WCHAR data
133 * instead of ASCII data and get it back the same way.
135 if( bSet )
137 szUserPass[0] = 0;
138 GetWindowTextW( hUserItem, szUserPass,
139 (sizeof szUserPass-1)/sizeof(WCHAR) );
140 lstrcatW(szUserPass, szColon);
141 u_len = strlenW( szUserPass );
142 GetWindowTextW( hPassItem, szUserPass+u_len,
143 (sizeof szUserPass)/sizeof(WCHAR)-u_len );
145 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
146 u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
147 r = WNetCachePassword( (CHAR*)szResource, r_len,
148 (CHAR*)szUserPass, u_len, dwMagic, 0 );
150 return ( r == WN_SUCCESS );
153 sz = sizeof szUserPass;
154 r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
155 r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
156 (CHAR*)szUserPass, &sz, dwMagic );
157 if( r != WN_SUCCESS )
158 return FALSE;
160 p = strchrW( szUserPass, ':' );
161 if( p )
163 *p = 0;
164 SetWindowTextW( hUserItem, szUserPass );
165 SetWindowTextW( hPassItem, p+1 );
168 return TRUE;
171 /***********************************************************************
172 * WININET_SetAuthorization
174 static BOOL WININET_SetAuthorization( http_request_t *request, LPWSTR username,
175 LPWSTR password, BOOL proxy )
177 http_session_t *session = request->session;
178 LPWSTR p, q;
180 p = heap_strdupW(username);
181 if( !p )
182 return FALSE;
184 q = heap_strdupW(password);
185 if( !q )
187 heap_free(p);
188 return FALSE;
191 if (proxy)
193 appinfo_t *hIC = session->appInfo;
195 heap_free(hIC->proxyUsername);
196 hIC->proxyUsername = p;
198 heap_free(hIC->proxyPassword);
199 hIC->proxyPassword = q;
201 else
203 heap_free(session->userName);
204 session->userName = p;
206 heap_free(session->password);
207 session->password = q;
210 return TRUE;
213 /***********************************************************************
214 * WININET_ProxyPasswordDialog
216 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
217 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
219 HWND hitem;
220 struct WININET_ErrorDlgParams *params;
221 WCHAR szRealm[0x80], szServer[0x80];
223 if( uMsg == WM_INITDIALOG )
225 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
227 /* save the parameter list */
228 params = (struct WININET_ErrorDlgParams*) lParam;
229 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
231 /* extract the Realm from the proxy response and show it */
232 if( WININET_GetAuthRealm( params->req->hdr.hInternet,
233 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
235 hitem = GetDlgItem( hdlg, IDC_REALM );
236 SetWindowTextW( hitem, szRealm );
239 hitem = GetDlgItem( hdlg, IDC_PROXY );
240 SetWindowTextW( hitem, params->req->session->appInfo->proxy );
242 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
244 return TRUE;
247 params = (struct WININET_ErrorDlgParams*)
248 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
250 switch( uMsg )
252 case WM_COMMAND:
253 if( wParam == IDOK )
255 WCHAR username[0x20], password[0x20];
257 username[0] = 0;
258 hitem = GetDlgItem( hdlg, IDC_USERNAME );
259 if( hitem )
260 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
262 password[0] = 0;
263 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
264 if( hitem )
265 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
267 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
268 if( hitem &&
269 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
270 WININET_GetAuthRealm( params->req->hdr.hInternet,
271 szRealm, sizeof szRealm/sizeof(WCHAR), TRUE) )
272 WININET_GetSetPassword( hdlg, params->req->session->appInfo->proxy, szRealm, TRUE );
273 WININET_SetAuthorization( params->req, username, password, TRUE );
275 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
276 return TRUE;
278 if( wParam == IDCANCEL )
280 EndDialog( hdlg, 0 );
281 return TRUE;
283 break;
285 return FALSE;
288 /***********************************************************************
289 * WININET_PasswordDialog
291 static INT_PTR WINAPI WININET_PasswordDialog(
292 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
294 HWND hitem;
295 struct WININET_ErrorDlgParams *params;
296 WCHAR szRealm[0x80], szServer[0x80];
298 if( uMsg == WM_INITDIALOG )
300 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
302 /* save the parameter list */
303 params = (struct WININET_ErrorDlgParams*) lParam;
304 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
306 /* extract the Realm from the response and show it */
307 if( WININET_GetAuthRealm( params->req->hdr.hInternet,
308 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
310 hitem = GetDlgItem( hdlg, IDC_REALM );
311 SetWindowTextW( hitem, szRealm );
314 hitem = GetDlgItem( hdlg, IDC_SERVER );
315 SetWindowTextW( hitem, params->req->session->hostName );
317 WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
319 return TRUE;
322 params = (struct WININET_ErrorDlgParams*)
323 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
325 switch( uMsg )
327 case WM_COMMAND:
328 if( wParam == IDOK )
330 WCHAR username[0x20], password[0x20];
332 username[0] = 0;
333 hitem = GetDlgItem( hdlg, IDC_USERNAME );
334 if( hitem )
335 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
337 password[0] = 0;
338 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
339 if( hitem )
340 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
342 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
343 if( hitem &&
344 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
345 WININET_GetAuthRealm( params->req->hdr.hInternet,
346 szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ))
348 WININET_GetSetPassword( hdlg, params->req->session->hostName, szRealm, TRUE );
350 WININET_SetAuthorization( params->req, username, password, FALSE );
352 EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
353 return TRUE;
355 if( wParam == IDCANCEL )
357 EndDialog( hdlg, 0 );
358 return TRUE;
360 break;
362 return FALSE;
365 /***********************************************************************
366 * WININET_InvalidCertificateDialog
368 static INT_PTR WINAPI WININET_InvalidCertificateDialog(
369 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
371 struct WININET_ErrorDlgParams *params;
372 HWND hitem;
373 WCHAR buf[1024];
375 if( uMsg == WM_INITDIALOG )
377 TRACE("WM_INITDIALOG (%08lx)\n", lParam);
379 /* save the parameter list */
380 params = (struct WININET_ErrorDlgParams*) lParam;
381 SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
383 switch( params->dwError )
385 case ERROR_INTERNET_INVALID_CA:
386 LoadStringW( WININET_hModule, IDS_CERT_CA_INVALID, buf, 1024 );
387 break;
388 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
389 LoadStringW( WININET_hModule, IDS_CERT_DATE_INVALID, buf, 1024 );
390 break;
391 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
392 LoadStringW( WININET_hModule, IDS_CERT_CN_INVALID, buf, 1024 );
393 break;
394 case ERROR_INTERNET_SEC_CERT_ERRORS:
395 /* FIXME: We should fetch information about the
396 * certificate here and show all the relevant errors.
398 LoadStringW( WININET_hModule, IDS_CERT_ERRORS, buf, 1024 );
399 break;
400 default:
401 FIXME( "No message for error %d\n", params->dwError );
402 buf[0] = '\0';
405 hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
406 SetWindowTextW( hitem, buf );
408 return TRUE;
411 params = (struct WININET_ErrorDlgParams*)
412 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
414 switch( uMsg )
416 case WM_COMMAND:
417 if( wParam == IDOK )
419 if( params->dwFlags & FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
421 http_request_t *req = params->req;
422 DWORD flags, size = sizeof(flags);
424 InternetQueryOptionW( req->hdr.hInternet, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size );
425 switch( params->dwError )
427 case ERROR_INTERNET_INVALID_CA:
428 flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
429 break;
430 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
431 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
432 break;
433 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
434 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
435 break;
436 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
437 flags |= SECURITY_FLAG_IGNORE_REVOCATION;
438 break;
439 case ERROR_INTERNET_SEC_CERT_ERRORS:
440 if(flags & _SECURITY_FLAG_CERT_REV_FAILED)
441 flags |= SECURITY_FLAG_IGNORE_REVOCATION;
442 if(flags & _SECURITY_FLAG_CERT_INVALID_CA)
443 flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
444 if(flags & _SECURITY_FLAG_CERT_INVALID_CN)
445 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
446 if(flags & _SECURITY_FLAG_CERT_INVALID_DATE)
447 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
448 break;
450 /* FIXME: Use helper function */
451 flags |= SECURITY_FLAG_SECURE;
452 req->security_flags |= flags;
453 if(is_valid_netconn(req->netconn))
454 req->netconn->security_flags |= flags;
457 EndDialog( hdlg, ERROR_SUCCESS );
458 return TRUE;
460 if( wParam == IDCANCEL )
462 TRACE("Pressed cancel.\n");
464 EndDialog( hdlg, ERROR_CANCELLED );
465 return TRUE;
467 break;
470 return FALSE;
473 /***********************************************************************
474 * InternetErrorDlg
476 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
477 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
479 struct WININET_ErrorDlgParams params;
480 http_request_t *req = NULL;
481 DWORD res = ERROR_SUCCESS;
483 TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
485 if( !hWnd && !(dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI) )
486 return ERROR_INVALID_HANDLE;
488 if(hRequest) {
489 req = (http_request_t*)get_handle_object(hRequest);
490 if(!req)
491 return ERROR_INVALID_HANDLE;
492 if(req->hdr.htype != WH_HHTTPREQ)
493 return ERROR_SUCCESS; /* Yes, that was tested */
496 params.req = req;
497 params.hWnd = hWnd;
498 params.dwError = dwError;
499 params.dwFlags = dwFlags;
500 params.lppvData = lppvData;
502 switch( dwError )
504 case ERROR_SUCCESS:
505 case ERROR_INTERNET_INCORRECT_PASSWORD: {
506 if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
507 break;
508 if(!req)
509 return ERROR_INVALID_HANDLE;
511 switch(req->status_code) {
512 case HTTP_STATUS_PROXY_AUTH_REQ:
513 res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_PROXYDLG ),
514 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
515 break;
516 case HTTP_STATUS_DENIED:
517 res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_AUTHDLG ),
518 hWnd, WININET_PasswordDialog, (LPARAM) &params );
519 break;
520 default:
521 WARN("unhandled status %u\n", req->status_code);
523 break;
525 case ERROR_INTERNET_SEC_CERT_ERRORS:
526 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
527 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
528 case ERROR_INTERNET_INVALID_CA:
529 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
530 if( dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI ) {
531 res = ERROR_CANCELLED;
532 break;
534 if(!req)
535 return ERROR_INVALID_HANDLE;
538 if( dwFlags & ~FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
539 FIXME("%08x contains unsupported flags.\n", dwFlags);
541 res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_INVCERTDLG ),
542 hWnd, WININET_InvalidCertificateDialog, (LPARAM) &params );
543 break;
544 case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
545 case ERROR_INTERNET_POST_IS_NON_SECURE:
546 FIXME("Need to display dialog for error %d\n", dwError);
547 res = ERROR_SUCCESS;
548 break;
549 default:
550 res = ERROR_NOT_SUPPORTED;
553 if(req)
554 WININET_Release(&req->hdr);
555 return res;
558 /***********************************************************************
559 * InternetShowSecurityInfoByURLA (@)
561 BOOL WINAPI InternetShowSecurityInfoByURLA(LPCSTR url, HWND window)
563 FIXME("stub: %s %p\n", url, window);
564 return FALSE;
567 /***********************************************************************
568 * InternetShowSecurityInfoByURLW (@)
570 BOOL WINAPI InternetShowSecurityInfoByURLW(LPCWSTR url, HWND window)
572 FIXME("stub: %s %p\n", debugstr_w(url), window);
573 return FALSE;
576 /***********************************************************************
577 * ShowX509EncodedCertificate (@)
579 DWORD WINAPI ShowX509EncodedCertificate(HWND parent, LPBYTE cert, DWORD len)
581 PCCERT_CONTEXT certContext = CertCreateCertificateContext(X509_ASN_ENCODING,
582 cert, len);
583 DWORD ret;
585 if (certContext)
587 CRYPTUI_VIEWCERTIFICATE_STRUCTW view;
589 memset(&view, 0, sizeof(view));
590 view.hwndParent = parent;
591 view.pCertContext = certContext;
592 if (CryptUIDlgViewCertificateW(&view, NULL))
593 ret = ERROR_SUCCESS;
594 else
595 ret = GetLastError();
596 CertFreeCertificateContext(certContext);
598 else
599 ret = GetLastError();
600 return ret;