blob: de9238f7dc80333ac78ebcde5438d1923a552db6 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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/LogWriter.h>
22#include <rfb_win32/Registry.h>
23#include <rdr/MemOutStream.h>
24#include <rdr/HexOutstream.h>
25#include <rdr/HexInStream.h>
26#include <rfb_win32/Security.h>
27
28#include <stdlib.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 close();
73 freeKey = fK;
74 key = k;
75}
76
77
78bool RegKey::createKey(const RegKey& root, const TCHAR* name) {
79 close();
80 LONG result = RegCreateKey(root.key, name, &key);
81 if (result != ERROR_SUCCESS) {
82 vlog.error("RegCreateKey(%x, %s): %x", root.key, name, result);
83 throw rdr::SystemException("RegCreateKeyEx", result);
84 }
85 freeKey = true;
86 return true;
87}
88
89void RegKey::openKey(const RegKey& root, const TCHAR* name, bool readOnly) {
90 close();
91 LONG result = RegOpenKeyEx(root.key, name, 0, readOnly ? KEY_READ : KEY_ALL_ACCESS, &key);
92 if (result != ERROR_SUCCESS)
93 throw rdr::SystemException("RegOpenKeyEx (open)", result);
94 freeKey = true;
95}
96
97void RegKey::setDACL(const PACL acl, bool inherit) {
98 DWORD result;
99 typedef DWORD (WINAPI *_SetSecurityInfo_proto) (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION, PSID, PSID, PACL, PACL);
100 DynamicFn<_SetSecurityInfo_proto> _SetSecurityInfo(_T("advapi32.dll"), "SetSecurityInfo");
101 if (!_SetSecurityInfo.isValid())
102 throw rdr::SystemException("RegKey::setDACL failed", ERROR_CALL_NOT_IMPLEMENTED);
103 if ((result = (*_SetSecurityInfo)(key, SE_REGISTRY_KEY,
104 DACL_SECURITY_INFORMATION |
105 (inherit ? UNPROTECTED_DACL_SECURITY_INFORMATION : PROTECTED_DACL_SECURITY_INFORMATION),
106 0, 0, acl, 0)) != ERROR_SUCCESS)
107 throw rdr::SystemException("RegKey::setDACL failed", result);
108}
109
110void RegKey::close() {
111 if (freeKey) {
112 RegCloseKey(key);
113 key = 0;
114 }
115}
116
117void RegKey::deleteKey(const TCHAR* name) const {
118 LONG result = RegDeleteKey(key, name);
119 if (result != ERROR_SUCCESS)
120 throw rdr::SystemException("RegDeleteKey", result);
121}
122
123void RegKey::deleteValue(const TCHAR* name) const {
124 LONG result = RegDeleteValue(key, name);
125 if (result != ERROR_SUCCESS)
126 throw rdr::SystemException("RegDeleteValue", result);
127}
128
129void RegKey::awaitChange(bool watchSubTree, DWORD filter) const {
130 LONG result = RegNotifyChangeKeyValue(key, watchSubTree, filter, 0, FALSE);
131 if (result != ERROR_SUCCESS)
132 throw rdr::SystemException("RegNotifyChangeKeyValue", result);
133}
134
135
136RegKey::operator HKEY() const {return key;}
137
138
139void RegKey::setExpandString(const TCHAR* valname, const TCHAR* value) const {
140 LONG result = RegSetValueEx(key, valname, 0, REG_EXPAND_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
141 if (result != ERROR_SUCCESS) throw rdr::SystemException("setExpandString", result);
142}
143
144void RegKey::setString(const TCHAR* valname, const TCHAR* value) const {
145 LONG result = RegSetValueEx(key, valname, 0, REG_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
146 if (result != ERROR_SUCCESS) throw rdr::SystemException("setString", result);
147}
148
149void RegKey::setBinary(const TCHAR* valname, const void* value, int length) const {
150 LONG result = RegSetValueEx(key, valname, 0, REG_BINARY, (const BYTE*)value, length);
151 if (result != ERROR_SUCCESS) throw rdr::SystemException("setBinary", result);
152}
153
154void RegKey::setInt(const TCHAR* valname, int value) const {
155 LONG result = RegSetValueEx(key, valname, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
156 if (result != ERROR_SUCCESS) throw rdr::SystemException("setInt", result);
157}
158
159void RegKey::setBool(const TCHAR* valname, bool value) const {
160 setInt(valname, value ? 1 : 0);
161}
162
163TCHAR* RegKey::getString(const TCHAR* valname) const {return getRepresentation(valname);}
164TCHAR* RegKey::getString(const TCHAR* valname, const TCHAR* def) const {
165 try {
166 return getString(valname);
167 } catch(rdr::Exception) {
168 return tstrDup(def);
169 }
170}
171
172void RegKey::getBinary(const TCHAR* valname, void** data, int* length) const {
173 TCharArray hex = getRepresentation(valname);
174 if (!rdr::HexInStream::hexStrToBin(CStr(hex.buf), (char**)data, length))
175 throw rdr::Exception("getBinary failed");
176}
177void RegKey::getBinary(const TCHAR* valname, void** data, int* length, void* def, int deflen) const {
178 try {
179 getBinary(valname, data, length);
180 } catch(rdr::Exception) {
181 if (deflen) {
182 *data = new char[deflen];
183 memcpy(*data, def, deflen);
184 } else
185 *data = 0;
186 *length = deflen;
187 }
188}
189
190int RegKey::getInt(const TCHAR* valname) const {
191 TCharArray tmp = getRepresentation(valname);
192 return _ttoi(tmp.buf);
193}
194int RegKey::getInt(const TCHAR* valname, int def) const {
195 try {
196 return getInt(valname);
197 } catch(rdr::Exception) {
198 return def;
199 }
200}
201
202bool RegKey::getBool(const TCHAR* valname) const {
203 return getInt(valname) > 0;
204}
205bool RegKey::getBool(const TCHAR* valname, bool def) const {
206 return getInt(valname, def ? 1 : 0) > 0;
207}
208
209TCHAR* RegKey::getRepresentation(const TCHAR* valname) const {
210 DWORD type, length;
211 LONG result = RegQueryValueEx(key, valname, 0, &type, 0, &length);
212 if (result != ERROR_SUCCESS)
213 throw rdr::SystemException("get registry value length", result);
214 CharArray data(length);
215 result = RegQueryValueEx(key, valname, 0, &type, (BYTE*)data.buf, &length);
216 if (result != ERROR_SUCCESS)
217 throw rdr::SystemException("get registry value", result);
218
219 switch (type) {
220 case REG_BINARY:
221 {
222 TCharArray hex = rdr::HexOutStream::binToHexStr(data.buf, length);
223 return hex.takeBuf();
224 }
225 case REG_SZ:
226 if (length) {
227 // We must terminate the string, just to be sure. Stupid Win32...
228 int len = length/sizeof(TCHAR);
229 TCharArray str(len+1);
230 memcpy(str.buf, data.buf, length);
231 str.buf[len] = 0;
232 return str.takeBuf();
233 } else {
234 return tstrDup(_T(""));
235 }
236 case REG_DWORD:
237 {
238 TCharArray tmp(16);
239 _stprintf(tmp.buf, _T("%u"), *((DWORD*)data.buf));
240 return tmp.takeBuf();
241 }
242 default:
243 throw rdr::Exception("unsupported registry type");
244 }
245}
246
247bool RegKey::isValue(const TCHAR* valname) const {
248 try {
249 TCharArray tmp = getRepresentation(valname);
250 return true;
251 } catch(rdr::Exception) {
252 return false;
253 }
254}
255
256const TCHAR* RegKey::getValueName(int i) {
257 DWORD maxValueNameLen;
258 LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0);
259 if (result != ERROR_SUCCESS)
260 throw rdr::SystemException("RegQueryInfoKey", result);
261 if (valueNameBufLen < maxValueNameLen + 1) {
262 valueNameBufLen = maxValueNameLen + 1;
263 delete [] valueName.buf;
264 valueName.buf = new TCHAR[valueNameBufLen];
265 }
266 DWORD length = valueNameBufLen;
267 result = RegEnumValue(key, i, valueName.buf, &length, NULL, 0, 0, 0);
268 if (result == ERROR_NO_MORE_ITEMS) return 0;
269 if (result != ERROR_SUCCESS)
270 throw rdr::SystemException("RegEnumValue", result);
271 return valueName.buf;
272}