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.8KB

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