OpenVPN
openvpn.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/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-2024 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 HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include "syshead.h"
29
30#include "init.h"
31#include "forward.h"
32#include "multi.h"
33#include "win32.h"
34#include "platform.h"
35#include "string.h"
36
37#include "memdbg.h"
38
39#define P2P_CHECK_SIG() EVENT_LOOP_CHECK_SIGNAL(c, process_signal_p2p, c);
40
41static bool
43{
44 remap_signal(c);
45 return process_signal(c);
46}
47
48
49/**************************************************************************/
57static void
59{
61
62 /* set point-to-point mode */
63 c->mode = CM_P2P;
64 /* initialize tunnel instance, avoid SIGHUP when config is stdin since
65 * re-reading the config from stdin will not work */
66 bool stdin_config = c->options.config && (strcmp(c->options.config, "stdin") == 0);
67 init_instance_handle_signals(c, c->es, stdin_config ? 0 : CC_HARD_USR1_TO_HUP);
68 if (IS_SIG(c))
69 {
70 return;
71 }
72
73 /* main event loop */
74 while (true)
75 {
77
78 /* process timers, TLS, etc. */
79 pre_select(c);
81
82 /* set up and do the I/O wait */
85
86 /* timeout? */
88 {
89 perf_pop();
90 continue;
91 }
92
93 /* process the I/O which triggered select */
94 process_io(c, c->c2.link_sockets[0]);
96
97 perf_pop();
98 }
99
101
103
104 /* tear down tunnel instance (unless --persist-tun) */
106}
107
108#undef PROCESS_SIGNAL_P2P
109
110void
112{
113 net_ctx_init(c, &c->net_ctx);
114
115 /* init verbosity and mute levels */
117
118 /* Initialise OpenSSL provider, this needs to be initialised this
119 * early since option post-processing and also openssl info
120 * printing depends on it */
121 for (int j = 1; j < MAX_PARMS && c->options.providers.names[j]; j++)
122 {
125 }
126}
127
128static void
130{
131 for (int j = 1; j < MAX_PARMS && c->options.providers.providers[j]; j++)
132 {
135 }
137}
138
139
140/**************************************************************************/
160static
161int
162openvpn_main(int argc, char *argv[])
163{
164 struct context c;
165
166#if PEDANTIC
167 fprintf(stderr, "Sorry, I was built with --enable-pedantic and I am incapable of doing any real work!\n");
168 return 1;
169#endif
170
171#ifdef _WIN32
172 SetConsoleOutputCP(CP_UTF8);
173#endif
174
175 CLEAR(c);
176
177 /* signify first time for components which can
178 * only be initialized once per program instantiation. */
179 c.first_time = true;
180
181 /* initialize program-wide statics */
182 if (init_static())
183 {
184 /*
185 * This loop is initially executed on startup and then
186 * once per SIGHUP.
187 */
188 do
189 {
190 /* enter pre-initialization mode with regard to signal handling */
192
193 /* zero context struct but leave first_time member alone */
195
196 /* static signal info object */
197 c.sig = &siginfo_static;
198
199 /* initialize garbage collector scoped to context object */
200 gc_init(&c.gc);
201
202 /* initialize environmental variable store */
203 c.es = env_set_create(NULL);
204#ifdef _WIN32
206#endif
207
208#ifdef ENABLE_MANAGEMENT
209 /* initialize management subsystem */
211#endif
212
213 /* initialize options to default state */
214 init_options(&c.options, true);
215
216 /* parse command line options, and read configuration file */
217 parse_argv(&c.options, argc, argv, M_USAGE, OPT_P_DEFAULT, NULL, c.es);
218
219#ifdef ENABLE_PLUGIN
220 /* plugins may contribute options configuration */
222 init_plugins(&c);
223 open_plugins(&c, true, OPENVPN_PLUGIN_INIT_PRE_CONFIG_PARSE);
224#endif
225
226 /* Early initialisation that need to happen before option
227 * post processing and other early startup but after parsing */
228 init_early(&c);
229
230 /* set dev options */
232
233 /* openssl print info? */
235 {
236 break;
237 }
238
239 /* --genkey mode? */
240 if (do_genkey(&c.options))
241 {
242 break;
243 }
244
245 /* tun/tap persist command? */
247 {
248 break;
249 }
250
251 /* sanity check on options */
253
254 /* show all option settings */
256
257 /* print version number */
258 msg(M_INFO, "%s", title_string);
259#ifdef _WIN32
261#endif
263
265
266 /* misc stuff */
267 pre_setup(&c.options);
268
269 /* test crypto? */
270 if (do_test_crypto(&c.options))
271 {
272 break;
273 }
274
275 /* Query passwords before becoming a daemon if we don't use the
276 * management interface to get them. */
278 {
280 }
281
282 /* become a daemon if --daemon */
283 if (c.first_time)
284 {
287 }
288
289#ifdef ENABLE_MANAGEMENT
290 /* open management subsystem */
291 if (!open_management(&c))
292 {
293 break;
294 }
295 /* query for passwords through management interface, if needed */
297 {
299 }
300#endif
301
302 /* set certain options as environmental variables */
304
305 /* finish context init */
306 context_init_1(&c);
307
308 do
309 {
310 /* run tunnel depending on mode */
311 switch (c.options.mode)
312 {
315 break;
316
317 case MODE_SERVER:
318 tunnel_server(&c);
319 break;
320
321 default:
322 ASSERT(0);
323 }
324
325 /* indicates first iteration -- has program-wide scope */
326 c.first_time = false;
327
328 /* any signals received? */
329 if (IS_SIG(&c))
330 {
331 print_signal(c.sig, NULL, M_INFO);
332 }
333
334 /* pass restart status to management subsystem */
336 }
337 while (signal_reset(c.sig, SIGUSR1) == SIGUSR1);
338
341 gc_reset(&c.gc);
342 uninit_early(&c);
343 }
344 while (signal_reset(c.sig, SIGHUP) == SIGHUP);
345 }
346
347 context_gc_free(&c);
348
349#ifdef ENABLE_MANAGEMENT
350 /* close management interface */
352#endif
353
354 /* uninitialize program-wide statics */
356
357 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
358 return 0; /* NOTREACHED */
359}
360
361#ifdef _WIN32
362int
363wmain(int argc, wchar_t *wargv[])
364{
365 char **argv;
366 int ret;
367 int i;
368
369 if ((argv = calloc(argc+1, sizeof(char *))) == NULL)
370 {
371 return 1;
372 }
373
374 for (i = 0; i < argc; i++)
375 {
376 int n = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
377 argv[i] = malloc(n);
378 WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i], n, NULL, NULL);
379 }
380
381 ret = openvpn_main(argc, argv);
382
383 for (i = 0; i < argc; i++)
384 {
385 free(argv[i]);
386 }
387 free(argv);
388
389 return ret;
390}
391#else /* ifdef _WIN32 */
392int
393main(int argc, char *argv[])
394{
395 return openvpn_main(argc, argv);
396}
397#endif /* ifdef _WIN32 */
static void gc_init(struct gc_arena *a)
Definition buffer.h:1012
static void gc_reset(struct gc_arena *a)
Definition buffer.h:1046
void crypto_unload_provider(const char *provname, provider_t *provider)
Unloads the given (OpenSSL) provider.
provider_t * crypto_load_provider(const char *provider)
Load the given (OpenSSL) providers.
void env_set_destroy(struct env_set *es)
Definition env_set.c:166
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:156
#define M_INFO
Definition errlevel.h:55
#define ES_TIMEOUT
Definition event.h:69
void process_io(struct context *c, struct link_socket *sock)
Definition forward.c:2387
void pre_select(struct context *c)
Definition forward.c:1997
Interface functions to the internal and external multiplexers.
static unsigned int p2p_iow_flags(const struct context *c)
Definition forward.h:352
static void io_wait(struct context *c, const unsigned int flags)
Definition forward.h:377
static int openvpn_main(int argc, char *argv[])
OpenVPN's main init-run-cleanup loop.
Definition openvpn.c:162
void tunnel_server(struct context *top)
Main event loop for OpenVPN in server mode.
Definition multi.c:4219
static void tunnel_point_to_point(struct context *c)
Main event loop for OpenVPN in client mode, where only one VPN tunnel is active.
Definition openvpn.c:58
void uninit_management_callback(void)
Definition init.c:4532
bool open_management(struct context *c)
Definition init.c:4469
bool do_genkey(const struct options *options)
Definition init.c:1031
void open_plugins(struct context *c, const bool import_options, int init_point)
Definition init.c:4303
void init_verb_mute(struct context *c, unsigned int flags)
Definition init.c:959
void close_instance(struct context *c)
Definition init.c:4872
void persist_client_stats(struct context *c)
Definition init.c:4543
bool do_test_crypto(const struct options *o)
Definition init.c:5187
void init_plugins(struct context *c)
Definition init.c:4293
void context_init_1(struct context *c)
Definition init.c:747
void pre_setup(const struct options *options)
Definition init.c:1314
void init_management(void)
Definition init.c:4460
void uninit_static(void)
Definition init.c:941
void init_instance_handle_signals(struct context *c, const struct env_set *env, const unsigned int flags)
Definition init.c:4558
void write_pid_file(const char *filename, const char *chroot_dir)
Definition init.c:5116
void context_gc_free(struct context *c)
Definition init.c:798
void init_options_dev(struct options *options)
Definition init.c:982
void init_query_passwords(const struct context *c)
Query for private key and auth-user-pass username/passwords.
Definition init.c:651
void context_clear_2(struct context *c)
Definition init.c:89
bool print_openssl_info(const struct options *options)
Definition init.c:992
bool do_persist_tuntap(struct options *options, openvpn_net_ctx_t *ctx)
Definition init.c:1114
bool init_static(void)
Definition init.c:837
bool possibly_become_daemon(const struct options *options)
Definition init.c:1177
void context_clear_all_except_first_time(struct context *c)
Definition init.c:95
void close_management(void)
Definition init.c:4519
#define IVM_LEVEL_1
Definition init.h:49
#define CC_HARD_USR1_TO_HUP
Definition init.h:106
#define MF_QUERY_PASSWORDS
Definition manage.h:29
Header file for server-mode related structures and functions.
static int net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)
Definition networking.h:48
static void net_ctx_free(openvpn_net_ctx_t *ctx)
Definition networking.h:63
int main(void)
#define CLEAR(x)
Definition basic.h:33
void openvpn_exit(const int status)
Definition error.c:735
#define M_USAGE
Definition error.h:106
#define OPENVPN_EXIT_STATUS_GOOD
Definition error.h:53
#define msg(flags,...)
Definition error.h:144
#define ASSERT(x)
Definition error.h:195
static void uninit_early(struct context *c)
Definition openvpn.c:129
int wmain(int argc, wchar_t *wargv[])
Definition openvpn.c:363
static bool process_signal_p2p(struct context *c)
Definition openvpn.c:42
#define P2P_CHECK_SIG()
Definition openvpn.c:39
void init_early(struct context *c)
Definition openvpn.c:111
#define CM_P2P
Definition openvpn.h:482
void uninit_options(struct options *o)
Definition options.c:925
void show_windows_version(const unsigned int flags)
Definition options.c:4974
void show_dco_version(const unsigned int flags)
Definition options.c:4983
void show_settings(const struct options *o)
Definition options.c:1836
void parse_argv(struct options *options, const int argc, char *argv[], const int msglevel, const unsigned int permission_mask, unsigned int *option_types_found, struct env_set *es)
Definition options.c:5500
void options_postprocess(struct options *options, struct env_set *es)
Definition options.c:4296
void show_library_versions(const unsigned int flags)
Definition options.c:4993
void setenv_settings(struct env_set *es, const struct options *o)
Definition options.c:1028
void init_options(struct options *o, const bool init_gc)
Definition options.c:803
const char title_string[]
Definition options.c:69
#define MODE_POINT_TO_POINT
Definition options.h:258
#define MODE_SERVER
Definition options.h:259
#define OPT_P_DEFAULT
Definition options.h:762
#define MAX_PARMS
Definition options.h:52
static void perf_push(int type)
Definition perf.h:78
#define PERF_EVENT_LOOP
Definition perf.h:44
static void perf_pop(void)
Definition perf.h:82
bool process_signal(struct context *c)
Definition sig.c:640
void signal_restart_status(const struct signal_info *si)
Definition sig.c:348
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
Definition sig.c:266
void pre_init_signal_catch(void)
Definition sig.c:398
void remap_signal(struct context *c)
Definition sig.c:591
struct signal_info siginfo_static
Definition sig.c:45
void print_signal(const struct signal_info *si, const char *title, int msglevel)
Definition sig.c:294
#define IS_SIG(c)
Definition sig.h:48
Definition argv.h:35
unsigned int event_set_status
Definition openvpn.h:235
struct link_socket ** link_sockets
Definition openvpn.h:237
Contains all state information for one tunnel.
Definition openvpn.h:474
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:487
bool did_we_daemonize
Whether demonization has already taken place.
Definition openvpn.h:507
bool first_time
True on the first iteration of OpenVPN's main loop.
Definition openvpn.h:478
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:500
openvpn_net_ctx_t net_ctx
Networking API opaque context.
Definition openvpn.h:498
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct env_set * es
Set of environment variables.
Definition openvpn.h:496
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:475
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:492
struct provider_list providers
Definition options.h:580
const char * writepid
Definition options.h:382
unsigned int management_flags
Definition options.h:459
int mode
Definition options.h:260
const char * config
Definition options.h:255
const char * chroot_dir
Definition options.h:377
provider_t * providers[MAX_PARMS]
Definition options.h:215
const char * names[MAX_PARMS]
Definition options.h:213
void set_win_sys_path_via_env(struct env_set *es)
Definition win32.c:1128