Release 20040408.
[wine.git] / dlls / mpr / pwcache.c
blob056e8d452fdc455e6396d9920a57588d5d3906f4
1 /*
2 * MPR Password Cache functions
4 * Copyright 1999 Ulrich Weigand
5 * Copyright 2003 Mike McCormack for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnetwk.h"
28 #include "winreg.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(mpr);
33 static const char mpr_key[] = "Software\\Wine\\Wine\\Mpr\\";
35 static LPSTR MPR_GetValueName( LPSTR pbResource, WORD cbResource, BYTE nType )
37 LPSTR name;
38 DWORD i, x = 0;
40 /* just a hash so the value name doesn't get too large */
41 for( i=0; i<cbResource; i++ )
42 x = ((x<<7) | (x >> 25)) ^ toupper(pbResource[i]);
44 name = HeapAlloc( GetProcessHeap(), 0, 0x10 );
45 if( name )
46 sprintf( name, "I-%08lX-%02X", x, nType );
47 TRACE( "Value is %s\n", name );
48 return name;
51 /**************************************************************************
52 * WNetCachePassword [MPR.@] Saves password in cache
54 * NOTES
55 * only the parameter count is verifyed
57 * ---- everything below this line might be wrong (js) -----
58 * RETURNS
59 * Success: WN_SUCCESS
60 * Failure: WN_ACCESS_DENIED, WN_BAD_PASSWORD, WN_BADVALUE, WN_NET_ERROR,
61 * WN_NOT_SUPPORTED, WN_OUT_OF_MEMORY
63 DWORD WINAPI WNetCachePassword(
64 LPSTR pbResource, /* [in] Name of workgroup, computer, or resource */
65 WORD cbResource, /* [in] Size of name */
66 LPSTR pbPassword, /* [in] Buffer containing password */
67 WORD cbPassword, /* [in] Size of password */
68 BYTE nType, /* [in] Type of password to cache */
69 WORD x)
72 HKEY hkey;
73 DWORD r;
74 LPSTR valname;
76 WARN( "(%p(%s), %d, %p(%s), %d, %d, 0x%08x): totally insecure\n",
77 pbResource, debugstr_a(pbResource), cbResource,
78 pbPassword, debugstr_a(pbPassword), cbPassword,
79 nType, x );
81 r = RegCreateKeyA( HKEY_CURRENT_USER, mpr_key, &hkey );
82 if( r )
83 return WN_ACCESS_DENIED;
85 valname = MPR_GetValueName( pbResource, cbResource, nType );
86 if( valname )
88 r = RegSetValueExA( hkey, valname, 0, REG_BINARY,
89 pbPassword, cbPassword );
90 if( r )
91 r = WN_ACCESS_DENIED;
92 else
93 r = WN_SUCCESS;
94 HeapFree( GetProcessHeap(), 0, valname );
96 else
97 r = WN_OUT_OF_MEMORY;
99 RegCloseKey( hkey );
101 return r;
104 /*****************************************************************
105 * WNetRemoveCachedPassword [MPR.@]
107 UINT WINAPI WNetRemoveCachedPassword( LPSTR pbResource, WORD cbResource,
108 BYTE nType )
110 HKEY hkey;
111 DWORD r;
112 LPSTR valname;
114 WARN( "(%p(%s), %d, %d): totally insecure\n",
115 pbResource, debugstr_a(pbResource), cbResource, nType );
117 r = RegCreateKeyA( HKEY_CURRENT_USER, mpr_key, &hkey );
118 if( r )
119 return WN_ACCESS_DENIED;
121 valname = MPR_GetValueName( pbResource, cbResource, nType );
122 if( valname )
124 r = RegDeleteValueA( hkey, valname );
125 if( r )
126 r = WN_ACCESS_DENIED;
127 else
128 r = WN_SUCCESS;
129 HeapFree( GetProcessHeap(), 0, valname );
131 else
132 r = WN_OUT_OF_MEMORY;
134 return r;
137 /*****************************************************************
138 * WNetGetCachedPassword [MPR.@] Retrieves password from cache
140 * NOTES
141 * the stub seems to be wrong:
142 * arg1: ptr 0x40xxxxxx -> (no string)
143 * arg2: len 36
144 * arg3: ptr 0x40xxxxxx -> (no string)
145 * arg4: ptr 0x40xxxxxx -> 0xc8
146 * arg5: type? 4
148 * ---- everything below this line might be wrong (js) -----
149 * RETURNS
150 * Success: WN_SUCCESS
151 * Failure: WN_ACCESS_DENIED, WN_BAD_PASSWORD, WN_BAD_VALUE,
152 * WN_NET_ERROR, WN_NOT_SUPPORTED, WN_OUT_OF_MEMORY
154 DWORD WINAPI WNetGetCachedPassword(
155 LPSTR pbResource, /* [in] Name of workgroup, computer, or resource */
156 WORD cbResource, /* [in] Size of name */
157 LPSTR pbPassword, /* [out] Buffer to receive password */
158 LPWORD pcbPassword, /* [out] Receives size of password */
159 BYTE nType) /* [in] Type of password to retrieve */
161 HKEY hkey;
162 DWORD r, type = 0, sz;
163 LPSTR valname;
165 WARN( "(%p(%s), %d, %p, %p, %d): stub\n",
166 pbResource, debugstr_a(pbResource), cbResource,
167 pbPassword, pcbPassword, nType );
169 r = RegCreateKeyA( HKEY_CURRENT_USER, mpr_key, &hkey );
170 if( r )
171 return WN_ACCESS_DENIED;
173 valname = MPR_GetValueName( pbResource, cbResource, nType );
174 if( valname )
176 sz = *pcbPassword;
177 r = RegQueryValueExA( hkey, valname, 0, &type, pbPassword, &sz );
178 *pcbPassword = sz;
179 if( r )
180 r = WN_ACCESS_DENIED;
181 else
182 r = WN_SUCCESS;
183 HeapFree( GetProcessHeap(), 0, valname );
185 else
186 r = WN_OUT_OF_MEMORY;
188 return r;
191 /*******************************************************************
192 * WNetEnumCachedPasswords [MPR.@]
194 * NOTES
195 * the parameter count is verifyed
197 * This function is a huge security risk, as virii and such can use
198 * it to grab all the passwords in the cache. It's bad enough to
199 * store the passwords (insecurely).
201 * observed values:
202 * arg1 ptr 0x40xxxxxx -> (no string)
203 * arg2 int 32
204 * arg3 type? 4
205 * arg4 enumPasswordProc (verifyed)
206 * arg5 ptr 0x40xxxxxx -> 0x0
208 * ---- everything below this line might be wrong (js) -----
211 UINT WINAPI WNetEnumCachedPasswords( LPSTR pbPrefix, WORD cbPrefix,
212 BYTE nType, ENUMPASSWORDPROC enumPasswordProc, DWORD x)
214 WARN( "(%p(%s), %d, %d, %p, 0x%08lx): don't implement this\n",
215 pbPrefix, debugstr_a(pbPrefix), cbPrefix,
216 nType, enumPasswordProc, x );
218 return WN_NOT_SUPPORTED;