OpenVPN
common.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2011-2026 Heiko Hund <heiko.hund@sophos.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include "service.h"
24#include "validate.h"
25// cppcheck-suppress missingInclude ; we run cppcheck on Linux w/o this file
26#include "eventmsg.h"
27
28#include <pathcch.h>
29
30LPCWSTR service_instance = L"";
31static wchar_t win_sys_path[MAX_PATH];
32
33#if defined(__GNUC__) || defined(__clang__)
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wsign-conversion"
36#endif
37
38static DWORD
39GetRegString(HKEY key, LPCWSTR value, LPWSTR data, DWORD size, LPCWSTR default_value)
40{
41 LONG status = RegGetValue(key, NULL, value, RRF_RT_REG_SZ, NULL, (LPBYTE)data, &size);
42
43 if (status == ERROR_FILE_NOT_FOUND && default_value)
44 {
45 size_t len = size / sizeof(data[0]);
46 if (swprintf(data, len, L"%ls", default_value) >= 0)
47 {
48 status = ERROR_SUCCESS;
49 }
50 }
51
52 if (status != ERROR_SUCCESS)
53 {
54 SetLastError(status);
55 return MsgToEventLog(
57 L"Error querying registry value: HKLM\\SOFTWARE\\" _L(PACKAGE_NAME) L"%ls\\%ls",
58 service_instance, value);
59 }
60
61 return ERROR_SUCCESS;
62}
63
64
73static BOOL
74ensure_trailing_backslash(PWSTR dir, size_t size)
75{
76 HRESULT res = PathCchAddBackslash(dir, size);
77 return (res == S_OK || res == S_FALSE) ? TRUE : FALSE;
78}
79
80
81DWORD
83{
84 WCHAR reg_path[256];
85 WCHAR priority[64];
86 WCHAR append[2];
87 DWORD error;
88 HKEY key;
89 WCHAR install_path[MAX_PATH];
90 WCHAR default_value[MAX_PATH];
91
92 swprintf(reg_path, _countof(reg_path), L"SOFTWARE\\" _L(PACKAGE_NAME) L"%ls", service_instance);
93
94 LONG status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_path, 0, KEY_READ, &key);
95 if (status != ERROR_SUCCESS)
96 {
97 SetLastError(status);
98 return MsgToEventLog(M_SYSERR, L"Could not open Registry key HKLM\\%ls not found",
99 reg_path);
100 }
101
102 /* The default value of REG_KEY is the install path */
103 status = GetRegString(key, NULL, install_path, sizeof(install_path), NULL);
104 if (status != ERROR_SUCCESS)
105 {
106 error = status;
107 goto out;
108 }
109
110 swprintf(default_value, _countof(default_value), L"%ls\\bin\\openvpn.exe", install_path);
111 error = GetRegString(key, L"exe_path", s->exe_path, sizeof(s->exe_path), default_value);
112 if (error != ERROR_SUCCESS)
113 {
114 goto out;
115 }
116
117 swprintf(default_value, _countof(default_value), L"%ls\\config\\", install_path);
118 error = GetRegString(key, L"config_dir", s->config_dir, sizeof(s->config_dir), default_value);
119 if (error != ERROR_SUCCESS || !ensure_trailing_backslash(s->config_dir, _countof(s->config_dir)))
120 {
121 goto out;
122 }
123
124 swprintf(default_value, _countof(default_value), L"%ls\\bin\\", install_path);
125 error = GetRegString(key, L"bin_dir", s->bin_dir, sizeof(s->bin_dir), default_value);
126 if (error != ERROR_SUCCESS || !ensure_trailing_backslash(s->bin_dir, _countof(s->bin_dir)))
127 {
128 goto out;
129 }
130
131 error = GetRegString(key, L"config_ext", s->ext_string, sizeof(s->ext_string), L".ovpn");
132 if (error != ERROR_SUCCESS)
133 {
134 goto out;
135 }
136
137 swprintf(default_value, _countof(default_value), L"%ls\\log\\", install_path);
138 error = GetRegString(key, L"log_dir", s->log_dir, sizeof(s->log_dir), default_value);
139 if (error != ERROR_SUCCESS || !ensure_trailing_backslash(s->log_dir, _countof(s->log_dir)))
140 {
141 goto out;
142 }
143
144 error = GetRegString(key, L"priority", priority, sizeof(priority), L"NORMAL_PRIORITY_CLASS");
145 if (error != ERROR_SUCCESS)
146 {
147 goto out;
148 }
149
150 error = GetRegString(key, L"log_append", append, sizeof(append), L"0");
151 if (error != ERROR_SUCCESS)
152 {
153 goto out;
154 }
155
156 /* read if present, else use default */
157 error = GetRegString(key, L"ovpn_admin_group", s->ovpn_admin_group, sizeof(s->ovpn_admin_group),
159 if (error != ERROR_SUCCESS)
160 {
161 goto out;
162 }
163
164 error = GetRegString(key, L"ovpn_service_user", s->ovpn_service_user,
166 if (error != ERROR_SUCCESS)
167 {
168 goto out;
169 }
170
171 /* set process priority */
172 if (!_wcsicmp(priority, L"IDLE_PRIORITY_CLASS"))
173 {
174 s->priority = IDLE_PRIORITY_CLASS;
175 }
176 else if (!_wcsicmp(priority, L"BELOW_NORMAL_PRIORITY_CLASS"))
177 {
178 s->priority = BELOW_NORMAL_PRIORITY_CLASS;
179 }
180 else if (!_wcsicmp(priority, L"NORMAL_PRIORITY_CLASS"))
181 {
182 s->priority = NORMAL_PRIORITY_CLASS;
183 }
184 else if (!_wcsicmp(priority, L"ABOVE_NORMAL_PRIORITY_CLASS"))
185 {
186 s->priority = ABOVE_NORMAL_PRIORITY_CLASS;
187 }
188 else if (!_wcsicmp(priority, L"HIGH_PRIORITY_CLASS"))
189 {
190 s->priority = HIGH_PRIORITY_CLASS;
191 }
192 else
193 {
194 SetLastError(ERROR_INVALID_DATA);
195 error = MsgToEventLog(M_SYSERR, L"Unknown priority name: %ls", priority);
196 goto out;
197 }
198
199 /* set log file append/truncate flag */
200 if (append[0] == L'0')
201 {
202 s->append = FALSE;
203 }
204 else if (append[0] == L'1')
205 {
206 s->append = TRUE;
207 }
208 else
209 {
210 SetLastError(ERROR_INVALID_DATA);
211 error = MsgToEventLog(M_ERR, L"Log file append flag (given as '%ls') must be '0' or '1'",
212 append);
213 goto out;
214 }
215
216out:
217 RegCloseKey(key);
218 return error;
219}
220
221
222LPCWSTR
224{
225 DWORD error;
226 static WCHAR buf[256];
227 DWORD len;
228 LPWSTR tmp = NULL;
229
230 error = GetLastError();
231 len = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
232 | FORMAT_MESSAGE_IGNORE_INSERTS,
233 NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&tmp, 0, NULL);
234
235 if (!len || !tmp)
236 {
237 swprintf(buf, _countof(buf), L"Unknown error (0x%lx)", error);
238 if (tmp)
239 {
240 LocalFree(tmp);
241 }
242 return buf;
243 }
244
245 /* trim trailing CR / LF / spaces safely */
246 while (len && (tmp[len - 1] == L'\r' || tmp[len - 1] == L'\n' || tmp[len - 1] == L' '))
247 {
248 tmp[--len] = L'\0';
249 }
250
251 swprintf(buf, _countof(buf), L"%ls (0x%lx)", tmp, error);
252
253 LocalFree(tmp);
254 return buf;
255}
256
257
258DWORD
259MsgToEventLog(DWORD flags, LPCWSTR format, ...)
260{
261 HANDLE hEventSource;
262 DWORD error = 0;
263 LPCWSTR err_msg = L"";
264
265 if (flags & MSG_FLAGS_SYS_CODE)
266 {
267 error = GetLastError();
268 err_msg = GetLastErrorText();
269 }
270
271 hEventSource = RegisterEventSource(NULL, APPNAME);
272 if (hEventSource != NULL)
273 {
274 va_list arglist;
275 WCHAR msg[2][256];
276
277 swprintf(msg[0], _countof(msg[0]), L"%ls%ls%ls: %ls", APPNAME, service_instance,
278 (flags & MSG_FLAGS_ERROR) ? L" error" : L"", err_msg);
279
280 va_start(arglist, format);
281 vswprintf(msg[1], _countof(msg[1]), format, arglist);
282 va_end(arglist);
283
284 const WCHAR *mesg[] = { msg[0], msg[1] };
285 ReportEvent(hEventSource,
286 (flags & MSG_FLAGS_ERROR) ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
287 0,
288 EVT_TEXT_2,
289 NULL,
290 2,
291 0,
292 mesg,
293 NULL);
294 DeregisterEventSource(hEventSource);
295 }
296
297 return error;
298}
299
300wchar_t *
301utf8to16_size(const char *utf8, int size)
302{
303 int n = MultiByteToWideChar(CP_UTF8, 0, utf8, size, NULL, 0);
304 if (n == 0)
305 {
306 return NULL;
307 }
308 wchar_t *utf16 = malloc(n * sizeof(wchar_t));
309 if (!utf16)
310 {
311 return NULL;
312 }
313 MultiByteToWideChar(CP_UTF8, 0, utf8, size, utf16, n);
314 return utf16;
315}
316
317#if defined(__GNUC__) || defined(__clang__)
318#pragma GCC diagnostic pop
319#endif
320
321const wchar_t *
323{
324 if (!GetSystemDirectoryW(win_sys_path, _countof(win_sys_path)))
325 {
326 const wchar_t *default_sys_path = L"C:\\Windows\\system32";
327
328 wcscpy_s(win_sys_path, _countof(win_sys_path), default_sys_path);
329 win_sys_path[_countof(win_sys_path) - 1] = L'\0';
330 }
331
332 return win_sys_path;
333}
const wchar_t * get_win_sys_path(void)
Definition common.c:322
wchar_t * utf8to16_size(const char *utf8, int size)
Convert a UTF-8 string to UTF-16.
Definition common.c:301
static DWORD GetRegString(HKEY key, LPCWSTR value, LPWSTR data, DWORD size, LPCWSTR default_value)
Definition common.c:39
static BOOL ensure_trailing_backslash(PWSTR dir, size_t size)
Make sure that a dir path ends with a backslash.
Definition common.c:74
DWORD MsgToEventLog(DWORD flags, LPCWSTR format,...)
Definition common.c:259
LPCWSTR service_instance
Definition common.c:30
DWORD GetOpenvpnSettings(settings_t *s)
Definition common.c:82
LPCWSTR GetLastErrorText(void)
Definition common.c:223
static SERVICE_STATUS status
Definition interactive.c:52
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
#define APPNAME
Definition service.h:36
#define M_SYSERR
Definition service.h:45
#define MSG_FLAGS_SYS_CODE
Definition service.h:43
#define MSG_FLAGS_ERROR
Definition service.h:42
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:145
BOOL append
Definition service.h:74
WCHAR ext_string[16]
Definition service.h:69
WCHAR ovpn_admin_group[MAX_NAME]
Definition service.h:71
WCHAR bin_dir[MAX_PATH]
Definition service.h:68
WCHAR ovpn_service_user[MAX_NAME]
Definition service.h:72
WCHAR log_dir[MAX_PATH]
Definition service.h:70
WCHAR config_dir[MAX_PATH]
Definition service.h:67
DWORD priority
Definition service.h:73
WCHAR exe_path[MAX_PATH]
Definition service.h:66
#define _L(q)
Definition basic.h:38
#define OVPN_SERVICE_USER
Definition validate.h:32
#define OVPN_ADMIN_GROUP
Definition validate.h:30
static char * win_sys_path
Definition win32.c:102