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

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