OpenVPN
win32-util.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single 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) 2002-2026 OpenVPN Inc <sales@openvpn.net>
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/*
24 * Win32-specific OpenVPN code, targeted at the mingw
25 * development environment.
26 */
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "syshead.h"
33
34#ifdef _WIN32
35
36#include "buffer.h"
37#include "win32-util.h"
38
39#if defined(__GNUC__) || defined(__clang__)
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Wsign-conversion"
42#endif
43
44WCHAR *
45wide_string(const char *utf8, struct gc_arena *gc)
46{
47 int n = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
48 WCHAR *ucs16 = gc_malloc(n * sizeof(WCHAR), false, gc);
49 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, ucs16, n);
50 return ucs16;
51}
52
53/* special to cmd.exe, which CreateProcess() uses to run .bat/.cmd (VU#123335) */
54#define CMD_QUOTE_TRIGGERS " &|<>^%()!"
55
56static bool
58{
59 for (const char *c = str; *c != '\0'; ++c)
60 {
61 if (strchr(CMD_QUOTE_TRIGGERS, *c) != NULL)
62 {
63 return true;
64 }
65 }
66 return false;
67}
68
69WCHAR *
70wide_cmd_line(const struct argv *a, struct gc_arena *gc)
71{
72 size_t nchars = 1;
73 size_t maxlen = 0;
74 size_t i;
75 struct buffer buf;
76 char *work = NULL;
77
78 if (!a)
79 {
80 return NULL;
81 }
82
83 for (i = 0; i < a->argc; ++i)
84 {
85 const char *arg = a->argv[i];
86 const size_t len = strlen(arg);
87 nchars += len + 3;
88 if (len > maxlen)
89 {
90 maxlen = len;
91 }
92 }
93
94 work = gc_malloc(maxlen + 1, false, gc);
96 buf = alloc_buf_gc(nchars, gc);
97
98 for (i = 0; i < a->argc; ++i)
99 {
100 const char *arg = a->argv[i];
101 strcpy(work, arg);
102 /* cmd.exe expands %VAR% and !VAR! even inside quotes, so a value like
103 * %X509_0_O% could turn back into a quote and start a new command.
104 * Replace those along with the double quotes and CRLF. */
106 if (i)
107 {
108 buf_printf(&buf, " ");
109 }
111 {
112 buf_printf(&buf, "\"%s\"", work);
113 }
114 else
115 {
116 buf_printf(&buf, "%s", work);
117 }
118 }
119
120 return wide_string(BSTR(&buf), gc);
121}
122
123char *
124utf16to8(const wchar_t *utf16, struct gc_arena *gc)
125{
126 char *utf8 = NULL;
127 int n = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);
128 if (n > 0)
129 {
130 utf8 = gc_malloc(n, true, gc);
131 if (utf8)
132 {
134 }
135 }
136 return utf8;
137}
138
139#if defined(__GNUC__) || defined(__clang__)
140#pragma GCC diagnostic pop
141#endif
142
143/*
144 * Return true if filename is safe to be used on Windows,
145 * by avoiding the following reserved names:
146 *
147 * CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9,
148 * LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, and CLOCK$
149 *
150 * See: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
151 */
152
153static bool
154cmp_prefix(const char *str, const bool n, const char *pre)
155{
156 size_t i = 0;
157
158 if (!str)
159 {
160 return false;
161 }
162
163 while (true)
164 {
165 const int c1 = pre[i];
166 int c2 = str[i];
167 ++i;
168 if (c1 == '\0')
169 {
170 if (n)
171 {
172 if (isdigit(c2))
173 {
174 c2 = str[i];
175 }
176 else
177 {
178 return false;
179 }
180 }
181 return c2 == '\0' || c2 == '.';
182 }
183 else if (c2 == '\0')
184 {
185 return false;
186 }
187 if (c1 != tolower(c2))
188 {
189 return false;
190 }
191 }
192}
193
194bool
196{
197 if (cmp_prefix(fn, false, "con"))
198 {
199 return false;
200 }
201 if (cmp_prefix(fn, false, "prn"))
202 {
203 return false;
204 }
205 if (cmp_prefix(fn, false, "aux"))
206 {
207 return false;
208 }
209 if (cmp_prefix(fn, false, "nul"))
210 {
211 return false;
212 }
213 if (cmp_prefix(fn, true, "com"))
214 {
215 return false;
216 }
217 if (cmp_prefix(fn, true, "lpt"))
218 {
219 return false;
220 }
221 if (cmp_prefix(fn, false, "clock$"))
222 {
223 return false;
224 }
225 return true;
226}
227
228const char *
230{
231 static char tmpdir[MAX_PATH];
233
235 {
236 return NULL;
237 }
238
239 int ret = WideCharToMultiByte(CP_UTF8, 0, wtmpdir, -1, NULL, 0, NULL, NULL);
240 /* According to documentation ret is never < 0, but include it here just in case */
241 if (ret <= 0)
242 {
243 msg(M_WARN | M_ERRNO, "Conversion of path name failed.");
244 return NULL;
245 }
246 if ((unsigned int)ret > sizeof(tmpdir))
247 {
248 msg(M_WARN, "Could not get temporary directory. Path is too long."
249 " Consider using --tmp-dir");
250 return NULL;
251 }
252
254 return tmpdir;
255}
256
257bool
259{
260 size_t dir_len = wcslen(dir);
261 /* dir_len <= 1 guards the dir[dir_len - 1] access below and rejects a
262 * degenerate single-character directory (a normalized absolute path is
263 * always longer). */
264 if (dir_len <= 1 || wcsnicmp(dir, path, dir_len) != 0)
265 {
266 return false;
267 }
268
269 /* A plain prefix match is not sufficient: if dir is "C:\foo" then
270 * "C:\foo_evil\bar.dll" shares the prefix but is not inside "C:\foo".
271 * Require that the matched prefix ends on a path separator, i.e. either
272 * dir already ends with a separator or the character following the prefix
273 * in path is one. */
274 if (dir[dir_len - 1] == L'\\' || dir[dir_len - 1] == L'/')
275 {
276 return true;
277 }
278
279 WCHAR next = path[dir_len];
280 return next == L'\\' || next == L'/';
281}
282
283#endif /* _WIN32 */
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Modifies a string in place by replacing certain classes of characters of it with a specified characte...
Definition buffer.c:1021
Buffer management functions and garbage collection.
#define CC_EXCLAMATION
exclamation mark
Definition buffer.h:1714
#define CC_PERCENT
percent sign
Definition buffer.h:1713
#define CC_DOUBLE_QUOTE
double quote
Definition buffer.h:1711
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
#define CC_CRLF
carriage return or newline
Definition buffer.h:1723
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1694
#define msg(flags,...)
Definition error.h:152
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
Definition argv.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
struct gc_arena gc
Definition test_ssl.c:138
bool win_path_in_dir(const WCHAR *path, const WCHAR *dir)
Check whether path resides within directory dir.
Definition win32-util.c:258
char * utf16to8(const wchar_t *utf16, struct gc_arena *gc)
Definition win32-util.c:124
static bool cmp_prefix(const char *str, const bool n, const char *pre)
Definition win32-util.c:154
bool win_safe_filename(const char *fn)
Definition win32-util.c:195
const char * win_get_tempdir(void)
Definition win32-util.c:229
#define CMD_QUOTE_TRIGGERS
Definition win32-util.c:54
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
Definition win32-util.c:45
WCHAR * wide_cmd_line(const struct argv *a, struct gc_arena *gc)
Definition win32-util.c:70
static bool argv_element_needs_quotes(const char *str)
Definition win32-util.c:57