You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Registry.cxx 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* 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. // -=- Registry.cxx
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rfb_win32/Registry.h>
  23. #include <rfb_win32/Security.h>
  24. #include <rdr/MemOutStream.h>
  25. #include <rdr/HexOutStream.h>
  26. #include <rdr/HexInStream.h>
  27. #include <stdlib.h>
  28. #include <rfb/LogWriter.h>
  29. // These flags are required to control access control inheritance,
  30. // but are not defined by VC6's headers. These definitions comes
  31. // from the Microsoft Platform SDK.
  32. #ifndef PROTECTED_DACL_SECURITY_INFORMATION
  33. #define PROTECTED_DACL_SECURITY_INFORMATION (0x80000000L)
  34. #endif
  35. #ifndef UNPROTECTED_DACL_SECURITY_INFORMATION
  36. #define UNPROTECTED_DACL_SECURITY_INFORMATION (0x20000000L)
  37. #endif
  38. using namespace rfb;
  39. using namespace rfb::win32;
  40. static LogWriter vlog("Registry");
  41. RegKey::RegKey() : key(0), freeKey(false), valueNameBufLen(0) {}
  42. RegKey::RegKey(const HKEY k) : key(0), freeKey(false), valueNameBufLen(0) {
  43. LONG result = RegOpenKeyEx(k, 0, 0, KEY_ALL_ACCESS, &key);
  44. if (result != ERROR_SUCCESS)
  45. throw rdr::SystemException("RegOpenKeyEx(HKEY)", result);
  46. vlog.debug("duplicated %p to %p", k, key);
  47. freeKey = true;
  48. }
  49. RegKey::RegKey(const RegKey& k) : key(0), freeKey(false), valueNameBufLen(0) {
  50. LONG result = RegOpenKeyEx(k.key, 0, 0, KEY_ALL_ACCESS, &key);
  51. if (result != ERROR_SUCCESS)
  52. throw rdr::SystemException("RegOpenKeyEx(RegKey&)", result);
  53. vlog.debug("duplicated %p to %p", k.key, key);
  54. freeKey = true;
  55. }
  56. RegKey::~RegKey() {
  57. close();
  58. }
  59. void RegKey::setHKEY(HKEY k, bool fK) {
  60. vlog.debug("setHKEY(%p,%d)", k, (int)fK);
  61. close();
  62. freeKey = fK;
  63. key = k;
  64. }
  65. bool RegKey::createKey(const RegKey& root, const TCHAR* name) {
  66. close();
  67. LONG result = RegCreateKey(root.key, name, &key);
  68. if (result != ERROR_SUCCESS) {
  69. vlog.error("RegCreateKey(%p, %s): %lx", root.key, name, result);
  70. throw rdr::SystemException("RegCreateKeyEx", result);
  71. }
  72. vlog.debug("createKey(%p,%s) = %p", root.key, (const char*)CStr(name), key);
  73. freeKey = true;
  74. return true;
  75. }
  76. void RegKey::openKey(const RegKey& root, const TCHAR* name, bool readOnly) {
  77. close();
  78. LONG result = RegOpenKeyEx(root.key, name, 0, readOnly ? KEY_READ : KEY_ALL_ACCESS, &key);
  79. if (result != ERROR_SUCCESS)
  80. throw rdr::SystemException("RegOpenKeyEx (open)", result);
  81. vlog.debug("openKey(%p,%s,%s) = %p", root.key, (const char*)CStr(name),
  82. readOnly ? "ro" : "rw", key);
  83. freeKey = true;
  84. }
  85. void RegKey::setDACL(const PACL acl, bool inherit) {
  86. DWORD result;
  87. if ((result = SetSecurityInfo(key, SE_REGISTRY_KEY,
  88. DACL_SECURITY_INFORMATION |
  89. (inherit ? UNPROTECTED_DACL_SECURITY_INFORMATION : PROTECTED_DACL_SECURITY_INFORMATION),
  90. 0, 0, acl, 0)) != ERROR_SUCCESS)
  91. throw rdr::SystemException("RegKey::setDACL failed", result);
  92. }
  93. void RegKey::close() {
  94. if (freeKey) {
  95. vlog.debug("RegCloseKey(%p)", key);
  96. RegCloseKey(key);
  97. key = 0;
  98. }
  99. }
  100. void RegKey::deleteKey(const TCHAR* name) const {
  101. LONG result = RegDeleteKey(key, name);
  102. if (result != ERROR_SUCCESS)
  103. throw rdr::SystemException("RegDeleteKey", result);
  104. }
  105. void RegKey::deleteValue(const TCHAR* name) const {
  106. LONG result = RegDeleteValue(key, name);
  107. if (result != ERROR_SUCCESS)
  108. throw rdr::SystemException("RegDeleteValue", result);
  109. }
  110. void RegKey::awaitChange(bool watchSubTree, DWORD filter, HANDLE event) const {
  111. LONG result = RegNotifyChangeKeyValue(key, watchSubTree, filter, event, event != 0);
  112. if (result != ERROR_SUCCESS)
  113. throw rdr::SystemException("RegNotifyChangeKeyValue", result);
  114. }
  115. RegKey::operator HKEY() const {return key;}
  116. void RegKey::setExpandString(const TCHAR* valname, const TCHAR* value) const {
  117. LONG result = RegSetValueEx(key, valname, 0, REG_EXPAND_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
  118. if (result != ERROR_SUCCESS) throw rdr::SystemException("setExpandString", result);
  119. }
  120. void RegKey::setString(const TCHAR* valname, const TCHAR* value) const {
  121. LONG result = RegSetValueEx(key, valname, 0, REG_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
  122. if (result != ERROR_SUCCESS) throw rdr::SystemException("setString", result);
  123. }
  124. void RegKey::setBinary(const TCHAR* valname, const void* value, size_t length) const {
  125. LONG result = RegSetValueEx(key, valname, 0, REG_BINARY, (const BYTE*)value, length);
  126. if (result != ERROR_SUCCESS) throw rdr::SystemException("setBinary", result);
  127. }
  128. void RegKey::setInt(const TCHAR* valname, int value) const {
  129. LONG result = RegSetValueEx(key, valname, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
  130. if (result != ERROR_SUCCESS) throw rdr::SystemException("setInt", result);
  131. }
  132. void RegKey::setBool(const TCHAR* valname, bool value) const {
  133. setInt(valname, value ? 1 : 0);
  134. }
  135. TCHAR* RegKey::getString(const TCHAR* valname) const {return getRepresentation(valname);}
  136. TCHAR* RegKey::getString(const TCHAR* valname, const TCHAR* def) const {
  137. try {
  138. return getString(valname);
  139. } catch(rdr::Exception&) {
  140. return tstrDup(def);
  141. }
  142. }
  143. void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length) const {
  144. TCharArray hex(getRepresentation(valname));
  145. if (!rdr::HexInStream::hexStrToBin(CStr(hex.buf), (char**)data, length))
  146. throw rdr::Exception("getBinary failed");
  147. }
  148. void RegKey::getBinary(const TCHAR* valname, void** data, size_t* length, void* def, size_t deflen) const {
  149. try {
  150. getBinary(valname, data, length);
  151. } catch(rdr::Exception&) {
  152. if (deflen) {
  153. *data = new char[deflen];
  154. memcpy(*data, def, deflen);
  155. } else
  156. *data = 0;
  157. *length = deflen;
  158. }
  159. }
  160. int RegKey::getInt(const TCHAR* valname) const {
  161. TCharArray tmp(getRepresentation(valname));
  162. return _ttoi(tmp.buf);
  163. }
  164. int RegKey::getInt(const TCHAR* valname, int def) const {
  165. try {
  166. return getInt(valname);
  167. } catch(rdr::Exception&) {
  168. return def;
  169. }
  170. }
  171. bool RegKey::getBool(const TCHAR* valname) const {
  172. return getInt(valname) > 0;
  173. }
  174. bool RegKey::getBool(const TCHAR* valname, bool def) const {
  175. return getInt(valname, def ? 1 : 0) > 0;
  176. }
  177. static inline TCHAR* terminateData(char* data, int length)
  178. {
  179. // We must terminate the string, just to be sure. Stupid Win32...
  180. int len = length/sizeof(TCHAR);
  181. TCharArray str(len+1);
  182. memcpy(str.buf, data, length);
  183. str.buf[len] = 0;
  184. return str.takeBuf();
  185. }
  186. TCHAR* RegKey::getRepresentation(const TCHAR* valname) const {
  187. DWORD type, length;
  188. LONG result = RegQueryValueEx(key, valname, 0, &type, 0, &length);
  189. if (result != ERROR_SUCCESS)
  190. throw rdr::SystemException("get registry value length", result);
  191. CharArray data(length);
  192. result = RegQueryValueEx(key, valname, 0, &type, (BYTE*)data.buf, &length);
  193. if (result != ERROR_SUCCESS)
  194. throw rdr::SystemException("get registry value", result);
  195. switch (type) {
  196. case REG_BINARY:
  197. {
  198. TCharArray hex(rdr::HexOutStream::binToHexStr(data.buf, length));
  199. return hex.takeBuf();
  200. }
  201. case REG_SZ:
  202. if (length) {
  203. return terminateData(data.buf, length);
  204. } else {
  205. return tstrDup(_T(""));
  206. }
  207. case REG_DWORD:
  208. {
  209. TCharArray tmp(16);
  210. _stprintf(tmp.buf, _T("%lu"), *((DWORD*)data.buf));
  211. return tmp.takeBuf();
  212. }
  213. case REG_EXPAND_SZ:
  214. {
  215. if (length) {
  216. TCharArray str(terminateData(data.buf, length));
  217. DWORD required = ExpandEnvironmentStrings(str.buf, 0, 0);
  218. if (required==0)
  219. throw rdr::SystemException("ExpandEnvironmentStrings", GetLastError());
  220. TCharArray result(required);
  221. length = ExpandEnvironmentStrings(str.buf, result.buf, required);
  222. if (required<length)
  223. throw rdr::Exception("unable to expand environment strings");
  224. return result.takeBuf();
  225. } else {
  226. return tstrDup(_T(""));
  227. }
  228. }
  229. default:
  230. throw rdr::Exception("unsupported registry type");
  231. }
  232. }
  233. bool RegKey::isValue(const TCHAR* valname) const {
  234. try {
  235. TCharArray tmp(getRepresentation(valname));
  236. return true;
  237. } catch(rdr::Exception&) {
  238. return false;
  239. }
  240. }
  241. const TCHAR* RegKey::getValueName(int i) {
  242. DWORD maxValueNameLen;
  243. LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0);
  244. if (result != ERROR_SUCCESS)
  245. throw rdr::SystemException("RegQueryInfoKey", result);
  246. if (valueNameBufLen < maxValueNameLen + 1) {
  247. valueNameBufLen = maxValueNameLen + 1;
  248. delete [] valueName.buf;
  249. valueName.buf = new TCHAR[valueNameBufLen];
  250. }
  251. DWORD length = valueNameBufLen;
  252. result = RegEnumValue(key, i, valueName.buf, &length, NULL, 0, 0, 0);
  253. if (result == ERROR_NO_MORE_ITEMS) return 0;
  254. if (result != ERROR_SUCCESS)
  255. throw rdr::SystemException("RegEnumValue", result);
  256. return valueName.buf;
  257. }
  258. const TCHAR* RegKey::getKeyName(int i) {
  259. DWORD maxValueNameLen;
  260. LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0, 0, 0, 0);
  261. if (result != ERROR_SUCCESS)
  262. throw rdr::SystemException("RegQueryInfoKey", result);
  263. if (valueNameBufLen < maxValueNameLen + 1) {
  264. valueNameBufLen = maxValueNameLen + 1;
  265. delete [] valueName.buf;
  266. valueName.buf = new TCHAR[valueNameBufLen];
  267. }
  268. DWORD length = valueNameBufLen;
  269. result = RegEnumKeyEx(key, i, valueName.buf, &length, NULL, 0, 0, 0);
  270. if (result == ERROR_NO_MORE_ITEMS) return 0;
  271. if (result != ERROR_SUCCESS)
  272. throw rdr::SystemException("RegEnumKey", result);
  273. return valueName.buf;
  274. }