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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* 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. // -=- Registry.cxx
  19. #include <rfb/LogWriter.h>
  20. #include <rfb_win32/Registry.h>
  21. #include <rdr/MemOutStream.h>
  22. #include <rdr/HexOutstream.h>
  23. #include <rdr/HexInStream.h>
  24. #include <rfb_win32/Security.h>
  25. #include <stdlib.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 %x to %x", 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 %x to %x", k.key, key);
  51. freeKey = true;
  52. }
  53. RegKey::~RegKey() {
  54. close();
  55. }
  56. void RegKey::setHKEY(HKEY k, bool fK) {
  57. close();
  58. freeKey = fK;
  59. key = k;
  60. }
  61. bool RegKey::createKey(const RegKey& root, const TCHAR* name) {
  62. close();
  63. LONG result = RegCreateKey(root.key, name, &key);
  64. if (result != ERROR_SUCCESS) {
  65. vlog.error("RegCreateKey(%x, %s): %x", root.key, name, result);
  66. throw rdr::SystemException("RegCreateKeyEx", result);
  67. }
  68. freeKey = true;
  69. return true;
  70. }
  71. void RegKey::openKey(const RegKey& root, const TCHAR* name, bool readOnly) {
  72. close();
  73. LONG result = RegOpenKeyEx(root.key, name, 0, readOnly ? KEY_READ : KEY_ALL_ACCESS, &key);
  74. if (result != ERROR_SUCCESS)
  75. throw rdr::SystemException("RegOpenKeyEx (open)", result);
  76. freeKey = true;
  77. }
  78. void RegKey::setDACL(const PACL acl, bool inherit) {
  79. DWORD result;
  80. typedef DWORD (WINAPI *_SetSecurityInfo_proto) (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION, PSID, PSID, PACL, PACL);
  81. DynamicFn<_SetSecurityInfo_proto> _SetSecurityInfo(_T("advapi32.dll"), "SetSecurityInfo");
  82. if (!_SetSecurityInfo.isValid())
  83. throw rdr::SystemException("RegKey::setDACL failed", ERROR_CALL_NOT_IMPLEMENTED);
  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. RegCloseKey(key);
  93. key = 0;
  94. }
  95. }
  96. void RegKey::deleteKey(const TCHAR* name) const {
  97. LONG result = RegDeleteKey(key, name);
  98. if (result != ERROR_SUCCESS)
  99. throw rdr::SystemException("RegDeleteKey", result);
  100. }
  101. void RegKey::deleteValue(const TCHAR* name) const {
  102. LONG result = RegDeleteValue(key, name);
  103. if (result != ERROR_SUCCESS)
  104. throw rdr::SystemException("RegDeleteValue", result);
  105. }
  106. void RegKey::awaitChange(bool watchSubTree, DWORD filter) const {
  107. LONG result = RegNotifyChangeKeyValue(key, watchSubTree, filter, 0, FALSE);
  108. if (result != ERROR_SUCCESS)
  109. throw rdr::SystemException("RegNotifyChangeKeyValue", result);
  110. }
  111. RegKey::operator HKEY() const {return key;}
  112. void RegKey::setExpandString(const TCHAR* valname, const TCHAR* value) const {
  113. LONG result = RegSetValueEx(key, valname, 0, REG_EXPAND_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
  114. if (result != ERROR_SUCCESS) throw rdr::SystemException("setExpandString", result);
  115. }
  116. void RegKey::setString(const TCHAR* valname, const TCHAR* value) const {
  117. LONG result = RegSetValueEx(key, valname, 0, REG_SZ, (const BYTE*)value, (_tcslen(value)+1)*sizeof(TCHAR));
  118. if (result != ERROR_SUCCESS) throw rdr::SystemException("setString", result);
  119. }
  120. void RegKey::setBinary(const TCHAR* valname, const void* value, int length) const {
  121. LONG result = RegSetValueEx(key, valname, 0, REG_BINARY, (const BYTE*)value, length);
  122. if (result != ERROR_SUCCESS) throw rdr::SystemException("setBinary", result);
  123. }
  124. void RegKey::setInt(const TCHAR* valname, int value) const {
  125. LONG result = RegSetValueEx(key, valname, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
  126. if (result != ERROR_SUCCESS) throw rdr::SystemException("setInt", result);
  127. }
  128. void RegKey::setBool(const TCHAR* valname, bool value) const {
  129. setInt(valname, value ? 1 : 0);
  130. }
  131. TCHAR* RegKey::getString(const TCHAR* valname) const {return getRepresentation(valname);}
  132. TCHAR* RegKey::getString(const TCHAR* valname, const TCHAR* def) const {
  133. try {
  134. return getString(valname);
  135. } catch(rdr::Exception) {
  136. return tstrDup(def);
  137. }
  138. }
  139. void RegKey::getBinary(const TCHAR* valname, void** data, int* length) const {
  140. TCharArray hex = getRepresentation(valname);
  141. if (!rdr::HexInStream::hexStrToBin(CStr(hex.buf), (char**)data, length))
  142. throw rdr::Exception("getBinary failed");
  143. }
  144. void RegKey::getBinary(const TCHAR* valname, void** data, int* length, void* def, int deflen) const {
  145. try {
  146. getBinary(valname, data, length);
  147. } catch(rdr::Exception) {
  148. if (deflen) {
  149. *data = new char[deflen];
  150. memcpy(*data, def, deflen);
  151. } else
  152. *data = 0;
  153. *length = deflen;
  154. }
  155. }
  156. int RegKey::getInt(const TCHAR* valname) const {
  157. TCharArray tmp = getRepresentation(valname);
  158. return _ttoi(tmp.buf);
  159. }
  160. int RegKey::getInt(const TCHAR* valname, int def) const {
  161. try {
  162. return getInt(valname);
  163. } catch(rdr::Exception) {
  164. return def;
  165. }
  166. }
  167. bool RegKey::getBool(const TCHAR* valname) const {
  168. return getInt(valname) > 0;
  169. }
  170. bool RegKey::getBool(const TCHAR* valname, bool def) const {
  171. return getInt(valname, def ? 1 : 0) > 0;
  172. }
  173. TCHAR* RegKey::getRepresentation(const TCHAR* valname) const {
  174. DWORD type, length;
  175. LONG result = RegQueryValueEx(key, valname, 0, &type, 0, &length);
  176. if (result != ERROR_SUCCESS)
  177. throw rdr::SystemException("get registry value length", result);
  178. CharArray data(length);
  179. result = RegQueryValueEx(key, valname, 0, &type, (BYTE*)data.buf, &length);
  180. if (result != ERROR_SUCCESS)
  181. throw rdr::SystemException("get registry value", result);
  182. switch (type) {
  183. case REG_BINARY:
  184. {
  185. TCharArray hex = rdr::HexOutStream::binToHexStr(data.buf, length);
  186. return hex.takeBuf();
  187. }
  188. case REG_SZ:
  189. if (length) {
  190. // We must terminate the string, just to be sure. Stupid Win32...
  191. int len = length/sizeof(TCHAR);
  192. TCharArray str(len+1);
  193. memcpy(str.buf, data.buf, length);
  194. str.buf[len] = 0;
  195. return str.takeBuf();
  196. } else {
  197. return tstrDup(_T(""));
  198. }
  199. case REG_DWORD:
  200. {
  201. TCharArray tmp(16);
  202. _stprintf(tmp.buf, _T("%u"), *((DWORD*)data.buf));
  203. return tmp.takeBuf();
  204. }
  205. default:
  206. throw rdr::Exception("unsupported registry type");
  207. }
  208. }
  209. bool RegKey::isValue(const TCHAR* valname) const {
  210. try {
  211. TCharArray tmp = getRepresentation(valname);
  212. return true;
  213. } catch(rdr::Exception) {
  214. return false;
  215. }
  216. }
  217. const TCHAR* RegKey::getValueName(int i) {
  218. DWORD maxValueNameLen;
  219. LONG result = RegQueryInfoKey(key, 0, 0, 0, 0, 0, 0, 0, &maxValueNameLen, 0, 0, 0);
  220. if (result != ERROR_SUCCESS)
  221. throw rdr::SystemException("RegQueryInfoKey", result);
  222. if (valueNameBufLen < maxValueNameLen + 1) {
  223. valueNameBufLen = maxValueNameLen + 1;
  224. delete [] valueName.buf;
  225. valueName.buf = new TCHAR[valueNameBufLen];
  226. }
  227. DWORD length = valueNameBufLen;
  228. result = RegEnumValue(key, i, valueName.buf, &length, NULL, 0, 0, 0);
  229. if (result == ERROR_NO_MORE_ITEMS) return 0;
  230. if (result != ERROR_SUCCESS)
  231. throw rdr::SystemException("RegEnumValue", result);
  232. return valueName.buf;
  233. }