OpenVPN
win32.h
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-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 #ifdef _WIN32
25 #ifndef OPENVPN_WIN32_H
26 #define OPENVPN_WIN32_H
27 
28 #include "mtu.h"
29 #include "openvpn-msg.h"
30 #include "argv.h"
31 
32 /* location of executables */
33 #define SYS_PATH_ENV_VAR_NAME "SystemRoot" /* environmental variable name that normally contains the system path */
34 #define NETSH_PATH_SUFFIX "\\system32\\netsh.exe"
35 #define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
36 #define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
37 #define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
38 
39 /*
40  * Win32-specific OpenVPN code, targeted at the mingw
41  * development environment.
42  */
43 
44 /* MSVC headers do not define this macro, so do it here */
45 #ifndef IN6_ARE_ADDR_EQUAL
46 #define IN6_ARE_ADDR_EQUAL(a,b) \
47  (memcmp((const void *)(a), (const void *)(b), sizeof(struct in6_addr)) == 0)
48 #endif
49 
50 void init_win32(void);
51 
52 void uninit_win32(void);
53 
54 void set_pause_exit_win32(void);
55 
57 {
58  SECURITY_ATTRIBUTES sa;
59  SECURITY_DESCRIPTOR sd;
60 };
61 
62 #define HANDLE_DEFINED(h) ((h) != NULL && (h) != INVALID_HANDLE_VALUE)
63 
64 /*
65  * Save old window title.
66  */
68 {
69  bool saved;
70  char old_window_title [256];
71 };
72 
73 struct rw_handle {
74  HANDLE read;
75  HANDLE write;
76 };
77 
78 /*
79  * Event-based notification of incoming TCP connections
80  */
81 
82 #define NE32_PERSIST_EVENT (1<<0)
83 #define NE32_WRITE_EVENT (1<<1)
84 
85 static inline bool
86 defined_net_event_win32(const struct rw_handle *event)
87 {
88  return event->read != NULL;
89 }
90 
91 void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags);
92 
94 
95 void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags);
96 
97 /*
98  * A stateful variant of the net_event_win32 functions above
99  */
100 
102 {
103  struct rw_handle handle;
106 };
107 
108 void net_event_win32_init(struct net_event_win32 *ne);
109 
110 void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd);
111 
112 void net_event_win32_reset(struct net_event_win32 *ne);
113 
115 
116 void net_event_win32_stop(struct net_event_win32 *ne);
117 
118 void net_event_win32_close(struct net_event_win32 *ne);
119 
120 static inline bool
122 {
123  return defined_net_event_win32(&ne->handle);
124 }
125 
126 static inline struct rw_handle *
128 {
129  return &ne->handle;
130 }
131 
132 static inline long
134 {
135  return ne->event_mask;
136 }
137 
138 static inline void
140 {
141  ne->event_mask &= ~selected_events;
142 }
143 
144 /*
145  * Signal handling
146  */
147 struct win32_signal {
148 #define WSO_MODE_UNDEF 0
149 #define WSO_MODE_SERVICE 1
150 #define WSO_MODE_CONSOLE 2
151  int mode;
152  struct rw_handle in;
155 };
156 
157 extern struct win32_signal win32_signal; /* static/global */
158 extern struct window_title window_title; /* static/global */
159 
160 void win32_signal_clear(struct win32_signal *ws);
161 
162 /* win32_signal_open startup type */
163 #define WSO_NOFORCE 0
164 #define WSO_FORCE_SERVICE 1
165 #define WSO_FORCE_CONSOLE 2
166 
167 void win32_signal_open(struct win32_signal *ws,
168  int force, /* set to WSO force parm */
169  const char *exit_event_name,
170  bool exit_event_initial_state);
171 
172 void win32_signal_close(struct win32_signal *ws);
173 
174 int win32_signal_get(struct win32_signal *ws);
175 
176 void win32_pause(struct win32_signal *ws);
177 
178 bool win32_service_interrupt(struct win32_signal *ws);
179 
180 /*
181  * Set the text on the window title bar
182  */
183 
184 void window_title_clear(struct window_title *wt);
185 
186 void window_title_save(struct window_title *wt);
187 
188 void window_title_restore(const struct window_title *wt);
189 
190 void window_title_generate(const char *title);
191 
192 /*
193  * We try to do all Win32 I/O using overlapped
194  * (i.e. asynchronous) I/O for a performance win.
195  */
197 #define IOSTATE_INITIAL 0
198 #define IOSTATE_QUEUED 1 /* overlapped I/O has been queued */
199 #define IOSTATE_IMMEDIATE_RETURN 2 /* I/O function returned immediately without queueing */
200  int iostate;
201  OVERLAPPED overlapped;
202  DWORD size;
203  DWORD flags;
204  int status;
206  union {
207  struct sockaddr_in addr;
208  struct sockaddr_in6 addr6;
209  };
210  int addrlen;
211  struct buffer buf_init;
212  struct buffer buf;
213 };
214 
215 void overlapped_io_init(struct overlapped_io *o,
216  const struct frame *frame,
217  BOOL event_state,
218  bool tuntap_buffer);
219 
220 void overlapped_io_close(struct overlapped_io *o);
221 
222 static inline bool
224 {
226 }
227 
228 char *overlapped_io_state_ascii(const struct overlapped_io *o);
229 
230 /*
231  * Use to control access to resources that only one
232  * OpenVPN process on a given machine can access at
233  * a given time.
234  */
235 
236 struct semaphore
237 {
238  const char *name;
239  bool locked;
240  HANDLE hand;
241 };
242 
243 void semaphore_clear(struct semaphore *s);
244 
245 void semaphore_open(struct semaphore *s, const char *name);
246 
247 bool semaphore_lock(struct semaphore *s, int timeout_milliseconds);
248 
249 void semaphore_release(struct semaphore *s);
250 
251 void semaphore_close(struct semaphore *s);
252 
253 /*
254  * Special global semaphore used to protect network
255  * shell commands from simultaneous instantiation.
256  *
257  * It seems you can't run more than one instance
258  * of netsh on the same machine at the same time.
259  */
260 
261 extern struct semaphore netcmd_semaphore;
262 void netcmd_semaphore_init(void);
263 
264 void netcmd_semaphore_close(void);
265 
266 void netcmd_semaphore_lock(void);
267 
268 void netcmd_semaphore_release(void);
269 
270 /* Set Win32 security attributes structure to allow all access */
272 
273 /* return true if filename is safe to be used on Windows */
274 bool win_safe_filename(const char *fn);
275 
276 /* add constant environmental variables needed by Windows */
277 struct env_set;
278 
279 /* get and set the current windows system path */
280 void set_win_sys_path(const char *newpath, struct env_set *es);
281 
282 void set_win_sys_path_via_env(struct env_set *es);
283 
284 char *get_win_sys_path(void);
285 
286 /* call self in a subprocess */
287 void fork_to_self(const char *cmdline);
288 
289 /* Find temporary directory */
290 const char *win_get_tempdir(void);
291 
292 /* Convert a string from UTF-8 to UCS-2 */
293 WCHAR *wide_string(const char *utf8, struct gc_arena *gc);
294 
295 bool win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel);
296 
297 bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);
298 
299 #define WIN_XP 0
300 #define WIN_VISTA 1
301 #define WIN_7 2
302 #define WIN_8 3
303 
304 int win32_version_info(void);
305 
306 /*
307  * String representation of Windows version number and name, see
308  * https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx
309  */
310 const char *win32_version_string(struct gc_arena *gc, bool add_name);
311 
312 /*
313  * Send the |size| bytes in buffer |data| to the interactive service |pipe|
314  * and read the result in |ack|. Returns false on communication error.
315  * The string in |context| is used to prefix error messages.
316  */
317 bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
318  ack_message_t *ack, const char *context);
319 
320 /*
321  * Attempt to simulate fork/execve on Windows
322  */
323 int
324 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
325 
326 #endif /* ifndef OPENVPN_WIN32_H */
327 #endif /* ifdef _WIN32 */
int status
Definition: win32.h:204
void window_title_restore(const struct window_title *wt)
Definition: win32.c:722
void semaphore_close(struct semaphore *s)
Definition: win32.c:827
void init_win32(void)
Definition: win32.c:105
void window_title_clear(struct window_title *wt)
Definition: win32.c:699
HANDLE read
Definition: win32.h:74
void win32_signal_close(struct win32_signal *ws)
Definition: win32.c:599
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition: win32.c:326
Contains all state information for one tunnel.
Definition: openvpn.h:500
Packet geometry parameters.
Definition: mtu.h:93
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition: win32.c:777
struct gc_arena * gc
Definition: env_set.h:43
static struct rw_handle * net_event_win32_get_event(struct net_event_win32 *ne)
Definition: win32.h:127
int win32_signal_get(struct win32_signal *ws)
Definition: win32.c:633
DWORD size
Definition: win32.h:202
OVERLAPPED overlapped
Definition: win32.h:201
void uninit_win32(void)
Definition: win32.c:116
void window_title_generate(const char *title)
Definition: win32.c:731
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition: win32.c:257
void set_win_sys_path_via_env(struct env_set *es)
Definition: win32.c:1224
void win32_signal_clear(struct win32_signal *ws)
Definition: win32.c:442
static bool overlapped_io_active(struct overlapped_io *o)
Definition: win32.h:223
void win32_pause(struct win32_signal *ws)
Definition: win32.c:684
void net_event_win32_stop(struct net_event_win32 *ne)
Definition: win32.c:359
void netcmd_semaphore_close(void)
Definition: win32.c:853
const char * win_get_tempdir(void)
Definition: win32.c:1241
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition: win32.c:1216
void overlapped_io_close(struct overlapped_io *o)
Definition: win32.c:184
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition: win32.c:218
void semaphore_release(struct semaphore *s)
Definition: win32.c:811
list flags
int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
Definition: win32.c:1088
bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, ack_message_t *ack, const char *context)
Definition: win32.c:1476
int iostate
Definition: win32.h:200
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition: win32.c:448
void netcmd_semaphore_release(void)
Definition: win32.c:875
bool win_safe_filename(const char *fn)
Definition: win32.c:935
bool console_mode_save_defined
Definition: win32.h:154
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition: win32.c:197
void net_event_win32_reset(struct net_event_win32 *ne)
Definition: win32.c:353
#define IOSTATE_IMMEDIATE_RETURN
Definition: win32.h:199
const char * win32_version_string(struct gc_arena *gc, bool add_name)
Definition: win32.c:1441
void netcmd_semaphore_lock(void)
Definition: win32.c:859
void set_pause_exit_win32(void)
Definition: win32.c:140
#define IOSTATE_QUEUED
Definition: win32.h:198
SECURITY_ATTRIBUTES sa
Definition: win32.h:58
void net_event_win32_init(struct net_event_win32 *ne)
Definition: win32.c:319
void window_title_save(struct window_title *wt)
Definition: win32.c:705
int addrlen
Definition: win32.h:210
static bool defined_net_event_win32(const struct rw_handle *event)
Definition: win32.h:86
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition: win32.c:1372
void netcmd_semaphore_init(void)
Definition: win32.c:847
long event_mask
Definition: win32.h:105
static void net_event_win32_clear_selected_events(struct net_event_win32 *ne, long selected_events)
Definition: win32.h:139
bool win32_service_interrupt(struct win32_signal *ws)
Definition: win32.c:619
struct rw_handle handle
Definition: win32.h:103
bool init_security_attributes_allow_all(struct security_attributes *obj)
Definition: win32.c:146
SECURITY_DESCRIPTOR sd
Definition: win32.h:59
bool locked
Definition: win32.h:239
HANDLE write
Definition: win32.h:75
char * get_win_sys_path(void)
Definition: win32.c:1209
int win32_version_info(void)
Definition: win32.c:1399
int mode
Definition: win32.h:151
SOCKET socket_descriptor_t
Definition: syshead.h:488
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition: win32.h:121
bool addr_defined
Definition: win32.h:205
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
struct semaphore netcmd_semaphore
Definition: win32.c:97
DWORD flags
Definition: win32.h:203
#define buf_init(buf, offset)
Definition: buffer.h:196
bool win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel)
Definition: win32.c:1322
DWORD console_mode_save
Definition: win32.h:153
void net_event_win32_close(struct net_event_win32 *ne)
Definition: win32.c:370
const char * name
Definition: win32.h:238
HANDLE hand
Definition: win32.h:240
void fork_to_self(const char *cmdline)
Definition: win32.c:1169
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
static long net_event_win32_get_event_mask(const struct net_event_win32 *ne)
Definition: win32.h:133
void semaphore_clear(struct semaphore *s)
Definition: win32.c:747
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition: win32.c:272
Definition: argv.h:35
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition: win32.c:335
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
Definition: win32.c:1157
socket_descriptor_t sd
Definition: win32.h:104
bool saved
Definition: win32.h:69
void semaphore_open(struct semaphore *s, const char *name)
Definition: win32.c:753
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state, bool tuntap_buffer)
Definition: win32.c:165