dialogs.c 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Wininet
 *
 * Copyright 2003 Mike McCormack for CodeWeavers Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 25 26 27
#if defined(__MINGW32__) || defined (_MSC_VER)
#include <ws2tcpip.h>
#endif

28 29
#include <stdarg.h>

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "winreg.h"
#include "wininet.h"
#include "winnetwk.h"
#include "wine/debug.h"
#include "winerror.h"
#define NO_SHLWAPI_STREAM
#include "shlwapi.h"

#include "internet.h"

#include "wine/unicode.h"

#include "resource.h"

WINE_DEFAULT_DEBUG_CHANNEL(wininet);

struct WININET_ErrorDlgParams
{
    HWND       hWnd;
    HINTERNET  hRequest;
    DWORD      dwError;
    DWORD      dwFlags;
    LPVOID*    lppvData;
};

/***********************************************************************
 *         WININET_GetProxyServer
 *
 *  Determine the name of the proxy server the request is using
 */
63
static BOOL WININET_GetProxyServer( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
64
{
65
    LPWININETHTTPREQW lpwhr;
66
    LPWININETHTTPSESSIONW lpwhs = NULL;
67 68
    LPWININETAPPINFOW hIC = NULL;
    LPWSTR p;
69

70
    lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
71 72 73
    if (NULL == lpwhr)
	return FALSE;

74
    lpwhs = lpwhr->lpHttpSession;
75 76 77
    if (NULL == lpwhs)
	return FALSE;

78
    hIC = lpwhs->lpAppInfo;
79 80 81
    if (NULL == hIC)
	return FALSE;

82
    lstrcpynW(szBuf, hIC->lpszProxy, sz);
83 84

    /* FIXME: perhaps it would be better to use InternetCrackUrl here */
85
    p = strchrW(szBuf, ':');
86
    if (p)
87 88 89 90 91 92 93 94 95 96
        *p = 0;

    return TRUE;
}

/***********************************************************************
 *         WININET_GetAuthRealm
 *
 *  Determine the name of the (basic) Authentication realm
 */
97
static BOOL WININET_GetAuthRealm( HINTERNET hRequest, LPWSTR szBuf, DWORD sz )
98
{
99
    LPWSTR p, q;
100
    DWORD index;
101
    static const WCHAR szRealm[] = { 'r','e','a','l','m','=',0 };
102 103 104

    /* extract the Realm from the proxy response and show it */
    index = 0;
105
    if( !HttpQueryInfoW( hRequest, HTTP_QUERY_PROXY_AUTHENTICATE,
106 107 108 109 110 111 112
                         szBuf, &sz, &index) )
        return FALSE;

    /*
     * FIXME: maybe we should check that we're
     * dealing with 'Basic' Authentication
     */
113
    p = strchrW( szBuf, ' ' );
114
    if( !p || strncmpW( p+1, szRealm, strlenW(szRealm) ) )
115
    {
116 117
        ERR("proxy response wrong? (%s)\n", debugstr_w(szBuf));
        return FALSE;
118 119
    }

120 121 122 123 124 125 126 127 128 129

    /* remove quotes */
    p += 7;
    if( *p == '"' )
    {
        p++;
        q = strrchrW( p, '"' );
        if( q )
            *q = 0;
    }
130
    strcpyW( szBuf, p );
131 132 133 134 135 136 137

    return TRUE;
}

/***********************************************************************
 *         WININET_GetSetPassword
 */
138 139
static BOOL WININET_GetSetPassword( HWND hdlg, LPCWSTR szServer, 
                                    LPCWSTR szRealm, BOOL bSet )
140
{
141 142
    WCHAR szResource[0x80], szUserPass[0x40];
    LPWSTR p;
143 144
    HWND hUserItem, hPassItem;
    DWORD r, dwMagic = 19;
145
    UINT r_len, u_len;
146
    WORD sz;
147 148
    static const WCHAR szColon[] = { ':',0 };
    static const WCHAR szbs[] = { '/', 0 };
149 150 151 152 153

    hUserItem = GetDlgItem( hdlg, IDC_USERNAME );
    hPassItem = GetDlgItem( hdlg, IDC_PASSWORD );

    /* now try fetch the username and password */
154 155 156
    lstrcpyW( szResource, szServer);
    lstrcatW( szResource, szbs);
    lstrcatW( szResource, szRealm);
157

158 159 160 161 162 163
    /*
     * WNetCachePassword is only concerned with the length
     * of the data stored (which we tell it) and it does
     * not use strlen() internally so we can add WCHAR data
     * instead of ASCII data and get it back the same way.
     */
164 165 166
    if( bSet )
    {
        szUserPass[0] = 0;
167 168 169 170 171 172 173 174 175 176 177
        GetWindowTextW( hUserItem, szUserPass, 
                        (sizeof szUserPass-1)/sizeof(WCHAR) );
        lstrcatW(szUserPass, szColon);
        u_len = strlenW( szUserPass );
        GetWindowTextW( hPassItem, szUserPass+u_len, 
                        (sizeof szUserPass)/sizeof(WCHAR)-u_len );

        r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
        u_len = (strlenW( szUserPass ) + 1)*sizeof(WCHAR);
        r = WNetCachePassword( (CHAR*)szResource, r_len,
                               (CHAR*)szUserPass, u_len, dwMagic, 0 );
178 179 180 181 182

        return ( r == WN_SUCCESS );
    }

    sz = sizeof szUserPass;
183 184 185
    r_len = (strlenW( szResource ) + 1)*sizeof(WCHAR);
    r = WNetGetCachedPassword( (CHAR*)szResource, r_len,
                               (CHAR*)szUserPass, &sz, dwMagic );
186 187 188
    if( r != WN_SUCCESS )
        return FALSE;

189
    p = strchrW( szUserPass, ':' );
190 191 192
    if( p )
    {
        *p = 0;
193 194
        SetWindowTextW( hUserItem, szUserPass );
        SetWindowTextW( hPassItem, p+1 );
195 196 197 198 199 200 201 202 203
    }

    return TRUE;
}

/***********************************************************************
 *         WININET_SetProxyAuthorization
 */
static BOOL WININET_SetProxyAuthorization( HINTERNET hRequest,
204
                                         LPWSTR username, LPWSTR password )
205
{
206
    LPWININETHTTPREQW lpwhr;
207
    LPWININETHTTPSESSIONW lpwhs;
208 209
    LPWININETAPPINFOW hIC;
    LPWSTR p;
210

211 212 213 214
    lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
    if( !lpwhr )
	return FALSE;
        
215
    lpwhs = lpwhr->lpHttpSession;
216 217 218 219 220 221
    if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
    {
        INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
	return FALSE;
    }

222
    hIC = lpwhs->lpAppInfo;
223

224
    p = HeapAlloc( GetProcessHeap(), 0, (strlenW( username ) + 1)*sizeof(WCHAR) );
225 226 227
    if( !p )
        return FALSE;
    
228
    lstrcpyW( p, username );
229 230
    hIC->lpszProxyUsername = p;

231
    p = HeapAlloc( GetProcessHeap(), 0, (strlenW( password ) + 1)*sizeof(WCHAR) );
232 233 234
    if( !p )
        return FALSE;
    
235
    lstrcpyW( p, password );
236 237 238 239 240 241 242 243 244 245 246 247 248
    hIC->lpszProxyPassword = p;

    return TRUE;
}

/***********************************************************************
 *         WININET_ProxyPasswordDialog
 */
static INT_PTR WINAPI WININET_ProxyPasswordDialog(
    HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    HWND hitem;
    struct WININET_ErrorDlgParams *params;
249
    WCHAR szRealm[0x80], szServer[0x80];
250 251 252 253 254 255 256

    if( uMsg == WM_INITDIALOG )
    {
        TRACE("WM_INITDIALOG (%08lx)\n", lParam);

        /* save the parameter list */
        params = (struct WININET_ErrorDlgParams*) lParam;
257
        SetWindowLongPtrW( hdlg, GWLP_USERDATA, lParam );
258 259 260

        /* extract the Realm from the proxy response and show it */
        if( WININET_GetAuthRealm( params->hRequest,
261
                                  szRealm, sizeof szRealm/sizeof(WCHAR)) )
262 263
        {
            hitem = GetDlgItem( hdlg, IDC_REALM );
264
            SetWindowTextW( hitem, szRealm );
265 266 267 268
        }

        /* extract the name of the proxy server */
        if( WININET_GetProxyServer( params->hRequest, 
269
                                    szServer, sizeof szServer/sizeof(WCHAR)) )
270 271
        {
            hitem = GetDlgItem( hdlg, IDC_PROXY );
272
            SetWindowTextW( hitem, szServer );
273 274 275 276 277 278 279 280
        }

        WININET_GetSetPassword( hdlg, szServer, szRealm, FALSE );

        return TRUE;
    }

    params = (struct WININET_ErrorDlgParams*)
281
                 GetWindowLongPtrW( hdlg, GWLP_USERDATA );
282 283 284 285 286 287

    switch( uMsg )
    {
    case WM_COMMAND:
        if( wParam == IDOK )
        {
288
            WCHAR username[0x20], password[0x20];
289 290 291 292

            username[0] = 0;
            hitem = GetDlgItem( hdlg, IDC_USERNAME );
            if( hitem )
293
                GetWindowTextW( hitem, username, sizeof username/sizeof(WCHAR) );
294 295 296 297
            
            password[0] = 0;
            hitem = GetDlgItem( hdlg, IDC_PASSWORD );
            if( hitem )
298
                GetWindowTextW( hitem, password, sizeof password/sizeof(WCHAR) );
299 300 301

            hitem = GetDlgItem( hdlg, IDC_SAVEPASSWORD );
            if( hitem &&
302
                SendMessageW( hitem, BM_GETSTATE, 0, 0 ) &&
303
                WININET_GetAuthRealm( params->hRequest,
304
                                  szRealm, sizeof szRealm/sizeof(WCHAR)) &&
305
                WININET_GetProxyServer( params->hRequest, 
306
                                    szServer, sizeof szServer/sizeof(WCHAR)) )
307 308 309
            {
                WININET_GetSetPassword( hdlg, szServer, szRealm, TRUE );
            }
310
            WININET_SetProxyAuthorization( params->hRequest, username, password );
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

            EndDialog( hdlg, ERROR_INTERNET_FORCE_RETRY );
            return TRUE;
        }
        if( wParam == IDCANCEL )
        {
            EndDialog( hdlg, 0 );
            return TRUE;
        }
        break;
    }
    return FALSE;
}

/***********************************************************************
 *         WININET_GetConnectionStatus
 */
static INT WININET_GetConnectionStatus( HINTERNET hRequest )
{
330
    WCHAR szStatus[0x20];
331 332 333 334
    DWORD sz, index, dwStatus;

    TRACE("%p\n", hRequest );

335
    sz = sizeof szStatus;
336
    index = 0;
337
    if( !HttpQueryInfoW( hRequest, HTTP_QUERY_STATUS_CODE,
338 339
                    szStatus, &sz, &index))
        return -1;
340
    dwStatus = atoiW( szStatus );
341

342
    TRACE("request %p status = %d\n", hRequest, dwStatus );
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

    return dwStatus;
}


/***********************************************************************
 *         InternetErrorDlg
 */
DWORD WINAPI InternetErrorDlg(HWND hWnd, HINTERNET hRequest,
                 DWORD dwError, DWORD dwFlags, LPVOID* lppvData)
{
    struct WININET_ErrorDlgParams params;
    HMODULE hwininet = GetModuleHandleA( "wininet.dll" );
    INT dwStatus;

358
    TRACE("%p %p %d %08x %p\n", hWnd, hRequest, dwError, dwFlags, lppvData);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

    params.hWnd = hWnd;
    params.hRequest = hRequest;
    params.dwError = dwError;
    params.dwFlags = dwFlags;
    params.lppvData = lppvData;

    switch( dwError )
    {
    case ERROR_SUCCESS:
        if( !(dwFlags & FLAGS_ERROR_UI_FILTER_FOR_ERRORS ) )
            return 0;
        dwStatus = WININET_GetConnectionStatus( hRequest );
        if( HTTP_STATUS_PROXY_AUTH_REQ != dwStatus )
            return ERROR_SUCCESS;
        return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
                    hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );

    case ERROR_INTERNET_INCORRECT_PASSWORD:
        return DialogBoxParamW( hwininet, MAKEINTRESOURCEW( IDD_PROXYDLG ),
                    hWnd, WININET_ProxyPasswordDialog, (LPARAM) &params );

    case ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR:
    case ERROR_INTERNET_INVALID_CA:
    case ERROR_INTERNET_POST_IS_NON_SECURE:
    case ERROR_INTERNET_SEC_CERT_CN_INVALID:
    case ERROR_INTERNET_SEC_CERT_DATE_INVALID:
386
        FIXME("Need to display dialog for error %d\n", dwError);
387 388 389 390
        return ERROR_SUCCESS;
    }
    return ERROR_INVALID_PARAMETER;
}