wininet: Added basic persistent cookie tests.
[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     http_request_t *req;
54     HWND       hWnd;
55     DWORD      dwError;
56     DWORD      dwFlags;
57     LPVOID*    lppvData;
58 };
59
60 /***********************************************************************
61  *         WININET_GetAuthRealm
62  *
63  *  Determine the name of the (basic) Authentication realm
64  */
65 static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz, BOOL proxy )
66 {
67     LPWSTR p, q;
68     DWORD index, query;
69     static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
70
71     if (proxy)
72         query = HTTP_QUERY_PROXY_AUTHENTICATE;
73     else
74         query = HTTP_QUERY_WWW_AUTHENTICATE;
75
76     /* extract the Realm from the response and show it */
77     index = 0;
78     if( !HttpQueryInfoW( hRequest, query, szBuf, &sz, &index) )
79         return FALSE;
80
81     /*
82      * FIXME: maybe we should check that we're
83      * dealing with 'Basic' Authentication
84      */
85     p = strchrW( szBuf, ' ' );
86     if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
87     {
88         ERR("response wrong? (%s)\n", debugstr_w(szBuf));
89         return FALSE;
90     }
91
92     /* remove quotes */
93     p += 7;
94     if( *p == '"' )
95     {
96         p++;
97         q = strrchrW( p, '"' );
98         if( q )
99             *q = 0;
100     }
101     strcpyW( szBuf, p );
102
103     return TRUE;
104 }
105
106 /* These two are not defined in the public headers */
107 extern DWORD WINAPI WNetCachePassword(LPSTR,WORD,LPSTR,WORD,BYTE,WORD);
108 extern DWORD WINAPI WNetGetCachedPassword(LPSTR,WORD,LPSTR,LPWORD,BYTE);
109
110 /***********************************************************************
111  *         WININET_GetSetPassword
112  */
113 static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer, 
114                                     LPCWSTR szRealm, BOOL bSet )
115 {
116     WCHAR szResource[0x80], szUserPass[0x40];
117     LPWSTR p;
118     HWND hUserItem, hPassItem;
119     DWORD r, dwMagic = 19;
120     UINT r_len, u_len;
121     WORD sz;
122     static const WCHAR szColon[] = { ':',0 };
123     static const WCHAR szbs[] = { '/', 0 };
124
125     hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
126     hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );
127
128     /* now try fetch the username and password */
129     lstrcpyW( szResource, szServer);
130     lstrcatW( szResource, szbs);
131     lstrcatW( szResource, szRealm);
132
133     /*
134      * WNetCachePassword is only concerned with the length
135      * of the data stored (which we tell it) and it does
136      * not use strlen() internally so we can add WCHAR data
137      * instead of ASCII data and get it back the same way.
138      */
139     if( bSet )
140     {
141         szUserPass[0] = 0;
142         GetWindowTextW( hUserItem, szUserPass, 
143                         (sizeof szUserPass-1)/sizeof(WCHAR) );
144         lstrcatW(szUserPass, szColon);
145         u_len = strlenW( szUserPass );
146         GetWindowTextW( hPassItem, szUserPass+u_len, 
147                         (sizeof szUserPass)/sizeof(WCHAR)-u_len );
148
149         r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
150         u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
151         r = WNetCachePassword( (CHAR*)szResource, r_len,
152                                (CHAR*)szUserPass, u_len, dwMagic, 0 );
153
154         return ( r == WN_SUCCESS );
155     }
156
157     sz = sizeof szUserPass;
158     r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
159     r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
160                                (CHAR*)szUserPass, &sz, dwMagic );
161     if( r != WN_SUCCESS )
162         return FALSE;
163
164     p = strchrW( szUserPass, ':' );
165     if( p )
166     {
167         *p = 0;
168         SetWindowTextW( hUserItem, szUserPass );
169         SetWindowTextW( hPassItem, p+1 );
170     }
171
172     return TRUE;
173 }
174
175 /***********************************************************************
176  *         WININET_SetAuthorization
177  */
178 static BOOL WININET_SetAuthorization( http_request_t *request, LPWSTR username,
179                                       LPWSTR password, BOOL proxy )
180 {
181     http_session_t *session = request->session;
182     LPWSTR p, q;
183
184     p = heap_strdupW(username);
185     if( !p )
186         return FALSE;
187
188     q = heap_strdupW(password);
189     if( !q )
190     {
191         heap_free(username);
192         return FALSE;
193     }
194
195     if (proxy)
196     {
197         appinfo_t *hIC = session->appInfo;
198
199         heap_free(hIC->proxyUsername);
200         hIC->proxyUsername = p;
201
202         heap_free(hIC->proxyPassword);
203         hIC->proxyPassword = q;
204     }
205     else
206     {
207         heap_free(session->userName);
208         session->userName = p;
209
210         heap_free(session->password);
211         session->password = q;
212     }
213
214     return TRUE;
215 }
216
217 /***********************************************************************
218  *         WININET_ProxyPasswordDialog
219  */
220 static INT_PTR WINAPI WININET_ProxyPasswordDialog(
221     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
222 {
223     HWND hitem;
224     struct WININET_ErrorDlgParams *params;
225     WCHAR szRealm[0x80], szServer[0x80];
226
227     if( uMsg == WM_INITDIALOG )
228     {
229         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
230
231         /* save the parameter list */
232         params = (struct WININET_ErrorDlgParams*) lParam;
233         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
234
235         /* extract the Realm from the proxy response and show it */
236         if( WININET_GetAuthRealm( params->req->hdr.hInternet,
237                                   szRealm, sizeof szRealm/sizeof(WCHAR), TRUE ) )
238         {
239             hitem = GetDlgItem( hdlg, IDC_REALM );
240             SetWindowTextW( hitem, szRealm );
241         }
242
243         hitem = GetDlgItem( hdlg, IDC_PROXY );
244         SetWindowTextW( hitem, params->req->session->appInfo->proxy );
245
246         WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
247
248         return TRUE;
249     }
250
251     params = (struct WININET_ErrorDlgParams*)
252                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
253
254     switch( uMsg )
255     {
256     case WM_COMMAND:
257         if( wParam == IDOK )
258         {
259             WCHAR username[0x20], password[0x20];
260
261             username[0] = 0;
262             hitem = GetDlgItem( hdlg, IDC_USERNAME );
263             if( hitem )
264                 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
265             
266             password[0] = 0;
267             hitem = GetDlgItem( hdlg, IDC_PASSWORD );
268             if( hitem )
269                 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
270
271             hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
272             if( hitem &&
273                 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
274                 WININET_GetAuthRealm( params->req->hdr.hInternet,
275                                       szRealm, sizeof szRealm/sizeof(WCHAR), TRUE) )
276                 WININET_GetSetPassword( hdlg, params->req->session->appInfo->proxy, szRealm, TRUE );
277             WININET_SetAuthorization( params->req, username, password, TRUE );
278
279             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
280             return TRUE;
281         }
282         if( wParam == IDCANCEL )
283         {
284             EndDialog( hdlg, 0 );
285             return TRUE;
286         }
287         break;
288     }
289     return FALSE;
290 }
291
292 /***********************************************************************
293  *         WININET_PasswordDialog
294  */
295 static INT_PTR WINAPI WININET_PasswordDialog(
296     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
297 {
298     HWND hitem;
299     struct WININET_ErrorDlgParams *params;
300     WCHAR szRealm[0x80], szServer[0x80];
301
302     if( uMsg == WM_INITDIALOG )
303     {
304         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
305
306         /* save the parameter list */
307         params = (struct WININET_ErrorDlgParams*) lParam;
308         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
309
310         /* extract the Realm from the response and show it */
311         if( WININET_GetAuthRealm( params->req->hdr.hInternet,
312                                   szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ) )
313         {
314             hitem = GetDlgItem( hdlg, IDC_REALM );
315             SetWindowTextW( hitem, szRealm );
316         }
317
318         hitem = GetDlgItem( hdlg, IDC_SERVER );
319         SetWindowTextW( hitem, params->req->session->hostName );
320
321         WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );
322
323         return TRUE;
324     }
325
326     params = (struct WININET_ErrorDlgParams*)
327                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
328
329     switch( uMsg )
330     {
331     case WM_COMMAND:
332         if( wParam == IDOK )
333         {
334             WCHAR username[0x20], password[0x20];
335
336             username[0] = 0;
337             hitem = GetDlgItem( hdlg, IDC_USERNAME );
338             if( hitem )
339                 GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
340
341             password[0] = 0;
342             hitem = GetDlgItem( hdlg, IDC_PASSWORD );
343             if( hitem )
344                 GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
345
346             hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
347             if( hitem &&
348                 SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
349                 WININET_GetAuthRealm( params->req->hdr.hInternet,
350                                       szRealm, sizeof szRealm/sizeof(WCHAR), FALSE ))
351             {
352                 WININET_GetSetPassword( hdlg, params->req->session->hostName, szRealm, TRUE );
353             }
354             WININET_SetAuthorization( params->req, username, password, FALSE );
355
356             EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
357             return TRUE;
358         }
359         if( wParam == IDCANCEL )
360         {
361             EndDialog( hdlg, 0 );
362             return TRUE;
363         }
364         break;
365     }
366     return FALSE;
367 }
368
369 /***********************************************************************
370  *         WININET_InvalidCertificateDialog
371  */
372 static INT_PTR WINAPI WININET_InvalidCertificateDialog(
373     HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
374 {
375     struct WININET_ErrorDlgParams *params;
376     HWND hitem;
377     WCHAR buf[1024];
378
379     if( uMsg == WM_INITDIALOG )
380     {
381         TRACE("WM_INITDIALOG (%08lx)\n", lParam);
382
383         /* save the parameter list */
384         params = (struct WININET_ErrorDlgParams*) lParam;
385         SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
386
387         switch( params->dwError )
388         {
389         case ERROR_INTERNET_INVALID_CA:
390             LoadStringW( WININET_hModule, IDS_CERT_CA_INVALID, buf, 1024 );
391             break;
392         case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
393             LoadStringW( WININET_hModule, IDS_CERT_DATE_INVALID, buf, 1024 );
394             break;
395         case ERROR_INTERNET_SEC_CERT_CN_INVALID:
396             LoadStringW( WININET_hModule, IDS_CERT_CN_INVALID, buf, 1024 );
397             break;
398         case ERROR_INTERNET_SEC_CERT_ERRORS:
399             /* FIXME: We should fetch information about the
400              * certificate here and show all the relevant errors.
401              */
402             LoadStringW( WININET_hModule, IDS_CERT_ERRORS, buf, 1024 );
403             break;
404         default:
405             FIXME( "No message for error %d\n", params->dwError );
406             buf[0] = '\0';
407         }
408
409         hitem = GetDlgItem( hdlg, IDC_CERT_ERROR );
410         SetWindowTextW( hitem, buf );
411
412         return TRUE;
413     }
414
415     params = (struct WININET_ErrorDlgParams*)
416                  GetWindowLongPtrW( hdlg, GWLP_USERDATA );
417
418     switch( uMsg )
419     {
420     case WM_COMMAND:
421         if( wParam == IDOK )
422         {
423             BOOL res = TRUE;
424
425             if( params->dwFlags & FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
426             {
427                 http_request_t *req = params->req;
428                 DWORD flags, size = sizeof(flags);
429
430                 InternetQueryOptionW( req->hdr.hInternet, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size );
431                 switch( params->dwError )
432                 {
433                 case ERROR_INTERNET_INVALID_CA:
434                     flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
435                     break;
436                 case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
437                     flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
438                     break;
439                 case ERROR_INTERNET_SEC_CERT_CN_INVALID:
440                     flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
441                     break;
442                 case ERROR_INTERNET_SEC_CERT_REV_FAILED:
443                     flags |= SECURITY_FLAG_IGNORE_REVOCATION;
444                     break;
445                 case ERROR_INTERNET_SEC_CERT_ERRORS:
446                     if(flags & _SECURITY_FLAG_CERT_REV_FAILED)
447                         flags |= SECURITY_FLAG_IGNORE_REVOCATION;
448                     if(flags & _SECURITY_FLAG_CERT_INVALID_CA)
449                         flags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
450                     if(flags & _SECURITY_FLAG_CERT_INVALID_CN)
451                         flags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
452                     if(flags & _SECURITY_FLAG_CERT_INVALID_DATE)
453                         flags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
454                     break;
455                 }
456                 /* FIXME: Use helper function */
457                 flags |= SECURITY_FLAG_SECURE;
458                 req->security_flags |= flags;
459                 if(req->netconn)
460                     req->netconn->security_flags |= flags;
461             }
462
463             EndDialog( hdlg, res ? ERROR_SUCCESS : ERROR_NOT_SUPPORTED );
464             return TRUE;
465         }
466         if( wParam == IDCANCEL )
467         {
468             TRACE("Pressed cancel.\n");
469
470             EndDialog( hdlg, ERROR_CANCELLED );
471             return TRUE;
472         }
473         break;
474     }
475
476     return FALSE;
477 }
478
479 /***********************************************************************
480  *         InternetErrorDlg
481  */
482 DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
483                  DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
484 {
485     struct WININET_ErrorDlgParams params;
486     http_request_t *req = NULL;
487     DWORD res = ERROR_SUCCESS;
488
489     TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
490
491     if( !hWnd && !(dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI) )
492         return ERROR_INVALID_HANDLE;
493
494     if(hRequest) {
495         req = (http_request_t*)get_handle_object(hRequest);
496         if(!req)
497             return ERROR_INVALID_HANDLE;
498         if(req->hdr.htype != WH_HHTTPREQ)
499             return ERROR_SUCCESS; /* Yes, that was tested */
500     }
501
502     params.req = req;
503     params.hWnd = hWnd;
504     params.dwError = dwError;
505     params.dwFlags = dwFlags;
506     params.lppvData = lppvData;
507
508     switch( dwError )
509     {
510     case ERROR_SUCCESS:
511     case ERROR_INTERNET_INCORRECT_PASSWORD: {
512         if( !dwError && !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
513             break;
514         if(!req)
515             return ERROR_INVALID_HANDLE;
516
517         switch(req->status_code) {
518         case HTTP_STATUS_PROXY_AUTH_REQ:
519             res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_PROXYDLG ),
520                                    hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );
521             break;
522         case HTTP_STATUS_DENIED:
523             res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_AUTHDLG ),
524                                     hWnd, WININET_PasswordDialog, (LPARAM) &params );
525             break;
526         default:
527             WARN("unhandled status %u\n", req->status_code);
528         }
529         break;
530     }
531     case ERROR_INTERNET_SEC_CERT_ERRORS:
532     case ERROR_INTERNET_SEC_CERT_CN_INVALID:
533     case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
534     case ERROR_INTERNET_INVALID_CA:
535     case ERROR_INTERNET_SEC_CERT_REV_FAILED:
536         if( dwFlags & FLAGS_ERROR_UI_FLAGS_NO_UI ) {
537             res = ERROR_CANCELLED;
538             break;
539         }
540         if(!req)
541             return ERROR_INVALID_HANDLE;
542
543
544         if( dwFlags & ~FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS )
545             FIXME("%08x contains unsupported flags.\n", dwFlags);
546
547         res = DialogBoxParamW( WININET_hModule, MAKEINTRESOURCEW( IDD_INVCERTDLG ),
548                                hWnd, WININET_InvalidCertificateDialog, (LPARAM) &params );
549         break;
550     case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
551     case ERROR_INTERNET_POST_IS_NON_SECURE:
552         FIXME("Need to display dialog for error %d\n", dwError);
553         res = ERROR_SUCCESS;
554         break;
555     default:
556         res = ERROR_NOT_SUPPORTED;
557     }
558
559     if(req)
560         WININET_Release(&req->hdr);
561     return res;
562 }