po: Update French translation.
[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 /* 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);
176
177 /***********************************************************************
178  *         WININET_GetSetPassword
179  */
180 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer, 
181                                     LPCWSTR szRealm, BOOL bSet )
182 {
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 };
191
192     hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
193     hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
194
195     /* now try fetch the username and password */
196     lstrcpyW( szResource, szServer);
197     lstrcatW( szResource, szbs);
198     lstrcatW( szResource, szRealm);
199
200     /*
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.
205      */
206     if( bSet )
207     {
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 );
215
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 );
220
221         return ( r == WN_SUCCESS );
222     }
223
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;
230
231     p = strchrW( szUserPass, ':' );
232     if( p )
233     {
234         *p = 0;
235         SetWindowTextW( hUserItem, szUserPass );
236         SetWindowTextW( hPassItem, p+1 );
237     }
238
239     return TRUE;
240 }
241
242 /***********************************************************************
243  *         WININET_SetAuthorization
244  */
245 static BOOL WININET_SetAuthorization( HINTERNET hRequest, LPWSTR username,
246                                       LPWSTR password, BOOL proxy )
247 {
248     http_request_t *request;
249     http_session_t *session;
250     BOOL ret = FALSE;
251     LPWSTR p, q;
252
253     request = (http_request_t*) get_handle_object( hRequest );
254     if( !request )
255         return FALSE;
256
257     session = request->session;
258     if (NULL == session ||  session->hdr.htype != WH_HHTTPSESSION)
259     {
260         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
261         goto done;
262     }
263
264     p = heap_strdupW(username);
265     if( !p )
266         goto done;
267
268     q = heap_strdupW(password);
269     if( !q )
270     {
271         heap_free(username);
272         goto done;
273     }
274
275     if (proxy)
276     {
277         appinfo_t *hIC = session->appInfo;
278
279         heap_free(hIC->proxyUsername);
280         hIC->proxyUsername = p;
281
282         heap_free(hIC->proxyPassword);
283         hIC->proxyPassword = q;
284     }
285     else
286     {
287         heap_free(session->userName);
288         session->userName = p;
289
290         heap_free(session->password);
291         session->password = q;
292     }
293
294     ret = TRUE;
295
296 done:
297     WININET_Release( &request->hdr );
298     return ret;
299 }
300
301 /***********************************************************************
302  *         WININET_ProxyPasswordDialog
303  */
304 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
305     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
306 {
307     HWND hitem;
308     struct WININET_ErrorDlgParams *params;
309     WCHAR szRealm[0x80], szServer[0x80];
310
311     if( uMsg == WM_INITDIALOG )
312     {
313         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
314
315         /* save the parameter list */
316         params = (struct WININET_ErrorDlgParams*) lParam;
317         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
318
319         /* extract the Realm from the proxy response and show it */
320         if( WININET_GetAuthRealm( params->hRequest,
321                                   szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
322         {
323             hitem = GetDlgItem( hdlg, IDC_REALM );
324             SetWindowTextW( hitem, szRealm );
325         }
326
327         /* extract the name of the proxy server */
328         if( WININET_GetProxyServer( params->hRequest, 
329                                     szServer, sizeof szServer/sizeof(WCHAR)) )
330         {
331             hitem = GetDlgItem( hdlg, IDC_PROXY );
332             SetWindowTextW( hitem, szServer );
333         }
334
335         WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
336
337         return TRUE;
338     }
339
340     params = (struct WININET_ErrorDlgParams*)
341                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
342
343     switch( uMsg )
344     {
345     case WM_COMMAND:
346         if( wParam == IDOK )
347         {
348             WCHAR username[0x20], password[0x20];
349
350             username[0] = 0;
351             hitem = GetDlgItem( hdlg, IDC_USERNAME );
352             if( hitem )
353                 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
354             
355             password[0] = 0;
356             hitem = GetDlgItem( hdlg, IDC_PASSWORD );
357             if( hitem )
358                 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
359
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)) )
367             {
368                 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
369             }
370             WININET_SetAuthorization( params->hRequest, username, password, TRUE );
371
372             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
373             return TRUE;
374         }
375         if( wParam == IDCANCEL )
376         {
377             EndDialog( hdlg, 0 );
378             return TRUE;
379         }
380         break;
381     }
382     return FALSE;
383 }
384
385 /***********************************************************************
386  *         WININET_PasswordDialog
387  */
388 static INT_PTR WINAPI WININET_PasswordDialog(
389     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
390 {
391     HWND hitem;
392     struct WININET_ErrorDlgParams *params;
393     WCHAR szRealm[0x80], szServer[0x80];
394
395     if( uMsg == WM_INITDIALOG )
396     {
397         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
398
399         /* save the parameter list */
400         params = (struct WININET_ErrorDlgParams*) lParam;
401         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
402
403         /* extract the Realm from the response and show it */
404         if( WININET_GetAuthRealm( params->hRequest,
405                                   szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
406         {
407             hitem = GetDlgItem( hdlg, IDC_REALM );
408             SetWindowTextW( hitem, szRealm );
409         }
410
411         /* extract the name of the server */
412         if( WININET_GetServer( params->hRequest,
413                                szServer, sizeof szServer/sizeof(WCHAR)) )
414         {
415             hitem = GetDlgItem( hdlg, IDC_SERVER );
416             SetWindowTextW( hitem, szServer );
417         }
418
419         WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
420
421         return TRUE;
422     }
423
424     params = (struct WININET_ErrorDlgParams*)
425                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
426
427     switch( uMsg )
428     {
429     case WM_COMMAND:
430         if( wParam == IDOK )
431         {
432             WCHAR username[0x20], password[0x20];
433
434             username[0] = 0;
435             hitem = GetDlgItem( hdlg, IDC_USERNAME );
436             if( hitem )
437                 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
438
439             password[0] = 0;
440             hitem = GetDlgItem( hdlg, IDC_PASSWORD );
441             if( hitem )
442                 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
443
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)) )
451             {
452                 WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
453             }
454             WININET_SetAuthorization( params->hRequest, username, password, FALSE );
455
456             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
457             return TRUE;
458         }
459         if( wParam == IDCANCEL )
460         {
461             EndDialog( hdlg, 0 );
462             return TRUE;
463         }
464         break;
465     }
466     return FALSE;
467 }
468
469 /***********************************************************************
470  *         WININET_InvalidCertificateDialog
471  */
472 static INT_PTR WINAPI WININET_InvalidCertificateDialog(
473     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
474 {
475     struct WININET_ErrorDlgParams *params;
476     HWND hitem;
477     WCHAR buf[1024];
478
479     if( uMsg == WM_INITDIALOG )
480     {
481         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
482
483         /* save the parameter list */
484         params = (struct WININET_ErrorDlgParams*) lParam;
485         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
486
487         switch( params->dwError )
488         {
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.
501              */
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';
507         }
508
509         hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
510         SetWindowTextW( hitem, buf );
511
512         return TRUE;
513     }
514
515     params = (struct WININET_ErrorDlgParams*)
516                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
517
518     switch( uMsg )
519     {
520     case WM_COMMAND:
521         if( wParam == IDOK )
522         {
523             BOOL res = TRUE;
524
525             if( params->dwFlags & FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
526             {
527                 DWORD flags, size = sizeof(flags);
528
529                 InternetQueryOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size );
530                 switch( params->dwError )
531                 {
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.
548                      */
549                     break;
550                 }
551                 res = InternetSetOptionW( params->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &flags, size );
552                 if(!res)
553                     WARN("InternetSetOption(INTERNET_OPTION_SECURITY_FLAGS) failed.\n");
554             }
555
556             EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
557             return TRUE;
558         }
559         if( wParam == IDCANCEL )
560         {
561             TRACE("Pressed cancel.\n");
562
563             EndDialog( hdlg, ERROR_CANCELLED );
564             return TRUE;
565         }
566         break;
567     }
568
569     return FALSE;
570 }
571
572 /***********************************************************************
573  *         WININET_GetConnectionStatus
574  */
575 static INT WININET_GetConnectionStatus( HINTERNET hRequest )
576 {
577     WCHAR szStatus[0x20];
578     DWORD sz, index, dwStatus;
579
580     TRACE("%p\n", hRequest );
581
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 );
588
589     TRACE("request %p status = %d\n", hRequest, dwStatus );
590
591     return dwStatus;
592 }
593
594
595 /***********************************************************************
596  *         InternetErrorDlg
597  */
598 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
599                  DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
600 {
601     struct WININET_ErrorDlgParams params;
602     INT dwStatus;
603
604     TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
605
606     if( !hWnd && !(dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI) )
607         return ERROR_INVALID_HANDLE;
608
609     params.hWnd = hWnd;
610     params.hRequest = hRequest;
611     params.dwError = dwError;
612     params.dwFlags = dwFlags;
613     params.lppvData = lppvData;
614
615     switch( dwError )
616     {
617     case ERROR_SUCCESS:
618     case ERROR_INTERNET_INCORRECT_PASSWORD:
619         if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
620             return 0;
621
622         dwStatus = WININET_GetConnectionStatus( hRequest );
623         switch (dwStatus)
624         {
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;
634         }
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;
641
642         if( dwFlags & ~FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
643             FIXME("%08x contains unsupported flags.\n", dwFlags);
644
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;
651     }
652
653     return ERROR_NOT_SUPPORTED;
654 }