forked from OpenVPN/openvpn-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.c
596 lines (520 loc) · 16.6 KB
/
registry.c
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2004 Mathias Sundman <[email protected]>
* 2016 Selva Nair <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <windows.h>
#include <tchar.h>
#include <shlobj.h>
#include "main.h"
#include "options.h"
#include "openvpn-gui-res.h"
#include "registry.h"
#include "localization.h"
extern options_t o;
struct regkey_str {
const WCHAR *name;
WCHAR *var;
int len;
const WCHAR *value;
} regkey_str[] = {
{L"config_dir", o.config_dir, _countof(o.config_dir), L"%USERPROFILE%\\OpenVPN\\config"},
{L"config_ext", o.ext_string, _countof(o.ext_string), L"ovpn"},
{L"log_dir", o.log_dir, _countof(o.log_dir), L"%USERPROFILE%\\OpenVPN\\log"}
};
struct regkey_int {
const WCHAR *name;
DWORD *var;
DWORD value;
} regkey_int[] = {
{L"log_append", &o.log_append, 0},
{L"iservice_admin", &o.iservice_admin, 1},
{L"show_balloon", &o.show_balloon, 1},
{L"silent_connection", &o.silent_connection, 0},
{L"preconnectscript_timeout", &o.preconnectscript_timeout, 10},
{L"connectscript_timeout", &o.connectscript_timeout, 30},
{L"disconnectscript_timeout", &o.disconnectscript_timeout, 10},
{L"show_script_window", &o.show_script_window, 0},
{L"config_menu_view", &o.config_menu_view, CONFIG_VIEW_AUTO},
{L"popup_mute_interval", &o.popup_mute_interval, 24},
{L"disable_popup_messages", &o.disable_popup_messages, 0},
{L"management_port_offset", &o.mgmt_port_offset, 25340},
{L"enable_peristent_connections", &o.enable_persistent, 2},
{L"enable_auto_restart", &o.enable_auto_restart, 1},
{L"ovpn_engine", &o.ovpn_engine, OPENVPN_ENGINE_OVPN2}
};
static int
RegValueExists(HKEY regkey, const WCHAR *name)
{
return (RegQueryValueEx(regkey, name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS);
}
static int
GetGlobalRegistryKeys()
{
TCHAR windows_dir[MAX_PATH];
HKEY regkey;
if (!GetWindowsDirectory(windows_dir, _countof(windows_dir)))
{
/* can't get windows dir */
ShowLocalizedMsg(IDS_ERR_GET_WINDOWS_DIR);
/* Use a default value */
_sntprintf_0(windows_dir, L"C:\\Windows");
}
/* set default editor and log_viewer as a fallback for opening config/log files */
_sntprintf_0(o.editor, L"%ls\\%ls", windows_dir, L"System32\\notepad.exe");
_sntprintf_0(o.log_viewer, L"%ls", o.editor);
/* Get path to OpenVPN installation. */
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\OpenVPN"), 0, KEY_READ, ®key)
!= ERROR_SUCCESS)
{
/* registry key not found */
regkey = NULL;
ShowLocalizedMsg(IDS_ERR_OPEN_REGISTRY);
}
if (!regkey || !GetRegistryValue(regkey, _T(""), o.install_path, _countof(o.install_path))
|| _tcslen(o.install_path) == 0)
{
/* error reading registry value */
if (regkey)
{
ShowLocalizedMsg(IDS_ERR_READING_REGISTRY);
}
/* Use a sane default value */
_sntprintf_0(o.install_path, _T("%ls"), _T("C:\\Program Files\\OpenVPN\\"));
}
if (o.install_path[_tcslen(o.install_path) - 1] != _T('\\'))
{
_tcscat(o.install_path, _T("\\"));
}
/* an admin-defined global config dir defined in HKLM\OpenVPN\config_dir */
if (!regkey || !GetRegistryValue(regkey, _T("config_dir"), o.global_config_dir, _countof(o.global_config_dir)))
{
/* use default = openvpnpath\config */
_sntprintf_0(o.global_config_dir, _T("%lsconfig"), o.install_path);
}
if (!regkey || !GetRegistryValue(regkey, _T("autostart_config_dir"), o.config_auto_dir, _countof(o.config_auto_dir)))
{
/* use default = openvpnpath\config-auto */
_sntprintf_0(o.config_auto_dir, L"%lsconfig-auto", o.install_path);
}
if (!regkey || !GetRegistryValue(regkey, _T("log_dir"), o.global_log_dir, _countof(o.global_log_dir)))
{
/* use default = openvpnpath\log */
_sntprintf_0(o.global_log_dir, L"%lslog", o.install_path);
}
if (!regkey || !GetRegistryValue(regkey, _T("ovpn_admin_group"), o.ovpn_admin_group, _countof(o.ovpn_admin_group)))
{
_tcsncpy(o.ovpn_admin_group, OVPN_ADMIN_GROUP, _countof(o.ovpn_admin_group)-1);
}
if (!regkey || !GetRegistryValue(regkey, _T("exe_path"), o.exe_path, _countof(o.exe_path)))
{
_sntprintf_0(o.exe_path, _T("%lsbin\\openvpn.exe"), o.install_path);
}
if (!regkey || !GetRegistryValue(regkey, _T("priority"), o.priority_string, _countof(o.priority_string)))
{
_tcsncpy(o.priority_string, _T("NORMAL_PRIORITY_CLASS"), _countof(o.priority_string)-1);
}
if (!regkey || !GetRegistryValueNumeric(regkey, _T("disable_save_passwords"), &o.disable_save_passwords))
{
o.disable_save_passwords = 0;
}
if (regkey)
{
RegCloseKey(regkey);
}
return true;
}
int
GetRegistryKeys()
{
HKEY regkey;
DWORD status;
int i;
if (!GetGlobalRegistryKeys())
{
return false;
}
status = RegOpenKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, KEY_READ, ®key);
for (i = 0; i < (int) _countof(regkey_str); ++i)
{
if (status != ERROR_SUCCESS
|| !GetRegistryValue(regkey, regkey_str[i].name, regkey_str[i].var, regkey_str[i].len))
{
/* no value found in registry, use the default */
wcsncpy(regkey_str[i].var, regkey_str[i].value, regkey_str[i].len);
regkey_str[i].var[regkey_str[i].len-1] = L'\0';
PrintDebug(L"default: %ls = %ls", regkey_str[i].name, regkey_str[i].var);
}
else
{
PrintDebug(L"from registry: %ls = %ls", regkey_str[i].name, regkey_str[i].var);
}
}
for (i = 0; i < (int) _countof(regkey_int); ++i)
{
if (status != ERROR_SUCCESS
|| !GetRegistryValueNumeric(regkey, regkey_int[i].name, regkey_int[i].var))
{
/* no value found in registry, use the default */
*regkey_int[i].var = regkey_int[i].value;
PrintDebug(L"default: %ls = %lu", regkey_int[i].name, *regkey_int[i].var);
}
else
{
PrintDebug(L"from registry: %ls = %lu", regkey_int[i].name, *regkey_int[i].var);
}
}
if (status == ERROR_SUCCESS)
{
RegCloseKey(regkey);
}
if ((o.disconnectscript_timeout == 0))
{
ShowLocalizedMsg(IDS_ERR_DISCONN_SCRIPT_TIMEOUT);
o.disconnectscript_timeout = 10;
}
if ((o.preconnectscript_timeout == 0))
{
ShowLocalizedMsg(IDS_ERR_PRECONN_SCRIPT_TIMEOUT);
o.preconnectscript_timeout = 10;
}
if (o.mgmt_port_offset < 1 || o.mgmt_port_offset > 61000)
{
o.mgmt_port_offset = 25340;
}
/* Read group policy setting for password reveal */
status = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Policies\\Microsoft\\Windows\\CredUI", 0, KEY_READ, ®key);
if (status != ERROR_SUCCESS
|| !GetRegistryValueNumeric(regkey, L"DisablePasswordReveal", &o.disable_password_reveal))
{
o.disable_password_reveal = 0;
PrintDebug(L"default: %ls = %lu", L"DisablePasswordReveal", o.disable_password_reveal);
}
else
{
PrintDebug(L"from policy: %ls = %lu", L"DisablePasswordReveal", o.disable_password_reveal);
}
if (status == ERROR_SUCCESS)
{
RegCloseKey(regkey);
}
ExpandOptions();
return true;
}
int
SaveRegistryKeys()
{
HKEY regkey;
DWORD status;
int i;
int ret = false;
status = RegCreateKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_WRITE|KEY_READ, NULL, ®key, NULL);
if (status != ERROR_SUCCESS)
{
ShowLocalizedMsg(IDS_ERR_CREATE_REG_HKCU_KEY, GUI_REGKEY_HKCU);
goto out;
}
for (i = 0; i < (int) _countof(regkey_str); ++i)
{
/* save only if the value differs from default or already present in registry */
if (CompareStringExpanded(regkey_str[i].var, regkey_str[i].value) != 0
|| RegValueExists(regkey, regkey_str[i].name) )
{
if (!SetRegistryValue(regkey, regkey_str[i].name, regkey_str[i].var))
{
goto out;
}
}
}
for (i = 0; i < (int) _countof(regkey_int); ++i)
{
if (*regkey_int[i].var != regkey_int[i].value
|| RegValueExists(regkey, regkey_int[i].name) )
{
if (!SetRegistryValueNumeric(regkey, regkey_int[i].name, *regkey_int[i].var))
{
goto out;
}
}
}
ret = true;
out:
if (status == ERROR_SUCCESS)
{
RegCloseKey(regkey);
}
return ret;
}
static BOOL
GetRegistryVersion(version_t *v)
{
HKEY regkey;
CLEAR(*v);
DWORD len = sizeof(*v);
if (RegOpenKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, KEY_READ, ®key) == ERROR_SUCCESS)
{
if (RegGetValueW(regkey, NULL, L"version", RRF_RT_REG_BINARY, NULL, v, &len)
!= ERROR_SUCCESS)
{
CLEAR(*v);
}
RegCloseKey(regkey);
}
return true;
}
static BOOL
SetRegistryVersion(const version_t *v)
{
HKEY regkey;
DWORD status;
BOOL ret = false;
status = RegCreateKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_WRITE, NULL, ®key, NULL);
if (status == ERROR_SUCCESS)
{
if (RegSetValueEx(regkey, L"version", 0, REG_BINARY, (const BYTE *) v, sizeof(*v)) == ERROR_SUCCESS)
{
ret = true;
}
RegCloseKey(regkey);
}
else
{
PrintDebug(L"Eror opening/creating 'HKCU\\%ls' registry key", GUI_REGKEY_HKCU);
}
return ret;
}
static int
MigrateNilingsKeys()
{
DWORD status;
BOOL ret = false;
HKEY regkey, regkey_proxy, regkey_nilings;
status = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Nilings\\OpenVPN-GUI", 0,
KEY_READ, ®key_nilings);
if (status != ERROR_SUCCESS)
{
return true; /* No old keys to migrate */
}
status = RegCreateKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, ®key, NULL);
if (status != ERROR_SUCCESS)
{
ShowLocalizedMsg(IDS_ERR_CREATE_REG_HKCU_KEY, GUI_REGKEY_HKCU);
RegCloseKey(regkey_nilings);
return false;
}
/* For some reason this needs ALL_ACCESS for the CopyTree below to work */
status = RegCreateKeyEx(regkey, L"proxy", 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, ®key_proxy, NULL);
if (status == ERROR_SUCCESS)
{
DWORD ui_lang;
/* Move language setting from Nilings to GUI_REGKEY_HKCU */
if (GetRegistryValueNumeric(regkey_nilings, L"ui_language", &ui_lang))
{
SetRegistryValueNumeric(regkey, L"ui_language", ui_lang);
}
status = RegCopyTree(regkey_nilings, NULL, regkey_proxy);
if (status == ERROR_SUCCESS)
{
RegDeleteValue(regkey_proxy, L"ui_language"); /* in case copied here */
ret = true;
}
RegCloseKey(regkey_proxy);
}
else
{
PrintDebug(L"Error creating key 'proxy' in HKCU\\%ls", GUI_REGKEY_HKCU);
}
RegCloseKey(regkey);
RegCloseKey(regkey_nilings);
return ret;
}
int
UpdateRegistry(void)
{
version_t v;
GetRegistryVersion(&v);
if (memcmp(&v, &o.version, sizeof(v)) == 0)
{
return true;
}
switch (v.major)
{
case 0: /* Cleanup GUI_REGKEY_HKCU and migrate any values under Nilings */
RegDeleteTree(HKEY_CURRENT_USER, GUI_REGKEY_HKCU); /* delete all values and subkeys */
if (!MigrateNilingsKeys())
{
return false;
}
/* fall through to handle further updates */
case 11:
/* current version -- nothing to do */
break;
default:
break;
}
SetRegistryVersion(&o.version);
PrintDebug(L"Registry updated to version %hu.%hu", o.version.major, o.version.minor);
return true;
}
LONG
GetRegistryValue(HKEY regkey, const TCHAR *name, TCHAR *data, DWORD len)
{
LONG status;
DWORD type;
DWORD data_len;
data_len = len * sizeof(*data);
/* get a registry string */
status = RegQueryValueEx(regkey, name, NULL, &type, (byte *) data, &data_len);
if (status != ERROR_SUCCESS || type != REG_SZ)
{
return(0);
}
data_len /= sizeof(*data);
if (data_len > 0)
{
data[data_len - 1] = L'\0'; /* REG_SZ strings are not guaranteed to be null-terminated */
}
else
{
data[0] = L'\0';
}
return(data_len);
}
LONG
GetRegistryValueNumeric(HKEY regkey, const TCHAR *name, DWORD *data)
{
DWORD type;
DWORD size = sizeof(*data);
LONG status = RegQueryValueEx(regkey, name, NULL, &type, (PBYTE) data, &size);
return (type == REG_DWORD && status == ERROR_SUCCESS) ? 1 : 0;
}
int
SetRegistryValue(HKEY regkey, const TCHAR *name, const TCHAR *data)
{
/* set a registry string */
DWORD size = (_tcslen(data) + 1) * sizeof(*data);
if (RegSetValueEx(regkey, name, 0, REG_SZ, (PBYTE) data, size) != ERROR_SUCCESS)
{
/* Error writing registry value */
ShowLocalizedMsg(IDS_ERR_WRITE_REGVALUE, GUI_REGKEY_HKCU, name);
return(0);
}
return(1);
}
int
SetRegistryValueNumeric(HKEY regkey, const TCHAR *name, DWORD data)
{
LONG status = RegSetValueEx(regkey, name, 0, REG_DWORD, (PBYTE) &data, sizeof(data));
if (status == ERROR_SUCCESS)
{
return 1;
}
ShowLocalizedMsg(IDS_ERR_WRITE_REGVALUE, GUI_REGKEY_HKCU, name);
return 0;
}
/*
* Open HKCU\Software\OpenVPN-GUI\configs\config-name.
* The caller must close the key. Returns 1 on success.
*/
static int
OpenConfigRegistryKey(const WCHAR *config_name, HKEY *regkey, BOOL create)
{
DWORD status;
const WCHAR fmt[] = L"SOFTWARE\\OpenVPN-GUI\\configs\\%ls";
int count = (wcslen(config_name) + wcslen(fmt) + 1);
WCHAR *name = malloc(count * sizeof(WCHAR));
if (!name)
{
return 0;
}
_snwprintf(name, count, fmt, config_name);
name[count-1] = L'\0';
if (!create)
{
status = RegOpenKeyEx(HKEY_CURRENT_USER, name, 0, KEY_READ | KEY_WRITE, regkey);
}
else
{
/* create if key doesn't exist */
status = RegCreateKeyEx(HKEY_CURRENT_USER, name, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, regkey, NULL);
}
free(name);
return (status == ERROR_SUCCESS);
}
int
SetConfigRegistryValueBinary(const WCHAR *config_name, const WCHAR *name, const BYTE *data, DWORD len)
{
HKEY regkey;
DWORD status;
if (!OpenConfigRegistryKey(config_name, ®key, TRUE))
{
return 0;
}
status = RegSetValueEx(regkey, name, 0, REG_BINARY, data, len);
RegCloseKey(regkey);
return (status == ERROR_SUCCESS);
}
/*
* Read registry value into the user supplied buffer data that can hold
* up to len bytes. Returns the actual number of bytes read or zero on error.
* If data is NULL returns the required buffer size, and no data is read.
*/
DWORD
GetConfigRegistryValue(const WCHAR *config_name, const WCHAR *name, BYTE *data, DWORD len)
{
DWORD status;
DWORD type;
HKEY regkey;
if (!OpenConfigRegistryKey(config_name, ®key, FALSE))
{
return 0;
}
status = RegQueryValueEx(regkey, name, NULL, &type, data, &len);
RegCloseKey(regkey);
if (status == ERROR_SUCCESS)
{
return len;
}
else
{
return 0;
}
}
int
DeleteConfigRegistryValue(const WCHAR *config_name, const WCHAR *name)
{
DWORD status;
HKEY regkey;
if (!OpenConfigRegistryKey(config_name, ®key, FALSE))
{
return 0;
}
status = RegDeleteValue(regkey, name);
RegCloseKey(regkey);
return (status == ERROR_SUCCESS);
}