comctl32/tests: Some tests for TVM_GETNEXTITEM and item toggle.
[wine/multimedia.git] / dlls / wininet / dialogs.c
blob5e226c476fd39444f5b27d8519f80df7806e7b93
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 /***********************************************************************
174 * WININET_GetSetPassword
176 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer,
177 LPCWSTR szRealm, BOOL bSet )
179 WCHAR szResource[0x80], szUserPass[0x40];
180 LPWSTR p;
181 HWND hUserItem, hPassItem;
182 DWORD r, dwMagic = 19;
183 UINT r_len, u_len;
184 WORD sz;
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.
202 if( bSet )
204 szUserPass[0] = 0;
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 )
225 return FALSE;
227 p = strchrW( szUserPass, ':' );
228 if( p )
230 *p = 0;
231 SetWindowTextW( hUserItem, szUserPass );
232 SetWindowTextW( hPassItem, p+1 );
235 return TRUE;
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;
246 BOOL ret = FALSE;
247 LPWSTR p, q;
249 request = (http_request_t*) get_handle_object( hRequest );
250 if( !request )
251 return FALSE;
253 session = request->session;
254 if (NULL == session || session->hdr.htype != WH_HHTTPSESSION)
256 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
257 goto done;
260 p = heap_strdupW(username);
261 if( !p )
262 goto done;
264 q = heap_strdupW(password);
265 if( !q )
267 HeapFree(GetProcessHeap(), 0, username);
268 goto done;
271 if (proxy)
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;
281 else
283 HeapFree(GetProcessHeap(), 0, session->userName);
284 session->userName = p;
286 HeapFree(GetProcessHeap(), 0, session->password);
287 session->password = q;
290 ret = TRUE;
292 done:
293 WININET_Release( &request->hdr );
294 return ret;
297 /***********************************************************************
298 * WININET_ProxyPasswordDialog
300 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
301 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
303 HWND hitem;
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 );
333 return TRUE;
336 params = (struct WININET_ErrorDlgParams*)
337 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
339 switch( uMsg )
341 case WM_COMMAND:
342 if( wParam == IDOK )
344 WCHAR username[0x20], password[0x20];
346 username[0] = 0;
347 hitem = GetDlgItem( hdlg, IDC_USERNAME );
348 if( hitem )
349 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
351 password[0] = 0;
352 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
353 if( hitem )
354 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
356 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
357 if( hitem &&
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 );
369 return TRUE;
371 if( wParam == IDCANCEL )
373 EndDialog( hdlg, 0 );
374 return TRUE;
376 break;
378 return FALSE;
381 /***********************************************************************
382 * WININET_PasswordDialog
384 static INT_PTR WINAPI WININET_PasswordDialog(
385 HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
387 HWND hitem;
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 );
417 return TRUE;
420 params = (struct WININET_ErrorDlgParams*)
421 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
423 switch( uMsg )
425 case WM_COMMAND:
426 if( wParam == IDOK )
428 WCHAR username[0x20], password[0x20];
430 username[0] = 0;
431 hitem = GetDlgItem( hdlg, IDC_USERNAME );
432 if( hitem )
433 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
435 password[0] = 0;
436 hitem = GetDlgItem( hdlg, IDC_PASSWORD );
437 if( hitem )
438 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
440 hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
441 if( hitem &&
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 );
453 return TRUE;
455 if( wParam == IDCANCEL )
457 EndDialog( hdlg, 0 );
458 return TRUE;
460 break;
462 return FALSE;
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;
472 HWND hitem;
473 WCHAR buf[1024];
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 );
487 break;
488 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
489 LoadStringW( WININET_hModule, IDS_CERT_DATE_INVALID, buf, 1024 );
490 break;
491 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
492 LoadStringW( WININET_hModule, IDS_CERT_CN_INVALID, buf, 1024 );
493 break;
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 );
499 break;
500 default:
501 FIXME( "No message for error %d\n", params->dwError );
502 buf[0] = '\0';
505 hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
506 SetWindowTextW( hitem, buf );
508 return TRUE;
511 params = (struct WININET_ErrorDlgParams*)
512 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
514 switch( uMsg )
516 case WM_COMMAND:
517 if( wParam == IDOK )
519 BOOL res = TRUE;
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;
530 break;
531 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
532 flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
533 break;
534 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
535 flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
536 break;
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.
545 break;
547 res = InternetSetOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, size );
548 if(!res)
549 WARN("InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed.\n");
552 EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
553 return TRUE;
555 if( wParam == IDCANCEL )
557 TRACE("Pressed cancel.\n");
559 EndDialog( hdlg, ERROR_CANCELLED );
560 return TRUE;
562 break;
565 return FALSE;
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;
579 index = 0;
580 if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
581 szStatus, &sz, &index))
582 return -1;
583 dwStatus = atoiW( szStatus );
585 TRACE("request %p status = %d\n", hRequest, dwStatus );
587 return dwStatus;
591 /***********************************************************************
592 * InternetErrorDlg
594 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
595 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
597 struct WININET_ErrorDlgParams params;
598 INT dwStatus;
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;
605 params.hWnd = hWnd;
606 params.hRequest = hRequest;
607 params.dwError = dwError;
608 params.dwFlags = dwFlags;
609 params.lppvData = lppvData;
611 switch( dwError )
613 case ERROR_SUCCESS:
614 case ERROR_INTERNET_INCORRECT_PASSWORD:
615 if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
616 return 0;
618 dwStatus = WININET_GetConnectionStatus( hRequest );
619 switch (dwStatus)
621 case HTTP_STATUS_PROXY_AUTH_REQ:
622 return DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_PROXYDLG ),
623 hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
624 case HTTP_STATUS_DENIED:
625 return DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_AUTHDLG ),
626 hWnd, WININET_PasswordDialog, (LPARAM) &params );
627 default:
628 WARN("unhandled status %u\n", dwStatus);
629 return 0;
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) &params );
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;