regedit: An English (United States) spelling fix.
[wine/multimedia.git] / dlls / wininet / dialogs.c
blob9674718bce7e5e9c668632c0ea47630a4de95d6a
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 HWND hWnd;
54 HINTERNET hRequest;
55 DWORD dwError;
56 DWORD dwFlags;
57 LPVOID* lppvData;
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;
70 BOOL ret = FALSE;
71 LPWSTR p;
73 request = (http_request_t*) get_handle_object( hRequest );
74 if (NULL == request)
75 return FALSE;
77 session = request->session;
78 if (NULL == session)
79 goto done;
81 hIC = session->appInfo;
82 if (NULL == hIC)
83 goto done;
85 lstrcpynW(szBuf, hIC->proxy, sz);
87 /* FIXME: perhaps it would be better to use InternetCrackUrl here */
88 p = strchrW(szBuf, ':');
89 if (p)
90 *p = 0;
92 ret = TRUE;
94 done:
95 WININET_Release( &request->hdr );
96 return ret;
99 /***********************************************************************
100 * WININET_GetServer
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;
108 BOOL ret = FALSE;
110 request = (http_request_t*) get_handle_object( hRequest );
111 if (NULL == request)
112 return FALSE;
114 session = request->session;
115 if (NULL == session)
116 goto done;
118 lstrcpynW(szBuf, session->hostName, sz);
120 ret = TRUE;
122 done:
123 WININET_Release( &request->hdr );
124 return ret;
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 )
134 LPWSTR p, q;
135 DWORD index, query;
136 static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
138 if (proxy)
139 query = HTTP_QUERY_PROXY_AUTHENTICATE;
140 else
141 query = HTTP_QUERY_WWW_AUTHENTICATE;
143 /* extract the Realm from the response and show it */
144 index = 0;
145 if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
146 return FALSE;
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));
156 return FALSE;
159 /* remove quotes */
160 p += 7;
161 if( *p == '"' )
163 p++;
164 q = strrchrW( p, '"' );
165 if( q )
166 *q = 0;
168 strcpyW( szBuf, p );
170 return TRUE;
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];
184 LPWSTR p;
185 HWND hUserItem, hPassItem;
186 DWORD r, dwMagic = 19;
187 UINT r_len, u_len;
188 WORD sz;
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.
206 if( bSet )
208 szUserPass[0] = 0;
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 )
229 return FALSE;
231 p = strchrW( szUserPass, ':' );
232 if( p )
234 *p = 0;
235 SetWindowTextW( hUserItem, szUserPass );
236 SetWindowTextW( hPassItem, p+1 );
239 return TRUE;
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;
250 BOOL ret = FALSE;
251 LPWSTR p, q;
253 request = (http_request_t*) get_handle_object( hRequest );
254 if( !request )
255 return FALSE;
257 session = request->session;
258 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
260 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
261 goto done;
264 p = heap_strdupW(username);
265 if( !p )
266 goto done;
268 q = heap_strdupW(password);
269 if( !q )
271 heap_free(username);
272 goto done;
275 if (proxy)
277 appinfo_t *hIC = session->appInfo;
279 heap_free(hIC->proxyUsername);
280 hIC->proxyUsername = p;
282 heap_free(hIC->proxyPassword);
283 hIC->proxyPassword = q;
285 else
287 heap_free(session->userName);
288 session->userName = p;
290 heap_free(session->password);
291 session->password = q;
294 ret = TRUE;
296 done:
297 WININET_Release( &request->hdr );
298 return ret;
301 /***********************************************************************
302 * WININET_ProxyPasswordDialog
304 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
305 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
307 HWND hitem;
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 );
337 return TRUE;
340 params = (struct WININET_ErrorDlgParams*)
341 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
343 switch( uMsg )
345 case WM_COMMAND:
346 if( wParam == IDOK )
348 WCHAR username[0x20], password[0x20];
350 username[0] = 0;
351 hitem = GetDlgItem( hdlg, IDC_USERNAME );
352 if( hitem )
353 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
355 password[0] = 0;
356 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
357 if( hitem )
358 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
360 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
361 if( hitem &&
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 );
373 return TRUE;
375 if( wParam == IDCANCEL )
377 EndDialog( hdlg, 0 );
378 return TRUE;
380 break;
382 return FALSE;
385 /***********************************************************************
386 * WININET_PasswordDialog
388 static INT_PTR WINAPI WININET_PasswordDialog(
389 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
391 HWND hitem;
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 );
421 return TRUE;
424 params = (struct WININET_ErrorDlgParams*)
425 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
427 switch( uMsg )
429 case WM_COMMAND:
430 if( wParam == IDOK )
432 WCHAR username[0x20], password[0x20];
434 username[0] = 0;
435 hitem = GetDlgItem( hdlg, IDC_USERNAME );
436 if( hitem )
437 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
439 password[0] = 0;
440 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
441 if( hitem )
442 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
444 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
445 if( hitem &&
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 );
457 return TRUE;
459 if( wParam == IDCANCEL )
461 EndDialog( hdlg, 0 );
462 return TRUE;
464 break;
466 return FALSE;
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;
476 HWND hitem;
477 WCHAR buf[1024];
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 );
491 break;
492 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
493 LoadStringW( WININET_hModule, IDS_CERT_DATE_INVALID, buf, 1024 );
494 break;
495 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
496 LoadStringW( WININET_hModule, IDS_CERT_CN_INVALID, buf, 1024 );
497 break;
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 );
503 break;
504 default:
505 FIXME( "No message for error %d\n", params->dwError );
506 buf[0] = '\0';
509 hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
510 SetWindowTextW( hitem, buf );
512 return TRUE;
515 params = (struct WININET_ErrorDlgParams*)
516 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
518 switch( uMsg )
520 case WM_COMMAND:
521 if( wParam == IDOK )
523 BOOL res = TRUE;
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;
534 break;
535 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
536 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
537 break;
538 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
539 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
540 break;
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.
549 break;
551 res = InternetSetOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, size );
552 if(!res)
553 WARN("InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed.\n");
556 EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
557 return TRUE;
559 if( wParam == IDCANCEL )
561 TRACE("Pressed cancel.\n");
563 EndDialog( hdlg, ERROR_CANCELLED );
564 return TRUE;
566 break;
569 return FALSE;
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;
583 index = 0;
584 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
585 szStatus, &sz, &index))
586 return -1;
587 dwStatus = atoiW( szStatus );
589 TRACE("request %p status = %d\n", hRequest, dwStatus );
591 return dwStatus;
595 /***********************************************************************
596 * InternetErrorDlg
598 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
599 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
601 struct WININET_ErrorDlgParams params;
602 INT dwStatus;
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;
609 params.hWnd = hWnd;
610 params.hRequest = hRequest;
611 params.dwError = dwError;
612 params.dwFlags = dwFlags;
613 params.lppvData = lppvData;
615 switch( dwError )
617 case ERROR_SUCCESS:
618 case ERROR_INTERNET_INCORRECT_PASSWORD:
619 if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
620 return 0;
622 dwStatus = WININET_GetConnectionStatus( hRequest );
623 switch (dwStatus)
625 case HTTP_STATUS_PROXY_AUTH_REQ:
626 return DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_PROXYDLG ),
627 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
628 case HTTP_STATUS_DENIED:
629 return DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_AUTHDLG ),
630 hWnd, WININET_PasswordDialog, (LPARAM) &params );
631 default:
632 WARN("unhandled status %u\n", dwStatus);
633 return 0;
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) &params );
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;