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