1999-08-07 16:32:33 +02:00
|
|
|
/*
|
|
|
|
* MPR Password Cache functions
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* Copyright 1999 Ulrich Weigand
|
2004-04-28 01:30:25 +02:00
|
|
|
* Copyright 2003,2004 Mike McCormack for CodeWeavers
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1999-08-07 16:32:33 +02:00
|
|
|
*/
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2003-07-19 00:59:07 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
1999-08-07 16:32:33 +02:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnetwk.h"
|
2003-07-19 00:59:07 +02:00
|
|
|
#include "winreg.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1999-08-07 16:32:33 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(mpr);
|
1999-08-07 16:32:33 +02:00
|
|
|
|
2003-07-19 00:59:07 +02:00
|
|
|
static const char mpr_key[] = "Software\\Wine\\Wine\\Mpr\\";
|
|
|
|
|
2004-04-28 01:30:25 +02:00
|
|
|
static inline BYTE hex( BYTE x )
|
|
|
|
{
|
|
|
|
if( x <= 9 )
|
|
|
|
return x + '0';
|
|
|
|
return x + 'A' - 10;
|
|
|
|
}
|
|
|
|
|
2009-01-03 05:15:09 +01:00
|
|
|
static inline signed char ctox( CHAR x )
|
2004-04-28 01:30:25 +02:00
|
|
|
{
|
|
|
|
if( ( x >= '0' ) && ( x <= '9' ) )
|
|
|
|
return x - '0';
|
|
|
|
if( ( x >= 'A' ) && ( x <= 'F' ) )
|
|
|
|
return x - 'A' + 10;
|
|
|
|
if( ( x >= 'a' ) && ( x <= 'a' ) )
|
|
|
|
return x - 'a' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-05-27 00:23:10 +02:00
|
|
|
static LPSTR MPR_GetValueName( LPCSTR pbResource, WORD cbResource, BYTE nType )
|
2003-07-19 00:59:07 +02:00
|
|
|
{
|
|
|
|
LPSTR name;
|
2004-04-28 01:30:25 +02:00
|
|
|
DWORD i;
|
2003-07-19 00:59:07 +02:00
|
|
|
|
2004-04-28 01:30:25 +02:00
|
|
|
name = HeapAlloc( GetProcessHeap(), 0, 6+cbResource*2 );
|
2007-09-21 15:08:22 +02:00
|
|
|
if( !name ) return NULL;
|
|
|
|
|
|
|
|
sprintf( name, "X-%02X-", nType );
|
2004-04-28 01:30:25 +02:00
|
|
|
for(i=0; i<cbResource; i++)
|
|
|
|
{
|
|
|
|
name[5+i*2]=hex((pbResource[i]&0xf0)>>4);
|
|
|
|
name[6+i*2]=hex(pbResource[i]&0x0f);
|
|
|
|
}
|
|
|
|
name[5+i*2]=0;
|
2003-07-19 00:59:07 +02:00
|
|
|
TRACE( "Value is %s\n", name );
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2004-04-28 01:30:25 +02:00
|
|
|
|
1999-08-07 16:32:33 +02:00
|
|
|
/**************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* WNetCachePassword [MPR.@] Saves password in cache
|
1999-08-07 16:32:33 +02:00
|
|
|
*
|
1999-11-23 23:27:03 +01:00
|
|
|
* NOTES
|
2008-04-04 21:23:01 +02:00
|
|
|
* Only the parameter count is verified
|
1999-11-23 23:27:03 +01:00
|
|
|
*
|
|
|
|
* ---- everything below this line might be wrong (js) -----
|
1999-08-07 16:32:33 +02:00
|
|
|
* RETURNS
|
|
|
|
* Success: WN_SUCCESS
|
|
|
|
* Failure: WN_ACCESS_DENIED, WN_BAD_PASSWORD, WN_BADVALUE, WN_NET_ERROR,
|
|
|
|
* WN_NOT_SUPPORTED, WN_OUT_OF_MEMORY
|
|
|
|
*/
|
|
|
|
DWORD WINAPI WNetCachePassword(
|
|
|
|
LPSTR pbResource, /* [in] Name of workgroup, computer, or resource */
|
|
|
|
WORD cbResource, /* [in] Size of name */
|
|
|
|
LPSTR pbPassword, /* [in] Buffer containing password */
|
|
|
|
WORD cbPassword, /* [in] Size of password */
|
1999-11-23 23:27:03 +01:00
|
|
|
BYTE nType, /* [in] Type of password to cache */
|
|
|
|
WORD x)
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-08-07 16:32:33 +02:00
|
|
|
{
|
2003-07-19 00:59:07 +02:00
|
|
|
HKEY hkey;
|
|
|
|
DWORD r;
|
|
|
|
LPSTR valname;
|
|
|
|
|
|
|
|
WARN( "(%p(%s), %d, %p(%s), %d, %d, 0x%08x): totally insecure\n",
|
1999-11-23 23:27:03 +01:00
|
|
|
pbResource, debugstr_a(pbResource), cbResource,
|
|
|
|
pbPassword, debugstr_a(pbPassword), cbPassword,
|
|
|
|
nType, x );
|
1999-08-07 16:32:33 +02:00
|
|
|
|
2005-06-13 20:56:00 +02:00
|
|
|
/* @@ Wine registry key: HKCU\Software\Wine\Wine\Mpr */
|
2003-07-19 00:59:07 +02:00
|
|
|
r = RegCreateKeyA( HKEY_CURRENT_USER, mpr_key, &hkey );
|
|
|
|
if( r )
|
|
|
|
return WN_ACCESS_DENIED;
|
|
|
|
|
|
|
|
valname = MPR_GetValueName( pbResource, cbResource, nType );
|
|
|
|
if( valname )
|
|
|
|
{
|
|
|
|
r = RegSetValueExA( hkey, valname, 0, REG_BINARY,
|
2005-07-07 14:08:42 +02:00
|
|
|
(LPBYTE)pbPassword, cbPassword );
|
2003-07-19 00:59:07 +02:00
|
|
|
if( r )
|
2004-04-28 01:30:25 +02:00
|
|
|
r = WN_CANCEL;
|
2003-07-19 00:59:07 +02:00
|
|
|
else
|
|
|
|
r = WN_SUCCESS;
|
|
|
|
HeapFree( GetProcessHeap(), 0, valname );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
r = WN_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
RegCloseKey( hkey );
|
|
|
|
|
|
|
|
return r;
|
1999-08-07 16:32:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* WNetRemoveCachedPassword [MPR.@]
|
1999-08-07 16:32:33 +02:00
|
|
|
*/
|
2004-04-28 01:30:25 +02:00
|
|
|
UINT WINAPI WNetRemoveCachedPassword(
|
|
|
|
LPSTR pbResource, /* [in] resource ID to delete */
|
|
|
|
WORD cbResource, /* [in] number of bytes in the resource ID */
|
|
|
|
BYTE nType ) /* [in] Type of the resource to delete */
|
1999-08-07 16:32:33 +02:00
|
|
|
{
|
2003-07-19 00:59:07 +02:00
|
|
|
HKEY hkey;
|
|
|
|
DWORD r;
|
|
|
|
LPSTR valname;
|
|
|
|
|
|
|
|
WARN( "(%p(%s), %d, %d): totally insecure\n",
|
1999-11-23 23:27:03 +01:00
|
|
|
pbResource, debugstr_a(pbResource), cbResource, nType );
|
1999-08-07 16:32:33 +02:00
|
|
|
|
2005-06-13 20:56:00 +02:00
|
|
|
/* @@ Wine registry key: HKCU\Software\Wine\Wine\Mpr */
|
2003-07-19 00:59:07 +02:00
|
|
|
r = RegCreateKeyA( HKEY_CURRENT_USER, mpr_key, &hkey );
|
|
|
|
if( r )
|
|
|
|
return WN_ACCESS_DENIED;
|
|
|
|
|
|
|
|
valname = MPR_GetValueName( pbResource, cbResource, nType );
|
|
|
|
if( valname )
|
|
|
|
{
|
|
|
|
r = RegDeleteValueA( hkey, valname );
|
|
|
|
if( r )
|
|
|
|
r = WN_ACCESS_DENIED;
|
|
|
|
else
|
|
|
|
r = WN_SUCCESS;
|
|
|
|
HeapFree( GetProcessHeap(), 0, valname );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
r = WN_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return r;
|
1999-08-07 16:32:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* WNetGetCachedPassword [MPR.@] Retrieves password from cache
|
1999-08-07 16:32:33 +02:00
|
|
|
*
|
1999-11-23 23:27:03 +01:00
|
|
|
* NOTES
|
|
|
|
* the stub seems to be wrong:
|
|
|
|
* arg1: ptr 0x40xxxxxx -> (no string)
|
|
|
|
* arg2: len 36
|
|
|
|
* arg3: ptr 0x40xxxxxx -> (no string)
|
|
|
|
* arg4: ptr 0x40xxxxxx -> 0xc8
|
|
|
|
* arg5: type? 4
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
1999-11-23 23:27:03 +01:00
|
|
|
* ---- everything below this line might be wrong (js) -----
|
1999-08-07 16:32:33 +02:00
|
|
|
* RETURNS
|
|
|
|
* Success: WN_SUCCESS
|
2002-06-01 01:06:46 +02:00
|
|
|
* Failure: WN_ACCESS_DENIED, WN_BAD_PASSWORD, WN_BAD_VALUE,
|
1999-08-07 16:32:33 +02:00
|
|
|
* WN_NET_ERROR, WN_NOT_SUPPORTED, WN_OUT_OF_MEMORY
|
|
|
|
*/
|
|
|
|
DWORD WINAPI WNetGetCachedPassword(
|
|
|
|
LPSTR pbResource, /* [in] Name of workgroup, computer, or resource */
|
|
|
|
WORD cbResource, /* [in] Size of name */
|
|
|
|
LPSTR pbPassword, /* [out] Buffer to receive password */
|
|
|
|
LPWORD pcbPassword, /* [out] Receives size of password */
|
|
|
|
BYTE nType) /* [in] Type of password to retrieve */
|
|
|
|
{
|
2003-07-19 00:59:07 +02:00
|
|
|
HKEY hkey;
|
|
|
|
DWORD r, type = 0, sz;
|
|
|
|
LPSTR valname;
|
|
|
|
|
2004-04-28 01:30:25 +02:00
|
|
|
WARN( "(%p(%s), %d, %p, %p, %d): totally insecure\n",
|
1999-11-23 23:27:03 +01:00
|
|
|
pbResource, debugstr_a(pbResource), cbResource,
|
|
|
|
pbPassword, pcbPassword, nType );
|
1999-08-07 16:32:33 +02:00
|
|
|
|
2004-04-28 01:30:25 +02:00
|
|
|
memset( pbPassword, 0, *pcbPassword);
|
|
|
|
|
2005-06-13 20:56:00 +02:00
|
|
|
/* @@ Wine registry key: HKCU\Software\Wine\Wine\Mpr */
|
2003-07-19 00:59:07 +02:00
|
|
|
r = RegCreateKeyA( HKEY_CURRENT_USER, mpr_key, &hkey );
|
|
|
|
if( r )
|
|
|
|
return WN_ACCESS_DENIED;
|
|
|
|
|
|
|
|
valname = MPR_GetValueName( pbResource, cbResource, nType );
|
|
|
|
if( valname )
|
|
|
|
{
|
|
|
|
sz = *pcbPassword;
|
2005-07-07 14:08:42 +02:00
|
|
|
r = RegQueryValueExA( hkey, valname, 0, &type, (LPBYTE)pbPassword, &sz );
|
2003-07-19 00:59:07 +02:00
|
|
|
*pcbPassword = sz;
|
|
|
|
if( r )
|
2004-04-28 01:30:25 +02:00
|
|
|
r = WN_CANCEL;
|
2003-07-19 00:59:07 +02:00
|
|
|
else
|
|
|
|
r = WN_SUCCESS;
|
|
|
|
HeapFree( GetProcessHeap(), 0, valname );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
r = WN_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return r;
|
1999-08-07 16:32:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* WNetEnumCachedPasswords [MPR.@]
|
1999-11-23 23:27:03 +01:00
|
|
|
*
|
|
|
|
* NOTES
|
2008-04-04 21:23:01 +02:00
|
|
|
* The parameter count is verified
|
2003-07-19 00:59:07 +02:00
|
|
|
*
|
|
|
|
* This function is a huge security risk, as virii and such can use
|
|
|
|
* it to grab all the passwords in the cache. It's bad enough to
|
|
|
|
* store the passwords (insecurely).
|
1999-11-23 23:27:03 +01:00
|
|
|
*
|
2004-04-28 01:30:25 +02:00
|
|
|
* bpPrefix and cbPrefix are used to filter the returned passwords
|
|
|
|
* the first cbPrefix bytes of the password resource identifier
|
|
|
|
* should match the same number of bytes in bpPrefix
|
1999-11-23 23:27:03 +01:00
|
|
|
*
|
2004-04-28 01:30:25 +02:00
|
|
|
* RETURNS
|
|
|
|
* Success: WN_SUCCESS (even if no entries were enumerated)
|
|
|
|
* Failure: WN_ACCESS_DENIED, WN_BAD_PASSWORD, WN_BAD_VALUE,
|
|
|
|
* WN_NET_ERROR, WN_NOT_SUPPORTED, WN_OUT_OF_MEMORY
|
1999-08-07 16:32:33 +02:00
|
|
|
*/
|
1999-11-23 23:27:03 +01:00
|
|
|
|
2004-04-28 01:30:25 +02:00
|
|
|
UINT WINAPI WNetEnumCachedPasswords(
|
|
|
|
LPSTR pbPrefix, /* [in] prefix to filter cache entries */
|
|
|
|
WORD cbPrefix, /* [in] number of bytes in Prefix substring */
|
|
|
|
BYTE nType, /* [in] match the Type ID of the entry */
|
|
|
|
ENUMPASSWORDPROC enumPasswordProc, /* [in] callback function */
|
|
|
|
DWORD param) /* [in] parameter passed to enum function */
|
1999-08-07 16:32:33 +02:00
|
|
|
{
|
2004-04-28 01:30:25 +02:00
|
|
|
HKEY hkey;
|
|
|
|
DWORD r, type, val_sz, data_sz, i, j, size;
|
|
|
|
PASSWORD_CACHE_ENTRY *entry;
|
|
|
|
CHAR val[256], prefix[6];
|
|
|
|
|
2006-10-12 21:34:20 +02:00
|
|
|
WARN( "(%s, %d, %d, %p, 0x%08x) totally insecure\n",
|
2004-04-28 01:30:25 +02:00
|
|
|
debugstr_an(pbPrefix,cbPrefix), cbPrefix,
|
|
|
|
nType, enumPasswordProc, param );
|
|
|
|
|
2005-06-13 20:56:00 +02:00
|
|
|
/* @@ Wine registry key: HKCU\Software\Wine\Wine\Mpr */
|
2004-04-28 01:30:25 +02:00
|
|
|
r = RegCreateKeyA( HKEY_CURRENT_USER, mpr_key, &hkey );
|
|
|
|
if( r )
|
|
|
|
return WN_ACCESS_DENIED;
|
|
|
|
|
|
|
|
sprintf(prefix, "X-%02X-", nType );
|
|
|
|
|
|
|
|
for( i=0; ; i++ )
|
|
|
|
{
|
|
|
|
val_sz = sizeof val;
|
|
|
|
data_sz = 0;
|
|
|
|
type = 0;
|
|
|
|
val[0] = 0;
|
|
|
|
r = RegEnumValueA( hkey, i, val, &val_sz, NULL, &type, NULL, &data_sz );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
break;
|
|
|
|
if( type != REG_BINARY )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* check the value is in the format we expect */
|
|
|
|
if( val_sz < sizeof prefix )
|
|
|
|
continue;
|
|
|
|
if( memcmp( prefix, val, 5 ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* decode the value */
|
|
|
|
for(j=5; j<val_sz; j+=2 )
|
|
|
|
{
|
2009-01-03 05:15:09 +01:00
|
|
|
signed char hi = ctox( val[j] ), lo = ctox( val[j+1] );
|
2004-04-28 01:30:25 +02:00
|
|
|
if( ( hi < 0 ) || ( lo < 0 ) )
|
|
|
|
break;
|
|
|
|
val[(j-5)/2] = (hi<<4) | lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the decoded length */
|
|
|
|
val_sz = (j - 5)/2;
|
|
|
|
val[val_sz]=0;
|
|
|
|
if( val_sz < cbPrefix )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* check the prefix matches */
|
|
|
|
if( memcmp(val, pbPrefix, cbPrefix) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* read the value data */
|
2013-03-05 09:36:24 +01:00
|
|
|
size = offsetof( PASSWORD_CACHE_ENTRY, abResource[val_sz + data_sz] );
|
|
|
|
entry = HeapAlloc( GetProcessHeap(), 0, size );
|
2004-04-28 01:30:25 +02:00
|
|
|
memcpy( entry->abResource, val, val_sz );
|
|
|
|
entry->cbEntry = size;
|
|
|
|
entry->cbResource = val_sz;
|
|
|
|
entry->cbPassword = data_sz;
|
|
|
|
entry->iEntry = i;
|
|
|
|
entry->nType = nType;
|
|
|
|
r = RegEnumValueA( hkey, i, NULL, &val_sz, NULL, &type,
|
|
|
|
&entry->abResource[val_sz], &data_sz );
|
|
|
|
if( r == ERROR_SUCCESS )
|
|
|
|
enumPasswordProc( entry, param );
|
|
|
|
HeapFree( GetProcessHeap(), 0, entry );
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey( hkey );
|
1999-08-07 16:32:33 +02:00
|
|
|
|
2004-04-28 01:30:25 +02:00
|
|
|
return WN_SUCCESS;
|
1999-08-07 16:32:33 +02:00
|
|
|
}
|