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-2018 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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 /*
25 * Win32-specific OpenVPN code, targeted at the mingw
26 * development environment.
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #elif defined(_MSC_VER)
32 #include "config-msvc.h"
33 #endif
34
35 #include "syshead.h"
36
37 #ifdef _WIN32
38
39 #include "buffer.h"
40 #include "win32-util.h"
41
42 WCHAR *
wide_string(const char * utf8,struct gc_arena * gc)43 wide_string(const char *utf8, struct gc_arena *gc)
44 {
45 int n = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
46 WCHAR *ucs16 = gc_malloc(n * sizeof(WCHAR), false, gc);
47 MultiByteToWideChar(CP_UTF8, 0, utf8, -1, ucs16, n);
48 return ucs16;
49 }
50
51
52 /*
53 * Return true if filename is safe to be used on Windows,
54 * by avoiding the following reserved names:
55 *
56 * CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9,
57 * LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9, and CLOCK$
58 *
59 * See: http://msdn.microsoft.com/en-us/library/aa365247.aspx
60 * and http://msdn.microsoft.com/en-us/library/86k9f82k(VS.80).aspx
61 */
62
63 static bool
cmp_prefix(const char * str,const bool n,const char * pre)64 cmp_prefix(const char *str, const bool n, const char *pre)
65 {
66 size_t i = 0;
67
68 if (!str)
69 {
70 return false;
71 }
72
73 while (true)
74 {
75 const int c1 = pre[i];
76 int c2 = str[i];
77 ++i;
78 if (c1 == '\0')
79 {
80 if (n)
81 {
82 if (isdigit(c2))
83 {
84 c2 = str[i];
85 }
86 else
87 {
88 return false;
89 }
90 }
91 return c2 == '\0' || c2 == '.';
92 }
93 else if (c2 == '\0')
94 {
95 return false;
96 }
97 if (c1 != tolower(c2))
98 {
99 return false;
100 }
101 }
102 }
103
104 bool
win_safe_filename(const char * fn)105 win_safe_filename(const char *fn)
106 {
107 if (cmp_prefix(fn, false, "con"))
108 {
109 return false;
110 }
111 if (cmp_prefix(fn, false, "prn"))
112 {
113 return false;
114 }
115 if (cmp_prefix(fn, false, "aux"))
116 {
117 return false;
118 }
119 if (cmp_prefix(fn, false, "nul"))
120 {
121 return false;
122 }
123 if (cmp_prefix(fn, true, "com"))
124 {
125 return false;
126 }
127 if (cmp_prefix(fn, true, "lpt"))
128 {
129 return false;
130 }
131 if (cmp_prefix(fn, false, "clock$"))
132 {
133 return false;
134 }
135 return true;
136 }
137 #endif /* _WIN32 */
138