blob: 1a828edf36d09b4a7b6462962f51e66e7e4e7553 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Registry.cxx
20
21#include <rfb_win32/Registry.h>
22#include <rfb_win32/Security.h>
23#include <rfb_win32/DynamicFn.h>
24#include <rdr/MemOutStream.h>
Peter Åstrandd6b96702008-12-09 10:36:16 +000025#include <rdr/HexOutStream.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000026#include <rdr/HexInStream.h>
27#include <stdlib.h>
28#include <rfb/LogWriter.h>
29
30// These flags are required to control access control inheritance,
31// but are not defined by VC6's headers. These definitions comes
32// from the Microsoft Platform SDK.
33#ifndef PROTECTED_DACL_SECURITY_INFORMATION
34#define PROTECTED_DACL_SECURITY_INFORMATION (0x80000000L)
35#endif
36#ifndef UNPROTECTED_DACL_SECURITY_INFORMATION
37#define UNPROTECTED_DACL_SECURITY_INFORMATION (0x20000000L)
38#endif
39
40
41using namespace rfb;
42using namespace rfb::win32;
43
44
45static LogWriter vlog("Registry");
46
47
48RegKey::RegKey() : key(0), freeKey(false), valueNameBufLen(0) {}
49
50RegKey::RegKey(const HKEY k) : key(0), freeKey(false), valueNameBufLen(0) {
51 LONG result = RegOpenKeyEx(k, 0, 0, KEY_ALL_ACCESS, &key);
52 if (result != ERROR_SUCCESS)
53 throw rdr::SystemException("RegOpenKeyEx(HKEY)", result);
54 vlog.debug("duplicated %x to %x", k, key);
55 freeKey = true;
56}
57
58RegKey::RegKey(const RegKey& k) : key(0), freeKey(false), valueNameBufLen(0) {
59 LONG result = RegOpenKeyEx(k.key, 0, 0, KEY_ALL_ACCESS, &key);
60 if (result != ERROR_SUCCESS)
61 throw rdr::SystemException("RegOpenKeyEx(RegKey&)", result);
62 vlog.debug("duplicated %x to %x", k.key, key);
63 freeKey = true;
64}
65
66RegKey::~RegKey() {
67 close();
68}
69
70
71void RegKey::setHKEY(HKEY k, bool fK) {
72 vlog.debug("setHKEY(%x,%d)", k, (int)fK);
73 close();
74 freeKey = fK;
75 key = k;
76}
77
78
79bool RegKey::createKey(const RegKey& root, const TCHAR* name) {
80 close();
81 LONG result = RegCreateKey(root.key, name, &key);
82 if (result != ERROR_SUCCESS) {
83 vlog.error("RegCreateKey(%x, %s): %x", root.key, name, result);
84 throw rdr::SystemException("RegCreateKeyEx", result);
85 }
86 vlog.debug("createKey(%x,%s) = %x", root.key, (const char*)CStr(name), key);
87 freeKey = true;
88 return true;
89}
90
91void RegKey::openKey(const RegKey& root, const TCHAR* name, bool readOnly) {
92 close();
93 LONG result = RegOpenKeyEx(root.key, name, 0, readOnly ? KEY_READ : KEY_ALL_ACCESS, &key);
94 if (result != ERROR_SUCCESS)
95 throw rdr::SystemException("RegOpenKeyEx (open)", result);
96 vlog.debug("openKey(%x,%s,%s) = %x", root.key, (const char*)CStr(name),
97 readOnly ? "ro" : "rw", key);
98 freeKey = true;
99}
100
101void RegKey::setDACL(const PACL acl, bool inherit) {
102 DWORD result;
103 typedef DWORD (WINAPI *_SetSecurityInfo_proto) (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION, PSID, PSID, PACL, PACL);
104 DynamicFn<_SetSecurityInfo_proto> _SetSecurityInfo(_T("advapi32.dll"), "SetSecurityInfo");
105 if (!_SetSecurityInfo.isValid())
106 throw rdr::SystemException("RegKey::setDACL failed", ERROR_CALL_NOT_IMPLEMENTED);
107 if ((result = (*_SetSecurityInfo)(key, SE_REGISTRY_KEY,
108 DACL_SECURITY_INFORMATION |
109 (inherit ? UNPROTECTED_DACL_SECURITY_INFORMATION : PROTECTED_DACL_SECURITY_INFORMATION),
110 0, 0, acl, 0)) != ERROR_SUCCESS)
111 throw rdr::SystemException("RegKey::setDACL failed", result);
112}
113
114void RegKey::close() {
115 if (freeKey) {
116 vlog.debug("RegCloseKey(%x)", key);
117 RegCloseKey(key);
118 key = 0;
119 }
120}
121
122void RegKey::deleteKey(const TCHAR* name) const {
123 LONG result = RegDeleteKey(key, name);
124 if (result != ERROR_SUCCESS)
125 throw rdr::SystemException("RegDeleteKey", result);
126}
127
128void RegKey::deleteValue(const TCHAR* name) const {
129 LONG result = RegDeleteValue(key, name);
130 if (result != ERROR_SUCCESS)
131 throw rdr::SystemException("RegDeleteValue", result);
132}
133
134void RegKey::awaitChange(bool watchSubTree, DWORD filter, HANDLE event) const {
135 LONG result = RegNotifyChangeKeyValue(key, watchSubTree, filter, event, event != 0);
136 if (result != ERROR_SUCCESS)
137 throw rdr::SystemException("RegNotifyChangeKeyValue", result);
138}
139
140
141RegKey::operator HKEY() const {return key;}
142
143
144void RegKey::setExpandString(const TCHAR* valname, const TCHAR* value) const {
145 LONG result = RegSetValueEx(key, valname, 0, REG_EXPAND_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
146 if (result != ERROR_SUCCESS) throw rdr::SystemException("setExpandString", result);
147}
148
149void RegKey::setString(const TCHAR* valname, const TCHAR* value) const {
150 LONG result = RegSetValueEx(key, valname, 0, REG_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
151 if (result != ERROR_SUCCESS) throw rdr::SystemException("setString", result);
152}
153
154void RegKey::setBinary(const TCHAR* valname, const void* value, int length) const {
155 LONG result = RegSetValueEx(key, valname, 0, REG_BINARY, (const BYTE*)value, length);
156 if (result != ERROR_SUCCESS) throw rdr::SystemException("setBinary", result);
157}
158
159void RegKey::setInt(const TCHAR* valname, int value) const {
160 LONG result = RegSetValueEx(key, valname, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
161 if (result != ERROR_SUCCESS) throw rdr::SystemException("setInt", result);
162}
163
164void RegKey::setBool(const TCHAR* valname, bool value) const {
165 setInt(valname, value ? 1 : 0);
166}
167
168TCHAR* RegKey::getString(const TCHAR* valname) const {return getRepresentation(valname);}
169TCHAR* RegKey::getString(const TCHAR* valname, const TCHAR* def) const {
170 try {
171 return getString(valname);
172 } catch(rdr::Exception) {
173 return tstrDup(def);
174 }
175}
176
177void RegKey::getBinary(const TCHAR* valname, void** data, int* length) const {
178 TCharArray hex = getRepresentation(valname);
179 if (!rdr::HexInStream::hexStrToBin(CStr(hex.buf), (char**)data, length))
180 throw rdr::Exception("getBinary failed");
181}
182void RegKey::getBinary(const TCHAR* valname, void** data, int* length, void* def, int deflen) const {
183 try {
184 getBinary(valname, data, length);
185 } catch(rdr::Exception) {
186 if (deflen) {
187 *data = new char[deflen];
188 memcpy(*data, def, deflen);
189 } else
190 *data = 0;
191 *length = deflen;
192 }
193}
194
195int RegKey::getInt(const TCHAR* valname) const {
196 TCharArray tmp = getRepresentation(valname);
197 return _ttoi(tmp.buf);
198}
199int RegKey::getInt(const TCHAR* valname, int def) const {
200 try {
201 return getInt(valname);
202 } catch(rdr::Exception) {
203 return def;
204 }
205}
206
207bool RegKey::getBool(const TCHAR* valname) const {
208 return getInt(valname) > 0;
209}
210bool RegKey::getBool(const TCHAR* valname, bool def) const {
211 return getInt(valname, def ? 1 : 0) > 0;
212}
213
214static inline TCHAR* terminateData(char* data, int length)
215{
216 // We must terminate the string, just to be sure. Stupid Win32...
217 int len = length/sizeof(TCHAR);
218 TCharArray str(len+1);
219 memcpy(str.buf, data, length);
220 str.buf[len] = 0;
221 return str.takeBuf();
222}
223
224TCHAR* RegKey::getRepresentation(const TCHAR* valname) const {
225 DWORD type, length;
226 LONG result = RegQueryValueEx(key, valname, 0, &type, 0, &length);
227 if (result != ERROR_SUCCESS)
228 throw rdr::SystemException("get registry value length", result);
229 CharArray data(length);
230 result = RegQueryValueEx(key, valname, 0, &type, (BYTE*)data.buf, &length);
231 if (result != ERROR_SUCCESS)
232 throw rdr::SystemException("get registry value", result);
233
234 switch (type) {
235 case REG_BINARY:
236 {
237 TCharArray hex = rdr::HexOutStream::binToHexStr(data.buf, length);
238 return hex.takeBuf();
239 }
240 case REG_SZ:
241 if (length) {
242 return terminateData(data.buf, length);
243 } else {
244 return tstrDup(_T(""));
245 }
246 case REG_DWORD:
247 {
248 TCharArray tmp(16);
249 _stprintf(tmp.buf, _T("%u"), *((DWORD*)data.buf));
250 return tmp.takeBuf();
251 }
252 case REG_EXPAND_SZ:
253 {
254 if (length) {
255 TCharArray str(terminateData(data.buf, length));
256 DWORD required = ExpandEnvironmentStrings(str.buf, 0, 0);
257 if (required==0)
258 throw rdr::SystemException("ExpandEnvironmentStrings", GetLastError());
259 TCharArray result(required);
260 length = ExpandEnvironmentStrings(str.buf, result.buf, required);
261 if (required<length)
262 rdr::Exception("unable to expand environment strings");
263 return result.takeBuf();
264 } else {
265 return tstrDup(_T(""));
266 }
267 }
268 default:
269 throw rdr::Exception("unsupported registry type");
270 }
271}
272
273bool RegKey::isValue(const TCHAR* valname) const {
274 try {
275 TCharArray tmp = getRepresentation(valname);
276 return true;
277 } catch(rdr::Exception) {
278 return false;
279 }
280}
281
282const TCHAR* RegKey::getValueName(int i) {
283 DWORD maxValueNameLen;
284 LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0);
285 if (result != ERROR_SUCCESS)
286 throw rdr::SystemException("RegQueryInfoKey", result);
287 if (valueNameBufLen < maxValueNameLen + 1) {
288 valueNameBufLen = maxValueNameLen + 1;
289 delete [] valueName.buf;
290 valueName.buf = new TCHAR[valueNameBufLen];
291 }
292 DWORD length = valueNameBufLen;
293 result = RegEnumValue(key, i, valueName.buf, &length, NULL, 0, 0, 0);
294 if (result == ERROR_NO_MORE_ITEMS) return 0;
295 if (result != ERROR_SUCCESS)
296 throw rdr::SystemException("RegEnumValue", result);
297 return valueName.buf;
298}
299
300const TCHAR* RegKey::getKeyName(int i) {
301 DWORD maxValueNameLen;
302 LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0, 0, 0, 0);
303 if (result != ERROR_SUCCESS)
304 throw rdr::SystemException("RegQueryInfoKey", result);
305 if (valueNameBufLen < maxValueNameLen + 1) {
306 valueNameBufLen = maxValueNameLen + 1;
307 delete [] valueName.buf;
308 valueName.buf = new TCHAR[valueNameBufLen];
309 }
310 DWORD length = valueNameBufLen;
311 result = RegEnumKeyEx(key, i, valueName.buf, &length, NULL, 0, 0, 0);
312 if (result == ERROR_NO_MORE_ITEMS) return 0;
313 if (result != ERROR_SUCCESS)
314 throw rdr::SystemException("RegEnumKey", result);
315 return valueName.buf;
316}