OpenVPN
win32.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 <minwindef.h>
37#include <winsock2.h>
38#include <accctrl.h>
39#include <aclapi.h>
40#include <wow64apiset.h>
41
42#include "buffer.h"
43#include "error.h"
44#include "mtu.h"
45#include "run_command.h"
46#include "sig.h"
47#include "win32-util.h"
48#include "win32.h"
49#include "openvpn-msg.h"
50
51#include "memdbg.h"
52
53#include <versionhelpers.h>
54
55#include "wfp_block.h"
56
57/*
58 * WFP handle
59 */
60static HANDLE m_hEngineHandle = NULL; /* GLOBAL */
61
62/*
63 * TAP adapter original metric value
64 */
65static int tap_metric_v4 = -1; /* GLOBAL */
66static int tap_metric_v6 = -1; /* GLOBAL */
67
68/*
69 * Windows internal socket API state (opaque).
70 */
71static struct WSAData wsa_state; /* GLOBAL */
72
73/*
74 * Should we call win32_pause() on program exit?
75 */
76static bool pause_exit_enabled = false; /* GLOBAL */
77
78/*
79 * win32_signal is used to get input from the keyboard
80 * if we are running in a console, or get input from an
81 * event object if we are running as a service.
82 */
83
84struct win32_signal win32_signal; /* GLOBAL */
85
86/*
87 * Save our old window title so we can restore
88 * it on exit.
89 */
90struct window_title window_title; /* GLOBAL*/
91
92/*
93 * Special global semaphore used to protect network
94 * shell commands from simultaneous instantiation.
95 */
96
97struct semaphore netcmd_semaphore; /* GLOBAL */
98
99/*
100 * Windows system pathname such as c:\windows
101 */
102static char *win_sys_path = NULL; /* GLOBAL */
103
107static void set_openssl_env_vars(void);
108
109void
111{
112 if (WSAStartup(0x0101, &wsa_state))
113 {
114 msg(M_ERR, "WSAStartup failed");
115 }
118
120}
121
122void
124{
127 {
129 {
130 struct win32_signal w;
131 win32_signal_open(&w, WSO_FORCE_CONSOLE, NULL, false);
132 win32_pause(&w);
134 }
135 else
136 {
138 }
139 }
142 WSACleanup();
143 free(win_sys_path);
144}
145
146void
148{
149 pause_exit_enabled = true;
150}
151
159bool
161{
162 CLEAR(*obj);
163
164 obj->sa.nLength = sizeof(SECURITY_ATTRIBUTES);
165 obj->sa.lpSecurityDescriptor = &obj->sd;
166 obj->sa.bInheritHandle = FALSE;
167 if (!InitializeSecurityDescriptor(&obj->sd, SECURITY_DESCRIPTOR_REVISION))
168 {
169 return false;
170 }
171 if (!SetSecurityDescriptorDacl(&obj->sd, TRUE, NULL, FALSE))
172 {
173 return false;
174 }
175 return true;
176}
177
191static bool
193{
194 bool ret = false;
195
196 CLEAR(*obj);
197 obj->sa.nLength = sizeof(SECURITY_ATTRIBUTES);
198 obj->sa.lpSecurityDescriptor = &obj->sd;
199 obj->sa.bInheritHandle = FALSE;
200
201 if (!InitializeSecurityDescriptor(&obj->sd, SECURITY_DESCRIPTOR_REVISION))
202 {
203 return ret;
204 }
205
206 HANDLE token = NULL;
207 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
208 {
209 return ret;
210 }
211
212 PTOKEN_USER info = NULL;
213 DWORD info_len = 0;
214 if (!GetTokenInformation(token, TokenUser, info, info_len, &info_len)
215 && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
216 {
217 goto out;
218 }
219
220 info = malloc(info_len);
221 if (!info || !GetTokenInformation(token, TokenUser, info, info_len, &info_len))
222 {
223 goto out;
224 }
225
226 EXPLICIT_ACCESS ea = { 0 };
227 ea.grfAccessPermissions = GENERIC_ALL;
228 ea.grfAccessMode = SET_ACCESS;
229 ea.grfInheritance = NO_INHERITANCE;
230 ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
231 ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
232 ea.Trustee.ptstrName = (LPTSTR)info->User.Sid;
233
234 if (SetEntriesInAcl(1, &ea, NULL, &obj->dacl) != ERROR_SUCCESS)
235 {
236 goto out;
237 }
238
239 if (SetSecurityDescriptorDacl(&obj->sd, TRUE, obj->dacl, FALSE))
240 {
241 ret = true;
242 }
243
244out:
245 free(info);
246 CloseHandle(token);
247 return ret;
248}
249
255static void
257{
258 if (obj->dacl)
259 {
260 LocalFree(obj->dacl);
261 obj->dacl = NULL;
262 }
263}
264
265void
266overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
267{
268 CLEAR(*o);
269
270 /* manual reset event, initially set according to event_state */
271 o->overlapped.hEvent = CreateEvent(NULL, TRUE, event_state, NULL);
272 if (o->overlapped.hEvent == NULL)
273 {
274 msg(M_ERR, "Error: overlapped_io_init: CreateEvent failed");
275 }
276
277 /* allocate buffer for overlapped I/O */
279}
280
281void
283{
284 if (o->overlapped.hEvent)
285 {
286 if (!CloseHandle(o->overlapped.hEvent))
287 {
288 msg(M_WARN | M_ERRNO, "Warning: CloseHandle failed on overlapped I/O event object");
289 }
290 }
291 free_buf(&o->buf_init);
292}
293
294char *
296{
297 switch (o->iostate)
298 {
299 case IOSTATE_INITIAL:
300 return "0";
301
302 case IOSTATE_QUEUED:
303 return "Q";
304
306 return "1";
307 }
308 return "?";
309}
310
311/*
312 * Event-based notification of network events
313 */
314
315void
316init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd,
317 unsigned int flags)
318{
319 /* manual reset events, initially set to unsignaled */
320
321 /* initialize write event */
322 if (!(flags & NE32_PERSIST_EVENT) || !event->write)
323 {
324 if (flags & NE32_WRITE_EVENT)
325 {
326 event->write = CreateEvent(NULL, TRUE, FALSE, NULL);
327 if (event->write == NULL)
328 {
329 msg(M_ERR, "Error: init_net_event_win32: CreateEvent (write) failed");
330 }
331 }
332 else
333 {
334 event->write = NULL;
335 }
336 }
337
338 /* initialize read event */
339 if (!(flags & NE32_PERSIST_EVENT) || !event->read)
340 {
341 event->read = CreateEvent(NULL, TRUE, FALSE, NULL);
342 if (event->read == NULL)
343 {
344 msg(M_ERR, "Error: init_net_event_win32: CreateEvent (read) failed");
345 }
346 }
347
348 /* setup network events to change read event state */
349 if (WSAEventSelect(sd, event->read, network_events) != 0)
350 {
351 msg(M_FATAL | M_ERRNO, "Error: init_net_event_win32: WSAEventSelect call failed");
352 }
353}
354
355long
357{
358 WSANETWORKEVENTS wne;
359 if (WSAEnumNetworkEvents(sd, event->read, &wne) != 0)
360 {
361 msg(M_FATAL | M_ERRNO, "Error: reset_net_event_win32: WSAEnumNetworkEvents call failed");
362 return 0; /* NOTREACHED */
363 }
364 else
365 {
366 return wne.lNetworkEvents;
367 }
368}
369
370void
371close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
372{
373 if (event->read)
374 {
375 if (socket_defined(sd))
376 {
377 if (WSAEventSelect(sd, event->read, 0) != 0)
378 {
379 msg(M_WARN | M_ERRNO, "Warning: close_net_event_win32: WSAEventSelect call failed");
380 }
381 }
382 if (!ResetEvent(event->read))
383 {
384 msg(M_WARN | M_ERRNO, "Warning: ResetEvent (read) failed in close_net_event_win32");
385 }
386 if (!(flags & NE32_PERSIST_EVENT))
387 {
388 if (!CloseHandle(event->read))
389 {
391 "Warning: CloseHandle (read) failed in close_net_event_win32");
392 }
393 event->read = NULL;
394 }
395 }
396
397 if (event->write)
398 {
399 if (!ResetEvent(event->write))
400 {
401 msg(M_WARN | M_ERRNO, "Warning: ResetEvent (write) failed in close_net_event_win32");
402 }
403 if (!(flags & NE32_PERSIST_EVENT))
404 {
405 if (!CloseHandle(event->write))
406 {
408 "Warning: CloseHandle (write) failed in close_net_event_win32");
409 }
410 event->write = NULL;
411 }
412 }
413}
414
415/*
416 * struct net_event_win32
417 */
418
419void
421{
422 CLEAR(*ne);
423 ne->sd = SOCKET_UNDEFINED;
424}
425
426void
428{
429 ASSERT(!socket_defined(ne->sd));
430 ne->sd = sd;
431 ne->event_mask = 0;
433}
434
435void
437{
438 BOOL status;
439 if (ne->event_mask & FD_WRITE)
440 {
441 status = SetEvent(ne->handle.write);
442 }
443 else
444 {
445 status = ResetEvent(ne->handle.write);
446 }
447 if (!status)
448 {
449 msg(M_WARN | M_ERRNO, "Warning: SetEvent/ResetEvent failed in net_event_win32_reset_write");
450 }
451}
452
453void
458
459void
461{
463 {
465 }
466 ne->sd = SOCKET_UNDEFINED;
467 ne->event_mask = 0;
468}
469
470void
472{
474 {
475 close_net_event_win32(&ne->handle, ne->sd, 0);
476 }
478}
479
480/*
481 * Simulate *nix signals on Windows.
482 *
483 * Two modes:
484 * (1) Console mode -- map keyboard function keys to signals
485 * (2) Service mode -- map Windows event object to SIGTERM
486 */
487
488static void
490{
491 if (ws->mode == WSO_MODE_SERVICE && HANDLE_DEFINED(ws->in.read))
492 {
493 SetEvent(ws->in.read);
494 }
495 else /* generate a key-press event */
496 {
497 DWORD tmp;
498 INPUT_RECORD ir;
499 HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
500
501 CLEAR(ir);
502 ir.EventType = KEY_EVENT;
503 ir.Event.KeyEvent.bKeyDown = true;
504 if (!stdin_handle || !WriteConsoleInput(stdin_handle, &ir, 1, &tmp))
505 {
506 msg(M_WARN | M_ERRNO, "WARN: win_trigger_event: WriteConsoleInput");
507 }
508 }
509}
510
511/*
512 * Callback to handle console ctrl events
513 */
514static bool WINAPI
515win_ctrl_handler(DWORD signum)
516{
517 msg(D_LOW, "win_ctrl_handler: signal received (code=%lu)", (unsigned long)signum);
518
520 {
521 return true;
522 }
523
524 switch (signum)
525 {
526 case CTRL_C_EVENT:
527 case CTRL_BREAK_EVENT:
529 /* trigget the win32_signal to interrupt the event loop */
531 return true;
532 break;
533
534 default:
535 msg(D_LOW, "win_ctrl_handler: signal (code=%lu) not handled", (unsigned long)signum);
536 break;
537 }
538 /* pass all other signals to the next handler */
539 return false;
540}
541
542void
544{
545 CLEAR(*ws);
546}
547
548#if defined(__GNUC__) || defined(__clang__)
549#pragma GCC diagnostic push
550#pragma GCC diagnostic ignored "-Wsign-conversion"
551#endif
552
553void
554win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name,
555 bool exit_event_initial_state)
556{
557 CLEAR(*ws);
558
559 ws->mode = WSO_MODE_UNDEF;
560 ws->in.read = INVALID_HANDLE_VALUE;
561 ws->in.write = INVALID_HANDLE_VALUE;
562 ws->console_mode_save = 0;
563 ws->console_mode_save_defined = false;
564
565 if (force == WSO_NOFORCE || force == WSO_FORCE_CONSOLE)
566 {
567 /*
568 * Try to open console.
569 */
570 ws->in.read = GetStdHandle(STD_INPUT_HANDLE);
571 if (ws->in.read != INVALID_HANDLE_VALUE)
572 {
573 if (GetConsoleMode(ws->in.read, &ws->console_mode_save))
574 {
575 /* running on a console */
576 const DWORD new_console_mode =
578 & ~(ENABLE_WINDOW_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT
579 | ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT);
580
581 if (new_console_mode != ws->console_mode_save)
582 {
583 if (!SetConsoleMode(ws->in.read, new_console_mode))
584 {
585 msg(M_ERR, "Error: win32_signal_open: SetConsoleMode failed");
586 }
587 ws->console_mode_save_defined = true;
588 }
590 }
591 else
592 {
593 ws->in.read = INVALID_HANDLE_VALUE; /* probably running as a service */
594 }
595 }
596 }
597
598 /*
599 * If console open failed, assume we are running
600 * as a service.
601 */
602 if ((force == WSO_NOFORCE || force == WSO_FORCE_SERVICE) && !HANDLE_DEFINED(ws->in.read)
603 && exit_event_name)
604 {
605 struct security_attributes sa;
606 struct gc_arena gc = gc_new();
607 const wchar_t *exit_event_nameW = wide_string(exit_event_name, &gc);
608
610 {
611 msg(M_ERR, "Error: win32_signal_open: init SA failed");
612 }
613
614 ws->in.read =
615 CreateEventW(&sa.sa, TRUE, exit_event_initial_state ? TRUE : FALSE, exit_event_nameW);
616 if (ws->in.read == NULL)
617 {
618 msg(M_WARN | M_ERRNO, "NOTE: CreateEventW '%s' failed", exit_event_name);
619 }
620 else
621 {
622 if (WaitForSingleObject(ws->in.read, 0) != WAIT_TIMEOUT)
623 {
624 msg(M_FATAL, "ERROR: Exit Event ('%s') is signaled", exit_event_name);
625 }
626 else
627 {
629 }
630 }
632 gc_free(&gc);
633 }
634 /* set the ctrl handler in both console and service modes */
635 if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)win_ctrl_handler, true))
636 {
637 msg(M_WARN | M_ERRNO, "WARN: SetConsoleCtrlHandler failed");
638 }
639}
640
641static bool
643{
645 if (HANDLE_DEFINED(ws->in.read))
646 {
647 DWORD n;
648 if (GetNumberOfConsoleInputEvents(ws->in.read, &n))
649 {
650 return n > 0;
651 }
652 }
653 return false;
654}
655
656static unsigned int
657keyboard_ir_to_key(INPUT_RECORD *ir)
658{
659 if (ir->Event.KeyEvent.uChar.AsciiChar == 0)
660 {
661 return ir->Event.KeyEvent.wVirtualScanCode;
662 }
663
664 if ((ir->Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
665 && (ir->Event.KeyEvent.wVirtualKeyCode != 18))
666 {
667 return ir->Event.KeyEvent.wVirtualScanCode * 256;
668 }
669
670 return ir->Event.KeyEvent.uChar.AsciiChar;
671}
672
673static unsigned int
675{
677 if (HANDLE_DEFINED(ws->in.read))
678 {
679 INPUT_RECORD ir;
680 do
681 {
682 DWORD n;
684 {
685 return 0;
686 }
687 if (!ReadConsoleInput(ws->in.read, &ir, 1, &n))
688 {
689 return 0;
690 }
691 } while (ir.EventType != KEY_EVENT || ir.Event.KeyEvent.bKeyDown != TRUE);
692
693 return keyboard_ir_to_key(&ir);
694 }
695 else
696 {
697 return 0;
698 }
699}
700
701void
703{
704 if (ws->mode == WSO_MODE_SERVICE && HANDLE_DEFINED(ws->in.read))
705 {
706 CloseHandle(ws->in.read);
707 }
709 {
710 if (!SetConsoleMode(ws->in.read, ws->console_mode_save))
711 {
712 msg(M_ERR, "Error: win32_signal_close: SetConsoleMode failed");
713 }
714 }
715 CLEAR(*ws);
716}
717
718/*
719 * Return true if interrupt occurs in service mode.
720 */
721bool
723{
724 if (ws->mode == WSO_MODE_SERVICE)
725 {
726 if (HANDLE_DEFINED(ws->in.read) && WaitForSingleObject(ws->in.read, 0) == WAIT_OBJECT_0)
727 {
728 return true;
729 }
730 }
731 return false;
732}
733
734int
736{
737 int ret = 0;
738
739 if (ws->mode == WSO_MODE_SERVICE)
740 {
742 {
743 ret = SIGTERM;
744 }
745 }
746 else if (ws->mode == WSO_MODE_CONSOLE)
747 {
748 switch (win32_keyboard_get(ws))
749 {
750 case 0x3B: /* F1 -> USR1 */
751 ret = SIGUSR1;
752 break;
753
754 case 0x3C: /* F2 -> USR2 */
755 ret = SIGUSR2;
756 break;
757
758 case 0x3D: /* F3 -> HUP */
759 ret = SIGHUP;
760 break;
761
762 case 0x3E: /* F4 -> TERM */
763 ret = SIGTERM;
764 break;
765
766 case 0x03: /* CTRL-C -> TERM */
767 ret = SIGTERM;
768 break;
769 }
770 }
771 if (ret)
772 {
773 throw_signal(ret); /* this will update siginfo_static.signal received */
774 }
776}
777
778void
780{
781 if (ws->mode == WSO_MODE_CONSOLE && HANDLE_DEFINED(ws->in.read))
782 {
783 msg(M_INFO | M_NOPREFIX, "Press any key to continue...");
784 do
785 {
786 WaitForSingleObject(ws->in.read, INFINITE);
787 } while (!win32_keyboard_get(ws));
788 }
789}
790
791/* window functions */
792
793void
795{
796 CLEAR(*wt);
797}
798
799void
801{
802 if (!wt->saved)
803 {
804 if (!GetConsoleTitle(wt->old_window_title, sizeof(wt->old_window_title)))
805 {
806 wt->old_window_title[0] = 0;
807 wt->saved = false;
808 }
809 else
810 {
811 wt->saved = true;
812 }
813 }
814}
815
816void
818{
819 if (wt->saved)
820 {
821 SetConsoleTitle(wt->old_window_title);
822 }
823}
824
825void
826window_title_generate(const char *title)
827{
828 struct gc_arena gc = gc_new();
829 struct buffer out = alloc_buf_gc(256, &gc);
830 if (!title)
831 {
832 title = "";
833 }
834 buf_printf(&out, "[%s] " PACKAGE_NAME " " PACKAGE_VERSION " F4:EXIT F1:USR1 F2:USR2 F3:HUP",
835 title);
836 SetConsoleTitle(BSTR(&out));
837 gc_free(&gc);
838}
839
840/* semaphore functions */
841
842void
844{
845 CLEAR(*s);
846}
847
848void
849semaphore_open(struct semaphore *s, const char *name)
850{
851 struct security_attributes sa;
852
853 s->locked = false;
854 s->name = name;
855 s->hand = NULL;
856
858 {
859 s->hand = CreateSemaphore(&sa.sa, 1, 1, name);
860 }
862
863 if (s->hand == NULL)
864 {
865 msg(M_ERR, "Cannot create Win32 semaphore '%s'", name);
866 }
867 else
868 {
869 dmsg(D_SEMAPHORE, "Created Win32 semaphore '%s'", s->name);
870 }
871}
872
873bool
874semaphore_lock(struct semaphore *s, int timeout_milliseconds)
875{
876 bool ret = true;
877
878 if (s->hand)
879 {
880 DWORD status;
881 ASSERT(!s->locked);
882
883 dmsg(
885 "Attempting to lock Win32 semaphore '%s' prior to net shell command (timeout = %d sec)",
886 s->name, timeout_milliseconds / 1000);
887 status = WaitForSingleObject(s->hand, timeout_milliseconds);
888 if (status == WAIT_FAILED)
889 {
890 msg(M_ERR, "Wait failed on Win32 semaphore '%s'", s->name);
891 }
892 ret = (status == WAIT_TIMEOUT) ? false : true;
893 if (ret)
894 {
895 dmsg(D_SEMAPHORE, "Locked Win32 semaphore '%s'", s->name);
896 s->locked = true;
897 }
898 else
899 {
900 dmsg(D_SEMAPHORE, "Wait on Win32 semaphore '%s' timed out after %d milliseconds",
901 s->name, timeout_milliseconds);
902 }
903 }
904 return ret;
905}
906
907void
909{
910 if (s->hand)
911 {
912 ASSERT(s->locked);
913 dmsg(D_SEMAPHORE, "Releasing Win32 semaphore '%s'", s->name);
914 if (!ReleaseSemaphore(s->hand, 1, NULL))
915 {
916 msg(M_WARN | M_ERRNO, "ReleaseSemaphore failed on Win32 semaphore '%s'", s->name);
917 }
918 s->locked = false;
919 }
920}
921
922void
924{
925 if (s->hand)
926 {
927 if (s->locked)
928 {
930 }
931 dmsg(D_SEMAPHORE, "Closing Win32 semaphore '%s'", s->name);
932 CloseHandle(s->hand);
933 s->hand = NULL;
934 }
935}
936
937/*
938 * Special global semaphore used to protect network
939 * shell commands from simultaneous instantiation.
940 */
941
942void
947
948void
953
954void
956{
957 const int timeout_seconds = 600;
958
960 {
962 }
963
964 if (!semaphore_lock(&netcmd_semaphore, timeout_seconds * 1000))
965 {
966 msg(M_FATAL, "Cannot lock net command semaphore");
967 }
968}
969
970void
972{
974 /* netcmd_semaphore has max count of 1 - safe to close after release */
976}
977
978/*
979 * Service functions for openvpn_execve
980 */
981
982static char *
983env_block(const struct env_set *es)
984{
985 char force_path[256];
986 char *sysroot = get_win_sys_path();
987
988 if (!checked_snprintf(force_path, sizeof(force_path), "PATH=%s\\System32;%s;%s\\System32\\Wbem",
989 sysroot, sysroot, sysroot))
990 {
991 msg(M_WARN, "env_block: default path truncated to %s", force_path);
992 }
993
994 if (es)
995 {
996 const struct env_item *e;
997 char *ret;
998 char *p;
999 size_t nchars = 1;
1000 bool path_seen = false;
1001
1002 for (e = es->list; e != NULL; e = e->next)
1003 {
1004 nchars += strlen(e->string) + 1;
1005 }
1006
1007 nchars += strlen(force_path) + 1;
1008
1009 ret = (char *)malloc(nchars);
1011
1012 p = ret;
1013 for (e = es->list; e != NULL; e = e->next)
1014 {
1015 if (env_allowed(e->string))
1016 {
1017 strcpy(p, e->string);
1018 p += strlen(e->string) + 1;
1019 }
1020 if (strncmp(e->string, "PATH=", 5) == 0)
1021 {
1022 path_seen = true;
1023 }
1024 }
1025
1026 /* make sure PATH is set */
1027 if (!path_seen)
1028 {
1029 msg(M_INFO, "env_block: add %s", force_path);
1030 strcpy(p, force_path);
1031 p += strlen(force_path) + 1;
1032 }
1033
1034 *p = '\0';
1035 return ret;
1036 }
1037 else
1038 {
1039 return NULL;
1040 }
1041}
1042
1043/*
1044 * Attempt to simulate fork/execve on Windows
1045 */
1046int
1047openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
1048{
1049 int ret = OPENVPN_EXECVE_ERROR;
1050
1051 if (a && a->argv[0])
1052 {
1053 if (openvpn_execve_allowed(flags))
1054 {
1055 struct gc_arena gc = gc_new();
1056 STARTUPINFOW start_info;
1057 PROCESS_INFORMATION proc_info;
1058
1059 char *env = env_block(es);
1060 WCHAR *cl = wide_cmd_line(a, &gc);
1061 WCHAR *cmd = wide_string(a->argv[0], &gc);
1062
1063 /* this allows console programs to run, and is ignored otherwise */
1064 DWORD proc_flags = CREATE_NO_WINDOW;
1065
1066 CLEAR(start_info);
1067 CLEAR(proc_info);
1068
1069 /* fill in STARTUPINFO struct */
1070 GetStartupInfoW(&start_info);
1071 start_info.cb = sizeof(start_info);
1072 start_info.dwFlags = STARTF_USESHOWWINDOW;
1073 start_info.wShowWindow = SW_HIDE;
1074
1075 if (CreateProcessW(cmd, cl, NULL, NULL, FALSE, proc_flags, env, NULL, &start_info,
1076 &proc_info))
1077 {
1078 DWORD exit_status = 0;
1079 CloseHandle(proc_info.hThread);
1080 WaitForSingleObject(proc_info.hProcess, INFINITE);
1081 if (GetExitCodeProcess(proc_info.hProcess, &exit_status))
1082 {
1083 ret = (int)exit_status;
1084 }
1085 else
1086 {
1087 msg(M_WARN | M_ERRNO, "openvpn_execve: GetExitCodeProcess %ls failed", cmd);
1088 }
1089 CloseHandle(proc_info.hProcess);
1090 }
1091 else
1092 {
1093 msg(M_WARN | M_ERRNO, "openvpn_execve: CreateProcess %ls failed", cmd);
1094 }
1095 free(env);
1096 gc_free(&gc);
1097 }
1098 else
1099 {
1100 static bool exec_warn = false;
1101
1103 if (!exec_warn && (script_security() < SSEC_SCRIPTS))
1104 {
1106 exec_warn = true;
1107 }
1108 }
1109 }
1110 else
1111 {
1112 msg(M_WARN, "openvpn_execve: called with empty argv");
1113 }
1114 return ret;
1115}
1116
1117/*
1118 * call ourself in another process
1119 */
1120void
1121fork_to_self(const char *cmdline)
1122{
1123 STARTUPINFO start_info;
1124 PROCESS_INFORMATION proc_info;
1125 char self_exe[256];
1126 char *cl = string_alloc(cmdline, NULL);
1127 DWORD status;
1128
1129 CLEAR(start_info);
1130 CLEAR(proc_info);
1131 CLEAR(self_exe);
1132
1133 status = GetModuleFileName(NULL, self_exe, sizeof(self_exe));
1134 if (status == 0 || status == sizeof(self_exe))
1135 {
1136 msg(M_WARN | M_ERRNO,
1137 "fork_to_self: CreateProcess failed: cannot get module name via GetModuleFileName");
1138 goto done;
1139 }
1140
1141 /* fill in STARTUPINFO struct */
1142 GetStartupInfo(&start_info);
1143 start_info.cb = sizeof(start_info);
1144 start_info.dwFlags = STARTF_USESHOWWINDOW;
1145 start_info.wShowWindow = SW_HIDE;
1146
1147 if (CreateProcess(self_exe, cl, NULL, NULL, FALSE, 0, NULL, NULL, &start_info, &proc_info))
1148 {
1149 CloseHandle(proc_info.hThread);
1150 CloseHandle(proc_info.hProcess);
1151 }
1152 else
1153 {
1154 msg(M_WARN | M_ERRNO, "fork_to_self: CreateProcess failed: %s", cmdline);
1155 }
1156
1157done:
1158 free(cl);
1159}
1160
1161char *
1163{
1165 return win_sys_path;
1166}
1167
1168void
1169set_win_sys_path(const char *newpath, struct env_set *es)
1170{
1171 free(win_sys_path);
1172 win_sys_path = string_alloc(newpath, NULL);
1173 setenv_str(es, SYS_PATH_ENV_VAR_NAME, win_sys_path); /* route.exe needs this */
1174}
1175
1176void
1178{
1179 char buf[256];
1180 DWORD status = GetEnvironmentVariable(SYS_PATH_ENV_VAR_NAME, buf, sizeof(buf));
1181 if (!status)
1182 {
1183 msg(M_ERR, "Cannot find environmental variable %s", SYS_PATH_ENV_VAR_NAME);
1184 }
1185 if (status > sizeof(buf) - 1)
1186 {
1187 msg(M_FATAL, "String overflow attempting to read environmental variable %s",
1189 }
1190 set_win_sys_path(buf, es);
1191}
1192
1193static bool
1194win_get_exe_path(PWCHAR path, DWORD size)
1195{
1196 DWORD status = GetModuleFileNameW(NULL, path, size);
1197 if (status == 0 || status == size)
1198 {
1199 msg(M_WARN | M_ERRNO, "cannot get executable path");
1200 return false;
1201 }
1202 return true;
1203}
1204
1205static void
1206win_wfp_msg_handler(DWORD err, const char *msg)
1207{
1208 struct gc_arena gc = gc_new();
1209
1210 if (err == 0)
1211 {
1212 msg(M_INFO, "%s", msg);
1213 }
1214 else
1215 {
1216 msg(M_WARN, "Error in WFP: %s : %s [status=0x%lx]", msg, strerror_win32(err, &gc), err);
1217 }
1218
1219 gc_free(&gc);
1220}
1221
1222static bool
1223win_wfp_block_service(bool add, bool dns_only, int index, const HANDLE pipe)
1224{
1225 bool ret = false;
1226 ack_message_t ack;
1227 struct gc_arena gc = gc_new();
1228
1230 sizeof(wfp_block_message_t), 0 },
1231 .flags = dns_only ? wfp_block_dns : wfp_block_local,
1232 .iface = { .index = index, .name = "" } };
1233
1234 if (!send_msg_iservice(pipe, &data, sizeof(data), &ack, "WFP block"))
1235 {
1236 goto out;
1237 }
1238
1239 if (ack.error_number != NO_ERROR)
1240 {
1241 msg(M_WARN,
1242 "WFP block: %s block filters using service failed: %s [status=0x%x if_index=%lu]",
1243 (add ? "adding" : "deleting"), strerror_win32(ack.error_number, &gc), ack.error_number,
1244 data.iface.index);
1245 goto out;
1246 }
1247
1248 ret = true;
1249 msg(M_INFO, "%s WFP block filters using service succeeded.", (add ? "Adding" : "Deleting"));
1250out:
1251 gc_free(&gc);
1252 return ret;
1253}
1254
1255bool
1256win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only)
1257{
1258 WCHAR openvpnpath[MAX_PATH];
1259 bool ret = false;
1260 DWORD status;
1261
1262 if (msg_channel)
1263 {
1264 dmsg(D_LOW, "Using service to add WFP block filters");
1265 ret = win_wfp_block_service(true, dns_only, index, msg_channel);
1266 goto out;
1267 }
1268
1269 ret = win_get_exe_path(openvpnpath, _countof(openvpnpath));
1270 if (ret == false)
1271 {
1272 goto out;
1273 }
1274
1275 status =
1276 add_wfp_block_filters(&m_hEngineHandle, index, openvpnpath, win_wfp_msg_handler, dns_only);
1277 if (status == 0)
1278 {
1279 int is_auto = 0;
1280 tap_metric_v4 = get_interface_metric(index, AF_INET, &is_auto);
1281 if (is_auto)
1282 {
1283 tap_metric_v4 = 0;
1284 }
1285 tap_metric_v6 = get_interface_metric(index, AF_INET6, &is_auto);
1286 if (is_auto)
1287 {
1288 tap_metric_v6 = 0;
1289 }
1291 if (!status)
1292 {
1294 }
1295 }
1296
1297 ret = (status == 0);
1298
1299out:
1300
1301 return ret;
1302}
1303
1304bool
1305win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
1306{
1307 dmsg(D_LOW, "Uninitializing WFP");
1308
1309 if (msg_channel)
1310 {
1311 msg(D_LOW, "Using service to delete WFP block filters");
1312 win_wfp_block_service(false, false, index, msg_channel);
1313 }
1314 else
1315 {
1317 m_hEngineHandle = NULL;
1318 if (tap_metric_v4 >= 0)
1319 {
1320 set_interface_metric(index, AF_INET, tap_metric_v4);
1321 }
1322 if (tap_metric_v6 >= 0)
1323 {
1324 set_interface_metric(index, AF_INET6, tap_metric_v6);
1325 }
1326 }
1327
1328 return true;
1329}
1330
1331typedef enum
1332{
1336 ARCH_NATIVE, /* means no emulation, makes sense for host arch */
1339
1340static void
1341win32_get_arch(arch_t *process_arch, arch_t *host_arch)
1342{
1343#ifdef _ARM64_
1344 *process_arch = ARCH_ARM64;
1345#elif defined(_WIN64)
1346 *process_arch = ARCH_AMD64;
1347#else
1348 *process_arch = ARCH_X86;
1349#endif
1350
1351 *host_arch = ARCH_NATIVE;
1352 /* Determine if we're running on a different host-arch */
1353 USHORT process_machine = 0;
1354 USHORT native_machine = 0;
1355
1356 if (!IsWow64Process2(GetCurrentProcess(), &process_machine, &native_machine))
1357 {
1358 return;
1359 }
1360
1361 switch (native_machine)
1362 {
1363 case IMAGE_FILE_MACHINE_ARM64:
1364 *host_arch = ARCH_ARM64;
1365 break;
1366
1367 case IMAGE_FILE_MACHINE_AMD64:
1368 *host_arch = ARCH_AMD64;
1369 break;
1370
1371 case IMAGE_FILE_MACHINE_I386:
1372 *host_arch = ARCH_X86;
1373 break;
1374
1375 default:
1376 *host_arch = ARCH_UNKNOWN;
1377 break;
1378 }
1379
1380 if (*host_arch == *process_arch)
1381 {
1382 *host_arch = ARCH_NATIVE;
1383 }
1384}
1385
1386static void
1388{
1389 switch (arch)
1390 {
1391 case ARCH_X86:
1392 buf_printf(out, "x86");
1393 break;
1394
1395 case ARCH_AMD64:
1396 buf_printf(out, "amd64");
1397 break;
1398
1399 case ARCH_ARM64:
1400 buf_printf(out, "arm64");
1401 break;
1402
1403 case ARCH_UNKNOWN:
1404 buf_printf(out, "(unknown)");
1405 break;
1406
1407 default:
1408 break;
1409 }
1410}
1411
1412typedef LONG(WINAPI *RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
1413
1414const char *
1416{
1417 HMODULE hMod = GetModuleHandleW(L"ntdll.dll");
1418 if (!hMod)
1419 {
1420 return "N/A";
1421 }
1422
1423 RtlGetVersionPtr fn = (RtlGetVersionPtr)GetProcAddress(hMod, "RtlGetVersion");
1424 if (!fn)
1425 {
1426 return "N/A";
1427 }
1428
1429 RTL_OSVERSIONINFOW rovi = { 0 };
1430 rovi.dwOSVersionInfoSize = sizeof(rovi);
1431 if (fn(&rovi) != 0)
1432 {
1433 return "N/A";
1434 }
1435
1436 struct buffer out = alloc_buf_gc(256, gc);
1437
1438 buf_printf(&out, "%lu.%lu.%lu", rovi.dwMajorVersion, rovi.dwMinorVersion, rovi.dwBuildNumber);
1439
1440 buf_printf(&out, ",");
1441
1445
1446 if (host_arch != ARCH_NATIVE)
1447 {
1448 buf_printf(&out, " running on ");
1450 buf_printf(&out, " host");
1451 }
1452
1453 return (const char *)out.data;
1454}
1455
1456bool
1458 const char *context)
1459{
1460 struct gc_arena gc = gc_new();
1461 DWORD len;
1462 bool ret = true;
1463
1464 if (!WriteFile(pipe, data, size, &len, NULL) || !ReadFile(pipe, ack, sizeof(*ack), &len, NULL))
1465 {
1466 msg(M_WARN, "%s: could not talk to service: %s [%lu]", context ? context : "Unknown",
1467 strerror_win32(GetLastError(), &gc), GetLastError());
1468 ret = false;
1469 }
1470
1471 gc_free(&gc);
1472 return ret;
1473}
1474
1475bool
1476get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size)
1477{
1478 WCHAR reg_path[256];
1479 HKEY hkey;
1480 swprintf(reg_path, _countof(reg_path), L"SOFTWARE\\" PACKAGE_NAME);
1481
1482 LONG status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0, KEY_READ, &hkey);
1483 if (status != ERROR_SUCCESS)
1484 {
1485 return false;
1486 }
1487
1488 status = RegGetValueW(hkey, NULL, key, RRF_RT_REG_SZ, NULL, (LPBYTE)value, &size);
1489
1490 RegCloseKey(hkey);
1491
1492 return status == ERROR_SUCCESS;
1493}
1494
1495static void
1497{
1498 WCHAR install_path[MAX_PATH] = { 0 };
1499 if (!get_openvpn_reg_value(NULL, install_path, sizeof(install_path)))
1500 {
1501 const WCHAR *ssl_fallback_dir = L"C:\\Windows\\System32";
1502 /* if we cannot find installation path from the registry,
1503 * use Windows directory as a fallback
1504 */
1505 swprintf(install_path, _countof(install_path), L"%ls", ssl_fallback_dir);
1506 }
1507
1508 if ((install_path[wcslen(install_path) - 1]) == L'\\')
1509 {
1510 install_path[wcslen(install_path) - 1] = L'\0';
1511 }
1512
1513 static struct
1514 {
1515 WCHAR *name;
1516 WCHAR *value;
1517 } ossl_env[] = { { L"OPENSSL_CONF", L"openssl.cnf" },
1518 { L"OPENSSL_ENGINES", L"engines" },
1519 { L"OPENSSL_MODULES", L"modules" } };
1520
1521 for (size_t i = 0; i < SIZE(ossl_env); ++i)
1522 {
1523 size_t size = 0;
1524
1525 _wgetenv_s(&size, NULL, 0, ossl_env[i].name);
1526 if (size == 0)
1527 {
1528 WCHAR val[MAX_PATH] = { 0 };
1529 swprintf(val, _countof(val), L"%ls\\ssl\\%ls", install_path, ossl_env[i].value);
1530 _wputenv_s(ossl_env[i].name, val);
1531 }
1532 }
1533}
1534
1535void
1536win32_sleep(const int n)
1537{
1538 if (n < 0)
1539 {
1540 return;
1541 }
1542
1543 /* Sleep() is not interruptible. Use a WAIT_OBJECT to catch signal */
1544
1546 {
1547 if (n > 0)
1548 {
1549 Sleep(n * 1000);
1550 }
1551 return;
1552 }
1553
1554 update_time();
1555 time_t expire = now + n;
1556
1557 while (expire >= now)
1558 {
1559 DWORD wait_ms = (DWORD)((expire - now) * 1000);
1560 DWORD status = WaitForSingleObject(win32_signal.in.read, wait_ms);
1561 if ((status == WAIT_OBJECT_0 && win32_signal_get(&win32_signal)) || status == WAIT_TIMEOUT)
1562 {
1563 return;
1564 }
1565
1566 update_time();
1567
1568 if (status != WAIT_OBJECT_0) /* wait failed or some unexpected error ? */
1569 {
1570 if (expire > now)
1571 {
1572 Sleep((DWORD)((expire - now) * 1000));
1573 }
1574 return;
1575 }
1576 }
1577}
1578
1579bool
1580plugin_in_trusted_dir(const WCHAR *plugin_path)
1581{
1582 /* UNC paths are not allowed */
1583 if (wcsncmp(plugin_path, L"\\\\", 2) == 0)
1584 {
1585 msg(M_WARN, "UNC paths for plugins are not allowed.");
1586 return false;
1587 }
1588
1589 WCHAR plugin_dir[MAX_PATH] = { 0 };
1590
1591 /* Attempt to retrieve the trusted plugin directory path from the registry,
1592 * using installation path as a fallback */
1593 if (!get_openvpn_reg_value(L"plugin_dir", plugin_dir, sizeof(plugin_dir))
1594 && !get_openvpn_reg_value(NULL, plugin_dir, sizeof(plugin_dir)))
1595 {
1596 msg(M_WARN, "Installation path could not be determined.");
1597 }
1598
1599 /* Get the system directory */
1600 WCHAR system_dir[MAX_PATH] = { 0 };
1601 if (GetSystemDirectoryW(system_dir, _countof(system_dir)) == 0)
1602 {
1603 msg(M_NONFATAL | M_ERRNO, "Failed to get system directory.");
1604 }
1605
1606 if ((wcslen(plugin_dir) == 0) && (wcslen(system_dir) == 0))
1607 {
1608 return false;
1609 }
1610
1611 WCHAR normalized_plugin_dir[MAX_PATH] = { 0 };
1612
1613 /* Normalize the plugin dir */
1614 if (wcslen(plugin_dir) > 0)
1615 {
1616 if (!GetFullPathNameW(plugin_dir, MAX_PATH, normalized_plugin_dir, NULL))
1617 {
1618 msg(M_NONFATAL | M_ERRNO, "Failed to normalize plugin dir.");
1619 return false;
1620 }
1621 }
1622
1623 /* Check if the plugin path resides within the plugin/install directory */
1624 if (win_path_in_dir(plugin_path, normalized_plugin_dir))
1625 {
1626 return true;
1627 }
1628
1629 /* Fallback to the system directory */
1630 return win_path_in_dir(plugin_path, system_dir);
1631}
1632
1633bool
1634protect_buffer_win32(char *buf, DWORD len)
1635{
1636 bool ret;
1637 if (len % CRYPTPROTECTMEMORY_BLOCK_SIZE)
1638 {
1639 msg(M_NONFATAL, "Error: Unable to encrypt memory: buffer size not a multiple of %d",
1640 CRYPTPROTECTMEMORY_BLOCK_SIZE);
1641 return false;
1642 }
1643 ret = CryptProtectMemory(buf, len, CRYPTPROTECTMEMORY_SAME_PROCESS);
1644 if (!ret)
1645 {
1646 msg(M_NONFATAL | M_ERRNO, "Failed to encrypt memory.");
1647 }
1648 return ret;
1649}
1650
1651bool
1652unprotect_buffer_win32(char *buf, DWORD len)
1653{
1654 bool ret;
1655 if (len % CRYPTPROTECTMEMORY_BLOCK_SIZE)
1656 {
1657 msg(M_NONFATAL, "Error: Unable to decrypt memory: buffer size not a multiple of %d",
1658 CRYPTPROTECTMEMORY_BLOCK_SIZE);
1659 return false;
1660 }
1661 ret = CryptUnprotectMemory(buf, len, CRYPTPROTECTMEMORY_SAME_PROCESS);
1662 if (!ret)
1663 {
1664 msg(M_FATAL | M_ERRNO, "Failed to decrypt memory.");
1665 }
1666 return ret;
1667}
1668
1669#if defined(__GNUC__) || defined(__clang__)
1670#pragma GCC diagnostic pop
1671#endif
1672
1673#endif /* ifdef _WIN32 */
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:174
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
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 checked_snprintf(char *str, size_t size, const char *format,...)
Like snprintf() but returns an boolean.
Definition buffer.c:1150
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
Buffer management functions and garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
#define SCRIPT_SECURITY_WARNING
Definition common.h:99
#define PACKAGE_NAME
Definition config.h:492
#define PACKAGE_VERSION
Definition config.h:504
#define PACKAGE
Definition config.h:486
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:305
bool env_allowed(const char *str)
Definition env_set.c:430
#define D_SEMAPHORE_LOW
Definition errlevel.h:132
#define D_SEMAPHORE
Definition errlevel.h:133
#define D_LOW
Definition errlevel.h:95
#define M_INFO
Definition errlevel.h:54
static SERVICE_STATUS status
Definition interactive.c:52
void alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
Definition mtu.c:47
@ wfp_block_local
Definition openvpn-msg.h:76
@ wfp_block_dns
Definition openvpn-msg.h:77
@ msg_del_wfp_block
Definition openvpn-msg.h:44
@ msg_add_wfp_block
Definition openvpn-msg.h:43
#define CLEAR(x)
Definition basic.h:32
#define SIZE(x)
Definition basic.h:29
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
Definition error.c:786
#define M_NOPREFIX
Definition error.h:98
#define M_FATAL
Definition error.h:90
#define M_NONFATAL
Definition error.h:91
#define dmsg(flags,...)
Definition error.h:164
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
time_t now
Definition otime.c:33
static void update_time(void)
Definition otime.h:84
bool openvpn_execve_allowed(const unsigned int flags)
int script_security(void)
Definition run_command.c:42
#define SSEC_SCRIPTS
allow calling of built-in programs and user-defined scripts
Definition run_command.h:35
#define OPENVPN_EXECVE_ERROR
Definition run_command.h:40
#define OPENVPN_EXECVE_NOT_ALLOWED
Definition run_command.h:41
void throw_signal(const int signum)
Throw a hard signal.
Definition sig.c:175
struct signal_info siginfo_static
Definition sig.c:44
Definition argv.h:35
char ** argv
Definition argv.h:39
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:78
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Contains all state information for one tunnel.
Definition openvpn.h:445
char * string
Definition env_set.h:38
struct env_item * next
Definition env_set.h:39
struct env_item * list
Definition env_set.h:45
Packet geometry parameters.
Definition mtu.h:113
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:145
socket_descriptor_t sd
Definition win32.h:113
long event_mask
Definition win32.h:114
struct rw_handle handle
Definition win32.h:112
OVERLAPPED overlapped
Definition win32.h:210
struct buffer buf_init
Definition win32.h:221
int iostate
Definition win32.h:209
HANDLE write
Definition win32.h:83
HANDLE read
Definition win32.h:82
SECURITY_ATTRIBUTES sa
Definition win32.h:64
SECURITY_DESCRIPTOR sd
Definition win32.h:65
const char * name
Definition win32.h:245
bool locked
Definition win32.h:246
HANDLE hand
Definition win32.h:247
volatile int signal_received
Definition sig.h:42
message_header_t header
DWORD console_mode_save
Definition win32.h:163
struct rw_handle in
Definition win32.h:162
bool console_mode_save_defined
Definition win32.h:164
bool saved
Definition win32.h:76
char old_window_title[256]
Definition win32.h:77
#define SIGHUP
Definition syshead.h:55
#define SOCKET_UNDEFINED
Definition syshead.h:439
#define SIGTERM
Definition syshead.h:59
SOCKET socket_descriptor_t
Definition syshead.h:441
#define SIGUSR1
Definition syshead.h:57
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:449
#define SIGUSR2
Definition syshead.h:58
struct gc_arena gc
Definition test_ssl.c:138
int get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family, int *is_auto)
Return interface metric value for the specified interface index.
Definition wfp_block.c:378
DWORD set_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family, const ULONG metric)
Sets interface metric value for specified interface index.
Definition wfp_block.c:417
DWORD delete_wfp_block_filters(HANDLE engine_handle)
Definition wfp_block.c:353
DWORD add_wfp_block_filters(HANDLE *engine_handle, int index, const WCHAR *exe_path, wfp_block_msg_handler_t msg_handler, BOOL dns_only)
Definition wfp_block.c:158
#define WFP_BLOCK_IFACE_METRIC
Definition wfp_block.h:33
bool win_path_in_dir(const WCHAR *path, const WCHAR *dir)
Check whether path resides within directory dir.
Definition win32-util.c:258
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 unsigned int keyboard_ir_to_key(INPUT_RECORD *ir)
Definition win32.c:657
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:316
static bool win_get_exe_path(PWCHAR path, DWORD size)
Definition win32.c:1194
void net_event_win32_init(struct net_event_win32 *ne)
Definition win32.c:420
void net_event_win32_close(struct net_event_win32 *ne)
Definition win32.c:471
void win32_signal_open(struct win32_signal *ws, int force, const char *exit_event_name, bool exit_event_initial_state)
Definition win32.c:554
void uninit_win32(void)
Definition win32.c:123
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
Definition win32.c:266
bool win_wfp_block(const NET_IFINDEX index, const HANDLE msg_channel, BOOL dns_only)
Definition win32.c:1256
void window_title_generate(const char *title)
Definition win32.c:826
static bool init_security_attributes_allow_user(struct security_attributes *obj)
Initializes security attributes with a DACL restricted to the current process user.
Definition win32.c:192
void set_win_sys_path(const char *newpath, struct env_set *es)
Definition win32.c:1169
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:371
void window_title_clear(struct window_title *wt)
Definition win32.c:794
static unsigned int win32_keyboard_get(struct win32_signal *ws)
Definition win32.c:674
bool get_openvpn_reg_value(const WCHAR *key, WCHAR *value, DWORD size)
Fetches a registry value for OpenVPN registry key.
Definition win32.c:1476
static bool pause_exit_enabled
Definition win32.c:76
void window_title_save(struct window_title *wt)
Definition win32.c:800
bool protect_buffer_win32(char *buf, DWORD len)
Encrypt a region of memory using CryptProtectMemory() with access restricted to the current process.
Definition win32.c:1634
void semaphore_clear(struct semaphore *s)
Definition win32.c:843
bool plugin_in_trusted_dir(const WCHAR *plugin_path)
Checks if a plugin is located in a trusted directory.
Definition win32.c:1580
void semaphore_close(struct semaphore *s)
Definition win32.c:923
static bool keyboard_input_available(struct win32_signal *ws)
Definition win32.c:642
bool unprotect_buffer_win32(char *buf, DWORD len)
Decrypt a previously encrypted region of memory using CryptUnProtectMemory() with access restricted t...
Definition win32.c:1652
void net_event_win32_reset_write(struct net_event_win32 *ne)
Definition win32.c:436
bool semaphore_lock(struct semaphore *s, int timeout_milliseconds)
Definition win32.c:874
void net_event_win32_start(struct net_event_win32 *ne, long network_events, socket_descriptor_t sd)
Definition win32.c:427
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel)
Definition win32.c:1305
void fork_to_self(const char *cmdline)
Definition win32.c:1121
static char * env_block(const struct env_set *es)
Definition win32.c:983
static void win_trigger_event(struct win32_signal *ws)
Definition win32.c:489
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition win32.c:295
static int tap_metric_v4
Definition win32.c:65
void overlapped_io_close(struct overlapped_io *o)
Definition win32.c:282
void netcmd_semaphore_release(void)
Definition win32.c:971
void win32_sleep(const int n)
Definition win32.c:1536
static char * win_sys_path
Definition win32.c:102
static bool win_wfp_block_service(bool add, bool dns_only, int index, const HANDLE pipe)
Definition win32.c:1223
int openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags)
Definition win32.c:1047
static void win32_print_arch(arch_t arch, struct buffer *out)
Definition win32.c:1387
static void win32_get_arch(arch_t *process_arch, arch_t *host_arch)
Definition win32.c:1341
void semaphore_release(struct semaphore *s)
Definition win32.c:908
void init_win32(void)
Definition win32.c:110
static HANDLE m_hEngineHandle
Definition win32.c:60
void set_win_sys_path_via_env(struct env_set *es)
Definition win32.c:1177
static void set_openssl_env_vars(void)
Set OpenSSL environment variables to a safe directory.
Definition win32.c:1496
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:356
static bool WINAPI win_ctrl_handler(DWORD signum)
Definition win32.c:515
void window_title_restore(const struct window_title *wt)
Definition win32.c:817
struct semaphore netcmd_semaphore
Definition win32.c:97
char * get_win_sys_path(void)
Definition win32.c:1162
bool win32_service_interrupt(struct win32_signal *ws)
Definition win32.c:722
static int tap_metric_v6
Definition win32.c:66
void win32_pause(struct win32_signal *ws)
Definition win32.c:779
static void win_wfp_msg_handler(DWORD err, const char *msg)
Definition win32.c:1206
static struct WSAData wsa_state
Definition win32.c:71
void netcmd_semaphore_init(void)
Definition win32.c:943
void netcmd_semaphore_lock(void)
Definition win32.c:955
void win32_signal_close(struct win32_signal *ws)
Definition win32.c:702
const char * win32_version_string(struct gc_arena *gc)
Get Windows version string with architecture info.
Definition win32.c:1415
void win32_signal_clear(struct win32_signal *ws)
Definition win32.c:543
bool send_msg_iservice(HANDLE pipe, const void *data, DWORD size, ack_message_t *ack, const char *context)
Send the size bytes in buffer data to the interactive service pipe and read the result in ack.
Definition win32.c:1457
int win32_signal_get(struct win32_signal *ws)
Definition win32.c:735
void net_event_win32_reset(struct net_event_win32 *ne)
Definition win32.c:454
void netcmd_semaphore_close(void)
Definition win32.c:949
void set_pause_exit_win32(void)
Definition win32.c:147
arch_t
Definition win32.c:1332
@ ARCH_AMD64
Definition win32.c:1334
@ ARCH_NATIVE
Definition win32.c:1336
@ ARCH_X86
Definition win32.c:1333
@ ARCH_ARM64
Definition win32.c:1335
@ ARCH_UNKNOWN
Definition win32.c:1337
static void free_security_attributes(struct security_attributes *obj)
Releases resources allocated by init_security_attributes_allow_user().
Definition win32.c:256
bool init_security_attributes_allow_all(struct security_attributes *obj)
Initializes security attributes with a NULL DACL, allowing unrestricted access to the resulting objec...
Definition win32.c:160
void semaphore_open(struct semaphore *s, const char *name)
Definition win32.c:849
void net_event_win32_stop(struct net_event_win32 *ne)
Definition win32.c:460
LONG(WINAPI * RtlGetVersionPtr)(PRTL_OSVERSIONINFOW)
Definition win32.c:1412
static bool net_event_win32_defined(const struct net_event_win32 *ne)
Definition win32.h:130
#define SYS_PATH_ENV_VAR_NAME
Definition win32.h:37
#define WSO_MODE_UNDEF
Definition win32.h:158
#define NE32_PERSIST_EVENT
Definition win32.h:90
#define IOSTATE_IMMEDIATE_RETURN
Definition win32.h:208
#define WSO_MODE_SERVICE
Definition win32.h:159
#define WSO_MODE_CONSOLE
Definition win32.h:160
#define IOSTATE_INITIAL
Definition win32.h:206
#define IOSTATE_QUEUED
Definition win32.h:207
#define NE32_WRITE_EVENT
Definition win32.h:91
#define WSO_FORCE_SERVICE
Definition win32.h:174
#define WSO_FORCE_CONSOLE
Definition win32.h:175
#define WSO_NOFORCE
Definition win32.h:173
#define HANDLE_DEFINED(h)
Definition win32.h:69