wininet: Moved INTERNET_STATUS_REQUEST_COMPLETE notifications sending to separated...
[wine] / dlls / wininet / dialogs.c
1 /*
2  * Wininet
3  *
4  * Copyright 2003 Mike McCormack for CodeWeavers Inc.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #if defined(__MINGW32__) || defined (_MSC_VER)
25 #include <ws2tcpip.h>
26 #endif
27
28 #include <stdarg.h>
29
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"
40
41 #include "internet.h"
42
43 #include "wine/unicode.h"
44
45 #include "resource.h"
46
47 #define MAX_STRING_LEN 1024
48
49 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
50
51 struct WININET_ErrorDlgParams
52 {
53     HWND       hWnd;
54     HINTERNET  hRequest;
55     DWORD      dwError;
56     DWORD      dwFlags;
57     LPVOID*    lppvData;
58 };
59
60 /***********************************************************************
61  *         WININET_GetProxyServer
62  *
63  *  Determine the name of the proxy server the request is using
64  */
65 static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
66 {
67     http_request_t *request;
68     http_session_t *session = NULL;
69     appinfo_t *hIC = NULL;
70     BOOL ret = FALSE;
71     LPWSTR p;
72
73     request = (http_request_t*) get_handle_object( hRequest );
74     if (NULL == request)
75         return FALSE;
76
77     session = request->session;
78     if (NULL == session)
79         goto done;
80
81     hIC = session->appInfo;
82     if (NULL == hIC)
83         goto done;
84
85     lstrcpynW(szBuf, hIC->proxy, sz);
86
87     /* FIXME: perhaps it would be better to use InternetCrackUrl here */
88     p = strchrW(szBuf, ':');
89     if (p)
90         *p = 0;
91
92     ret = TRUE;
93
94 done:
95     WININET_Release( &request->hdr );
96     return ret;
97 }
98
99 /***********************************************************************
100  *         WININET_GetServer
101  *
102  *  Determine the name of the web server
103  */
104 static BOOL WININET_GetServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
105 {
106     http_request_t *request;
107     http_session_t *session = NULL;
108     BOOL ret = FALSE;
109
110     request = (http_request_t*) get_handle_object( hRequest );
111     if (NULL == request)
112         return FALSE;
113
114     session = request->session;
115     if (NULL == session)
116         goto done;
117
118     lstrcpynW(szBuf, session->hostName, sz);
119
120     ret = TRUE;
121
122 done:
123     WININET_Release( &request->hdr );
124     return ret;
125 }
126
127 /***********************************************************************
128  *         WININET_GetAuthRealm
129  *
130  *  Determine the name of the (basic) Authentication realm
131  */
132 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz, BOOL proxy )
133 {
134     LPWSTR p, q;
135     DWORD index, query;
136     static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
137
138     if (proxy)
139         query = HTTP_QUERY_PROXY_AUTHENTICATE;
140     else
141         query = HTTP_QUERY_WWW_AUTHENTICATE;
142
143     /* extract the Realm from the response and show it */
144     index = 0;
145     if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
146         return FALSE;
147
148     /*
149      * FIXME: maybe we should check that we're
150      * dealing with 'Basic' Authentication
151      */
152     p = strchrW( szBuf, ' ' );
153     if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
154     {
155         ERR("response wrong? (%s)\n", debugstr_w(szBuf));
156         return FALSE;
157     }
158
159     /* remove quotes */
160     p += 7;
161     if( *p == '"' )
162     {
163         p++;
164         q = strrchrW( p, '"' );
165         if( q )
166             *q = 0;
167     }
168     strcpyW( szBuf, p );
169
170     return TRUE;
171 }
172
173 /***********************************************************************
174  *         WININET_GetSetPassword
175  */
176 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer, 
177                                     LPCWSTR szRealm, BOOL bSet )
178 {
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 };
187
188     hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
189     hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
190
191     /* now try fetch the username and password */
192     lstrcpyW( szResource, szServer);
193     lstrcatW( szResource, szbs);
194     lstrcatW( szResource, szRealm);
195
196     /*
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.
201      */
202     if( bSet )
203     {
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 );
211
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 );
216
217         return ( r == WN_SUCCESS );
218     }
219
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;
226
227     p = strchrW( szUserPass, ':' );
228     if( p )
229     {
230         *p = 0;
231         SetWindowTextW( hUserItem, szUserPass );
232         SetWindowTextW( hPassItem, p+1 );
233     }
234
235     return TRUE;
236 }
237
238 /***********************************************************************
239  *         WININET_SetAuthorization
240  */
241 static BOOL WININET_SetAuthorization( HINTERNET hRequest, LPWSTR username,
242                                       LPWSTR password, BOOL proxy )
243 {
244     http_request_t *request;
245     http_session_t *session;
246     BOOL ret = FALSE;
247     LPWSTR p, q;
248
249     request = (http_request_t*) get_handle_object( hRequest );
250     if( !request )
251         return FALSE;
252
253     session = request->session;
254     if (NULL == session ||  session->hdr.htype != WH_HHTTPSESSION)
255     {
256         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
257         goto done;
258     }
259
260     p = heap_strdupW(username);
261     if( !p )
262         goto done;
263
264     q = heap_strdupW(password);
265     if( !q )
266     {
267         heap_free(username);
268         goto done;
269     }
270
271     if (proxy)
272     {
273         appinfo_t *hIC = session->appInfo;
274
275         heap_free(hIC->proxyUsername);
276         hIC->proxyUsername = p;
277
278         heap_free(hIC->proxyPassword);
279         hIC->proxyPassword = q;
280     }
281     else
282     {
283         heap_free(session->userName);
284         session->userName = p;
285
286         heap_free(session->password);
287         session->password = q;
288     }
289
290     ret = TRUE;
291
292 done:
293     WININET_Release( &request->hdr );
294     return ret;
295 }
296
297 /***********************************************************************
298  *         WININET_ProxyPasswordDialog
299  */
300 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
301     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
302 {
303     HWND hitem;
304     struct WININET_ErrorDlgParams *params;
305     WCHAR szRealm[0x80], szServer[0x80];
306
307     if( uMsg == WM_INITDIALOG )
308     {
309         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
310
311         /* save the parameter list */
312         params = (struct WININET_ErrorDlgParams*) lParam;
313         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
314
315         /* extract the Realm from the proxy response and show it */
316         if( WININET_GetAuthRealm( params->hRequest,
317                                   szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
318         {
319             hitem = GetDlgItem( hdlg, IDC_REALM );
320             SetWindowTextW( hitem, szRealm );
321         }
322
323         /* extract the name of the proxy server */
324         if( WININET_GetProxyServer( params->hRequest, 
325                                     szServer, sizeof szServer/sizeof(WCHAR)) )
326         {
327             hitem = GetDlgItem( hdlg, IDC_PROXY );
328             SetWindowTextW( hitem, szServer );
329         }
330
331         WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
332
333         return TRUE;
334     }
335
336     params = (struct WININET_ErrorDlgParams*)
337                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
338
339     switch( uMsg )
340     {
341     case WM_COMMAND:
342         if( wParam == IDOK )
343         {
344             WCHAR username[0x20], password[0x20];
345
346             username[0] = 0;
347             hitem = GetDlgItem( hdlg, IDC_USERNAME );
348             if( hitem )
349                 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
350             
351             password[0] = 0;
352             hitem = GetDlgItem( hdlg, IDC_PASSWORD );
353             if( hitem )
354                 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
355
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)) )
363             {
364                 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
365             }
366             WININET_SetAuthorization( params->hRequest, username, password, TRUE );
367
368             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
369             return TRUE;
370         }
371         if( wParam == IDCANCEL )
372         {
373             EndDialog( hdlg, 0 );
374             return TRUE;
375         }
376         break;
377     }
378     return FALSE;
379 }
380
381 /***********************************************************************
382  *         WININET_PasswordDialog
383  */
384 static INT_PTR WINAPI WININET_PasswordDialog(
385     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
386 {
387     HWND hitem;
388     struct WININET_ErrorDlgParams *params;
389     WCHAR szRealm[0x80], szServer[0x80];
390
391     if( uMsg == WM_INITDIALOG )
392     {
393         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
394
395         /* save the parameter list */
396         params = (struct WININET_ErrorDlgParams*) lParam;
397         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
398
399         /* extract the Realm from the response and show it */
400         if( WININET_GetAuthRealm( params->hRequest,
401                                   szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
402         {
403             hitem = GetDlgItem( hdlg, IDC_REALM );
404             SetWindowTextW( hitem, szRealm );
405         }
406
407         /* extract the name of the server */
408         if( WININET_GetServer( params->hRequest,
409                                szServer, sizeof szServer/sizeof(WCHAR)) )
410         {
411             hitem = GetDlgItem( hdlg, IDC_SERVER );
412             SetWindowTextW( hitem, szServer );
413         }
414
415         WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
416
417         return TRUE;
418     }
419
420     params = (struct WININET_ErrorDlgParams*)
421                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
422
423     switch( uMsg )
424     {
425     case WM_COMMAND:
426         if( wParam == IDOK )
427         {
428             WCHAR username[0x20], password[0x20];
429
430             username[0] = 0;
431             hitem = GetDlgItem( hdlg, IDC_USERNAME );
432             if( hitem )
433                 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
434
435             password[0] = 0;
436             hitem = GetDlgItem( hdlg, IDC_PASSWORD );
437             if( hitem )
438                 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
439
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)) )
447             {
448                 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
449             }
450             WININET_SetAuthorization( params->hRequest, username, password, FALSE );
451
452             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
453             return TRUE;
454         }
455         if( wParam == IDCANCEL )
456         {
457             EndDialog( hdlg, 0 );
458             return TRUE;
459         }
460         break;
461     }
462     return FALSE;
463 }
464
465 /***********************************************************************
466  *         WININET_InvalidCertificateDialog
467  */
468 static INT_PTR WINAPI WININET_InvalidCertificateDialog(
469     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
470 {
471     struct WININET_ErrorDlgParams *params;
472     HWND hitem;
473     WCHAR buf[1024];
474
475     if( uMsg == WM_INITDIALOG )
476     {
477         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
478
479         /* save the parameter list */
480         params = (struct WININET_ErrorDlgParams*) lParam;
481         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
482
483         switch( params->dwError )
484         {
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.
497              */
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';
503         }
504
505         hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
506         SetWindowTextW( hitem, buf );
507
508         return TRUE;
509     }
510
511     params = (struct WININET_ErrorDlgParams*)
512                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
513
514     switch( uMsg )
515     {
516     case WM_COMMAND:
517         if( wParam == IDOK )
518         {
519             BOOL res = TRUE;
520
521             if( params->dwFlags & FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
522             {
523                 DWORD flags, size = sizeof(flags);
524
525                 InternetQueryOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size );
526                 switch( params->dwError )
527                 {
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.
544                      */
545                     break;
546                 }
547                 res = InternetSetOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, size );
548                 if(!res)
549                     WARN("InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed.\n");
550             }
551
552             EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
553             return TRUE;
554         }
555         if( wParam == IDCANCEL )
556         {
557             TRACE("Pressed cancel.\n");
558
559             EndDialog( hdlg, ERROR_CANCELLED );
560             return TRUE;
561         }
562         break;
563     }
564
565     return FALSE;
566 }
567
568 /***********************************************************************
569  *         WININET_GetConnectionStatus
570  */
571 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
572 {
573     WCHAR szStatus[0x20];
574     DWORD sz, index, dwStatus;
575
576     TRACE("%p\n", hRequest );
577
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 );
584
585     TRACE("request %p status = %d\n", hRequest, dwStatus );
586
587     return dwStatus;
588 }
589
590
591 /***********************************************************************
592  *         InternetErrorDlg
593  */
594 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
595                  DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
596 {
597     struct WININET_ErrorDlgParams params;
598     INT dwStatus;
599
600     TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
601
602     if( !hWnd && !(dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI) )
603         return ERROR_INVALID_HANDLE;
604
605     params.hWnd = hWnd;
606     params.hRequest = hRequest;
607     params.dwError = dwError;
608     params.dwFlags = dwFlags;
609     params.lppvData = lppvData;
610
611     switch( dwError )
612     {
613     case ERROR_SUCCESS:
614     case ERROR_INTERNET_INCORRECT_PASSWORD:
615         if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
616             return 0;
617
618         dwStatus = WININET_GetConnectionStatus( hRequest );
619         switch (dwStatus)
620         {
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;
630         }
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;
637
638         if( dwFlags & ~FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
639             FIXME("%08x contains unsupported flags.\n", dwFlags);
640
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;
647     }
648
649     return ERROR_NOT_SUPPORTED;
650 }