WinDNSHelper.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #include "WinDNSHelper.hpp"
  9. #include <WbemIdl.h>
  10. #include <comdef.h>
  11. #include <sstream>
  12. #include <string>
  13. #include <strsafe.h>
  14. #include <vector>
  15. #define MAX_KEY_LENGTH 255
  16. #define MAX_VALUE_NAME 16383
  17. namespace ZeroTier {
  18. BOOL RegDelnodeRecurse(HKEY hKeyRoot, LPTSTR lpSubKey)
  19. {
  20. LPTSTR lpEnd;
  21. LONG lResult;
  22. DWORD dwSize;
  23. TCHAR szName[MAX_PATH];
  24. HKEY hKey;
  25. FILETIME ftWrite;
  26. // First, see if we can delete the key without having
  27. // to recurse.
  28. lResult = RegDeleteKey(hKeyRoot, lpSubKey);
  29. if (lResult == ERROR_SUCCESS)
  30. return TRUE;
  31. lResult = RegOpenKeyEx(hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);
  32. if (lResult != ERROR_SUCCESS) {
  33. if (lResult == ERROR_FILE_NOT_FOUND) {
  34. return TRUE;
  35. }
  36. else {
  37. return FALSE;
  38. }
  39. }
  40. // Check for an ending slash and add one if it is missing.
  41. lpEnd = lpSubKey + lstrlen(lpSubKey);
  42. if (*(lpEnd - 1) != TEXT('\\')) {
  43. *lpEnd = TEXT('\\');
  44. lpEnd++;
  45. *lpEnd = TEXT('\0');
  46. }
  47. // Enumerate the keys
  48. dwSize = MAX_PATH;
  49. lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL, NULL, NULL, &ftWrite);
  50. if (lResult == ERROR_SUCCESS) {
  51. do {
  52. *lpEnd = TEXT('\0');
  53. StringCchCat(lpSubKey, MAX_PATH * 2, szName);
  54. if (! RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
  55. break;
  56. }
  57. dwSize = MAX_PATH;
  58. lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL, NULL, NULL, &ftWrite);
  59. } while (lResult == ERROR_SUCCESS);
  60. }
  61. lpEnd--;
  62. *lpEnd = TEXT('\0');
  63. RegCloseKey(hKey);
  64. // Try again to delete the key.
  65. lResult = RegDeleteKey(hKeyRoot, lpSubKey);
  66. if (lResult == ERROR_SUCCESS)
  67. return TRUE;
  68. return FALSE;
  69. }
  70. //*************************************************************
  71. //
  72. // RegDelnode()
  73. //
  74. // Purpose: Deletes a registry key and all its subkeys / values.
  75. //
  76. // Parameters: hKeyRoot - Root key
  77. // lpSubKey - SubKey to delete
  78. //
  79. // Return: TRUE if successful.
  80. // FALSE if an error occurs.
  81. //
  82. //*************************************************************
  83. BOOL RegDelnode(HKEY hKeyRoot, LPCTSTR lpSubKey)
  84. {
  85. TCHAR szDelKey[MAX_PATH * 2];
  86. StringCchCopy(szDelKey, MAX_PATH * 2, lpSubKey);
  87. return RegDelnodeRecurse(hKeyRoot, szDelKey);
  88. }
  89. std::vector<std::string> getSubKeys(const char* key)
  90. {
  91. std::vector<std::string> subkeys;
  92. HKEY hKey;
  93. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  94. TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
  95. DWORD cbName; // size of name string
  96. TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
  97. DWORD cchClassName = MAX_PATH; // size of class string
  98. DWORD cSubKeys = 0; // number of subkeys
  99. DWORD cbMaxSubKey; // longest subkey size
  100. DWORD cchMaxClass; // longest class string
  101. DWORD cValues; // number of values for key
  102. DWORD cchMaxValue; // longest value name
  103. DWORD cbMaxValueData; // longest value data
  104. DWORD cbSecurityDescriptor; // size of security descriptor
  105. FILETIME ftLastWriteTime; // last write time
  106. DWORD i, retCode;
  107. TCHAR achValue[MAX_VALUE_NAME];
  108. DWORD cchValue = MAX_VALUE_NAME;
  109. retCode = RegQueryInfoKey(
  110. hKey, // key handle
  111. achClass, // buffer for class name
  112. &cchClassName, // size of class string
  113. NULL, // reserved
  114. &cSubKeys, // number of subkeys
  115. &cbMaxSubKey, // longest subkey size
  116. &cchMaxClass, // longest class string
  117. &cValues, // number of values for this key
  118. &cchMaxValue, // longest value name
  119. &cbMaxValueData, // longest value data
  120. &cbSecurityDescriptor, // security descriptor
  121. &ftLastWriteTime); // last write time
  122. for (i = 0; i < cSubKeys; ++i) {
  123. cbName = MAX_KEY_LENGTH;
  124. retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime);
  125. if (retCode == ERROR_SUCCESS) {
  126. subkeys.push_back(achKey);
  127. }
  128. }
  129. }
  130. RegCloseKey(hKey);
  131. return subkeys;
  132. }
  133. std::vector<std::string> getValueList(const char* key)
  134. {
  135. std::vector<std::string> values;
  136. HKEY hKey;
  137. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  138. TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
  139. DWORD cbName; // size of name string
  140. TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
  141. DWORD cchClassName = MAX_PATH; // size of class string
  142. DWORD cSubKeys = 0; // number of subkeys
  143. DWORD cbMaxSubKey; // longest subkey size
  144. DWORD cchMaxClass; // longest class string
  145. DWORD cValues; // number of values for key
  146. DWORD cchMaxValue; // longest value name
  147. DWORD cbMaxValueData; // longest value data
  148. DWORD cbSecurityDescriptor; // size of security descriptor
  149. FILETIME ftLastWriteTime; // last write time
  150. DWORD i, retCode;
  151. TCHAR achValue[MAX_VALUE_NAME];
  152. DWORD cchValue = MAX_VALUE_NAME;
  153. retCode = RegQueryInfoKey(
  154. hKey, // key handle
  155. achClass, // buffer for class name
  156. &cchClassName, // size of class string
  157. NULL, // reserved
  158. &cSubKeys, // number of subkeys
  159. &cbMaxSubKey, // longest subkey size
  160. &cchMaxClass, // longest class string
  161. &cValues, // number of values for this key
  162. &cchMaxValue, // longest value name
  163. &cbMaxValueData, // longest value data
  164. &cbSecurityDescriptor, // security descriptor
  165. &ftLastWriteTime); // last write time
  166. for (i = 0, retCode = ERROR_SUCCESS; i < cValues; ++i) {
  167. cchValue = MAX_VALUE_NAME;
  168. achValue[0] = '\0';
  169. retCode = RegEnumValue(hKey, i, achValue, &cchValue, NULL, NULL, NULL, NULL);
  170. if (retCode == ERROR_SUCCESS) {
  171. values.push_back(achValue);
  172. }
  173. }
  174. }
  175. RegCloseKey(hKey);
  176. return values;
  177. }
  178. std::pair<bool, std::string> WinDNSHelper::hasDNSConfig(uint64_t nwid)
  179. {
  180. char networkStr[20] = { 0 };
  181. sprintf(networkStr, "%.16llx", nwid);
  182. const char* baseKey = "SYSTEM\\CurrentControlSet\\Services\\Dnscache\\Parameters\\DnsPolicyConfig";
  183. auto subkeys = getSubKeys(baseKey);
  184. for (auto it = subkeys.begin(); it != subkeys.end(); ++it) {
  185. char sub[MAX_KEY_LENGTH] = { 0 };
  186. sprintf(sub, "%s\\%s", baseKey, it->c_str());
  187. auto dnsRecords = getValueList(sub);
  188. for (auto it2 = dnsRecords.begin(); it2 != dnsRecords.end(); ++it2) {
  189. if ((*it2) == "Comment") {
  190. HKEY hKey;
  191. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, sub, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
  192. char buf[16384] = { 0 };
  193. DWORD size = sizeof(buf);
  194. DWORD retCode = RegGetValueA(HKEY_LOCAL_MACHINE, sub, it2->c_str(), RRF_RT_REG_SZ, NULL, &buf, &size);
  195. if (retCode == ERROR_SUCCESS) {
  196. if (std::string(networkStr) == std::string(buf)) {
  197. RegCloseKey(hKey);
  198. return std::make_pair(true, std::string(sub));
  199. }
  200. }
  201. else {
  202. }
  203. }
  204. RegCloseKey(hKey);
  205. }
  206. }
  207. }
  208. return std::make_pair(false, std::string());
  209. }
  210. void WinDNSHelper::setDNS(uint64_t nwid, const char* domain, const std::vector<InetAddress>& servers)
  211. {
  212. auto hasConfig = hasDNSConfig(nwid);
  213. std::stringstream ss;
  214. for (auto it = servers.begin(); it != servers.end(); ++it) {
  215. char ipaddr[256] = { 0 };
  216. ss << it->toIpString(ipaddr);
  217. if ((it + 1) != servers.end()) {
  218. ss << ";";
  219. }
  220. }
  221. std::string serverValue = ss.str();
  222. if (hasConfig.first) {
  223. // update existing config
  224. HKEY dnsKey;
  225. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, hasConfig.second.c_str(), 0, KEY_READ | KEY_WRITE, &dnsKey) == ERROR_SUCCESS) {
  226. auto retCode = RegSetKeyValueA(dnsKey, NULL, "GenericDNSServers", REG_SZ, serverValue.data(), (DWORD)serverValue.length());
  227. if (retCode != ERROR_SUCCESS) {
  228. fprintf(stderr, "Error writing dns servers: %d\n", retCode);
  229. }
  230. }
  231. }
  232. else {
  233. // add new config
  234. const char* baseKey = "SYSTEM\\CurrentControlSet\\Services\\Dnscache\\Parameters\\DnsPolicyConfig";
  235. GUID guid;
  236. CoCreateGuid(&guid);
  237. wchar_t guidTmp[128] = { 0 };
  238. char guidStr[128] = { 0 };
  239. StringFromGUID2(guid, guidTmp, 128);
  240. wcstombs(guidStr, guidTmp, 128);
  241. char fullKey[MAX_KEY_LENGTH] = { 0 };
  242. sprintf(fullKey, "%s\\%s", baseKey, guidStr);
  243. HKEY dnsKey;
  244. RegCreateKeyA(HKEY_LOCAL_MACHINE, fullKey, &dnsKey);
  245. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, fullKey, 0, KEY_READ | KEY_WRITE, &dnsKey) == ERROR_SUCCESS) {
  246. char nwString[32] = { 0 };
  247. sprintf(nwString, "%.16llx", nwid);
  248. RegSetKeyValueA(dnsKey, NULL, "Comment", REG_SZ, nwString, strlen(nwString));
  249. DWORD configOpts = 8;
  250. RegSetKeyValueA(dnsKey, NULL, "ConfigOptions", REG_DWORD, &configOpts, sizeof(DWORD));
  251. RegSetKeyValueA(dnsKey, NULL, "DisplayName", REG_SZ, "", 0);
  252. RegSetKeyValueA(dnsKey, NULL, "GenericDNSServers", REG_SZ, serverValue.data(), serverValue.length());
  253. RegSetKeyValueA(dnsKey, NULL, "IPSECCARestriction", REG_SZ, "", 0);
  254. std::string d = "." + std::string(domain);
  255. RegSetKeyValueA(dnsKey, NULL, "Name", REG_MULTI_SZ, d.data(), d.length());
  256. DWORD version = 2;
  257. RegSetKeyValueA(dnsKey, NULL, "Version", REG_DWORD, &version, sizeof(DWORD));
  258. }
  259. }
  260. }
  261. void WinDNSHelper::removeDNS(uint64_t nwid)
  262. {
  263. auto hasConfig = hasDNSConfig(nwid);
  264. if (hasConfig.first) {
  265. RegDelnode(HKEY_LOCAL_MACHINE, hasConfig.second.c_str());
  266. }
  267. }
  268. } // namespace ZeroTier