forked from zerotier/ZeroTierOne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinDNSHelper.cpp
353 lines (300 loc) · 9.42 KB
/
WinDNSHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "WinDNSHelper.hpp"
#include <comdef.h>
#include <WbemIdl.h>
#include <vector>
#include <string>
#include <sstream>
#include <strsafe.h>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
namespace ZeroTier
{
BOOL RegDelnodeRecurse(HKEY hKeyRoot, LPTSTR lpSubKey)
{
LPTSTR lpEnd;
LONG lResult;
DWORD dwSize;
TCHAR szName[MAX_PATH];
HKEY hKey;
FILETIME ftWrite;
// First, see if we can delete the key without having
// to recurse.
lResult = RegDeleteKey(hKeyRoot, lpSubKey);
if (lResult == ERROR_SUCCESS)
return TRUE;
lResult = RegOpenKeyEx(hKeyRoot, lpSubKey, 0, KEY_READ, &hKey);
if (lResult != ERROR_SUCCESS)
{
if (lResult == ERROR_FILE_NOT_FOUND) {
return TRUE;
}
else {
return FALSE;
}
}
// Check for an ending slash and add one if it is missing.
lpEnd = lpSubKey + lstrlen(lpSubKey);
if (*(lpEnd - 1) != TEXT('\\'))
{
*lpEnd = TEXT('\\');
lpEnd++;
*lpEnd = TEXT('\0');
}
// Enumerate the keys
dwSize = MAX_PATH;
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
NULL, NULL, &ftWrite);
if (lResult == ERROR_SUCCESS)
{
do {
*lpEnd = TEXT('\0');
StringCchCat(lpSubKey, MAX_PATH * 2, szName);
if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
break;
}
dwSize = MAX_PATH;
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
NULL, NULL, &ftWrite);
} while (lResult == ERROR_SUCCESS);
}
lpEnd--;
*lpEnd = TEXT('\0');
RegCloseKey(hKey);
// Try again to delete the key.
lResult = RegDeleteKey(hKeyRoot, lpSubKey);
if (lResult == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
//*************************************************************
//
// RegDelnode()
//
// Purpose: Deletes a registry key and all its subkeys / values.
//
// Parameters: hKeyRoot - Root key
// lpSubKey - SubKey to delete
//
// Return: TRUE if successful.
// FALSE if an error occurs.
//
//*************************************************************
BOOL RegDelnode(HKEY hKeyRoot, LPCTSTR lpSubKey)
{
TCHAR szDelKey[MAX_PATH * 2];
StringCchCopy(szDelKey, MAX_PATH * 2, lpSubKey);
return RegDelnodeRecurse(hKeyRoot, szDelKey);
}
std::vector<std::string> getSubKeys(const char* key)
{
std::vector<std::string> subkeys;
HKEY hKey;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
key,
0,
KEY_READ,
&hKey) == ERROR_SUCCESS) {
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;
TCHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
for (i = 0; i < cSubKeys; ++i) {
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(
hKey,
i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS) {
subkeys.push_back(achKey);
}
}
}
RegCloseKey(hKey);
return subkeys;
}
std::vector<std::string> getValueList(const char* key) {
std::vector<std::string> values;
HKEY hKey;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
key,
0,
KEY_READ,
&hKey) == ERROR_SUCCESS) {
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;
TCHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
for (i = 0, retCode = ERROR_SUCCESS; i < cValues; ++i) {
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(
hKey,
i,
achValue,
&cchValue,
NULL,
NULL,
NULL,
NULL);
if (retCode == ERROR_SUCCESS) {
values.push_back(achValue);
}
}
}
RegCloseKey(hKey);
return values;
}
std::pair<bool, std::string> WinDNSHelper::hasDNSConfig(uint64_t nwid)
{
char networkStr[20] = { 0 };
sprintf(networkStr, "%.16llx", nwid);
const char* baseKey = "SYSTEM\\CurrentControlSet\\Services\\Dnscache\\Parameters\\DnsPolicyConfig";
auto subkeys = getSubKeys(baseKey);
for (auto it = subkeys.begin(); it != subkeys.end(); ++it) {
char sub[MAX_KEY_LENGTH] = { 0 };
sprintf(sub, "%s\\%s", baseKey, it->c_str());
auto dnsRecords = getValueList(sub);
for (auto it2 = dnsRecords.begin(); it2 != dnsRecords.end(); ++it2) {
if ((*it2) == "Comment") {
HKEY hKey;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
sub,
0,
KEY_READ,
&hKey) == ERROR_SUCCESS) {
char buf[16384] = { 0 };
DWORD size = sizeof(buf);
DWORD retCode = RegGetValueA(
HKEY_LOCAL_MACHINE,
sub,
it2->c_str(),
RRF_RT_REG_SZ,
NULL,
&buf,
&size);
if (retCode == ERROR_SUCCESS) {
if (std::string(networkStr) == std::string(buf)) {
RegCloseKey(hKey);
return std::make_pair(true, std::string(sub));
}
}
else {
}
}
RegCloseKey(hKey);
}
}
}
return std::make_pair(false, std::string());
}
void WinDNSHelper::setDNS(uint64_t nwid, const char* domain, const std::vector<InetAddress>& servers)
{
auto hasConfig = hasDNSConfig(nwid);
std::stringstream ss;
for (auto it = servers.begin(); it != servers.end(); ++it) {
char ipaddr[256] = { 0 };
ss << it->toIpString(ipaddr);
if ((it + 1) != servers.end()) {
ss << ";";
}
}
std::string serverValue = ss.str();
if (hasConfig.first) {
// update existing config
HKEY dnsKey;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, hasConfig.second.c_str(), 0, KEY_READ | KEY_WRITE, &dnsKey) == ERROR_SUCCESS) {
auto retCode = RegSetKeyValueA(dnsKey, NULL, "GenericDNSServers", REG_SZ, serverValue.data(), (DWORD)serverValue.length());
if (retCode != ERROR_SUCCESS) {
fprintf(stderr, "Error writing dns servers: %d\n", retCode);
}
}
} else {
// add new config
const char* baseKey = "SYSTEM\\CurrentControlSet\\Services\\Dnscache\\Parameters\\DnsPolicyConfig";
GUID guid;
CoCreateGuid(&guid);
wchar_t guidTmp[128] = { 0 };
char guidStr[128] = { 0 };
StringFromGUID2(guid, guidTmp, 128);
wcstombs(guidStr, guidTmp, 128);
char fullKey[MAX_KEY_LENGTH] = { 0 };
sprintf(fullKey, "%s\\%s", baseKey, guidStr);
HKEY dnsKey;
RegCreateKeyA(HKEY_LOCAL_MACHINE, fullKey, &dnsKey);
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, fullKey, 0, KEY_READ | KEY_WRITE, &dnsKey) == ERROR_SUCCESS) {
char nwString[32] = { 0 };
sprintf(nwString, "%.16llx", nwid);
RegSetKeyValueA(dnsKey, NULL, "Comment", REG_SZ, nwString, strlen(nwString));
DWORD configOpts = 8;
RegSetKeyValueA(dnsKey, NULL, "ConfigOptions", REG_DWORD, &configOpts, sizeof(DWORD));
RegSetKeyValueA(dnsKey, NULL, "DisplayName", REG_SZ, "", 0);
RegSetKeyValueA(dnsKey, NULL, "GenericDNSServers", REG_SZ, serverValue.data(), serverValue.length());
RegSetKeyValueA(dnsKey, NULL, "IPSECCARestriction", REG_SZ, "", 0);
std::string d = "." + std::string(domain);
RegSetKeyValueA(dnsKey, NULL, "Name", REG_MULTI_SZ, d.data(), d.length());
DWORD version = 2;
RegSetKeyValueA(dnsKey, NULL, "Version", REG_DWORD, &version, sizeof(DWORD));
}
}
}
void WinDNSHelper::removeDNS(uint64_t nwid)
{
auto hasConfig = hasDNSConfig(nwid);
if (hasConfig.first) {
RegDelnode(HKEY_LOCAL_MACHINE, hasConfig.second.c_str());
}
}
}