venus: Remove dedebug
[openafs.git] / src / WINNT / client_creds / settings.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afsconfig.h>
15 #include <afs/param.h>
16 #include <roken.h>
17 }
18
19 #include <windows.h>
20 #include <winerror.h>
21 #include <WINNT/TaLocale.h>
22 #include "afscreds.h"
23 #include "settings.h"
24
25
26 /*
27  * ROUTINES ___________________________________________________________________
28  *
29  */
30
31 BOOL RestoreSettings (HKEY hkParent,
32                       LPCTSTR pszBase,
33                       LPCTSTR pszValue,
34                       PVOID pStructure,
35                       size_t cbStructure,
36                       WORD wVerExpected)
37 {
38    BOOL rc = FALSE;
39
40    size_t cbStored;
41    if ((cbStored = GetRegValueSize (hkParent, pszBase, pszValue)) != 0)
42       {
43       if (cbStored >= sizeof(WORD)+cbStructure)
44          {
45          PVOID pStructureFinal;
46          if ((pStructureFinal = Allocate (cbStored)) != NULL)
47             {
48             if (GetBinaryRegValue (hkParent, pszBase, pszValue, pStructureFinal, cbStored))
49                {
50                WORD wVerStored = *(LPWORD)pStructureFinal;
51
52                if ( (HIBYTE(wVerStored) == HIBYTE(wVerExpected)) &&
53                     (LOBYTE(wVerStored) >= LOBYTE(wVerExpected)) )
54                   {
55                   memcpy (pStructure, &((LPBYTE)pStructureFinal)[ sizeof(WORD) ], cbStructure);
56                   rc = TRUE;
57                   }
58                }
59
60             Free (pStructureFinal);
61             }
62          }
63       }
64
65    return rc;
66 }
67
68
69 BOOL StoreSettings (HKEY hkParent,
70                     LPCTSTR pszBase,
71                     LPCTSTR pszValue,
72                     PVOID pStructure,
73                     size_t cbStructure,
74                     WORD wVersion)
75 {
76    BOOL rc = FALSE;
77
78    PVOID pStructureFinal;
79    if ((pStructureFinal = Allocate (sizeof(WORD) + cbStructure)) != NULL)
80       {
81       *(LPWORD)pStructureFinal = wVersion;
82       memcpy (&((LPBYTE)pStructureFinal)[ sizeof(WORD) ], pStructure, cbStructure);
83
84       rc = SetBinaryRegValue (hkParent, pszBase, pszValue, pStructureFinal, sizeof(WORD) + cbStructure);
85
86       Free (pStructureFinal);
87       }
88
89    return rc;
90 }
91
92
93 void EraseSettings (HKEY hkParent, LPCTSTR pszBase, LPCTSTR pszValue)
94 {
95    HKEY hk;
96    if (RegOpenKeyEx (hkParent, pszBase, 0,
97                       (IsWow64()?KEY_WOW64_64KEY:0)|KEY_SET_VALUE, &hk) == 0)
98       {
99       RegDeleteValue (hk, pszValue);
100       RegCloseKey (hk);
101       }
102 }
103
104
105 BOOL GetBinaryRegValue (HKEY hk,
106                         LPCTSTR pszBase,
107                         LPCTSTR pszValue,
108                         PVOID pData,
109                         size_t cbData)
110 {
111    BOOL rc = FALSE;
112
113    HKEY hkFinal;
114    if (RegOpenKeyEx (hk, pszBase, 0,
115                       (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hkFinal) == ERROR_SUCCESS)
116       {
117       DWORD dwType;
118       DWORD dwSize = (DWORD)cbData;
119
120       if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, (LPBYTE)pData, &dwSize) == ERROR_SUCCESS)
121          rc = TRUE;
122
123       RegCloseKey (hk);
124       }
125
126    return rc;
127 }
128
129
130 size_t GetRegValueSize (HKEY hk,
131                         LPCTSTR pszBase,
132                         LPCTSTR pszValue)
133 {
134    size_t cb = 0;
135
136    HKEY hkFinal;
137    if (RegOpenKeyEx (hk, pszBase, 0,
138                       (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hkFinal) == ERROR_SUCCESS)
139       {
140       DWORD dwType;
141       DWORD dwSize = 0;
142
143       if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS)
144          {
145          cb = (size_t)dwSize;
146          }
147
148       RegCloseKey (hk);
149       }
150
151    return cb;
152 }
153
154
155 BOOL SetBinaryRegValue (HKEY hk,
156                         LPCTSTR pszBase,
157                         LPCTSTR pszValue,
158                         PVOID pData,
159                         size_t cbData)
160 {
161    BOOL rc = FALSE;
162
163    HKEY hkFinal;
164    if (RegCreateKeyEx (hk, pszBase, 0, NULL, 0,
165                         (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE, NULL, &hkFinal, NULL) == ERROR_SUCCESS)
166       {
167       DWORD dwSize = (DWORD)cbData;
168
169       if (RegSetValueEx (hkFinal, pszValue, NULL, REG_BINARY, (LPBYTE)pData, dwSize) == ERROR_SUCCESS)
170          rc = TRUE;
171
172       RegCloseKey (hk);
173       }
174
175    return rc;
176 }
177
178
179 // Under Windows NT, RegDeleteKey() is not recursive--under Windows 95,
180 // it is.  Sigh.  This routine works recursively on either OS.
181 //
182 BOOL RegDeltreeKey (HKEY hk, LPTSTR pszKey)
183 {
184    HKEY hkSub;
185    if (RegOpenKeyEx (hk, pszKey, 0,
186                       (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE, &hkSub) == 0)
187       {
188       TCHAR szFound[ MAX_PATH ];
189       while (RegEnumKey (hkSub, 0, szFound, MAX_PATH) == 0)
190          {
191          if (!RegDeltreeKey (hkSub, szFound))
192             {
193             RegCloseKey (hkSub);
194             return FALSE;
195             }
196          }
197
198       RegCloseKey (hkSub);
199       }
200
201    if (RegDeleteKey (hk, pszKey) != 0)
202       return FALSE;
203
204    return TRUE;
205 }
206