OpenVPN
options.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-2023 OpenVPN Inc <sales@openvpn.net>
9  * Copyright (C) 2008-2023 David Sommerseth <dazo@eurephia.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2
13  * as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 /*
26  * 2004-01-28: Added Socks5 proxy support
27  * (Christof Meerwald, http://cmeerw.org)
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #ifdef HAVE_CONFIG_VERSION_H
34 #include "config-version.h"
35 #endif
36 
37 #include "syshead.h"
38 
39 #include "buffer.h"
40 #include "error.h"
41 #include "common.h"
42 #include "run_command.h"
43 #include "shaper.h"
44 #include "crypto.h"
45 #include "ssl.h"
46 #include "ssl_ncp.h"
47 #include "options.h"
48 #include "misc.h"
49 #include "socket.h"
50 #include "packet_id.h"
51 #include "pkcs11.h"
52 #include "win32.h"
53 #include "push.h"
54 #include "pool.h"
55 #include "proto.h"
56 #include "helper.h"
57 #include "manage.h"
58 #include "forward.h"
59 #include "ssl_verify.h"
60 #include "platform.h"
61 #include "xkey_common.h"
62 #include "dco.h"
63 #include <ctype.h>
64 
65 #include "memdbg.h"
66 
67 const char title_string[] =
69 #ifdef CONFIGURE_GIT_REVISION
70  " [git:" CONFIGURE_GIT_REVISION CONFIGURE_GIT_FLAGS "]"
71 #endif
72  " " TARGET_ALIAS
73 #if defined(ENABLE_CRYPTO_MBEDTLS)
74  " [SSL (mbed TLS)]"
75 #elif defined(ENABLE_CRYPTO_OPENSSL)
76  " [SSL (OpenSSL)]"
77 #else
78  " [SSL]"
79 #endif /* defined(ENABLE_CRYPTO_MBEDTLS) */
80 #ifdef USE_COMP
81 #ifdef ENABLE_LZO
82  " [LZO]"
83 #endif
84 #ifdef ENABLE_LZ4
85  " [LZ4]"
86 #endif
87 #ifdef ENABLE_COMP_STUB
88  " [COMP_STUB]"
89 #endif
90 #endif /* USE_COMP */
91 #if EPOLL
92  " [EPOLL]"
93 #endif
94 #ifdef PRODUCT_TAP_DEBUG
95  " [TAPDBG]"
96 #endif
97 #ifdef ENABLE_PKCS11
98  " [PKCS11]"
99 #endif
100 #if ENABLE_IP_PKTINFO
101 #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
102  " [MH/PKTINFO]"
103 #elif defined(IP_RECVDSTADDR)
104  " [MH/RECVDA]"
105 #endif
106 #endif
107  " [AEAD]"
108 #ifdef ENABLE_DCO
109  " [DCO]"
110 #endif
111 #ifdef CONFIGURE_GIT_REVISION
112  " built on " __DATE__
113 #endif
114 ;
115 
116 #ifndef ENABLE_SMALL
117 
118 static const char usage_message[] =
119  "%s\n"
120  "\n"
121  "General Options:\n"
122  "--config file : Read configuration options from file.\n"
123  "--help : Show options.\n"
124  "--version : Show copyright and version information.\n"
125  "\n"
126  "Tunnel Options:\n"
127  "--local host : Local host name or ip address. Implies --bind.\n"
128  "--remote host [port] : Remote host name or ip address.\n"
129  "--remote-random : If multiple --remote options specified, choose one randomly.\n"
130  "--remote-random-hostname : Add a random string to remote DNS name.\n"
131  "--mode m : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
132  "--proto p : Use protocol p for communicating with peer.\n"
133  " p = udp (default), tcp-server, tcp-client\n"
134  " udp4, tcp4-server, tcp4-client\n"
135  " udp6, tcp6-server, tcp6-client\n"
136  "--proto-force p : only consider protocol p in list of connection profiles.\n"
137  " p = udp or tcp\n"
138  "--connect-retry n [m] : For client, number of seconds to wait between\n"
139  " connection retries (default=%d). On repeated retries\n"
140  " the wait time is exponentially increased to a maximum of m\n"
141  " (default=%d).\n"
142  "--connect-retry-max n : Maximum connection attempt retries, default infinite.\n"
143  "--http-proxy s p [up] [auth] : Connect to remote host\n"
144  " through an HTTP proxy at address s and port p.\n"
145  " If proxy authentication is required,\n"
146  " up is a file containing username/password on 2 lines, or\n"
147  " 'stdin' to prompt from console. Add auth='ntlm' if\n"
148  " the proxy requires NTLM authentication.\n"
149  "--http-proxy s p 'auto[-nct]' : Like the above directive, but automatically\n"
150  " determine auth method and query for username/password\n"
151  " if needed. auto-nct disables weak proxy auth methods.\n"
152  "--http-proxy-option type [parm] : Set extended HTTP proxy options.\n"
153  " Repeat to set multiple options.\n"
154  " VERSION version (default=1.0)\n"
155  " AGENT user-agent\n"
156  "--socks-proxy s [p] [up] : Connect to remote host through a Socks5 proxy at\n"
157  " address s and port p (default port = 1080).\n"
158  " If proxy authentication is required,\n"
159  " up is a file containing username/password on 2 lines, or\n"
160  " 'stdin' to prompt for console.\n"
161  "--socks-proxy-retry : Retry indefinitely on Socks proxy errors.\n"
162  "--resolv-retry n: If hostname resolve fails for --remote, retry\n"
163  " resolve for n seconds before failing (disabled by default).\n"
164  " Set n=\"infinite\" to retry indefinitely.\n"
165  "--float : Allow remote to change its IP address/port, such as through\n"
166  " DHCP (this is the default if --remote is not used).\n"
167  "--ipchange cmd : Run command cmd on remote ip address initial\n"
168  " setting or change -- execute as: cmd ip-address port#\n"
169  "--port port : TCP/UDP port # for both local and remote.\n"
170  "--lport port : TCP/UDP port # for local (default=%s). Implies --bind.\n"
171  "--rport port : TCP/UDP port # for remote (default=%s).\n"
172  "--bind : Bind to local address and port. (This is the default unless\n"
173  " --proto tcp-client"
174  " or --http-proxy"
175  " or --socks-proxy"
176  " is used).\n"
177  "--nobind : Do not bind to local address and port.\n"
178  "--dev tunX|tapX : tun/tap device (X can be omitted for dynamic device.\n"
179  "--dev-type dt : Which device type are we using? (dt = tun or tap) Use\n"
180  " this option only if the tun/tap device used with --dev\n"
181  " does not begin with \"tun\" or \"tap\".\n"
182  "--dev-node node : Explicitly set the device node rather than using\n"
183  " /dev/net/tun, /dev/tun, /dev/tap, etc.\n"
184 #if defined(ENABLE_DCO)
185  "--disable-dco : Do not attempt using Data Channel Offload.\n"
186 #endif
187  "--lladdr hw : Set the link layer address of the tap device.\n"
188  "--topology t : Set --dev tun topology: 'net30', 'p2p', or 'subnet'.\n"
189 #ifdef ENABLE_IPROUTE
190  "--iproute cmd : Use this command instead of default " IPROUTE_PATH ".\n"
191 #endif
192  "--ifconfig l rn : TUN: configure device to use IP address l as a local\n"
193  " endpoint and rn as a remote endpoint. l & rn should be\n"
194  " swapped on the other peer. l & rn must be private\n"
195  " addresses outside of the subnets used by either peer.\n"
196  " TAP: configure device to use IP address l as a local\n"
197  " endpoint and rn as a subnet mask.\n"
198  "--ifconfig-ipv6 l r : configure device to use IPv6 address l as local\n"
199  " endpoint (as a /64) and r as remote endpoint\n"
200  "--ifconfig-noexec : Don't actually execute ifconfig/netsh command, instead\n"
201  " pass --ifconfig parms by environment to scripts.\n"
202  "--ifconfig-nowarn : Don't warn if the --ifconfig option on this side of the\n"
203  " connection doesn't match the remote side.\n"
204  "--route network [netmask] [gateway] [metric] :\n"
205  " Add route to routing table after connection\n"
206  " is established. Multiple routes can be specified.\n"
207  " netmask default: 255.255.255.255\n"
208  " gateway default: taken from --route-gateway or --ifconfig\n"
209  " Specify default by leaving blank or setting to \"default\".\n"
210  "--route-ipv6 network/bits [gateway] [metric] :\n"
211  " Add IPv6 route to routing table after connection\n"
212  " is established. Multiple routes can be specified.\n"
213  " gateway default: taken from --route-ipv6-gateway or 'remote'\n"
214  " in --ifconfig-ipv6\n"
215  "--route-gateway gw|'dhcp' : Specify a default gateway for use with --route.\n"
216  "--route-ipv6-gateway gw : Specify a default gateway for use with --route-ipv6.\n"
217  "--route-metric m : Specify a default metric for use with --route.\n"
218  "--route-delay n [w] : Delay n seconds after connection initiation before\n"
219  " adding routes (may be 0). If not specified, routes will\n"
220  " be added immediately after tun/tap open. On Windows, wait\n"
221  " up to w seconds for TUN/TAP adapter to come up.\n"
222  "--route-up cmd : Run command cmd after routes are added.\n"
223  "--route-pre-down cmd : Run command cmd before routes are removed.\n"
224  "--route-noexec : Don't add routes automatically. Instead pass routes to\n"
225  " --route-up script using environmental variables.\n"
226  "--route-nopull : When used with --client or --pull, accept options pushed\n"
227  " by server EXCEPT for routes, dns, and dhcp options.\n"
228  "--allow-pull-fqdn : Allow client to pull DNS names from server for\n"
229  " --ifconfig, --route, and --route-gateway.\n"
230  "--redirect-gateway [flags]: Automatically execute routing\n"
231  " commands to redirect all outgoing IP traffic through the\n"
232  " VPN. Add 'local' flag if both " PACKAGE_NAME " servers are directly\n"
233  " connected via a common subnet, such as with WiFi.\n"
234  " Add 'def1' flag to set default route using using 0.0.0.0/1\n"
235  " and 128.0.0.0/1 rather than 0.0.0.0/0. Add 'bypass-dhcp'\n"
236  " flag to add a direct route to DHCP server, bypassing tunnel.\n"
237  " Add 'bypass-dns' flag to similarly bypass tunnel for DNS.\n"
238  "--redirect-private [flags]: Like --redirect-gateway, but omit actually changing\n"
239  " the default gateway. Useful when pushing private subnets.\n"
240  "--block-ipv6 : (Client) Instead sending IPv6 to the server generate\n"
241  " ICMPv6 host unreachable messages on the client.\n"
242  " (Server) Instead of forwarding IPv6 packets send\n"
243  " ICMPv6 host unreachable packets to the client.\n"
244  "--client-nat snat|dnat network netmask alias : on client add 1-to-1 NAT rule.\n"
245  "--push-peer-info : (client only) push client info to server.\n"
246  "--setenv name value : Set a custom environmental variable to pass to script.\n"
247  "--setenv FORWARD_COMPATIBLE 1 : Relax config file syntax checking to allow\n"
248  " directives for future OpenVPN versions to be ignored.\n"
249  "--ignore-unknown-option opt1 opt2 ...: Relax config file syntax. Allow\n"
250  " these options to be ignored when unknown\n"
251  "--script-security level: Where level can be:\n"
252  " 0 -- strictly no calling of external programs\n"
253  " 1 -- (default) only call built-ins such as ifconfig\n"
254  " 2 -- allow calling of built-ins and scripts\n"
255  " 3 -- allow password to be passed to scripts via env\n"
256  "--shaper n : Restrict output to peer to n bytes per second.\n"
257  "--keepalive n m : Helper option for setting timeouts in server mode. Send\n"
258  " ping once every n seconds, restart if ping not received\n"
259  " for m seconds.\n"
260  "--inactive n [bytes] : Exit after n seconds of activity on tun/tap device\n"
261  " produces a combined in/out byte count < bytes.\n"
262  "--session-timeout n: Limit connection time to n seconds.\n"
263  "--ping-exit n : Exit if n seconds pass without reception of remote ping.\n"
264  "--ping-restart n: Restart if n seconds pass without reception of remote ping.\n"
265  "--ping-timer-rem: Run the --ping-exit/--ping-restart timer only if we have a\n"
266  " remote address.\n"
267  "--ping n : Ping remote once every n seconds over TCP/UDP port.\n"
268 #if ENABLE_IP_PKTINFO
269  "--multihome : Configure a multi-homed UDP server.\n"
270 #endif
271  "--fast-io : (experimental) Optimize TUN/TAP/UDP writes.\n"
272  "--remap-usr1 s : On SIGUSR1 signals, remap signal (s='SIGHUP' or 'SIGTERM').\n"
273  "--persist-tun : Keep tun/tap device open across SIGUSR1 or --ping-restart.\n"
274  "--persist-remote-ip : Keep remote IP address across SIGUSR1 or --ping-restart.\n"
275  "--persist-local-ip : Keep local IP address across SIGUSR1 or --ping-restart.\n"
276  "--persist-key : Don't re-read key files across SIGUSR1 or --ping-restart.\n"
277 #if PASSTOS_CAPABILITY
278  "--passtos : TOS passthrough (applies to IPv4 only).\n"
279 #endif
280  "--tun-mtu n : Take the tun/tap device MTU to be n and derive the\n"
281  " TCP/UDP MTU from it (default=%d).\n"
282  "--tun-mtu-extra n : Assume that tun/tap device might return as many\n"
283  " as n bytes more than the tun-mtu size on read\n"
284  " (default TUN=0 TAP=%d).\n"
285  "--link-mtu n : Take the TCP/UDP device MTU to be n and derive the tun MTU\n"
286  " from it.\n"
287  "--mtu-disc type : Should we do Path MTU discovery on TCP/UDP channel?\n"
288  " 'no' -- Never send DF (Don't Fragment) frames\n"
289  " 'maybe' -- Use per-route hints\n"
290  " 'yes' -- Always DF (Don't Fragment)\n"
291  "--mtu-test : Empirically measure and report MTU.\n"
292 #ifdef ENABLE_FRAGMENT
293  "--fragment max : Enable internal datagram fragmentation so that no UDP\n"
294  " datagrams are sent which are larger than max bytes.\n"
295  " Adds 4 bytes of overhead per datagram.\n"
296 #endif
297  "--mssfix [n] : Set upper bound on TCP MSS, default = tun-mtu size\n"
298  " or --fragment max value, whichever is lower.\n"
299  "--sndbuf size : Set the TCP/UDP send buffer size.\n"
300  "--rcvbuf size : Set the TCP/UDP receive buffer size.\n"
301 #if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK
302  "--mark value : Mark encrypted packets being sent with value. The mark value\n"
303  " can be matched in policy routing and packetfilter rules.\n"
304  "--bind-dev dev : Bind to the given device when making connection to a peer or\n"
305  " listening for connections. This allows sending encrypted packets\n"
306  " via a VRF present on the system.\n"
307 #endif
308  "--txqueuelen n : Set the tun/tap TX queue length to n (Linux only).\n"
309 #ifdef ENABLE_MEMSTATS
310  "--memstats file : Write live usage stats to memory mapped binary file.\n"
311 #endif
312  "--mlock : Disable Paging -- ensures key material and tunnel\n"
313  " data will never be written to disk.\n"
314  "--up cmd : Run command cmd after successful tun device open.\n"
315  " Execute as: cmd tun/tap-dev tun-mtu link-mtu \\\n"
316  " ifconfig-local-ip ifconfig-remote-ip\n"
317  " (pre --user or --group UID/GID change)\n"
318  "--up-delay : Delay tun/tap open and possible --up script execution\n"
319  " until after TCP/UDP connection establishment with peer.\n"
320  "--down cmd : Run command cmd after tun device close.\n"
321  " (post --user/--group UID/GID change and/or --chroot)\n"
322  " (command parameters are same as --up option)\n"
323  "--down-pre : Run --down command before TUN/TAP close.\n"
324  "--up-restart : Run up/down commands for all restarts including those\n"
325  " caused by --ping-restart or SIGUSR1\n"
326  "--user user : Set UID to user after initialization.\n"
327  "--group group : Set GID to group after initialization.\n"
328  "--chroot dir : Chroot to this directory after initialization.\n"
329 #ifdef ENABLE_SELINUX
330  "--setcon context: Apply this SELinux context after initialization.\n"
331 #endif
332  "--cd dir : Change to this directory before initialization.\n"
333  "--daemon [name] : Become a daemon after initialization.\n"
334  " The optional 'name' parameter will be passed\n"
335  " as the program name to the system logger.\n"
336  "--syslog [name] : Output to syslog, but do not become a daemon.\n"
337  " See --daemon above for a description of the 'name' parm.\n"
338  "--log file : Output log to file which is created/truncated on open.\n"
339  "--log-append file : Append log to file, or create file if nonexistent.\n"
340  "--suppress-timestamps : Don't log timestamps to stdout/stderr.\n"
341  "--machine-readable-output : Always log timestamp, message flags to stdout/stderr.\n"
342  "--writepid file : Write main process ID to file.\n"
343  "--nice n : Change process priority (>0 = lower, <0 = higher).\n"
344  "--echo [parms ...] : Echo parameters to log output.\n"
345  "--verb n : Set output verbosity to n (default=%d):\n"
346  " (Level 3 is recommended if you want a good summary\n"
347  " of what's happening without being swamped by output).\n"
348  " : 0 -- no output except fatal errors\n"
349  " : 1 -- startup info + connection initiated messages +\n"
350  " non-fatal encryption & net errors\n"
351  " : 2,3 -- show TLS negotiations & route info\n"
352  " : 4 -- show parameters\n"
353  " : 5 -- show 'RrWw' chars on console for each packet sent\n"
354  " and received from TCP/UDP (caps) or tun/tap (lc)\n"
355  " : 6 to 11 -- debug messages of increasing verbosity\n"
356  "--mute n : Log at most n consecutive messages in the same category.\n"
357  "--status file [n] : Write operational status to file every n seconds.\n"
358  "--status-version [n] : Choose the status file format version number.\n"
359  " Currently, n can be 1, 2, or 3 (default=1).\n"
360  "--disable-occ : (DEPRECATED) Disable options consistency check between peers.\n"
361 #ifdef ENABLE_DEBUG
362  "--gremlin mask : Special stress testing mode (for debugging only).\n"
363 #endif
364 #if defined(USE_COMP)
365  "--compress alg : Use compression algorithm alg\n"
366  "--allow-compression: Specify whether compression should be allowed\n"
367 #if defined(ENABLE_LZO)
368  "--comp-lzo : Use LZO compression -- may add up to 1 byte per\n"
369  " packet for incompressible data.\n"
370  "--comp-noadapt : Don't use adaptive compression when --comp-lzo\n"
371  " is specified.\n"
372 #endif
373 #endif
374 #ifdef ENABLE_MANAGEMENT
375  "--management ip port [pass] : Enable a TCP server on ip:port to handle\n"
376  " management functions. pass is a password file\n"
377  " or 'stdin' to prompt from console.\n"
378 #if UNIX_SOCK_SUPPORT
379  " To listen on a unix domain socket, specific the pathname\n"
380  " in place of ip and use 'unix' as the port number.\n"
381 #endif
382  "--management-client : Management interface will connect as a TCP client to\n"
383  " ip/port rather than listen as a TCP server.\n"
384  "--management-query-passwords : Query management channel for private key\n"
385  " and auth-user-pass passwords.\n"
386  "--management-query-proxy : Query management channel for proxy information.\n"
387  "--management-query-remote : Query management channel for --remote directive.\n"
388  "--management-hold : Start " PACKAGE_NAME " in a hibernating state, until a client\n"
389  " of the management interface explicitly starts it.\n"
390  "--management-signal : Issue SIGUSR1 when management disconnect event occurs.\n"
391  "--management-forget-disconnect : Forget passwords when management disconnect\n"
392  " event occurs.\n"
393  "--management-up-down : Report tunnel up/down events to management interface.\n"
394  "--management-log-cache n : Cache n lines of log file history for usage\n"
395  " by the management channel.\n"
396 #if UNIX_SOCK_SUPPORT
397  "--management-client-user u : When management interface is a unix socket, only\n"
398  " allow connections from user u.\n"
399  "--management-client-group g : When management interface is a unix socket, only\n"
400  " allow connections from group g.\n"
401 #endif
402  "--management-client-auth : gives management interface client the responsibility\n"
403  " to authenticate clients after their client certificate\n"
404  " has been verified.\n"
405 #endif /* ifdef ENABLE_MANAGEMENT */
406 #ifdef ENABLE_PLUGIN
407  "--plugin m [str]: Load plug-in module m passing str as an argument\n"
408  " to its initialization function.\n"
409 #endif
410  "--vlan-tagging : Enable 802.1Q-based VLAN tagging.\n"
411  "--vlan-accept tagged|untagged|all : Set VLAN tagging mode. Default is 'all'.\n"
412  "--vlan-pvid v : Sets the Port VLAN Identifier. Defaults to 1.\n"
413  "\n"
414  "Multi-Client Server options (when --mode server is used):\n"
415  "--server network netmask : Helper option to easily configure server mode.\n"
416  "--server-ipv6 network/bits : Configure IPv6 server mode.\n"
417  "--server-bridge [IP netmask pool-start-IP pool-end-IP] : Helper option to\n"
418  " easily configure ethernet bridging server mode.\n"
419  "--push \"option\" : Push a config file option back to the peer for remote\n"
420  " execution. Peer must specify --pull in its config file.\n"
421  "--push-reset : Don't inherit global push list for specific\n"
422  " client instance.\n"
423  "--push-remove opt : Remove options matching 'opt' from the push list for\n"
424  " a specific client instance.\n"
425  "--ifconfig-pool start-IP end-IP [netmask] : Set aside a pool of subnets\n"
426  " to be dynamically allocated to connecting clients.\n"
427  "--ifconfig-pool-persist file [seconds] : Persist/unpersist ifconfig-pool\n"
428  " data to file, at seconds intervals (default=600).\n"
429  " If seconds=0, file will be treated as read-only.\n"
430  "--ifconfig-ipv6-pool base-IP/bits : set aside an IPv6 network block\n"
431  " to be dynamically allocated to connecting clients.\n"
432  "--ifconfig-push local remote-netmask : Push an ifconfig option to remote,\n"
433  " overrides --ifconfig-pool dynamic allocation.\n"
434  " Only valid in a client-specific config file.\n"
435  "--ifconfig-ipv6-push local/bits remote : Push an ifconfig-ipv6 option to\n"
436  " remote, overrides --ifconfig-ipv6-pool allocation.\n"
437  " Only valid in a client-specific config file.\n"
438  "--iroute network [netmask] : Route subnet to client.\n"
439  "--iroute-ipv6 network/bits : Route IPv6 subnet to client.\n"
440  " Sets up internal routes only.\n"
441  " Only valid in a client-specific config file.\n"
442  "--disable : Client is disabled.\n"
443  " Only valid in a client-specific config file.\n"
444  "--verify-client-cert [none|optional|require] : perform no, optional or\n"
445  " mandatory client certificate verification.\n"
446  " Default is to require the client to supply a certificate.\n"
447  "--username-as-common-name : For auth-user-pass authentication, use\n"
448  " the authenticated username as the common name,\n"
449  " rather than the common name from the client cert.\n"
450  "--auth-user-pass-verify cmd method: Query client for username/password and\n"
451  " run command cmd to verify. If method='via-env', pass\n"
452  " user/pass via environment, if method='via-file', pass\n"
453  " user/pass via temporary file.\n"
454  "--auth-gen-token [lifetime] Generate a random authentication token which is pushed\n"
455  " to each client, replacing the password. Useful when\n"
456  " OTP based two-factor auth mechanisms are in use and\n"
457  " --reneg-* options are enabled. Optionally a lifetime in seconds\n"
458  " for generated tokens can be set.\n"
459  "--opt-verify : (DEPRECATED) Clients that connect with options that are incompatible\n"
460  " with those of the server will be disconnected.\n"
461  "--auth-user-pass-optional : Allow connections by clients that don't\n"
462  " specify a username/password.\n"
463  "--no-name-remapping : (DEPRECATED) Allow Common Name and X509 Subject to include\n"
464  " any printable character.\n"
465  "--client-to-client : Internally route client-to-client traffic.\n"
466  "--duplicate-cn : Allow multiple clients with the same common name to\n"
467  " concurrently connect.\n"
468  "--client-connect cmd : Run command cmd on client connection.\n"
469  "--client-disconnect cmd : Run command cmd on client disconnection.\n"
470  "--client-config-dir dir : Directory for custom client config files.\n"
471  "--ccd-exclusive : Refuse connection unless custom client config is found.\n"
472  "--tmp-dir dir : Temporary directory, used for --client-connect return file and plugin communication.\n"
473  "--hash-size r v : Set the size of the real address hash table to r and the\n"
474  " virtual address table to v.\n"
475  "--bcast-buffers n : Allocate n broadcast buffers.\n"
476  "--tcp-queue-limit n : Maximum number of queued TCP output packets.\n"
477  "--tcp-nodelay : Macro that sets TCP_NODELAY socket flag on the server\n"
478  " as well as pushes it to connecting clients.\n"
479  "--learn-address cmd : Run command cmd to validate client virtual addresses.\n"
480  "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n"
481  "--connect-freq-initial n s : Allow a maximum of n replies for initial connections attempts per s seconds.\n"
482  "--max-clients n : Allow a maximum of n simultaneously connected clients.\n"
483  "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n"
484  "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n"
485  " older than n seconds. Run this check every t\n"
486  " seconds (defaults to n).\n"
487  "--explicit-exit-notify [n] : In UDP server mode send [RESTART] command on exit/restart to connected\n"
488  " clients. n = 1 - reconnect to same server,\n"
489  " 2 - advance to next server, default=1.\n"
490 #if PORT_SHARE
491  "--port-share host port [dir] : When run in TCP mode, proxy incoming HTTPS\n"
492  " sessions to a web server at host:port. dir specifies an\n"
493  " optional directory to write origin IP:port data.\n"
494 #endif
495  "\n"
496  "Client options (when connecting to a multi-client server):\n"
497  "--client : Helper option to easily configure client mode.\n"
498  "--auth-user-pass [up] : Authenticate with server using username/password.\n"
499  " up is a file containing the username on the first line,\n"
500  " and a password on the second. If either the password or both\n"
501  " the username and the password are omitted OpenVPN will prompt\n"
502  " for them from console.\n"
503  "--pull : Accept certain config file options from the peer as if they\n"
504  " were part of the local config file. Must be specified\n"
505  " when connecting to a '--mode server' remote host.\n"
506  "--pull-filter accept|ignore|reject t : Filter each option received from the\n"
507  " server if it starts with the text t. The action flag accept,\n"
508  " ignore or reject causes the option to be allowed, removed or\n"
509  " rejected with error. May be specified multiple times, and\n"
510  " each filter is applied in the order of appearance.\n"
511  "--dns server <n> <option> <value> [value ...] : Configure option for DNS server #n\n"
512  " Valid options are :\n"
513  " address <addr[:port]> [addr[:port] ...] : server addresses 4/6\n"
514  " resolve-domains <domain> [domain ...] : split domains\n"
515  " exclude-domains <domain> [domain ...] : domains not to resolve\n"
516  " dnssec <yes|no|optional> : option to use DNSSEC\n"
517  " type <DoH|DoT> : query server over HTTPS / TLS\n"
518  " sni <domain> : DNS server name indication\n"
519  "--dns search-domains <domain> [domain ...]:\n"
520  " Add domains to DNS domain search list\n"
521  "--auth-retry t : How to handle auth failures. Set t to\n"
522  " none (default), interact, or nointeract.\n"
523  "--static-challenge t e : Enable static challenge/response protocol using\n"
524  " challenge text t, with e indicating echo flag (0|1)\n"
525  "--connect-timeout n : when polling possible remote servers to connect to\n"
526  " in a round-robin fashion, spend no more than n seconds\n"
527  " waiting for a response before trying the next server.\n"
528  "--allow-recursive-routing : When this option is set, OpenVPN will not drop\n"
529  " incoming tun packets with same destination as host.\n"
530  "--explicit-exit-notify [n] : On exit/restart, send exit signal to\n"
531  " server/remote. n = # of retries, default=1.\n"
532  "\n"
533  "Data Channel Encryption Options (must be compatible between peers):\n"
534  "(These options are meaningful for both Static Key & TLS-mode)\n"
535  "--secret f [d] : (DEPRECATED) Enable Static Key encryption mode (non-TLS).\n"
536  " Use shared secret file f, generate with --genkey.\n"
537  " The optional d parameter controls key directionality.\n"
538  " If d is specified, use separate keys for each\n"
539  " direction, set d=0 on one side of the connection,\n"
540  " and d=1 on the other side.\n"
541  "--auth alg : Authenticate packets with HMAC using message\n"
542  " digest algorithm alg (default=%s).\n"
543  " (usually adds 16 or 20 bytes per packet)\n"
544  " Set alg=none to disable authentication.\n"
545  "--cipher alg : Encrypt packets with cipher algorithm alg.\n"
546  " You should usually use --data-ciphers instead.\n"
547  " Set alg=none to disable encryption.\n"
548  "--data-ciphers list : List of ciphers that are allowed to be negotiated.\n"
549 #ifndef ENABLE_CRYPTO_MBEDTLS
550  "--engine [name] : Enable OpenSSL hardware crypto engine functionality.\n"
551 #endif
552  "--no-replay : (DEPRECATED) Disable replay protection.\n"
553  "--mute-replay-warnings : Silence the output of replay warnings to log file.\n"
554  "--replay-window n [t] : Use a replay protection sliding window of size n\n"
555  " and a time window of t seconds.\n"
556  " Default n=%d t=%d\n"
557  "--replay-persist file : Persist replay-protection state across sessions\n"
558  " using file.\n"
559  "--test-crypto : Run a self-test of crypto features enabled.\n"
560  " For debugging only.\n"
561 #ifdef ENABLE_PREDICTION_RESISTANCE
562  "--use-prediction-resistance: Enable prediction resistance on the random\n"
563  " number generator.\n"
564 #endif
565  "\n"
566  "TLS Key Negotiation Options:\n"
567  "(These options are meaningful only for TLS-mode)\n"
568  "--tls-server : Enable TLS and assume server role during TLS handshake.\n"
569  "--tls-client : Enable TLS and assume client role during TLS handshake.\n"
570  "--ca file : Certificate authority file in .pem format containing\n"
571  " root certificate.\n"
572 #ifndef ENABLE_CRYPTO_MBEDTLS
573  "--capath dir : A directory of trusted certificates (CAs"
574  " and CRLs).\n"
575 #endif /* ENABLE_CRYPTO_MBEDTLS */
576  "--dh file : File containing Diffie Hellman parameters\n"
577  " in .pem format (for --tls-server only).\n"
578  " Use \"openssl dhparam -out dh1024.pem 1024\" to generate.\n"
579  "--cert file : Local certificate in .pem format -- must be signed\n"
580  " by a Certificate Authority in --ca file.\n"
581  "--extra-certs file : one or more PEM certs that complete the cert chain.\n"
582  "--key file : Local private key in .pem format.\n"
583  "--tls-version-min <version> ['or-highest'] : sets the minimum TLS version we\n"
584  " will accept from the peer. If version is unrecognized and 'or-highest'\n"
585  " is specified, require max TLS version supported by SSL implementation.\n"
586  "--tls-version-max <version> : sets the maximum TLS version we will use.\n"
587 #ifndef ENABLE_CRYPTO_MBEDTLS
588  "--pkcs12 file : PKCS#12 file containing local private key, local certificate\n"
589  " and optionally the root CA certificate.\n"
590 #endif
591 #ifdef ENABLE_X509ALTUSERNAME
592  "--x509-username-field : Field in x509 certificate containing the username.\n"
593  " Default is CN in the Subject field.\n"
594 #endif
595  "--verify-hash hash [algo] : Specify fingerprint for level-1 certificate.\n"
596  " Valid algo flags are SHA1 and SHA256. \n"
597 #ifdef _WIN32
598  "--cryptoapicert select-string : Load the certificate and private key from the\n"
599  " Windows Certificate System Store.\n"
600 #endif
601  "--tls-cipher l : A list l of allowable TLS ciphers separated by : (optional).\n"
602  "--tls-ciphersuites l: A list of allowed TLS 1.3 cipher suites seperated by : (optional)\n"
603  " : Use --show-tls to see a list of supported TLS ciphers (suites).\n"
604  "--tls-cert-profile p : Set the allowed certificate crypto algorithm profile\n"
605  " (default=legacy).\n"
606  "--providers l : A list l of OpenSSL providers to load.\n"
607  "--tls-timeout n : Packet retransmit timeout on TLS control channel\n"
608  " if no ACK from remote within n seconds (default=%d).\n"
609  "--reneg-bytes n : Renegotiate data chan. key after n bytes sent and recvd.\n"
610  "--reneg-pkts n : Renegotiate data chan. key after n packets sent and recvd.\n"
611  "--reneg-sec max [min] : Renegotiate data chan. key after at most max (default=%d)\n"
612  " and at least min (defaults to 90%% of max on servers and equal\n"
613  " to max on clients).\n"
614  "--hand-window n : Data channel key exchange must finalize within n seconds\n"
615  " of handshake initiation by any peer (default=%d).\n"
616  "--tran-window n : Transition window -- old key can live this many seconds\n"
617  " after new key renegotiation begins (default=%d).\n"
618  "--single-session: Allow only one session (reset state on restart).\n"
619  "--tls-exit : Exit on TLS negotiation failure.\n"
620  "--tls-auth f [d]: Add an additional layer of authentication on top of the TLS\n"
621  " control channel to protect against attacks on the TLS stack\n"
622  " and DoS attacks.\n"
623  " f (required) is a shared-secret key file.\n"
624  " The optional d parameter controls key directionality,\n"
625  " see --secret option for more info.\n"
626  "--tls-crypt key : Add an additional layer of authenticated encryption on top\n"
627  " of the TLS control channel to hide the TLS certificate,\n"
628  " provide basic post-quantum security and protect against\n"
629  " attacks on the TLS stack and DoS attacks.\n"
630  " key (required) provides the pre-shared key file.\n"
631  " see --secret option for more info.\n"
632  "--tls-crypt-v2 key : For clients: use key as a client-specific tls-crypt key.\n"
633  " For servers: use key to decrypt client-specific keys. For\n"
634  " key generation (--genkey tls-crypt-v2-client): use key to\n"
635  " encrypt generated client-specific key. (See --tls-crypt.)\n"
636  "--genkey tls-crypt-v2-client [keyfile] [base64 metadata]: Generate a\n"
637  " fresh tls-crypt-v2 client key, and store to\n"
638  " keyfile. If supplied, include metadata in wrapped key.\n"
639  "--genkey tls-crypt-v2-server [keyfile] [base64 metadata]: Generate a\n"
640  " fresh tls-crypt-v2 server key, and store to keyfile\n"
641  "--tls-crypt-v2-verify cmd : Run command cmd to verify the metadata of the\n"
642  " client-supplied tls-crypt-v2 client key\n"
643  "--askpass [file]: Get PEM password from controlling tty before we daemonize.\n"
644  "--auth-nocache : Don't cache --askpass or --auth-user-pass passwords.\n"
645  "--crl-verify crl ['dir']: Check peer certificate against a CRL.\n"
646  "--tls-verify cmd: Run command cmd to verify the X509 name of a\n"
647  " pending TLS connection that has otherwise passed all other\n"
648  " tests of certification. cmd should return 0 to allow\n"
649  " TLS handshake to proceed, or 1 to fail. (cmd is\n"
650  " executed as 'cmd certificate_depth subject')\n"
651  "--tls-export-cert [directory] : Get peer cert in PEM format and store it \n"
652  " in an openvpn temporary file in [directory]. Peer cert is \n"
653  " stored before tls-verify script execution and deleted after.\n"
654  "--verify-x509-name name: Accept connections only from a host with X509 subject\n"
655  " DN name. The remote host must also pass all other tests\n"
656  " of verification.\n"
657  "--ns-cert-type t: (DEPRECATED) Require that peer certificate was signed with \n"
658  " an explicit nsCertType designation t = 'client' | 'server'.\n"
659  "--x509-track x : Save peer X509 attribute x in environment for use by\n"
660  " plugins and management interface.\n"
661 #ifdef HAVE_EXPORT_KEYING_MATERIAL
662  "--keying-material-exporter label len : Save Exported Keying Material (RFC5705)\n"
663  " of len bytes (min. 16 bytes) using label in environment for use by plugins.\n"
664 #endif
665  "--remote-cert-ku v ... : Require that the peer certificate was signed with\n"
666  " explicit key usage, you can specify more than one value.\n"
667  " value should be given in hex format.\n"
668  "--remote-cert-eku oid : Require that the peer certificate was signed with\n"
669  " explicit extended key usage. Extended key usage can be encoded\n"
670  " as an object identifier or OpenSSL string representation.\n"
671  "--remote-cert-tls t: Require that peer certificate was signed with explicit\n"
672  " key usage and extended key usage based on RFC3280 TLS rules.\n"
673  " t = 'client' | 'server'.\n"
674 #ifdef ENABLE_PKCS11
675  "\n"
676  "PKCS#11 Options:\n"
677  "--pkcs11-providers provider ... : PKCS#11 provider to load.\n"
678  "--pkcs11-protected-authentication [0|1] ... : Use PKCS#11 protected authentication\n"
679  " path. Set for each provider.\n"
680  "--pkcs11-private-mode hex ... : PKCS#11 private key mode mask.\n"
681  " 0 : Try to determine automatically (default).\n"
682  " 1 : Use Sign.\n"
683  " 2 : Use SignRecover.\n"
684  " 4 : Use Decrypt.\n"
685  " 8 : Use Unwrap.\n"
686  "--pkcs11-cert-private [0|1] ... : Set if login should be performed before\n"
687  " certificate can be accessed. Set for each provider.\n"
688  "--pkcs11-pin-cache seconds : Number of seconds to cache PIN. The default is -1\n"
689  " cache until token is removed.\n"
690  "--pkcs11-id-management : Acquire identity from management interface.\n"
691  "--pkcs11-id serialized-id 'id' : Identity to use, get using standalone --show-pkcs11-ids\n"
692 #endif /* ENABLE_PKCS11 */
693  "\n"
694  "SSL Library information:\n"
695  "--show-ciphers : Show cipher algorithms to use with --cipher option.\n"
696  "--show-digests : Show message digest algorithms to use with --auth option.\n"
697  "--show-engines : Show hardware crypto accelerator engines (if available).\n"
698  "--show-tls : Show all TLS ciphers (TLS used only as a control channel).\n"
699 #ifdef _WIN32
700  "\n"
701  "Windows Specific:\n"
702  "--win-sys path : Pathname of Windows system directory. Default is the pathname\n"
703  " from SystemRoot environment variable.\n"
704  "--ip-win32 method : When using --ifconfig on Windows, set TAP-Windows adapter\n"
705  " IP address using method = manual, netsh, ipapi,\n"
706  " dynamic, or adaptive (default = adaptive).\n"
707  " Dynamic method allows two optional parameters:\n"
708  " offset: DHCP server address offset (> -256 and < 256).\n"
709  " If 0, use network address, if >0, take nth\n"
710  " address forward from network address, if <0,\n"
711  " take nth address backward from broadcast\n"
712  " address.\n"
713  " Default is 0.\n"
714  " lease-time: Lease time in seconds.\n"
715  " Default is one year.\n"
716  "--route-method : Which method to use for adding routes on Windows?\n"
717  " adaptive (default) -- Try ipapi then fall back to exe.\n"
718  " ipapi -- Use IP helper API.\n"
719  " exe -- Call the route.exe shell command.\n"
720  "--dhcp-option type [parm] : Set extended TAP-Windows properties, must\n"
721  " be used with --ip-win32 dynamic. For options\n"
722  " which allow multiple addresses,\n"
723  " --dhcp-option must be repeated.\n"
724  " DOMAIN name : Set DNS suffix\n"
725  " DOMAIN-SEARCH entry : Add entry to DNS domain search list\n"
726  " DNS addr : Set domain name server address(es) (IPv4 and IPv6)\n"
727  " NTP : Set NTP server address(es)\n"
728  " NBDD : Set NBDD server address(es)\n"
729  " WINS addr : Set WINS server address(es)\n"
730  " NBT type : Set NetBIOS over TCP/IP Node type\n"
731  " 1: B, 2: P, 4: M, 8: H\n"
732  " NBS id : Set NetBIOS scope ID\n"
733  " DISABLE-NBT : Disable Netbios-over-TCP/IP.\n"
734  "--dhcp-renew : Ask Windows to renew the TAP adapter lease on startup.\n"
735  "--dhcp-pre-release : Ask Windows to release the previous TAP adapter lease on\n"
736  " startup.\n"
737  "--register-dns : Run ipconfig /flushdns and ipconfig /registerdns\n"
738  " on connection initiation.\n"
739  "--tap-sleep n : Sleep for n seconds after TAP adapter open before\n"
740  " attempting to set adapter properties.\n"
741  "--pause-exit : When run from a console window, pause before exiting.\n"
742  "--service ex [0|1] : For use when " PACKAGE_NAME " is being instantiated by a\n"
743  " service, and should not be used directly by end-users.\n"
744  " ex is the name of an event object which, when\n"
745  " signaled, will cause " PACKAGE_NAME " to exit. A second\n"
746  " optional parameter controls the initial state of ex.\n"
747  "--show-net-up : Show " PACKAGE_NAME "'s view of routing table and net adapter list\n"
748  " after TAP adapter is up and routes have been added.\n"
749  "--windows-driver : Which tun driver to use?\n"
750  " ovpn-dco (default)\n"
751  " tap-windows6\n"
752  " wintun\n"
753  "--block-outside-dns : Block DNS on other network adapters to prevent DNS leaks\n"
754  "Windows Standalone Options:\n"
755  "\n"
756  "--show-adapters : Show all TAP-Windows adapters.\n"
757  "--show-net : Show " PACKAGE_NAME "'s view of routing table and net adapter list.\n"
758  "--show-valid-subnets : Show valid subnets for --dev tun emulation.\n"
759  "--allow-nonadmin [TAP-adapter] : Allow " PACKAGE_NAME " running without admin privileges\n"
760  " to access TAP adapter.\n"
761 #endif /* ifdef _WIN32 */
762  "\n"
763  "Generate a new key :\n"
764  "--genkey secret file : Generate a new random key of type and write to file\n"
765  " (for use with --secret, --tls-auth or --tls-crypt)."
766 #ifdef ENABLE_FEATURE_TUN_PERSIST
767  "\n"
768  "Tun/tap config mode (available with linux 2.4+):\n"
769  "--mktun : Create a persistent tunnel.\n"
770  "--rmtun : Remove a persistent tunnel.\n"
771  "--dev tunX|tapX : tun/tap device\n"
772  "--dev-type dt : Device type. See tunnel options above for details.\n"
773  "--user user : User to set privilege to.\n"
774  "--group group : Group to set privilege to.\n"
775 #endif
776 #ifdef ENABLE_PKCS11
777  "\n"
778  "PKCS#11 standalone options:\n"
779 #ifdef DEFAULT_PKCS11_MODULE
780  "--show-pkcs11-ids [provider] [cert_private] : Show PKCS#11 available ids.\n"
781 #else
782  "--show-pkcs11-ids provider [cert_private] : Show PKCS#11 available ids.\n"
783 #endif
784  " --verb option can be added *BEFORE* this.\n"
785 #endif /* ENABLE_PKCS11 */
786  "\n"
787  "General Standalone Options:\n"
788 #ifdef ENABLE_DEBUG
789  "--show-gateway : Show info about default gateway.\n"
790 #endif
791 ;
792 
793 #endif /* !ENABLE_SMALL */
794 
795 /*
796  * This is where the options defaults go.
797  * Any option not explicitly set here
798  * will be set to 0.
799  */
800 void
801 init_options(struct options *o, const bool init_gc)
802 {
803  CLEAR(*o);
804  if (init_gc)
805  {
806  gc_init(&o->gc);
807  gc_init(&o->dns_options.gc);
808  o->gc_owned = true;
809  }
811  o->topology = TOP_NET30;
812  o->ce.proto = PROTO_UDP;
813  o->ce.af = AF_UNSPEC;
814  o->ce.bind_ipv6_only = false;
815  o->ce.connect_retry_seconds = 1;
816  o->ce.connect_retry_seconds_max = 300;
817  o->ce.connect_timeout = 120;
818  o->connect_retry_max = 0;
820  o->verbosity = 1;
821  o->status_file_update_freq = 60;
822  o->status_file_version = 1;
823  o->ce.bind_local = true;
825  o->ce.occ_mtu = 0;
828  o->ce.mtu_discover_type = -1;
829  o->ce.mssfix = 0;
830  o->ce.mssfix_default = true;
831  o->ce.mssfix_encap = true;
832  o->route_delay_window = 30;
834  o->resolve_in_advance = false;
835  o->proto_force = -1;
836  o->occ = true;
837 #ifdef ENABLE_MANAGEMENT
841 #endif
842 #ifdef ENABLE_FEATURE_TUN_PERSIST
843  o->persist_mode = 1;
844 #endif
845 #ifdef _WIN32
846 #if 0
848 #else
850 #endif
851  o->tuntap_options.dhcp_lease_time = 31536000; /* one year */
852  o->tuntap_options.dhcp_masq_offset = 0; /* use network address as internal DHCP server address */
854  o->block_outside_dns = false;
856 #endif
857  o->vlan_accept = VLAN_ALL;
858  o->vlan_pvid = 1;
859  o->real_hash_size = 256;
860  o->virtual_hash_size = 256;
861  o->n_bcast_buf = 256;
862  o->tcp_queue_limit = 64;
863  o->max_clients = 1024;
864  o->cf_initial_per = 10;
865  o->cf_initial_max = 100;
866  o->max_routes_per_client = 256;
870  o->authname = "SHA1";
871  o->replay = true;
875 #ifdef ENABLE_PREDICTION_RESISTANCE
876  o->use_prediction_resistance = false;
877 #endif
878  o->tls_timeout = 2;
879  o->renegotiate_bytes = -1;
880  o->renegotiate_seconds = 3600;
881  o->renegotiate_seconds_min = -1;
882  o->handshake_window = 60;
883  o->transition_window = 3600;
884  o->tls_cert_profile = NULL;
885  o->ecdh_curve = NULL;
886 #ifdef ENABLE_X509ALTUSERNAME
887  o->x509_username_field[0] = X509_USERNAME_FIELD_DEFAULT;
888 #endif
889 #ifdef ENABLE_PKCS11
890  o->pkcs11_pin_cache_period = -1;
891 #endif /* ENABLE_PKCS11 */
892 
893 /* P2MP server context features */
894  o->auth_token_generate = false;
895 
896  /* Set default --tmp-dir */
897 #ifdef _WIN32
898  /* On Windows, find temp dir via environment variables */
899  o->tmp_dir = win_get_tempdir();
900 #else
901  /* Non-windows platforms use $TMPDIR, and if not set, default to '/tmp' */
902  o->tmp_dir = getenv("TMPDIR");
903  if (!o->tmp_dir)
904  {
905  o->tmp_dir = "/tmp";
906  }
907 #endif /* _WIN32 */
908  o->allow_recursive_routing = false;
909 
910 #ifndef ENABLE_DCO
911  o->tuntap_options.disable_dco = true;
912 #endif /* ENABLE_DCO */
913 }
914 
915 void
917 {
918  if (o->connection_list)
919  {
920  CLEAR(*o->connection_list);
921  }
922  if (o->remote_list)
923  {
924  CLEAR(*o->remote_list);
925  }
926  if (o->gc_owned)
927  {
928  gc_free(&o->gc);
929  gc_free(&o->dns_options.gc);
930  }
931 }
932 
934 {
935 #define PUF_TYPE_UNDEF 0
936 #define PUF_TYPE_ACCEPT 1
937 #define PUF_TYPE_IGNORE 2
938 #define PUF_TYPE_REJECT 3
939  int type;
940  int size;
941  char *pattern;
942  struct pull_filter *next;
943 };
944 
946 {
947  struct pull_filter *head;
948  struct pull_filter *tail;
949 };
950 
951 #ifndef ENABLE_SMALL
952 
953 static const char *
955 {
956  if (type == PUF_TYPE_ACCEPT)
957  {
958  return "accept";
959  }
960  if (type == PUF_TYPE_IGNORE)
961  {
962  return "ignore";
963  }
964  if (type == PUF_TYPE_REJECT)
965  {
966  return "reject";
967  }
968  else
969  {
970  return "???";
971  }
972 }
973 
974 #define SHOW_PARM(name, value, format) msg(D_SHOW_PARMS, " " #name " = " format, (value))
975 #define SHOW_STR(var) SHOW_PARM(var, (o->var ? o->var : "[UNDEF]"), "'%s'")
976 #define SHOW_STR_INLINE(var) SHOW_PARM(var, \
977  o->var ## _inline ? "[INLINE]" : \
978  (o->var ? o->var : "[UNDEF]"), \
979  "'%s'")
980 #define SHOW_INT(var) SHOW_PARM(var, o->var, "%d")
981 #define SHOW_UINT(var) SHOW_PARM(var, o->var, "%u")
982 #define SHOW_INT64(var) SHOW_PARM(var, o->var, "%" PRIi64)
983 #define SHOW_UNSIGNED(var) SHOW_PARM(var, o->var, "0x%08x")
984 #define SHOW_BOOL(var) SHOW_PARM(var, (o->var ? "ENABLED" : "DISABLED"), "%s");
985 
986 #endif /* ifndef ENABLE_SMALL */
987 
988 static void
990  const struct connection_entry *e,
991  const int i)
992 {
993  setenv_str_i(es, "proto", proto2ascii(e->proto, e->af, false), i);
994  setenv_str_i(es, "local", e->local, i);
995  setenv_str_i(es, "local_port", e->local_port, i);
996  setenv_str_i(es, "remote", e->remote, i);
997  setenv_str_i(es, "remote_port", e->remote_port, i);
998 
999  if (e->http_proxy_options)
1000  {
1001  setenv_str_i(es, "http_proxy_server", e->http_proxy_options->server, i);
1002  setenv_str_i(es, "http_proxy_port", e->http_proxy_options->port, i);
1003  }
1004  if (e->socks_proxy_server)
1005  {
1006  setenv_str_i(es, "socks_proxy_server", e->socks_proxy_server, i);
1007  setenv_str_i(es, "socks_proxy_port", e->socks_proxy_port, i);
1008  }
1009 }
1010 
1011 void
1012 setenv_settings(struct env_set *es, const struct options *o)
1013 {
1014  setenv_str(es, "config", o->config);
1015  setenv_int(es, "verb", o->verbosity);
1016  setenv_int(es, "daemon", o->daemon);
1017  setenv_int(es, "daemon_log_redirect", o->log);
1018  setenv_long_long(es, "daemon_start_time", time(NULL));
1019  setenv_int(es, "daemon_pid", platform_getpid());
1020 
1021  if (o->connection_list)
1022  {
1023  int i;
1024  for (i = 0; i < o->connection_list->len; ++i)
1025  {
1027  }
1028  }
1029  else
1030  {
1031  setenv_connection_entry(es, &o->ce, 1);
1032  }
1033 
1034  if (!o->pull)
1035  {
1037  }
1038 }
1039 
1040 #ifndef _WIN32
1041 static void
1042 setenv_foreign_option(struct options *o, const char *argv[], int len, struct env_set *es)
1043 {
1044  if (len > 0)
1045  {
1046  struct gc_arena gc = gc_new();
1047  struct buffer name = alloc_buf_gc(OPTION_PARM_SIZE, &gc);
1048  struct buffer value = alloc_buf_gc(OPTION_PARM_SIZE, &gc);
1049  int i;
1050  bool first = true;
1051  bool good = true;
1052 
1053  good &= buf_printf(&name, "foreign_option_%d", o->foreign_option_index + 1);
1054  ++o->foreign_option_index;
1055  for (i = 0; i < len; ++i)
1056  {
1057  if (argv[i])
1058  {
1059  if (!first)
1060  {
1061  good &= buf_printf(&value, " ");
1062  }
1063  good &= buf_printf(&value, "%s", argv[i]);
1064  first = false;
1065  }
1066  }
1067  if (good)
1068  {
1069  setenv_str(es, BSTR(&name), BSTR(&value));
1070  }
1071  else
1072  {
1073  msg(M_WARN, "foreign_option: name/value overflow");
1074  }
1075  gc_free(&gc);
1076  }
1077 }
1078 #endif /* ifndef _WIN32 */
1079 
1080 static in_addr_t
1081 get_ip_addr(const char *ip_string, int msglevel, bool *error)
1082 {
1083  unsigned int flags = GETADDR_HOST_ORDER;
1084  bool succeeded = false;
1085  in_addr_t ret;
1086 
1087  if (msglevel & M_FATAL)
1088  {
1089  flags |= GETADDR_FATAL;
1090  }
1091 
1092  ret = getaddr(flags, ip_string, 0, &succeeded, NULL);
1093  if (!succeeded && error)
1094  {
1095  *error = true;
1096  }
1097  return ret;
1098 }
1099 
1105 static char *
1106 get_ipv6_addr_no_netbits(const char *addr, struct gc_arena *gc)
1107 {
1108  const char *end = strchr(addr, '/');
1109  char *ret = NULL;
1110  if (NULL == end)
1111  {
1112  ret = string_alloc(addr, gc);
1113  }
1114  else
1115  {
1116  size_t len = end - addr;
1117  ret = gc_malloc(len + 1, true, gc);
1118  memcpy(ret, addr, len);
1119  }
1120  return ret;
1121 }
1122 
1123 static bool
1124 ipv6_addr_safe_hexplusbits( const char *ipv6_prefix_spec )
1125 {
1126  struct in6_addr t_addr;
1127  unsigned int t_bits;
1128 
1129  return get_ipv6_addr( ipv6_prefix_spec, &t_addr, &t_bits, M_WARN );
1130 }
1131 
1132 static char *
1133 string_substitute(const char *src, int from, int to, struct gc_arena *gc)
1134 {
1135  char *ret = (char *) gc_malloc(strlen(src) + 1, true, gc);
1136  char *dest = ret;
1137  char c;
1138 
1139  do
1140  {
1141  c = *src++;
1142  if (c == from)
1143  {
1144  c = to;
1145  }
1146  *dest++ = c;
1147  }
1148  while (c);
1149  return ret;
1150 }
1151 
1161 static struct verify_hash_list *
1162 parse_hash_fingerprint(const char *str, int nbytes, int msglevel, struct gc_arena *gc)
1163 {
1164  int i = 0;
1165  const char *cp = str;
1166 
1167  struct verify_hash_list *ret;
1168  ALLOC_OBJ_CLEAR_GC(ret, struct verify_hash_list, gc);
1169 
1170  char term = 0;
1171  unsigned int byte;
1172 
1173  while (*cp && i < nbytes)
1174  {
1175  /* valid segments consist of exactly two hex digits, then ':' or EOS */
1176  if (!isxdigit(cp[0])
1177  || !isxdigit(cp[1])
1178  || (cp[2] != ':' && cp[2] != '\0')
1179  || sscanf(cp, "%x", &byte) != 1)
1180  {
1181  msg(msglevel, "format error in hash fingerprint: %s", str);
1182  break;
1183  }
1184 
1185  ret->hash[i++] = (uint8_t)byte;
1186 
1187  term = cp[2];
1188  if (term == '\0')
1189  {
1190  break;
1191  }
1192  cp += 3;
1193  }
1194  if (i < nbytes)
1195  {
1196  msg(msglevel, "hash fingerprint is wrong length - expected %d bytes, got %d: %s", nbytes, i, str);
1197  }
1198  else if (term != '\0')
1199  {
1200  msg(msglevel, "hash fingerprint too long - expected only %d bytes: %s", nbytes, str);
1201  }
1202  return ret;
1203 }
1204 
1215 static struct verify_hash_list *
1216 parse_hash_fingerprint_multiline(const char *str, int nbytes, int msglevel,
1217  struct gc_arena *gc)
1218 {
1219  struct gc_arena gc_temp = gc_new();
1220  char *lines = string_alloc(str, &gc_temp);
1221 
1222  struct verify_hash_list *ret = NULL;
1223 
1224  const char *line;
1225  while ((line = strsep(&lines, "\n")))
1226  {
1227  /* ignore leading whitespace */
1228  while (isspace(*line))
1229  {
1230  line++;
1231  }
1232  /* skip empty lines and comment lines */
1233  if (strlen(line) == 0 || *line == '#' || *line == ';')
1234  {
1235  continue;
1236  }
1237 
1238  struct verify_hash_list *hash = parse_hash_fingerprint(line, nbytes,
1239  msglevel, gc);
1240 
1241  if (!hash)
1242  {
1243  gc_free(&gc_temp);
1244  return NULL;
1245  }
1246 
1247  hash->next = ret;
1248  ret = hash;
1249  }
1250  gc_free(&gc_temp);
1251 
1252  return ret;
1253 }
1254 #ifdef _WIN32
1255 
1256 #ifndef ENABLE_SMALL
1257 
1258 static void
1259 show_dhcp_option_list(const char *name, const char *const *array, int len)
1260 {
1261  int i;
1262  for (i = 0; i < len; ++i)
1263  {
1264  msg(D_SHOW_PARMS, " %s[%d] = %s", name, i, array[i] );
1265  }
1266 }
1267 
1268 static void
1269 show_dhcp_option_addrs(const char *name, const in_addr_t *array, int len)
1270 {
1271  struct gc_arena gc = gc_new();
1272  int i;
1273  for (i = 0; i < len; ++i)
1274  {
1275  msg(D_SHOW_PARMS, " %s[%d] = %s",
1276  name,
1277  i,
1278  print_in_addr_t(array[i], 0, &gc));
1279  }
1280  gc_free(&gc);
1281 }
1282 
1283 static void
1285 {
1286  SHOW_BOOL(ip_win32_defined);
1287  SHOW_INT(ip_win32_type);
1288  SHOW_INT(dhcp_masq_offset);
1289  SHOW_INT(dhcp_lease_time);
1290  SHOW_INT(tap_sleep);
1291  SHOW_UNSIGNED(dhcp_options);
1293  SHOW_BOOL(dhcp_pre_release);
1294  SHOW_STR(domain);
1295  SHOW_STR(netbios_scope);
1296  SHOW_INT(netbios_node_type);
1297  SHOW_BOOL(disable_nbt);
1298 
1299  show_dhcp_option_addrs("DNS", o->dns, o->dns_len);
1300  show_dhcp_option_addrs("WINS", o->wins, o->wins_len);
1301  show_dhcp_option_addrs("NTP", o->ntp, o->ntp_len);
1302  show_dhcp_option_addrs("NBDD", o->nbdd, o->nbdd_len);
1304 }
1305 
1306 #endif /* ifndef ENABLE_SMALL */
1307 #endif /* ifdef _WIN32 */
1308 
1309 #if defined(_WIN32) || defined(TARGET_ANDROID)
1310 static void
1311 dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, int *len, int msglevel)
1312 {
1313  struct in6_addr addr;
1314  if (*len >= N_DHCP_ADDR)
1315  {
1316  msg(msglevel, "--dhcp-option DNS: maximum of %d IPv6 dns servers can be specified",
1317  N_DHCP_ADDR);
1318  }
1319  else if (get_ipv6_addr(parm, &addr, NULL, msglevel))
1320  {
1321  dns6_list[(*len)++] = addr;
1322  }
1323 }
1324 static void
1325 dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, int *len, int msglevel)
1326 {
1327  if (*len >= N_DHCP_ADDR)
1328  {
1329  msg(msglevel, "--dhcp-option %s: maximum of %d %s servers can be specified",
1330  name,
1331  N_DHCP_ADDR,
1332  name);
1333  }
1334  else
1335  {
1336  if (ip_addr_dotted_quad_safe(parm)) /* FQDN -- IP address only */
1337  {
1338  bool error = false;
1339  const in_addr_t addr = get_ip_addr(parm, msglevel, &error);
1340  if (!error)
1341  {
1342  array[(*len)++] = addr;
1343  }
1344  }
1345  else
1346  {
1347  msg(msglevel, "dhcp-option parameter %s '%s' must be an IP address", name, parm);
1348  }
1349  }
1350 }
1351 
1352 /*
1353  * If DNS options are set use these for TUN/TAP options as well.
1354  * Applies to DNS, DNS6 and DOMAIN-SEARCH.
1355  * Existing options will be discarded.
1356  */
1357 static void
1359 {
1360  struct tuntap_options *tt = &o->tuntap_options;
1361  struct dns_options *dns = &o->dns_options;
1362 
1363  if (dns->search_domains)
1364  {
1365  tt->domain_search_list_len = 0;
1366  const struct dns_domain *domain = dns->search_domains;
1367  while (domain && tt->domain_search_list_len < N_SEARCH_LIST_LEN)
1368  {
1369  tt->domain_search_list[tt->domain_search_list_len++] = domain->name;
1370  domain = domain->next;
1371  }
1372  if (domain)
1373  {
1374  msg(M_WARN, "WARNING: couldn't copy all --dns search-domains to --dhcp-option");
1375  }
1376  }
1377 
1378  if (dns->servers)
1379  {
1380  tt->dns_len = 0;
1381  tt->dns6_len = 0;
1382  bool overflow = false;
1383  const struct dns_server *server = dns->servers;
1384  while (server)
1385  {
1386  for (int i = 0; i < server->addr_count; ++i)
1387  {
1388  if (server->addr[i].family == AF_INET)
1389  {
1390  if (tt->dns_len >= N_DHCP_ADDR)
1391  {
1392  overflow = true;
1393  continue;
1394  }
1395  tt->dns[tt->dns_len++] = server->addr[i].in.a4.s_addr;
1396  }
1397  else
1398  {
1399  if (tt->dns6_len >= N_DHCP_ADDR)
1400  {
1401  overflow = true;
1402  continue;
1403  }
1404  tt->dns6[tt->dns6_len++] = server->addr[i].in.a6;
1405  }
1406  }
1407  server = server->next;
1408  }
1409  if (overflow)
1410  {
1411  msg(M_WARN, "WARNING: couldn't copy all --dns server addresses to --dhcp-option");
1412  }
1413  }
1414 }
1415 #else /* if defined(_WIN32) || defined(TARGET_ANDROID) */
1416 static void
1417 foreign_options_copy_dns(struct options *o, struct env_set *es)
1418 {
1419  const struct dns_domain *domain = o->dns_options.search_domains;
1420  const struct dns_server *server = o->dns_options.servers;
1421  if (!domain && !server)
1422  {
1423  return;
1424  }
1425 
1426  /* reset the index since we're starting all over again */
1427  int opt_max = o->foreign_option_index;
1428  o->foreign_option_index = 0;
1429 
1430  for (int i = 1; i <= opt_max; ++i)
1431  {
1432  char name[32];
1433  openvpn_snprintf(name, sizeof(name), "foreign_option_%d", i);
1434 
1435  const char *env_str = env_set_get(es, name);
1436  const char *value = strchr(env_str, '=') + 1;
1437  if ((domain && strstr(value, "dhcp-option DOMAIN-SEARCH") == value)
1438  || (server && strstr(value, "dhcp-option DNS") == value))
1439  {
1440  setenv_del(es, name);
1441  }
1442  else
1443  {
1444  setenv_foreign_option(o, &value, 1, es);
1445  }
1446  }
1447 
1448  struct gc_arena gc = gc_new();
1449 
1450  while (server)
1451  {
1452  for (int i = 0; i < server->addr_count; ++i)
1453  {
1454  if (server->addr[i].family == AF_INET)
1455  {
1456  const char *argv[] = {
1457  "dhcp-option",
1458  "DNS",
1459  print_in_addr_t(server->addr[i].in.a4.s_addr, 0, &gc)
1460  };
1461  setenv_foreign_option(o, argv, 3, es);
1462  }
1463  else
1464  {
1465  const char *argv[] = {
1466  "dhcp-option",
1467  "DNS6",
1468  print_in6_addr(server->addr[i].in.a6, 0, &gc)
1469  };
1470  setenv_foreign_option(o, argv, 3, es);
1471  }
1472  }
1473  server = server->next;
1474  }
1475  while (domain)
1476  {
1477  const char *argv[] = { "dhcp-option", "DOMAIN-SEARCH", domain->name };
1478  setenv_foreign_option(o, argv, 3, es);
1479  domain = domain->next;
1480  }
1481 
1482  gc_free(&gc);
1483 
1484  /* remove old leftover entries */
1485  while (o->foreign_option_index < opt_max)
1486  {
1487  char name[32];
1488  openvpn_snprintf(name, sizeof(name), "foreign_option_%d", opt_max--);
1489  setenv_del(es, name);
1490  }
1491 }
1492 #endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */
1493 
1494 #ifndef ENABLE_SMALL
1495 static const char *
1497 {
1498  switch (mode)
1499  {
1500  case VLAN_ONLY_TAGGED:
1501  return "tagged";
1502 
1504  return "untagged";
1505 
1506  case VLAN_ALL:
1507  return "all";
1508  }
1509  return NULL;
1510 }
1511 
1512 static void
1513 show_p2mp_parms(const struct options *o)
1514 {
1515  struct gc_arena gc = gc_new();
1516 
1517  msg(D_SHOW_PARMS, " server_network = %s", print_in_addr_t(o->server_network, 0, &gc));
1518  msg(D_SHOW_PARMS, " server_netmask = %s", print_in_addr_t(o->server_netmask, 0, &gc));
1519  msg(D_SHOW_PARMS, " server_network_ipv6 = %s", print_in6_addr(o->server_network_ipv6, 0, &gc) );
1520  SHOW_INT(server_netbits_ipv6);
1521  msg(D_SHOW_PARMS, " server_bridge_ip = %s", print_in_addr_t(o->server_bridge_ip, 0, &gc));
1522  msg(D_SHOW_PARMS, " server_bridge_netmask = %s", print_in_addr_t(o->server_bridge_netmask, 0, &gc));
1523  msg(D_SHOW_PARMS, " server_bridge_pool_start = %s", print_in_addr_t(o->server_bridge_pool_start, 0, &gc));
1524  msg(D_SHOW_PARMS, " server_bridge_pool_end = %s", print_in_addr_t(o->server_bridge_pool_end, 0, &gc));
1525  if (o->push_list.head)
1526  {
1527  const struct push_entry *e = o->push_list.head;
1528  while (e)
1529  {
1530  if (e->enable)
1531  {
1532  msg(D_SHOW_PARMS, " push_entry = '%s'", e->option);
1533  }
1534  e = e->next;
1535  }
1536  }
1537  SHOW_BOOL(ifconfig_pool_defined);
1538  msg(D_SHOW_PARMS, " ifconfig_pool_start = %s", print_in_addr_t(o->ifconfig_pool_start, 0, &gc));
1539  msg(D_SHOW_PARMS, " ifconfig_pool_end = %s", print_in_addr_t(o->ifconfig_pool_end, 0, &gc));
1540  msg(D_SHOW_PARMS, " ifconfig_pool_netmask = %s", print_in_addr_t(o->ifconfig_pool_netmask, 0, &gc));
1541  SHOW_STR(ifconfig_pool_persist_filename);
1542  SHOW_INT(ifconfig_pool_persist_refresh_freq);
1543  SHOW_BOOL(ifconfig_ipv6_pool_defined);
1544  msg(D_SHOW_PARMS, " ifconfig_ipv6_pool_base = %s", print_in6_addr(o->ifconfig_ipv6_pool_base, 0, &gc));
1545  SHOW_INT(ifconfig_ipv6_pool_netbits);
1546  SHOW_INT(n_bcast_buf);
1547  SHOW_INT(tcp_queue_limit);
1548  SHOW_INT(real_hash_size);
1549  SHOW_INT(virtual_hash_size);
1550  SHOW_STR(client_connect_script);
1552  SHOW_STR(client_disconnect_script);
1553  SHOW_STR(client_crresponse_script);
1554  SHOW_STR(client_config_dir);
1555  SHOW_BOOL(ccd_exclusive);
1556  SHOW_STR(tmp_dir);
1557  SHOW_BOOL(push_ifconfig_defined);
1558  msg(D_SHOW_PARMS, " push_ifconfig_local = %s", print_in_addr_t(o->push_ifconfig_local, 0, &gc));
1559  msg(D_SHOW_PARMS, " push_ifconfig_remote_netmask = %s", print_in_addr_t(o->push_ifconfig_remote_netmask, 0, &gc));
1560  SHOW_BOOL(push_ifconfig_ipv6_defined);
1561  msg(D_SHOW_PARMS, " push_ifconfig_ipv6_local = %s/%d", print_in6_addr(o->push_ifconfig_ipv6_local, 0, &gc), o->push_ifconfig_ipv6_netbits );
1562  msg(D_SHOW_PARMS, " push_ifconfig_ipv6_remote = %s", print_in6_addr(o->push_ifconfig_ipv6_remote, 0, &gc));
1563  SHOW_BOOL(enable_c2c);
1564  SHOW_BOOL(duplicate_cn);
1565  SHOW_INT(cf_max);
1566  SHOW_INT(cf_per);
1567  SHOW_INT(cf_initial_max);
1568  SHOW_INT(cf_initial_per);
1569  SHOW_INT(max_clients);
1570  SHOW_INT(max_routes_per_client);
1571  SHOW_STR(auth_user_pass_verify_script);
1572  SHOW_BOOL(auth_user_pass_verify_script_via_file);
1573  SHOW_BOOL(auth_token_generate);
1574  SHOW_INT(auth_token_lifetime);
1575  SHOW_STR_INLINE(auth_token_secret_file);
1576 #if PORT_SHARE
1577  SHOW_STR(port_share_host);
1578  SHOW_STR(port_share_port);
1579 #endif
1580  SHOW_BOOL(vlan_tagging);
1581  msg(D_SHOW_PARMS, " vlan_accept = %s", print_vlan_accept(o->vlan_accept));
1582  SHOW_INT(vlan_pvid);
1583 
1584  SHOW_BOOL(client);
1585  SHOW_BOOL(pull);
1586  SHOW_STR_INLINE(auth_user_pass_file);
1587 
1588  gc_free(&gc);
1589 }
1590 
1591 #endif /* ! ENABLE_SMALL */
1592 
1593 static void
1595  const char *network_str,
1596  const char *netmask_str,
1597  int msglevel)
1598 {
1599  struct iroute *ir;
1600 
1601  ALLOC_OBJ_GC(ir, struct iroute, &o->gc);
1602  ir->network = getaddr(GETADDR_HOST_ORDER, network_str, 0, NULL, NULL);
1603  ir->netbits = 32; /* host route if no netmask given */
1604 
1605  if (netmask_str)
1606  {
1607  const in_addr_t netmask = getaddr(GETADDR_HOST_ORDER, netmask_str, 0, NULL, NULL);
1608  ir->netbits = netmask_to_netbits2(netmask);
1609 
1610  if (ir->netbits < 0)
1611  {
1612  msg(msglevel, "in --iroute %s %s : Bad network/subnet specification",
1613  network_str,
1614  netmask_str);
1615  return;
1616  }
1617  }
1618 
1619  ir->next = o->iroutes;
1620  o->iroutes = ir;
1621 }
1622 
1623 static void
1625  const char *prefix_str,
1626  int msglevel)
1627 {
1628  struct iroute_ipv6 *ir;
1629 
1630  ALLOC_OBJ_GC(ir, struct iroute_ipv6, &o->gc);
1631 
1632  if (!get_ipv6_addr(prefix_str, &ir->network, &ir->netbits, msglevel ))
1633  {
1634  msg(msglevel, "in --iroute-ipv6 %s: Bad IPv6 prefix specification",
1635  prefix_str);
1636  return;
1637  }
1638 
1639  ir->next = o->iroutes_ipv6;
1640  o->iroutes_ipv6 = ir;
1641 }
1642 
1643 #ifndef ENABLE_SMALL
1644 static void
1646 {
1647  int i;
1648  msg(D_SHOW_PARMS, "BEGIN http_proxy");
1649  SHOW_STR(server);
1650  SHOW_STR(port);
1651  SHOW_STR(auth_method_string);
1652  SHOW_STR(auth_file);
1653  SHOW_STR(http_version);
1654  SHOW_STR(user_agent);
1655  for (i = 0; i < MAX_CUSTOM_HTTP_HEADER && o->custom_headers[i].name; i++)
1656  {
1657  if (o->custom_headers[i].content)
1658  {
1659  msg(D_SHOW_PARMS, " custom_header[%d] = %s: %s", i,
1660  o->custom_headers[i].name, o->custom_headers[i].content);
1661  }
1662  else
1663  {
1664  msg(D_SHOW_PARMS, " custom_header[%d] = %s", i,
1665  o->custom_headers[i].name);
1666  }
1667  }
1668  msg(D_SHOW_PARMS, "END http_proxy");
1669 }
1670 #endif /* ifndef ENABLE_SMALL */
1671 
1672 void
1674 {
1675  gc_detach(&o->gc);
1676  o->routes = NULL;
1677  o->client_nat = NULL;
1678  clone_push_list(o);
1679 }
1680 
1681 void
1683 {
1684  if (!options->routes)
1685  {
1687  }
1688 }
1689 
1690 static void
1692 {
1693  if (!options->routes_ipv6)
1694  {
1696  }
1697 }
1698 
1699 static void
1701 {
1702  if (!options->client_nat)
1703  {
1705  }
1706 }
1707 
1708 #ifndef ENABLE_SMALL
1709 static void
1711 {
1712  msg(D_SHOW_PARMS, " proto = %s", proto2ascii(o->proto, o->af, false));
1713  SHOW_STR(local);
1714  SHOW_STR(local_port);
1715  SHOW_STR(remote);
1716  SHOW_STR(remote_port);
1717  SHOW_BOOL(remote_float);
1718  SHOW_BOOL(bind_defined);
1720  SHOW_BOOL(bind_ipv6_only);
1721  SHOW_INT(connect_retry_seconds);
1722  SHOW_INT(connect_timeout);
1723 
1724  if (o->http_proxy_options)
1725  {
1727  }
1728  SHOW_STR(socks_proxy_server);
1729  SHOW_STR(socks_proxy_port);
1730  SHOW_INT(tun_mtu);
1731  SHOW_BOOL(tun_mtu_defined);
1732  SHOW_INT(link_mtu);
1733  SHOW_BOOL(link_mtu_defined);
1734  SHOW_INT(tun_mtu_extra);
1735  SHOW_BOOL(tun_mtu_extra_defined);
1736  SHOW_INT(tls_mtu);
1737 
1738  SHOW_INT(mtu_discover_type);
1739 
1740 #ifdef ENABLE_FRAGMENT
1741  SHOW_INT(fragment);
1742 #endif
1743  SHOW_INT(mssfix);
1744  SHOW_BOOL(mssfix_encap);
1745  SHOW_BOOL(mssfix_fixed);
1746 
1747  SHOW_INT(explicit_exit_notification);
1748 
1749  SHOW_STR_INLINE(tls_auth_file);
1750  SHOW_PARM(key_direction, keydirection2ascii(o->key_direction, false, true),
1751  "%s");
1752  SHOW_STR_INLINE(tls_crypt_file);
1753  SHOW_STR_INLINE(tls_crypt_v2_file);
1754 }
1755 
1756 
1757 static void
1759 {
1760  if (o->connection_list)
1761  {
1762  const struct connection_list *l = o->connection_list;
1763  int i;
1764  for (i = 0; i < l->len; ++i)
1765  {
1766  msg(D_SHOW_PARMS, "Connection profiles [%d]:", i);
1768  }
1769  }
1770  else
1771  {
1772  msg(D_SHOW_PARMS, "Connection profiles [default]:");
1774  }
1775  msg(D_SHOW_PARMS, "Connection profiles END");
1776 }
1777 
1778 static void
1780 {
1781  struct pull_filter *f;
1782  if (!l)
1783  {
1784  return;
1785  }
1786 
1787  msg(D_SHOW_PARMS, " Pull filters:");
1788  for (f = l->head; f; f = f->next)
1789  {
1790  msg(D_SHOW_PARMS, " %s \"%s\"", pull_filter_type_name(f->type), f->pattern);
1791  }
1792 }
1793 
1794 #endif /* ifndef ENABLE_SMALL */
1795 
1796 void
1797 show_settings(const struct options *o)
1798 {
1799 #ifndef ENABLE_SMALL
1800  msg(D_SHOW_PARMS, "Current Parameter Settings:");
1801 
1802  SHOW_STR(config);
1803 
1804  SHOW_INT(mode);
1805 
1806 #ifdef ENABLE_FEATURE_TUN_PERSIST
1807  SHOW_BOOL(persist_config);
1808  SHOW_INT(persist_mode);
1809 #endif
1810 
1811  SHOW_BOOL(show_ciphers);
1812  SHOW_BOOL(show_digests);
1813  SHOW_BOOL(show_engines);
1814  SHOW_BOOL(genkey);
1815  SHOW_STR(genkey_filename);
1816  SHOW_STR(key_pass_file);
1817  SHOW_BOOL(show_tls_ciphers);
1818 
1819  SHOW_INT(connect_retry_max);
1821 
1822  SHOW_BOOL(remote_random);
1823 
1824  SHOW_STR(ipchange);
1825  SHOW_STR(dev);
1826  SHOW_STR(dev_type);
1827  SHOW_STR(dev_node);
1828 #if defined(ENABLE_DCO)
1830 #endif
1831  SHOW_STR(lladdr);
1832  SHOW_INT(topology);
1833  SHOW_STR(ifconfig_local);
1834  SHOW_STR(ifconfig_remote_netmask);
1835  SHOW_BOOL(ifconfig_noexec);
1836  SHOW_BOOL(ifconfig_nowarn);
1837  SHOW_STR(ifconfig_ipv6_local);
1838  SHOW_INT(ifconfig_ipv6_netbits);
1839  SHOW_STR(ifconfig_ipv6_remote);
1840 
1841  SHOW_INT(shaper);
1842  SHOW_INT(mtu_test);
1843 
1844  SHOW_BOOL(mlock);
1845 
1846  SHOW_INT(keepalive_ping);
1847  SHOW_INT(keepalive_timeout);
1848  SHOW_INT(inactivity_timeout);
1849  SHOW_INT(session_timeout);
1850  SHOW_INT64(inactivity_minimum_bytes);
1851  SHOW_INT(ping_send_timeout);
1852  SHOW_INT(ping_rec_timeout);
1853  SHOW_INT(ping_rec_timeout_action);
1854  SHOW_BOOL(ping_timer_remote);
1855  SHOW_INT(remap_sigusr1);
1856  SHOW_BOOL(persist_tun);
1857  SHOW_BOOL(persist_local_ip);
1858  SHOW_BOOL(persist_remote_ip);
1859  SHOW_BOOL(persist_key);
1860 
1861 #if PASSTOS_CAPABILITY
1862  SHOW_BOOL(passtos);
1863 #endif
1864 
1865  SHOW_INT(resolve_retry_seconds);
1866  SHOW_BOOL(resolve_in_advance);
1867 
1868  SHOW_STR(username);
1869  SHOW_STR(groupname);
1870  SHOW_STR(chroot_dir);
1871  SHOW_STR(cd_dir);
1872 #ifdef ENABLE_SELINUX
1873  SHOW_STR(selinux_context);
1874 #endif
1875  SHOW_STR(writepid);
1876  SHOW_STR(up_script);
1877  SHOW_STR(down_script);
1878  SHOW_BOOL(down_pre);
1879  SHOW_BOOL(up_restart);
1880  SHOW_BOOL(up_delay);
1881  SHOW_BOOL(daemon);
1882  SHOW_BOOL(log);
1885  SHOW_INT(nice);
1886  SHOW_INT(verbosity);
1887  SHOW_INT(mute);
1888 #ifdef ENABLE_DEBUG
1889  SHOW_INT(gremlin);
1890 #endif
1891  SHOW_STR(status_file);
1892  SHOW_INT(status_file_version);
1893  SHOW_INT(status_file_update_freq);
1894 
1895  SHOW_BOOL(occ);
1896  SHOW_INT(rcvbuf);
1897  SHOW_INT(sndbuf);
1898 #if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK
1899  SHOW_INT(mark);
1900 #endif
1901  SHOW_INT(sockflags);
1902 
1903  SHOW_BOOL(fast_io);
1904 
1905  SHOW_INT(comp.alg);
1906  SHOW_INT(comp.flags);
1907 
1908  SHOW_STR(route_script);
1909  SHOW_STR(route_default_gateway);
1910  SHOW_INT(route_default_metric);
1911  SHOW_BOOL(route_noexec);
1912  SHOW_INT(route_delay);
1913  SHOW_INT(route_delay_window);
1914  SHOW_BOOL(route_delay_defined);
1915  SHOW_BOOL(route_nopull);
1916  SHOW_BOOL(route_gateway_via_dhcp);
1917  SHOW_BOOL(allow_pull_fqdn);
1919 
1920  if (o->routes)
1921  {
1923  }
1924 
1925  if (o->client_nat)
1926  {
1928  }
1929 
1931 
1932 #ifdef ENABLE_MANAGEMENT
1933  SHOW_STR(management_addr);
1934  SHOW_STR(management_port);
1935  SHOW_STR(management_user_pass);
1936  SHOW_INT(management_log_history_cache);
1937  SHOW_INT(management_echo_buffer_size);
1938  SHOW_STR(management_client_user);
1939  SHOW_STR(management_client_group);
1940  SHOW_INT(management_flags);
1941 #endif
1942 #ifdef ENABLE_PLUGIN
1943  if (o->plugin_list)
1944  {
1946  }
1947 #endif
1948 
1949  SHOW_STR_INLINE(shared_secret_file);
1950  SHOW_PARM(key_direction, keydirection2ascii(o->key_direction, false, true), "%s");
1951  SHOW_STR(ciphername);
1952  SHOW_STR(ncp_ciphers);
1953  SHOW_STR(authname);
1954 #ifndef ENABLE_CRYPTO_MBEDTLS
1955  SHOW_BOOL(engine);
1956 #endif /* ENABLE_CRYPTO_MBEDTLS */
1957  SHOW_BOOL(replay);
1958  SHOW_BOOL(mute_replay_warnings);
1959  SHOW_INT(replay_window);
1960  SHOW_INT(replay_time);
1961  SHOW_STR(packet_id_file);
1963 #ifdef ENABLE_PREDICTION_RESISTANCE
1964  SHOW_BOOL(use_prediction_resistance);
1965 #endif
1966 
1967  SHOW_BOOL(tls_server);
1968  SHOW_BOOL(tls_client);
1969  SHOW_STR_INLINE(ca_file);
1970  SHOW_STR(ca_path);
1971  SHOW_STR_INLINE(dh_file);
1973  {
1974  SHOW_PARM("cert_file", "EXTERNAL_CERT", "%s");
1975  }
1976  else
1977  {
1978  SHOW_STR_INLINE(cert_file);
1979  }
1980  SHOW_STR_INLINE(extra_certs_file);
1981 
1982  if ((o->management_flags & MF_EXTERNAL_KEY))
1983  {
1984  SHOW_PARM("priv_key_file", "EXTERNAL_PRIVATE_KEY", "%s");
1985  }
1986  else
1987  {
1988  SHOW_STR_INLINE(priv_key_file);
1989  }
1990 #ifndef ENABLE_CRYPTO_MBEDTLS
1991  SHOW_STR_INLINE(pkcs12_file);
1992 #endif
1993 #ifdef ENABLE_CRYPTOAPI
1994  SHOW_STR(cryptoapi_cert);
1995 #endif
1996  SHOW_STR(cipher_list);
1997  SHOW_STR(cipher_list_tls13);
1998  SHOW_STR(tls_cert_profile);
2000  SHOW_STR(tls_export_cert);
2001  SHOW_INT(verify_x509_type);
2002  SHOW_STR(verify_x509_name);
2003  SHOW_STR_INLINE(crl_file);
2004  SHOW_INT(ns_cert_type);
2005  {
2006  int i;
2007  for (i = 0; i<MAX_PARMS; i++)
2008  {
2009  SHOW_INT(remote_cert_ku[i]);
2010  }
2011  }
2012  SHOW_STR(remote_cert_eku);
2013  if (o->verify_hash)
2014  {
2015  SHOW_INT(verify_hash_algo);
2016  SHOW_INT(verify_hash_depth);
2017  struct gc_arena gc = gc_new();
2018  struct verify_hash_list *hl = o->verify_hash;
2019  int digest_len = (o->verify_hash_algo == MD_SHA1) ? SHA_DIGEST_LENGTH :
2021  while (hl)
2022  {
2023  char *s = format_hex_ex(hl->hash, digest_len, 0,
2024  1, ":", &gc);
2025  SHOW_PARM(verify_hash, s, "%s");
2026  hl = hl->next;
2027  }
2028  gc_free(&gc);
2029  }
2030  SHOW_INT(ssl_flags);
2031 
2032  SHOW_INT(tls_timeout);
2033 
2034  SHOW_INT(renegotiate_bytes);
2035  SHOW_INT(renegotiate_packets);
2036  SHOW_INT(renegotiate_seconds);
2037 
2038  SHOW_INT(handshake_window);
2039  SHOW_INT(transition_window);
2040 
2041  SHOW_BOOL(single_session);
2043  SHOW_BOOL(tls_exit);
2044 
2045  SHOW_STR(tls_crypt_v2_metadata);
2046 
2047 #ifdef ENABLE_PKCS11
2048  {
2049  int i;
2050  for (i = 0; i<MAX_PARMS && o->pkcs11_providers[i] != NULL; i++)
2051  {
2052  SHOW_PARM(pkcs11_providers, o->pkcs11_providers[i], "%s");
2053  }
2054  }
2055  {
2056  int i;
2057  for (i = 0; i<MAX_PARMS; i++)
2058  {
2059  SHOW_PARM(pkcs11_protected_authentication, o->pkcs11_protected_authentication[i] ? "ENABLED" : "DISABLED", "%s");
2060  }
2061  }
2062  {
2063  int i;
2064  for (i = 0; i<MAX_PARMS; i++)
2065  {
2066  SHOW_PARM(pkcs11_private_mode, o->pkcs11_private_mode[i], "%08x");
2067  }
2068  }
2069  {
2070  int i;
2071  for (i = 0; i<MAX_PARMS; i++)
2072  {
2073  SHOW_PARM(pkcs11_cert_private, o->pkcs11_cert_private[i] ? "ENABLED" : "DISABLED", "%s");
2074  }
2075  }
2076  SHOW_INT(pkcs11_pin_cache_period);
2077  SHOW_STR(pkcs11_id);
2079 #endif /* ENABLE_PKCS11 */
2080 
2081  show_p2mp_parms(o);
2082 
2083 #ifdef _WIN32
2084  SHOW_BOOL(show_net_up);
2085  SHOW_INT(route_method);
2086  SHOW_BOOL(block_outside_dns);
2088 #endif
2089 #endif /* ifndef ENABLE_SMALL */
2090 }
2091 
2092 #undef SHOW_PARM
2093 #undef SHOW_STR
2094 #undef SHOW_INT
2095 #undef SHOW_BOOL
2096 
2097 #ifdef ENABLE_MANAGEMENT
2098 
2099 static struct http_proxy_options *
2101  const char *port,
2102  const char *flags,
2103  const int msglevel,
2104  struct gc_arena *gc)
2105 {
2106  if (server && port)
2107  {
2108  struct http_proxy_options *ho;
2109  ALLOC_OBJ_CLEAR_GC(ho, struct http_proxy_options, gc);
2110  ho->server = string_alloc(server, gc);
2111  ho->port = port;
2112  if (flags && !strcmp(flags, "nct"))
2113  {
2114  ho->auth_retry = PAR_NCT;
2115  }
2116  else
2117  {
2118  ho->auth_retry = PAR_ALL;
2119  }
2120  ho->http_version = "1.0";
2121  ho->user_agent = "OpenVPN-Autoproxy/1.0";
2122  return ho;
2123  }
2124  else
2125  {
2126  return NULL;
2127  }
2128 }
2129 
2130 static void
2132 {
2133  const struct connection_list *l = o->connection_list;
2134  int i;
2135  bool succeed = false;
2136  for (i = 0; i < l->len; ++i)
2137  {
2138  struct connection_entry *ce = l->array[i];
2139  if (ce->proto == PROTO_TCP_CLIENT || ce->proto == PROTO_TCP)
2140  {
2142  succeed = true;
2143  }
2144  }
2145  if (succeed)
2146  {
2147  for (i = 0; i < l->len; ++i)
2148  {
2149  struct connection_entry *ce = l->array[i];
2150  if (ce->proto == PROTO_UDP)
2151  {
2152  ce->flags |= CE_DISABLED;
2153  }
2154  }
2155  }
2156  else
2157  {
2158  msg(M_WARN, "Note: option http-proxy-override ignored because no TCP-based connection profiles are defined");
2159  }
2160 }
2161 
2162 #endif /* ifdef ENABLE_MANAGEMENT */
2163 
2164 static struct connection_list *
2166 {
2167  if (!options->connection_list)
2168  {
2170  }
2171  return options->connection_list;
2172 }
2173 
2174 static struct connection_entry *
2175 alloc_connection_entry(struct options *options, const int msglevel)
2176 {
2178  struct connection_entry *e;
2179 
2180  if (l->len == l->capacity)
2181  {
2182  int capacity = l->capacity + CONNECTION_LIST_SIZE;
2183  struct connection_entry **ce = gc_realloc(l->array, capacity*sizeof(struct connection_entry *), &options->gc);
2184  if (ce == NULL)
2185  {
2186  msg(msglevel, "Unable to process more connection options: out of memory. Number of entries = %d", l->len);
2187  return NULL;
2188  }
2189  l->array = ce;
2190  l->capacity = capacity;
2191  }
2192  ALLOC_OBJ_GC(e, struct connection_entry, &options->gc);
2193  l->array[l->len++] = e;
2194  return e;
2195 }
2196 
2197 static struct remote_list *
2199 {
2200  if (!options->remote_list)
2201  {
2203  }
2204  return options->remote_list;
2205 }
2206 
2207 static struct remote_entry *
2208 alloc_remote_entry(struct options *options, const int msglevel)
2209 {
2211  struct remote_entry *e;
2212 
2213  if (l->len == l->capacity)
2214  {
2215  int capacity = l->capacity + CONNECTION_LIST_SIZE;
2216  struct remote_entry **re = gc_realloc(l->array, capacity*sizeof(struct remote_entry *), &options->gc);
2217  if (re == NULL)
2218  {
2219  msg(msglevel, "Unable to process more remote options: out of memory. Number of entries = %d", l->len);
2220  return NULL;
2221  }
2222  l->array = re;
2223  l->capacity = capacity;
2224  }
2225  ALLOC_OBJ_GC(e, struct remote_entry, &options->gc);
2226  l->array[l->len++] = e;
2227  return e;
2228 }
2229 
2230 static struct pull_filter_list *
2232 {
2233  if (!o->pull_filter_list)
2234  {
2236  }
2237  return o->pull_filter_list;
2238 }
2239 
2240 static struct pull_filter *
2241 alloc_pull_filter(struct options *o, const int msglevel)
2242 {
2244  struct pull_filter *f;
2245 
2246  ALLOC_OBJ_CLEAR_GC(f, struct pull_filter, &o->gc);
2247  if (l->head)
2248  {
2249  ASSERT(l->tail);
2250  l->tail->next = f;
2251  }
2252  else
2253  {
2254  ASSERT(!l->tail);
2255  l->head = f;
2256  }
2257  l->tail = f;
2258  return f;
2259 }
2260 
2261 static void
2263 {
2264  if (re->remote)
2265  {
2266  ce->remote = re->remote;
2267  }
2268  if (re->remote_port)
2269  {
2270  ce->remote_port = re->remote_port;
2271  }
2272  if (re->proto >= 0)
2273  {
2274  ce->proto = re->proto;
2275  }
2276  if (re->af > 0)
2277  {
2278  ce->af = re->af;
2279  }
2280 }
2281 
2282 static void
2283 connection_entry_preload_key(const char **key_file, bool *key_inline,
2284  struct gc_arena *gc)
2285 {
2286  if (key_file && *key_file && !(*key_inline))
2287  {
2288  struct buffer in = buffer_read_from_file(*key_file, gc);
2289  if (!buf_valid(&in))
2290  {
2291  msg(M_FATAL, "Cannot pre-load keyfile (%s)", *key_file);
2292  }
2293 
2294  *key_file = (const char *) in.data;
2295  *key_inline = true;
2296  }
2297 }
2298 
2299 static void
2301 {
2303  || options->pkcs12_file
2304  || options->ca_file
2305 #ifndef ENABLE_CRYPTO_MBEDTLS
2306  || options->ca_path
2307 #endif
2308  )
2309  {
2310  return;
2311  }
2312 
2313  const char *const str = "You must define CA file (--ca)"
2314 #ifndef ENABLE_CRYPTO_MBEDTLS
2315  " or CA path (--capath)"
2316 #endif
2317  " and/or peer fingerprint verification (--peer-fingerprint)";
2318  msg(M_USAGE, "%s", str);
2319 }
2320 
2321 static void
2323  const struct connection_entry *ce)
2324 {
2325  struct options defaults;
2326  int dev = DEV_TYPE_UNDEF;
2327  bool pull = false;
2328 
2329  init_options(&defaults, true);
2330 
2331  if (options->test_crypto)
2332  {
2333  notnull(options->shared_secret_file, "key file (--secret)");
2334  }
2335  else
2336  {
2337  notnull(options->dev, "TUN/TAP device (--dev)");
2338  }
2339 
2340  /*
2341  * Get tun/tap/null device type
2342  */
2344 
2345  /*
2346  * If "proto tcp" is specified, make sure we know whether it is
2347  * tcp-client or tcp-server.
2348  */
2349  if (ce->proto == PROTO_TCP)
2350  {
2351  msg(M_USAGE,
2352  "--proto tcp is ambiguous in this context. Please specify "
2353  "--proto tcp-server or --proto tcp-client");
2354  }
2355 
2356  if (options->lladdr && dev != DEV_TYPE_TAP)
2357  {
2358  msg(M_USAGE, "--lladdr can only be used in --dev tap mode");
2359  }
2360 
2361  /*
2362  * Sanity check on MTU parameters
2363  */
2365  {
2366  msg(M_USAGE, "only one of --tun-mtu or --link-mtu may be defined");
2367  }
2368 
2369  if (!proto_is_udp(ce->proto) && options->mtu_test)
2370  {
2371  msg(M_USAGE, "--mtu-test only makes sense with --proto udp");
2372  }
2373 
2374  /* will we be pulling options from server? */
2375  pull = options->pull;
2376 
2377  /*
2378  * Sanity check on --local, --remote, and --ifconfig
2379  */
2380 
2381  if (proto_is_net(ce->proto)
2384  {
2385  msg(M_USAGE, "--remote and --local addresses are the same");
2386  }
2387 
2390  {
2391  msg(M_USAGE,
2392  "--local and --remote addresses must be distinct from --ifconfig "
2393  "addresses");
2394  }
2395 
2398  {
2399  msg(M_USAGE,
2400  "--local addresses must be distinct from --ifconfig addresses");
2401  }
2402 
2405  {
2406  msg(M_USAGE,
2407  "local and remote/netmask --ifconfig addresses must be different");
2408  }
2409 
2410  if (ce->bind_defined && !ce->bind_local)
2411  {
2412  msg(M_USAGE, "--bind and --nobind can't be used together");
2413  }
2414 
2415  if (ce->local && !ce->bind_local)
2416  {
2417  msg(M_USAGE,
2418  "--local and --nobind don't make sense when used together");
2419  }
2420 
2421  if (ce->local_port_defined && !ce->bind_local)
2422  {
2423  msg(M_USAGE,
2424  "--lport and --nobind don't make sense when used together");
2425  }
2426 
2427  if (!ce->remote && !ce->bind_local)
2428  {
2429  msg(M_USAGE, "--nobind doesn't make sense unless used with --remote");
2430  }
2431 
2432  /*
2433  * Check for consistency of management options
2434  */
2435 #ifdef ENABLE_MANAGEMENT
2436  if (!options->management_addr
2439  {
2440  msg(M_USAGE, "--management is not specified, however one or more options which modify the behavior of --management were specified");
2441  }
2442 
2445  {
2446  msg(M_USAGE, "--management-client-(user|group) can only be used on unix domain sockets");
2447  }
2448 
2452  {
2453  msg(M_WARN, "WARNING: Using --management on a TCP port WITHOUT "
2454  "passwords is STRONGLY discouraged and considered insecure");
2455  }
2456 
2457 #endif /* ifdef ENABLE_MANAGEMENT */
2458 
2459 #if !defined(HAVE_XKEY_PROVIDER)
2460  if ((tls_version_max() >= TLS_VER_1_3)
2463  )
2464  {
2465  msg(M_FATAL, "management-external-key with TLS 1.3 or later requires "
2466  "nopadding argument/support");
2467  }
2468 #endif
2469  /*
2470  * Windows-specific options.
2471  */
2472 
2473 #ifdef _WIN32
2475  {
2476  msg(M_USAGE, "On Windows, --ifconfig is required when --dev tun is used");
2477  }
2478 
2481  {
2482  msg(M_USAGE, "On Windows, --ip-win32 doesn't make sense unless --ifconfig is also used");
2483  }
2484 
2486  {
2487  const char *prefix = "Some dhcp-options require DHCP server";
2489  {
2490  msg(M_USAGE, "%s, which is not supported by selected %s driver",
2492  }
2495  {
2496  msg(M_USAGE, "%s, which requires --ip-win32 dynamic or adaptive",
2497  prefix);
2498  }
2499  }
2500 
2502  {
2503  msg(M_USAGE, "--windows-driver wintun requires --dev tun");
2504  }
2505 #endif /* ifdef _WIN32 */
2506 
2507  /*
2508  * Check that protocol options make sense.
2509  */
2510 
2511 #ifdef ENABLE_FRAGMENT
2512  if (!proto_is_udp(ce->proto) && ce->fragment)
2513  {
2514  msg(M_USAGE, "--fragment can only be used with --proto udp");
2515  }
2516 #endif
2517 
2518  if (!ce->remote && ce->proto == PROTO_TCP_CLIENT)
2519  {
2520  msg(M_USAGE, "--remote MUST be used in TCP Client mode");
2521  }
2522 
2524  {
2525  msg(M_USAGE,
2526  "--http-proxy MUST be used in TCP Client mode (i.e. --proto "
2527  "tcp-client)");
2528  }
2529 
2531  {
2532  msg(M_USAGE,
2533  "--http-proxy not specified but other http proxy options present");
2534  }
2535 
2537  {
2538  msg(M_USAGE,
2539  "--http-proxy can not be used together with --socks-proxy");
2540  }
2541 
2543  {
2544  msg(M_USAGE, "--socks-proxy can not be used in TCP Server mode");
2545  }
2546 
2547  if (ce->proto == PROTO_TCP_SERVER && (options->connection_list->len > 1))
2548  {
2549  msg(M_USAGE, "TCP server mode allows at most one --remote address");
2550  }
2551 
2552  /*
2553  * Check consistency of --mode server options.
2554  */
2555  if (options->mode == MODE_SERVER)
2556  {
2557 #define USAGE_VALID_SERVER_PROTOS "--mode server currently only supports " \
2558  "--proto values of udp, tcp-server, tcp4-server, or tcp6-server"
2559 #ifdef TARGET_ANDROID
2560  msg(M_FATAL, "--mode server not supported on Android");
2561 #endif
2562  if (!(dev == DEV_TYPE_TUN || dev == DEV_TYPE_TAP))
2563  {
2564  msg(M_USAGE, "--mode server only works with --dev tun or --dev tap");
2565  }
2566  if (options->pull)
2567  {
2568  msg(M_USAGE, "--pull cannot be used with --mode server");
2569  }
2571  {
2572  msg(M_WARN, "--pull-filter ignored for --mode server");
2573  }
2574  if (!(proto_is_udp(ce->proto) || ce->proto == PROTO_TCP_SERVER))
2575  {
2577  }
2578 #if PORT_SHARE
2579  if ((options->port_share_host || options->port_share_port)
2580  && (ce->proto != PROTO_TCP_SERVER))
2581  {
2582  msg(M_USAGE, "--port-share only works in TCP server mode "
2583  "(--proto values of tcp-server, tcp4-server, or tcp6-server)");
2584  }
2585 #endif
2586  if (!options->tls_server)
2587  {
2588  msg(M_USAGE, "--mode server requires --tls-server");
2589  }
2590  if (ce->remote)
2591  {
2592  msg(M_USAGE, "--remote cannot be used with --mode server");
2593  }
2594  if (!ce->bind_local)
2595  {
2596  msg(M_USAGE, "--nobind cannot be used with --mode server");
2597  }
2598  if (ce->http_proxy_options)
2599  {
2600  msg(M_USAGE, "--http-proxy cannot be used with --mode server");
2601  }
2602  if (ce->socks_proxy_server)
2603  {
2604  msg(M_USAGE, "--socks-proxy cannot be used with --mode server");
2605  }
2606  /* <connection> blocks force to have a remote embedded, so we check
2607  * for the --remote and bail out if it is present
2608  */
2609  if (options->connection_list->len >1
2611  {
2612  msg(M_USAGE, "<connection> cannot be used with --mode server");
2613  }
2614 
2615  if (options->shaper)
2616  {
2617  msg(M_USAGE, "--shaper cannot be used with --mode server");
2618  }
2619  if (options->ipchange)
2620  {
2621  msg(M_USAGE,
2622  "--ipchange cannot be used with --mode server (use "
2623  "--client-connect instead)");
2624  }
2625  if (!(proto_is_dgram(ce->proto) || ce->proto == PROTO_TCP_SERVER))
2626  {
2628  }
2629  if (!proto_is_udp(ce->proto) && (options->cf_max || options->cf_per))
2630  {
2631  msg(M_USAGE, "--connect-freq only works with --mode server --proto udp. Try --max-clients instead.");
2632  }
2634  {
2635  msg(M_USAGE, "The third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mode");
2636  }
2637  if (options->routes && (options->routes->flags & RG_ENABLE))
2638  {
2639  msg(M_USAGE, "--redirect-gateway cannot be used with --mode server (however --push \"redirect-gateway\" is fine)");
2640  }
2642  {
2643  msg(M_USAGE, "--route-delay cannot be used with --mode server");
2644  }
2645  if (options->up_delay)
2646  {
2647  msg(M_USAGE, "--up-delay cannot be used with --mode server");
2648  }
2652  {
2653  msg(M_USAGE,
2654  "--ifconfig-pool-persist must be used with --ifconfig-pool or --ifconfig-ipv6-pool");
2655  }
2657  {
2658  msg(M_USAGE, "--ifconfig-ipv6-pool needs --ifconfig-ipv6");
2659  }
2661  {
2662  msg(M_USAGE, "--allow-recursive-routing cannot be used with --mode server");
2663  }
2665  {
2666  msg(M_USAGE, "--auth-user-pass cannot be used with --mode server (it should be used on the client side only)");
2667  }
2669  {
2670  msg(M_USAGE, "--ccd-exclusive must be used with --client-config-dir");
2671  }
2673  {
2674  msg(M_USAGE, "--auth-gen-token needs a non-infinite "
2675  "--renegotiate_seconds setting");
2676  }
2679  {
2680  msg(M_USAGE, "--auth-gen-token renewal time needs to be at least "
2681  " two times --hand-window (%d).",
2683 
2684  }
2685  {
2686  const bool ccnr = (options->auth_user_pass_verify_script
2689  const char *postfix = "must be used with --management-client-auth, an --auth-user-pass-verify script, or plugin";
2691  {
2692  msg(M_USAGE, "--verify-client-cert none|optional %s", postfix);
2693  }
2695  {
2696  msg(M_USAGE, "--username-as-common-name %s", postfix);
2697  }
2699  {
2700  msg(M_USAGE, "--auth-user-pass-optional %s", postfix);
2701  }
2702  }
2703 
2704  if (options->vlan_tagging && dev != DEV_TYPE_TAP)
2705  {
2706  msg(M_USAGE, "--vlan-tagging must be used with --dev tap");
2707  }
2708  if (!options->vlan_tagging)
2709  {
2710  if (options->vlan_accept != defaults.vlan_accept)
2711  {
2712  msg(M_USAGE, "--vlan-accept requires --vlan-tagging");
2713  }
2714  if (options->vlan_pvid != defaults.vlan_pvid)
2715  {
2716  msg(M_USAGE, "--vlan-pvid requires --vlan-tagging");
2717  }
2718  }
2719  }
2720  else
2721  {
2722  /*
2723  * When not in server mode, err if parameters are
2724  * specified which require --mode server.
2725  */
2727  {
2728  msg(M_USAGE, "--ifconfig-pool/--ifconfig-pool-persist requires --mode server");
2729  }
2731  {
2732  msg(M_USAGE, "--ifconfig-ipv6-pool requires --mode server");
2733  }
2734  if (options->real_hash_size != defaults.real_hash_size
2736  {
2737  msg(M_USAGE, "--hash-size requires --mode server");
2738  }
2740  {
2741  msg(M_USAGE, "--learn-address requires --mode server");
2742  }
2744  {
2745  msg(M_USAGE, "--client-connect requires --mode server");
2746  }
2748  {
2749  msg(M_USAGE, "--client-crresponse requires --mode server");
2750  }
2752  {
2753  msg(M_USAGE, "--client-disconnect requires --mode server");
2754  }
2756  {
2757  msg(M_USAGE, "--client-config-dir/--ccd-exclusive requires --mode server");
2758  }
2759  if (options->enable_c2c)
2760  {
2761  msg(M_USAGE, "--client-to-client requires --mode server");
2762  }
2763  if (options->duplicate_cn)
2764  {
2765  msg(M_USAGE, "--duplicate-cn requires --mode server");
2766  }
2767  if (options->cf_max || options->cf_per)
2768  {
2769  msg(M_USAGE, "--connect-freq requires --mode server");
2770  }
2772  {
2773  msg(M_USAGE, "--verify-client-cert requires --mode server");
2774  }
2776  {
2777  msg(M_USAGE, "--username-as-common-name requires --mode server");
2778  }
2780  {
2781  msg(M_USAGE, "--auth-user-pass-optional requires --mode server");
2782  }
2784  {
2785  msg(M_USAGE, "--opt-verify requires --mode server");
2786  }
2788  {
2789  msg(M_WARN, "WARNING: setting tcp-nodelay on the client side will not "
2790  "affect the server. To have TCP_NODELAY in both direction use "
2791  "tcp-nodelay in the server configuration instead.");
2792  }
2794  {
2795  msg(M_USAGE, "--auth-user-pass-verify requires --mode server");
2796  }
2798  {
2799  msg(M_USAGE, "--auth-gen-token requires --mode server");
2800  }
2801 #if PORT_SHARE
2802  if (options->port_share_host || options->port_share_port)
2803  {
2804  msg(M_USAGE, "--port-share requires TCP server mode (--mode server --proto tcp-server)");
2805  }
2806 #endif
2807 
2809  {
2810  msg(M_USAGE, "--stale-routes-check requires --mode server");
2811  }
2812 
2813  if (options->vlan_tagging)
2814  {
2815  msg(M_USAGE, "--vlan-tagging requires --mode server");
2816  }
2817  }
2818 
2819  /*
2820  * Check consistency of replay options
2821  */
2822  if (!options->replay
2823  && (options->replay_window != defaults.replay_window
2824  || options->replay_time != defaults.replay_time))
2825  {
2826  msg(M_USAGE, "--replay-window doesn't make sense when replay protection is disabled with --no-replay");
2827  }
2828 
2829  /*
2830  * SSL/TLS mode sanity checks.
2831  */
2833  +(options->shared_secret_file != NULL) > 1)
2834  {
2835  msg(M_USAGE, "specify only one of --tls-server, --tls-client, or --secret");
2836  }
2837 
2838  if (!options->tls_server && !options->tls_client)
2839  {
2840  msg(M_INFO, "DEPRECATION: No tls-client or tls-server option in "
2841  "configuration detected. OpenVPN 2.7 will remove the "
2842  "functionality to run a VPN without TLS. "
2843  "See the examples section in the manual page for "
2844  "examples of a similar quick setup with peer-fingerprint.");
2845  }
2846 
2848  {
2849  msg(M_WARN, "WARNING: POTENTIALLY DANGEROUS OPTION "
2850  "--verify-client-cert none|optional "
2851  "may accept clients which do not present a certificate");
2852  }
2853 
2854  const int tls_version_max =
2857  const int tls_version_min =
2860 
2861  if (tls_version_max > 0 && tls_version_max < tls_version_min)
2862  {
2863  msg(M_USAGE, "--tls-version-min bigger than --tls-version-max");
2864  }
2865 
2867  {
2869 #ifdef ENABLE_PKCS11
2870  if (!options->pkcs11_providers[0] && options->pkcs11_id)
2871  {
2872  msg(M_WARN, "Option pkcs11-id is ignored as no pkcs11-providers are specified");
2873  }
2874  else if (!options->pkcs11_providers[0] && options->pkcs11_id_management)
2875  {
2876  msg(M_WARN, "Option pkcs11-id-management is ignored as no pkcs11-providers are specified");
2877  }
2878 
2879  if (options->pkcs11_providers[0])
2880  {
2881  if (options->pkcs11_id_management && options->pkcs11_id != NULL)
2882  {
2883  msg(M_USAGE, "Parameter --pkcs11-id cannot be used when --pkcs11-id-management is also specified.");
2884  }
2885  if (!options->pkcs11_id_management && options->pkcs11_id == NULL)
2886  {
2887  msg(M_USAGE, "Parameter --pkcs11-id or --pkcs11-id-management should be specified.");
2888  }
2889  if (options->cert_file)
2890  {
2891  msg(M_USAGE, "Parameter --cert cannot be used when --pkcs11-provider is also specified.");
2892  }
2893  if (options->priv_key_file)
2894  {
2895  msg(M_USAGE, "Parameter --key cannot be used when --pkcs11-provider is also specified.");
2896  }
2898  {
2899  msg(M_USAGE, "Parameter --management-external-key cannot be used when --pkcs11-provider is also specified.");
2900  }
2902  {
2903  msg(M_USAGE, "Parameter --management-external-cert cannot be used when --pkcs11-provider is also specified.");
2904  }
2905  if (options->pkcs12_file)
2906  {
2907  msg(M_USAGE, "Parameter --pkcs12 cannot be used when --pkcs11-provider is also specified.");
2908  }
2909 #ifdef ENABLE_CRYPTOAPI
2910  if (options->cryptoapi_cert)
2911  {
2912  msg(M_USAGE, "Parameter --cryptoapicert cannot be used when --pkcs11-provider is also specified.");
2913  }
2914 #endif
2915  }
2916  else
2917 #endif /* ifdef ENABLE_PKCS11 */
2919  {
2920  msg(M_USAGE, "--key and --management-external-key are mutually exclusive");
2921  }
2923  {
2924  if (options->cert_file)
2925  {
2926  msg(M_USAGE, "--cert and --management-external-cert are mutually exclusive");
2927  }
2928  else if (!(options->management_flags & MF_EXTERNAL_KEY))
2929  {
2930  msg(M_USAGE, "--management-external-cert must be used with --management-external-key");
2931  }
2932  }
2933  else
2934 #ifdef ENABLE_CRYPTOAPI
2935  if (options->cryptoapi_cert)
2936  {
2937  if (options->cert_file)
2938  {
2939  msg(M_USAGE, "Parameter --cert cannot be used when --cryptoapicert is also specified.");
2940  }
2941  if (options->priv_key_file)
2942  {
2943  msg(M_USAGE, "Parameter --key cannot be used when --cryptoapicert is also specified.");
2944  }
2945  if (options->pkcs12_file)
2946  {
2947  msg(M_USAGE, "Parameter --pkcs12 cannot be used when --cryptoapicert is also specified.");
2948  }
2950  {
2951  msg(M_USAGE, "Parameter --management-external-key cannot be used when --cryptoapicert is also specified.");
2952  }
2954  {
2955  msg(M_USAGE, "Parameter --management-external-cert cannot be used when --cryptoapicert is also specified.");
2956  }
2957  }
2958  else
2959 #endif /* ifdef ENABLE_CRYPTOAPI */
2960  if (options->pkcs12_file)
2961  {
2962 #ifdef ENABLE_CRYPTO_MBEDTLS
2963  msg(M_USAGE, "Parameter --pkcs12 cannot be used with the mbed TLS version version of OpenVPN.");
2964 #else
2965  if (options->ca_path)
2966  {
2967  msg(M_USAGE, "Parameter --capath cannot be used when --pkcs12 is also specified.");
2968  }
2969  if (options->cert_file)
2970  {
2971  msg(M_USAGE, "Parameter --cert cannot be used when --pkcs12 is also specified.");
2972  }
2973  if (options->priv_key_file)
2974  {
2975  msg(M_USAGE, "Parameter --key cannot be used when --pkcs12 is also specified.");
2976  }
2978  {
2979  msg(M_USAGE, "Parameter --management-external-key cannot be used when --pkcs12 is also specified.");
2980  }
2982  {
2983  msg(M_USAGE, "Parameter --management-external-cert cannot be used when --pkcs12 is also specified.");
2984  }
2985 #endif /* ifdef ENABLE_CRYPTO_MBEDTLS */
2986  }
2987  else
2988  {
2989 #ifdef ENABLE_CRYPTO_MBEDTLS
2990  if (options->ca_path)
2991  {
2992  msg(M_USAGE, "Parameter --capath cannot be used with the mbed TLS version version of OpenVPN.");
2993  }
2994 #endif /* ifdef ENABLE_CRYPTO_MBEDTLS */
2995  if (pull)
2996  {
2997 
2998  const int sum =
3001 
3002  if (sum == 0)
3003  {
3005  {
3006  msg(M_USAGE, "No client-side authentication method is "
3007  "specified. You must use either "
3008  "--cert/--key, --pkcs12, or "
3009  "--auth-user-pass");
3010  }
3011  }
3012  else if (sum != 2)
3013  {
3014  msg(M_USAGE, "If you use one of --cert or --key, you must use them both");
3015  }
3016  }
3017  else
3018  {
3020  {
3021  notnull(options->cert_file, "certificate file (--cert) or PKCS#12 file (--pkcs12)");
3022  }
3024  {
3025  notnull(options->priv_key_file, "private key file (--key) or PKCS#12 file (--pkcs12)");
3026  }
3027  }
3028  }
3029  if (ce->tls_auth_file && ce->tls_crypt_file)
3030  {
3031  msg(M_USAGE, "--tls-auth and --tls-crypt are mutually exclusive");
3032  }
3034  && (ce->tls_auth_file || ce->tls_crypt_file))
3035  {
3036  msg(M_USAGE, "--tls-crypt-v2, --tls-auth and --tls-crypt are mutually exclusive in client mode");
3037  }
3038  }
3039  else
3040  {
3041  /*
3042  * Make sure user doesn't specify any TLS options
3043  * when in non-TLS mode.
3044  */
3045 
3046 #define MUST_BE_UNDEF(parm) if (options->parm != defaults.parm) {msg(M_USAGE, err, #parm); \
3047 }
3048 
3049  const char err[] = "Parameter %s can only be specified in TLS-mode, i.e. where --tls-server or --tls-client is also specified.";
3050 
3056 #ifndef ENABLE_CRYPTO_MBEDTLS
3058 #endif
3081 #ifdef ENABLE_PKCS11
3082  MUST_BE_UNDEF(pkcs11_providers[0]);
3083  MUST_BE_UNDEF(pkcs11_private_mode[0]);
3084  MUST_BE_UNDEF(pkcs11_id);
3086 #endif
3087 
3088  if (pull)
3089  {
3090  msg(M_USAGE, err, "--pull");
3091  }
3092  }
3093 #undef MUST_BE_UNDEF
3094 
3096  {
3097  msg(M_USAGE, "--auth-user-pass requires --pull");
3098  }
3099 
3100  uninit_options(&defaults);
3101 }
3102 
3103 static void
3105 {
3106  const int dev = dev_type_enum(o->dev, o->dev_type);
3107 
3109  {
3110  if (ce->proto == PROTO_TCP)
3111  {
3113  }
3114  }
3115 
3116  if (o->client)
3117  {
3118  if (ce->proto == PROTO_TCP)
3119  {
3121  }
3122  }
3123 
3124  /* an option is present that requires local bind to enabled */
3125  bool need_bind = ce->local || ce->local_port_defined || ce->bind_defined;
3126 
3127  /* socks proxy is enabled */
3128  bool uses_socks = ce->proto == PROTO_UDP && ce->socks_proxy_server;
3129 
3130  /* If binding is not forced by an explicit option and we have (at least)
3131  * one of --tcp-client, --pull (or --client), or socks we do not bind
3132  * locally to have "normal" IP client behaviour of a random source port */
3133  if (!need_bind && (ce->proto == PROTO_TCP_CLIENT || uses_socks || o->pull))
3134  {
3135  ce->bind_local = false;
3136  }
3137 
3138  if (!ce->bind_local)
3139  {
3140  ce->local_port = NULL;
3141  }
3142 
3143  /* if protocol forcing is enabled, disable all protocols
3144  * except for the forced one
3145  */
3146  if (o->proto_force >= 0 && o->proto_force != ce->proto)
3147  {
3148  ce->flags |= CE_DISABLED;
3149  }
3150 
3151  /* our socks code is not fully IPv6 enabled yet (TCP works, UDP not)
3152  * so fall back to IPv4-only (trac #1221)
3153  */
3154  if (ce->socks_proxy_server && proto_is_udp(ce->proto) && ce->af != AF_INET)
3155  {
3156  if (ce->af == AF_INET6)
3157  {
3158  msg(M_INFO, "WARNING: '--proto udp6' is not compatible with "
3159  "'--socks-proxy' today. Forcing IPv4 mode." );
3160  }
3161  else
3162  {
3163  msg(M_INFO, "NOTICE: dual-stack mode for '--proto udp' does not "
3164  "work correctly with '--socks-proxy' today. Forcing IPv4." );
3165  }
3166  ce->af = AF_INET;
3167  }
3168 
3169  /*
3170  * Set MTU defaults
3171  */
3172  {
3174  {
3175  ce->tun_mtu_defined = true;
3176  }
3177  if ((dev == DEV_TYPE_TAP) && !ce->tun_mtu_extra_defined)
3178  {
3179  ce->tun_mtu_extra_defined = true;
3181  }
3182  }
3183 
3184  /*
3185  * If --mssfix is supplied without a parameter or not specified at all,
3186  * default it to --fragment value, if --fragment is specified and otherwise
3187  * to the default if tun-mtu is 1500
3188  */
3189  if (o->ce.mssfix_default)
3190  {
3191 #ifdef ENABLE_FRAGMENT
3192  if (ce->fragment)
3193  {
3194  ce->mssfix = ce->fragment;
3195  }
3196  else
3197 #endif
3198  if (ce->tun_mtu_defined)
3199  {
3200  if (o->ce.tun_mtu == TUN_MTU_DEFAULT)
3201  {
3202  /* We want to only set mssfix default value if we use a default
3203  * MTU Size, otherwise the different size of tun should either
3204  * already solve the problem or mssfix might artifically make the
3205  * payload packets smaller without mssfix 0 */
3207  ce->mssfix_encap = true;
3208  }
3209  else
3210  {
3211  /* We still apply the mssfix value but only adjust it to the
3212  * size of the tun interface. */
3213  ce->mssfix = ce->tun_mtu;
3214  ce->mssfix_fixed = true;
3215  }
3216  }
3217  }
3218 
3219  /*
3220  * Set per-connection block tls-auth/crypt/crypto-v2 fields if undefined.
3221  *
3222  * At the end only one of these will be really set because the parser
3223  * logic prevents configurations where more are set.
3224  */
3226  {
3230 
3233 
3236  }
3237 
3238  /* Pre-cache tls-auth/crypt(-v2) key file if persist-key was specified and
3239  * keys were not already embedded in the config file.
3240  */
3241  if (o->persist_key)
3242  {
3244  &ce->tls_auth_file_inline, &o->gc);
3246  &ce->tls_crypt_file_inline, &o->gc);
3248  &ce->tls_crypt_v2_file_inline, &o->gc);
3249  }
3250 
3252  {
3253  msg(M_WARN, "NOTICE: --explicit-exit-notify ignored for --proto tcp");
3255  }
3256 }
3257 
3258 #ifdef _WIN32
3259 /* If iservice is in use, we need def1 method for redirect-gateway */
3260 static void
3262 {
3263  if (opt->routes
3265  && opt->routes->flags & RG_REROUTE_GW
3266  && !(opt->routes->flags & RG_DEF1))
3267  {
3268  msg(M_INFO, "Flag 'def1' added to --redirect-gateway (iservice is in use)");
3269  opt->routes->flags |= RG_DEF1;
3270  }
3271 }
3272 #endif
3273 
3274 /*
3275  * Save/Restore certain option defaults before --pull is applied.
3276  */
3277 
3278 static void
3280 {
3285 
3286  if (o->routes)
3287  {
3289  o->pre_connect->routes_defined = true;
3290  }
3291  if (o->routes_ipv6)
3292  {
3294  o->pre_connect->routes_ipv6_defined = true;
3295  }
3296  if (o->client_nat)
3297  {
3299  o->pre_connect->client_nat_defined = true;
3300  }
3301 
3304 
3306 
3307  /* NCP related options that can be overwritten by a push */
3309  o->pre_connect->authname = o->authname;
3310 
3311  /* Ping related options should be reset to the config values on reconnect */
3315 
3316  /* Miscellaneous Options */
3317  o->pre_connect->comp = o->comp;
3318 }
3319 
3320 void
3322 {
3323  const struct options_pre_connect *pp = o->pre_connect;
3324  if (pp)
3325  {
3326  CLEAR(o->tuntap_options);
3327  if (pp->tuntap_options_defined)
3328  {
3329  o->tuntap_options = pp->tuntap_options;
3330  }
3331 
3332  if (pp->routes_defined)
3333  {
3334  rol_check_alloc(o);
3335  copy_route_option_list(o->routes, pp->routes, gc);
3336  }
3337  else
3338  {
3339  o->routes = NULL;
3340  }
3341 
3342  if (pp->routes_ipv6_defined)
3343  {
3344  rol6_check_alloc(o);
3346  }
3347  else
3348  {
3349  o->routes_ipv6 = NULL;
3350  }
3351 
3354 
3355  /* Free DNS options and reset them to pre-pull state */
3356  gc_free(&o->dns_options.gc);
3357  struct gc_arena dns_gc = gc_new();
3358  o->dns_options = clone_dns_options(pp->dns_options, &dns_gc);
3359  o->dns_options.gc = dns_gc;
3360 
3361  if (pp->client_nat_defined)
3362  {
3363  cnol_check_alloc(o);
3365  }
3366  else
3367  {
3368  o->client_nat = NULL;
3369  }
3370 
3372 
3373  o->ciphername = pp->ciphername;
3374  o->authname = pp->authname;
3375 
3379 
3380  /* Miscellaneous Options */
3381  o->comp = pp->comp;
3382  }
3383 
3384  o->push_continuation = 0;
3385  o->push_option_types_found = 0;
3386  o->imported_protocol_flags = 0;
3387 }
3388 
3389 static void
3391 {
3392 #ifdef _WIN32
3393  const int dev = dev_type_enum(options->dev, options->dev_type);
3394 
3395  /* when using wintun/ovpn-dco, kernel doesn't send DHCP requests, so don't use it */
3400  {
3402  }
3403 
3404  if ((dev == DEV_TYPE_TUN || dev == DEV_TYPE_TAP) && !options->route_delay_defined)
3405  {
3406  /* delay may only be necessary when we perform DHCP handshake */
3409  if ((options->mode == MODE_POINT_TO_POINT) && dhcp)
3410  {
3411  options->route_delay_defined = true;
3412  options->route_delay = 5; /* Vista sometimes has a race without this */
3413  }
3414  }
3415 
3416  if (options->ifconfig_noexec)
3417  {
3419  options->ifconfig_noexec = false;
3420  }
3421 
3423 
3424  /*
3425  * Check consistency of --mode server options.
3426  */
3427  if (options->mode == MODE_SERVER)
3428  {
3429  /*
3430  * We need to explicitly set --tap-sleep because
3431  * we do not schedule event timers in the top-level context.
3432  */
3435  {
3437  }
3438  options->route_delay_defined = false;
3439  }
3440 #endif /* ifdef _WIN32 */
3441 
3442 #ifdef DEFAULT_PKCS11_MODULE
3443  /* If p11-kit is present on the system then load its p11-kit-proxy.so
3444  * by default if the user asks for PKCS#11 without otherwise specifying
3445  * the module to use. */
3446  if (!options->pkcs11_providers[0]
3447  && (options->pkcs11_id || options->pkcs11_id_management))
3448  {
3449  options->pkcs11_providers[0] = DEFAULT_PKCS11_MODULE;
3450  }
3451 #endif
3452 }
3453 
3454 static void
3456 {
3457  if (o->connection_list)
3458  {
3459  int i;
3460  for (i = 0; i < o->connection_list->len; ++i)
3461  {
3463  }
3464  }
3465  else
3466  {
3468  }
3469 
3471 
3472  if (dco_enabled(o) && o->enable_c2c)
3473  {
3474  msg(M_WARN, "Note: --client-to-client has no effect when using data "
3475  "channel offload: packets are always sent to the VPN "
3476  "interface and then routed based on the system routing table");
3477  }
3478 }
3479 
3485 static void
3487 {
3488  if (o->ncp_ciphers)
3489  {
3490  /* custom --data-ciphers set, keep list */
3491  return;
3492  }
3493 
3494  /* check if crypto library supports chacha */
3495  bool can_do_chacha = cipher_valid("CHACHA20-POLY1305");
3496 
3497  if (can_do_chacha && dco_enabled(o))
3498  {
3499  /* also make sure that dco supports chacha */
3500  can_do_chacha = tls_item_in_cipher_list("CHACHA20-POLY1305", dco_get_supported_ciphers());
3501  }
3502 
3503  if (can_do_chacha)
3504  {
3505  o->ncp_ciphers = "AES-256-GCM:AES-128-GCM:CHACHA20-POLY1305";
3506  }
3507  else
3508  {
3509  o->ncp_ciphers = "AES-256-GCM:AES-128-GCM";
3510  }
3511 }
3512 
3513 static void
3515 {
3516  if (!o->pull && !(o->mode == MODE_SERVER))
3517  {
3518  /* If the cipher is not set, use the old default of BF-CBC. We will
3519  * warn that this is deprecated on cipher initialisation, no need
3520  * to warn here as well */
3521  if (!o->ciphername)
3522  {
3523  o->ciphername = "BF-CBC";
3524  }
3525  else
3526  {
3527  o->enable_ncp_fallback = true;
3528  }
3529  return;
3530  }
3531 
3532  /* pull or P2MP mode */
3533  if (!o->ciphername)
3534  {
3535  /* We still need to set the ciphername to BF-CBC since various other
3536  * parts of OpenVPN assert that the ciphername is set */
3537  o->ciphername = "BF-CBC";
3538 
3539  msg(M_INFO, "Note: --cipher is not set. OpenVPN versions before 2.5 "
3540  "defaulted to BF-CBC as fallback when cipher negotiation "
3541  "failed in this case. If you need this fallback please add "
3542  "'--data-ciphers-fallback BF-CBC' to your configuration "
3543  "and/or add BF-CBC to --data-ciphers.");
3544  }
3545  else if (!o->enable_ncp_fallback
3547  {
3548  msg(M_WARN, "DEPRECATED OPTION: --cipher set to '%s' but missing in "
3549  "--data-ciphers (%s). OpenVPN ignores --cipher for cipher "
3550  "negotiations. ",
3551  o->ciphername, o->ncp_ciphers);
3552  }
3553 }
3554 
3569 static bool
3570 need_compatibility_before(const struct options *o, unsigned int version)
3571 {
3572  return o->backwards_compatible != 0 && o->backwards_compatible < version;
3573 }
3574 
3579 static void
3581 {
3582  /* TLS min version is not set */
3583  int tls_ver_min = (o->ssl_flags >> SSLF_TLS_VERSION_MIN_SHIFT)
3585  if (tls_ver_min == 0)
3586  {
3587  int tls_ver_max = (o->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT)
3589  if (need_compatibility_before(o, 20307))
3590  {
3591  /* 2.3.6 and earlier have TLS 1.0 only, set minimum to TLS 1.0 */
3593  }
3594  else if (tls_ver_max == 0 || tls_ver_max >= TLS_VER_1_2)
3595  {
3596  /* Use TLS 1.2 as proper default */
3598  }
3599  else
3600  {
3601  /* Maximize the minimum version */
3602  o->ssl_flags |= (tls_ver_max << SSLF_TLS_VERSION_MIN_SHIFT);
3603  }
3604  }
3605 
3606  if (need_compatibility_before(o, 20400))
3607  {
3608  if (!o->ciphername)
3609  {
3610  /* If ciphername is not set default to BF-CBC when targeting these
3611  * old versions that do not have NCP */
3612  o->ciphername = "BF-CBC";
3613  }
3614  /* Versions < 2.4.0 additionally might be compiled with --enable-small and
3615  * not have OCC strings required for "poor man's NCP" */
3616  o->enable_ncp_fallback = true;
3617  }
3618 
3619  /* Versions < 2.5.0 do need --cipher in the list of accepted ciphers.
3620  * Version 2.4 probably does not need it but NCP was not so
3621  * good with 2.4 and ncp-disable might be more common on 2.4 peers.
3622  * Only do this iff --cipher is set (explicitly or by compat mode
3623  * < 2.4.0, see above). This is not 100% correct backwards compatible
3624  * behaviour but 2.5 already behaved like this */
3625  if (o->ciphername && need_compatibility_before(o, 20500)
3627  {
3629  }
3630 
3631 #ifdef USE_COMP
3632  /* Compression is deprecated and we do not want to announce support for it
3633  * by default anymore, additionally DCO breaks with compression.
3634  *
3635  * Disable compression by default starting with 2.6.0 if no other
3636  * compression related option has been explicitly set */
3637  if (!need_compatibility_before(o, 20600) && (o->comp.flags == 0))
3638  {
3639  if (!comp_non_stub_enabled(&o->comp))
3640  {
3642  }
3643  }
3644 #else /* ifdef USE_COMP */
3646 #endif
3647 }
3648 
3649 static void
3651 {
3652  int i;
3653  /*
3654  * Process helper-type options which map to other, more complex
3655  * sequences of options.
3656  */
3658  helper_keepalive(o);
3659  helper_tcp_nodelay(o);
3660 
3663 
3666  if (o->ncp_ciphers == NULL)
3667  {
3668  msg(M_USAGE, "--data-ciphers list contains unsupported ciphers or is too long.");
3669  }
3670 
3671  if (o->remote_list && !o->connection_list)
3672  {
3673  /*
3674  * Convert remotes into connection list
3675  */
3676  const struct remote_list *rl = o->remote_list;
3677  for (i = 0; i < rl->len; ++i)
3678  {
3679  const struct remote_entry *re = rl->array[i];
3680  struct connection_entry ce = o->ce;
3681  struct connection_entry *ace;
3682 
3683  ASSERT(re->remote);
3684  connection_entry_load_re(&ce, re);
3685  ace = alloc_connection_entry(o, M_USAGE);
3686  ASSERT(ace);
3687  *ace = ce;
3688  }
3689  }
3690  else if (!o->remote_list && !o->connection_list)
3691  {
3692  struct connection_entry *ace;
3693  ace = alloc_connection_entry(o, M_USAGE);
3694  ASSERT(ace);
3695  *ace = o->ce;
3696  }
3697 
3698  ASSERT(o->connection_list);
3699  for (i = 0; i < o->connection_list->len; ++i)
3700  {
3702  }
3703 
3704  if (o->tls_server)
3705  {
3706  /* Check that DH file is specified, or explicitly disabled */
3707  notnull(o->dh_file, "DH file (--dh)");
3708  if (streq(o->dh_file, "none"))
3709  {
3710  o->dh_file = NULL;
3711  }
3712  }
3713  else if (o->dh_file)
3714  {
3715  /* DH file is only meaningful in a tls-server context. */
3716  msg(M_WARN, "WARNING: Ignoring option 'dh' in tls-client mode, please only "
3717  "include this in your server configuration");
3718  o->dh_file = NULL;
3719  }
3720 #if ENABLE_MANAGEMENT
3721  if (o->http_proxy_override)
3722  {
3724  }
3725 #endif
3726  if (!o->ca_file && !o->ca_path && o->verify_hash
3727  && o->verify_hash_depth == 0)
3728  {
3729  msg(M_INFO, "Using certificate fingerprint to verify peer (no CA "
3730  "option set). ");
3731  o->verify_hash_no_ca = true;
3732  }
3733 
3734  if (o->config && streq(o->config, "stdin") && o->remap_sigusr1 == SIGHUP)
3735  {
3736  msg(M_USAGE, "Options 'config stdin' and 'remap-usr1 SIGHUP' are "
3737  "incompatible with each other.");
3738  }
3739 
3740  if (dco_enabled(o))
3741  {
3742  /* check if any option should force disabling DCO */
3745  }
3746 #ifdef USE_COMP
3747  if (dco_enabled(o))
3748  {
3750  }
3751 #endif
3752 
3753 #ifdef _WIN32
3754  if (dco_enabled(o))
3755  {
3757  }
3758  else
3759  {
3761  {
3762  msg(M_WARN, "Option --windows-driver ovpn-dco is ignored because Data Channel Offload is disabled");
3764  }
3766  {
3768  }
3769  }
3770 #else /* _WIN32 */
3771  if (dco_enabled(o) && o->dev_node)
3772  {
3773  msg(M_WARN, "Note: ignoring --dev-node as it has no effect when using "
3774  "data channel offload");
3775  o->dev_node = NULL;
3776  }
3777 #endif /* _WIN32 */
3778 
3779  /* this depends on o->windows_driver, which is set above */
3781 
3782  /* check that compression settings in the options are okay */
3784 
3785  /*
3786  * Save certain parms before modifying options during connect, especially
3787  * when using --pull
3788  */
3789  if (o->pull)
3790  {
3792  }
3793  else
3794  {
3795 #if defined(_WIN32) || defined(TARGET_ANDROID)
3797 #else
3798  foreign_options_copy_dns(o, es);
3799 #endif
3800  }
3802  {
3804  }
3805  pre_connect_save(o);
3806 }
3807 
3808 /*
3809  * Check file/directory sanity
3810  *
3811  */
3812 #ifndef ENABLE_SMALL
3814 #define CHKACC_FILE (1<<0)
3815 #define CHKACC_DIRPATH (1<<1)
3816 #define CHKACC_FILEXSTWR (1<<2)
3817 #define CHKACC_ACPTSTDIN (1<<3)
3818 #define CHKACC_PRIVATE (1<<4)
3820 static bool
3821 check_file_access(const int type, const char *file, const int mode, const char *opt)
3822 {
3823  int errcode = 0;
3824 
3825  /* If no file configured, no errors to look for */
3826  if (!file)
3827  {
3828  return false;
3829  }
3830 
3831  /* If stdin is allowed and the file name is 'stdin', then do no
3832  * further checks as stdin is always available
3833  */
3834  if ( (type & CHKACC_ACPTSTDIN) && streq(file, "stdin") )
3835  {
3836  return false;
3837  }
3838 
3839  /* Is the directory path leading to the given file accessible? */
3840  if (type & CHKACC_DIRPATH)
3841  {
3842  char *fullpath = string_alloc(file, NULL); /* POSIX dirname() implementation may modify its arguments */
3843  char *dirpath = dirname(fullpath);
3844 
3845  if (platform_access(dirpath, mode|X_OK) != 0)
3846  {
3847  errcode = errno;
3848  }
3849  free(fullpath);
3850  }
3851 
3852  /* Is the file itself accessible? */
3853  if (!errcode && (type & CHKACC_FILE) && (platform_access(file, mode) != 0) )
3854  {
3855  errcode = errno;
3856  }
3857 
3858  /* If the file exists and is accessible, is it writable? */
3859  if (!errcode && (type & CHKACC_FILEXSTWR) && (platform_access(file, F_OK) == 0) )
3860  {
3861  if (platform_access(file, W_OK) != 0)
3862  {
3863  errcode = errno;
3864  }
3865  }
3866 
3867  /* Warn if a given private file is group/others accessible. */
3868  if (type & CHKACC_PRIVATE)
3869  {
3870  platform_stat_t st;
3871  if (platform_stat(file, &st))
3872  {
3873  msg(M_WARN | M_ERRNO, "WARNING: cannot stat file '%s'", file);
3874  }
3875 #ifndef _WIN32
3876  else
3877  {
3878  if (st.st_mode & (S_IRWXG|S_IRWXO))
3879  {
3880  msg(M_WARN, "WARNING: file '%s' is group or others accessible", file);
3881  }
3882  }
3883 #endif
3884  }
3885 
3886  /* Scream if an error is found */
3887  if (errcode > 0)
3888  {
3889  msg(M_NOPREFIX | M_OPTERR | M_ERRNO, "%s fails with '%s'", opt, file);
3890  }
3891 
3892  /* Return true if an error occurred */
3893  return (errcode != 0 ? true : false);
3894 }
3895 
3896 /* A wrapper for check_file_access() which also takes a chroot directory.
3897  * If chroot is NULL, behaviour is exactly the same as calling check_file_access() directly,
3898  * otherwise it will look for the file inside the given chroot directory instead.
3899  */
3900 static bool
3901 check_file_access_chroot(const char *chroot, const int type, const char *file, const int mode, const char *opt)
3902 {
3903  bool ret = false;
3904 
3905  /* If no file configured, no errors to look for */
3906  if (!file)
3907  {
3908  return false;
3909  }
3910 
3911  /* If chroot is set, look for the file/directory inside the chroot */
3912  if (chroot)
3913  {
3914  struct gc_arena gc = gc_new();
3915  struct buffer chroot_file;
3916 
3917  chroot_file = prepend_dir(chroot, file, &gc);
3918  ret = check_file_access(type, BSTR(&chroot_file), mode, opt);
3919  gc_free(&gc);
3920  }
3921  else
3922  {
3923  /* No chroot in play, just call core file check function */
3924  ret = check_file_access(type, file, mode, opt);
3925  }
3926  return ret;
3927 }
3928 
3933 static bool
3934 check_file_access_chroot_inline(bool is_inline, const char *chroot,
3935  const int type, const char *file,
3936  const int mode, const char *opt)
3937 {
3938  if (is_inline)
3939  {
3940  return false;
3941  }
3942 
3943  return check_file_access_chroot(chroot, type, file, mode, opt);
3944 }
3945 
3950 static bool
3951 check_file_access_inline(bool is_inline, const int type, const char *file,
3952  const int mode, const char *opt)
3953 {
3954  if (is_inline)
3955  {
3956  return false;
3957  }
3958 
3959  return check_file_access(type, file, mode, opt);
3960 }
3961 
3962 /*
3963  * Verifies that the path in the "command" that comes after certain script options (e.g., --up) is a
3964  * valid file with appropriate permissions.
3965  *
3966  * "command" consists of a path, optionally followed by a space, which may be
3967  * followed by arbitrary arguments. It is NOT a full shell command line -- shell expansion is not
3968  * performed.
3969  *
3970  * The path and arguments in "command" may be single- or double-quoted or escaped.
3971  *
3972  * The path is extracted from "command", then check_file_access() is called to check it. The
3973  * arguments, if any, are ignored.
3974  *
3975  * Note that the type, mode, and opt arguments to this routine are the same as the corresponding
3976  * check_file_access() arguments.
3977  */
3978 static bool
3979 check_cmd_access(const char *command, const char *opt, const char *chroot)
3980 {
3981  struct argv argv;
3982  bool return_code;
3983 
3984  /* If no command was set, there are no errors to look for */
3985  if (!command)
3986  {
3987  return false;
3988  }
3989 
3990  /* Extract executable path and arguments */
3991  argv = argv_new();
3992  argv_parse_cmd(&argv, command);
3993 
3994  /* if an executable is specified then check it; otherwise, complain */
3995  if (argv.argv[0])
3996  {
3997  /* Scripts requires R_OK as well, but that might fail on binaries which
3998  * only requires X_OK to function on Unix - a scenario not unlikely to
3999  * be seen on suid binaries.
4000  */
4001  return_code = check_file_access_chroot(chroot, CHKACC_FILE, argv.argv[0], X_OK, opt);
4002  }
4003  else
4004  {
4005  msg(M_NOPREFIX|M_OPTERR, "%s fails with '%s': No path to executable.",
4006  opt, command);
4007  return_code = true;
4008  }
4009 
4010  argv_free(&argv);
4011 
4012  return return_code;
4013 }
4014 
4015 /*
4016  * Sanity check of all file/dir options. Checks that file/dir
4017  * is accessible by OpenVPN
4018  */
4019 static void
4021 {
4022  bool errs = false;
4023 
4024  /* ** SSL/TLS/crypto related files ** */
4026  options->dh_file, R_OK, "--dh");
4027 
4028  if (!options->verify_hash_no_ca)
4029  {
4031  options->ca_file, R_OK, "--ca");
4032  }
4033 
4035  options->ca_path, R_OK, "--capath");
4036 
4038  options->cert_file, R_OK, "--cert");
4039 
4041  options->extra_certs_file, R_OK,
4042  "--extra-certs");
4043 
4045  {
4048  options->priv_key_file, R_OK, "--key");
4049  }
4050 
4053  options->pkcs12_file, R_OK, "--pkcs12");
4054 
4056  {
4058  options->crl_file, R_OK|X_OK,
4059  "--crl-verify directory");
4060  }
4061  else
4062  {
4066  R_OK, "--crl-verify");
4067  }
4068 
4070  for (int i = 0; i < options->connection_list->len; ++i)
4071  {
4072  struct connection_entry *ce = options->connection_list->array[i];
4073 
4076  ce->tls_auth_file, R_OK,
4077  "--tls-auth");
4080  ce->tls_crypt_file, R_OK,
4081  "--tls-crypt");
4084  ce->tls_crypt_v2_file, R_OK,
4085  "--tls-crypt-v2");
4086  }
4087 
4090  options->shared_secret_file, R_OK,
4091  "--secret");
4092 
4094  options->packet_id_file, R_OK|W_OK, "--replay-persist");
4095 
4096  /* ** Password files ** */
4098  options->key_pass_file, R_OK, "--askpass");
4099 #ifdef ENABLE_MANAGEMENT
4102  "--management user/password file");
4103 #endif /* ENABLE_MANAGEMENT */
4107  "--auth-user-pass");
4108  /* ** System related ** */
4110  R_OK|X_OK, "--chroot directory");
4112  R_OK|W_OK, "--writepid");
4113 
4114  /* ** Log related ** */
4116  R_OK|W_OK, "--status");
4117 
4118  /* ** Config related ** */
4120  R_OK|W_OK|X_OK, "--tls-export-cert");
4122  R_OK|X_OK, "--client-config-dir");
4124  R_OK|W_OK|X_OK, "Temporary directory (--tmp-dir)");
4125 
4126  if (errs)
4127  {
4128  msg(M_USAGE, "Please correct these errors.");
4129  }
4130 }
4131 #endif /* !ENABLE_SMALL */
4132 
4133 /*
4134  * Sanity check on options.
4135  * Also set some options based on other
4136  * options.
4137  */
4138 void
4140 {
4143 #ifndef ENABLE_SMALL
4145 #endif /* !ENABLE_SMALL */
4146 }
4147 
4148 /*
4149  * Sanity check on options after more options were pulled from server.
4150  * Also time to modify some options based on other options.
4151  */
4152 bool
4154 {
4155  bool success = dns_options_verify(D_PUSH_ERRORS, &o->dns_options);
4156  if (success)
4157  {
4160 #if defined(_WIN32) || defined(TARGET_ANDROID)
4162 #else
4163  foreign_options_copy_dns(o, es);
4164 #endif
4165  }
4166  return success;
4167 }
4168 
4169 /*
4170  * Build an options string to represent data channel encryption options.
4171  * This string must match exactly between peers. The keysize is checked
4172  * separately by read_key().
4173  *
4174  * The following options must match on both peers:
4175  *
4176  * Tunnel options:
4177  *
4178  * --dev tun|tap [unit number need not match]
4179  * --dev-type tun|tap
4180  * --link-mtu
4181  * --udp-mtu
4182  * --tun-mtu
4183  * --proto udp
4184  * --proto tcp-client [matched with --proto tcp-server
4185  * on the other end of the connection]
4186  * --proto tcp-server [matched with --proto tcp-client on
4187  * the other end of the connection]
4188  * --tun-ipv6
4189  * --ifconfig x y [matched with --ifconfig y x on
4190  * the other end of the connection]
4191  *
4192  * --comp-lzo
4193  * --compress alg
4194  * --fragment
4195  *
4196  * Crypto Options:
4197  *
4198  * --cipher
4199  * --auth
4200  * --secret
4201  * --no-replay
4202  *
4203  * SSL Options:
4204  *
4205  * --tls-auth
4206  * --tls-client [matched with --tls-server on
4207  * the other end of the connection]
4208  * --tls-server [matched with --tls-client on
4209  * the other end of the connection]
4210  */
4211 char *
4212 options_string(const struct options *o,
4213  const struct frame *frame,
4214  struct tuntap *tt,
4215  openvpn_net_ctx_t *ctx,
4216  bool remote,
4217  struct gc_arena *gc)
4218 {
4219  struct buffer out = alloc_buf(OPTION_LINE_SIZE);
4220  bool tt_local = false;
4221 
4222  buf_printf(&out, "V4");
4223 
4224  /*
4225  * Tunnel Options
4226  */
4227 
4228  buf_printf(&out, ",dev-type %s", dev_type_string(o->dev, o->dev_type));
4229  /* the link-mtu that we send has only a meaning if have a fixed
4230  * cipher (p2p) or have a fallback cipher configured for older non
4231  * ncp clients. But not sending it will make even 2.4 complain
4232  * about it being missing. So still send it. */
4233  buf_printf(&out, ",link-mtu %u",
4234  (unsigned int) calc_options_string_link_mtu(o, frame));
4235 
4236  if (o->ce.occ_mtu != 0)
4237  {
4238  buf_printf(&out, ",tun-mtu %d", o->ce.occ_mtu);
4239  }
4240  else
4241  {
4242  buf_printf(&out, ",tun-mtu %d", frame->tun_mtu);
4243  }
4244 
4245  buf_printf(&out, ",proto %s", proto_remote(o->ce.proto, remote));
4246 
4247  bool p2p_nopull = o->mode == MODE_POINT_TO_POINT && !PULL_DEFINED(o);
4248  /* send tun_ipv6 only in peer2peer mode - in client/server mode, it
4249  * is usually pushed by the server, triggering a non-helpful warning
4250  */
4251  if (o->ifconfig_ipv6_local && p2p_nopull)
4252  {
4253  buf_printf(&out, ",tun-ipv6");
4254  }
4255 
4256  /*
4257  * Try to get ifconfig parameters into the options string.
4258  * If tt is undefined, make a temporary instantiation.
4259  */
4260  if (!tt)
4261  {
4262  tt = init_tun(o->dev,
4263  o->dev_type,
4264  o->topology,
4265  o->ifconfig_local,
4270  NULL,
4271  NULL,
4272  false,
4273  NULL,
4274  ctx,
4275  NULL);
4276  if (tt)
4277  {
4278  tt_local = true;
4279  }
4280  }
4281 
4282  if (tt && p2p_nopull)
4283  {
4284  const char *ios = ifconfig_options_string(tt, remote, o->ifconfig_nowarn, gc);
4285  if (ios && strlen(ios))
4286  {
4287  buf_printf(&out, ",ifconfig %s", ios);
4288  }
4289  }
4290  if (tt_local)
4291  {
4292  free(tt);
4293  tt = NULL;
4294  }
4295 
4296 #ifdef USE_COMP
4297  if (o->comp.alg != COMP_ALG_UNDEF)
4298  {
4299  buf_printf(&out, ",comp-lzo"); /* for compatibility, this simply indicates that compression context is active, not necessarily LZO per-se */
4300  }
4301 #endif
4302 
4303 #ifdef ENABLE_FRAGMENT
4304  if (o->ce.fragment)
4305  {
4306  buf_printf(&out, ",mtu-dynamic");
4307  }
4308 #endif
4309 
4310 #define TLS_CLIENT (o->tls_client)
4311 #define TLS_SERVER (o->tls_server)
4312 
4313  /*
4314  * Key direction
4315  */
4316  {
4317  const char *kd = keydirection2ascii(o->key_direction, remote, false);
4318  if (kd)
4319  {
4320  buf_printf(&out, ",keydir %s", kd);
4321  }
4322  }
4323 
4324  /*
4325  * Crypto Options
4326  */
4328  {
4329  struct key_type kt;
4330 
4331  ASSERT((o->shared_secret_file != NULL)
4332  + (TLS_CLIENT == true)
4333  + (TLS_SERVER == true)
4334  <= 1);
4335 
4336  /* Skip resolving BF-CBC to allow SSL libraries without BF-CBC
4337  * to work here in the default configuration */
4338  const char *ciphername = o->ciphername;
4339  int keysize = 0;
4340 
4341  if (strcmp(o->ciphername, "BF-CBC") == 0)
4342  {
4343  init_key_type(&kt, "none", o->authname, true, false);
4344  keysize = 128;
4345  }
4346  else
4347  {
4348  init_key_type(&kt, o->ciphername, o->authname, true, false);
4349  ciphername = cipher_kt_name(kt.cipher);
4350  if (cipher_defined(o->ciphername))
4351  {
4352  keysize = cipher_kt_key_size(kt.cipher) * 8;
4353  }
4354  }
4355  /* Only announce the cipher to our peer if we are willing to
4356  * support it */
4357  if (p2p_nopull || tls_item_in_cipher_list(ciphername, o->ncp_ciphers))
4358  {
4359  buf_printf(&out, ",cipher %s", ciphername);
4360  }
4361  buf_printf(&out, ",auth %s", md_kt_name(kt.digest));
4362  buf_printf(&out, ",keysize %d", keysize);
4363  if (o->shared_secret_file)
4364  {
4365  buf_printf(&out, ",secret");
4366  }
4367  if (!o->replay)
4368  {
4369  buf_printf(&out, ",no-replay");
4370  }
4371 
4372 #ifdef ENABLE_PREDICTION_RESISTANCE
4373  if (o->use_prediction_resistance)
4374  {
4375  buf_printf(&out, ",use-prediction-resistance");
4376  }
4377 #endif
4378  }
4379 
4380  /*
4381  * SSL Options
4382  */
4383  {
4384  if (TLS_CLIENT || TLS_SERVER)
4385  {
4386  if (o->ce.tls_auth_file)
4387  {
4388  buf_printf(&out, ",tls-auth");
4389  }
4390  /* Not adding tls-crypt here, because we won't reach this code if
4391  * tls-auth/tls-crypt does not match. Removing tls-auth here would
4392  * break stuff, so leaving that in place. */
4393 
4394  buf_printf(&out, ",key-method %d", KEY_METHOD_2);
4395  }
4396 
4397  if (remote)
4398  {
4399  if (TLS_CLIENT)
4400  {
4401  buf_printf(&out, ",tls-server");
4402  }
4403  else if (TLS_SERVER)
4404  {
4405  buf_printf(&out, ",tls-client");
4406  }
4407  }
4408  else
4409  {
4410  if (TLS_CLIENT)
4411  {
4412  buf_printf(&out, ",tls-client");
4413  }
4414  else if (TLS_SERVER)
4415  {
4416  buf_printf(&out, ",tls-server");
4417  }
4418  }
4419  }
4420 
4421 #undef TLS_CLIENT
4422 #undef TLS_SERVER
4423 
4424  return BSTR(&out);
4425 }
4426 
4427 /*
4428  * Compare option strings for equality.
4429  * If the first two chars of the strings differ, it means that
4430  * we are looking at different versions of the options string,
4431  * therefore don't compare them and return true.
4432  */
4433 
4434 bool
4435 options_cmp_equal(char *actual, const char *expected)
4436 {
4437  return options_cmp_equal_safe(actual, expected, strlen(actual) + 1);
4438 }
4439 
4440 void
4441 options_warning(char *actual, const char *expected)
4442 {
4443  options_warning_safe(actual, expected, strlen(actual) + 1);
4444 }
4445 
4446 static const char *
4447 options_warning_extract_parm1(const char *option_string,
4448  struct gc_arena *gc_ret)
4449 {
4450  struct gc_arena gc = gc_new();
4451  struct buffer b = string_alloc_buf(option_string, &gc);
4452  char *p = gc_malloc(OPTION_PARM_SIZE, false, &gc);
4453  const char *ret;
4454 
4455  buf_parse(&b, ' ', p, OPTION_PARM_SIZE);
4456  ret = string_alloc(p, gc_ret);
4457  gc_free(&gc);
4458  return ret;
4459 }
4460 
4461 static void
4462 options_warning_safe_scan2(const int msglevel,
4463  const int delim,
4464  const bool report_inconsistent,
4465  const char *p1,
4466  const struct buffer *b2_src,
4467  const char *b1_name,
4468  const char *b2_name)
4469 {
4470  /* We will stop sending 'key-method', 'keydir', 'proto' and 'tls-auth' in
4471  * OCC in a future version (because it's not useful). To reduce questions
4472  * when interoperating, we no longer printing a warning about it.
4473  */
4474  if (strprefix(p1, "key-method ")
4475  || strprefix(p1, "keydir ")
4476  || strprefix(p1, "proto ")
4477  || streq(p1, "tls-auth")
4478  || strprefix(p1, "tun-ipv6")
4479  || strprefix(p1, "cipher "))
4480  {
4481  return;
4482  }
4483 
4484  if (strlen(p1) > 0)
4485  {
4486  struct gc_arena gc = gc_new();
4487  struct buffer b2 = *b2_src;
4488  const char *p1_prefix = options_warning_extract_parm1(p1, &gc);
4489  char *p2 = gc_malloc(OPTION_PARM_SIZE, false, &gc);
4490 
4491  while (buf_parse(&b2, delim, p2, OPTION_PARM_SIZE))
4492  {
4493  if (strlen(p2))
4494  {
4495  const char *p2_prefix = options_warning_extract_parm1(p2, &gc);
4496 
4497  if (!strcmp(p1, p2))
4498  {
4499  goto done;
4500  }
4501  if (!strcmp(p1_prefix, p2_prefix))
4502  {
4503  if (report_inconsistent)
4504  {
4505  msg(msglevel, "WARNING: '%s' is used inconsistently, %s='%s', %s='%s'",
4506  safe_print(p1_prefix, &gc),
4507  b1_name,
4508  safe_print(p1, &gc),
4509  b2_name,
4510  safe_print(p2, &gc));
4511  }
4512  goto done;
4513  }
4514  }
4515  }
4516 
4517  msg(msglevel, "WARNING: '%s' is present in %s config but missing in %s config, %s='%s'",
4518  safe_print(p1_prefix, &gc),
4519  b1_name,
4520  b2_name,
4521  b1_name,
4522  safe_print(p1, &gc));
4523 
4524 done:
4525  gc_free(&gc);
4526  }
4527 }
4528 
4529 static void
4530 options_warning_safe_scan1(const int msglevel,
4531  const int delim,
4532  const bool report_inconsistent,
4533  const struct buffer *b1_src,
4534  const struct buffer *b2_src,
4535  const char *b1_name,
4536  const char *b2_name)
4537 {
4538  struct gc_arena gc = gc_new();
4539  struct buffer b = *b1_src;
4540  char *p = gc_malloc(OPTION_PARM_SIZE, true, &gc);
4541 
4542  while (buf_parse(&b, delim, p, OPTION_PARM_SIZE))
4543  {
4544  options_warning_safe_scan2(msglevel, delim, report_inconsistent, p, b2_src, b1_name, b2_name);
4545  }
4546 
4547  gc_free(&gc);
4548 }
4549 
4550 static void
4551 options_warning_safe_ml(const int msglevel, char *actual, const char *expected, size_t actual_n)
4552 {
4553  struct gc_arena gc = gc_new();
4554 
4555  if (actual_n > 0)
4556  {
4557  struct buffer local = alloc_buf_gc(OPTION_PARM_SIZE + 16, &gc);
4558  struct buffer remote = alloc_buf_gc(OPTION_PARM_SIZE + 16, &gc);
4559  actual[actual_n - 1] = 0;
4560 
4561  buf_printf(&local, "version %s", expected);
4562  buf_printf(&remote, "version %s", actual);
4563 
4564  options_warning_safe_scan1(msglevel, ',', true,
4565  &local, &remote,
4566  "local", "remote");
4567 
4568  options_warning_safe_scan1(msglevel, ',', false,
4569  &remote, &local,
4570  "remote", "local");
4571  }
4572 
4573  gc_free(&gc);
4574 }
4575 
4576 bool
4577 options_cmp_equal_safe(char *actual, const char *expected, size_t actual_n)
4578 {
4579  struct gc_arena gc = gc_new();
4580  bool ret = true;
4581 
4582  if (actual_n > 0)
4583  {
4584  actual[actual_n - 1] = 0;
4585  if (strncmp(actual, expected, 2))
4586  {
4587  msg(D_SHOW_OCC, "NOTE: Options consistency check may be skewed by version differences");
4588  options_warning_safe_ml(D_SHOW_OCC, actual, expected, actual_n);
4589  }
4590  else
4591  {
4592  ret = !strcmp(actual, expected);
4593  }
4594  }
4595  gc_free(&gc);
4596  return ret;
4597 }
4598 
4599 void
4600 options_warning_safe(char *actual, const char *expected, size_t actual_n)
4601 {
4602  options_warning_safe_ml(D_SHOW_OCC, actual, expected, actual_n);
4603 }
4604 
4605 const char *
4606 options_string_version(const char *s, struct gc_arena *gc)
4607 {
4608  struct buffer out = alloc_buf_gc(4, gc);
4609  strncpynt((char *) BPTR(&out), s, 3);
4610  return BSTR(&out);
4611 }
4612 
4613 char *
4614 options_string_extract_option(const char *options_string, const char *opt_name,
4615  struct gc_arena *gc)
4616 {
4617  char *ret = NULL;
4618  const size_t opt_name_len = strlen(opt_name);
4619 
4620  const char *p = options_string;
4621  while (p)
4622  {
4623  if (0 == strncmp(p, opt_name, opt_name_len)
4624  && strlen(p) > (opt_name_len+1) && p[opt_name_len] == ' ')
4625  {
4626  /* option found, extract value */
4627  const char *start = &p[opt_name_len+1];
4628  const char *end = strchr(p, ',');
4629  size_t val_len = end ? end - start : strlen(start);
4630  ret = gc_malloc(val_len+1, true, gc);
4631  memcpy(ret, start, val_len);
4632  break;
4633  }
4634  p = strchr(p, ',');
4635  if (p)
4636  {
4637  p++; /* skip delimiter */
4638  }
4639  }
4640  return ret;
4641 }
4642 
4643 #ifdef _WIN32
4644 
4651 static enum windows_driver_type
4652 parse_windows_driver(const char *str, const int msglevel)
4653 {
4654  if (streq(str, "tap-windows6"))
4655  {
4657  }
4658  else if (streq(str, "wintun"))
4659  {
4660  return WINDOWS_DRIVER_WINTUN;
4661  }
4662 
4663  else if (streq(str, "ovpn-dco"))
4664  {
4665  return WINDOWS_DRIVER_DCO;
4666  }
4667  else
4668  {
4669  msg(msglevel, "--windows-driver must be tap-windows6, wintun "
4670  "or ovpn-dco");
4672  }
4673 }
4674 #endif /* ifdef _WIN32 */
4675 
4676 /*
4677  * parse/print topology coding
4678  */
4679 
4680 int
4681 parse_topology(const char *str, const int msglevel)
4682 {
4683  if (streq(str, "net30"))
4684  {
4685  return TOP_NET30;
4686  }
4687  else if (streq(str, "p2p"))
4688  {
4689  return TOP_P2P;
4690  }
4691  else if (streq(str, "subnet"))
4692  {
4693  return TOP_SUBNET;
4694  }
4695  else
4696  {
4697  msg(msglevel, "--topology must be net30, p2p, or subnet");
4698  return TOP_UNDEF;
4699  }
4700 }
4701 
4702 const char *
4703 print_topology(const int topology)
4704 {
4705  switch (topology)
4706  {
4707  case TOP_UNDEF:
4708  return "undef";
4709 
4710  case TOP_NET30:
4711  return "net30";
4712 
4713  case TOP_P2P:
4714  return "p2p";
4715 
4716  case TOP_SUBNET:
4717  return "subnet";
4718 
4719  default:
4720  return "unknown";
4721  }
4722 }
4723 
4724 /*
4725  * Manage auth-retry variable
4726  */
4727 
4728 static int global_auth_retry; /* GLOBAL */
4729 
4730 int
4732 {
4733  return global_auth_retry;
4734 }
4735 
4736 bool
4737 auth_retry_set(const int msglevel, const char *option)
4738 {
4739  if (streq(option, "interact"))
4740  {
4742  }
4743  else if (streq(option, "nointeract"))
4744  {
4746  }
4747  else if (streq(option, "none"))
4748  {
4750  }
4751  else
4752  {
4753  msg(msglevel, "--auth-retry method must be 'interact', 'nointeract', or 'none'");
4754  return false;
4755  }
4756  return true;
4757 }
4758 
4759 const char *
4761 {
4762  switch (global_auth_retry)
4763  {
4764  case AR_NONE:
4765  return "none";
4766 
4767  case AR_NOINTERACT:
4768  return "nointeract";
4769 
4770  case AR_INTERACT:
4771  return "interact";
4772 
4773  default:
4774  return "???";
4775  }
4776 }
4777 
4778 /*
4779  * Print the help message.
4780  */
4781 static void
4782 usage(void)
4783 {
4784  FILE *fp = msg_fp(0);
4785 
4786 #ifdef ENABLE_SMALL
4787 
4788  fprintf(fp, "Usage message not available\n");
4789 
4790 #else
4791 
4792  struct options o;
4793  init_options(&o, true);
4794 
4795  fprintf(fp, usage_message,
4796  title_string,
4799  o.ce.local_port, o.ce.remote_port,
4801  o.verbosity,
4802  o.authname,
4806  fflush(fp);
4807 
4808 #endif /* ENABLE_SMALL */
4809 
4810  openvpn_exit(OPENVPN_EXIT_STATUS_USAGE); /* exit point */
4811 }
4812 
4813 void
4815 {
4816  msg(M_WARN|M_NOPREFIX, "Use --help for more information.");
4817  openvpn_exit(OPENVPN_EXIT_STATUS_USAGE); /* exit point */
4818 }
4819 
4820 #ifdef _WIN32
4821 void
4822 show_windows_version(const unsigned int flags)
4823 {
4824  struct gc_arena gc = gc_new();
4825  msg(flags, "Windows version %s", win32_version_string(&gc, true));
4826  gc_free(&gc);
4827 }
4828 #endif
4829 
4830 void
4831 show_dco_version(const unsigned int flags)
4832 {
4833 #ifdef ENABLE_DCO
4834  struct gc_arena gc = gc_new();
4835  msg(flags, "DCO version: %s", dco_version_string(&gc));
4836  gc_free(&gc);
4837 #endif
4838 }
4839 
4840 void
4841 show_library_versions(const unsigned int flags)
4842 {
4843 #ifdef ENABLE_LZO
4844 #define LZO_LIB_VER_STR ", LZO ", lzo_version_string()
4845 #else
4846 #define LZO_LIB_VER_STR "", ""
4847 #endif
4848 
4849  msg(flags, "library versions: %s%s%s", get_ssl_library_version(),
4850  LZO_LIB_VER_STR);
4851 
4852 #undef LZO_LIB_VER_STR
4853 }
4854 
4855 static void
4857 {
4860 #ifdef _WIN32
4862 #endif
4864  msg(M_INFO|M_NOPREFIX, "Originally developed by James Yonan");
4865  msg(M_INFO|M_NOPREFIX, "Copyright (C) 2002-2023 OpenVPN Inc <sales@openvpn.net>");
4866 #ifndef ENABLE_SMALL
4867 #ifdef CONFIGURE_DEFINES
4868  msg(M_INFO|M_NOPREFIX, "Compile time defines: %s", CONFIGURE_DEFINES);
4869 #endif
4870 #ifdef CONFIGURE_SPECIAL_BUILD
4871  msg(M_INFO|M_NOPREFIX, "special build: %s", CONFIGURE_SPECIAL_BUILD);
4872 #endif
4873 #endif
4875 }
4876 
4877 void
4878 notnull(const char *arg, const char *description)
4879 {
4880  if (!arg)
4881  {
4882  msg(M_USAGE, "You must define %s", description);
4883  }
4884 }
4885 
4886 bool
4887 string_defined_equal(const char *s1, const char *s2)
4888 {
4889  if (s1 && s2)
4890  {
4891  return !strcmp(s1, s2);
4892  }
4893  else
4894  {
4895  return false;
4896  }
4897 }
4898 
4899 #if 0
4900 static void
4901 ping_rec_err(int msglevel)
4902 {
4903  msg(msglevel, "only one of --ping-exit or --ping-restart options may be specified");
4904 }
4905 #endif
4906 
4907 static int
4908 positive_atoi(const char *str)
4909 {
4910  const int i = atoi(str);
4911  return i < 0 ? 0 : i;
4912 }
4913 
4914 #ifdef _WIN32 /* This function is only used when compiling on Windows */
4915 static unsigned int
4916 atou(const char *str)
4917 {
4918  unsigned int val = 0;
4919  sscanf(str, "%u", &val);
4920  return val;
4921 }
4922 #endif
4923 
4924 static inline bool
4925 space(unsigned char c)
4926 {
4927  return c == '\0' || isspace(c);
4928 }
4929 
4930 int
4931 parse_line(const char *line,
4932  char *p[],
4933  const int n,
4934  const char *file,
4935  const int line_num,
4936  int msglevel,
4937  struct gc_arena *gc)
4938 {
4939  const int STATE_INITIAL = 0;
4940  const int STATE_READING_QUOTED_PARM = 1;
4941  const int STATE_READING_UNQUOTED_PARM = 2;
4942  const int STATE_DONE = 3;
4943  const int STATE_READING_SQUOTED_PARM = 4;
4944 
4945  const char *error_prefix = "";
4946 
4947  int ret = 0;
4948  const char *c = line;
4949  int state = STATE_INITIAL;
4950  bool backslash = false;
4951  char in, out;
4952 
4953  char parm[OPTION_PARM_SIZE];
4954  unsigned int parm_len = 0;
4955 
4956  msglevel &= ~M_OPTERR;
4957 
4958  if (msglevel & M_MSG_VIRT_OUT)
4959  {
4960  error_prefix = "ERROR: ";
4961  }
4962 
4963  do
4964  {
4965  in = *c;
4966  out = 0;
4967 
4968  if (!backslash && in == '\\' && state != STATE_READING_SQUOTED_PARM)
4969  {
4970  backslash = true;
4971  }
4972  else
4973  {
4974  if (state == STATE_INITIAL)
4975  {
4976  if (!space(in))
4977  {
4978  if (in == ';' || in == '#') /* comment */
4979  {
4980  break;
4981  }
4982  if (!backslash && in == '\"')
4983  {
4984  state = STATE_READING_QUOTED_PARM;
4985  }
4986  else if (!backslash && in == '\'')
4987  {
4988  state = STATE_READING_SQUOTED_PARM;
4989  }
4990  else
4991  {
4992  out = in;
4993  state = STATE_READING_UNQUOTED_PARM;
4994  }
4995  }
4996  }
4997  else if (state == STATE_READING_UNQUOTED_PARM)
4998  {
4999  if (!backslash && space(in))
5000  {
5001  state = STATE_DONE;
5002  }
5003  else
5004  {
5005  out = in;
5006  }
5007  }
5008  else if (state == STATE_READING_QUOTED_PARM)
5009  {
5010  if (!backslash && in == '\"')
5011  {
5012  state = STATE_DONE;
5013  }
5014  else
5015  {
5016  out = in;
5017  }
5018  }
5019  else if (state == STATE_READING_SQUOTED_PARM)
5020  {
5021  if (in == '\'')
5022  {
5023  state = STATE_DONE;
5024  }
5025  else
5026  {
5027  out = in;
5028  }
5029  }
5030  if (state == STATE_DONE)
5031  {
5032  /* ASSERT (parm_len > 0); */
5033  p[ret] = gc_malloc(parm_len + 1, true, gc);
5034  memcpy(p[ret], parm, parm_len);
5035  p[ret][parm_len] = '\0';
5036  state = STATE_INITIAL;
5037  parm_len = 0;
5038  ++ret;
5039  }
5040 
5041  if (backslash && out)
5042  {
5043  if (!(out == '\\' || out == '\"' || space(out)))
5044  {
5045 #ifdef ENABLE_SMALL
5046  msg(msglevel, "%sOptions warning: Bad backslash ('\\') usage in %s:%d", error_prefix, file, line_num);
5047 #else
5048  msg(msglevel, "%sOptions warning: Bad backslash ('\\') usage in %s:%d: remember that backslashes are treated as shell-escapes and if you need to pass backslash characters as part of a Windows filename, you should use double backslashes such as \"c:\\\\" PACKAGE "\\\\static.key\"", error_prefix, file, line_num);
5049 #endif
5050  return 0;
5051  }
5052  }
5053  backslash = false;
5054  }
5055 
5056  /* store parameter character */
5057  if (out)
5058  {
5059  if (parm_len >= SIZE(parm))
5060  {
5061  parm[SIZE(parm) - 1] = 0;
5062  msg(msglevel, "%sOptions error: Parameter at %s:%d is too long (%d chars max): %s",
5063  error_prefix, file, line_num, (int) SIZE(parm), parm);
5064  return 0;
5065  }
5066  parm[parm_len++] = out;
5067  }
5068 
5069  /* avoid overflow if too many parms in one config file line */
5070  if (ret >= n)
5071  {
5072  break;
5073  }
5074 
5075  } while (*c++ != '\0');
5076 
5077  if (state == STATE_READING_QUOTED_PARM)
5078  {
5079  msg(msglevel, "%sOptions error: No closing quotation (\") in %s:%d", error_prefix, file, line_num);
5080  return 0;
5081  }
5082  if (state == STATE_READING_SQUOTED_PARM)
5083  {
5084  msg(msglevel, "%sOptions error: No closing single quotation (\') in %s:%d", error_prefix, file, line_num);
5085  return 0;
5086  }
5087  if (state != STATE_INITIAL)
5088  {
5089  msg(msglevel, "%sOptions error: Residual parse state (%d) in %s:%d", error_prefix, state, file, line_num);
5090  return 0;
5091  }
5092 #if 0
5093  {
5094  int i;
5095  for (i = 0; i < ret; ++i)
5096  {
5097  msg(M_INFO|M_NOPREFIX, "%s:%d ARG[%d] '%s'", file, line_num, i, p[i]);
5098  }
5099  }
5100 #endif
5101  return ret;
5102 }
5103 
5104 static void
5106 {
5107  if (strlen(*p) >= 3 && !strncmp(*p, "--", 2))
5108  {
5109  *p += 2;
5110  }
5111 }
5112 
5113 struct in_src {
5114 #define IS_TYPE_FP 1
5115 #define IS_TYPE_BUF 2
5116  int type;
5117  union {
5118  FILE *fp;
5120  } u;
5121 };
5122 
5123 static bool
5124 in_src_get(const struct in_src *is, char *line, const int size)
5125 {
5126  if (is->type == IS_TYPE_FP)
5127  {
5128  return BOOL_CAST(fgets(line, size, is->u.fp));
5129  }
5130  else if (is->type == IS_TYPE_BUF)
5131  {
5132  bool status = buf_parse(is->u.multiline, '\n', line, size);
5133  if ((int) strlen(line) + 1 < size)
5134  {
5135  strcat(line, "\n");
5136  }
5137  return status;
5138  }
5139  else
5140  {
5141  ASSERT(0);
5142  return false;
5143  }
5144 }
5145 
5146 static char *
5147 read_inline_file(struct in_src *is, const char *close_tag,
5148  int *num_lines, struct gc_arena *gc)
5149 {
5150  char line[OPTION_LINE_SIZE];
5151  struct buffer buf = alloc_buf(8*OPTION_LINE_SIZE);
5152  char *ret;
5153  bool endtagfound = false;
5154 
5155  while (in_src_get(is, line, sizeof(line)))
5156  {
5157  (*num_lines)++;
5158  char *line_ptr = line;
5159  /* Remove leading spaces */
5160  while (isspace(*line_ptr))
5161  {
5162  line_ptr++;
5163  }
5164  if (!strncmp(line_ptr, close_tag, strlen(close_tag)))
5165  {
5166  endtagfound = true;
5167  break;
5168  }
5169  if (!buf_safe(&buf, strlen(line)+1))
5170  {
5171  /* Increase buffer size */
5172  struct buffer buf2 = alloc_buf(buf.capacity * 2);
5173  ASSERT(buf_copy(&buf2, &buf));
5174  buf_clear(&buf);
5175  free_buf(&buf);
5176  buf = buf2;
5177  }
5178  buf_printf(&buf, "%s", line);
5179  }
5180  if (!endtagfound)
5181  {
5182  msg(M_FATAL, "ERROR: Endtag %s missing", close_tag);
5183  }
5184  ret = string_alloc(BSTR(&buf), gc);
5185  buf_clear(&buf);
5186  free_buf(&buf);
5187  secure_memzero(line, sizeof(line));
5188  return ret;
5189 }
5190 
5191 static int
5192 check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc)
5193 {
5194  int num_inline_lines = 0;
5195 
5196  if (p[0] && !p[1])
5197  {
5198  char *arg = p[0];
5199  if (arg[0] == '<' && arg[strlen(arg)-1] == '>')
5200  {
5201  struct buffer close_tag;
5202 
5203  arg[strlen(arg) - 1] = '\0';
5204  p[0] = string_alloc(arg + 1, gc);
5205  close_tag = alloc_buf(strlen(p[0]) + 4);
5206  buf_printf(&close_tag, "</%s>", p[0]);
5207  p[1] = read_inline_file(is, BSTR(&close_tag), &num_inline_lines, gc);
5208  p[2] = NULL;
5209  free_buf(&close_tag);
5210  }
5211  }
5212  return num_inline_lines;
5213 }
5214 
5215 static int
5216 check_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc)
5217 {
5218  struct in_src is;
5219  is.type = IS_TYPE_FP;
5220  is.u.fp = fp;
5221  return check_inline_file(&is, p, gc);
5222 }
5223 
5224 static int
5226  struct gc_arena *gc)
5227 {
5228  struct in_src is;
5229  is.type = IS_TYPE_BUF;
5230  is.u.multiline = multiline;
5231  return check_inline_file(&is, p, gc);
5232 }
5233 
5234 static void
5235 add_option(struct options *options,
5236  char *p[],
5237  bool is_inline,
5238  const char *file,
5239  int line,
5240  const int level,
5241  const int msglevel,
5242  const unsigned int permission_mask,
5243  unsigned int *option_types_found,
5244  struct env_set *es);
5245 
5246 static void
5248  const char *file,
5249  int level,
5250  const char *top_file,
5251  const int top_line,
5252  const int msglevel,
5253  const unsigned int permission_mask,
5254  unsigned int *option_types_found,
5255  struct env_set *es)
5256 {
5257  const int max_recursive_levels = 10;
5258  FILE *fp;
5259  int line_num;
5260  char line[OPTION_LINE_SIZE+1];
5261  char *p[MAX_PARMS+1];
5262 
5263  ++level;
5264  if (level <= max_recursive_levels)
5265  {
5266  if (streq(file, "stdin"))
5267  {
5268  fp = stdin;
5269  }
5270  else
5271  {
5272  fp = platform_fopen(file, "r");
5273  }
5274  if (fp)
5275  {
5276  line_num = 0;
5277  while (fgets(line, sizeof(line), fp))
5278  {
5279  int offset = 0;
5280  CLEAR(p);
5281  ++line_num;
5282  if (strlen(line) == OPTION_LINE_SIZE)
5283  {
5284  msg(msglevel, "In %s:%d: Maximum option line length (%d) exceeded, line starts with %s",
5285  file, line_num, OPTION_LINE_SIZE, line);
5286  }
5287 
5288  /* Ignore UTF-8 BOM at start of stream */
5289  if (line_num == 1 && strncmp(line, "\xEF\xBB\xBF", 3) == 0)
5290  {
5291  offset = 3;
5292  }
5293  if (parse_line(line + offset, p, SIZE(p)-1, file, line_num, msglevel, &options->gc))
5294  {
5295  bypass_doubledash(&p[0]);
5296  int lines_inline = check_inline_file_via_fp(fp, p, &options->gc);
5297  add_option(options, p, lines_inline, file, line_num, level,
5298  msglevel, permission_mask, option_types_found,
5299  es);
5300  line_num += lines_inline;
5301  }
5302  }
5303  if (fp != stdin)
5304  {
5305  fclose(fp);
5306  }
5307  }
5308  else
5309  {
5310  msg(msglevel, "In %s:%d: Error opening configuration file: %s", top_file, top_line, file);
5311  }
5312  }
5313  else
5314  {
5315  msg(msglevel, "In %s:%d: Maximum recursive include levels exceeded in include attempt of file %s -- probably you have a configuration file that tries to include itself.", top_file, top_line, file);
5316  }
5317  secure_memzero(line, sizeof(line));
5318  CLEAR(p);
5319 }
5320 
5321 static void
5322 read_config_string(const char *prefix,
5323  struct options *options,
5324  const char *config,
5325  const int msglevel,
5326  const unsigned int permission_mask,
5327  unsigned int *option_types_found,
5328  struct env_set *es)
5329 {
5330  char line[OPTION_LINE_SIZE];
5331  struct buffer multiline;
5332  int line_num = 0;
5333 
5334  buf_set_read(&multiline, (uint8_t *)config, strlen(config));
5335 
5336  while (buf_parse(&multiline, '\n', line, sizeof(line)))
5337  {
5338  char *p[MAX_PARMS+1];
5339  CLEAR(p);
5340  ++line_num;
5341  if (parse_line(line, p, SIZE(p)-1, prefix, line_num, msglevel, &options->gc))
5342  {
5343  bypass_doubledash(&p[0]);
5344  int lines_inline = check_inline_file_via_buf(&multiline, p, &options->gc);
5345  add_option(options, p, lines_inline, prefix, line_num, 0, msglevel,
5346  permission_mask, option_types_found, es);
5347  line_num += lines_inline;
5348  }
5349  CLEAR(p);
5350  }
5351  secure_memzero(line, sizeof(line));
5352 }
5353 
5354 void
5356  const int argc,
5357  char *argv[],
5358  const int msglevel,
5359  const unsigned int permission_mask,
5360  unsigned int *option_types_found,
5361  struct env_set *es)
5362 {
5363  /* usage message */
5364  if (argc <= 1)
5365  {
5366  usage();
5367  }
5368 
5369  /* config filename specified only? */
5370  if (argc == 2 && strncmp(argv[1], "--", 2))
5371  {
5372  char *p[MAX_PARMS+1];
5373  CLEAR(p);
5374  p[0] = "config";
5375  p[1] = argv[1];
5376  add_option(options, p, false, NULL, 0, 0, msglevel, permission_mask,
5377  option_types_found, es);
5378  }
5379  else
5380  {
5381  /* parse command line */
5382  for (int i = 1; i < argc; ++i)
5383  {
5384  char *p[MAX_PARMS+1];
5385  CLEAR(p);
5386  p[0] = argv[i];
5387  if (strncmp(p[0], "--", 2))
5388  {
5389  msg(msglevel, "I'm trying to parse \"%s\" as an --option parameter but I don't see a leading '--'", p[0]);
5390  }
5391  else
5392  {
5393  p[0] += 2;
5394  }
5395 
5396  int j;
5397  for (j = 1; j < MAX_PARMS; ++j)
5398  {
5399  if (i + j < argc)
5400  {
5401  char *arg = argv[i + j];
5402  if (strncmp(arg, "--", 2))
5403  {
5404  p[j] = arg;
5405  }
5406  else
5407  {
5408  break;
5409  }
5410  }
5411  }
5412  add_option(options, p, false, NULL, 0, 0, msglevel, permission_mask,
5413  option_types_found, es);
5414  i += j - 1;
5415  }
5416  }
5417 }
5418 
5427 static bool
5428 apply_pull_filter(const struct options *o, char *line)
5429 {
5430  struct pull_filter *f;
5431 
5432  if (!o->pull_filter_list)
5433  {
5434  return true;
5435  }
5436 
5437  /* skip leading spaces matching the behaviour of parse_line */
5438  while (isspace(*line))
5439  {
5440  line++;
5441  }
5442 
5443  for (f = o->pull_filter_list->head; f; f = f->next)
5444  {
5445  if (f->type == PUF_TYPE_ACCEPT && strncmp(line, f->pattern, f->size) == 0)
5446  {
5447  msg(D_LOW, "Pushed option accepted by filter: '%s'", line);
5448  return true;
5449  }
5450  else if (f->type == PUF_TYPE_IGNORE && strncmp(line, f->pattern, f->size) == 0)
5451  {
5452  msg(D_PUSH, "Pushed option removed by filter: '%s'", line);
5453  *line = '\0';
5454  return true;
5455  }
5456  else if (f->type == PUF_TYPE_REJECT && strncmp(line, f->pattern, f->size) == 0)
5457  {
5458  msg(M_WARN, "Pushed option rejected by filter: '%s'. Restarting.", line);
5459  *line = '\0';
5460  throw_signal_soft(SIGUSR1, "Offending option received from server");
5461  return false;
5462  }
5463  }
5464  return true;
5465 }
5466 
5467 bool
5469  struct buffer *buf,
5470  unsigned int permission_mask,
5471  unsigned int *option_types_found,
5472  struct env_set *es)
5473 {
5474  char line[OPTION_PARM_SIZE];
5475  int line_num = 0;
5476  const char *file = "[PUSH-OPTIONS]";
5477  const int msglevel = D_PUSH_ERRORS|M_OPTERR;
5478 
5479  while (buf_parse(buf, ',', line, sizeof(line)))
5480  {
5481  char *p[MAX_PARMS+1];
5482  CLEAR(p);
5483  ++line_num;
5484  if (!apply_pull_filter(options, line))
5485  {
5486  return false; /* Cause push/pull error and stop push processing */
5487  }
5488  if (parse_line(line, p, SIZE(p)-1, file, line_num, msglevel, &options->gc))
5489  {
5490  add_option(options, p, false, file, line_num, 0, msglevel,
5491  permission_mask, option_types_found, es);
5492  }
5493  }
5494  return true;
5495 }
5496 
5497 void
5499  const char *filename,
5500  int msglevel,
5501  unsigned int permission_mask,
5502  unsigned int *option_types_found,
5503  struct env_set *es)
5504 {
5505  msg(D_PUSH, "OPTIONS IMPORT: reading client specific options from: %s", filename);
5506  read_config_file(o,
5507  filename,
5508  0,
5509  filename,
5510  0,
5511  msglevel,
5512  permission_mask,
5513  option_types_found,
5514  es);
5515 }
5516 
5517 void
5519  const char *config,
5520  const int msglevel,
5521  const unsigned int permission_mask,
5522  unsigned int *option_types_found,
5523  struct env_set *es)
5524 {
5525  read_config_string("[CONFIG-STRING]", options, config, msglevel, permission_mask, option_types_found, es);
5526 }
5527 
5528 #define VERIFY_PERMISSION(mask) { \
5529  if (!verify_permission(p[0], file, line, (mask), permission_mask, \