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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 char* 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, name, key);
  73. freeKey = true;
  74. return true;
  75. }
  76. void RegKey::openKey(const RegKey& root, const char* 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, 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 char* 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 char* 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 char* valname, const char* value) const {
  117. LONG result = RegSetValueEx(key, valname, 0, REG_EXPAND_SZ, (const BYTE*)value, (strlen(value)+1)*sizeof(char));
  118. if (result != ERROR_SUCCESS) throw rdr::SystemException("setExpandString", result);
  119. }
  120. void RegKey::setString(const char* valname, const char* value) const {
  121. LONG result = RegSetValueEx(key, valname, 0, REG_SZ, (const BYTE*)value, (strlen(value)+1)*sizeof(char));
  122. if (result != ERROR_SUCCESS) throw rdr::SystemException("setString", result);
  123. }
  124. void RegKey::setBinary(const char* 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 char* 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 char* valname, bool value) const {
  133. setInt(valname, value ? 1 : 0);
  134. }
  135. std::string RegKey::getString(const char* valname) const {
  136. return getRepresentation(valname);
  137. }
  138. std::string RegKey::getString(const char* valname, const char* def) const {
  139. try {
  140. return getString(valname);
  141. } catch(rdr::Exception&) {
  142. return def;
  143. }
  144. }
  145. std::vector<uint8_t> RegKey::getBinary(const char* valname) const {
  146. std::string hex = getRepresentation(valname);
  147. return hexToBin(hex.data(), hex.size());
  148. }
  149. std::vector<uint8_t> RegKey::getBinary(const char* valname, const uint8_t* def, size_t deflen) const {
  150. try {
  151. return getBinary(valname);
  152. } catch(rdr::Exception&) {
  153. std::vector<uint8_t> out(deflen);
  154. memcpy(out.data(), def, deflen);
  155. return out;
  156. }
  157. }
  158. int RegKey::getInt(const char* valname) const {
  159. return atoi(getRepresentation(valname).c_str());
  160. }
  161. int RegKey::getInt(const char* valname, int def) const {
  162. try {
  163. return getInt(valname);
  164. } catch(rdr::Exception&) {
  165. return def;
  166. }
  167. }
  168. bool RegKey::getBool(const char* valname) const {
  169. return getInt(valname) > 0;
  170. }
  171. bool RegKey::getBool(const char* valname, bool def) const {
  172. return getInt(valname, def ? 1 : 0) > 0;
  173. }
  174. std::string RegKey::getRepresentation(const char* valname) const {
  175. DWORD type, length;
  176. LONG result = RegQueryValueEx(key, valname, 0, &type, 0, &length);
  177. if (result != ERROR_SUCCESS)
  178. throw rdr::SystemException("get registry value length", result);
  179. CharArray data(length);
  180. result = RegQueryValueEx(key, valname, 0, &type, (BYTE*)data.buf, &length);
  181. if (result != ERROR_SUCCESS)
  182. throw rdr::SystemException("get registry value", result);
  183. switch (type) {
  184. case REG_BINARY:
  185. {
  186. return binToHex((const uint8_t*)data.buf, length);
  187. }
  188. case REG_SZ:
  189. if (length) {
  190. return std::string(data.buf, length);
  191. } else {
  192. return "";
  193. }
  194. case REG_DWORD:
  195. {
  196. char tmp[16];
  197. sprintf(tmp, "%lu", *((DWORD*)data.buf));
  198. return tmp;
  199. }
  200. case REG_EXPAND_SZ:
  201. {
  202. if (length) {
  203. std::string str(data.buf, length);
  204. DWORD required = ExpandEnvironmentStrings(str.c_str(), 0, 0);
  205. if (required==0)
  206. throw rdr::SystemException("ExpandEnvironmentStrings", GetLastError());
  207. CharArray result(required);
  208. length = ExpandEnvironmentStrings(str.c_str(), result.buf, required);
  209. if (required<length)
  210. throw rdr::Exception("unable to expand environment strings");
  211. return result.buf;
  212. } else {
  213. return "";
  214. }
  215. }
  216. default:
  217. throw rdr::Exception("unsupported registry type");
  218. }
  219. }
  220. bool RegKey::isValue(const char* valname) const {
  221. try {
  222. getRepresentation(valname);
  223. return true;
  224. } catch(rdr::Exception&) {
  225. return false;
  226. }
  227. }
  228. const char* RegKey::getValueName(int i) {
  229. DWORD maxValueNameLen;
  230. LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0);
  231. if (result != ERROR_SUCCESS)
  232. throw rdr::SystemException("RegQueryInfoKey", result);
  233. if (valueNameBufLen < maxValueNameLen + 1) {
  234. valueNameBufLen = maxValueNameLen + 1;
  235. delete [] valueName.buf;
  236. valueName.buf = new char[valueNameBufLen];
  237. }
  238. DWORD length = valueNameBufLen;
  239. result = RegEnumValue(key, i, valueName.buf, &length, NULL, 0, 0, 0);
  240. if (result == ERROR_NO_MORE_ITEMS) return 0;
  241. if (result != ERROR_SUCCESS)
  242. throw rdr::SystemException("RegEnumValue", result);
  243. return valueName.buf;
  244. }
  245. const char* RegKey::getKeyName(int i) {
  246. DWORD maxValueNameLen;
  247. LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0, 0, 0, 0);
  248. if (result != ERROR_SUCCESS)
  249. throw rdr::SystemException("RegQueryInfoKey", result);
  250. if (valueNameBufLen < maxValueNameLen + 1) {
  251. valueNameBufLen = maxValueNameLen + 1;
  252. delete [] valueName.buf;
  253. valueName.buf = new char[valueNameBufLen];
  254. }
  255. DWORD length = valueNameBufLen;
  256. result = RegEnumKeyEx(key, i, valueName.buf, &length, NULL, 0, 0, 0);
  257. if (result == ERROR_NO_MORE_ITEMS) return 0;
  258. if (result != ERROR_SUCCESS)
  259. throw rdr::SystemException("RegEnumKey", result);
  260. return valueName.buf;
  261. }