]> git.ipfire.org Git - thirdparty/openvpn.git/blob - src/openvpn/options.c
Push server mtu to client when supported and support occ mtu
[thirdparty/openvpn.git] / src / openvpn / options.c
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-2022 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2008-2022 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 #elif defined(_MSC_VER)
33 #include "config-msvc.h"
34 #endif
35 #ifdef HAVE_CONFIG_VERSION_H
36 #include "config-version.h"
37 #endif
38
39 #include "syshead.h"
40
41 #include "buffer.h"
42 #include "error.h"
43 #include "common.h"
44 #include "run_command.h"
45 #include "shaper.h"
46 #include "crypto.h"
47 #include "ssl.h"
48 #include "ssl_ncp.h"
49 #include "options.h"
50 #include "misc.h"
51 #include "socket.h"
52 #include "packet_id.h"
53 #include "pkcs11.h"
54 #include "win32.h"
55 #include "push.h"
56 #include "pool.h"
57 #include "proto.h"
58 #include "helper.h"
59 #include "manage.h"
60 #include "forward.h"
61 #include "ssl_verify.h"
62 #include "platform.h"
63 #include "xkey_common.h"
64 #include "dco.h"
65 #include <ctype.h>
66
67 #include "memdbg.h"
68
69 const char title_string[] =
70 PACKAGE_STRING
71 #ifdef CONFIGURE_GIT_REVISION
72 " [git:" CONFIGURE_GIT_REVISION CONFIGURE_GIT_FLAGS "]"
73 #endif
74 " " TARGET_ALIAS
75 #if defined(ENABLE_CRYPTO_MBEDTLS)
76 " [SSL (mbed TLS)]"
77 #elif defined(ENABLE_CRYPTO_OPENSSL)
78 " [SSL (OpenSSL)]"
79 #else
80 " [SSL]"
81 #endif /* defined(ENABLE_CRYPTO_MBEDTLS) */
82 #ifdef USE_COMP
83 #ifdef ENABLE_LZO
84 " [LZO]"
85 #endif
86 #ifdef ENABLE_LZ4
87 " [LZ4]"
88 #endif
89 #ifdef ENABLE_COMP_STUB
90 " [COMP_STUB]"
91 #endif
92 #endif /* USE_COMP */
93 #if EPOLL
94 " [EPOLL]"
95 #endif
96 #ifdef PRODUCT_TAP_DEBUG
97 " [TAPDBG]"
98 #endif
99 #ifdef ENABLE_PKCS11
100 " [PKCS11]"
101 #endif
102 #if ENABLE_IP_PKTINFO
103 #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
104 " [MH/PKTINFO]"
105 #elif defined(IP_RECVDSTADDR)
106 " [MH/RECVDA]"
107 #endif
108 #endif
109 " [AEAD]"
110 #ifdef ENABLE_DCO
111 " [DCO]"
112 #endif
113 #ifdef CONFIGURE_GIT_REVISION
114 " built on " __DATE__
115 #endif
116 ;
117
118 #ifndef ENABLE_SMALL
119
120 static const char usage_message[] =
121 "%s\n"
122 "\n"
123 "General Options:\n"
124 "--config file : Read configuration options from file.\n"
125 "--help : Show options.\n"
126 "--version : Show copyright and version information.\n"
127 "\n"
128 "Tunnel Options:\n"
129 "--local host : Local host name or ip address. Implies --bind.\n"
130 "--remote host [port] : Remote host name or ip address.\n"
131 "--remote-random : If multiple --remote options specified, choose one randomly.\n"
132 "--remote-random-hostname : Add a random string to remote DNS name.\n"
133 "--mode m : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
134 "--proto p : Use protocol p for communicating with peer.\n"
135 " p = udp (default), tcp-server, tcp-client\n"
136 " udp4, tcp4-server, tcp4-client\n"
137 " udp6, tcp6-server, tcp6-client\n"
138 "--proto-force p : only consider protocol p in list of connection profiles.\n"
139 " p = udp or tcp\n"
140 "--connect-retry n [m] : For client, number of seconds to wait between\n"
141 " connection retries (default=%d). On repeated retries\n"
142 " the wait time is exponentially increased to a maximum of m\n"
143 " (default=%d).\n"
144 "--connect-retry-max n : Maximum connection attempt retries, default infinite.\n"
145 "--http-proxy s p [up] [auth] : Connect to remote host\n"
146 " through an HTTP proxy at address s and port p.\n"
147 " If proxy authentication is required,\n"
148 " up is a file containing username/password on 2 lines, or\n"
149 " 'stdin' to prompt from console. Add auth='ntlm' if\n"
150 " the proxy requires NTLM authentication.\n"
151 "--http-proxy s p 'auto[-nct]' : Like the above directive, but automatically\n"
152 " determine auth method and query for username/password\n"
153 " if needed. auto-nct disables weak proxy auth methods.\n"
154 "--http-proxy-option type [parm] : Set extended HTTP proxy options.\n"
155 " Repeat to set multiple options.\n"
156 " VERSION version (default=1.0)\n"
157 " AGENT user-agent\n"
158 "--socks-proxy s [p] [up] : Connect to remote host through a Socks5 proxy at\n"
159 " address s and port p (default port = 1080).\n"
160 " If proxy authentication is required,\n"
161 " up is a file containing username/password on 2 lines, or\n"
162 " 'stdin' to prompt for console.\n"
163 "--socks-proxy-retry : Retry indefinitely on Socks proxy errors.\n"
164 "--resolv-retry n: If hostname resolve fails for --remote, retry\n"
165 " resolve for n seconds before failing (disabled by default).\n"
166 " Set n=\"infinite\" to retry indefinitely.\n"
167 "--float : Allow remote to change its IP address/port, such as through\n"
168 " DHCP (this is the default if --remote is not used).\n"
169 "--ipchange cmd : Run command cmd on remote ip address initial\n"
170 " setting or change -- execute as: cmd ip-address port#\n"
171 "--port port : TCP/UDP port # for both local and remote.\n"
172 "--lport port : TCP/UDP port # for local (default=%s). Implies --bind.\n"
173 "--rport port : TCP/UDP port # for remote (default=%s).\n"
174 "--bind : Bind to local address and port. (This is the default unless\n"
175 " --proto tcp-client"
176 " or --http-proxy"
177 " or --socks-proxy"
178 " is used).\n"
179 "--nobind : Do not bind to local address and port.\n"
180 "--dev tunX|tapX : tun/tap device (X can be omitted for dynamic device.\n"
181 "--dev-type dt : Which device type are we using? (dt = tun or tap) Use\n"
182 " this option only if the tun/tap device used with --dev\n"
183 " does not begin with \"tun\" or \"tap\".\n"
184 "--dev-node node : Explicitly set the device node rather than using\n"
185 " /dev/net/tun, /dev/tun, /dev/tap, etc.\n"
186 #if defined(ENABLE_DCO)
187 "--disable-dco : Do not attempt using Data Channel Offload.\n"
188 #endif
189 "--lladdr hw : Set the link layer address of the tap device.\n"
190 "--topology t : Set --dev tun topology: 'net30', 'p2p', or 'subnet'.\n"
191 #ifdef ENABLE_IPROUTE
192 "--iproute cmd : Use this command instead of default " IPROUTE_PATH ".\n"
193 #endif
194 "--ifconfig l rn : TUN: configure device to use IP address l as a local\n"
195 " endpoint and rn as a remote endpoint. l & rn should be\n"
196 " swapped on the other peer. l & rn must be private\n"
197 " addresses outside of the subnets used by either peer.\n"
198 " TAP: configure device to use IP address l as a local\n"
199 " endpoint and rn as a subnet mask.\n"
200 "--ifconfig-ipv6 l r : configure device to use IPv6 address l as local\n"
201 " endpoint (as a /64) and r as remote endpoint\n"
202 "--ifconfig-noexec : Don't actually execute ifconfig/netsh command, instead\n"
203 " pass --ifconfig parms by environment to scripts.\n"
204 "--ifconfig-nowarn : Don't warn if the --ifconfig option on this side of the\n"
205 " connection doesn't match the remote side.\n"
206 "--route network [netmask] [gateway] [metric] :\n"
207 " Add route to routing table after connection\n"
208 " is established. Multiple routes can be specified.\n"
209 " netmask default: 255.255.255.255\n"
210 " gateway default: taken from --route-gateway or --ifconfig\n"
211 " Specify default by leaving blank or setting to \"default\".\n"
212 "--route-ipv6 network/bits [gateway] [metric] :\n"
213 " Add IPv6 route to routing table after connection\n"
214 " is established. Multiple routes can be specified.\n"
215 " gateway default: taken from --route-ipv6-gateway or 'remote'\n"
216 " in --ifconfig-ipv6\n"
217 "--route-gateway gw|'dhcp' : Specify a default gateway for use with --route.\n"
218 "--route-ipv6-gateway gw : Specify a default gateway for use with --route-ipv6.\n"
219 "--route-metric m : Specify a default metric for use with --route.\n"
220 "--route-delay n [w] : Delay n seconds after connection initiation before\n"
221 " adding routes (may be 0). If not specified, routes will\n"
222 " be added immediately after tun/tap open. On Windows, wait\n"
223 " up to w seconds for TUN/TAP adapter to come up.\n"
224 "--route-up cmd : Run command cmd after routes are added.\n"
225 "--route-pre-down cmd : Run command cmd before routes are removed.\n"
226 "--route-noexec : Don't add routes automatically. Instead pass routes to\n"
227 " --route-up script using environmental variables.\n"
228 "--route-nopull : When used with --client or --pull, accept options pushed\n"
229 " by server EXCEPT for routes, dns, and dhcp options.\n"
230 "--allow-pull-fqdn : Allow client to pull DNS names from server for\n"
231 " --ifconfig, --route, and --route-gateway.\n"
232 "--redirect-gateway [flags]: Automatically execute routing\n"
233 " commands to redirect all outgoing IP traffic through the\n"
234 " VPN. Add 'local' flag if both " PACKAGE_NAME " servers are directly\n"
235 " connected via a common subnet, such as with WiFi.\n"
236 " Add 'def1' flag to set default route using using 0.0.0.0/1\n"
237 " and 128.0.0.0/1 rather than 0.0.0.0/0. Add 'bypass-dhcp'\n"
238 " flag to add a direct route to DHCP server, bypassing tunnel.\n"
239 " Add 'bypass-dns' flag to similarly bypass tunnel for DNS.\n"
240 "--redirect-private [flags]: Like --redirect-gateway, but omit actually changing\n"
241 " the default gateway. Useful when pushing private subnets.\n"
242 "--block-ipv6 : (Client) Instead sending IPv6 to the server generate\n"
243 " ICMPv6 host unreachable messages on the client.\n"
244 " (Server) Instead of forwarding IPv6 packets send\n"
245 " ICMPv6 host unreachable packets to the client.\n"
246 "--client-nat snat|dnat network netmask alias : on client add 1-to-1 NAT rule.\n"
247 "--push-peer-info : (client only) push client info to server.\n"
248 "--setenv name value : Set a custom environmental variable to pass to script.\n"
249 "--setenv FORWARD_COMPATIBLE 1 : Relax config file syntax checking to allow\n"
250 " directives for future OpenVPN versions to be ignored.\n"
251 "--ignore-unkown-option opt1 opt2 ...: Relax config file syntax. Allow\n"
252 " these options to be ignored when unknown\n"
253 "--script-security level: Where level can be:\n"
254 " 0 -- strictly no calling of external programs\n"
255 " 1 -- (default) only call built-ins such as ifconfig\n"
256 " 2 -- allow calling of built-ins and scripts\n"
257 " 3 -- allow password to be passed to scripts via env\n"
258 "--shaper n : Restrict output to peer to n bytes per second.\n"
259 "--keepalive n m : Helper option for setting timeouts in server mode. Send\n"
260 " ping once every n seconds, restart if ping not received\n"
261 " for m seconds.\n"
262 "--inactive n [bytes] : Exit after n seconds of activity on tun/tap device\n"
263 " produces a combined in/out byte count < bytes.\n"
264 "--session-timeout n: Limit connection time to n seconds.\n"
265 "--ping-exit n : Exit if n seconds pass without reception of remote ping.\n"
266 "--ping-restart n: Restart if n seconds pass without reception of remote ping.\n"
267 "--ping-timer-rem: Run the --ping-exit/--ping-restart timer only if we have a\n"
268 " remote address.\n"
269 "--ping n : Ping remote once every n seconds over TCP/UDP port.\n"
270 #if ENABLE_IP_PKTINFO
271 "--multihome : Configure a multi-homed UDP server.\n"
272 #endif
273 "--fast-io : (experimental) Optimize TUN/TAP/UDP writes.\n"
274 "--remap-usr1 s : On SIGUSR1 signals, remap signal (s='SIGHUP' or 'SIGTERM').\n"
275 "--persist-tun : Keep tun/tap device open across SIGUSR1 or --ping-restart.\n"
276 "--persist-remote-ip : Keep remote IP address across SIGUSR1 or --ping-restart.\n"
277 "--persist-local-ip : Keep local IP address across SIGUSR1 or --ping-restart.\n"
278 "--persist-key : Don't re-read key files across SIGUSR1 or --ping-restart.\n"
279 #if PASSTOS_CAPABILITY
280 "--passtos : TOS passthrough (applies to IPv4 only).\n"
281 #endif
282 "--tun-mtu n : Take the tun/tap device MTU to be n and derive the\n"
283 " TCP/UDP MTU from it (default=%d).\n"
284 "--tun-mtu-extra n : Assume that tun/tap device might return as many\n"
285 " as n bytes more than the tun-mtu size on read\n"
286 " (default TUN=0 TAP=%d).\n"
287 "--link-mtu n : Take the TCP/UDP device MTU to be n and derive the tun MTU\n"
288 " from it.\n"
289 "--mtu-disc type : Should we do Path MTU discovery on TCP/UDP channel?\n"
290 " 'no' -- Never send DF (Don't Fragment) frames\n"
291 " 'maybe' -- Use per-route hints\n"
292 " 'yes' -- Always DF (Don't Fragment)\n"
293 "--mtu-test : Empirically measure and report MTU.\n"
294 #ifdef ENABLE_FRAGMENT
295 "--fragment max : Enable internal datagram fragmentation so that no UDP\n"
296 " datagrams are sent which are larger than max bytes.\n"
297 " Adds 4 bytes of overhead per datagram.\n"
298 #endif
299 "--mssfix [n] : Set upper bound on TCP MSS, default = tun-mtu size\n"
300 " or --fragment max value, whichever is lower.\n"
301 "--sndbuf size : Set the TCP/UDP send buffer size.\n"
302 "--rcvbuf size : Set the TCP/UDP receive buffer size.\n"
303 #if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK
304 "--mark value : Mark encrypted packets being sent with value. The mark value\n"
305 " can be matched in policy routing and packetfilter rules.\n"
306 "--bind-dev dev : Bind to the given device when making connection to a peer or\n"
307 " listening for connections. This allows sending encrypted packets\n"
308 " via a VRF present on the system.\n"
309 #endif
310 "--txqueuelen n : Set the tun/tap TX queue length to n (Linux only).\n"
311 #ifdef ENABLE_MEMSTATS
312 "--memstats file : Write live usage stats to memory mapped binary file.\n"
313 #endif
314 "--mlock : Disable Paging -- ensures key material and tunnel\n"
315 " data will never be written to disk.\n"
316 "--up cmd : Run command cmd after successful tun device open.\n"
317 " Execute as: cmd tun/tap-dev tun-mtu link-mtu \\\n"
318 " ifconfig-local-ip ifconfig-remote-ip\n"
319 " (pre --user or --group UID/GID change)\n"
320 "--up-delay : Delay tun/tap open and possible --up script execution\n"
321 " until after TCP/UDP connection establishment with peer.\n"
322 "--down cmd : Run command cmd after tun device close.\n"
323 " (post --user/--group UID/GID change and/or --chroot)\n"
324 " (command parameters are same as --up option)\n"
325 "--down-pre : Run --down command before TUN/TAP close.\n"
326 "--up-restart : Run up/down commands for all restarts including those\n"
327 " caused by --ping-restart or SIGUSR1\n"
328 "--user user : Set UID to user after initialization.\n"
329 "--group group : Set GID to group after initialization.\n"
330 "--chroot dir : Chroot to this directory after initialization.\n"
331 #ifdef ENABLE_SELINUX
332 "--setcon context: Apply this SELinux context after initialization.\n"
333 #endif
334 "--cd dir : Change to this directory before initialization.\n"
335 "--daemon [name] : Become a daemon after initialization.\n"
336 " The optional 'name' parameter will be passed\n"
337 " as the program name to the system logger.\n"
338 "--syslog [name] : Output to syslog, but do not become a daemon.\n"
339 " See --daemon above for a description of the 'name' parm.\n"
340 "--log file : Output log to file which is created/truncated on open.\n"
341 "--log-append file : Append log to file, or create file if nonexistent.\n"
342 "--suppress-timestamps : Don't log timestamps to stdout/stderr.\n"
343 "--machine-readable-output : Always log timestamp, message flags to stdout/stderr.\n"
344 "--writepid file : Write main process ID to file.\n"
345 "--nice n : Change process priority (>0 = lower, <0 = higher).\n"
346 "--echo [parms ...] : Echo parameters to log output.\n"
347 "--verb n : Set output verbosity to n (default=%d):\n"
348 " (Level 3 is recommended if you want a good summary\n"
349 " of what's happening without being swamped by output).\n"
350 " : 0 -- no output except fatal errors\n"
351 " : 1 -- startup info + connection initiated messages +\n"
352 " non-fatal encryption & net errors\n"
353 " : 2,3 -- show TLS negotiations & route info\n"
354 " : 4 -- show parameters\n"
355 " : 5 -- show 'RrWw' chars on console for each packet sent\n"
356 " and received from TCP/UDP (caps) or tun/tap (lc)\n"
357 " : 6 to 11 -- debug messages of increasing verbosity\n"
358 "--mute n : Log at most n consecutive messages in the same category.\n"
359 "--status file [n] : Write operational status to file every n seconds.\n"
360 "--status-version [n] : Choose the status file format version number.\n"
361 " Currently, n can be 1, 2, or 3 (default=1).\n"
362 "--disable-occ : Disable options consistency check between peers.\n"
363 #ifdef ENABLE_DEBUG
364 "--gremlin mask : Special stress testing mode (for debugging only).\n"
365 #endif
366 #if defined(USE_COMP)
367 "--compress alg : Use compression algorithm alg\n"
368 "--allow-compression: Specify whether compression should be allowed\n"
369 #if defined(ENABLE_LZO)
370 "--comp-lzo : Use LZO compression -- may add up to 1 byte per\n"
371 " packet for incompressible data.\n"
372 "--comp-noadapt : Don't use adaptive compression when --comp-lzo\n"
373 " is specified.\n"
374 #endif
375 #endif
376 #ifdef ENABLE_MANAGEMENT
377 "--management ip port [pass] : Enable a TCP server on ip:port to handle\n"
378 " management functions. pass is a password file\n"
379 " or 'stdin' to prompt from console.\n"
380 #if UNIX_SOCK_SUPPORT
381 " To listen on a unix domain socket, specific the pathname\n"
382 " in place of ip and use 'unix' as the port number.\n"
383 #endif
384 "--management-client : Management interface will connect as a TCP client to\n"
385 " ip/port rather than listen as a TCP server.\n"
386 "--management-query-passwords : Query management channel for private key\n"
387 " and auth-user-pass passwords.\n"
388 "--management-query-proxy : Query management channel for proxy information.\n"
389 "--management-query-remote : Query management channel for --remote directive.\n"
390 "--management-hold : Start " PACKAGE_NAME " in a hibernating state, until a client\n"
391 " of the management interface explicitly starts it.\n"
392 "--management-signal : Issue SIGUSR1 when management disconnect event occurs.\n"
393 "--management-forget-disconnect : Forget passwords when management disconnect\n"
394 " event occurs.\n"
395 "--management-up-down : Report tunnel up/down events to management interface.\n"
396 "--management-log-cache n : Cache n lines of log file history for usage\n"
397 " by the management channel.\n"
398 #if UNIX_SOCK_SUPPORT
399 "--management-client-user u : When management interface is a unix socket, only\n"
400 " allow connections from user u.\n"
401 "--management-client-group g : When management interface is a unix socket, only\n"
402 " allow connections from group g.\n"
403 #endif
404 "--management-client-auth : gives management interface client the responsibility\n"
405 " to authenticate clients after their client certificate\n"
406 " has been verified.\n"
407 #endif /* ifdef ENABLE_MANAGEMENT */
408 #ifdef ENABLE_PLUGIN
409 "--plugin m [str]: Load plug-in module m passing str as an argument\n"
410 " to its initialization function.\n"
411 #endif
412 "--vlan-tagging : Enable 802.1Q-based VLAN tagging.\n"
413 "--vlan-accept tagged|untagged|all : Set VLAN tagging mode. Default is 'all'.\n"
414 "--vlan-pvid v : Sets the Port VLAN Identifier. Defaults to 1.\n"
415 "\n"
416 "Multi-Client Server options (when --mode server is used):\n"
417 "--server network netmask : Helper option to easily configure server mode.\n"
418 "--server-ipv6 network/bits : Configure IPv6 server mode.\n"
419 "--server-bridge [IP netmask pool-start-IP pool-end-IP] : Helper option to\n"
420 " easily configure ethernet bridging server mode.\n"
421 "--push \"option\" : Push a config file option back to the peer for remote\n"
422 " execution. Peer must specify --pull in its config file.\n"
423 "--push-reset : Don't inherit global push list for specific\n"
424 " client instance.\n"
425 "--push-remove opt : Remove options matching 'opt' from the push list for\n"
426 " a specific client instance.\n"
427 "--ifconfig-pool start-IP end-IP [netmask] : Set aside a pool of subnets\n"
428 " to be dynamically allocated to connecting clients.\n"
429 "--ifconfig-pool-persist file [seconds] : Persist/unpersist ifconfig-pool\n"
430 " data to file, at seconds intervals (default=600).\n"
431 " If seconds=0, file will be treated as read-only.\n"
432 "--ifconfig-ipv6-pool base-IP/bits : set aside an IPv6 network block\n"
433 " to be dynamically allocated to connecting clients.\n"
434 "--ifconfig-push local remote-netmask : Push an ifconfig option to remote,\n"
435 " overrides --ifconfig-pool dynamic allocation.\n"
436 " Only valid in a client-specific config file.\n"
437 "--ifconfig-ipv6-push local/bits remote : Push an ifconfig-ipv6 option to\n"
438 " remote, overrides --ifconfig-ipv6-pool allocation.\n"
439 " Only valid in a client-specific config file.\n"
440 "--iroute network [netmask] : Route subnet to client.\n"
441 "--iroute-ipv6 network/bits : Route IPv6 subnet to client.\n"
442 " Sets up internal routes only.\n"
443 " Only valid in a client-specific config file.\n"
444 "--disable : Client is disabled.\n"
445 " Only valid in a client-specific config file.\n"
446 "--verify-client-cert [none|optional|require] : perform no, optional or\n"
447 " mandatory client certificate verification.\n"
448 " Default is to require the client to supply a certificate.\n"
449 "--username-as-common-name : For auth-user-pass authentication, use\n"
450 " the authenticated username as the common name,\n"
451 " rather than the common name from the client cert.\n"
452 "--auth-user-pass-verify cmd method: Query client for username/password and\n"
453 " run command cmd to verify. If method='via-env', pass\n"
454 " user/pass via environment, if method='via-file', pass\n"
455 " user/pass via temporary file.\n"
456 "--auth-gen-token [lifetime] Generate a random authentication token which is pushed\n"
457 " to each client, replacing the password. Useful when\n"
458 " OTP based two-factor auth mechanisms are in use and\n"
459 " --reneg-* options are enabled. Optionally a lifetime in seconds\n"
460 " for generated tokens can be set.\n"
461 "--opt-verify : Clients that connect with options that are incompatible\n"
462 " with those of the server will be disconnected.\n"
463 "--auth-user-pass-optional : Allow connections by clients that don't\n"
464 " specify a username/password.\n"
465 "--no-name-remapping : (DEPRECATED) Allow Common Name and X509 Subject to include\n"
466 " any printable character.\n"
467 "--client-to-client : Internally route client-to-client traffic.\n"
468 "--duplicate-cn : Allow multiple clients with the same common name to\n"
469 " concurrently connect.\n"
470 "--client-connect cmd : Run command cmd on client connection.\n"
471 "--client-disconnect cmd : Run command cmd on client disconnection.\n"
472 "--client-config-dir dir : Directory for custom client config files.\n"
473 "--ccd-exclusive : Refuse connection unless custom client config is found.\n"
474 "--tmp-dir dir : Temporary directory, used for --client-connect return file and plugin communication.\n"
475 "--hash-size r v : Set the size of the real address hash table to r and the\n"
476 " virtual address table to v.\n"
477 "--bcast-buffers n : Allocate n broadcast buffers.\n"
478 "--tcp-queue-limit n : Maximum number of queued TCP output packets.\n"
479 "--tcp-nodelay : Macro that sets TCP_NODELAY socket flag on the server\n"
480 " as well as pushes it to connecting clients.\n"
481 "--learn-address cmd : Run command cmd to validate client virtual addresses.\n"
482 "--connect-freq n s : Allow a maximum of n new connections per s seconds.\n"
483 "--max-clients n : Allow a maximum of n simultaneously connected clients.\n"
484 "--max-routes-per-client n : Allow a maximum of n internal routes per client.\n"
485 "--stale-routes-check n [t] : Remove routes with a last activity timestamp\n"
486 " older than n seconds. Run this check every t\n"
487 " seconds (defaults to n).\n"
488 "--explicit-exit-notify [n] : In UDP server mode send [RESTART] command on exit/restart to connected\n"
489 " clients. n = 1 - reconnect to same server,\n"
490 " 2 - advance to next server, default=1.\n"
491 #if PORT_SHARE
492 "--port-share host port [dir] : When run in TCP mode, proxy incoming HTTPS\n"
493 " sessions to a web server at host:port. dir specifies an\n"
494 " optional directory to write origin IP:port data.\n"
495 #endif
496 "\n"
497 "Client options (when connecting to a multi-client server):\n"
498 "--client : Helper option to easily configure client mode.\n"
499 "--auth-user-pass [up] : Authenticate with server using username/password.\n"
500 " up is a file containing the username on the first line,\n"
501 " and a password on the second. If either the password or both\n"
502 " the username and the password are omitted OpenVPN will prompt\n"
503 " for them from console.\n"
504 "--pull : Accept certain config file options from the peer as if they\n"
505 " were part of the local config file. Must be specified\n"
506 " when connecting to a '--mode server' remote host.\n"
507 "--pull-filter accept|ignore|reject t : Filter each option received from the\n"
508 " server if it starts with the text t. The action flag accept,\n"
509 " ignore or reject causes the option to be allowed, removed or\n"
510 " rejected with error. May be specified multiple times, and\n"
511 " each filter is applied in the order of appearance.\n"
512 "--dns server <n> <option> <value> [value ...] : Configure option for DNS server #n\n"
513 " Valid options are :\n"
514 " address <addr[:port]> [addr[:port]] : server address 4/6\n"
515 " resolve-domains <domain> [domain ...] : split domains\n"
516 " exclude-domains <domain> [domain ...] : domains not to resolve\n"
517 " dnssec <yes|no|optional> : option to use DNSSEC\n"
518 " type <DoH|DoT> : query server over HTTPS / TLS\n"
519 " sni <domain> : DNS server name indication\n"
520 "--dns search-domains <domain> [domain ...]:\n"
521 " Add domains to DNS domain search list\n"
522 "--auth-retry t : How to handle auth failures. Set t to\n"
523 " none (default), interact, or nointeract.\n"
524 "--static-challenge t e : Enable static challenge/response protocol using\n"
525 " challenge text t, with e indicating echo flag (0|1)\n"
526 "--connect-timeout n : when polling possible remote servers to connect to\n"
527 " in a round-robin fashion, spend no more than n seconds\n"
528 " waiting for a response before trying the next server.\n"
529 "--allow-recursive-routing : When this option is set, OpenVPN will not drop\n"
530 " incoming tun packets with same destination as host.\n"
531 "--explicit-exit-notify [n] : On exit/restart, send exit signal to\n"
532 " server/remote. n = # of retries, default=1.\n"
533 "\n"
534 "Data Channel Encryption Options (must be compatible between peers):\n"
535 "(These options are meaningful for both Static Key & TLS-mode)\n"
536 "--secret f [d] : (DEPRECATED) Enable Static Key encryption mode (non-TLS).\n"
537 " Use shared secret file f, generate with --genkey.\n"
538 " The optional d parameter controls key directionality.\n"
539 " If d is specified, use separate keys for each\n"
540 " direction, set d=0 on one side of the connection,\n"
541 " and d=1 on the other side.\n"
542 "--auth alg : Authenticate packets with HMAC using message\n"
543 " digest algorithm alg (default=%s).\n"
544 " (usually adds 16 or 20 bytes per packet)\n"
545 " Set alg=none to disable authentication.\n"
546 "--cipher alg : Encrypt packets with cipher algorithm alg\n"
547 " (default=%s).\n"
548 " Set alg=none to disable encryption.\n"
549 "--data-ciphers list : List of ciphers that are allowed to be negotiated.\n"
550 #ifndef ENABLE_CRYPTO_MBEDTLS
551 "--engine [name] : Enable OpenSSL hardware crypto engine functionality.\n"
552 #endif
553 "--no-replay : (DEPRECATED) Disable replay protection.\n"
554 "--mute-replay-warnings : Silence the output of replay warnings to log file.\n"
555 "--replay-window n [t] : Use a replay protection sliding window of size n\n"
556 " and a time window of t seconds.\n"
557 " Default n=%d t=%d\n"
558 "--replay-persist file : Persist replay-protection state across sessions\n"
559 " using file.\n"
560 "--test-crypto : Run a self-test of crypto features enabled.\n"
561 " For debugging only.\n"
562 #ifdef ENABLE_PREDICTION_RESISTANCE
563 "--use-prediction-resistance: Enable prediction resistance on the random\n"
564 " number generator.\n"
565 #endif
566 "\n"
567 "TLS Key Negotiation Options:\n"
568 "(These options are meaningful only for TLS-mode)\n"
569 "--tls-server : Enable TLS and assume server role during TLS handshake.\n"
570 "--tls-client : Enable TLS and assume client role during TLS handshake.\n"
571 "--key-method m : (DEPRECATED) Data channel key exchange method. m should be a method\n"
572 " number, such as 1 (default), 2, etc.\n"
573 "--ca file : Certificate authority file in .pem format containing\n"
574 " root certificate.\n"
575 #ifndef ENABLE_CRYPTO_MBEDTLS
576 "--capath dir : A directory of trusted certificates (CAs"
577 " and CRLs).\n"
578 #endif /* ENABLE_CRYPTO_MBEDTLS */
579 "--dh file : File containing Diffie Hellman parameters\n"
580 " in .pem format (for --tls-server only).\n"
581 " Use \"openssl dhparam -out dh1024.pem 1024\" to generate.\n"
582 "--cert file : Local certificate in .pem format -- must be signed\n"
583 " by a Certificate Authority in --ca file.\n"
584 "--extra-certs file : one or more PEM certs that complete the cert chain.\n"
585 "--key file : Local private key in .pem format.\n"
586 "--tls-version-min <version> ['or-highest'] : sets the minimum TLS version we\n"
587 " will accept from the peer. If version is unrecognized and 'or-highest'\n"
588 " is specified, require max TLS version supported by SSL implementation.\n"
589 "--tls-version-max <version> : sets the maximum TLS version we will use.\n"
590 #ifndef ENABLE_CRYPTO_MBEDTLS
591 "--pkcs12 file : PKCS#12 file containing local private key, local certificate\n"
592 " and optionally the root CA certificate.\n"
593 #endif
594 #ifdef ENABLE_X509ALTUSERNAME
595 "--x509-username-field : Field in x509 certificate containing the username.\n"
596 " Default is CN in the Subject field.\n"
597 #endif
598 "--verify-hash hash [algo] : Specify fingerprint for level-1 certificate.\n"
599 " Valid algo flags are SHA1 and SHA256. \n"
600 #ifdef _WIN32
601 "--cryptoapicert select-string : Load the certificate and private key from the\n"
602 " Windows Certificate System Store.\n"
603 #endif
604 "--tls-cipher l : A list l of allowable TLS ciphers separated by : (optional).\n"
605 "--tls-ciphersuites l: A list of allowed TLS 1.3 cipher suites seperated by : (optional)\n"
606 " : Use --show-tls to see a list of supported TLS ciphers (suites).\n"
607 "--tls-cert-profile p : Set the allowed certificate crypto algorithm profile\n"
608 " (default=legacy).\n"
609 "--providers l : A list l of OpenSSL providers to load.\n"
610 "--tls-timeout n : Packet retransmit timeout on TLS control channel\n"
611 " if no ACK from remote within n seconds (default=%d).\n"
612 "--reneg-bytes n : Renegotiate data chan. key after n bytes sent and recvd.\n"
613 "--reneg-pkts n : Renegotiate data chan. key after n packets sent and recvd.\n"
614 "--reneg-sec max [min] : Renegotiate data chan. key after at most max (default=%d)\n"
615 " and at least min (defaults to 90%% of max on servers and equal\n"
616 " to max on clients).\n"
617 "--hand-window n : Data channel key exchange must finalize within n seconds\n"
618 " of handshake initiation by any peer (default=%d).\n"
619 "--tran-window n : Transition window -- old key can live this many seconds\n"
620 " after new key renegotiation begins (default=%d).\n"
621 "--single-session: Allow only one session (reset state on restart).\n"
622 "--tls-exit : Exit on TLS negotiation failure.\n"
623 "--tls-auth f [d]: Add an additional layer of authentication on top of the TLS\n"
624 " control channel to protect against attacks on the TLS stack\n"
625 " and DoS attacks.\n"
626 " f (required) is a shared-secret key file.\n"
627 " The optional d parameter controls key directionality,\n"
628 " see --secret option for more info.\n"
629 "--tls-crypt key : Add an additional layer of authenticated encryption on top\n"
630 " of the TLS control channel to hide the TLS certificate,\n"
631 " provide basic post-quantum security and protect against\n"
632 " attacks on the TLS stack and DoS attacks.\n"
633 " key (required) provides the pre-shared key file.\n"
634 " see --secret option for more info.\n"
635 "--tls-crypt-v2 key : For clients: use key as a client-specific tls-crypt key.\n"
636 " For servers: use key to decrypt client-specific keys. For\n"
637 " key generation (--genkey tls-crypt-v2-client): use key to\n"
638 " encrypt generated client-specific key. (See --tls-crypt.)\n"
639 "--genkey tls-crypt-v2-client [keyfile] [base64 metadata]: Generate a\n"
640 " fresh tls-crypt-v2 client key, and store to\n"
641 " keyfile. If supplied, include metadata in wrapped key.\n"
642 "--genkey tls-crypt-v2-server [keyfile] [base64 metadata]: Generate a\n"
643 " fresh tls-crypt-v2 server key, and store to keyfile\n"
644 "--tls-crypt-v2-verify cmd : Run command cmd to verify the metadata of the\n"
645 " client-supplied tls-crypt-v2 client key\n"
646 "--askpass [file]: Get PEM password from controlling tty before we daemonize.\n"
647 "--auth-nocache : Don't cache --askpass or --auth-user-pass passwords.\n"
648 "--crl-verify crl ['dir']: Check peer certificate against a CRL.\n"
649 "--tls-verify cmd: Run command cmd to verify the X509 name of a\n"
650 " pending TLS connection that has otherwise passed all other\n"
651 " tests of certification. cmd should return 0 to allow\n"
652 " TLS handshake to proceed, or 1 to fail. (cmd is\n"
653 " executed as 'cmd certificate_depth subject')\n"
654 "--tls-export-cert [directory] : Get peer cert in PEM format and store it \n"
655 " in an openvpn temporary file in [directory]. Peer cert is \n"
656 " stored before tls-verify script execution and deleted after.\n"
657 "--verify-x509-name name: Accept connections only from a host with X509 subject\n"
658 " DN name. The remote host must also pass all other tests\n"
659 " of verification.\n"
660 "--ns-cert-type t: (DEPRECATED) Require that peer certificate was signed with \n"
661 " an explicit nsCertType designation t = 'client' | 'server'.\n"
662 "--x509-track x : Save peer X509 attribute x in environment for use by\n"
663 " plugins and management interface.\n"
664 #ifdef HAVE_EXPORT_KEYING_MATERIAL
665 "--keying-material-exporter label len : Save Exported Keying Material (RFC5705)\n"
666 " of len bytes (min. 16 bytes) using label in environment for use by plugins.\n"
667 #endif
668 "--remote-cert-ku v ... : Require that the peer certificate was signed with\n"
669 " explicit key usage, you can specify more than one value.\n"
670 " value should be given in hex format.\n"
671 "--remote-cert-eku oid : Require that the peer certificate was signed with\n"
672 " explicit extended key usage. Extended key usage can be encoded\n"
673 " as an object identifier or OpenSSL string representation.\n"
674 "--remote-cert-tls t: Require that peer certificate was signed with explicit\n"
675 " key usage and extended key usage based on RFC3280 TLS rules.\n"
676 " t = 'client' | 'server'.\n"
677 #ifdef ENABLE_PKCS11
678 "\n"
679 "PKCS#11 Options:\n"
680 "--pkcs11-providers provider ... : PKCS#11 provider to load.\n"
681 "--pkcs11-protected-authentication [0|1] ... : Use PKCS#11 protected authentication\n"
682 " path. Set for each provider.\n"
683 "--pkcs11-private-mode hex ... : PKCS#11 private key mode mask.\n"
684 " 0 : Try to determine automatically (default).\n"
685 " 1 : Use Sign.\n"
686 " 2 : Use SignRecover.\n"
687 " 4 : Use Decrypt.\n"
688 " 8 : Use Unwrap.\n"
689 "--pkcs11-cert-private [0|1] ... : Set if login should be performed before\n"
690 " certificate can be accessed. Set for each provider.\n"
691 "--pkcs11-pin-cache seconds : Number of seconds to cache PIN. The default is -1\n"
692 " cache until token is removed.\n"
693 "--pkcs11-id-management : Acquire identity from management interface.\n"
694 "--pkcs11-id serialized-id 'id' : Identity to use, get using standalone --show-pkcs11-ids\n"
695 #endif /* ENABLE_PKCS11 */
696 "\n"
697 "SSL Library information:\n"
698 "--show-ciphers : Show cipher algorithms to use with --cipher option.\n"
699 "--show-digests : Show message digest algorithms to use with --auth option.\n"
700 "--show-engines : Show hardware crypto accelerator engines (if available).\n"
701 "--show-tls : Show all TLS ciphers (TLS used only as a control channel).\n"
702 #ifdef _WIN32
703 "\n"
704 "Windows Specific:\n"
705 "--win-sys path : Pathname of Windows system directory. Default is the pathname\n"
706 " from SystemRoot environment variable.\n"
707 "--ip-win32 method : When using --ifconfig on Windows, set TAP-Windows adapter\n"
708 " IP address using method = manual, netsh, ipapi,\n"
709 " dynamic, or adaptive (default = adaptive).\n"
710 " Dynamic method allows two optional parameters:\n"
711 " offset: DHCP server address offset (> -256 and < 256).\n"
712 " If 0, use network address, if >0, take nth\n"
713 " address forward from network address, if <0,\n"
714 " take nth address backward from broadcast\n"
715 " address.\n"
716 " Default is 0.\n"
717 " lease-time: Lease time in seconds.\n"
718 " Default is one year.\n"
719 "--route-method : Which method to use for adding routes on Windows?\n"
720 " adaptive (default) -- Try ipapi then fall back to exe.\n"
721 " ipapi -- Use IP helper API.\n"
722 " exe -- Call the route.exe shell command.\n"
723 "--dhcp-option type [parm] : Set extended TAP-Windows properties, must\n"
724 " be used with --ip-win32 dynamic. For options\n"
725 " which allow multiple addresses,\n"
726 " --dhcp-option must be repeated.\n"
727 " DOMAIN name : Set DNS suffix\n"
728 " DOMAIN-SEARCH entry : Add entry to DNS domain search list\n"
729 " DNS addr : Set domain name server address(es) (IPv4 and IPv6)\n"
730 " NTP : Set NTP server address(es)\n"
731 " NBDD : Set NBDD server address(es)\n"
732 " WINS addr : Set WINS server address(es)\n"
733 " NBT type : Set NetBIOS over TCP/IP Node type\n"
734 " 1: B, 2: P, 4: M, 8: H\n"
735 " NBS id : Set NetBIOS scope ID\n"
736 " DISABLE-NBT : Disable Netbios-over-TCP/IP.\n"
737 "--dhcp-renew : Ask Windows to renew the TAP adapter lease on startup.\n"
738 "--dhcp-pre-release : Ask Windows to release the previous TAP adapter lease on\n"
739 " startup.\n"
740 "--register-dns : Run ipconfig /flushdns and ipconfig /registerdns\n"
741 " on connection initiation.\n"
742 "--tap-sleep n : Sleep for n seconds after TAP adapter open before\n"
743 " attempting to set adapter properties.\n"
744 "--pause-exit : When run from a console window, pause before exiting.\n"
745 "--service ex [0|1] : For use when " PACKAGE_NAME " is being instantiated by a\n"
746 " service, and should not be used directly by end-users.\n"
747 " ex is the name of an event object which, when\n"
748 " signaled, will cause " PACKAGE_NAME " to exit. A second\n"
749 " optional parameter controls the initial state of ex.\n"
750 "--show-net-up : Show " PACKAGE_NAME "'s view of routing table and net adapter list\n"
751 " after TAP adapter is up and routes have been added.\n"
752 "--windows-driver : Which tun driver to use?\n"
753 " ovpn-dco (default)\n"
754 " tap-windows6\n"
755 " wintun\n"
756 "--block-outside-dns : Block DNS on other network adapters to prevent DNS leaks\n"
757 "Windows Standalone Options:\n"
758 "\n"
759 "--show-adapters : Show all TAP-Windows adapters.\n"
760 "--show-net : Show " PACKAGE_NAME "'s view of routing table and net adapter list.\n"
761 "--show-valid-subnets : Show valid subnets for --dev tun emulation.\n"
762 "--allow-nonadmin [TAP-adapter] : Allow " PACKAGE_NAME " running without admin privileges\n"
763 " to access TAP adapter.\n"
764 #endif /* ifdef _WIN32 */
765 "\n"
766 "Generate a new key :\n"
767 "--genkey secret file : Generate a new random key of type and write to file\n"
768 " (for use with --secret, --tls-auth or --tls-crypt)."
769 #ifdef ENABLE_FEATURE_TUN_PERSIST
770 "\n"
771 "Tun/tap config mode (available with linux 2.4+):\n"
772 "--mktun : Create a persistent tunnel.\n"
773 "--rmtun : Remove a persistent tunnel.\n"
774 "--dev tunX|tapX : tun/tap device\n"
775 "--dev-type dt : Device type. See tunnel options above for details.\n"
776 "--user user : User to set privilege to.\n"
777 "--group group : Group to set privilege to.\n"
778 #endif
779 #ifdef ENABLE_PKCS11
780 "\n"
781 "PKCS#11 standalone options:\n"
782 #ifdef DEFAULT_PKCS11_MODULE
783 "--show-pkcs11-ids [provider] [cert_private] : Show PKCS#11 available ids.\n"
784 #else
785 "--show-pkcs11-ids provider [cert_private] : Show PKCS#11 available ids.\n"
786 #endif
787 " --verb option can be added *BEFORE* this.\n"
788 #endif /* ENABLE_PKCS11 */
789 "\n"
790 "General Standalone Options:\n"
791 #ifdef ENABLE_DEBUG
792 "--show-gateway : Show info about default gateway.\n"
793 #endif
794 ;
795
796 #endif /* !ENABLE_SMALL */
797
798 /*
799 * This is where the options defaults go.
800 * Any option not explicitly set here
801 * will be set to 0.
802 */
803 void
804 init_options(struct options *o, const bool init_gc)
805 {
806 CLEAR(*o);
807 if (init_gc)
808 {
809 gc_init(&o->gc);
810 gc_init(&o->dns_options.gc);
811 o->gc_owned = true;
812 }
813 o->mode = MODE_POINT_TO_POINT;
814 o->topology = TOP_NET30;
815 o->ce.proto = PROTO_UDP;
816 o->ce.af = AF_UNSPEC;
817 o->ce.bind_ipv6_only = false;
818 o->ce.connect_retry_seconds = 5;
819 o->ce.connect_retry_seconds_max = 300;
820 o->ce.connect_timeout = 120;
821 o->connect_retry_max = 0;
822 o->ce.local_port = o->ce.remote_port = OPENVPN_PORT;
823 o->verbosity = 1;
824 o->status_file_update_freq = 60;
825 o->status_file_version = 1;
826 o->ce.bind_local = true;
827 o->ce.tun_mtu = TUN_MTU_DEFAULT;
828 o->ce.occ_mtu = 0;
829 o->ce.link_mtu = LINK_MTU_DEFAULT;
830 o->ce.tls_mtu = TLS_MTU_DEFAULT;
831 o->ce.mtu_discover_type = -1;
832 o->ce.mssfix = 0;
833 o->ce.mssfix_default = true;
834 o->ce.mssfix_encap = true;
835 o->route_delay_window = 30;
836 o->resolve_retry_seconds = RESOLV_RETRY_INFINITE;
837 o->resolve_in_advance = false;
838 o->proto_force = -1;
839 o->occ = true;
840 #ifdef ENABLE_MANAGEMENT
841 o->management_log_history_cache = 250;
842 o->management_echo_buffer_size = 100;
843 o->management_state_buffer_size = 100;
844 #endif
845 #ifdef ENABLE_FEATURE_TUN_PERSIST
846 o->persist_mode = 1;
847 #endif
848 #ifdef _WIN32
849 #if 0
850 o->tuntap_options.ip_win32_type = IPW32_SET_ADAPTIVE;
851 #else
852 o->tuntap_options.ip_win32_type = IPW32_SET_DHCP_MASQ;
853 #endif
854 o->tuntap_options.dhcp_lease_time = 31536000; /* one year */
855 o->tuntap_options.dhcp_masq_offset = 0; /* use network address as internal DHCP server address */
856 o->route_method = ROUTE_METHOD_ADAPTIVE;
857 o->block_outside_dns = false;
858 o->windows_driver = WINDOWS_DRIVER_UNSPECIFIED;
859 #endif
860 o->vlan_accept = VLAN_ALL;
861 o->vlan_pvid = 1;
862 o->real_hash_size = 256;
863 o->virtual_hash_size = 256;
864 o->n_bcast_buf = 256;
865 o->tcp_queue_limit = 64;
866 o->max_clients = 1024;
867 o->max_routes_per_client = 256;
868 o->stale_routes_check_interval = 0;
869 o->ifconfig_pool_persist_refresh_freq = 600;
870 o->scheduled_exit_interval = 5;
871 o->authname = "SHA1";
872 o->replay = true;
873 o->replay_window = DEFAULT_SEQ_BACKTRACK;
874 o->replay_time = DEFAULT_TIME_BACKTRACK;
875 o->key_direction = KEY_DIRECTION_BIDIRECTIONAL;
876 #ifdef ENABLE_PREDICTION_RESISTANCE
877 o->use_prediction_resistance = false;
878 #endif
879 o->tls_timeout = 2;
880 o->renegotiate_bytes = -1;
881 o->renegotiate_seconds = 3600;
882 o->renegotiate_seconds_min = -1;
883 o->handshake_window = 60;
884 o->transition_window = 3600;
885 o->tls_cert_profile = NULL;
886 o->ecdh_curve = NULL;
887 #ifdef ENABLE_X509ALTUSERNAME
888 o->x509_username_field[0] = X509_USERNAME_FIELD_DEFAULT;
889 #endif
890 #ifdef ENABLE_PKCS11
891 o->pkcs11_pin_cache_period = -1;
892 #endif /* ENABLE_PKCS11 */
893
894 /* P2MP server context features */
895 o->auth_token_generate = false;
896
897 /* Set default --tmp-dir */
898 #ifdef _WIN32
899 /* On Windows, find temp dir via environment variables */
900 o->tmp_dir = win_get_tempdir();
901 #else
902 /* Non-windows platforms use $TMPDIR, and if not set, default to '/tmp' */
903 o->tmp_dir = getenv("TMPDIR");
904 if (!o->tmp_dir)
905 {
906 o->tmp_dir = "/tmp";
907 }
908 #endif /* _WIN32 */
909 o->allow_recursive_routing = false;
910
911 #ifndef ENABLE_DCO
912 o->tuntap_options.disable_dco = true;
913 #endif /* ENABLE_DCO */
914 }
915
916 void
917 uninit_options(struct options *o)
918 {
919 if (o->gc_owned)
920 {
921 gc_free(&o->gc);
922 gc_free(&o->dns_options.gc);
923 }
924 }
925
926 struct pull_filter
927 {
928 #define PUF_TYPE_UNDEF 0 /** undefined filter type */
929 #define PUF_TYPE_ACCEPT 1 /** filter type to accept a matching option */
930 #define PUF_TYPE_IGNORE 2 /** filter type to ignore a matching option */
931 #define PUF_TYPE_REJECT 3 /** filter type to reject and trigger SIGUSR1 */
932 int type;
933 int size;
934 char *pattern;
935 struct pull_filter *next;
936 };
937
938 struct pull_filter_list
939 {
940 struct pull_filter *head;
941 struct pull_filter *tail;
942 };
943
944 #ifndef ENABLE_SMALL
945
946 static const char *
947 pull_filter_type_name(int type)
948 {
949 if (type == PUF_TYPE_ACCEPT)
950 {
951 return "accept";
952 }
953 if (type == PUF_TYPE_IGNORE)
954 {
955 return "ignore";
956 }
957 if (type == PUF_TYPE_REJECT)
958 {
959 return "reject";
960 }
961 else
962 {
963 return "???";
964 }
965 }
966
967 #define SHOW_PARM(name, value, format) msg(D_SHOW_PARMS, " " #name " = " format, (value))
968 #define SHOW_STR(var) SHOW_PARM(var, (o->var ? o->var : "[UNDEF]"), "'%s'")
969 #define SHOW_STR_INLINE(var) SHOW_PARM(var, \
970 o->var ## _inline ? "[INLINE]" : \
971 (o->var ? o->var : "[UNDEF]"), \
972 "'%s'")
973 #define SHOW_INT(var) SHOW_PARM(var, o->var, "%d")
974 #define SHOW_UINT(var) SHOW_PARM(var, o->var, "%u")
975 #define SHOW_INT64(var) SHOW_PARM(var, o->var, "%" PRIi64)
976 #define SHOW_UNSIGNED(var) SHOW_PARM(var, o->var, "0x%08x")
977 #define SHOW_BOOL(var) SHOW_PARM(var, (o->var ? "ENABLED" : "DISABLED"), "%s");
978
979 #endif /* ifndef ENABLE_SMALL */
980
981 static void
982 setenv_connection_entry(struct env_set *es,
983 const struct connection_entry *e,
984 const int i)
985 {
986 setenv_str_i(es, "proto", proto2ascii(e->proto, e->af, false), i);
987 setenv_str_i(es, "local", e->local, i);
988 setenv_str_i(es, "local_port", e->local_port, i);
989 setenv_str_i(es, "remote", e->remote, i);
990 setenv_str_i(es, "remote_port", e->remote_port, i);
991
992 if (e->http_proxy_options)
993 {
994 setenv_str_i(es, "http_proxy_server", e->http_proxy_options->server, i);
995 setenv_str_i(es, "http_proxy_port", e->http_proxy_options->port, i);
996 }
997 if (e->socks_proxy_server)
998 {
999 setenv_str_i(es, "socks_proxy_server", e->socks_proxy_server, i);
1000 setenv_str_i(es, "socks_proxy_port", e->socks_proxy_port, i);
1001 }
1002 }
1003
1004 void
1005 setenv_settings(struct env_set *es, const struct options *o)
1006 {
1007 setenv_str(es, "config", o->config);
1008 setenv_int(es, "verb", o->verbosity);
1009 setenv_int(es, "daemon", o->daemon);
1010 setenv_int(es, "daemon_log_redirect", o->log);
1011 setenv_long_long(es, "daemon_start_time", time(NULL));
1012 setenv_int(es, "daemon_pid", platform_getpid());
1013
1014 if (o->connection_list)
1015 {
1016 int i;
1017 for (i = 0; i < o->connection_list->len; ++i)
1018 {
1019 setenv_connection_entry(es, o->connection_list->array[i], i+1);
1020 }
1021 }
1022 else
1023 {
1024 setenv_connection_entry(es, &o->ce, 1);
1025 }
1026
1027 if (!o->pull)
1028 {
1029 setenv_dns_options(&o->dns_options, es);
1030 }
1031 }
1032
1033 static void
1034 setenv_foreign_option(struct options *o, const char *argv[], int len, struct env_set *es)
1035 {
1036 if (len > 0)
1037 {
1038 struct gc_arena gc = gc_new();
1039 struct buffer name = alloc_buf_gc(OPTION_PARM_SIZE, &gc);
1040 struct buffer value = alloc_buf_gc(OPTION_PARM_SIZE, &gc);
1041 int i;
1042 bool first = true;
1043 bool good = true;
1044
1045 good &= buf_printf(&name, "foreign_option_%d", o->foreign_option_index + 1);
1046 ++o->foreign_option_index;
1047 for (i = 0; i < len; ++i)
1048 {
1049 if (argv[i])
1050 {
1051 if (!first)
1052 {
1053 good &= buf_printf(&value, " ");
1054 }
1055 good &= buf_printf(&value, "%s", argv[i]);
1056 first = false;
1057 }
1058 }
1059 if (good)
1060 {
1061 setenv_str(es, BSTR(&name), BSTR(&value));
1062 }
1063 else
1064 {
1065 msg(M_WARN, "foreign_option: name/value overflow");
1066 }
1067 gc_free(&gc);
1068 }
1069 }
1070
1071 static in_addr_t
1072 get_ip_addr(const char *ip_string, int msglevel, bool *error)
1073 {
1074 unsigned int flags = GETADDR_HOST_ORDER;
1075 bool succeeded = false;
1076 in_addr_t ret;
1077
1078 if (msglevel & M_FATAL)
1079 {
1080 flags |= GETADDR_FATAL;
1081 }
1082
1083 ret = getaddr(flags, ip_string, 0, &succeeded, NULL);
1084 if (!succeeded && error)
1085 {
1086 *error = true;
1087 }
1088 return ret;
1089 }
1090
1091 /**
1092 * Returns newly allocated string containing address part without "/nn".
1093 *
1094 * If gc != NULL, the allocated memory is registered in the supplied gc.
1095 */
1096 static char *
1097 get_ipv6_addr_no_netbits(const char *addr, struct gc_arena *gc)
1098 {
1099 const char *end = strchr(addr, '/');
1100 char *ret = NULL;
1101 if (NULL == end)
1102 {
1103 ret = string_alloc(addr, gc);
1104 }
1105 else
1106 {
1107 size_t len = end - addr;
1108 ret = gc_malloc(len + 1, true, gc);
1109 memcpy(ret, addr, len);
1110 }
1111 return ret;
1112 }
1113
1114 static bool
1115 ipv6_addr_safe_hexplusbits( const char *ipv6_prefix_spec )
1116 {
1117 struct in6_addr t_addr;
1118 unsigned int t_bits;
1119
1120 return get_ipv6_addr( ipv6_prefix_spec, &t_addr, &t_bits, M_WARN );
1121 }
1122
1123 static char *
1124 string_substitute(const char *src, int from, int to, struct gc_arena *gc)
1125 {
1126 char *ret = (char *) gc_malloc(strlen(src) + 1, true, gc);
1127 char *dest = ret;
1128 char c;
1129
1130 do
1131 {
1132 c = *src++;
1133 if (c == from)
1134 {
1135 c = to;
1136 }
1137 *dest++ = c;
1138 }
1139 while (c);
1140 return ret;
1141 }
1142
1143 /**
1144 * Parses a hexstring and checks if the string has the correct length. Return
1145 * a verify_hash_list containing the parsed hash string.
1146 *
1147 * @param str String to check/parse
1148 * @param nbytes Number of bytes expected in the hexstr (e.g. 20 for SHA1)
1149 * @param msglevel message level to use when printing warnings/errors
1150 * @param gc The returned object will be allocated in this gc
1151 */
1152 static struct verify_hash_list *
1153 parse_hash_fingerprint(const char *str, int nbytes, int msglevel, struct gc_arena *gc)
1154 {
1155 int i = 0;
1156 const char *cp = str;
1157
1158 struct verify_hash_list *ret;
1159 ALLOC_OBJ_CLEAR_GC(ret, struct verify_hash_list, gc);
1160
1161 char term = 0;
1162 unsigned int byte;
1163
1164 while (*cp && i < nbytes)
1165 {
1166 /* valid segments consist of exactly two hex digits, then ':' or EOS */
1167 if (!isxdigit(cp[0])
1168 || !isxdigit(cp[1])
1169 || (cp[2] != ':' && cp[2] != '\0')
1170 || sscanf(cp, "%x", &byte) != 1)
1171 {
1172 msg(msglevel, "format error in hash fingerprint: %s", str);
1173 break;
1174 }
1175
1176 ret->hash[i++] = (uint8_t)byte;
1177
1178 term = cp[2];
1179 if (term == '\0')
1180 {
1181 break;
1182 }
1183 cp += 3;
1184 }
1185 if (i < nbytes)
1186 {
1187 msg(msglevel, "hash fingerprint is wrong length - expected %d bytes, got %d: %s", nbytes, i, str);
1188 }
1189 else if (term != '\0')
1190 {
1191 msg(msglevel, "hash fingerprint too long - expected only %d bytes: %s", nbytes, str);
1192 }
1193 return ret;
1194 }
1195
1196 /**
1197 * Parses a string consisting of multiple lines of hexstrings and checks if each
1198 * string has the correct length. Empty lines are ignored. Returns
1199 * a linked list of (possibly) multiple verify_hash_list objects.
1200 *
1201 * @param str String to check/parse
1202 * @param nbytes Number of bytes expected in the hexstring (e.g. 20 for SHA1)
1203 * @param msglevel message level to use when printing warnings/errors
1204 * @param gc The returned list items will be allocated in this gc
1205 */
1206 static struct verify_hash_list *
1207 parse_hash_fingerprint_multiline(const char *str, int nbytes, int msglevel,
1208 struct gc_arena *gc)
1209 {
1210 struct gc_arena gc_temp = gc_new();
1211 char *lines = string_alloc(str, &gc_temp);
1212
1213 struct verify_hash_list *ret = NULL;
1214
1215 const char *line;
1216 while ((line = strsep(&lines, "\n")))
1217 {
1218 /* ignore leading whitespace */
1219 while (isspace(*line))
1220 {
1221 line++;
1222 }
1223 /* skip empty lines and comment lines */
1224 if (strlen(line) == 0 || *line == '#' || *line == ';')
1225 {
1226 continue;
1227 }
1228
1229 struct verify_hash_list *hash = parse_hash_fingerprint(line, nbytes,
1230 msglevel, gc);
1231
1232 if (!hash)
1233 {
1234 gc_free(&gc_temp);
1235 return NULL;
1236 }
1237
1238 hash->next = ret;
1239 ret = hash;
1240 }
1241 gc_free(&gc_temp);
1242
1243 return ret;
1244 }
1245 #ifdef _WIN32
1246
1247 #ifndef ENABLE_SMALL
1248
1249 static void
1250 show_dhcp_option_list(const char *name, const char *const *array, int len)
1251 {
1252 int i;
1253 for (i = 0; i < len; ++i)
1254 {
1255 msg(D_SHOW_PARMS, " %s[%d] = %s", name, i, array[i] );
1256 }
1257 }
1258
1259 static void
1260 show_dhcp_option_addrs(const char *name, const in_addr_t *array, int len)
1261 {
1262 struct gc_arena gc = gc_new();
1263 int i;
1264 for (i = 0; i < len; ++i)
1265 {
1266 msg(D_SHOW_PARMS, " %s[%d] = %s",
1267 name,
1268 i,
1269 print_in_addr_t(array[i], 0, &gc));
1270 }
1271 gc_free(&gc);
1272 }
1273
1274 static void
1275 show_tuntap_options(const struct tuntap_options *o)
1276 {
1277 SHOW_BOOL(ip_win32_defined);
1278 SHOW_INT(ip_win32_type);
1279 SHOW_INT(dhcp_masq_offset);
1280 SHOW_INT(dhcp_lease_time);
1281 SHOW_INT(tap_sleep);
1282 SHOW_BOOL(dhcp_options);
1283 SHOW_BOOL(dhcp_renew);
1284 SHOW_BOOL(dhcp_pre_release);
1285 SHOW_STR(domain);
1286 SHOW_STR(netbios_scope);
1287 SHOW_INT(netbios_node_type);
1288 SHOW_BOOL(disable_nbt);
1289
1290 show_dhcp_option_addrs("DNS", o->dns, o->dns_len);
1291 show_dhcp_option_addrs("WINS", o->wins, o->wins_len);
1292 show_dhcp_option_addrs("NTP", o->ntp, o->ntp_len);
1293 show_dhcp_option_addrs("NBDD", o->nbdd, o->nbdd_len);
1294 show_dhcp_option_list("DOMAIN-SEARCH", o->domain_search_list, o->domain_search_list_len);
1295 }
1296
1297 #endif /* ifndef ENABLE_SMALL */
1298 #endif /* ifdef _WIN32 */
1299
1300 #if defined(_WIN32) || defined(TARGET_ANDROID)
1301 static void
1302 dhcp_option_dns6_parse(const char *parm, struct in6_addr *dns6_list, int *len, int msglevel)
1303 {
1304 struct in6_addr addr;
1305 if (*len >= N_DHCP_ADDR)
1306 {
1307 msg(msglevel, "--dhcp-option DNS: maximum of %d IPv6 dns servers can be specified",
1308 N_DHCP_ADDR);
1309 }
1310 else if (get_ipv6_addr(parm, &addr, NULL, msglevel))
1311 {
1312 dns6_list[(*len)++] = addr;
1313 }
1314 }
1315 static void
1316 dhcp_option_address_parse(const char *name, const char *parm, in_addr_t *array, int *len, int msglevel)
1317 {
1318 if (*len >= N_DHCP_ADDR)
1319 {
1320 msg(msglevel, "--dhcp-option %s: maximum of %d %s servers can be specified",
1321 name,
1322 N_DHCP_ADDR,
1323 name);
1324 }
1325 else
1326 {
1327 if (ip_addr_dotted_quad_safe(parm)) /* FQDN -- IP address only */
1328 {
1329 bool error = false;
1330 const in_addr_t addr = get_ip_addr(parm, msglevel, &error);
1331 if (!error)
1332 {
1333 array[(*len)++] = addr;
1334 }
1335 }
1336 else
1337 {
1338 msg(msglevel, "dhcp-option parameter %s '%s' must be an IP address", name, parm);
1339 }
1340 }
1341 }
1342
1343 /*
1344 * If DNS options are set use these for TUN/TAP options as well.
1345 * Applies to DNS, DNS6 and DOMAIN-SEARCH.
1346 * Existing options will be discarded.
1347 */
1348 static void
1349 tuntap_options_copy_dns(struct options *o)
1350 {
1351 struct tuntap_options *tt = &o->tuntap_options;
1352 struct dns_options *dns = &o->dns_options;
1353
1354 if (dns->search_domains)
1355 {
1356 tt->domain_search_list_len = 0;
1357 const struct dns_domain *domain = dns->search_domains;
1358 while (domain && tt->domain_search_list_len < N_SEARCH_LIST_LEN)
1359 {
1360 tt->domain_search_list[tt->domain_search_list_len++] = domain->name;
1361 domain = domain->next;
1362 }
1363 if (domain)
1364 {
1365 msg(M_WARN, "WARNING: couldn't copy all --dns search-domains to --dhcp-option");
1366 }
1367 }
1368
1369 if (dns->servers)
1370 {
1371 tt->dns_len = 0;
1372 tt->dns6_len = 0;
1373 bool overflow = false;
1374 const struct dns_server *server = dns->servers;
1375 while (server)
1376 {
1377 if (server->addr4_defined && tt->dns_len < N_DHCP_ADDR)
1378 {
1379 tt->dns[tt->dns_len++] = server->addr4.s_addr;
1380 }
1381 else
1382 {
1383 overflow = true;
1384 }
1385 if (server->addr6_defined && tt->dns6_len < N_DHCP_ADDR)
1386 {
1387 tt->dns6[tt->dns6_len++] = server->addr6;
1388 }
1389 else
1390 {
1391 overflow = true;
1392 }
1393 server = server->next;
1394 }
1395 if (overflow)
1396 {
1397 msg(M_WARN, "WARNING: couldn't copy all --dns server addresses to --dhcp-option");
1398 }
1399 }
1400 }
1401 #else /* if defined(_WIN32) || defined(TARGET_ANDROID) */
1402 static void
1403 foreign_options_copy_dns(struct options *o, struct env_set *es)
1404 {
1405 const struct dns_domain *domain = o->dns_options.search_domains;
1406 const struct dns_server *server = o->dns_options.servers;
1407 if (!domain && !server)
1408 {
1409 return;
1410 }
1411
1412 /* reset the index since we're starting all over again */
1413 int opt_max = o->foreign_option_index;
1414 o->foreign_option_index = 0;
1415
1416 for (int i = 1; i <= opt_max; ++i)
1417 {
1418 char name[32];
1419 openvpn_snprintf(name, sizeof(name), "foreign_option_%d", i);
1420
1421 const char *env_str = env_set_get(es, name);
1422 const char *value = strchr(env_str, '=') + 1;
1423 if ((domain && strstr(value, "dhcp-option DOMAIN-SEARCH") == value)
1424 || (server && strstr(value, "dhcp-option DNS") == value))
1425 {
1426 setenv_del(es, name);
1427 }
1428 else
1429 {
1430 setenv_foreign_option(o, &value, 1, es);
1431 }
1432 }
1433
1434 struct gc_arena gc = gc_new();
1435
1436 while (server)
1437 {
1438 if (server->addr4_defined)
1439 {
1440 const char *argv[] = {
1441 "dhcp-option",
1442 "DNS",
1443 print_in_addr_t(server->addr4.s_addr, 0, &gc)
1444 };
1445 setenv_foreign_option(o, argv, 3, es);
1446 }
1447 if (server->addr6_defined)
1448 {
1449 const char *argv[] = {
1450 "dhcp-option",
1451 "DNS6",
1452 print_in6_addr(server->addr6, 0, &gc)
1453 };
1454 setenv_foreign_option(o, argv, 3, es);
1455 }
1456 server = server->next;
1457 }
1458 while (domain)
1459 {
1460 const char *argv[] = { "dhcp-option", "DOMAIN-SEARCH", domain->name };
1461 setenv_foreign_option(o, argv, 3, es);
1462 domain = domain->next;
1463 }
1464
1465 gc_free(&gc);
1466
1467 /* remove old leftover entries */
1468 while (o->foreign_option_index < opt_max)
1469 {
1470 char name[32];
1471 openvpn_snprintf(name, sizeof(name), "foreign_option_%d", opt_max--);
1472 setenv_del(es, name);
1473 }
1474 }
1475 #endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */
1476
1477 #ifndef ENABLE_SMALL
1478 static const char *
1479 print_vlan_accept(enum vlan_acceptable_frames mode)
1480 {
1481 switch (mode)
1482 {
1483 case VLAN_ONLY_TAGGED:
1484 return "tagged";
1485
1486 case VLAN_ONLY_UNTAGGED_OR_PRIORITY:
1487 return "untagged";
1488
1489 case VLAN_ALL:
1490 return "all";
1491 }
1492 return NULL;
1493 }
1494
1495 static void
1496 show_p2mp_parms(const struct options *o)
1497 {
1498 struct gc_arena gc = gc_new();
1499
1500 msg(D_SHOW_PARMS, " server_network = %s", print_in_addr_t(o->server_network, 0, &gc));
1501 msg(D_SHOW_PARMS, " server_netmask = %s", print_in_addr_t(o->server_netmask, 0, &gc));
1502 msg(D_SHOW_PARMS, " server_network_ipv6 = %s", print_in6_addr(o->server_network_ipv6, 0, &gc) );
1503 SHOW_INT(server_netbits_ipv6);
1504 msg(D_SHOW_PARMS, " server_bridge_ip = %s", print_in_addr_t(o->server_bridge_ip, 0, &gc));
1505 msg(D_SHOW_PARMS, " server_bridge_netmask = %s", print_in_addr_t(o->server_bridge_netmask, 0, &gc));
1506 msg(D_SHOW_PARMS, " server_bridge_pool_start = %s", print_in_addr_t(o->server_bridge_pool_start, 0, &gc));
1507 msg(D_SHOW_PARMS, " server_bridge_pool_end = %s", print_in_addr_t(o->server_bridge_pool_end, 0, &gc));
1508 if (o->push_list.head)
1509 {
1510 const struct push_entry *e = o->push_list.head;
1511 while (e)
1512 {
1513 if (e->enable)
1514 {
1515 msg(D_SHOW_PARMS, " push_entry = '%s'", e->option);
1516 }
1517 e = e->next;
1518 }
1519 }
1520 SHOW_BOOL(ifconfig_pool_defined);
1521 msg(D_SHOW_PARMS, " ifconfig_pool_start = %s", print_in_addr_t(o->ifconfig_pool_start, 0, &gc));
1522 msg(D_SHOW_PARMS, " ifconfig_pool_end = %s", print_in_addr_t(o->ifconfig_pool_end, 0, &gc));
1523 msg(D_SHOW_PARMS, " ifconfig_pool_netmask = %s", print_in_addr_t(o->ifconfig_pool_netmask, 0, &gc));
1524 SHOW_STR(ifconfig_pool_persist_filename);
1525 SHOW_INT(ifconfig_pool_persist_refresh_freq);
1526 SHOW_BOOL(ifconfig_ipv6_pool_defined);
1527 msg(D_SHOW_PARMS, " ifconfig_ipv6_pool_base = %s", print_in6_addr(o->ifconfig_ipv6_pool_base, 0, &gc));
1528 SHOW_INT(ifconfig_ipv6_pool_netbits);
1529 SHOW_INT(n_bcast_buf);
1530 SHOW_INT(tcp_queue_limit);
1531 SHOW_INT(real_hash_size);
1532 SHOW_INT(virtual_hash_size);
1533 SHOW_STR(client_connect_script);
1534 SHOW_STR(learn_address_script);
1535 SHOW_STR(client_disconnect_script);
1536 SHOW_STR(client_crresponse_script);
1537 SHOW_STR(client_config_dir);
1538 SHOW_BOOL(ccd_exclusive);
1539 SHOW_STR(tmp_dir);
1540 SHOW_BOOL(push_ifconfig_defined);
1541 msg(D_SHOW_PARMS, " push_ifconfig_local = %s", print_in_addr_t(o->push_ifconfig_local, 0, &gc));
1542 msg(D_SHOW_PARMS, " push_ifconfig_remote_netmask = %s", print_in_addr_t(o->push_ifconfig_remote_netmask, 0, &gc));
1543 SHOW_BOOL(push_ifconfig_ipv6_defined);
1544 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 );
1545 msg(D_SHOW_PARMS, " push_ifconfig_ipv6_remote = %s", print_in6_addr(o->push_ifconfig_ipv6_remote, 0, &gc));
1546 SHOW_BOOL(enable_c2c);
1547 SHOW_BOOL(duplicate_cn);
1548 SHOW_INT(cf_max);
1549 SHOW_INT(cf_per);
1550 SHOW_INT(max_clients);
1551 SHOW_INT(max_routes_per_client);
1552 SHOW_STR(auth_user_pass_verify_script);
1553 SHOW_BOOL(auth_user_pass_verify_script_via_file);
1554 SHOW_BOOL(auth_token_generate);
1555 SHOW_INT(auth_token_lifetime);
1556 SHOW_STR_INLINE(auth_token_secret_file);
1557 #if PORT_SHARE
1558 SHOW_STR(port_share_host);
1559 SHOW_STR(port_share_port);
1560 #endif
1561 SHOW_BOOL(vlan_tagging);
1562 msg(D_SHOW_PARMS, " vlan_accept = %s", print_vlan_accept(o->vlan_accept));
1563 SHOW_INT(vlan_pvid);
1564
1565 SHOW_BOOL(client);
1566 SHOW_BOOL(pull);
1567 SHOW_STR_INLINE(auth_user_pass_file);
1568
1569 gc_free(&gc);
1570 }
1571
1572 #endif /* ! ENABLE_SMALL */
1573
1574 static void
1575 option_iroute(struct options *o,
1576 const char *network_str,
1577 const char *netmask_str,
1578 int msglevel)
1579 {
1580 struct iroute *ir;
1581
1582 ALLOC_OBJ_GC(ir, struct iroute, &o->gc);
1583 ir->network = getaddr(GETADDR_HOST_ORDER, network_str, 0, NULL, NULL);
1584 ir->netbits = 32; /* host route if no netmask given */
1585
1586 if (netmask_str)
1587 {
1588 const in_addr_t netmask = getaddr(GETADDR_HOST_ORDER, netmask_str, 0, NULL, NULL);
1589 ir->netbits = netmask_to_netbits2(netmask);
1590
1591 if (ir->netbits < 0)
1592 {
1593 msg(msglevel, "in --iroute %s %s : Bad network/subnet specification",
1594 network_str,
1595 netmask_str);
1596 return;
1597 }
1598 }
1599
1600 ir->next = o->iroutes;
1601 o->iroutes = ir;
1602 }
1603
1604 static void
1605 option_iroute_ipv6(struct options *o,
1606 const char *prefix_str,
1607 int msglevel)
1608 {
1609 struct iroute_ipv6 *ir;
1610
1611 ALLOC_OBJ_GC(ir, struct iroute_ipv6, &o->gc);
1612
1613 if (!get_ipv6_addr(prefix_str, &ir->network, &ir->netbits, msglevel ))
1614 {
1615 msg(msglevel, "in --iroute-ipv6 %s: Bad IPv6 prefix specification",
1616 prefix_str);
1617 return;
1618 }
1619
1620 ir->next = o->iroutes_ipv6;
1621 o->iroutes_ipv6 = ir;
1622 }
1623
1624 #ifndef ENABLE_SMALL
1625 static void
1626 show_http_proxy_options(const struct http_proxy_options *o)
1627 {
1628 int i;
1629 msg(D_SHOW_PARMS, "BEGIN http_proxy");
1630 SHOW_STR(server);
1631 SHOW_STR(port);
1632 SHOW_STR(auth_method_string);
1633 SHOW_STR(auth_file);
1634 SHOW_STR(http_version);
1635 SHOW_STR(user_agent);
1636 for (i = 0; i < MAX_CUSTOM_HTTP_HEADER && o->custom_headers[i].name; i++)
1637 {
1638 if (o->custom_headers[i].content)
1639 {
1640 msg(D_SHOW_PARMS, " custom_header[%d] = %s: %s", i,
1641 o->custom_headers[i].name, o->custom_headers[i].content);
1642 }
1643 else
1644 {
1645 msg(D_SHOW_PARMS, " custom_header[%d] = %s", i,
1646 o->custom_headers[i].name);
1647 }
1648 }
1649 msg(D_SHOW_PARMS, "END http_proxy");
1650 }
1651 #endif /* ifndef ENABLE_SMALL */
1652
1653 void
1654 options_detach(struct options *o)
1655 {
1656 gc_detach(&o->gc);
1657 o->routes = NULL;
1658 o->client_nat = NULL;
1659 clone_push_list(o);
1660 }
1661
1662 void
1663 rol_check_alloc(struct options *options)
1664 {
1665 if (!options->routes)
1666 {
1667 options->routes = new_route_option_list(&options->gc);
1668 }
1669 }
1670
1671 static void
1672 rol6_check_alloc(struct options *options)
1673 {
1674 if (!options->routes_ipv6)
1675 {
1676 options->routes_ipv6 = new_route_ipv6_option_list(&options->gc);
1677 }
1678 }
1679
1680 static void
1681 cnol_check_alloc(struct options *options)
1682 {
1683 if (!options->client_nat)
1684 {
1685 options->client_nat = new_client_nat_list(&options->gc);
1686 }
1687 }
1688
1689 #ifndef ENABLE_SMALL
1690 static void
1691 show_connection_entry(const struct connection_entry *o)
1692 {
1693 msg(D_SHOW_PARMS, " proto = %s", proto2ascii(o->proto, o->af, false));
1694 SHOW_STR(local);
1695 SHOW_STR(local_port);
1696 SHOW_STR(remote);
1697 SHOW_STR(remote_port);
1698 SHOW_BOOL(remote_float);
1699 SHOW_BOOL(bind_defined);
1700 SHOW_BOOL(bind_local);
1701 SHOW_BOOL(bind_ipv6_only);
1702 SHOW_INT(connect_retry_seconds);
1703 SHOW_INT(connect_timeout);
1704
1705 if (o->http_proxy_options)
1706 {
1707 show_http_proxy_options(o->http_proxy_options);
1708 }
1709 SHOW_STR(socks_proxy_server);
1710 SHOW_STR(socks_proxy_port);
1711 SHOW_INT(tun_mtu);
1712 SHOW_BOOL(tun_mtu_defined);
1713 SHOW_INT(link_mtu);
1714 SHOW_BOOL(link_mtu_defined);
1715 SHOW_INT(tun_mtu_extra);
1716 SHOW_BOOL(tun_mtu_extra_defined);
1717 SHOW_INT(tls_mtu);
1718
1719 SHOW_INT(mtu_discover_type);
1720
1721 #ifdef ENABLE_FRAGMENT
1722 SHOW_INT(fragment);
1723 #endif
1724 SHOW_INT(mssfix);
1725 SHOW_BOOL(mssfix_encap);
1726 SHOW_BOOL(mssfix_fixed);
1727
1728 SHOW_INT(explicit_exit_notification);
1729
1730 SHOW_STR_INLINE(tls_auth_file);
1731 SHOW_PARM(key_direction, keydirection2ascii(o->key_direction, false, true),
1732 "%s");
1733 SHOW_STR_INLINE(tls_crypt_file);
1734 SHOW_STR_INLINE(tls_crypt_v2_file);
1735 }
1736
1737
1738 static void
1739 show_connection_entries(const struct options *o)
1740 {
1741 if (o->connection_list)
1742 {
1743 const struct connection_list *l = o->connection_list;
1744 int i;
1745 for (i = 0; i < l->len; ++i)
1746 {
1747 msg(D_SHOW_PARMS, "Connection profiles [%d]:", i);
1748 show_connection_entry(l->array[i]);
1749 }
1750 }
1751 else
1752 {
1753 msg(D_SHOW_PARMS, "Connection profiles [default]:");
1754 show_connection_entry(&o->ce);
1755 }
1756 msg(D_SHOW_PARMS, "Connection profiles END");
1757 }
1758
1759 static void
1760 show_pull_filter_list(const struct pull_filter_list *l)
1761 {
1762 struct pull_filter *f;
1763 if (!l)
1764 {
1765 return;
1766 }
1767
1768 msg(D_SHOW_PARMS, " Pull filters:");
1769 for (f = l->head; f; f = f->next)
1770 {
1771 msg(D_SHOW_PARMS, " %s \"%s\"", pull_filter_type_name(f->type), f->pattern);
1772 }
1773 }
1774
1775 #endif /* ifndef ENABLE_SMALL */
1776
1777 void
1778 show_settings(const struct options *o)
1779 {
1780 #ifndef ENABLE_SMALL
1781 msg(D_SHOW_PARMS, "Current Parameter Settings:");
1782
1783 SHOW_STR(config);
1784
1785 SHOW_INT(mode);
1786
1787 #ifdef ENABLE_FEATURE_TUN_PERSIST
1788 SHOW_BOOL(persist_config);
1789 SHOW_INT(persist_mode);
1790 #endif
1791
1792 SHOW_BOOL(show_ciphers);
1793 SHOW_BOOL(show_digests);
1794 SHOW_BOOL(show_engines);
1795 SHOW_BOOL(genkey);
1796 SHOW_STR(genkey_filename);
1797 SHOW_STR(key_pass_file);
1798 SHOW_BOOL(show_tls_ciphers);
1799
1800 SHOW_INT(connect_retry_max);
1801 show_connection_entries(o);
1802
1803 SHOW_BOOL(remote_random);
1804
1805 SHOW_STR(ipchange);
1806 SHOW_STR(dev);
1807 SHOW_STR(dev_type);
1808 SHOW_STR(dev_node);
1809 #if defined(ENABLE_DCO)
1810 SHOW_BOOL(tuntap_options.disable_dco);
1811 #endif
1812 SHOW_STR(lladdr);
1813 SHOW_INT(topology);
1814 SHOW_STR(ifconfig_local);
1815 SHOW_STR(ifconfig_remote_netmask);
1816 SHOW_BOOL(ifconfig_noexec);
1817 SHOW_BOOL(ifconfig_nowarn);
1818 SHOW_STR(ifconfig_ipv6_local);
1819 SHOW_INT(ifconfig_ipv6_netbits);
1820 SHOW_STR(ifconfig_ipv6_remote);
1821
1822 SHOW_INT(shaper);
1823 SHOW_INT(mtu_test);
1824
1825 SHOW_BOOL(mlock);
1826
1827 SHOW_INT(keepalive_ping);
1828 SHOW_INT(keepalive_timeout);
1829 SHOW_INT(inactivity_timeout);
1830 SHOW_INT(session_timeout);
1831 SHOW_INT64(inactivity_minimum_bytes);
1832 SHOW_INT(ping_send_timeout);
1833 SHOW_INT(ping_rec_timeout);
1834 SHOW_INT(ping_rec_timeout_action);
1835 SHOW_BOOL(ping_timer_remote);
1836 SHOW_INT(remap_sigusr1);
1837 SHOW_BOOL(persist_tun);
1838 SHOW_BOOL(persist_local_ip);
1839 SHOW_BOOL(persist_remote_ip);
1840 SHOW_BOOL(persist_key);
1841
1842 #if PASSTOS_CAPABILITY
1843 SHOW_BOOL(passtos);
1844 #endif
1845
1846 SHOW_INT(resolve_retry_seconds);
1847 SHOW_BOOL(resolve_in_advance);
1848
1849 SHOW_STR(username);
1850 SHOW_STR(groupname);
1851 SHOW_STR(chroot_dir);
1852 SHOW_STR(cd_dir);
1853 #ifdef ENABLE_SELINUX
1854 SHOW_STR(selinux_context);
1855 #endif
1856 SHOW_STR(writepid);
1857 SHOW_STR(up_script);
1858 SHOW_STR(down_script);
1859 SHOW_BOOL(down_pre);
1860 SHOW_BOOL(up_restart);
1861 SHOW_BOOL(up_delay);
1862 SHOW_BOOL(daemon);
1863 SHOW_BOOL(log);
1864 SHOW_BOOL(suppress_timestamps);
1865 SHOW_BOOL(machine_readable_output);
1866 SHOW_INT(nice);
1867 SHOW_INT(verbosity);
1868 SHOW_INT(mute);
1869 #ifdef ENABLE_DEBUG
1870 SHOW_INT(gremlin);
1871 #endif
1872 SHOW_STR(status_file);
1873 SHOW_INT(status_file_version);
1874 SHOW_INT(status_file_update_freq);
1875
1876 SHOW_BOOL(occ);
1877 SHOW_INT(rcvbuf);
1878 SHOW_INT(sndbuf);
1879 #if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK
1880 SHOW_INT(mark);
1881 #endif
1882 SHOW_INT(sockflags);
1883
1884 SHOW_BOOL(fast_io);
1885
1886 #ifdef USE_COMP
1887 SHOW_INT(comp.alg);
1888 SHOW_INT(comp.flags);
1889 #endif
1890
1891 SHOW_STR(route_script);
1892 SHOW_STR(route_default_gateway);
1893 SHOW_INT(route_default_metric);
1894 SHOW_BOOL(route_noexec);
1895 SHOW_INT(route_delay);
1896 SHOW_INT(route_delay_window);
1897 SHOW_BOOL(route_delay_defined);
1898 SHOW_BOOL(route_nopull);
1899 SHOW_BOOL(route_gateway_via_dhcp);
1900 SHOW_BOOL(allow_pull_fqdn);
1901 show_pull_filter_list(o->pull_filter_list);
1902
1903 if (o->routes)
1904 {
1905 print_route_options(o->routes, D_SHOW_PARMS);
1906 }
1907
1908 if (o->client_nat)
1909 {
1910 print_client_nat_list(o->client_nat, D_SHOW_PARMS);
1911 }
1912
1913 show_dns_options(&o->dns_options);
1914
1915 #ifdef ENABLE_MANAGEMENT
1916 SHOW_STR(management_addr);
1917 SHOW_STR(management_port);
1918 SHOW_STR(management_user_pass);
1919 SHOW_INT(management_log_history_cache);
1920 SHOW_INT(management_echo_buffer_size);
1921 SHOW_STR(management_client_user);
1922 SHOW_STR(management_client_group);
1923 SHOW_INT(management_flags);
1924 #endif
1925 #ifdef ENABLE_PLUGIN
1926 if (o->plugin_list)
1927 {
1928 plugin_option_list_print(o->plugin_list, D_SHOW_PARMS);
1929 }
1930 #endif
1931
1932 SHOW_STR_INLINE(shared_secret_file);
1933 SHOW_PARM(key_direction, keydirection2ascii(o->key_direction, false, true), "%s");
1934 SHOW_STR(ciphername);
1935 SHOW_STR(ncp_ciphers);
1936 SHOW_STR(authname);
1937 #ifndef ENABLE_CRYPTO_MBEDTLS
1938 SHOW_BOOL(engine);
1939 #endif /* ENABLE_CRYPTO_MBEDTLS */
1940 SHOW_BOOL(replay);
1941 SHOW_BOOL(mute_replay_warnings);
1942 SHOW_INT(replay_window);
1943 SHOW_INT(replay_time);
1944 SHOW_STR(packet_id_file);
1945 SHOW_BOOL(test_crypto);
1946 #ifdef ENABLE_PREDICTION_RESISTANCE
1947 SHOW_BOOL(use_prediction_resistance);
1948 #endif
1949
1950 SHOW_BOOL(tls_server);
1951 SHOW_BOOL(tls_client);
1952 SHOW_STR_INLINE(ca_file);
1953 SHOW_STR(ca_path);
1954 SHOW_STR_INLINE(dh_file);
1955 #ifdef ENABLE_MANAGEMENT
1956 if ((o->management_flags & MF_EXTERNAL_CERT))
1957 {
1958 SHOW_PARM("cert_file", "EXTERNAL_CERT", "%s");
1959 }
1960 else
1961 #endif
1962 SHOW_STR_INLINE(cert_file);
1963 SHOW_STR_INLINE(extra_certs_file);
1964
1965 #ifdef ENABLE_MANAGEMENT
1966 if ((o->management_flags & MF_EXTERNAL_KEY))
1967 {
1968 SHOW_PARM("priv_key_file", "EXTERNAL_PRIVATE_KEY", "%s");
1969 }
1970 else
1971 #endif
1972 SHOW_STR_INLINE(priv_key_file);
1973 #ifndef ENABLE_CRYPTO_MBEDTLS
1974 SHOW_STR_INLINE(pkcs12_file);
1975 #endif
1976 #ifdef ENABLE_CRYPTOAPI
1977 SHOW_STR(cryptoapi_cert);
1978 #endif
1979 SHOW_STR(cipher_list);
1980 SHOW_STR(cipher_list_tls13);
1981 SHOW_STR(tls_cert_profile);
1982 SHOW_STR(tls_verify);
1983 SHOW_STR(tls_export_cert);
1984 SHOW_INT(verify_x509_type);
1985 SHOW_STR(verify_x509_name);
1986 SHOW_STR_INLINE(crl_file);
1987 SHOW_INT(ns_cert_type);
1988 {
1989 int i;
1990 for (i = 0; i<MAX_PARMS; i++)
1991 {
1992 SHOW_INT(remote_cert_ku[i]);
1993 }
1994 }
1995 SHOW_STR(remote_cert_eku);
1996 if (o->verify_hash)
1997 {
1998 SHOW_INT(verify_hash_algo);
1999 SHOW_INT(verify_hash_depth);
2000 struct gc_arena gc = gc_new();
2001 struct verify_hash_list *hl = o->verify_hash;
2002 int digest_len = (o->verify_hash_algo == MD_SHA1) ? SHA_DIGEST_LENGTH :
2003 SHA256_DIGEST_LENGTH;
2004 while (hl)
2005 {
2006 char *s = format_hex_ex(hl->hash, digest_len, 0,
2007 1, ":", &gc);
2008 SHOW_PARM(verify_hash, s, "%s");
2009 hl = hl->next;
2010 }
2011 gc_free(&gc);
2012 }
2013 SHOW_INT(ssl_flags);
2014
2015 SHOW_INT(tls_timeout);
2016
2017 SHOW_INT(renegotiate_bytes);
2018 SHOW_INT(renegotiate_packets);
2019 SHOW_INT(renegotiate_seconds);
2020
2021 SHOW_INT(handshake_window);
2022 SHOW_INT(transition_window);
2023
2024 SHOW_BOOL(single_session);
2025 SHOW_BOOL(push_peer_info);
2026 SHOW_BOOL(tls_exit);
2027
2028 SHOW_STR(tls_crypt_v2_metadata);
2029
2030 #ifdef ENABLE_PKCS11
2031 {
2032 int i;
2033 for (i = 0; i<MAX_PARMS && o->pkcs11_providers[i] != NULL; i++)
2034 {
2035 SHOW_PARM(pkcs11_providers, o->pkcs11_providers[i], "%s");
2036 }
2037 }
2038 {
2039 int i;
2040 for (i = 0; i<MAX_PARMS; i++)
2041 {
2042 SHOW_PARM(pkcs11_protected_authentication, o->pkcs11_protected_authentication[i] ? "ENABLED" : "DISABLED", "%s");
2043 }
2044 }
2045 {
2046 int i;
2047 for (i = 0; i<MAX_PARMS; i++)
2048 {
2049 SHOW_PARM(pkcs11_private_mode, o->pkcs11_private_mode[i], "%08x");
2050 }
2051 }
2052 {
2053 int i;
2054 for (i = 0; i<MAX_PARMS; i++)
2055 {
2056 SHOW_PARM(pkcs11_cert_private, o->pkcs11_cert_private[i] ? "ENABLED" : "DISABLED", "%s");
2057 }
2058 }
2059 SHOW_INT(pkcs11_pin_cache_period);
2060 SHOW_STR(pkcs11_id);
2061 SHOW_BOOL(pkcs11_id_management);
2062 #endif /* ENABLE_PKCS11 */
2063
2064 show_p2mp_parms(o);
2065
2066 #ifdef _WIN32
2067 SHOW_BOOL(show_net_up);
2068 SHOW_INT(route_method);
2069 SHOW_BOOL(block_outside_dns);
2070 show_tuntap_options(&o->tuntap_options);
2071 #endif
2072 #endif /* ifndef ENABLE_SMALL */
2073 }
2074
2075 #undef SHOW_PARM
2076 #undef SHOW_STR
2077 #undef SHOW_INT
2078 #undef SHOW_BOOL
2079
2080 #ifdef ENABLE_MANAGEMENT
2081
2082 static struct http_proxy_options *
2083 parse_http_proxy_override(const char *server,
2084 const char *port,
2085 const char *flags,
2086 const int msglevel,
2087 struct gc_arena *gc)
2088 {
2089 if (server && port)
2090 {
2091 struct http_proxy_options *ho;
2092 ALLOC_OBJ_CLEAR_GC(ho, struct http_proxy_options, gc);
2093 ho->server = string_alloc(server, gc);
2094 ho->port = port;
2095 if (flags && !strcmp(flags, "nct"))
2096 {
2097 ho->auth_retry = PAR_NCT;
2098 }
2099 else
2100 {
2101 ho->auth_retry = PAR_ALL;
2102 }
2103 ho->http_version = "1.0";
2104 ho->user_agent = "OpenVPN-Autoproxy/1.0";
2105 return ho;
2106 }
2107 else
2108 {
2109 return NULL;
2110 }
2111 }
2112
2113 static void
2114 options_postprocess_http_proxy_override(struct options *o)
2115 {
2116 const struct connection_list *l = o->connection_list;
2117 int i;
2118 bool succeed = false;
2119 for (i = 0; i < l->len; ++i)
2120 {
2121 struct connection_entry *ce = l->array[i];
2122 if (ce->proto == PROTO_TCP_CLIENT || ce->proto == PROTO_TCP)
2123 {
2124 ce->http_proxy_options = o->http_proxy_override;
2125 succeed = true;
2126 }
2127 }
2128 if (succeed)
2129 {
2130 for (i = 0; i < l->len; ++i)
2131 {
2132 struct connection_entry *ce = l->array[i];
2133 if (ce->proto == PROTO_UDP)
2134 {
2135 ce->flags |= CE_DISABLED;
2136 }
2137 }
2138 }
2139 else
2140 {
2141 msg(M_WARN, "Note: option http-proxy-override ignored because no TCP-based connection profiles are defined");
2142 }
2143 }
2144
2145 #endif /* ifdef ENABLE_MANAGEMENT */
2146
2147 static struct connection_list *
2148 alloc_connection_list_if_undef(struct options *options)
2149 {
2150 if (!options->connection_list)
2151 {
2152 ALLOC_OBJ_CLEAR_GC(options->connection_list, struct connection_list, &options->gc);
2153 }
2154 return options->connection_list;
2155 }
2156
2157 static struct connection_entry *
2158 alloc_connection_entry(struct options *options, const int msglevel)
2159 {
2160 struct connection_list *l = alloc_connection_list_if_undef(options);
2161 struct connection_entry *e;
2162
2163 if (l->len >= CONNECTION_LIST_SIZE)
2164 {
2165 msg(msglevel, "Maximum number of 'connection' options (%d) exceeded", CONNECTION_LIST_SIZE);
2166 return NULL;
2167 }
2168 ALLOC_OBJ_GC(e, struct connection_entry, &options->gc);
2169 l->array[l->len++] = e;
2170 return e;
2171 }
2172
2173 static struct remote_list *
2174 alloc_remote_list_if_undef(struct options *options)
2175 {
2176 if (!options->remote_list)
2177 {
2178 ALLOC_OBJ_CLEAR_GC(options->remote_list, struct remote_list, &options->gc);
2179 }
2180 return options->remote_list;
2181 }
2182
2183 static struct remote_entry *
2184 alloc_remote_entry(struct options *options, const int msglevel)
2185 {
2186 struct remote_list *l = alloc_remote_list_if_undef(options);
2187 struct remote_entry *e;
2188
2189 if (l->len >= CONNECTION_LIST_SIZE)
2190 {
2191 msg(msglevel, "Maximum number of 'remote' options (%d) exceeded", CONNECTION_LIST_SIZE);
2192 return NULL;
2193 }
2194 ALLOC_OBJ_GC(e, struct remote_entry, &options->gc);
2195 l->array[l->len++] = e;
2196 return e;
2197 }
2198
2199 static struct pull_filter_list *
2200 alloc_pull_filter_list(struct options *o)
2201 {
2202 if (!o->pull_filter_list)
2203 {
2204 ALLOC_OBJ_CLEAR_GC(o->pull_filter_list, struct pull_filter_list, &o->gc);
2205 }
2206 return o->pull_filter_list;
2207 }
2208
2209 static struct pull_filter *
2210 alloc_pull_filter(struct options *o, const int msglevel)
2211 {
2212 struct pull_filter_list *l = alloc_pull_filter_list(o);
2213 struct pull_filter *f;
2214
2215 ALLOC_OBJ_CLEAR_GC(f, struct pull_filter, &o->gc);
2216 if (l->head)
2217 {
2218 ASSERT(l->tail);
2219 l->tail->next = f;
2220 }
2221 else
2222 {
2223 ASSERT(!l->tail);
2224 l->head = f;
2225 }
2226 l->tail = f;
2227 return f;
2228 }
2229
2230 static void
2231 connection_entry_load_re(struct connection_entry *ce, const struct remote_entry *re)
2232 {
2233 if (re->remote)
2234 {
2235 ce->remote = re->remote;
2236 }
2237 if (re->remote_port)
2238 {
2239 ce->remote_port = re->remote_port;
2240 }
2241 if (re->proto >= 0)
2242 {
2243 ce->proto = re->proto;
2244 }
2245 if (re->af > 0)
2246 {
2247 ce->af = re->af;
2248 }
2249 }
2250
2251 static void
2252 connection_entry_preload_key(const char **key_file, bool *key_inline,
2253 struct gc_arena *gc)
2254 {
2255 if (key_file && *key_file && !(*key_inline))
2256 {
2257 struct buffer in = buffer_read_from_file(*key_file, gc);
2258 if (!buf_valid(&in))
2259 {
2260 msg(M_FATAL, "Cannot pre-load keyfile (%s)", *key_file);
2261 }
2262
2263 *key_file = (const char *) in.data;
2264 *key_inline = true;
2265 }
2266 }
2267
2268 static void
2269 check_ca_required(const struct options *options)
2270 {
2271 if (options->verify_hash_no_ca
2272 || options->pkcs12_file
2273 || options->ca_file
2274 #ifndef ENABLE_CRYPTO_MBEDTLS
2275 || options->ca_path
2276 #endif
2277 )
2278 {
2279 return;
2280 }
2281
2282 const char *const str = "You must define CA file (--ca)"
2283 #ifndef ENABLE_CRYPTO_MBEDTLS
2284 " or CA path (--capath)"
2285 #endif
2286 " and/or peer fingerprint verification (--peer-fingerprint)";
2287 msg(M_USAGE, str);
2288 }
2289
2290 static void
2291 options_postprocess_verify_ce(const struct options *options,
2292 const struct connection_entry *ce)
2293 {
2294 struct options defaults;
2295 int dev = DEV_TYPE_UNDEF;
2296 bool pull = false;
2297
2298 init_options(&defaults, true);
2299
2300 if (options->test_crypto)
2301 {
2302 notnull(options->shared_secret_file, "key file (--secret)");
2303 }
2304 else
2305 {
2306 notnull(options->dev, "TUN/TAP device (--dev)");
2307 }
2308
2309 /*
2310 * Get tun/tap/null device type
2311 */
2312 dev = dev_type_enum(options->dev, options->dev_type);
2313
2314 /*
2315 * If "proto tcp" is specified, make sure we know whether it is
2316 * tcp-client or tcp-server.
2317 */
2318 if (ce->proto == PROTO_TCP)
2319 {
2320 msg(M_USAGE,
2321 "--proto tcp is ambiguous in this context. Please specify "
2322 "--proto tcp-server or --proto tcp-client");
2323 }
2324
2325 if (options->lladdr && dev != DEV_TYPE_TAP)
2326 {
2327 msg(M_USAGE, "--lladdr can only be used in --dev tap mode");
2328 }
2329
2330 /*
2331 * Sanity check on MTU parameters
2332 */
2333 if (options->ce.tun_mtu_defined && options->ce.link_mtu_defined)
2334 {
2335 msg(M_USAGE, "only one of --tun-mtu or --link-mtu may be defined");
2336 }
2337
2338 if (!proto_is_udp(ce->proto) && options->mtu_test)
2339 {
2340 msg(M_USAGE, "--mtu-test only makes sense with --proto udp");
2341 }
2342
2343 /* will we be pulling options from server? */
2344 pull = options->pull;
2345
2346 /*
2347 * Sanity check on --local, --remote, and --ifconfig
2348 */
2349
2350 if (proto_is_net(ce->proto)
2351 && string_defined_equal(ce->local, ce->remote)
2352 && string_defined_equal(ce->local_port, ce->remote_port))
2353 {
2354 msg(M_USAGE, "--remote and --local addresses are the same");
2355 }
2356
2357 if (string_defined_equal(ce->remote, options->ifconfig_local)
2358 || string_defined_equal(ce->remote, options->ifconfig_remote_netmask))
2359 {
2360 msg(M_USAGE,
2361 "--local and --remote addresses must be distinct from --ifconfig "
2362 "addresses");
2363 }
2364
2365 if (string_defined_equal(ce->local, options->ifconfig_local)
2366 || string_defined_equal(ce->local, options->ifconfig_remote_netmask))
2367 {
2368 msg(M_USAGE,
2369 "--local addresses must be distinct from --ifconfig addresses");
2370 }
2371
2372 if (string_defined_equal(options->ifconfig_local,
2373 options->ifconfig_remote_netmask))
2374 {
2375 msg(M_USAGE,
2376 "local and remote/netmask --ifconfig addresses must be different");
2377 }
2378
2379 if (ce->bind_defined && !ce->bind_local)
2380 {
2381 msg(M_USAGE, "--bind and --nobind can't be used together");
2382 }
2383
2384 if (ce->local && !ce->bind_local)
2385 {
2386 msg(M_USAGE,
2387 "--local and --nobind don't make sense when used together");
2388 }
2389
2390 if (ce->local_port_defined && !ce->bind_local)
2391 {
2392 msg(M_USAGE,
2393 "--lport and --nobind don't make sense when used together");
2394 }
2395
2396 if (!ce->remote && !ce->bind_local)
2397 {
2398 msg(M_USAGE, "--nobind doesn't make sense unless used with --remote");
2399 }
2400
2401 /*
2402 * Check for consistency of management options
2403 */
2404 #ifdef ENABLE_MANAGEMENT
2405 if (!options->management_addr
2406 && (options->management_flags
2407 || options->management_log_history_cache != defaults.management_log_history_cache))
2408 {
2409 msg(M_USAGE, "--management is not specified, however one or more options which modify the behavior of --management were specified");
2410 }
2411
2412 if ((options->management_client_user || options->management_client_group)
2413 && !(options->management_flags & MF_UNIX_SOCK))
2414 {
2415 msg(M_USAGE, "--management-client-(user|group) can only be used on unix domain sockets");
2416 }
2417
2418 if (options->management_addr
2419 && !(options->management_flags & MF_UNIX_SOCK)
2420 && (!options->management_user_pass))
2421 {
2422 msg(M_WARN, "WARNING: Using --management on a TCP port WITHOUT "
2423 "passwords is STRONGLY discouraged and considered insecure");
2424 }
2425
2426 #endif /* ifdef ENABLE_MANAGEMENT */
2427
2428 #if defined(ENABLE_MANAGEMENT) && !defined(HAVE_XKEY_PROVIDER)
2429 if ((tls_version_max() >= TLS_VER_1_3)
2430 && (options->management_flags & MF_EXTERNAL_KEY)
2431 && !(options->management_flags & (MF_EXTERNAL_KEY_NOPADDING))
2432 )
2433 {
2434 msg(M_FATAL, "management-external-key with TLS 1.3 or later requires "
2435 "nopadding argument/support");
2436 }
2437 #endif
2438 /*
2439 * Windows-specific options.
2440 */
2441
2442 #ifdef _WIN32
2443 if (dev == DEV_TYPE_TUN && !(pull || (options->ifconfig_local && options->ifconfig_remote_netmask)))
2444 {
2445 msg(M_USAGE, "On Windows, --ifconfig is required when --dev tun is used");
2446 }
2447
2448 if ((options->tuntap_options.ip_win32_defined)
2449 && !(pull || (options->ifconfig_local && options->ifconfig_remote_netmask)))
2450 {
2451 msg(M_USAGE, "On Windows, --ip-win32 doesn't make sense unless --ifconfig is also used");
2452 }
2453
2454 if (options->tuntap_options.dhcp_options
2455 && options->windows_driver != WINDOWS_DRIVER_WINTUN
2456 && options->tuntap_options.ip_win32_type != IPW32_SET_DHCP_MASQ
2457 && options->tuntap_options.ip_win32_type != IPW32_SET_ADAPTIVE)
2458 {
2459 msg(M_USAGE, "--dhcp-option requires --ip-win32 dynamic or adaptive");
2460 }
2461
2462 if (options->windows_driver == WINDOWS_DRIVER_WINTUN && dev != DEV_TYPE_TUN)
2463 {
2464 msg(M_USAGE, "--windows-driver wintun requires --dev tun");
2465 }
2466 #endif /* ifdef _WIN32 */
2467
2468 /*
2469 * Check that protocol options make sense.
2470 */
2471
2472 #ifdef ENABLE_FRAGMENT
2473 if (!proto_is_udp(ce->proto) && ce->fragment)
2474 {
2475 msg(M_USAGE, "--fragment can only be used with --proto udp");
2476 }
2477 #endif
2478
2479 if (!ce->remote && ce->proto == PROTO_TCP_CLIENT)
2480 {
2481 msg(M_USAGE, "--remote MUST be used in TCP Client mode");
2482 }
2483
2484 if ((ce->http_proxy_options) && ce->proto != PROTO_TCP_CLIENT)
2485 {
2486 msg(M_USAGE,
2487 "--http-proxy MUST be used in TCP Client mode (i.e. --proto "
2488 "tcp-client)");
2489 }
2490
2491 if ((ce->http_proxy_options) && !ce->http_proxy_options->server)
2492 {
2493 msg(M_USAGE,
2494 "--http-proxy not specified but other http proxy options present");
2495 }
2496
2497 if (ce->http_proxy_options && ce->socks_proxy_server)
2498 {
2499 msg(M_USAGE,
2500 "--http-proxy can not be used together with --socks-proxy");
2501 }
2502
2503 if (ce->socks_proxy_server && ce->proto == PROTO_TCP_SERVER)
2504 {
2505 msg(M_USAGE, "--socks-proxy can not be used in TCP Server mode");
2506 }
2507
2508 if (ce->proto == PROTO_TCP_SERVER && (options->connection_list->len > 1))
2509 {
2510 msg(M_USAGE, "TCP server mode allows at most one --remote address");
2511 }
2512
2513 /*
2514 * Check consistency of --mode server options.
2515 */
2516 if (options->mode == MODE_SERVER)
2517 {
2518 #define USAGE_VALID_SERVER_PROTOS "--mode server currently only supports " \
2519 "--proto values of udp, tcp-server, tcp4-server, or tcp6-server"
2520 #ifdef TARGET_ANDROID
2521 msg(M_FATAL, "--mode server not supported on Android");
2522 #endif
2523 if (!(dev == DEV_TYPE_TUN || dev == DEV_TYPE_TAP))
2524 {
2525 msg(M_USAGE, "--mode server only works with --dev tun or --dev tap");
2526 }
2527 if (options->pull)
2528 {
2529 msg(M_USAGE, "--pull cannot be used with --mode server");
2530 }
2531 if (options->pull_filter_list)
2532 {
2533 msg(M_WARN, "--pull-filter ignored for --mode server");
2534 }
2535 if (!(proto_is_udp(ce->proto) || ce->proto == PROTO_TCP_SERVER))
2536 {
2537 msg(M_USAGE, USAGE_VALID_SERVER_PROTOS);
2538 }
2539 #if PORT_SHARE
2540 if ((options->port_share_host || options->port_share_port)
2541 && (ce->proto != PROTO_TCP_SERVER))
2542 {
2543 msg(M_USAGE, "--port-share only works in TCP server mode "
2544 "(--proto values of tcp-server, tcp4-server, or tcp6-server)");
2545 }
2546 #endif
2547 if (!options->tls_server)
2548 {
2549 msg(M_USAGE, "--mode server requires --tls-server");
2550 }
2551 if (ce->remote)
2552 {
2553 msg(M_USAGE, "--remote cannot be used with --mode server");
2554 }
2555 if (!ce->bind_local)
2556 {
2557 msg(M_USAGE, "--nobind cannot be used with --mode server");
2558 }
2559 if (ce->http_proxy_options)
2560 {
2561 msg(M_USAGE, "--http-proxy cannot be used with --mode server");
2562 }
2563 if (ce->socks_proxy_server)
2564 {
2565 msg(M_USAGE, "--socks-proxy cannot be used with --mode server");
2566 }
2567 /* <connection> blocks force to have a remote embedded, so we check
2568 * for the --remote and bail out if it is present
2569 */
2570 if (options->connection_list->len >1
2571 || options->connection_list->array[0]->remote)
2572 {
2573 msg(M_USAGE, "<connection> cannot be used with --mode server");
2574 }
2575
2576 if (options->shaper)
2577 {
2578 msg(M_USAGE, "--shaper cannot be used with --mode server");
2579 }
2580 if (options->ipchange)
2581 {
2582 msg(M_USAGE,
2583 "--ipchange cannot be used with --mode server (use "
2584 "--client-connect instead)");
2585 }
2586 if (!(proto_is_dgram(ce->proto) || ce->proto == PROTO_TCP_SERVER))
2587 {
2588 msg(M_USAGE, USAGE_VALID_SERVER_PROTOS);
2589 }
2590 if (!proto_is_udp(ce->proto) && (options->cf_max || options->cf_per))
2591 {
2592 msg(M_USAGE, "--connect-freq only works with --mode server --proto udp. Try --max-clients instead.");
2593 }
2594 if (!(dev == DEV_TYPE_TAP || (dev == DEV_TYPE_TUN && options->topology == TOP_SUBNET)) && options->ifconfig_pool_netmask)
2595 {
2596 msg(M_USAGE, "The third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mode");
2597 }
2598 if (options->routes && (options->routes->flags & RG_ENABLE))
2599 {
2600 msg(M_USAGE, "--redirect-gateway cannot be used with --mode server (however --push \"redirect-gateway\" is fine)");
2601 }
2602 if (options->route_delay_defined)
2603 {
2604 msg(M_USAGE, "--route-delay cannot be used with --mode server");
2605 }
2606 if (options->up_delay)
2607 {
2608 msg(M_USAGE, "--up-delay cannot be used with --mode server");
2609 }
2610 if (!options->ifconfig_pool_defined
2611 && !options->ifconfig_ipv6_pool_defined
2612 && options->ifconfig_pool_persist_filename)
2613 {
2614 msg(M_USAGE,
2615 "--ifconfig-pool-persist must be used with --ifconfig-pool or --ifconfig-ipv6-pool");
2616 }
2617 if (options->ifconfig_ipv6_pool_defined && !options->ifconfig_ipv6_local)
2618 {
2619 msg(M_USAGE, "--ifconfig-ipv6-pool needs --ifconfig-ipv6");
2620 }
2621 if (options->allow_recursive_routing)
2622 {
2623 msg(M_USAGE, "--allow-recursive-routing cannot be used with --mode server");
2624 }
2625 if (options->auth_user_pass_file)
2626 {
2627 msg(M_USAGE, "--auth-user-pass cannot be used with --mode server (it should be used on the client side only)");
2628 }
2629 if (options->ccd_exclusive && !options->client_config_dir)
2630 {
2631 msg(M_USAGE, "--ccd-exclusive must be used with --client-config-dir");
2632 }
2633 if (options->auth_token_generate && !options->renegotiate_seconds)
2634 {
2635 msg(M_USAGE, "--auth-gen-token needs a non-infinite "
2636 "--renegotiate_seconds setting");
2637 }
2638 if (options->auth_token_generate && options->auth_token_renewal
2639 && options->auth_token_renewal < 2 * options->handshake_window)
2640 {
2641 msg(M_USAGE, "--auth-gen-token renewal time needs to be at least "
2642 " two times --hand-window (%d).",
2643 options->handshake_window);
2644
2645 }
2646 {
2647 const bool ccnr = (options->auth_user_pass_verify_script
2648 || PLUGIN_OPTION_LIST(options)
2649 || MAN_CLIENT_AUTH_ENABLED(options));
2650 const char *postfix = "must be used with --management-client-auth, an --auth-user-pass-verify script, or plugin";
2651 if ((options->ssl_flags & (SSLF_CLIENT_CERT_NOT_REQUIRED|SSLF_CLIENT_CERT_OPTIONAL)) && !ccnr)
2652 {
2653 msg(M_USAGE, "--verify-client-cert none|optional %s", postfix);
2654 }
2655 if ((options->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME) && !ccnr)
2656 {
2657 msg(M_USAGE, "--username-as-common-name %s", postfix);
2658 }
2659 if ((options->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL) && !ccnr)
2660 {
2661 msg(M_USAGE, "--auth-user-pass-optional %s", postfix);
2662 }
2663 }
2664
2665 if (options->vlan_tagging && dev != DEV_TYPE_TAP)
2666 {
2667 msg(M_USAGE, "--vlan-tagging must be used with --dev tap");
2668 }
2669 if (!options->vlan_tagging)
2670 {
2671 if (options->vlan_accept != defaults.vlan_accept)
2672 {
2673 msg(M_USAGE, "--vlan-accept requires --vlan-tagging");
2674 }
2675 if (options->vlan_pvid != defaults.vlan_pvid)
2676 {
2677 msg(M_USAGE, "--vlan-pvid requires --vlan-tagging");
2678 }
2679 }
2680 }
2681 else
2682 {
2683 /*
2684 * When not in server mode, err if parameters are
2685 * specified which require --mode server.
2686 */
2687 if (options->ifconfig_pool_defined || options->ifconfig_pool_persist_filename)
2688 {
2689 msg(M_USAGE, "--ifconfig-pool/--ifconfig-pool-persist requires --mode server");
2690 }
2691 if (options->ifconfig_ipv6_pool_defined)
2692 {
2693 msg(M_USAGE, "--ifconfig-ipv6-pool requires --mode server");
2694 }
2695 if (options->real_hash_size != defaults.real_hash_size
2696 || options->virtual_hash_size != defaults.virtual_hash_size)
2697 {
2698 msg(M_USAGE, "--hash-size requires --mode server");
2699 }
2700 if (options->learn_address_script)
2701 {
2702 msg(M_USAGE, "--learn-address requires --mode server");
2703 }
2704 if (options->client_connect_script)
2705 {
2706 msg(M_USAGE, "--client-connect requires --mode server");
2707 }
2708 if (options->client_crresponse_script)
2709 {
2710 msg(M_USAGE, "--client-crresponse requires --mode server");
2711 }
2712 if (options->client_disconnect_script)
2713 {
2714 msg(M_USAGE, "--client-disconnect requires --mode server");
2715 }
2716 if (options->client_config_dir || options->ccd_exclusive)
2717 {
2718 msg(M_USAGE, "--client-config-dir/--ccd-exclusive requires --mode server");
2719 }
2720 if (options->enable_c2c)
2721 {
2722 msg(M_USAGE, "--client-to-client requires --mode server");
2723 }
2724 if (options->duplicate_cn)
2725 {
2726 msg(M_USAGE, "--duplicate-cn requires --mode server");
2727 }
2728 if (options->cf_max || options->cf_per)
2729 {
2730 msg(M_USAGE, "--connect-freq requires --mode server");
2731 }
2732 if (options->ssl_flags & (SSLF_CLIENT_CERT_NOT_REQUIRED|SSLF_CLIENT_CERT_OPTIONAL))
2733 {
2734 msg(M_USAGE, "--verify-client-cert requires --mode server");
2735 }
2736 if (options->ssl_flags & SSLF_USERNAME_AS_COMMON_NAME)
2737 {
2738 msg(M_USAGE, "--username-as-common-name requires --mode server");
2739 }
2740 if (options->ssl_flags & SSLF_AUTH_USER_PASS_OPTIONAL)
2741 {
2742 msg(M_USAGE, "--auth-user-pass-optional requires --mode server");
2743 }
2744 if (options->ssl_flags & SSLF_OPT_VERIFY)
2745 {
2746 msg(M_USAGE, "--opt-verify requires --mode server");
2747 }
2748 if (options->server_flags & SF_TCP_NODELAY_HELPER)
2749 {
2750 msg(M_WARN, "WARNING: setting tcp-nodelay on the client side will not "
2751 "affect the server. To have TCP_NODELAY in both direction use "
2752 "tcp-nodelay in the server configuration instead.");
2753 }
2754 if (options->auth_user_pass_verify_script)
2755 {
2756 msg(M_USAGE, "--auth-user-pass-verify requires --mode server");
2757 }
2758 if (options->auth_token_generate)
2759 {
2760 msg(M_USAGE, "--auth-gen-token requires --mode server");
2761 }
2762 #if PORT_SHARE
2763 if (options->port_share_host || options->port_share_port)
2764 {
2765 msg(M_USAGE, "--port-share requires TCP server mode (--mode server --proto tcp-server)");
2766 }
2767 #endif
2768
2769 if (options->stale_routes_check_interval)
2770 {
2771 msg(M_USAGE, "--stale-routes-check requires --mode server");
2772 }
2773
2774 if (options->vlan_tagging)
2775 {
2776 msg(M_USAGE, "--vlan-tagging requires --mode server");
2777 }
2778 }
2779
2780 /*
2781 * Check consistency of replay options
2782 */
2783 if (!options->replay
2784 && (options->replay_window != defaults.replay_window
2785 || options->replay_time != defaults.replay_time))
2786 {
2787 msg(M_USAGE, "--replay-window doesn't make sense when replay protection is disabled with --no-replay");
2788 }
2789
2790 /*
2791 * SSL/TLS mode sanity checks.
2792 */
2793 if (options->tls_server + options->tls_client
2794 +(options->shared_secret_file != NULL) > 1)
2795 {
2796 msg(M_USAGE, "specify only one of --tls-server, --tls-client, or --secret");
2797 }
2798
2799 if (!options->tls_server && !options->tls_client)
2800 {
2801 msg(M_INFO, "DEPRECATION: No tls-client or tls-server option in "
2802 "configuration detected. OpenVPN 2.7 will remove the "
2803 "functionality to run a VPN without TLS. "
2804 "See the examples section in the manual page for "
2805 "examples of a similar quick setup with peer-fingerprint.");
2806 }
2807
2808 if (options->ssl_flags & (SSLF_CLIENT_CERT_NOT_REQUIRED|SSLF_CLIENT_CERT_OPTIONAL))
2809 {
2810 msg(M_WARN, "WARNING: POTENTIALLY DANGEROUS OPTION "
2811 "--verify-client-cert none|optional "
2812 "may accept clients which do not present a certificate");
2813 }
2814
2815 const int tls_version_max =
2816 (options->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT)
2817 & SSLF_TLS_VERSION_MAX_MASK;
2818 const int tls_version_min =
2819 (options->ssl_flags >> SSLF_TLS_VERSION_MIN_SHIFT)
2820 & SSLF_TLS_VERSION_MIN_MASK;
2821
2822 if (tls_version_max > 0 && tls_version_max < tls_version_min)
2823 {
2824 msg(M_USAGE, "--tls-version-min bigger than --tls-version-max");
2825 }
2826
2827 if (options->tls_server || options->tls_client)
2828 {
2829 check_ca_required(options);
2830 #ifdef ENABLE_PKCS11
2831 if (options->pkcs11_providers[0])
2832 {
2833 if (options->pkcs11_id_management && options->pkcs11_id != NULL)
2834 {
2835 msg(M_USAGE, "Parameter --pkcs11-id cannot be used when --pkcs11-id-management is also specified.");
2836 }
2837 if (!options->pkcs11_id_management && options->pkcs11_id == NULL)
2838 {
2839 msg(M_USAGE, "Parameter --pkcs11-id or --pkcs11-id-management should be specified.");
2840 }
2841 if (options->cert_file)
2842 {
2843 msg(M_USAGE, "Parameter --cert cannot be used when --pkcs11-provider is also specified.");
2844 }
2845 if (options->priv_key_file)
2846 {
2847 msg(M_USAGE, "Parameter --key cannot be used when --pkcs11-provider is also specified.");
2848 }
2849 #ifdef ENABLE_MANAGEMENT
2850 if (options->management_flags & MF_EXTERNAL_KEY)
2851 {
2852 msg(M_USAGE, "Parameter --management-external-key cannot be used when --pkcs11-provider is also specified.");
2853 }
2854 if (options->management_flags & MF_EXTERNAL_CERT)
2855 {
2856 msg(M_USAGE, "Parameter --management-external-cert cannot be used when --pkcs11-provider is also specified.");
2857 }
2858 #endif
2859 if (options->pkcs12_file)
2860 {
2861 msg(M_USAGE, "Parameter --pkcs12 cannot be used when --pkcs11-provider is also specified.");
2862 }
2863 #ifdef ENABLE_CRYPTOAPI
2864 if (options->cryptoapi_cert)
2865 {
2866 msg(M_USAGE, "Parameter --cryptoapicert cannot be used when --pkcs11-provider is also specified.");
2867 }
2868 #endif
2869 }
2870 else
2871 #endif /* ifdef ENABLE_PKCS11 */
2872 #ifdef ENABLE_MANAGEMENT
2873 if ((options->management_flags & MF_EXTERNAL_KEY) && options->priv_key_file)
2874 {
2875 msg(M_USAGE, "--key and --management-external-key are mutually exclusive");
2876 }
2877 else if ((options->management_flags & MF_EXTERNAL_CERT))
2878 {
2879 if (options->cert_file)
2880 {
2881 msg(M_USAGE, "--cert and --management-external-cert are mutually exclusive");
2882 }
2883 else if (!(options->management_flags & MF_EXTERNAL_KEY))
2884 {
2885 msg(M_USAGE, "--management-external-cert must be used with --management-external-key");
2886 }
2887 }
2888 else
2889 #endif
2890 #ifdef ENABLE_CRYPTOAPI
2891 if (options->cryptoapi_cert)
2892 {
2893 if (options->cert_file)
2894 {
2895 msg(M_USAGE, "Parameter --cert cannot be used when --cryptoapicert is also specified.");
2896 }
2897 if (options->priv_key_file)
2898 {
2899 msg(M_USAGE, "Parameter --key cannot be used when --cryptoapicert is also specified.");
2900 }
2901 if (options->pkcs12_file)
2902 {
2903 msg(M_USAGE, "Parameter --pkcs12 cannot be used when --cryptoapicert is also specified.");
2904 }
2905 #ifdef ENABLE_MANAGEMENT
2906 if (options->management_flags & MF_EXTERNAL_KEY)
2907 {
2908 msg(M_USAGE, "Parameter --management-external-key cannot be used when --cryptoapicert is also specified.");
2909 }
2910 if (options->management_flags & MF_EXTERNAL_CERT)
2911 {
2912 msg(M_USAGE, "Parameter --management-external-cert cannot be used when --cryptoapicert is also specified.");
2913 }
2914 #endif
2915 }
2916 else
2917 #endif /* ifdef ENABLE_CRYPTOAPI */
2918 if (options->pkcs12_file)
2919 {
2920 #ifdef ENABLE_CRYPTO_MBEDTLS
2921 msg(M_USAGE, "Parameter --pkcs12 cannot be used with the mbed TLS version version of OpenVPN.");
2922 #else
2923 if (options->ca_path)
2924 {
2925 msg(M_USAGE, "Parameter --capath cannot be used when --pkcs12 is also specified.");
2926 }
2927 if (options->cert_file)
2928 {
2929 msg(M_USAGE, "Parameter --cert cannot be used when --pkcs12 is also specified.");
2930 }
2931 if (options->priv_key_file)
2932 {
2933 msg(M_USAGE, "Parameter --key cannot be used when --pkcs12 is also specified.");
2934 }
2935 #ifdef ENABLE_MANAGEMENT
2936 if (options->management_flags & MF_EXTERNAL_KEY)
2937 {
2938 msg(M_USAGE, "Parameter --management-external-key cannot be used when --pkcs12 is also specified.");
2939 }
2940 if (options->management_flags & MF_EXTERNAL_CERT)
2941 {
2942 msg(M_USAGE, "Parameter --management-external-cert cannot be used when --pkcs12 is also specified.");
2943 }
2944 #endif
2945 #endif /* ifdef ENABLE_CRYPTO_MBEDTLS */
2946 }
2947 else
2948 {
2949 #ifdef ENABLE_CRYPTO_MBEDTLS
2950 if (options->ca_path)
2951 {
2952 msg(M_USAGE, "Parameter --capath cannot be used with the mbed TLS version version of OpenVPN.");
2953 }
2954 #endif /* ifdef ENABLE_CRYPTO_MBEDTLS */
2955 if (pull)
2956 {
2957
2958 const int sum =
2959 #ifdef ENABLE_MANAGEMENT
2960 ((options->cert_file != NULL) || (options->management_flags & MF_EXTERNAL_CERT))
2961 +((options->priv_key_file != NULL) || (options->management_flags & MF_EXTERNAL_KEY));
2962 #else
2963 (options->cert_file != NULL) + (options->priv_key_file != NULL);
2964 #endif
2965
2966 if (sum == 0)
2967 {
2968 if (!options->auth_user_pass_file)
2969 {
2970 msg(M_USAGE, "No client-side authentication method is "
2971 "specified. You must use either "
2972 "--cert/--key, --pkcs12, or "
2973 "--auth-user-pass");
2974 }
2975 }
2976 else if (sum == 2)
2977 {
2978 }
2979 else
2980 {
2981 msg(M_USAGE, "If you use one of --cert or --key, you must use them both");
2982 }
2983 }
2984 else
2985 {
2986 #ifdef ENABLE_MANAGEMENT
2987 if (!(options->management_flags & MF_EXTERNAL_CERT))
2988 #endif
2989 notnull(options->cert_file, "certificate file (--cert) or PKCS#12 file (--pkcs12)");
2990 #ifdef ENABLE_MANAGEMENT
2991 if (!(options->management_flags & MF_EXTERNAL_KEY))
2992 #endif
2993 notnull(options->priv_key_file, "private key file (--key) or PKCS#12 file (--pkcs12)");
2994 }
2995 }
2996 if (ce->tls_auth_file && ce->tls_crypt_file)
2997 {
2998 msg(M_USAGE, "--tls-auth and --tls-crypt are mutually exclusive");
2999 }
3000 if (options->tls_client && ce->tls_crypt_v2_file
3001 && (ce->tls_auth_file || ce->tls_crypt_file))
3002 {
3003 msg(M_USAGE, "--tls-crypt-v2, --tls-auth and --tls-crypt are mutually exclusive in client mode");
3004 }
3005 }
3006 else
3007 {
3008 /*
3009 * Make sure user doesn't specify any TLS options
3010 * when in non-TLS mode.
3011 */
3012
3013 #define MUST_BE_UNDEF(parm) if (options->parm != defaults.parm) {msg(M_USAGE, err, #parm); \
3014 }
3015
3016 const char err[] = "Parameter %s can only be specified in TLS-mode, i.e. where --tls-server or --tls-client is also specified.";
3017
3018 MUST_BE_UNDEF(ca_file);
3019 MUST_BE_UNDEF(ca_path);
3020 MUST_BE_UNDEF(dh_file);
3021 MUST_BE_UNDEF(cert_file);
3022 MUST_BE_UNDEF(priv_key_file);
3023 #ifndef ENABLE_CRYPTO_MBEDTLS
3024 MUST_BE_UNDEF(pkcs12_file);
3025 #endif
3026 MUST_BE_UNDEF(cipher_list);
3027 MUST_BE_UNDEF(cipher_list_tls13);
3028 MUST_BE_UNDEF(tls_cert_profile);
3029 MUST_BE_UNDEF(tls_verify);
3030 MUST_BE_UNDEF(tls_export_cert);
3031 MUST_BE_UNDEF(verify_x509_name);
3032 MUST_BE_UNDEF(tls_timeout);
3033 MUST_BE_UNDEF(renegotiate_bytes);
3034 MUST_BE_UNDEF(renegotiate_packets);
3035 MUST_BE_UNDEF(renegotiate_seconds);
3036 MUST_BE_UNDEF(handshake_window);
3037 MUST_BE_UNDEF(transition_window);
3038 MUST_BE_UNDEF(tls_auth_file);
3039 MUST_BE_UNDEF(tls_crypt_file);
3040 MUST_BE_UNDEF(tls_crypt_v2_file);
3041 MUST_BE_UNDEF(single_session);
3042 MUST_BE_UNDEF(push_peer_info);
3043 MUST_BE_UNDEF(tls_exit);
3044 MUST_BE_UNDEF(crl_file);
3045 MUST_BE_UNDEF(ns_cert_type);
3046 MUST_BE_UNDEF(remote_cert_ku[0]);
3047 MUST_BE_UNDEF(remote_cert_eku);
3048 #ifdef ENABLE_PKCS11
3049 MUST_BE_UNDEF(pkcs11_providers[0]);
3050 MUST_BE_UNDEF(pkcs11_private_mode[0]);
3051 MUST_BE_UNDEF(pkcs11_id);
3052 MUST_BE_UNDEF(pkcs11_id_management);
3053 #endif
3054
3055 if (pull)
3056 {
3057 msg(M_USAGE, err, "--pull");
3058 }
3059 }
3060 #undef MUST_BE_UNDEF
3061
3062 if (options->auth_user_pass_file && !options->pull)
3063 {
3064 msg(M_USAGE, "--auth-user-pass requires --pull");
3065 }
3066
3067 uninit_options(&defaults);
3068 }
3069
3070 static void
3071 options_postprocess_mutate_ce(struct options *o, struct connection_entry *ce)
3072 {
3073 const int dev = dev_type_enum(o->dev, o->dev_type);
3074
3075 if (o->server_defined || o->server_bridge_defined || o->server_bridge_proxy_dhcp)
3076 {
3077 if (ce->proto == PROTO_TCP)
3078 {
3079 ce->proto = PROTO_TCP_SERVER;
3080 }
3081 }
3082
3083 if (o->client)
3084 {
3085 if (ce->proto == PROTO_TCP)
3086 {
3087 ce->proto = PROTO_TCP_CLIENT;
3088 }
3089 }
3090
3091 /* an option is present that requires local bind to enabled */
3092 bool need_bind = ce->local || ce->local_port_defined || ce->bind_defined;
3093
3094 /* socks proxy is enabled */
3095 bool uses_socks = ce->proto == PROTO_UDP && ce->socks_proxy_server;
3096
3097 /* If binding is not forced by an explicit option and we have (at least)
3098 * one of --tcp-client, --pull (or --client), or socks we do not bind
3099 * locally to have "normal" IP client behaviour of a random source port */
3100 if (!need_bind && (ce->proto == PROTO_TCP_CLIENT || uses_socks || o->pull))
3101 {
3102 ce->bind_local = false;
3103 }
3104
3105 if (!ce->bind_local)
3106 {
3107 ce->local_port = NULL;
3108 }
3109
3110 /* if protocol forcing is enabled, disable all protocols
3111 * except for the forced one
3112 */
3113 if (o->proto_force >= 0 && o->proto_force != ce->proto)
3114 {
3115 ce->flags |= CE_DISABLED;
3116 }
3117
3118 /* our socks code is not fully IPv6 enabled yet (TCP works, UDP not)
3119 * so fall back to IPv4-only (trac #1221)
3120 */
3121 if (ce->socks_proxy_server && proto_is_udp(ce->proto) && ce->af != AF_INET)
3122 {
3123 if (ce->af == AF_INET6)
3124 {
3125 msg(M_INFO, "WARNING: '--proto udp6' is not compatible with "
3126 "'--socks-proxy' today. Forcing IPv4 mode." );
3127 }
3128 else
3129 {
3130 msg(M_INFO, "NOTICE: dual-stack mode for '--proto udp' does not "
3131 "work correctly with '--socks-proxy' today. Forcing IPv4." );
3132 }
3133 ce->af = AF_INET;
3134 }
3135
3136 /*
3137 * Set MTU defaults
3138 */
3139 {
3140 if (!ce->tun_mtu_defined && !ce->link_mtu_defined)
3141 {
3142 ce->tun_mtu_defined = true;
3143 }
3144 if ((dev == DEV_TYPE_TAP) && !ce->tun_mtu_extra_defined)
3145 {
3146 ce->tun_mtu_extra_defined = true;
3147 ce->tun_mtu_extra = TAP_MTU_EXTRA_DEFAULT;
3148 }
3149 }
3150
3151 /*
3152 * If --mssfix is supplied without a parameter or not specified at all,
3153 * default it to --fragment value, if --fragment is specified and otherwise
3154 * to the default if tun-mtu is 1500
3155 */
3156 if (o->ce.mssfix_default)
3157 {
3158 #ifdef ENABLE_FRAGMENT
3159 if (ce->fragment)
3160 {
3161 ce->mssfix = ce->fragment;
3162 }
3163 else
3164 #endif
3165 if (ce->tun_mtu_defined)
3166 {
3167 if (o->ce.tun_mtu == TUN_MTU_DEFAULT)
3168 {
3169 /* We want to only set mssfix default value if we use a default
3170 * MTU Size, otherwise the different size of tun should either
3171 * already solve the problem or mssfix might artifically make the
3172 * payload packets smaller without mssfix 0 */
3173 ce->mssfix = MSSFIX_DEFAULT;
3174 ce->mssfix_encap = true;
3175 }
3176 else
3177 {
3178 /* We still apply the mssfix value but only adjust it to the
3179 * size of the tun interface. */
3180 ce->mssfix = ce->tun_mtu;
3181 ce->mssfix_fixed = true;
3182 }
3183 }
3184 }
3185
3186 /*
3187 * Set per-connection block tls-auth/crypt/crypto-v2 fields if undefined.
3188 *
3189 * At the end only one of these will be really set because the parser
3190 * logic prevents configurations where more are set.
3191 */
3192 if (!ce->tls_auth_file && !ce->tls_crypt_file && !ce->tls_crypt_v2_file)
3193 {
3194 ce->tls_auth_file = o->tls_auth_file;
3195 ce->tls_auth_file_inline = o->tls_auth_file_inline;
3196 ce->key_direction = o->key_direction;
3197
3198 ce->tls_crypt_file = o->tls_crypt_file;
3199 ce->tls_crypt_file_inline = o->tls_crypt_file_inline;
3200
3201 ce->tls_crypt_v2_file = o->tls_crypt_v2_file;
3202 ce->tls_crypt_v2_file_inline = o->tls_crypt_v2_file_inline;
3203 }
3204
3205 /* Pre-cache tls-auth/crypt(-v2) key file if persist-key was specified and
3206 * keys were not already embedded in the config file.
3207 */
3208 if (o->persist_key)
3209 {
3210 connection_entry_preload_key(&ce->tls_auth_file,
3211 &ce->tls_auth_file_inline, &o->gc);
3212 connection_entry_preload_key(&ce->tls_crypt_file,
3213 &ce->tls_crypt_file_inline, &o->gc);
3214 connection_entry_preload_key(&ce->tls_crypt_v2_file,
3215 &ce->tls_crypt_v2_file_inline, &o->gc);
3216 }
3217
3218 if (!proto_is_udp(ce->proto) && ce->explicit_exit_notification)
3219 {
3220 msg(M_WARN, "NOTICE: --explicit-exit-notify ignored for --proto tcp");
3221 ce->explicit_exit_notification = 0;
3222 }
3223 }
3224
3225 #ifdef _WIN32
3226 /* If iservice is in use, we need def1 method for redirect-gateway */
3227 static void
3228 remap_redirect_gateway_flags(struct options *opt)
3229 {
3230 if (opt->routes
3231 && opt->route_method == ROUTE_METHOD_SERVICE
3232 && opt->routes->flags & RG_REROUTE_GW
3233 && !(opt->routes->flags & RG_DEF1))
3234 {
3235 msg(M_INFO, "Flag 'def1' added to --redirect-gateway (iservice is in use)");
3236 opt->routes->flags |= RG_DEF1;
3237 }
3238 }
3239 #endif
3240
3241 /*
3242 * Save/Restore certain option defaults before --pull is applied.
3243 */
3244
3245 static void
3246 pre_connect_save(struct options *o)
3247 {
3248 ALLOC_OBJ_CLEAR_GC(o->pre_connect, struct options_pre_connect, &o->gc);
3249 o->pre_connect->tuntap_options = o->tuntap_options;
3250 o->pre_connect->tuntap_options_defined = true;
3251 o->pre_connect->foreign_option_index = o->foreign_option_index;
3252
3253 if (o->routes)
3254 {
3255 o->pre_connect->routes = clone_route_option_list(o->routes, &o->gc);
3256 o->pre_connect->routes_defined = true;
3257 }
3258 if (o->routes_ipv6)
3259 {
3260 o->pre_connect->routes_ipv6 = clone_route_ipv6_option_list(o->routes_ipv6, &o->gc);
3261 o->pre_connect->routes_ipv6_defined = true;
3262 }
3263 if (o->client_nat)
3264 {
3265 o->pre_connect->client_nat = clone_client_nat_option_list(o->client_nat, &o->gc);
3266 o->pre_connect->client_nat_defined = true;
3267 }
3268
3269 o->pre_connect->route_default_gateway = o->route_default_gateway;
3270 o->pre_connect->route_ipv6_default_gateway = o->route_ipv6_default_gateway;
3271
3272 o->pre_connect->dns_options = clone_dns_options(o->dns_options, &o->gc);
3273
3274 /* NCP related options that can be overwritten by a push */
3275 o->pre_connect->ciphername = o->ciphername;
3276 o->pre_connect->authname = o->authname;
3277
3278 /* Ping related options should be reset to the config values on reconnect */
3279 o->pre_connect->ping_rec_timeout = o->ping_rec_timeout;
3280 o->pre_connect->ping_rec_timeout_action = o->ping_rec_timeout_action;
3281 o->pre_connect->ping_send_timeout = o->ping_send_timeout;
3282
3283 /* Miscellaneous Options */
3284 #ifdef USE_COMP
3285 o->pre_connect->comp = o->comp;
3286 #endif
3287 }
3288
3289 void
3290 pre_connect_restore(struct options *o, struct gc_arena *gc)
3291 {
3292 const struct options_pre_connect *pp = o->pre_connect;
3293 if (pp)
3294 {
3295 CLEAR(o->tuntap_options);
3296 if (pp->tuntap_options_defined)
3297 {
3298 o->tuntap_options = pp->tuntap_options;
3299 }
3300
3301 if (pp->routes_defined)
3302 {
3303 rol_check_alloc(o);
3304 copy_route_option_list(o->routes, pp->routes, gc);
3305 }
3306 else
3307 {
3308 o->routes = NULL;
3309 }
3310
3311 if (pp->routes_ipv6_defined)
3312 {
3313 rol6_check_alloc(o);
3314 copy_route_ipv6_option_list(o->routes_ipv6, pp->routes_ipv6, gc);
3315 }
3316 else
3317 {
3318 o->routes_ipv6 = NULL;
3319 }
3320
3321 o->route_default_gateway = pp->route_default_gateway;
3322 o->route_ipv6_default_gateway = pp->route_ipv6_default_gateway;
3323
3324 /* Free DNS options and reset them to pre-pull state */
3325 gc_free(&o->dns_options.gc);
3326 struct gc_arena dns_gc = gc_new();
3327 o->dns_options = clone_dns_options(pp->dns_options, &dns_gc);
3328 o->dns_options.gc = dns_gc;
3329
3330 if (pp->client_nat_defined)
3331 {
3332 cnol_check_alloc(o);
3333 copy_client_nat_option_list(o->client_nat, pp->client_nat);
3334 }
3335 else
3336 {
3337 o->client_nat = NULL;
3338 }
3339
3340 o->foreign_option_index = pp->foreign_option_index;
3341
3342 o->ciphername = pp->ciphername;
3343 o->authname = pp->authname;
3344
3345 o->ping_rec_timeout = pp->ping_rec_timeout;
3346 o->ping_rec_timeout_action = pp->ping_rec_timeout_action;
3347 o->ping_send_timeout = pp->ping_send_timeout;
3348
3349 /* Miscellaneous Options */
3350 #ifdef USE_COMP
3351 o->comp = pp->comp;
3352 #endif
3353 }
3354
3355 o->push_continuation = 0;
3356 o->push_option_types_found = 0;
3357 o->imported_protocol_flags = 0;
3358 }
3359
3360 static void
3361 options_postprocess_mutate_invariant(struct options *options)
3362 {
3363 #ifdef _WIN32
3364 const int dev = dev_type_enum(options->dev, options->dev_type);
3365
3366 /* when using wintun/ovpn-dco, kernel doesn't send DHCP requests, so don't use it */
3367 if ((options->windows_driver == WINDOWS_DRIVER_WINTUN
3368 || options->windows_driver == WINDOWS_DRIVER_DCO)
3369 && (options->tuntap_options.ip_win32_type == IPW32_SET_DHCP_MASQ
3370 || options->tuntap_options.ip_win32_type == IPW32_SET_ADAPTIVE))
3371 {
3372 options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
3373 }
3374
3375 if ((dev == DEV_TYPE_TUN || dev == DEV_TYPE_TAP) && !options->route_delay_defined)
3376 {
3377 /* delay may only be necessary when we perform DHCP handshake */
3378 const bool dhcp = (options->tuntap_options.ip_win32_type == IPW32_SET_DHCP_MASQ)
3379 || (options->tuntap_options.ip_win32_type == IPW32_SET_ADAPTIVE);
3380 if ((options->mode == MODE_POINT_TO_POINT) && dhcp)
3381 {
3382 options->route_delay_defined = true;
3383 options->route_delay = 5; /* Vista sometimes has a race without this */
3384 }
3385 }
3386
3387 if (options->ifconfig_noexec)
3388 {
3389 options->tuntap_options.ip_win32_type = IPW32_SET_MANUAL;
3390 options->ifconfig_noexec = false;
3391 }
3392
3393 remap_redirect_gateway_flags(options);
3394
3395 /*
3396 * Check consistency of --mode server options.
3397 */
3398 if (options->mode == MODE_SERVER)
3399 {
3400 /*
3401 * We need to explicitly set --tap-sleep because
3402 * we do not schedule event timers in the top-level context.
3403 */
3404 options->tuntap_options.tap_sleep = 10;
3405 if (options->route_delay_defined && options->route_delay)
3406 {
3407 options->tuntap_options.tap_sleep = options->route_delay;
3408 }
3409 options->route_delay_defined = false;
3410 }
3411 #endif /* ifdef _WIN32 */
3412
3413 #ifdef DEFAULT_PKCS11_MODULE
3414 /* If p11-kit is present on the system then load its p11-kit-proxy.so
3415 * by default if the user asks for PKCS#11 without otherwise specifying
3416 * the module to use. */
3417 if (!options->pkcs11_providers[0]
3418 && (options->pkcs11_id || options->pkcs11_id_management))
3419 {
3420 options->pkcs11_providers[0] = DEFAULT_PKCS11_MODULE;
3421 }
3422 #endif
3423 }
3424
3425 static void
3426 options_postprocess_verify(const struct options *o)
3427 {
3428 if (o->connection_list)
3429 {
3430 int i;
3431 for (i = 0; i < o->connection_list->len; ++i)
3432 {
3433 options_postprocess_verify_ce(o, o->connection_list->array[i]);
3434 }
3435 }
3436 else
3437 {
3438 options_postprocess_verify_ce(o, &o->ce);
3439 }
3440
3441 dns_options_verify(M_FATAL, &o->dns_options);
3442
3443 if (dco_enabled(o) && o->enable_c2c)
3444 {
3445 msg(M_WARN, "Note: --client-to-client has no effect when using data "
3446 "channel offload: packets are always sent to the VPN "
3447 "interface and then routed based on the system routing table");
3448 }
3449 }
3450
3451 /**
3452 * Checks for availibility of Chacha20-Poly1305 and sets
3453 * the ncp_cipher to either AES-256-GCM:AES-128-GCM or
3454 * AES-256-GCM:AES-128-GCM:CHACHA20-POLY1305.
3455 */
3456 static void
3457 options_postprocess_setdefault_ncpciphers(struct options *o)
3458 {
3459 if (o->ncp_ciphers)
3460 {
3461 /* custom --data-ciphers set, keep list */
3462 return;
3463 }
3464
3465 /* check if crypto library supports chacha */
3466 bool can_do_chacha = cipher_valid("CHACHA20-POLY1305");
3467
3468 if (can_do_chacha && dco_enabled(o))
3469 {
3470 /* also make sure that dco supports chacha */
3471 can_do_chacha = tls_item_in_cipher_list("CHACHA20-POLY1305", dco_get_supported_ciphers());
3472 }
3473
3474 if (can_do_chacha)
3475 {
3476 o->ncp_ciphers = "AES-256-GCM:AES-128-GCM:CHACHA20-POLY1305";
3477 }
3478 else
3479 {
3480 o->ncp_ciphers = "AES-256-GCM:AES-128-GCM";
3481 }
3482 }
3483
3484 static void
3485 options_postprocess_cipher(struct options *o)
3486 {
3487 if (!o->pull && !(o->mode == MODE_SERVER))
3488 {
3489 /* If the cipher is not set, use the old default of BF-CBC. We will
3490 * warn that this is deprecated on cipher initialisation, no need
3491 * to warn here as well */
3492 if (!o->ciphername)
3493 {
3494 o->ciphername = "BF-CBC";
3495 }
3496 else
3497 {
3498 o->enable_ncp_fallback = true;
3499 }
3500 return;
3501 }
3502
3503 /* pull or P2MP mode */
3504 if (!o->ciphername)
3505 {
3506 /* We still need to set the ciphername to BF-CBC since various other
3507 * parts of OpenVPN assert that the ciphername is set */
3508 o->ciphername = "BF-CBC";
3509
3510 msg(M_INFO, "Note: --cipher is not set. OpenVPN versions before 2.5 "
3511 "defaulted to BF-CBC as fallback when cipher negotiation "
3512 "failed in this case. If you need this fallback please add "
3513 "'--data-ciphers-fallback BF-CBC' to your configuration "
3514 "and/or add BF-CBC to --data-ciphers.");
3515 }
3516 else if (!o->enable_ncp_fallback
3517 && !tls_item_in_cipher_list(o->ciphername, o->ncp_ciphers))
3518 {
3519 msg(M_WARN, "DEPRECATED OPTION: --cipher set to '%s' but missing in "
3520 "--data-ciphers (%s). OpenVPN ignores --cipher for cipher "
3521 "negotiations. ",
3522 o->ciphername, o->ncp_ciphers);
3523 }
3524 }
3525
3526 /**
3527 * The option --compat-mode is used to set up default settings to values
3528 * used on the specified openvpn version and earlier.
3529 *
3530 * This function is used in various "default option" paths to test if the
3531 * user requested compatibility with a version before the one specified
3532 * as argument. This way some default settings can be automatically
3533 * altered to guarantee compatibility with the version specified by the
3534 * user via --compat-mode.
3535 *
3536 * @param version need compatibility with openvpn versions before the
3537 * one specified (20401 = before 2.4.1)
3538 * @return whether compatibility should be enabled
3539 */
3540 static bool
3541 need_compatibility_before(const struct options *o, unsigned int version)
3542 {
3543 return o->backwards_compatible != 0 && o->backwards_compatible < version;
3544 }
3545
3546 /**
3547 * Changes default values so that OpenVPN can be compatible with the user
3548 * specified version
3549 */
3550 static void
3551 options_set_backwards_compatible_options(struct options *o)
3552 {
3553 /* TLS min version is not set */
3554 int tls_ver_min = (o->ssl_flags >> SSLF_TLS_VERSION_MIN_SHIFT)
3555 & SSLF_TLS_VERSION_MIN_MASK;
3556 if (tls_ver_min == 0)
3557 {
3558 int tls_ver_max = (o->ssl_flags >> SSLF_TLS_VERSION_MAX_SHIFT)
3559 & SSLF_TLS_VERSION_MAX_MASK;
3560 if (need_compatibility_before(o, 20307))
3561 {
3562 /* 2.3.6 and earlier have TLS 1.0 only, set minimum to TLS 1.0 */
3563 o->ssl_flags |= (TLS_VER_1_0 << SSLF_TLS_VERSION_MIN_SHIFT);
3564 }
3565 else if (tls_ver_max == 0 || tls_ver_max >= TLS_VER_1_2)
3566 {
3567 /* Use TLS 1.2 as proper default */
3568 o->ssl_flags |= (TLS_VER_1_2 << SSLF_TLS_VERSION_MIN_SHIFT);
3569 }
3570 else
3571 {
3572 /* Maximize the minimum version */
3573 o->ssl_flags |= (tls_ver_max << SSLF_TLS_VERSION_MIN_SHIFT);
3574 }
3575 }
3576
3577 if (need_compatibility_before(o, 20400))
3578 {
3579 if (!o->ciphername)
3580 {
3581 /* If ciphername is not set default to BF-CBC when targeting these
3582 * old versions that do not have NCP */
3583 o->ciphername = "BF-CBC";
3584 }
3585 /* Versions < 2.4.0 additionally might be compiled with --enable-small and
3586 * not have OCC strings required for "poor man's NCP" */
3587 o->enable_ncp_fallback = true;
3588 }
3589
3590 /* Versions < 2.5.0 do need --cipher in the list of accepted ciphers.
3591 * Version 2.4 probably does not need it but NCP was not so
3592 * good with 2.4 and ncp-disable might be more common on 2.4 peers.
3593 * Only do this iff --cipher is set (explicitly or by compat mode
3594 * < 2.4.0, see above). This is not 100% correct backwards compatible
3595 * behaviour but 2.5 already behaved like this */
3596 if (o->ciphername && need_compatibility_before(o, 20500)
3597 && !tls_item_in_cipher_list(o->ciphername, o->ncp_ciphers))
3598 {
3599 append_cipher_to_ncp_list(o, o->ciphername);
3600 }
3601
3602 #ifdef USE_COMP
3603 /* Compression is deprecated and we do not want to announce support for it
3604 * by default anymore, additionally DCO breaks with compression.
3605 *
3606 * Disable compression by default starting with 2.6.0 if no other
3607 * compression related option has been explicitly set */
3608 if (!comp_non_stub_enabled(&o->comp) && !need_compatibility_before(o, 20600)
3609 && (o->comp.flags == 0))
3610 {
3611 o->comp.flags = COMP_F_ALLOW_STUB_ONLY|COMP_F_ADVERTISE_STUBS_ONLY;
3612 }
3613 #endif
3614 }
3615
3616 static void
3617 options_postprocess_mutate(struct options *o, struct env_set *es)
3618 {
3619 int i;
3620 /*
3621 * Process helper-type options which map to other, more complex
3622 * sequences of options.
3623 */
3624 helper_client_server(o);
3625 helper_keepalive(o);
3626 helper_tcp_nodelay(o);
3627
3628 options_postprocess_setdefault_ncpciphers(o);
3629 options_set_backwards_compatible_options(o);
3630
3631 options_postprocess_cipher(o);
3632 o->ncp_ciphers = mutate_ncp_cipher_list(o->ncp_ciphers, &o->gc);
3633 if (o->ncp_ciphers == NULL)
3634 {
3635 msg(M_USAGE, "--data-ciphers list contains unsupported ciphers or is too long.");
3636 }
3637
3638 if (o->remote_list && !o->connection_list)
3639 {
3640 /*
3641 * Convert remotes into connection list
3642 */
3643 const struct remote_list *rl = o->remote_list;
3644 for (i = 0; i < rl->len; ++i)
3645 {
3646 const struct remote_entry *re = rl->array[i];
3647 struct connection_entry ce = o->ce;
3648 struct connection_entry *ace;
3649
3650 ASSERT(re->remote);
3651 connection_entry_load_re(&ce, re);
3652 ace = alloc_connection_entry(o, M_USAGE);
3653 ASSERT(ace);
3654 *ace = ce;
3655 }
3656 }
3657 else if (!o->remote_list && !o->connection_list)
3658 {
3659 struct connection_entry *ace;
3660 ace = alloc_connection_entry(o, M_USAGE);
3661 ASSERT(ace);
3662 *ace = o->ce;
3663 }
3664
3665 ASSERT(o->connection_list);
3666 for (i = 0; i < o->connection_list->len; ++i)
3667 {
3668 options_postprocess_mutate_ce(o, o->connection_list->array[i]);
3669 }
3670
3671 if (o->tls_server)
3672 {
3673 /* Check that DH file is specified, or explicitly disabled */
3674 notnull(o->dh_file, "DH file (--dh)");
3675 if (streq(o->dh_file, "none"))
3676 {
3677 o->dh_file = NULL;
3678 }
3679 }
3680 else if (o->dh_file)
3681 {
3682 /* DH file is only meaningful in a tls-server context. */
3683 msg(M_WARN, "WARNING: Ignoring option 'dh' in tls-client mode, please only "
3684 "include this in your server configuration");
3685 o->dh_file = NULL;
3686 }
3687 #if ENABLE_MANAGEMENT
3688 if (o->http_proxy_override)
3689 {
3690 options_postprocess_http_proxy_override(o);
3691 }
3692 #endif
3693 if (!o->ca_file && !o->ca_path && o->verify_hash
3694 && o->verify_hash_depth == 0)
3695 {
3696 msg(M_INFO, "Using certificate fingerprint to verify peer (no CA "
3697 "option set). ");
3698 o->verify_hash_no_ca = true;
3699 }
3700
3701 if (o->config && streq(o->config, "stdin") && o->remap_sigusr1 == SIGHUP)
3702 {
3703 msg(M_USAGE, "Options 'config stdin' and 'remap-usr1 SIGHUP' are "
3704 "incompatible with each other.");
3705 }
3706
3707 if (dco_enabled(o))
3708 {
3709 /* check if any option should force disabling DCO */
3710 o->tuntap_options.disable_dco = !dco_check_option(D_DCO, o)
3711 || !dco_check_startup_option(D_DCO, o);
3712 }
3713
3714 #ifdef _WIN32
3715 if (dco_enabled(o))
3716 {
3717 o->windows_driver = WINDOWS_DRIVER_DCO;
3718 }
3719 else
3720 {
3721 if (o->windows_driver == WINDOWS_DRIVER_DCO)
3722 {
3723 msg(M_WARN, "Option --windows-driver ovpn-dco is ignored because Data Channel Offload is disabled");
3724 o->windows_driver = WINDOWS_DRIVER_TAP_WINDOWS6;
3725 }
3726 else if (o->windows_driver == WINDOWS_DRIVER_UNSPECIFIED)
3727 {
3728 o->windows_driver = WINDOWS_DRIVER_TAP_WINDOWS6;
3729 }
3730 }
3731 #endif
3732
3733 if (dco_enabled(o) && o->dev_node)
3734 {
3735 msg(M_WARN, "Note: ignoring --dev-node as it has no effect when using "
3736 "data channel offload");
3737 o->dev_node = NULL;
3738 }
3739
3740 /* this depends on o->windows_driver, which is set above */
3741 options_postprocess_mutate_invariant(o);
3742
3743 /*
3744 * Save certain parms before modifying options during connect, especially
3745 * when using --pull
3746 */
3747 if (o->pull)
3748 {
3749 dns_options_preprocess_pull(&o->dns_options);
3750 }
3751 else
3752 {
3753 #if defined(_WIN32) || defined(TARGET_ANDROID)
3754 tuntap_options_copy_dns(o);
3755 #else
3756 foreign_options_copy_dns(o, es);
3757 #endif
3758 }
3759 if (o->auth_token_generate && !o->auth_token_renewal)
3760 {
3761 o->auth_token_renewal = o->renegotiate_seconds;
3762 }
3763 pre_connect_save(o);
3764 }
3765
3766 /*
3767 * Check file/directory sanity
3768 *
3769 */
3770 #ifndef ENABLE_SMALL /** Expect people using the stripped down version to know what they do */
3771
3772 #define CHKACC_FILE (1<<0) /** Check for a file/directory presence */
3773 #define CHKACC_DIRPATH (1<<1) /** Check for directory presence where a file should reside */
3774 #define CHKACC_FILEXSTWR (1<<2) /** If file exists, is it writable? */
3775 #define CHKACC_ACPTSTDIN (1<<3) /** If filename is stdin, it's allowed and "exists" */
3776 #define CHKACC_PRIVATE (1<<4) /** Warn if this (private) file is group/others accessible */
3777
3778 static bool
3779 check_file_access(const int type, const char *file, const int mode, const char *opt)
3780 {
3781 int errcode = 0;
3782
3783 /* If no file configured, no errors to look for */
3784 if (!file)
3785 {
3786 return false;
3787 }
3788
3789 /* If stdin is allowed and the file name is 'stdin', then do no
3790 * further checks as stdin is always available
3791 */
3792 if ( (type & CHKACC_ACPTSTDIN) && streq(file, "stdin") )
3793 {
3794 return false;
3795 }
3796
3797 /* Is the directory path leading to the given file accessible? */
3798 if (type & CHKACC_DIRPATH)
3799 {
3800 char *fullpath = string_alloc(file, NULL); /* POSIX dirname() implementation may modify its arguments */
3801 char *dirpath = dirname(fullpath);
3802
3803 if (platform_access(dirpath, mode|X_OK) != 0)
3804 {
3805 errcode = errno;
3806 }
3807 free(fullpath);
3808 }
3809
3810 /* Is the file itself accessible? */
3811 if (!errcode && (type & CHKACC_FILE) && (platform_access(file, mode) != 0) )
3812 {
3813 errcode = errno;
3814 }
3815
3816 /* If the file exists and is accessible, is it writable? */
3817 if (!errcode && (type & CHKACC_FILEXSTWR) && (platform_access(file, F_OK) == 0) )
3818 {
3819 if (platform_access(file, W_OK) != 0)
3820 {
3821 errcode = errno;
3822 }
3823 }
3824
3825 /* Warn if a given private file is group/others accessible. */
3826 if (type & CHKACC_PRIVATE)
3827 {
3828 platform_stat_t st;
3829 if (platform_stat(file, &st))
3830 {
3831 msg(M_WARN | M_ERRNO, "WARNING: cannot stat file '%s'", file);
3832 }
3833 #ifndef _WIN32
3834 else
3835 {
3836 if (st.st_mode & (S_IRWXG|S_IRWXO))
3837 {
3838 msg(M_WARN, "WARNING: file '%s' is group or others accessible", file);
3839 }
3840 }
3841 #endif
3842 }
3843
3844 /* Scream if an error is found */
3845 if (errcode > 0)
3846 {
3847 msg(M_NOPREFIX | M_OPTERR | M_ERRNO, "%s fails with '%s'", opt, file);
3848 }
3849
3850 /* Return true if an error occurred */
3851 return (errcode != 0 ? true : false);
3852 }
3853
3854 /* A wrapper for check_file_access() which also takes a chroot directory.
3855 * If chroot is NULL, behaviour is exactly the same as calling check_file_access() directly,
3856 * otherwise it will look for the file inside the given chroot directory instead.
3857 */
3858 static bool
3859 check_file_access_chroot(const char *chroot, const int type, const char *file, const int mode, const char *opt)
3860 {
3861 bool ret = false;
3862
3863 /* If no file configured, no errors to look for */
3864 if (!file)
3865 {
3866 return false;
3867 }
3868
3869 /* If chroot is set, look for the file/directory inside the chroot */
3870 if (chroot)
3871 {
3872 struct gc_arena gc = gc_new();
3873 struct buffer chroot_file;
3874
3875 chroot_file = prepend_dir(chroot, file, &gc);
3876 ret = check_file_access(type, BSTR(&chroot_file), mode, opt);
3877 gc_free(&gc);
3878 }
3879 else
3880 {
3881 /* No chroot in play, just call core file check function */
3882 ret = check_file_access(type, file, mode, opt);
3883 }
3884 return ret;
3885 }
3886
3887 /**
3888 * A wrapper for check_file_access_chroot() that returns false immediately if
3889 * the file is inline (and therefore there is no access to check)
3890 */
3891 static bool
3892 check_file_access_chroot_inline(bool is_inline, const char *chroot,
3893 const int type, const char *file,
3894 const int mode, const char *opt)
3895 {
3896 if (is_inline)
3897 {
3898 return false;
3899 }
3900
3901 return check_file_access_chroot(chroot, type, file, mode, opt);
3902 }
3903
3904 /**
3905 * A wrapper for check_file_access() that returns false immediately if the file
3906 * is inline (and therefore there is no access to check)
3907 */
3908 static bool
3909 check_file_access_inline(bool is_inline, const int type, const char *file,
3910 const int mode, const char *opt)
3911 {
3912 if (is_inline)
3913 {
3914 return false;
3915 }
3916
3917 return check_file_access(type, file, mode, opt);
3918 }
3919
3920 /*
3921 * Verifies that the path in the "command" that comes after certain script options (e.g., --up) is a
3922 * valid file with appropriate permissions.
3923 *
3924 * "command" consists of a path, optionally followed by a space, which may be
3925 * followed by arbitrary arguments. It is NOT a full shell command line -- shell expansion is not
3926 * performed.
3927 *
3928 * The path and arguments in "command" may be single- or double-quoted or escaped.
3929 *
3930 * The path is extracted from "command", then check_file_access() is called to check it. The
3931 * arguments, if any, are ignored.
3932 *
3933 * Note that the type, mode, and opt arguments to this routine are the same as the corresponding
3934 * check_file_access() arguments.
3935 */
3936 static bool
3937 check_cmd_access(const char *command, const char *opt, const char *chroot)
3938 {
3939 struct argv argv;
3940 bool return_code;
3941
3942 /* If no command was set, there are no errors to look for */
3943 if (!command)
3944 {
3945 return false;
3946 }
3947
3948 /* Extract executable path and arguments */
3949 argv = argv_new();
3950 argv_parse_cmd(&argv, command);
3951
3952 /* if an executable is specified then check it; otherwise, complain */
3953 if (argv.argv[0])
3954 {
3955 /* Scripts requires R_OK as well, but that might fail on binaries which
3956 * only requires X_OK to function on Unix - a scenario not unlikely to
3957 * be seen on suid binaries.
3958 */
3959 return_code = check_file_access_chroot(chroot, CHKACC_FILE, argv.argv[0], X_OK, opt);
3960 }
3961 else
3962 {
3963 msg(M_NOPREFIX|M_OPTERR, "%s fails with '%s': No path to executable.",
3964 opt, command);
3965 return_code = true;
3966 }
3967
3968 argv_free(&argv);
3969
3970 return return_code;
3971 }
3972
3973 /*
3974 * Sanity check of all file/dir options. Checks that file/dir
3975 * is accessible by OpenVPN
3976 */
3977 static void
3978 options_postprocess_filechecks(struct options *options)
3979 {
3980 bool errs = false;
3981
3982 /* ** SSL/TLS/crypto related files ** */
3983 errs |= check_file_access_inline(options->dh_file_inline, CHKACC_FILE,
3984 options->dh_file, R_OK, "--dh");
3985
3986 if (!options->verify_hash_no_ca)
3987 {
3988 errs |= check_file_access_inline(options->ca_file_inline, CHKACC_FILE,
3989 options->ca_file, R_OK, "--ca");
3990 }
3991
3992 errs |= check_file_access_chroot(options->chroot_dir, CHKACC_FILE,
3993 options->ca_path, R_OK, "--capath");
3994
3995 errs |= check_file_access_inline(options->cert_file_inline, CHKACC_FILE,
3996 options->cert_file, R_OK, "--cert");
3997
3998 errs |= check_file_access_inline(options->extra_certs_file, CHKACC_FILE,
3999 options->extra_certs_file, R_OK,
4000 "--extra-certs");
4001
4002 #ifdef ENABLE_MANAGMENT
4003 if (!(options->management_flags & MF_EXTERNAL_KEY))
4004 #endif
4005 {
4006 errs |= check_file_access_inline(options->priv_key_file_inline,
4007 CHKACC_FILE|CHKACC_PRIVATE,
4008 options->priv_key_file, R_OK, "--key");
4009 }
4010
4011 errs |= check_file_access_inline(options->pkcs12_file_inline,
4012 CHKACC_FILE|CHKACC_PRIVATE,
4013 options->pkcs12_file, R_OK, "--pkcs12");
4014
4015 if (options->ssl_flags & SSLF_CRL_VERIFY_DIR)
4016 {
4017 errs |= check_file_access_chroot(options->chroot_dir, CHKACC_FILE,
4018 options->crl_file, R_OK|X_OK,
4019 "--crl-verify directory");
4020 }
4021 else
4022 {
4023 errs |= check_file_access_chroot_inline(options->crl_file_inline,
4024 options->chroot_dir,
4025 CHKACC_FILE, options->crl_file,
4026 R_OK, "--crl-verify");
4027 }
4028
4029 ASSERT(options->connection_list);
4030 for (int i = 0; i < options->connection_list->len; ++i)
4031 {
4032 struct connection_entry *ce = options->connection_list->array[i];
4033
4034 errs |= check_file_access_inline(ce->tls_auth_file_inline,
4035 CHKACC_FILE|CHKACC_PRIVATE,
4036 ce->tls_auth_file, R_OK,
4037 "--tls-auth");
4038 errs |= check_file_access_inline(ce->tls_crypt_file_inline,
4039 CHKACC_FILE|CHKACC_PRIVATE,
4040 ce->tls_crypt_file, R_OK,
4041 "--tls-crypt");
4042 errs |= check_file_access_inline(ce->tls_crypt_v2_file_inline,
4043 CHKACC_FILE|CHKACC_PRIVATE,
4044 ce->tls_crypt_v2_file, R_OK,
4045 "--tls-crypt-v2");
4046 }
4047
4048 errs |= check_file_access_inline(options->shared_secret_file_inline,
4049 CHKACC_FILE|CHKACC_PRIVATE,
4050 options->shared_secret_file, R_OK,
4051 "--secret");
4052
4053 errs |= check_file_access(CHKACC_DIRPATH|CHKACC_FILEXSTWR,
4054 options->packet_id_file, R_OK|W_OK, "--replay-persist");
4055
4056 /* ** Password files ** */
4057 errs |= check_file_access(CHKACC_FILE|CHKACC_ACPTSTDIN|CHKACC_PRIVATE,
4058 options->key_pass_file, R_OK, "--askpass");
4059 #ifdef ENABLE_MANAGEMENT
4060 errs |= check_file_access(CHKACC_FILE|CHKACC_ACPTSTDIN|CHKACC_PRIVATE,
4061 options->management_user_pass, R_OK,
4062 "--management user/password file");
4063 #endif /* ENABLE_MANAGEMENT */
4064 errs |= check_file_access_inline(options->auth_user_pass_file_inline,
4065 CHKACC_FILE|CHKACC_ACPTSTDIN|CHKACC_PRIVATE,
4066 options->auth_user_pass_file, R_OK,
4067 "--auth-user-pass");
4068 /* ** System related ** */
4069 errs |= check_file_access(CHKACC_FILE, options->chroot_dir,
4070 R_OK|X_OK, "--chroot directory");
4071 errs |= check_file_access(CHKACC_DIRPATH|CHKACC_FILEXSTWR, options->writepid,
4072 R_OK|W_OK, "--writepid");
4073
4074 /* ** Log related ** */
4075 errs |= check_file_access(CHKACC_DIRPATH|CHKACC_FILEXSTWR, options->status_file,
4076 R_OK|W_OK, "--status");
4077
4078 /* ** Config related ** */
4079 errs |= check_file_access_chroot(options->chroot_dir, CHKACC_FILE, options->tls_export_cert,
4080 R_OK|W_OK|X_OK, "--tls-export-cert");
4081 errs |= check_file_access_chroot(options->chroot_dir, CHKACC_FILE, options->client_config_dir,
4082 R_OK|X_OK, "--client-config-dir");
4083 errs |= check_file_access_chroot(options->chroot_dir, CHKACC_FILE, options->tmp_dir,
4084 R_OK|W_OK|X_OK, "Temporary directory (--tmp-dir)");
4085
4086 if (errs)
4087 {
4088 msg(M_USAGE, "Please correct these errors.");
4089 }
4090 }
4091 #endif /* !ENABLE_SMALL */
4092
4093 /*
4094 * Sanity check on options.
4095 * Also set some options based on other
4096 * options.
4097 */
4098 void
4099 options_postprocess(struct options *options, struct env_set *es)
4100 {
4101 options_postprocess_mutate(options, es);
4102 options_postprocess_verify(options);
4103 #ifndef ENABLE_SMALL
4104 options_postprocess_filechecks(options);
4105 #endif /* !ENABLE_SMALL */
4106 }
4107
4108 /*
4109 * Sanity check on options after more options were pulled from server.
4110 * Also time to modify some options based on other options.
4111 */
4112 bool
4113 options_postprocess_pull(struct options *o, struct env_set *es)
4114 {
4115 bool success = dns_options_verify(D_PUSH_ERRORS, &o->dns_options);
4116 if (success)
4117 {
4118 dns_options_postprocess_pull(&o->dns_options);
4119 setenv_dns_options(&o->dns_options, es);
4120 #if defined(_WIN32) || defined(TARGET_ANDROID)
4121 tuntap_options_copy_dns(o);
4122 #else
4123 foreign_options_copy_dns(o, es);
4124 #endif
4125 }
4126 return success;
4127 }
4128
4129 /*
4130 * Build an options string to represent data channel encryption options.
4131 * This string must match exactly between peers. The keysize is checked
4132 * separately by read_key().
4133 *
4134 * The following options must match on both peers:
4135 *
4136 * Tunnel options:
4137 *
4138 * --dev tun|tap [unit number need not match]
4139 * --dev-type tun|tap
4140 * --link-mtu
4141 * --udp-mtu
4142 * --tun-mtu
4143 * --proto udp
4144 * --proto tcp-client [matched with --proto tcp-server
4145 * on the other end of the connection]
4146 * --proto tcp-server [matched with --proto tcp-client on
4147 * the other end of the connection]
4148 * --tun-ipv6
4149 * --ifconfig x y [matched with --ifconfig y x on
4150 * the other end of the connection]
4151 *
4152 * --comp-lzo
4153 * --compress alg
4154 * --fragment
4155 *
4156 * Crypto Options:
4157 *
4158 * --cipher
4159 * --auth
4160 * --keysize
4161 * --secret
4162 * --no-replay
4163 *
4164 * SSL Options:
4165 *
4166 * --tls-auth
4167 * --tls-client [matched with --tls-server on
4168 * the other end of the connection]
4169 * --tls-server [matched with --tls-client on
4170 * the other end of the connection]
4171 */
4172 char *
4173 options_string(const struct options *o,
4174 const struct frame *frame,
4175 struct tuntap *tt,
4176 openvpn_net_ctx_t *ctx,
4177 bool remote,
4178 struct gc_arena *gc)
4179 {
4180 struct buffer out = alloc_buf(OPTION_LINE_SIZE);
4181 bool tt_local = false;
4182
4183 buf_printf(&out, "V4");
4184
4185 /*
4186 * Tunnel Options
4187 */
4188
4189 buf_printf(&out, ",dev-type %s", dev_type_string(o->dev, o->dev_type));
4190 /* the link-mtu that we send has only a meaning if have a fixed
4191 * cipher (p2p) or have a fallback cipher configured for older non
4192 * ncp clients. But not sending it will make even 2.4 complain
4193 * about it being missing. So still send it. */
4194 buf_printf(&out, ",link-mtu %u",
4195 (unsigned int) calc_options_string_link_mtu(o, frame));
4196
4197 if (o->ce.occ_mtu != 0)
4198 {
4199 buf_printf(&out, ",tun-mtu %d", o->ce.occ_mtu);
4200 }
4201 else
4202 {
4203 buf_printf(&out, ",tun-mtu %d", frame->tun_mtu);
4204 }
4205
4206 buf_printf(&out, ",proto %s", proto_remote(o->ce.proto, remote));
4207
4208 bool p2p_nopull = o->mode == MODE_POINT_TO_POINT && !PULL_DEFINED(o);
4209 /* send tun_ipv6 only in peer2peer mode - in client/server mode, it
4210 * is usually pushed by the server, triggering a non-helpful warning
4211 */
4212 if (o->ifconfig_ipv6_local && p2p_nopull)
4213 {
4214 buf_printf(&out, ",tun-ipv6");
4215 }
4216
4217 /*
4218 * Try to get ifconfig parameters into the options string.
4219 * If tt is undefined, make a temporary instantiation.
4220 */
4221 if (!tt)
4222 {
4223 tt = init_tun(o->dev,
4224 o->dev_type,
4225 o->topology,
4226 o->ifconfig_local,
4227 o->ifconfig_remote_netmask,
4228 o->ifconfig_ipv6_local,
4229 o->ifconfig_ipv6_netbits,
4230 o->ifconfig_ipv6_remote,
4231 NULL,
4232 NULL,
4233 false,
4234 NULL,
4235 ctx,
4236 NULL);
4237 if (tt)
4238 {
4239 tt_local = true;
4240 }
4241 }
4242
4243 if (tt && p2p_nopull)
4244 {
4245 const char *ios = ifconfig_options_string(tt, remote, o->ifconfig_nowarn, gc);
4246 if (ios && strlen(ios))
4247 {
4248 buf_printf(&out, ",ifconfig %s", ios);
4249 }
4250 }
4251 if (tt_local)
4252 {
4253 free(tt);
4254 tt = NULL;
4255 }
4256
4257 #ifdef USE_COMP
4258 if (o->comp.alg != COMP_ALG_UNDEF)
4259 {
4260 buf_printf(&out, ",comp-lzo"); /* for compatibility, this simply indicates that compression context is active, not necessarily LZO per-se */
4261 }
4262 #endif
4263
4264 #ifdef ENABLE_FRAGMENT
4265 if (o->ce.fragment)
4266 {
4267 buf_printf(&out, ",mtu-dynamic");
4268 }
4269 #endif
4270
4271 #define TLS_CLIENT (o->tls_client)
4272 #define TLS_SERVER (o->tls_server)
4273
4274 /*
4275 * Key direction
4276 */
4277 {
4278 const char *kd = keydirection2ascii(o->key_direction, remote, false);
4279 if (kd)
4280 {
4281 buf_printf(&out, ",keydir %s", kd);
4282 }
4283 }
4284
4285 /*
4286 * Crypto Options
4287 */
4288 if (o->shared_secret_file || TLS_CLIENT || TLS_SERVER)
4289 {
4290 struct key_type kt;
4291
4292 ASSERT((o->shared_secret_file != NULL)
4293 + (TLS_CLIENT == true)
4294 + (TLS_SERVER == true)
4295 <= 1);
4296
4297 /* Skip resolving BF-CBC to allow SSL libraries without BF-CBC
4298 * to work here in the default configuration */
4299 const char *ciphername = o->ciphername;
4300 int keysize = 0;
4301
4302 if (strcmp(o->ciphername, "BF-CBC") == 0)
4303 {
4304 init_key_type(&kt, "none", o->authname, true, false);
4305 keysize = 128;
4306 }
4307 else
4308 {
4309 init_key_type(&kt, o->ciphername, o->authname, true, false);
4310 ciphername = cipher_kt_name(kt.cipher);
4311 if (cipher_defined(o->ciphername))
4312 {
4313 keysize = cipher_kt_key_size(kt.cipher) * 8;
4314 }
4315 }
4316 /* Only announce the cipher to our peer if we are willing to
4317 * support it */
4318 if (p2p_nopull || tls_item_in_cipher_list(ciphername, o->ncp_ciphers))
4319 {
4320 buf_printf(&out, ",cipher %s", ciphername);
4321 }
4322 buf_printf(&out, ",auth %s", md_kt_name(kt.digest));
4323 buf_printf(&out, ",keysize %d", keysize);
4324 if (o->shared_secret_file)
4325 {
4326 buf_printf(&out, ",secret");
4327 }
4328 if (!o->replay)
4329 {
4330 buf_printf(&out, ",no-replay");
4331 }
4332
4333 #ifdef ENABLE_PREDICTION_RESISTANCE
4334 if (o->use_prediction_resistance)
4335 {
4336 buf_printf(&out, ",use-prediction-resistance");
4337 }
4338 #endif
4339 }
4340
4341 /*
4342 * SSL Options
4343 */
4344 {
4345 if (TLS_CLIENT || TLS_SERVER)
4346 {
4347 if (o->ce.tls_auth_file)
4348 {
4349 buf_printf(&out, ",tls-auth");
4350 }
4351 /* Not adding tls-crypt here, because we won't reach this code if
4352 * tls-auth/tls-crypt does not match. Removing tls-auth here would
4353 * break stuff, so leaving that in place. */
4354
4355 buf_printf(&out, ",key-method %d", KEY_METHOD_2);
4356 }
4357
4358 if (remote)
4359 {
4360 if (TLS_CLIENT)
4361 {
4362 buf_printf(&out, ",tls-server");
4363 }
4364 else if (TLS_SERVER)
4365 {
4366 buf_printf(&out, ",tls-client");
4367 }
4368 }
4369 else
4370 {
4371 if (TLS_CLIENT)
4372 {
4373 buf_printf(&out, ",tls-client");
4374 }
4375 else if (TLS_SERVER)
4376 {
4377 buf_printf(&out, ",tls-server");
4378 }
4379 }
4380 }
4381
4382 #undef TLS_CLIENT
4383 #undef TLS_SERVER
4384
4385 return BSTR(&out);
4386 }
4387
4388 /*
4389 * Compare option strings for equality.
4390 * If the first two chars of the strings differ, it means that
4391 * we are looking at different versions of the options string,
4392 * therefore don't compare them and return true.
4393 */
4394
4395 bool
4396 options_cmp_equal(char *actual, const char *expected)
4397 {
4398 return options_cmp_equal_safe(actual, expected, strlen(actual) + 1);
4399 }
4400
4401 void
4402 options_warning(char *actual, const char *expected)
4403 {
4404 options_warning_safe(actual, expected, strlen(actual) + 1);
4405 }
4406
4407 static const char *
4408 options_warning_extract_parm1(const char *option_string,
4409 struct gc_arena *gc_ret)
4410 {
4411 struct gc_arena gc = gc_new();
4412 struct buffer b = string_alloc_buf(option_string, &gc);
4413 char *p = gc_malloc(OPTION_PARM_SIZE, false, &gc);
4414 const char *ret;
4415
4416 buf_parse(&b, ' ', p, OPTION_PARM_SIZE);
4417 ret = string_alloc(p, gc_ret);
4418 gc_free(&gc);
4419 return ret;
4420 }
4421
4422 static void
4423 options_warning_safe_scan2(const int msglevel,
4424 const int delim,
4425 const bool report_inconsistent,
4426 const char *p1,
4427 const struct buffer *b2_src,
4428 const char *b1_name,
4429 const char *b2_name)
4430 {
4431 /* We will stop sending 'key-method', 'keydir', 'proto' and 'tls-auth' in
4432 * OCC in a future version (because it's not useful). To reduce questions
4433 * when interoperating, we no longer printing a warning about it.
4434 */
4435 if (strprefix(p1, "key-method ")
4436 || strprefix(p1, "keydir ")
4437 || strprefix(p1, "proto ")
4438 || streq(p1, "tls-auth")
4439 || strprefix(p1, "tun-ipv6")
4440 || strprefix(p1, "cipher "))
4441 {
4442 return;
4443 }
4444
4445 if (strlen(p1) > 0)
4446 {
4447 struct gc_arena gc = gc_new();
4448 struct buffer b2 = *b2_src;
4449 const char *p1_prefix = options_warning_extract_parm1(p1, &gc);
4450 char *p2 = gc_malloc(OPTION_PARM_SIZE, false, &gc);
4451
4452 while (buf_parse(&b2, delim, p2, OPTION_PARM_SIZE))
4453 {
4454 if (strlen(p2))
4455 {
4456 const char *p2_prefix = options_warning_extract_parm1(p2, &gc);
4457
4458 if (!strcmp(p1, p2))
4459 {
4460 goto done;
4461 }
4462 if (!strcmp(p1_prefix, p2_prefix))
4463 {
4464 if (report_inconsistent)
4465 {
4466 msg(msglevel, "WARNING: '%s' is used inconsistently, %s='%s', %s='%s'",
4467 safe_print(p1_prefix, &gc),
4468 b1_name,
4469 safe_print(p1, &gc),
4470 b2_name,
4471 safe_print(p2, &gc));
4472 }
4473 goto done;
4474 }
4475 }
4476 }
4477
4478 msg(msglevel, "WARNING: '%s' is present in %s config but missing in %s config, %s='%s'",
4479 safe_print(p1_prefix, &gc),
4480 b1_name,
4481 b2_name,
4482 b1_name,
4483 safe_print(p1, &gc));
4484
4485 done:
4486 gc_free(&gc);
4487 }
4488 }
4489
4490 static void
4491 options_warning_safe_scan1(const int msglevel,
4492 const int delim,
4493 const bool report_inconsistent,
4494 const struct buffer *b1_src,
4495 const struct buffer *b2_src,
4496 const char *b1_name,
4497 const char *b2_name)
4498 {
4499 struct gc_arena gc = gc_new();
4500 struct buffer b = *b1_src;
4501 char *p = gc_malloc(OPTION_PARM_SIZE, true, &gc);
4502
4503 while (buf_parse(&b, delim, p, OPTION_PARM_SIZE))
4504 {
4505 options_warning_safe_scan2(msglevel, delim, report_inconsistent, p, b2_src, b1_name, b2_name);
4506 }
4507
4508 gc_free(&gc);
4509 }
4510
4511 static void
4512 options_warning_safe_ml(const int msglevel, char *actual, const char *expected, size_t actual_n)
4513 {
4514 struct gc_arena gc = gc_new();
4515
4516 if (actual_n > 0)
4517 {
4518 struct buffer local = alloc_buf_gc(OPTION_PARM_SIZE + 16, &gc);
4519 struct buffer remote = alloc_buf_gc(OPTION_PARM_SIZE + 16, &gc);
4520 actual[actual_n - 1] = 0;
4521
4522 buf_printf(&local, "version %s", expected);
4523 buf_printf(&remote, "version %s", actual);
4524
4525 options_warning_safe_scan1(msglevel, ',', true,
4526 &local, &remote,
4527 "local", "remote");
4528
4529 options_warning_safe_scan1(msglevel, ',', false,
4530 &remote, &local,
4531 "remote", "local");
4532 }
4533
4534 gc_free(&gc);
4535 }
4536
4537 bool
4538 options_cmp_equal_safe(char *actual, const char *expected, size_t actual_n)
4539 {
4540 struct gc_arena gc = gc_new();
4541 bool ret = true;
4542
4543 if (actual_n > 0)
4544 {
4545 actual[actual_n - 1] = 0;
4546 #ifndef ENABLE_STRICT_OPTIONS_CHECK
4547 if (strncmp(actual, expected, 2))
4548 {
4549 msg(D_SHOW_OCC, "NOTE: Options consistency check may be skewed by version differences");
4550 options_warning_safe_ml(D_SHOW_OCC, actual, expected, actual_n);
4551 }
4552 else
4553 #endif
4554 ret = !strcmp(actual, expected);
4555 }
4556 gc_free(&gc);
4557 return ret;
4558 }
4559
4560 void
4561 options_warning_safe(char *actual, const char *expected, size_t actual_n)
4562 {
4563 options_warning_safe_ml(M_WARN, actual, expected, actual_n);
4564 }
4565
4566 const char *
4567 options_string_version(const char *s, struct gc_arena *gc)
4568 {
4569 struct buffer out = alloc_buf_gc(4, gc);
4570 strncpynt((char *) BPTR(&out), s, 3);
4571 return BSTR(&out);
4572 }
4573
4574 char *
4575 options_string_extract_option(const char *options_string, const char *opt_name,
4576 struct gc_arena *gc)
4577 {
4578 char *ret = NULL;
4579 const size_t opt_name_len = strlen(opt_name);
4580
4581 const char *p = options_string;
4582 while (p)
4583 {
4584 if (0 == strncmp(p, opt_name, opt_name_len)
4585 && strlen(p) > (opt_name_len+1) && p[opt_name_len] == ' ')
4586 {
4587 /* option found, extract value */
4588 const char *start = &p[opt_name_len+1];
4589 const char *end = strchr(p, ',');
4590 size_t val_len = end ? end - start : strlen(start);
4591 ret = gc_malloc(val_len+1, true, gc);
4592 memcpy(ret, start, val_len);
4593 break;
4594 }
4595 p = strchr(p, ',');
4596 if (p)
4597 {
4598 p++; /* skip delimiter */
4599 }
4600 }
4601 return ret;
4602 }
4603
4604 #ifdef _WIN32
4605 /**
4606 * Parses --windows-driver config option
4607 *
4608 * @param str value of --windows-driver option
4609 * @param msglevel msglevel to report parsing error
4610 * @return enum windows_driver_type driver type, WINDOWS_DRIVER_UNSPECIFIED on unknown --windows-driver value
4611 */
4612 static enum windows_driver_type
4613 parse_windows_driver(const char *str, const int msglevel)
4614 {
4615 if (streq(str, "tap-windows6"))
4616 {
4617 return WINDOWS_DRIVER_TAP_WINDOWS6;
4618 }
4619 else if (streq(str, "wintun"))
4620 {
4621 return WINDOWS_DRIVER_WINTUN;
4622 }
4623
4624 else if (streq(str, "ovpn-dco"))
4625 {
4626 return WINDOWS_DRIVER_DCO;
4627 }
4628 else
4629 {
4630 msg(msglevel, "--windows-driver must be tap-windows6, wintun "
4631 "or ovpn-dco");
4632 return WINDOWS_DRIVER_UNSPECIFIED;
4633 }
4634 }
4635 #endif /* ifdef _WIN32 */
4636
4637 /*
4638 * parse/print topology coding
4639 */
4640
4641 int
4642 parse_topology(const char *str, const int msglevel)
4643 {
4644 if (streq(str, "net30"))
4645 {
4646 return TOP_NET30;
4647 }
4648 else if (streq(str, "p2p"))
4649 {
4650 return TOP_P2P;
4651 }
4652 else if (streq(str, "subnet"))
4653 {
4654 return TOP_SUBNET;
4655 }
4656 else
4657 {
4658 msg(msglevel, "--topology must be net30, p2p, or subnet");
4659 return TOP_UNDEF;
4660 }
4661 }
4662
4663 const char *
4664 print_topology(const int topology)
4665 {
4666 switch (topology)
4667 {
4668 case TOP_UNDEF:
4669 return "undef";
4670
4671 case TOP_NET30:
4672 return "net30";
4673
4674 case TOP_P2P:
4675 return "p2p";
4676
4677 case TOP_SUBNET:
4678 return "subnet";
4679
4680 default:
4681 return "unknown";
4682 }
4683 }
4684
4685 /*
4686 * Manage auth-retry variable
4687 */
4688
4689 static int global_auth_retry; /* GLOBAL */
4690
4691 int
4692 auth_retry_get(void)
4693 {
4694 return global_auth_retry;
4695 }
4696
4697 bool
4698 auth_retry_set(const int msglevel, const char *option)
4699 {
4700 if (streq(option, "interact"))
4701 {
4702 global_auth_retry = AR_INTERACT;
4703 }
4704 else if (streq(option, "nointeract"))
4705 {
4706 global_auth_retry = AR_NOINTERACT;
4707 }
4708 else if (streq(option, "none"))
4709 {
4710 global_auth_retry = AR_NONE;
4711 }
4712 else
4713 {
4714 msg(msglevel, "--auth-retry method must be 'interact', 'nointeract', or 'none'");
4715 return false;
4716 }
4717 return true;
4718 }
4719
4720 const char *
4721 auth_retry_print(void)
4722 {
4723 switch (global_auth_retry)
4724 {
4725 case AR_NONE:
4726 return "none";
4727
4728 case AR_NOINTERACT:
4729 return "nointeract";
4730
4731 case AR_INTERACT:
4732 return "interact";
4733
4734 default:
4735 return "???";
4736 }
4737 }
4738
4739 /*
4740 * Print the help message.
4741 */
4742 static void
4743 usage(void)
4744 {
4745 FILE *fp = msg_fp(0);
4746
4747 #ifdef ENABLE_SMALL
4748
4749 fprintf(fp, "Usage message not available\n");
4750
4751 #else
4752
4753 struct options o;
4754 init_options(&o, true);
4755
4756 fprintf(fp, usage_message,
4757 title_string,
4758 o.ce.connect_retry_seconds,
4759 o.ce.connect_retry_seconds_max,
4760 o.ce.local_port, o.ce.remote_port,
4761 TUN_MTU_DEFAULT, TAP_MTU_EXTRA_DEFAULT,
4762 o.verbosity,
4763 o.authname, o.ciphername,
4764 o.replay_window, o.replay_time,
4765 o.tls_timeout, o.renegotiate_seconds,
4766 o.handshake_window, o.transition_window);
4767 fflush(fp);
4768
4769 #endif /* ENABLE_SMALL */
4770
4771 openvpn_exit(OPENVPN_EXIT_STATUS_USAGE); /* exit point */
4772 }
4773
4774 void
4775 usage_small(void)
4776 {
4777 msg(M_WARN|M_NOPREFIX, "Use --help for more information.");
4778 openvpn_exit(OPENVPN_EXIT_STATUS_USAGE); /* exit point */
4779 }
4780
4781 #ifdef _WIN32
4782 void
4783 show_windows_version(const unsigned int flags)
4784 {
4785 struct gc_arena gc = gc_new();
4786 msg(flags, "Windows version %s", win32_version_string(&gc, true));
4787 gc_free(&gc);
4788 }
4789 #endif
4790
4791 void
4792 show_library_versions(const unsigned int flags)
4793 {
4794 #ifdef ENABLE_LZO
4795 #define LZO_LIB_VER_STR ", LZO ", lzo_version_string()
4796 #else
4797 #define LZO_LIB_VER_STR "", ""
4798 #endif
4799
4800 msg(flags, "library versions: %s%s%s", get_ssl_library_version(),
4801 LZO_LIB_VER_STR);
4802
4803 #undef LZO_LIB_VER_STR
4804 }
4805
4806 static void
4807 usage_version(void)
4808 {
4809 msg(M_INFO|M_NOPREFIX, "%s", title_string);
4810 show_library_versions( M_INFO|M_NOPREFIX );
4811 #ifdef _WIN32
4812 show_windows_version( M_INFO|M_NOPREFIX );
4813 #endif
4814 msg(M_INFO|M_NOPREFIX, "Originally developed by James Yonan");
4815 msg(M_INFO|M_NOPREFIX, "Copyright (C) 2002-2022 OpenVPN Inc <sales@openvpn.net>");
4816 #ifndef ENABLE_SMALL
4817 #ifdef CONFIGURE_DEFINES
4818 msg(M_INFO|M_NOPREFIX, "Compile time defines: %s", CONFIGURE_DEFINES);
4819 #endif
4820 #ifdef CONFIGURE_SPECIAL_BUILD
4821 msg(M_INFO|M_NOPREFIX, "special build: %s", CONFIGURE_SPECIAL_BUILD);
4822 #endif
4823 #endif
4824 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD);
4825 }
4826
4827 void
4828 notnull(const char *arg, const char *description)
4829 {
4830 if (!arg)
4831 {
4832 msg(M_USAGE, "You must define %s", description);
4833 }
4834 }
4835
4836 bool
4837 string_defined_equal(const char *s1, const char *s2)
4838 {
4839 if (s1 && s2)
4840 {
4841 return !strcmp(s1, s2);
4842 }
4843 else
4844 {
4845 return false;
4846 }
4847 }
4848
4849 #if 0
4850 static void
4851 ping_rec_err(int msglevel)
4852 {
4853 msg(msglevel, "only one of --ping-exit or --ping-restart options may be specified");
4854 }
4855 #endif
4856
4857 static int
4858 positive_atoi(const char *str)
4859 {
4860 const int i = atoi(str);
4861 return i < 0 ? 0 : i;
4862 }
4863
4864 #ifdef _WIN32 /* This function is only used when compiling on Windows */
4865 static unsigned int
4866 atou(const char *str)
4867 {
4868 unsigned int val = 0;
4869 sscanf(str, "%u", &val);
4870 return val;
4871 }
4872 #endif
4873
4874 static inline bool
4875 space(unsigned char c)
4876 {
4877 return c == '\0' || isspace(c);
4878 }
4879
4880 int
4881 parse_line(const char *line,
4882 char *p[],
4883 const int n,
4884 const char *file,
4885 const int line_num,
4886 int msglevel,
4887 struct gc_arena *gc)
4888 {
4889 const int STATE_INITIAL = 0;
4890 const int STATE_READING_QUOTED_PARM = 1;
4891 const int STATE_READING_UNQUOTED_PARM = 2;
4892 const int STATE_DONE = 3;
4893 const int STATE_READING_SQUOTED_PARM = 4;
4894
4895 const char *error_prefix = "";
4896
4897 int ret = 0;
4898 const char *c = line;
4899 int state = STATE_INITIAL;
4900 bool backslash = false;
4901 char in, out;
4902
4903 char parm[OPTION_PARM_SIZE];
4904 unsigned int parm_len = 0;
4905
4906 msglevel &= ~M_OPTERR;
4907
4908 if (msglevel & M_MSG_VIRT_OUT)
4909 {
4910 error_prefix = "ERROR: ";
4911 }
4912
4913 do
4914 {
4915 in = *c;
4916 out = 0;
4917
4918 if (!backslash && in == '\\' && state != STATE_READING_SQUOTED_PARM)
4919 {
4920 backslash = true;
4921 }
4922 else
4923 {
4924 if (state == STATE_INITIAL)
4925 {
4926 if (!space(in))
4927 {
4928 if (in == ';' || in == '#') /* comment */
4929 {
4930 break;
4931 }
4932 if (!backslash && in == '\"')
4933 {
4934 state = STATE_READING_QUOTED_PARM;
4935 }
4936 else if (!backslash && in == '\'')
4937 {
4938 state = STATE_READING_SQUOTED_PARM;
4939 }
4940 else
4941 {
4942 out = in;
4943 state = STATE_READING_UNQUOTED_PARM;
4944 }
4945 }
4946 }
4947 else if (state == STATE_READING_UNQUOTED_PARM)
4948 {
4949 if (!backslash && space(in))
4950 {
4951 state = STATE_DONE;
4952 }
4953 else
4954 {
4955 out = in;
4956 }
4957 }
4958 else if (state == STATE_READING_QUOTED_PARM)
4959 {
4960 if (!backslash && in == '\"')
4961 {
4962 state = STATE_DONE;
4963 }
4964 else
4965 {
4966 out = in;
4967 }
4968 }
4969 else if (state == STATE_READING_SQUOTED_PARM)
4970 {
4971 if (in == '\'')
4972 {
4973 state = STATE_DONE;
4974 }
4975 else
4976 {
4977 out = in;
4978 }
4979 }
4980 if (state == STATE_DONE)
4981 {
4982 /* ASSERT (parm_len > 0); */
4983 p[ret] = gc_malloc(parm_len + 1, true, gc);
4984 memcpy(p[ret], parm, parm_len);
4985 p[ret][parm_len] = '\0';
4986 state = STATE_INITIAL;
4987 parm_len = 0;
4988 ++ret;
4989 }
4990
4991 if (backslash && out)
4992 {
4993 if (!(out == '\\' || out == '\"' || space(out)))
4994 {
4995 #ifdef ENABLE_SMALL
4996 msg(msglevel, "%sOptions warning: Bad backslash ('\\') usage in %s:%d", error_prefix, file, line_num);
4997 #else
4998 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);
4999 #endif
5000 return 0;
5001 }
5002 }
5003 backslash = false;
5004 }
5005
5006 /* store parameter character */
5007 if (out)
5008 {
5009 if (parm_len >= SIZE(parm))
5010 {
5011 parm[SIZE(parm) - 1] = 0;
5012 msg(msglevel, "%sOptions error: Parameter at %s:%d is too long (%d chars max): %s",
5013 error_prefix, file, line_num, (int) SIZE(parm), parm);
5014 return 0;
5015 }
5016 parm[parm_len++] = out;
5017 }
5018
5019 /* avoid overflow if too many parms in one config file line */
5020 if (ret >= n)
5021 {
5022 break;
5023 }
5024
5025 } while (*c++ != '\0');
5026
5027 if (state == STATE_READING_QUOTED_PARM)
5028 {
5029 msg(msglevel, "%sOptions error: No closing quotation (\") in %s:%d", error_prefix, file, line_num);
5030 return 0;
5031 }
5032 if (state == STATE_READING_SQUOTED_PARM)
5033 {
5034 msg(msglevel, "%sOptions error: No closing single quotation (\') in %s:%d", error_prefix, file, line_num);
5035 return 0;
5036 }
5037 if (state != STATE_INITIAL)
5038 {
5039 msg(msglevel, "%sOptions error: Residual parse state (%d) in %s:%d", error_prefix, state, file, line_num);
5040 return 0;
5041 }
5042 #if 0
5043 {
5044 int i;
5045 for (i = 0; i < ret; ++i)
5046 {
5047 msg(M_INFO|M_NOPREFIX, "%s:%d ARG[%d] '%s'", file, line_num, i, p[i]);
5048 }
5049 }
5050 #endif
5051 return ret;
5052 }
5053
5054 static void
5055 bypass_doubledash(char **p)
5056 {
5057 if (strlen(*p) >= 3 && !strncmp(*p, "--", 2))
5058 {
5059 *p += 2;
5060 }
5061 }
5062
5063 struct in_src {
5064 #define IS_TYPE_FP 1
5065 #define IS_TYPE_BUF 2
5066 int type;
5067 union {
5068 FILE *fp;
5069 struct buffer *multiline;
5070 } u;
5071 };
5072
5073 static bool
5074 in_src_get(const struct in_src *is, char *line, const int size)
5075 {
5076 if (is->type == IS_TYPE_FP)
5077 {
5078 return BOOL_CAST(fgets(line, size, is->u.fp));
5079 }
5080 else if (is->type == IS_TYPE_BUF)
5081 {
5082 bool status = buf_parse(is->u.multiline, '\n', line, size);
5083 if ((int) strlen(line) + 1 < size)
5084 {
5085 strcat(line, "\n");
5086 }
5087 return status;
5088 }
5089 else
5090 {
5091 ASSERT(0);
5092 return false;
5093 }
5094 }
5095
5096 static char *
5097 read_inline_file(struct in_src *is, const char *close_tag,
5098 int *num_lines, struct gc_arena *gc)
5099 {
5100 char line[OPTION_LINE_SIZE];
5101 struct buffer buf = alloc_buf(8*OPTION_LINE_SIZE);
5102 char *ret;
5103 bool endtagfound = false;
5104
5105 while (in_src_get(is, line, sizeof(line)))
5106 {
5107 (*num_lines)++;
5108 char *line_ptr = line;
5109 /* Remove leading spaces */
5110 while (isspace(*line_ptr))
5111 {
5112 line_ptr++;
5113 }
5114 if (!strncmp(line_ptr, close_tag, strlen(close_tag)))
5115 {
5116 endtagfound = true;
5117 break;
5118 }
5119 if (!buf_safe(&buf, strlen(line)+1))
5120 {
5121 /* Increase buffer size */
5122 struct buffer buf2 = alloc_buf(buf.capacity * 2);
5123 ASSERT(buf_copy(&buf2, &buf));
5124 buf_clear(&buf);
5125 free_buf(&buf);
5126 buf = buf2;
5127 }
5128 buf_printf(&buf, "%s", line);
5129 }
5130 if (!endtagfound)
5131 {
5132 msg(M_FATAL, "ERROR: Endtag %s missing", close_tag);
5133 }
5134 ret = string_alloc(BSTR(&buf), gc);
5135 buf_clear(&buf);
5136 free_buf(&buf);
5137 secure_memzero(line, sizeof(line));
5138 return ret;
5139 }
5140
5141 static int
5142 check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc)
5143 {
5144 int num_inline_lines = 0;
5145
5146 if (p[0] && !p[1])
5147 {
5148 char *arg = p[0];
5149 if (arg[0] == '<' && arg[strlen(arg)-1] == '>')
5150 {
5151 struct buffer close_tag;
5152
5153 arg[strlen(arg) - 1] = '\0';
5154 p[0] = string_alloc(arg + 1, gc);
5155 close_tag = alloc_buf(strlen(p[0]) + 4);
5156 buf_printf(&close_tag, "</%s>", p[0]);
5157 p[1] = read_inline_file(is, BSTR(&close_tag), &num_inline_lines, gc);
5158 p[2] = NULL;
5159 free_buf(&close_tag);
5160 }
5161 }
5162 return num_inline_lines;
5163 }
5164
5165 static int
5166 check_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc)
5167 {
5168 struct in_src is;
5169 is.type = IS_TYPE_FP;
5170 is.u.fp = fp;
5171 return check_inline_file(&is, p, gc);
5172 }
5173
5174 static int
5175 check_inline_file_via_buf(struct buffer *multiline, char *p[],
5176 struct gc_arena *gc)
5177 {
5178 struct in_src is;
5179 is.type = IS_TYPE_BUF;
5180 is.u.multiline = multiline;
5181 return check_inline_file(&is, p, gc);
5182 }
5183
5184 static void
5185 add_option(struct options *options,
5186 char *p[],
5187 bool is_inline,
5188 const char *file,
5189 int line,
5190 const int level,
5191 const int msglevel,
5192 const unsigned int permission_mask,
5193 unsigned int *option_types_found,
5194 struct env_set *es);
5195
5196 static void
5197 read_config_file(struct options *options,
5198 const char *file,
5199 int level,
5200 const char *top_file,
5201 const int top_line,
5202 const int msglevel,
5203 const unsigned int permission_mask,
5204 unsigned int *option_types_found,
5205 struct env_set *es)
5206 {
5207 const int max_recursive_levels = 10;
5208 FILE *fp;
5209 int line_num;
5210 char line[OPTION_LINE_SIZE+1];
5211 char *p[MAX_PARMS+1];
5212
5213 ++level;
5214 if (level <= max_recursive_levels)
5215 {
5216 if (streq(file, "stdin"))
5217 {
5218 fp = stdin;
5219 }
5220 else
5221 {
5222 fp = platform_fopen(file, "r");
5223 }
5224 if (fp)
5225 {
5226 line_num = 0;
5227 while (fgets(line, sizeof(line), fp))
5228 {
5229 int offset = 0;
5230 CLEAR(p);
5231 ++line_num;
5232 if (strlen(line) == OPTION_LINE_SIZE)
5233 {
5234 msg(msglevel, "In %s:%d: Maximum option line length (%d) exceeded, line starts with %s",
5235 file, line_num, OPTION_LINE_SIZE, line);
5236 }
5237
5238 /* Ignore UTF-8 BOM at start of stream */
5239 if (line_num == 1 && strncmp(line, "\xEF\xBB\xBF", 3) == 0)
5240 {
5241 offset = 3;
5242 }
5243 if (parse_line(line + offset, p, SIZE(p)-1, file, line_num, msglevel, &options->gc))
5244 {
5245 bypass_doubledash(&p[0]);
5246 int lines_inline = check_inline_file_via_fp(fp, p, &options->gc);
5247 add_option(options, p, lines_inline, file, line_num, level,
5248 msglevel, permission_mask, option_types_found,
5249 es);
5250 line_num += lines_inline;
5251 }
5252 }
5253 if (fp != stdin)
5254 {
5255 fclose(fp);
5256 }
5257 }
5258 else
5259 {
5260 msg(msglevel, "In %s:%d: Error opening configuration file: %s", top_file, top_line, file);
5261 }
5262 }
5263 else
5264 {
5265 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);
5266 }
5267 secure_memzero(line, sizeof(line));
5268 CLEAR(p);
5269 }
5270
5271 static void
5272 read_config_string(const char *prefix,
5273 struct options *options,
5274 const char *config,
5275 const int msglevel,
5276 const unsigned int permission_mask,
5277 unsigned int *option_types_found,
5278 struct env_set *es)
5279 {
5280 char line[OPTION_LINE_SIZE];
5281 struct buffer multiline;
5282 int line_num = 0;
5283
5284 buf_set_read(&multiline, (uint8_t *)config, strlen(config));
5285
5286 while (buf_parse(&multiline, '\n', line, sizeof(line)))
5287 {
5288 char *p[MAX_PARMS+1];
5289 CLEAR(p);
5290 ++line_num;
5291 if (parse_line(line, p, SIZE(p)-1, prefix, line_num, msglevel, &options->gc))
5292 {
5293 bypass_doubledash(&p[0]);
5294 int lines_inline = check_inline_file_via_buf(&multiline, p, &options->gc);
5295 add_option(options, p, lines_inline, prefix, line_num, 0, msglevel,
5296 permission_mask, option_types_found, es);
5297 line_num += lines_inline;
5298 }
5299 CLEAR(p);
5300 }
5301 secure_memzero(line, sizeof(line));
5302 }
5303
5304 void
5305 parse_argv(struct options *options,
5306 const int argc,
5307 char *argv[],
5308 const int msglevel,
5309 const unsigned int permission_mask,
5310 unsigned int *option_types_found,
5311 struct env_set *es)
5312 {
5313 int i, j;
5314
5315 /* usage message */
5316 if (argc <= 1)
5317 {
5318 usage();
5319 }
5320
5321 /* config filename specified only? */
5322 if (argc == 2 && strncmp(argv[1], "--", 2))
5323 {
5324 char *p[MAX_PARMS];
5325 CLEAR(p);
5326 p[0] = "config";
5327 p[1] = argv[1];
5328 add_option(options, p, false, NULL, 0, 0, msglevel, permission_mask,
5329 option_types_found, es);
5330 }
5331 else
5332 {
5333 /* parse command line */
5334 for (i = 1; i < argc; ++i)
5335 {
5336 char *p[MAX_PARMS];
5337 CLEAR(p);
5338 p[0] = argv[i];
5339 if (strncmp(p[0], "--", 2))
5340 {
5341 msg(msglevel, "I'm trying to parse \"%s\" as an --option parameter but I don't see a leading '--'", p[0]);
5342 }
5343 else
5344 {
5345 p[0] += 2;
5346 }
5347
5348 for (j = 1; j < MAX_PARMS; ++j)
5349 {
5350 if (i + j < argc)
5351 {
5352 char *arg = argv[i + j];
5353 if (strncmp(arg, "--", 2))
5354 {
5355 p[j] = arg;
5356 }
5357 else
5358 {
5359 break;
5360 }
5361 }
5362 }
5363 add_option(options, p, false, NULL, 0, 0, msglevel, permission_mask,
5364 option_types_found, es);
5365 i += j - 1;
5366 }
5367 }
5368 }
5369
5370 /**
5371 * Filter an option line by all pull filters.
5372 *
5373 * If a match is found, the line is modified depending on
5374 * the filter type, and returns true. If the filter type is
5375 * reject, SIGUSR1 is triggered and the return value is false.
5376 * In that case the caller must end the push processing.
5377 */
5378 static bool
5379 apply_pull_filter(const struct options *o, char *line)
5380 {
5381 struct pull_filter *f;
5382
5383 if (!o->pull_filter_list)
5384 {
5385 return true;
5386 }
5387
5388 for (f = o->pull_filter_list->head; f; f = f->next)
5389 {
5390 if (f->type == PUF_TYPE_ACCEPT && strncmp(line, f->pattern, f->size) == 0)
5391 {
5392 msg(D_LOW, "Pushed option accepted by filter: '%s'", line);
5393 return true;
5394 }
5395 else if (f->type == PUF_TYPE_IGNORE && strncmp(line, f->pattern, f->size) == 0)
5396 {
5397 msg(D_PUSH, "Pushed option removed by filter: '%s'", line);
5398 *line = '\0';
5399 return true;
5400 }
5401 else if (f->type == PUF_TYPE_REJECT && strncmp(line, f->pattern, f->size) == 0)
5402 {
5403 msg(M_WARN, "Pushed option rejected by filter: '%s'. Restarting.", line);
5404 *line = '\0';
5405 throw_signal_soft(SIGUSR1, "Offending option received from server");
5406 return false;
5407 }
5408 }
5409 return true;
5410 }
5411
5412 bool
5413 apply_push_options(struct options *options,
5414 struct buffer *buf,
5415 unsigned int permission_mask,
5416 unsigned int *option_types_found,
5417 struct env_set *es)
5418 {
5419 char line[OPTION_PARM_SIZE];
5420 int line_num = 0;
5421 const char *file = "[PUSH-OPTIONS]";
5422 const int msglevel = D_PUSH_ERRORS|M_OPTERR;
5423
5424 while (buf_parse(buf, ',', line, sizeof(line)))
5425 {
5426 char *p[MAX_PARMS+1];
5427 CLEAR(p);
5428 ++line_num;
5429 if (!apply_pull_filter(options, line))
5430 {
5431 return false; /* Cause push/pull error and stop push processing */
5432 }
5433 if (parse_line(line, p, SIZE(p)-1, file, line_num, msglevel, &options->gc))
5434 {
5435 add_option(options, p, false, file, line_num, 0, msglevel,
5436 permission_mask, option_types_found, es);
5437 }
5438 }
5439 return true;
5440 }
5441
5442 void
5443 options_server_import(struct options *o,
5444 const char *filename,
5445 int msglevel,
5446 unsigned int permission_mask,
5447 unsigned int *option_types_found,
5448 struct env_set *es)
5449 {
5450 msg(D_PUSH, "OPTIONS IMPORT: reading client specific options from: %s", filename);
5451 read_config_file(o,
5452 filename,
5453 0,
5454 filename,
5455 0,
5456 msglevel,
5457 permission_mask,
5458 option_types_found,
5459 es);
5460 }
5461
5462 void
5463 options_string_import(struct options *options,
5464 const char *config,
5465 const int msglevel,
5466 const unsigned int permission_mask,
5467 unsigned int *option_types_found,
5468 struct env_set *es)
5469 {
5470 read_config_string("[CONFIG-STRING]", options, config, msglevel, permission_mask, option_types_found, es);
5471 }
5472
5473 #define VERIFY_PERMISSION(mask) { \
5474 if (!verify_permission(p[0], file, line, (mask), permission_mask, \
5475 option_types_found, msglevel, options, is_inline)) \
5476 { \
5477 goto err; \
5478 } \
5479 }
5480
5481 static bool
5482 verify_permission(const char *name,
5483 const char *file,
5484 int line,
5485 const unsigned int type,
5486 const unsigned int allowed,
5487 unsigned int *found,
5488 const int msglevel,
5489 struct options *options,
5490 bool is_inline)
5491 {
5492 if (!(type & allowed))
5493 {
5494 msg(msglevel, "option '%s' cannot be used in this context (%s)", name, file);
5495 return false;
5496 }
5497
5498 if (is_inline && !(type & OPT_P_INLINE))
5499 {
5500 msg(msglevel, "option '%s' is not expected to be inline (%s:%d)", name,
5501 file, line);
5502 return false;
5503 }
5504
5505 if (found)
5506 {
5507 *found |= type;
5508 }
5509
5510 #ifndef ENABLE_SMALL
5511 /* Check if this options is allowed in connection block,
5512 * but we are currently not in a connection block
5513 * unless this is a pushed option.
5514 * Parsing a connection block uses a temporary options struct without
5515 * connection_list
5516 */
5517
5518 if ((type & OPT_P_CONNECTION) && options->connection_list
5519 && !(allowed & OPT_P_PULL_MODE))
5520 {
5521 if (file)
5522 {
5523 msg(M_WARN, "Option '%s' in %s:%d is ignored by previous <connection> blocks ", name, file, line);
5524 }
5525 else
5526 {
5527 msg(M_WARN, "Option '%s' is ignored by previous <connection> blocks", name);
5528 }
5529 }
5530 #endif
5531 return true;
5532 }
5533
5534 /*
5535 * Check that an option doesn't have too
5536 * many parameters.
5537 */
5538
5539 #define NM_QUOTE_HINT (1<<0)
5540
5541 static bool
5542 no_more_than_n_args(const int msglevel,
5543 char *p[],
5544 const int max,
5545 const unsigned int flags)
5546 {
5547 const int len = string_array_len((const char **)p);
5548
5549 if (!len)
5550 {
5551 return false;
5552 }
5553
5554 if (len > max)
5555 {
5556 msg(msglevel, "the --%s directive should have at most %d parameter%s.%s",
5557 p[0],
5558 max - 1,
5559 max >= 3 ? "s" : "",
5560 (flags & NM_QUOTE_HINT) ? " To pass a list of arguments as one of the parameters, try enclosing them in double quotes (\"\")." : "");
5561 return false;
5562 }
5563 else
5564 {
5565 return true;
5566 }
5567 }
5568
5569 static inline int
5570 msglevel_forward_compatible(struct options *options, const int msglevel)
5571 {
5572 return options->forward_compatible ? M_WARN : msglevel;
5573 }
5574
5575 static void
5576 set_user_script(struct options *options,
5577 const char **script,
5578 const char *new_script,
5579 const char *type,
5580 bool in_chroot)
5581 {
5582 if (*script)
5583 {
5584 msg(M_WARN, "Multiple --%s scripts defined. "
5585 "The previously configured script is overridden.", type);
5586 }
5587 *script = new_script;
5588 options->user_script_used = true;
5589
5590 #ifndef ENABLE_SMALL
5591 {
5592 char script_name[100];
5593 openvpn_snprintf(script_name, sizeof(script_name),
5594 "--%s script", type);
5595
5596 if (check_cmd_access(*script, script_name, (in_chroot ? options->chroot_dir : NULL)))
5597 {
5598 msg(M_USAGE, "Please correct this error.");
5599 }
5600
5601 }
5602 #endif
5603 }
5604
5605 #ifdef USE_COMP
5606 static void
5607 show_compression_warning(struct compress_options *info)
5608 {
5609 if (comp_non_stub_enabled(info))
5610 {
5611 /*
5612 * Check if already displayed the strong warning and enabled full
5613 * compression
5614 */
5615 if (!(info->flags & COMP_F_ALLOW_COMPRESS))
5616 {
5617 msg(M_WARN, "WARNING: Compression for receiving enabled. "
5618 "Compression has been used in the past to break encryption. "
5619 "Sent packets are not compressed unless \"allow-compression yes\" "
5620 "is also set.");
5621 }
5622 }
5623 }
5624 #endif
5625
5626 bool
5627 key_is_external(const struct options *options)
5628 {
5629 bool ret = false;
5630 #ifdef ENABLE_MANAGEMENT
5631 ret = ret || (options->management_flags & MF_EXTERNAL_KEY);
5632 #endif
5633 #ifdef ENABLE_PKCS11
5634 ret = ret || (options->pkcs11_providers[0] != NULL);
5635 #endif
5636 #ifdef ENABLE_CRYPTOAPI
5637 ret = ret || options->cryptoapi_cert;
5638 #endif
5639
5640 return ret;
5641 }
5642
5643 static void
5644 add_option(struct options *options,
5645 char *p[],
5646 bool is_inline,
5647 const char *file,
5648 int line,
5649 const int level,
5650 const int msglevel,
5651 const unsigned int permission_mask,
5652 unsigned int *option_types_found,
5653 struct env_set *es)
5654 {
5655 struct gc_arena gc = gc_new();
5656 const bool pull_mode = BOOL_CAST(permission_mask & OPT_P_PULL_MODE);
5657 int msglevel_fc = msglevel_forward_compatible(options, msglevel);
5658
5659 ASSERT(MAX_PARMS >= 7);
5660
5661 /*
5662 * If directive begins with "setenv opt" prefix, don't raise an error if
5663 * directive is unrecognized.
5664 */
5665 if (streq(p[0], "setenv") && p[1] && streq(p[1], "opt") && !(permission_mask & OPT_P_PULL_MODE))
5666 {
5667 if (!p[2])
5668 {
5669 p[2] = "setenv opt"; /* will trigger an error that includes setenv opt */
5670 }
5671 p += 2;
5672 msglevel_fc = M_WARN;
5673 }
5674
5675 if (!file)
5676 {
5677 file = "[CMD-LINE]";
5678 line = 1;
5679 }
5680 if (streq(p[0], "help"))
5681 {
5682 VERIFY_PERMISSION(OPT_P_GENERAL);
5683 usage();
5684 if (p[1])
5685 {
5686 msg(msglevel, "--help does not accept any parameters");
5687 goto err;
5688 }
5689 }
5690 if (streq(p[0], "version") && !p[1])
5691 {
5692 VERIFY_PERMISSION(OPT_P_GENERAL);
5693 usage_version();
5694 }
5695 else if (streq(p[0], "config") && p[1] && !p[2])
5696 {
5697 VERIFY_PERMISSION(OPT_P_CONFIG);
5698
5699 /* save first config file only in options */
5700 if (!options->config)
5701 {
5702 options->config = p[1];
5703 }
5704
5705 read_config_file(options, p[1], level, file, line, msglevel, permission_mask, option_types_found, es);
5706 }
5707 #if defined(ENABLE_DEBUG) && !defined(ENABLE_SMALL)
5708 else if (streq(p[0], "show-gateway") && !p[2])
5709 {
5710 struct route_gateway_info rgi;
5711 struct route_ipv6_gateway_info rgi6;
5712 struct in6_addr remote = IN6ADDR_ANY_INIT;
5713 openvpn_net_ctx_t net_ctx;
5714 VERIFY_PERMISSION(OPT_P_GENERAL);
5715 if (p[1])
5716 {
5717 get_ipv6_addr(p[1], &remote, NULL, M_WARN);
5718 }
5719 net_ctx_init(NULL, &net_ctx);
5720 get_default_gateway(&rgi, &net_ctx);
5721 get_default_gateway_ipv6(&rgi6, &remote, &net_ctx);
5722 print_default_gateway(M_INFO, &rgi, &rgi6);
5723 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
5724 }
5725 #endif
5726 else if (streq(p[0], "echo") || streq(p[0], "parameter"))
5727 {
5728 struct buffer string = alloc_buf_gc(OPTION_PARM_SIZE, &gc);
5729 int j;
5730 bool good = true;
5731
5732 VERIFY_PERMISSION(OPT_P_ECHO);
5733
5734 for (j = 1; j < MAX_PARMS; ++j)
5735 {
5736 if (!p[j])
5737 {
5738 break;
5739 }
5740 if (j > 1)
5741 {
5742 good &= buf_printf(&string, " ");
5743 }
5744 good &= buf_printf(&string, "%s", p[j]);
5745 }
5746 if (good)
5747 {
5748 /* only message-related ECHO are logged, since other ECHOs
5749 * can potentially include security-sensitive strings */
5750 if (p[1] && strncmp(p[1], "msg", 3) == 0)
5751 {
5752 msg(M_INFO, "%s:%s",
5753 pull_mode ? "ECHO-PULL" : "ECHO",
5754 BSTR(&string));
5755 }
5756 #ifdef ENABLE_MANAGEMENT
5757 if (management)
5758 {
5759 management_echo(management, BSTR(&string), pull_mode);
5760 }
5761 #endif
5762 }
5763 else
5764 {
5765 msg(M_WARN, "echo/parameter option overflow");
5766 }
5767 }
5768 #ifdef ENABLE_MANAGEMENT
5769 else if (streq(p[0], "management") && p[1] && p[2] && !p[4])
5770 {
5771 VERIFY_PERMISSION(OPT_P_GENERAL);
5772 if (streq(p[2], "unix"))
5773 {
5774 #if UNIX_SOCK_SUPPORT
5775 options->management_flags |= MF_UNIX_SOCK;
5776 #else
5777 msg(msglevel, "MANAGEMENT: this platform does not support unix domain sockets");
5778 goto err;
5779 #endif
5780 }
5781
5782 options->management_addr = p[1];
5783 options->management_port = p[2];
5784 if (p[3])
5785 {
5786 options->management_user_pass = p[3];
5787 }
5788 }
5789 else if (streq(p[0], "management-client-user") && p[1] && !p[2])
5790 {
5791 VERIFY_PERMISSION(OPT_P_GENERAL);
5792 options->management_client_user = p[1];
5793 }
5794 else if (streq(p[0], "management-client-group") && p[1] && !p[2])
5795 {
5796 VERIFY_PERMISSION(OPT_P_GENERAL);
5797 options->management_client_group = p[1];
5798 }
5799 else if (streq(p[0], "management-query-passwords") && !p[1])
5800 {
5801 VERIFY_PERMISSION(OPT_P_GENERAL);
5802 options->management_flags |= MF_QUERY_PASSWORDS;
5803 }
5804 else if (streq(p[0], "management-query-remote") && !p[1])
5805 {
5806 VERIFY_PERMISSION(OPT_P_GENERAL);
5807 options->management_flags |= MF_QUERY_REMOTE;
5808 }
5809 else if (streq(p[0], "management-query-proxy") && !p[1])
5810 {
5811 VERIFY_PERMISSION(OPT_P_GENERAL);
5812 options->management_flags |= MF_QUERY_PROXY;
5813 }
5814 else if (streq(p[0], "management-hold") && !p[1])
5815 {
5816 VERIFY_PERMISSION(OPT_P_GENERAL);
5817 options->management_flags |= MF_HOLD;
5818 }
5819 else if (streq(p[0], "management-signal") && !p[1])
5820 {
5821 VERIFY_PERMISSION(OPT_P_GENERAL);
5822 options->management_flags |= MF_SIGNAL;
5823 }
5824 else if (streq(p[0], "management-forget-disconnect") && !p[1])
5825 {
5826 VERIFY_PERMISSION(OPT_P_GENERAL);
5827 options->management_flags |= MF_FORGET_DISCONNECT;
5828 }
5829 else if (streq(p[0], "management-up-down") && !p[1])
5830 {
5831 VERIFY_PERMISSION(OPT_P_GENERAL);
5832 options->management_flags |= MF_UP_DOWN;
5833 }
5834 else if (streq(p[0], "management-client") && !p[1])
5835 {
5836 VERIFY_PERMISSION(OPT_P_GENERAL);
5837 options->management_flags |= MF_CONNECT_AS_CLIENT;
5838 }
5839 #ifdef ENABLE_MANAGEMENT
5840 else if (streq(p[0], "management-external-key"))
5841 {
5842 VERIFY_PERMISSION(OPT_P_GENERAL);
5843 for (int j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
5844 {
5845 if (streq(p[j], "nopadding"))
5846 {
5847 options->management_flags |= MF_EXTERNAL_KEY_NOPADDING;
5848 }
5849 else if (streq(p[j], "pkcs1"))
5850 {
5851 options->management_flags |= MF_EXTERNAL_KEY_PKCS1PAD;
5852 }
5853 else if (streq(p[j], "pss"))
5854 {
5855 options->management_flags |= MF_EXTERNAL_KEY_PSSPAD;
5856 }
5857 else if (streq(p[j], "digest"))
5858 {
5859 options->management_flags |= MF_EXTERNAL_KEY_DIGEST;
5860 }
5861 else
5862 {
5863 msg(msglevel, "Unknown management-external-key flag: %s", p[j]);
5864 }
5865 }
5866 /*
5867 * When no option is present, assume that only PKCS1
5868 * padding is supported
5869 */
5870 if (!(options->management_flags
5871 &(MF_EXTERNAL_KEY_NOPADDING | MF_EXTERNAL_KEY_PKCS1PAD)))
5872 {
5873 options->management_flags |= MF_EXTERNAL_KEY_PKCS1PAD;
5874 }
5875 options->management_flags |= MF_EXTERNAL_KEY;
5876 }
5877 else if (streq(p[0], "management-external-cert") && p[1] && !p[2])
5878 {
5879 VERIFY_PERMISSION(OPT_P_GENERAL);
5880 options->management_flags |= MF_EXTERNAL_CERT;
5881 options->management_certificate = p[1];
5882 }
5883 else if (streq(p[0], "management-client-auth") && !p[1])
5884 {
5885 VERIFY_PERMISSION(OPT_P_GENERAL);
5886 options->management_flags |= MF_CLIENT_AUTH;
5887 }
5888 #endif /* ifdef ENABLE_MANAGEMENT */
5889 else if (streq(p[0], "management-log-cache") && p[1] && !p[2])
5890 {
5891 int cache;
5892
5893 VERIFY_PERMISSION(OPT_P_GENERAL);
5894 cache = atoi(p[1]);
5895 if (cache < 1)
5896 {
5897 msg(msglevel, "--management-log-cache parameter is out of range");
5898 goto err;
5899 }
5900 options->management_log_history_cache = cache;
5901 }
5902 #endif /* ifdef ENABLE_MANAGEMENT */
5903 #ifdef ENABLE_PLUGIN
5904 else if (streq(p[0], "plugin") && p[1])
5905 {
5906 VERIFY_PERMISSION(OPT_P_PLUGIN);
5907 if (!options->plugin_list)
5908 {
5909 options->plugin_list = plugin_option_list_new(&options->gc);
5910 }
5911 if (!plugin_option_list_add(options->plugin_list, &p[1], &options->gc))
5912 {
5913 msg(msglevel, "plugin add failed: %s", p[1]);
5914 goto err;
5915 }
5916 }
5917 #endif
5918 else if (streq(p[0], "mode") && p[1] && !p[2])
5919 {
5920 VERIFY_PERMISSION(OPT_P_GENERAL);
5921 if (streq(p[1], "p2p"))
5922 {
5923 options->mode = MODE_POINT_TO_POINT;
5924 }
5925 else if (streq(p[1], "server"))
5926 {
5927 options->mode = MODE_SERVER;
5928 }
5929 else
5930 {
5931 msg(msglevel, "Bad --mode parameter: %s", p[1]);
5932 goto err;
5933 }
5934 }
5935 else if (streq(p[0], "dev") && p[1] && !p[2])
5936 {
5937 VERIFY_PERMISSION(OPT_P_GENERAL);
5938 options->dev = p[1];
5939 }
5940 else if (streq(p[0], "dev-type") && p[1] && !p[2])
5941 {
5942 VERIFY_PERMISSION(OPT_P_GENERAL);
5943 options->dev_type = p[1];
5944 }
5945 #ifdef _WIN32
5946 else if (streq(p[0], "windows-driver") && p[1] && !p[2])
5947 {
5948 VERIFY_PERMISSION(OPT_P_GENERAL);
5949 options->windows_driver = parse_windows_driver(p[1], M_FATAL);
5950 }
5951 #endif
5952 else if (streq(p[0], "disable-dco"))
5953 {
5954 options->tuntap_options.disable_dco = true;
5955 }
5956 else if (streq(p[0], "dev-node") && p[1] && !p[2])
5957 {
5958 VERIFY_PERMISSION(OPT_P_GENERAL);
5959 options->dev_node = p[1];
5960 }
5961 else if (streq(p[0], "lladdr") && p[1] && !p[2])
5962 {
5963 VERIFY_PERMISSION(OPT_P_UP);
5964 if (mac_addr_safe(p[1])) /* MAC address only */
5965 {
5966 options->lladdr = p[1];
5967 }
5968 else
5969 {
5970 msg(msglevel, "lladdr parm '%s' must be a MAC address", p[1]);
5971 goto err;
5972 }
5973 }
5974 else if (streq(p[0], "topology") && p[1] && !p[2])
5975 {
5976 VERIFY_PERMISSION(OPT_P_UP);
5977 options->topology = parse_topology(p[1], msglevel);
5978 }
5979 else if (streq(p[0], "tun-ipv6") && !p[1])
5980 {
5981 if (!pull_mode)
5982 {
5983 msg(M_WARN, "Note: option tun-ipv6 is ignored because modern operating systems do not need special IPv6 tun handling anymore.");
5984 }
5985 }
5986 #ifdef ENABLE_IPROUTE
5987 else if (streq(p[0], "iproute") && p[1] && !p[2])
5988 {
5989 VERIFY_PERMISSION(OPT_P_GENERAL);
5990 iproute_path = p[1];
5991 }
5992 #endif
5993 else if (streq(p[0], "ifconfig") && p[1] && p[2] && !p[3])
5994 {
5995 VERIFY_PERMISSION(OPT_P_UP);
5996 if (ip_or_dns_addr_safe(p[1], options->allow_pull_fqdn) && ip_or_dns_addr_safe(p[2], options->allow_pull_fqdn)) /* FQDN -- may be DNS name */
5997 {
5998 options->ifconfig_local = p[1];
5999 options->ifconfig_remote_netmask = p[2];
6000 }
6001 else
6002 {
6003 msg(msglevel, "ifconfig parms '%s' and '%s' must be valid addresses", p[1], p[2]);
6004 goto err;
6005 }
6006 }
6007 else if (streq(p[0], "ifconfig-ipv6") && p[1] && p[2] && !p[3])
6008 {
6009 unsigned int netbits;
6010
6011 VERIFY_PERMISSION(OPT_P_UP);
6012 if (get_ipv6_addr( p[1], NULL, &netbits, msglevel )
6013 && ipv6_addr_safe( p[2] ) )
6014 {
6015 if (netbits < 64 || netbits > 124)
6016 {
6017 msg( msglevel, "ifconfig-ipv6: /netbits must be between 64 and 124, not '/%d'", netbits );
6018 goto err;
6019 }
6020
6021 options->ifconfig_ipv6_local = get_ipv6_addr_no_netbits(p[1], &options->gc);
6022 options->ifconfig_ipv6_netbits = netbits;
6023 options->ifconfig_ipv6_remote = p[2];
6024 }
6025 else
6026 {
6027 msg(msglevel, "ifconfig-ipv6 parms '%s' and '%s' must be valid addresses", p[1], p[2]);
6028 goto err;
6029 }
6030 }
6031 else if (streq(p[0], "ifconfig-noexec") && !p[1])
6032 {
6033 VERIFY_PERMISSION(OPT_P_UP);
6034 options->ifconfig_noexec = true;
6035 }
6036 else if (streq(p[0], "ifconfig-nowarn") && !p[1])
6037 {
6038 VERIFY_PERMISSION(OPT_P_UP);
6039 options->ifconfig_nowarn = true;
6040 }
6041 else if (streq(p[0], "local") && p[1] && !p[2])
6042 {
6043 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6044 options->ce.local = p[1];
6045 }
6046 else if (streq(p[0], "remote-random") && !p[1])
6047 {
6048 VERIFY_PERMISSION(OPT_P_GENERAL);
6049 options->remote_random = true;
6050 }
6051 else if (streq(p[0], "connection") && p[1] && !p[3])
6052 {
6053 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
6054 if (is_inline)
6055 {
6056 struct options sub;
6057 struct connection_entry *e;
6058
6059 init_options(&sub, true);
6060 sub.ce = options->ce;
6061 read_config_string("[CONNECTION-OPTIONS]", &sub, p[1], msglevel,
6062 OPT_P_CONNECTION, option_types_found, es);
6063 if (!sub.ce.remote)
6064 {
6065 msg(msglevel, "Each 'connection' block must contain exactly one 'remote' directive");
6066 uninit_options(&sub);
6067 goto err;
6068 }
6069
6070 e = alloc_connection_entry(options, msglevel);
6071 if (!e)
6072 {
6073 uninit_options(&sub);
6074 goto err;
6075 }
6076 *e = sub.ce;
6077 gc_transfer(&options->gc, &sub.gc);
6078 uninit_options(&sub);
6079 }
6080 }
6081 else if (streq(p[0], "ignore-unknown-option") && p[1])
6082 {
6083 int i;
6084 int j;
6085 int numignored = 0;
6086 const char **ignore;
6087
6088 VERIFY_PERMISSION(OPT_P_GENERAL);
6089 /* Find out how many options to be ignored */
6090 for (i = 1; p[i]; i++)
6091 {
6092 numignored++;
6093 }
6094
6095 /* add number of options already ignored */
6096 for (i = 0; options->ignore_unknown_option
6097 && options->ignore_unknown_option[i]; i++)
6098 {
6099 numignored++;
6100 }
6101
6102 /* Allocate array */
6103 ALLOC_ARRAY_GC(ignore, const char *, numignored+1, &options->gc);
6104 for (i = 0; options->ignore_unknown_option
6105 && options->ignore_unknown_option[i]; i++)
6106 {
6107 ignore[i] = options->ignore_unknown_option[i];
6108 }
6109
6110 options->ignore_unknown_option = ignore;
6111
6112 for (j = 1; p[j]; j++)
6113 {
6114 /* Allow the user to specify ignore-unknown-option --opt too */
6115 if (p[j][0]=='-' && p[j][1]=='-')
6116 {
6117 options->ignore_unknown_option[i] = (p[j]+2);
6118 }
6119 else
6120 {
6121 options->ignore_unknown_option[i] = p[j];
6122 }
6123 i++;
6124 }
6125
6126 options->ignore_unknown_option[i] = NULL;
6127 }
6128 #if ENABLE_MANAGEMENT
6129 else if (streq(p[0], "http-proxy-override") && p[1] && p[2] && !p[4])
6130 {
6131 VERIFY_PERMISSION(OPT_P_GENERAL);
6132 options->http_proxy_override = parse_http_proxy_override(p[1], p[2], p[3], msglevel, &options->gc);
6133 if (!options->http_proxy_override)
6134 {
6135 goto err;
6136 }
6137 }
6138 #endif
6139 else if (streq(p[0], "remote") && p[1] && !p[4])
6140 {
6141 struct remote_entry re;
6142 re.remote = re.remote_port = NULL;
6143 re.proto = -1;
6144 re.af = 0;
6145
6146 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6147 re.remote = p[1];
6148 if (p[2])
6149 {
6150 re.remote_port = p[2];
6151 if (p[3])
6152 {
6153 const int proto = ascii2proto(p[3]);
6154 const sa_family_t af = ascii2af(p[3]);
6155 if (proto < 0)
6156 {
6157 msg(msglevel,
6158 "remote: bad protocol associated with host %s: '%s'",
6159 p[1], p[3]);
6160 goto err;
6161 }
6162 re.proto = proto;
6163 re.af = af;
6164 }
6165 }
6166 if (permission_mask & OPT_P_GENERAL)
6167 {
6168 struct remote_entry *e = alloc_remote_entry(options, msglevel);
6169 if (!e)
6170 {
6171 goto err;
6172 }
6173 *e = re;
6174 }
6175 else if (permission_mask & OPT_P_CONNECTION)
6176 {
6177 connection_entry_load_re(&options->ce, &re);
6178 }
6179 }
6180 else if (streq(p[0], "resolv-retry") && p[1] && !p[2])
6181 {
6182 VERIFY_PERMISSION(OPT_P_GENERAL);
6183 if (streq(p[1], "infinite"))
6184 {
6185 options->resolve_retry_seconds = RESOLV_RETRY_INFINITE;
6186 }
6187 else
6188 {
6189 options->resolve_retry_seconds = positive_atoi(p[1]);
6190 }
6191 }
6192 else if ((streq(p[0], "preresolve") || streq(p[0], "ip-remote-hint")) && !p[2])
6193 {
6194 VERIFY_PERMISSION(OPT_P_GENERAL);
6195 options->resolve_in_advance = true;
6196 /* Note the ip-remote-hint and the argument p[1] are for
6197 * backward compatibility */
6198 if (p[1])
6199 {
6200 options->ip_remote_hint = p[1];
6201 }
6202 }
6203 else if (streq(p[0], "connect-retry") && p[1] && !p[3])
6204 {
6205 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6206 options->ce.connect_retry_seconds = positive_atoi(p[1]);
6207 /*
6208 * Limit the base value of retry wait interval to 16 bits to avoid
6209 * overflow when scaled up for exponential backoff
6210 */
6211 if (options->ce.connect_retry_seconds > 0xFFFF)
6212 {
6213 options->ce.connect_retry_seconds = 0xFFFF;
6214 msg(M_WARN, "connect retry wait interval truncated to %d",
6215 options->ce.connect_retry_seconds);
6216 }
6217
6218 if (p[2])
6219 {
6220 options->ce.connect_retry_seconds_max =
6221 max_int(positive_atoi(p[2]), options->ce.connect_retry_seconds);
6222 }
6223 }
6224 else if ((streq(p[0], "connect-timeout") || streq(p[0], "server-poll-timeout"))
6225 && p[1] && !p[2])
6226 {
6227 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6228 options->ce.connect_timeout = positive_atoi(p[1]);
6229 }
6230 else if (streq(p[0], "connect-retry-max") && p[1] && !p[2])
6231 {
6232 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6233 options->connect_retry_max = positive_atoi(p[1]);
6234 }
6235 else if (streq(p[0], "ipchange") && p[1])
6236 {
6237 VERIFY_PERMISSION(OPT_P_SCRIPT);
6238 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
6239 {
6240 goto err;
6241 }
6242 set_user_script(options,
6243 &options->ipchange,
6244 string_substitute(p[1], ',', ' ', &options->gc),
6245 "ipchange", true);
6246 }
6247 else if (streq(p[0], "float") && !p[1])
6248 {
6249 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6250 options->ce.remote_float = true;
6251 }
6252 #ifdef ENABLE_DEBUG
6253 else if (streq(p[0], "gremlin") && p[1] && !p[2])
6254 {
6255 VERIFY_PERMISSION(OPT_P_GENERAL);
6256 options->gremlin = positive_atoi(p[1]);
6257 }
6258 #endif
6259 else if (streq(p[0], "chroot") && p[1] && !p[2])
6260 {
6261 VERIFY_PERMISSION(OPT_P_GENERAL);
6262 options->chroot_dir = p[1];
6263 }
6264 else if (streq(p[0], "cd") && p[1] && !p[2])
6265 {
6266 VERIFY_PERMISSION(OPT_P_GENERAL);
6267 if (platform_chdir(p[1]))
6268 {
6269 msg(M_ERR, "cd to '%s' failed", p[1]);
6270 goto err;
6271 }
6272 options->cd_dir = p[1];
6273 }
6274 #ifdef ENABLE_SELINUX
6275 else if (streq(p[0], "setcon") && p[1] && !p[2])
6276 {
6277 VERIFY_PERMISSION(OPT_P_GENERAL);
6278 options->selinux_context = p[1];
6279 }
6280 #endif
6281 else if (streq(p[0], "writepid") && p[1] && !p[2])
6282 {
6283 VERIFY_PERMISSION(OPT_P_GENERAL);
6284 options->writepid = p[1];
6285 }
6286 else if (streq(p[0], "up") && p[1])
6287 {
6288 VERIFY_PERMISSION(OPT_P_SCRIPT);
6289 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
6290 {
6291 goto err;
6292 }
6293 set_user_script(options, &options->up_script, p[1], "up", false);
6294 }
6295 else if (streq(p[0], "down") && p[1])
6296 {
6297 VERIFY_PERMISSION(OPT_P_SCRIPT);
6298 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
6299 {
6300 goto err;
6301 }
6302 set_user_script(options, &options->down_script, p[1], "down", true);
6303 }
6304 else if (streq(p[0], "down-pre") && !p[1])
6305 {
6306 VERIFY_PERMISSION(OPT_P_GENERAL);
6307 options->down_pre = true;
6308 }
6309 else if (streq(p[0], "up-delay") && !p[1])
6310 {
6311 VERIFY_PERMISSION(OPT_P_GENERAL);
6312 options->up_delay = true;
6313 }
6314 else if (streq(p[0], "up-restart") && !p[1])
6315 {
6316 VERIFY_PERMISSION(OPT_P_GENERAL);
6317 options->up_restart = true;
6318 }
6319 else if (streq(p[0], "syslog") && !p[2])
6320 {
6321 VERIFY_PERMISSION(OPT_P_GENERAL);
6322 open_syslog(p[1], false);
6323 }
6324 else if (streq(p[0], "daemon") && !p[2])
6325 {
6326 bool didit = false;
6327 VERIFY_PERMISSION(OPT_P_GENERAL);
6328 if (!options->daemon)
6329 {
6330 options->daemon = didit = true;
6331 open_syslog(p[1], false);
6332 }
6333 if (p[1])
6334 {
6335 if (!didit)
6336 {
6337 msg(M_WARN, "WARNING: Multiple --daemon directives specified, ignoring --daemon %s. (Note that initscripts sometimes add their own --daemon directive.)", p[1]);
6338 goto err;
6339 }
6340 }
6341 }
6342 else if (streq(p[0], "log") && p[1] && !p[2])
6343 {
6344 VERIFY_PERMISSION(OPT_P_GENERAL);
6345 options->log = true;
6346 redirect_stdout_stderr(p[1], false);
6347 }
6348 else if (streq(p[0], "suppress-timestamps") && !p[1])
6349 {
6350 VERIFY_PERMISSION(OPT_P_GENERAL);
6351 options->suppress_timestamps = true;
6352 set_suppress_timestamps(true);
6353 }
6354 else if (streq(p[0], "machine-readable-output") && !p[1])
6355 {
6356 VERIFY_PERMISSION(OPT_P_GENERAL);
6357 options->machine_readable_output = true;
6358 set_machine_readable_output(true);
6359 }
6360 else if (streq(p[0], "log-append") && p[1] && !p[2])
6361 {
6362 VERIFY_PERMISSION(OPT_P_GENERAL);
6363 options->log = true;
6364 redirect_stdout_stderr(p[1], true);
6365 }
6366 #ifdef ENABLE_MEMSTATS
6367 else if (streq(p[0], "memstats") && p[1] && !p[2])
6368 {
6369 VERIFY_PERMISSION(OPT_P_GENERAL);
6370 options->memstats_fn = p[1];
6371 }
6372 #endif
6373 else if (streq(p[0], "mlock") && !p[1])
6374 {
6375 VERIFY_PERMISSION(OPT_P_GENERAL);
6376 options->mlock = true;
6377 }
6378 #if ENABLE_IP_PKTINFO
6379 else if (streq(p[0], "multihome") && !p[1])
6380 {
6381 VERIFY_PERMISSION(OPT_P_GENERAL);
6382 options->sockflags |= SF_USE_IP_PKTINFO;
6383 }
6384 #endif
6385 else if (streq(p[0], "verb") && p[1] && !p[2])
6386 {
6387 VERIFY_PERMISSION(OPT_P_MESSAGES);
6388 options->verbosity = positive_atoi(p[1]);
6389 if (options->verbosity >= (D_TLS_DEBUG_MED & M_DEBUG_LEVEL))
6390 {
6391 /* We pass this flag to the SSL library to avoid
6392 * mbed TLS always generating debug level logging */
6393 options->ssl_flags |= SSLF_TLS_DEBUG_ENABLED;
6394 }
6395 #if !defined(ENABLE_DEBUG) && !defined(ENABLE_SMALL)
6396 /* Warn when a debug verbosity is supplied when built without debug support */
6397 if (options->verbosity >= 7)
6398 {
6399 msg(M_WARN, "NOTE: debug verbosity (--verb %d) is enabled but this build lacks debug support.",
6400 options->verbosity);
6401 }
6402 #endif
6403 }
6404 else if (streq(p[0], "mute") && p[1] && !p[2])
6405 {
6406 VERIFY_PERMISSION(OPT_P_MESSAGES);
6407 options->mute = positive_atoi(p[1]);
6408 }
6409 else if (streq(p[0], "errors-to-stderr") && !p[1])
6410 {
6411 VERIFY_PERMISSION(OPT_P_MESSAGES);
6412 errors_to_stderr();
6413 }
6414 else if (streq(p[0], "status") && p[1] && !p[3])
6415 {
6416 VERIFY_PERMISSION(OPT_P_GENERAL);
6417 options->status_file = p[1];
6418 if (p[2])
6419 {
6420 options->status_file_update_freq = positive_atoi(p[2]);
6421 }
6422 }
6423 else if (streq(p[0], "status-version") && p[1] && !p[2])
6424 {
6425 int version;
6426
6427 VERIFY_PERMISSION(OPT_P_GENERAL);
6428 version = atoi(p[1]);
6429 if (version < 1 || version > 3)
6430 {
6431 msg(msglevel, "--status-version must be 1 to 3");
6432 goto err;
6433 }
6434 options->status_file_version = version;
6435 }
6436 else if (streq(p[0], "remap-usr1") && p[1] && !p[2])
6437 {
6438 VERIFY_PERMISSION(OPT_P_GENERAL);
6439 if (streq(p[1], "SIGHUP"))
6440 {
6441 options->remap_sigusr1 = SIGHUP;
6442 }
6443 else if (streq(p[1], "SIGTERM"))
6444 {
6445 options->remap_sigusr1 = SIGTERM;
6446 }
6447 else
6448 {
6449 msg(msglevel, "--remap-usr1 parm must be 'SIGHUP' or 'SIGTERM'");
6450 goto err;
6451 }
6452 }
6453 else if ((streq(p[0], "link-mtu") || streq(p[0], "udp-mtu")) && p[1] && !p[2])
6454 {
6455 VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
6456 options->ce.link_mtu = positive_atoi(p[1]);
6457 options->ce.link_mtu_defined = true;
6458 }
6459 else if (streq(p[0], "tun-mtu") && p[1] && !p[3])
6460 {
6461 VERIFY_PERMISSION(OPT_P_PUSH_MTU|OPT_P_CONNECTION);
6462 options->ce.tun_mtu = positive_atoi(p[1]);
6463 options->ce.tun_mtu_defined = true;
6464 if (p[2])
6465 {
6466 options->ce.occ_mtu = positive_atoi(p[2]);
6467 }
6468 else
6469 {
6470 options->ce.occ_mtu = 0;
6471 }
6472 }
6473 else if (streq(p[0], "tun-mtu-max") && p[1] && !p[3])
6474 {
6475 VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
6476 int max_mtu = positive_atoi(p[1]);
6477 if (max_mtu < 68 || max_mtu > 65536)
6478 {
6479 msg(msglevel, "--tun-mtu-max value '%s' is invalid", p[1]);
6480 }
6481 else
6482 {
6483 options->ce.tun_mtu_max = max_mtu;
6484 }
6485 }
6486 else if (streq(p[0], "tun-mtu-extra") && p[1] && !p[2])
6487 {
6488 VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
6489 options->ce.tun_mtu_extra = positive_atoi(p[1]);
6490 options->ce.tun_mtu_extra_defined = true;
6491 }
6492 else if (streq(p[0], "max-packet-size") && p[1] && !p[2])
6493 {
6494 VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
6495 int maxmtu = positive_atoi(p[1]);
6496 options->ce.tls_mtu = constrain_int(maxmtu, TLS_CHANNEL_MTU_MIN, TLS_CHANNEL_BUF_SIZE);
6497
6498 if (maxmtu < TLS_CHANNEL_MTU_MIN || maxmtu > TLS_CHANNEL_BUF_SIZE)
6499 {
6500 msg(M_WARN, "Note: max-packet-size value outside of allowed "
6501 "control channel packet size (%d to %d), will use %d "
6502 "instead.", TLS_CHANNEL_MTU_MIN, TLS_CHANNEL_BUF_SIZE,
6503 options->ce.tls_mtu);
6504 }
6505
6506 /* also set mssfix maxmtu mtu */
6507 options->ce.mssfix = maxmtu;
6508 options->ce.mssfix_default = false;
6509 options->ce.mssfix_encap = true;
6510 }
6511 #ifdef ENABLE_FRAGMENT
6512 else if (streq(p[0], "mtu-dynamic"))
6513 {
6514 VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
6515 msg(msglevel, "--mtu-dynamic has been replaced by --fragment");
6516 goto err;
6517 }
6518 else if (streq(p[0], "fragment") && p[1] && !p[3])
6519 {
6520 VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
6521 options->ce.fragment = positive_atoi(p[1]);
6522
6523 if (p[2] && streq(p[2], "mtu"))
6524 {
6525 options->ce.fragment_encap = true;
6526 }
6527 else if (p[2])
6528 {
6529 msg(msglevel, "Unknown parameter to --fragment: %s", p[2]);
6530 }
6531 }
6532 #endif /* ifdef ENABLE_FRAGMENT */
6533 else if (streq(p[0], "mtu-disc") && p[1] && !p[2])
6534 {
6535 VERIFY_PERMISSION(OPT_P_MTU|OPT_P_CONNECTION);
6536 options->ce.mtu_discover_type = translate_mtu_discover_type_name(p[1]);
6537 }
6538 else if (streq(p[0], "mtu-test") && !p[1])
6539 {
6540 VERIFY_PERMISSION(OPT_P_GENERAL);
6541 options->mtu_test = true;
6542 }
6543 else if (streq(p[0], "nice") && p[1] && !p[2])
6544 {
6545 VERIFY_PERMISSION(OPT_P_NICE);
6546 options->nice = atoi(p[1]);
6547 }
6548 else if (streq(p[0], "rcvbuf") && p[1] && !p[2])
6549 {
6550 VERIFY_PERMISSION(OPT_P_SOCKBUF);
6551 options->rcvbuf = positive_atoi(p[1]);
6552 }
6553 else if (streq(p[0], "sndbuf") && p[1] && !p[2])
6554 {
6555 VERIFY_PERMISSION(OPT_P_SOCKBUF);
6556 options->sndbuf = positive_atoi(p[1]);
6557 }
6558 else if (streq(p[0], "mark") && p[1] && !p[2])
6559 {
6560 #if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK
6561 VERIFY_PERMISSION(OPT_P_GENERAL);
6562 options->mark = atoi(p[1]);
6563 #endif
6564 }
6565 else if (streq(p[0], "socket-flags"))
6566 {
6567 int j;
6568 VERIFY_PERMISSION(OPT_P_SOCKFLAGS);
6569 for (j = 1; j < MAX_PARMS && p[j]; ++j)
6570 {
6571 if (streq(p[j], "TCP_NODELAY"))
6572 {
6573 options->sockflags |= SF_TCP_NODELAY;
6574 }
6575 else
6576 {
6577 msg(msglevel, "unknown socket flag: %s", p[j]);
6578 }
6579 }
6580 }
6581 #ifdef TARGET_LINUX
6582 else if (streq(p[0], "bind-dev") && p[1])
6583 {
6584 VERIFY_PERMISSION(OPT_P_SOCKFLAGS);
6585 options->bind_dev = p[1];
6586 }
6587 #endif
6588 else if (streq(p[0], "txqueuelen") && p[1] && !p[2])
6589 {
6590 VERIFY_PERMISSION(OPT_P_GENERAL);
6591 #ifdef TARGET_LINUX
6592 options->tuntap_options.txqueuelen = positive_atoi(p[1]);
6593 #else
6594 msg(msglevel, "--txqueuelen not supported on this OS");
6595 goto err;
6596 #endif
6597 }
6598 else if (streq(p[0], "shaper") && p[1] && !p[2])
6599 {
6600 int shaper;
6601
6602 VERIFY_PERMISSION(OPT_P_SHAPER);
6603 shaper = atoi(p[1]);
6604 if (shaper < SHAPER_MIN || shaper > SHAPER_MAX)
6605 {
6606 msg(msglevel, "Bad shaper value, must be between %d and %d",
6607 SHAPER_MIN, SHAPER_MAX);
6608 goto err;
6609 }
6610 options->shaper = shaper;
6611 }
6612 else if (streq(p[0], "port") && p[1] && !p[2])
6613 {
6614 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6615 options->ce.local_port = options->ce.remote_port = p[1];
6616 }
6617 else if (streq(p[0], "lport") && p[1] && !p[2])
6618 {
6619 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6620 options->ce.local_port_defined = true;
6621 options->ce.local_port = p[1];
6622 }
6623 else if (streq(p[0], "rport") && p[1] && !p[2])
6624 {
6625 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6626 options->ce.remote_port = p[1];
6627 }
6628 else if (streq(p[0], "bind") && !p[2])
6629 {
6630 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6631 options->ce.bind_defined = true;
6632 if (p[1] && streq(p[1], "ipv6only"))
6633 {
6634 options->ce.bind_ipv6_only = true;
6635 }
6636
6637 }
6638 else if (streq(p[0], "nobind") && !p[1])
6639 {
6640 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6641 options->ce.bind_local = false;
6642 }
6643 else if (streq(p[0], "fast-io") && !p[1])
6644 {
6645 VERIFY_PERMISSION(OPT_P_GENERAL);
6646 options->fast_io = true;
6647 }
6648 else if (streq(p[0], "inactive") && p[1] && !p[3])
6649 {
6650 VERIFY_PERMISSION(OPT_P_TIMER);
6651 options->inactivity_timeout = positive_atoi(p[1]);
6652 if (p[2])
6653 {
6654 int64_t val = atoll(p[2]);
6655 options->inactivity_minimum_bytes = (val < 0) ? 0 : val;
6656 if (options->inactivity_minimum_bytes > INT_MAX)
6657 {
6658 msg(M_WARN, "WARNING: '--inactive' with a 'bytes' value"
6659 " >2 Gbyte was silently ignored in older versions. If "
6660 " your VPN exits unexpectedly with 'Inactivity timeout'"
6661 " in %d seconds, revisit this value.",
6662 options->inactivity_timeout );
6663 }
6664 }
6665 }
6666 else if (streq(p[0], "session-timeout") && p[1] && !p[2])
6667 {
6668 VERIFY_PERMISSION(OPT_P_TIMER);
6669 options->session_timeout = positive_atoi(p[1]);
6670 }
6671 else if (streq(p[0], "proto") && p[1] && !p[2])
6672 {
6673 int proto;
6674 sa_family_t af;
6675 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6676 proto = ascii2proto(p[1]);
6677 af = ascii2af(p[1]);
6678 if (proto < 0)
6679 {
6680 msg(msglevel,
6681 "Bad protocol: '%s'. Allowed protocols with --proto option: %s",
6682 p[1],
6683 proto2ascii_all(&gc));
6684 goto err;
6685 }
6686 options->ce.proto = proto;
6687 options->ce.af = af;
6688 }
6689 else if (streq(p[0], "proto-force") && p[1] && !p[2])
6690 {
6691 int proto_force;
6692 VERIFY_PERMISSION(OPT_P_GENERAL);
6693 proto_force = ascii2proto(p[1]);
6694 if (proto_force < 0)
6695 {
6696 msg(msglevel, "Bad --proto-force protocol: '%s'", p[1]);
6697 goto err;
6698 }
6699 options->proto_force = proto_force;
6700 }
6701 else if (streq(p[0], "http-proxy") && p[1] && !p[5])
6702 {
6703 struct http_proxy_options *ho;
6704
6705 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6706
6707 {
6708 if (!p[2])
6709 {
6710 msg(msglevel, "http-proxy port number not defined");
6711 goto err;
6712 }
6713
6714 ho = init_http_proxy_options_once(&options->ce.http_proxy_options, &options->gc);
6715
6716 ho->server = p[1];
6717 ho->port = p[2];
6718 }
6719
6720 if (p[3])
6721 {
6722 /* auto -- try to figure out proxy addr, port, and type automatically */
6723 /* semiauto -- given proxy addr:port, try to figure out type automatically */
6724 /* (auto|semiauto)-nct -- disable proxy auth cleartext protocols (i.e. basic auth) */
6725 if (streq(p[3], "auto"))
6726 {
6727 ho->auth_retry = PAR_ALL;
6728 }
6729 else if (streq(p[3], "auto-nct"))
6730 {
6731 ho->auth_retry = PAR_NCT;
6732 }
6733 else
6734 {
6735 ho->auth_method_string = "basic";
6736 ho->auth_file = p[3];
6737
6738 if (p[4])
6739 {
6740 ho->auth_method_string = p[4];
6741 }
6742 }
6743 }
6744 else
6745 {
6746 ho->auth_method_string = "none";
6747 }
6748 }
6749 else if (streq(p[0], "http-proxy-user-pass") && p[1])
6750 {
6751 struct http_proxy_options *ho;
6752 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
6753 ho = init_http_proxy_options_once(&options->ce.http_proxy_options, &options->gc);
6754 ho->auth_file = p[1];
6755 ho->inline_creds = is_inline;
6756 }
6757 else if (streq(p[0], "http-proxy-retry") || streq(p[0], "socks-proxy-retry"))
6758 {
6759 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6760 msg(M_WARN, "DEPRECATED OPTION: http-proxy-retry and socks-proxy-retry: "
6761 "In OpenVPN 2.4 proxy connection retries are handled like regular connections. "
6762 "Use connect-retry-max 1 to get a similar behavior as before.");
6763 }
6764 else if (streq(p[0], "http-proxy-timeout") && p[1] && !p[2])
6765 {
6766 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6767 msg(M_WARN, "DEPRECATED OPTION: http-proxy-timeout: In OpenVPN 2.4 the timeout until a connection to a "
6768 "server is established is managed with a single timeout set by connect-timeout");
6769 }
6770 else if (streq(p[0], "http-proxy-option") && p[1] && !p[4])
6771 {
6772 struct http_proxy_options *ho;
6773
6774 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6775 ho = init_http_proxy_options_once(&options->ce.http_proxy_options, &options->gc);
6776
6777 if (streq(p[1], "VERSION") && p[2] && !p[3])
6778 {
6779 ho->http_version = p[2];
6780 }
6781 else if (streq(p[1], "AGENT") && p[2] && !p[3])
6782 {
6783 ho->user_agent = p[2];
6784 }
6785 else if ((streq(p[1], "EXT1") || streq(p[1], "EXT2") || streq(p[1], "CUSTOM-HEADER"))
6786 && p[2])
6787 {
6788 /* In the wild patched versions use both EXT1/2 and CUSTOM-HEADER
6789 * with either two argument or one */
6790
6791 struct http_custom_header *custom_header = NULL;
6792 int i;
6793 /* Find the first free header */
6794 for (i = 0; i < MAX_CUSTOM_HTTP_HEADER; i++)
6795 {
6796 if (!ho->custom_headers[i].name)
6797 {
6798 custom_header = &ho->custom_headers[i];
6799 break;
6800 }
6801 }
6802 if (!custom_header)
6803 {
6804 msg(msglevel, "Cannot use more than %d http-proxy-option CUSTOM-HEADER : '%s'", MAX_CUSTOM_HTTP_HEADER, p[1]);
6805 }
6806 else
6807 {
6808 /* We will save p[2] and p[3], the proxy code will detect if
6809 * p[3] is NULL */
6810 custom_header->name = p[2];
6811 custom_header->content = p[3];
6812 }
6813 }
6814 else
6815 {
6816 msg(msglevel, "Bad http-proxy-option or missing or extra parameter: '%s'", p[1]);
6817 }
6818 }
6819 else if (streq(p[0], "socks-proxy") && p[1] && !p[4])
6820 {
6821 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
6822
6823 if (p[2])
6824 {
6825 options->ce.socks_proxy_port = p[2];
6826 }
6827 else
6828 {
6829 options->ce.socks_proxy_port = "1080";
6830 }
6831 options->ce.socks_proxy_server = p[1];
6832 options->ce.socks_proxy_authfile = p[3]; /* might be NULL */
6833 }
6834 else if (streq(p[0], "keepalive") && p[1] && p[2] && !p[3])
6835 {
6836 VERIFY_PERMISSION(OPT_P_GENERAL);
6837 options->keepalive_ping = atoi(p[1]);
6838 options->keepalive_timeout = atoi(p[2]);
6839 }
6840 else if (streq(p[0], "ping") && p[1] && !p[2])
6841 {
6842 VERIFY_PERMISSION(OPT_P_TIMER);
6843 options->ping_send_timeout = positive_atoi(p[1]);
6844 }
6845 else if (streq(p[0], "ping-exit") && p[1] && !p[2])
6846 {
6847 VERIFY_PERMISSION(OPT_P_TIMER);
6848 options->ping_rec_timeout = positive_atoi(p[1]);
6849 options->ping_rec_timeout_action = PING_EXIT;
6850 }
6851 else if (streq(p[0], "ping-restart") && p[1] && !p[2])
6852 {
6853 VERIFY_PERMISSION(OPT_P_TIMER);
6854 options->ping_rec_timeout = positive_atoi(p[1]);
6855 options->ping_rec_timeout_action = PING_RESTART;
6856 }
6857 else if (streq(p[0], "ping-timer-rem") && !p[1])
6858 {
6859 VERIFY_PERMISSION(OPT_P_TIMER);
6860 options->ping_timer_remote = true;
6861 }
6862 else if (streq(p[0], "explicit-exit-notify") && !p[2])
6863 {
6864 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION|OPT_P_EXPLICIT_NOTIFY);
6865 if (p[1])
6866 {
6867 options->ce.explicit_exit_notification = positive_atoi(p[1]);
6868 }
6869 else
6870 {
6871 options->ce.explicit_exit_notification = 1;
6872 }
6873 }
6874 else if (streq(p[0], "persist-tun") && !p[1])
6875 {
6876 VERIFY_PERMISSION(OPT_P_PERSIST);
6877 options->persist_tun = true;
6878 }
6879 else if (streq(p[0], "persist-key") && !p[1])
6880 {
6881 VERIFY_PERMISSION(OPT_P_PERSIST);
6882 options->persist_key = true;
6883 }
6884 else if (streq(p[0], "persist-local-ip") && !p[1])
6885 {
6886 VERIFY_PERMISSION(OPT_P_PERSIST_IP);
6887 options->persist_local_ip = true;
6888 }
6889 else if (streq(p[0], "persist-remote-ip") && !p[1])
6890 {
6891 VERIFY_PERMISSION(OPT_P_PERSIST_IP);
6892 options->persist_remote_ip = true;
6893 }
6894 else if (streq(p[0], "client-nat") && p[1] && p[2] && p[3] && p[4] && !p[5])
6895 {
6896 VERIFY_PERMISSION(OPT_P_ROUTE);
6897 cnol_check_alloc(options);
6898 add_client_nat_to_option_list(options->client_nat, p[1], p[2], p[3], p[4], msglevel);
6899 }
6900 else if (streq(p[0], "route") && p[1] && !p[5])
6901 {
6902 VERIFY_PERMISSION(OPT_P_ROUTE);
6903 rol_check_alloc(options);
6904 if (pull_mode)
6905 {
6906 if (!ip_or_dns_addr_safe(p[1], options->allow_pull_fqdn) && !is_special_addr(p[1])) /* FQDN -- may be DNS name */
6907 {
6908 msg(msglevel, "route parameter network/IP '%s' must be a valid address", p[1]);
6909 goto err;
6910 }
6911 if (p[2] && !ip_addr_dotted_quad_safe(p[2])) /* FQDN -- must be IP address */
6912 {
6913 msg(msglevel, "route parameter netmask '%s' must be an IP address", p[2]);
6914 goto err;
6915 }
6916 if (p[3] && !ip_or_dns_addr_safe(p[3], options->allow_pull_fqdn) && !is_special_addr(p[3])) /* FQDN -- may be DNS name */
6917 {
6918 msg(msglevel, "route parameter gateway '%s' must be a valid address", p[3]);
6919 goto err;
6920 }
6921 }
6922 add_route_to_option_list(options->routes, p[1], p[2], p[3], p[4]);
6923 }
6924 else if (streq(p[0], "route-ipv6") && p[1] && !p[4])
6925 {
6926 VERIFY_PERMISSION(OPT_P_ROUTE);
6927 rol6_check_alloc(options);
6928 if (pull_mode)
6929 {
6930 if (!ipv6_addr_safe_hexplusbits(p[1]))
6931 {
6932 msg(msglevel, "route-ipv6 parameter network/IP '%s' must be a valid address", p[1]);
6933 goto err;
6934 }
6935 if (p[2] && !ipv6_addr_safe(p[2]))
6936 {
6937 msg(msglevel, "route-ipv6 parameter gateway '%s' must be a valid address", p[2]);
6938 goto err;
6939 }
6940 /* p[3] is metric, if present */
6941 }
6942 add_route_ipv6_to_option_list(options->routes_ipv6, p[1], p[2], p[3]);
6943 }
6944 else if (streq(p[0], "max-routes") && !p[2])
6945 {
6946 msg(M_WARN, "DEPRECATED OPTION: --max-routes option ignored."
6947 "The number of routes is unlimited as of OpenVPN 2.4. "
6948 "This option will be removed in a future version, "
6949 "please remove it from your configuration.");
6950 }
6951 else if (streq(p[0], "route-gateway") && p[1] && !p[2])
6952 {
6953 VERIFY_PERMISSION(OPT_P_ROUTE_EXTRAS);
6954 if (streq(p[1], "dhcp"))
6955 {
6956 options->route_gateway_via_dhcp = true;
6957 }
6958 else
6959 {
6960 if (ip_or_dns_addr_safe(p[1], options->allow_pull_fqdn) || is_special_addr(p[1])) /* FQDN -- may be DNS name */
6961 {
6962 options->route_default_gateway = p[1];
6963 }
6964 else
6965 {
6966 msg(msglevel, "route-gateway parm '%s' must be a valid address", p[1]);
6967 goto err;
6968 }
6969 }
6970 }
6971 else if (streq(p[0], "route-ipv6-gateway") && p[1] && !p[2])
6972 {
6973 if (ipv6_addr_safe(p[1]))
6974 {
6975 options->route_ipv6_default_gateway = p[1];
6976 }
6977 else
6978 {
6979 msg(msglevel, "route-ipv6-gateway parm '%s' must be a valid address", p[1]);
6980 goto err;
6981 }
6982 }
6983 else if (streq(p[0], "route-metric") && p[1] && !p[2])
6984 {
6985 VERIFY_PERMISSION(OPT_P_ROUTE);
6986 options->route_default_metric = positive_atoi(p[1]);
6987 }
6988 else if (streq(p[0], "route-delay") && !p[3])
6989 {
6990 VERIFY_PERMISSION(OPT_P_ROUTE_EXTRAS);
6991 options->route_delay_defined = true;
6992 if (p[1])
6993 {
6994 options->route_delay = positive_atoi(p[1]);
6995 if (p[2])
6996 {
6997 options->route_delay_window = positive_atoi(p[2]);
6998 }
6999 }
7000 else
7001 {
7002 options->route_delay = 0;
7003 }
7004 }
7005 else if (streq(p[0], "route-up") && p[1])
7006 {
7007 VERIFY_PERMISSION(OPT_P_SCRIPT);
7008 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
7009 {
7010 goto err;
7011 }
7012 set_user_script(options, &options->route_script, p[1], "route-up", false);
7013 }
7014 else if (streq(p[0], "route-pre-down") && p[1])
7015 {
7016 VERIFY_PERMISSION(OPT_P_SCRIPT);
7017 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
7018 {
7019 goto err;
7020 }
7021 set_user_script(options,
7022 &options->route_predown_script,
7023 p[1],
7024 "route-pre-down", true);
7025 }
7026 else if (streq(p[0], "route-noexec") && !p[1])
7027 {
7028 VERIFY_PERMISSION(OPT_P_SCRIPT);
7029 options->route_noexec = true;
7030 }
7031 else if (streq(p[0], "route-nopull") && !p[1])
7032 {
7033 VERIFY_PERMISSION(OPT_P_GENERAL);
7034 options->route_nopull = true;
7035 }
7036 else if (streq(p[0], "pull-filter") && p[1] && p[2] && !p[3])
7037 {
7038 struct pull_filter *f;
7039 VERIFY_PERMISSION(OPT_P_GENERAL)
7040 f = alloc_pull_filter(options, msglevel);
7041
7042 if (strcmp("accept", p[1]) == 0)
7043 {
7044 f->type = PUF_TYPE_ACCEPT;
7045 }
7046 else if (strcmp("ignore", p[1]) == 0)
7047 {
7048 f->type = PUF_TYPE_IGNORE;
7049 }
7050 else if (strcmp("reject", p[1]) == 0)
7051 {
7052 f->type = PUF_TYPE_REJECT;
7053 }
7054 else
7055 {
7056 msg(msglevel, "Unknown --pull-filter type: %s", p[1]);
7057 goto err;
7058 }
7059 f->pattern = p[2];
7060 f->size = strlen(p[2]);
7061 }
7062 else if (streq(p[0], "allow-pull-fqdn") && !p[1])
7063 {
7064 VERIFY_PERMISSION(OPT_P_GENERAL);
7065 options->allow_pull_fqdn = true;
7066 }
7067 else if (streq(p[0], "redirect-gateway") || streq(p[0], "redirect-private"))
7068 {
7069 int j;
7070 VERIFY_PERMISSION(OPT_P_ROUTE);
7071 rol_check_alloc(options);
7072
7073 if (options->routes->flags & RG_ENABLE)
7074 {
7075 msg(M_WARN,
7076 "WARNING: You have specified redirect-gateway and "
7077 "redirect-private at the same time (or the same option "
7078 "multiple times). This is not well supported and may lead to "
7079 "unexpected results");
7080 }
7081
7082 options->routes->flags |= RG_ENABLE;
7083
7084 if (streq(p[0], "redirect-gateway"))
7085 {
7086 options->routes->flags |= RG_REROUTE_GW;
7087 }
7088 for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
7089 {
7090 if (streq(p[j], "local"))
7091 {
7092 options->routes->flags |= RG_LOCAL;
7093 }
7094 else if (streq(p[j], "autolocal"))
7095 {
7096 options->routes->flags |= RG_AUTO_LOCAL;
7097 }
7098 else if (streq(p[j], "def1"))
7099 {
7100 options->routes->flags |= RG_DEF1;
7101 }
7102 else if (streq(p[j], "bypass-dhcp"))
7103 {
7104 options->routes->flags |= RG_BYPASS_DHCP;
7105 }
7106 else if (streq(p[j], "bypass-dns"))
7107 {
7108 options->routes->flags |= RG_BYPASS_DNS;
7109 }
7110 else if (streq(p[j], "block-local"))
7111 {
7112 options->routes->flags |= RG_BLOCK_LOCAL;
7113 }
7114 else if (streq(p[j], "ipv6"))
7115 {
7116 rol6_check_alloc(options);
7117 options->routes_ipv6->flags |= RG_REROUTE_GW;
7118 }
7119 else if (streq(p[j], "!ipv4"))
7120 {
7121 options->routes->flags &= ~(RG_REROUTE_GW | RG_ENABLE);
7122 }
7123 else
7124 {
7125 msg(msglevel, "unknown --%s flag: %s", p[0], p[j]);
7126 goto err;
7127 }
7128 }
7129 #ifdef _WIN32
7130 /* we need this here to handle pushed --redirect-gateway */
7131 remap_redirect_gateway_flags(options);
7132 #endif
7133 }
7134 else if (streq(p[0], "block-ipv6") && !p[1])
7135 {
7136 VERIFY_PERMISSION(OPT_P_ROUTE);
7137 options->block_ipv6 = true;
7138 }
7139 else if (streq(p[0], "remote-random-hostname") && !p[1])
7140 {
7141 VERIFY_PERMISSION(OPT_P_GENERAL);
7142 options->sockflags |= SF_HOST_RANDOMIZE;
7143 }
7144 else if (streq(p[0], "setenv") && p[1] && !p[3])
7145 {
7146 VERIFY_PERMISSION(OPT_P_GENERAL);
7147 if (streq(p[1], "REMOTE_RANDOM_HOSTNAME") && !p[2])
7148 {
7149 options->sockflags |= SF_HOST_RANDOMIZE;
7150 }
7151 else if (streq(p[1], "GENERIC_CONFIG"))
7152 {
7153 msg(msglevel, "this is a generic configuration and cannot directly be used");
7154 goto err;
7155 }
7156 else if (streq(p[1], "PUSH_PEER_INFO") && !p[2])
7157 {
7158 options->push_peer_info = true;
7159 }
7160 else if (streq(p[1], "SERVER_POLL_TIMEOUT") && p[2])
7161 {
7162 options->ce.connect_timeout = positive_atoi(p[2]);
7163 }
7164 else
7165 {
7166 if (streq(p[1], "FORWARD_COMPATIBLE") && p[2] && streq(p[2], "1"))
7167 {
7168 options->forward_compatible = true;
7169 msglevel_fc = msglevel_forward_compatible(options, msglevel);
7170 }
7171 setenv_str(es, p[1], p[2] ? p[2] : "");
7172 }
7173 }
7174 else if (streq(p[0], "compat-mode") && p[1] && !p[3])
7175 {
7176 unsigned int major, minor, patch;
7177 if (!(sscanf(p[1], "%u.%u.%u", &major, &minor, &patch) == 3))
7178 {
7179 msg(msglevel, "cannot parse version number for --compat-mode: %s",
7180 p[1]);
7181 goto err;
7182 }
7183
7184 options->backwards_compatible = major * 10000 + minor * 100 + patch;
7185 }
7186 else if (streq(p[0], "setenv-safe") && p[1] && !p[3])
7187 {
7188 VERIFY_PERMISSION(OPT_P_SETENV);
7189 setenv_str_safe(es, p[1], p[2] ? p[2] : "");
7190 }
7191 else if (streq(p[0], "script-security") && p[1] && !p[2])
7192 {
7193 VERIFY_PERMISSION(OPT_P_GENERAL);
7194 script_security_set(atoi(p[1]));
7195 }
7196 else if (streq(p[0], "mssfix") && !p[3])
7197 {
7198 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
7199 if (p[1])
7200 {
7201 /* value specified, assume encapsulation is not
7202 * included unless "mtu" follows later */
7203 options->ce.mssfix = positive_atoi(p[1]);
7204 options->ce.mssfix_encap = false;
7205 options->ce.mssfix_default = false;
7206 }
7207 else
7208 {
7209 /* Set MTU to default values */
7210 options->ce.mssfix_default = true;
7211 options->ce.mssfix_encap = true;
7212 options->ce.mssfix_fixed = false;
7213 }
7214
7215 if (p[2] && streq(p[2], "mtu"))
7216 {
7217 options->ce.mssfix_encap = true;
7218 }
7219 else if (p[2] && streq(p[2], "fixed"))
7220 {
7221 options->ce.mssfix_fixed = true;
7222 }
7223 else if (p[2])
7224 {
7225 msg(msglevel, "Unknown parameter to --mssfix: %s", p[2]);
7226 }
7227 }
7228 else if (streq(p[0], "disable-occ") && !p[1])
7229 {
7230 VERIFY_PERMISSION(OPT_P_GENERAL);
7231 options->occ = false;
7232 }
7233 else if (streq(p[0], "server") && p[1] && p[2] && !p[4])
7234 {
7235 const int lev = M_WARN;
7236 bool error = false;
7237 in_addr_t network, netmask;
7238
7239 VERIFY_PERMISSION(OPT_P_GENERAL);
7240 network = get_ip_addr(p[1], lev, &error);
7241 netmask = get_ip_addr(p[2], lev, &error);
7242 if (error || !network || !netmask)
7243 {
7244 msg(msglevel, "error parsing --server parameters");
7245 goto err;
7246 }
7247 options->server_defined = true;
7248 options->server_network = network;
7249 options->server_netmask = netmask;
7250
7251 if (p[3])
7252 {
7253 if (streq(p[3], "nopool"))
7254 {
7255 options->server_flags |= SF_NOPOOL;
7256 }
7257 else
7258 {
7259 msg(msglevel, "error parsing --server: %s is not a recognized flag", p[3]);
7260 goto err;
7261 }
7262 }
7263 }
7264 else if (streq(p[0], "server-ipv6") && p[1] && !p[2])
7265 {
7266 const int lev = M_WARN;
7267 struct in6_addr network;
7268 unsigned int netbits = 0;
7269
7270 VERIFY_PERMISSION(OPT_P_GENERAL);
7271 if (!get_ipv6_addr(p[1], &network, &netbits, lev) )
7272 {
7273 msg(msglevel, "error parsing --server-ipv6 parameter");
7274 goto err;
7275 }
7276 if (netbits < 64 || netbits > 124)
7277 {
7278 msg(msglevel,
7279 "--server-ipv6 settings: network must be between /64 and /124 (not /%d)",
7280 netbits);
7281
7282 goto err;
7283 }
7284 options->server_ipv6_defined = true;
7285 options->server_network_ipv6 = network;
7286 options->server_netbits_ipv6 = netbits;
7287 }
7288 else if (streq(p[0], "server-bridge") && p[1] && p[2] && p[3] && p[4] && !p[5])
7289 {
7290 const int lev = M_WARN;
7291 bool error = false;
7292 in_addr_t ip, netmask, pool_start, pool_end;
7293
7294 VERIFY_PERMISSION(OPT_P_GENERAL);
7295 ip = get_ip_addr(p[1], lev, &error);
7296 netmask = get_ip_addr(p[2], lev, &error);
7297 pool_start = get_ip_addr(p[3], lev, &error);
7298 pool_end = get_ip_addr(p[4], lev, &error);
7299 if (error || !ip || !netmask || !pool_start || !pool_end)
7300 {
7301 msg(msglevel, "error parsing --server-bridge parameters");
7302 goto err;
7303 }
7304 options->server_bridge_defined = true;
7305 options->server_bridge_ip = ip;
7306 options->server_bridge_netmask = netmask;
7307 options->server_bridge_pool_start = pool_start;
7308 options->server_bridge_pool_end = pool_end;
7309 }
7310 else if (streq(p[0], "server-bridge") && p[1] && streq(p[1], "nogw") && !p[2])
7311 {
7312 VERIFY_PERMISSION(OPT_P_GENERAL);
7313 options->server_bridge_proxy_dhcp = true;
7314 options->server_flags |= SF_NO_PUSH_ROUTE_GATEWAY;
7315 }
7316 else if (streq(p[0], "server-bridge") && !p[1])
7317 {
7318 VERIFY_PERMISSION(OPT_P_GENERAL);
7319 options->server_bridge_proxy_dhcp = true;
7320 }
7321 else if (streq(p[0], "push") && p[1] && !p[2])
7322 {
7323 VERIFY_PERMISSION(OPT_P_PUSH);
7324 push_options(options, &p[1], msglevel, &options->gc);
7325 }
7326 else if (streq(p[0], "push-reset") && !p[1])
7327 {
7328 VERIFY_PERMISSION(OPT_P_INSTANCE);
7329 push_reset(options);
7330 }
7331 else if (streq(p[0], "push-remove") && p[1] && !p[2])
7332 {
7333 VERIFY_PERMISSION(OPT_P_INSTANCE);
7334 msg(D_PUSH, "PUSH_REMOVE '%s'", p[1]);
7335 push_remove_option(options, p[1]);
7336 }
7337 else if (streq(p[0], "ifconfig-pool") && p[1] && p[2] && !p[4])
7338 {
7339 const int lev = M_WARN;
7340 bool error = false;
7341 in_addr_t start, end, netmask = 0;
7342
7343 VERIFY_PERMISSION(OPT_P_GENERAL);
7344 start = get_ip_addr(p[1], lev, &error);
7345 end = get_ip_addr(p[2], lev, &error);
7346 if (p[3])
7347 {
7348 netmask = get_ip_addr(p[3], lev, &error);
7349 }
7350 if (error)
7351 {
7352 msg(msglevel, "error parsing --ifconfig-pool parameters");
7353 goto err;
7354 }
7355 if (!ifconfig_pool_verify_range(msglevel, start, end))
7356 {
7357 goto err;
7358 }
7359
7360 options->ifconfig_pool_defined = true;
7361 options->ifconfig_pool_start = start;
7362 options->ifconfig_pool_end = end;
7363 if (netmask)
7364 {
7365 options->ifconfig_pool_netmask = netmask;
7366 }
7367 }
7368 else if (streq(p[0], "ifconfig-pool-persist") && p[1] && !p[3])
7369 {
7370 VERIFY_PERMISSION(OPT_P_GENERAL);
7371 options->ifconfig_pool_persist_filename = p[1];
7372 if (p[2])
7373 {
7374 options->ifconfig_pool_persist_refresh_freq = positive_atoi(p[2]);
7375 }
7376 }
7377 else if (streq(p[0], "ifconfig-ipv6-pool") && p[1] && !p[2])
7378 {
7379 const int lev = M_WARN;
7380 struct in6_addr network;
7381 unsigned int netbits = 0;
7382
7383 VERIFY_PERMISSION(OPT_P_GENERAL);
7384 if (!get_ipv6_addr(p[1], &network, &netbits, lev ) )
7385 {
7386 msg(msglevel, "error parsing --ifconfig-ipv6-pool parameters");
7387 goto err;
7388 }
7389 if (netbits < 64 || netbits > 124)
7390 {
7391 msg(msglevel,
7392 "--ifconfig-ipv6-pool settings: network must be between /64 and /124 (not /%d)",
7393 netbits);
7394 goto err;
7395 }
7396
7397 options->ifconfig_ipv6_pool_defined = true;
7398 options->ifconfig_ipv6_pool_base = network;
7399 options->ifconfig_ipv6_pool_netbits = netbits;
7400 }
7401 else if (streq(p[0], "hash-size") && p[1] && p[2] && !p[3])
7402 {
7403 int real, virtual;
7404
7405 VERIFY_PERMISSION(OPT_P_GENERAL);
7406 real = atoi(p[1]);
7407 virtual = atoi(p[2]);
7408 if (real < 1 || virtual < 1)
7409 {
7410 msg(msglevel, "--hash-size sizes must be >= 1 (preferably a power of 2)");
7411 goto err;
7412 }
7413 options->real_hash_size = real;
7414 options->virtual_hash_size = real;
7415 }
7416 else if (streq(p[0], "connect-freq") && p[1] && p[2] && !p[3])
7417 {
7418 int cf_max, cf_per;
7419
7420 VERIFY_PERMISSION(OPT_P_GENERAL);
7421 cf_max = atoi(p[1]);
7422 cf_per = atoi(p[2]);
7423 if (cf_max < 0 || cf_per < 0)
7424 {
7425 msg(msglevel, "--connect-freq parms must be > 0");
7426 goto err;
7427 }
7428 options->cf_max = cf_max;
7429 options->cf_per = cf_per;
7430 }
7431 else if (streq(p[0], "max-clients") && p[1] && !p[2])
7432 {
7433 int max_clients;
7434
7435 VERIFY_PERMISSION(OPT_P_GENERAL);
7436 max_clients = atoi(p[1]);
7437 if (max_clients < 0)
7438 {
7439 msg(msglevel, "--max-clients must be at least 1");
7440 goto err;
7441 }
7442 if (max_clients >= MAX_PEER_ID) /* max peer-id value */
7443 {
7444 msg(msglevel, "--max-clients must be less than %d", MAX_PEER_ID);
7445 goto err;
7446 }
7447 options->max_clients = max_clients;
7448 }
7449 else if (streq(p[0], "max-routes-per-client") && p[1] && !p[2])
7450 {
7451 VERIFY_PERMISSION(OPT_P_INHERIT);
7452 options->max_routes_per_client = max_int(atoi(p[1]), 1);
7453 }
7454 else if (streq(p[0], "client-cert-not-required") && !p[1])
7455 {
7456 VERIFY_PERMISSION(OPT_P_GENERAL);
7457 msg(M_FATAL, "REMOVED OPTION: --client-cert-not-required, use '--verify-client-cert none' instead");
7458 }
7459 else if (streq(p[0], "verify-client-cert") && !p[2])
7460 {
7461 VERIFY_PERMISSION(OPT_P_GENERAL);
7462
7463 /* Reset any existing flags */
7464 options->ssl_flags &= ~SSLF_CLIENT_CERT_OPTIONAL;
7465 options->ssl_flags &= ~SSLF_CLIENT_CERT_NOT_REQUIRED;
7466 if (p[1])
7467 {
7468 if (streq(p[1], "none"))
7469 {
7470 options->ssl_flags |= SSLF_CLIENT_CERT_NOT_REQUIRED;
7471 }
7472 else if (streq(p[1], "optional"))
7473 {
7474 options->ssl_flags |= SSLF_CLIENT_CERT_OPTIONAL;
7475 }
7476 else if (!streq(p[1], "require"))
7477 {
7478 msg(msglevel, "parameter to --verify-client-cert must be 'none', 'optional' or 'require'");
7479 goto err;
7480 }
7481 }
7482 }
7483 else if (streq(p[0], "username-as-common-name") && !p[1])
7484 {
7485 VERIFY_PERMISSION(OPT_P_GENERAL);
7486 options->ssl_flags |= SSLF_USERNAME_AS_COMMON_NAME;
7487 }
7488 else if (streq(p[0], "auth-user-pass-optional") && !p[1])
7489 {
7490 VERIFY_PERMISSION(OPT_P_GENERAL);
7491 options->ssl_flags |= SSLF_AUTH_USER_PASS_OPTIONAL;
7492 }
7493 else if (streq(p[0], "opt-verify") && !p[1])
7494 {
7495 VERIFY_PERMISSION(OPT_P_GENERAL);
7496 options->ssl_flags |= SSLF_OPT_VERIFY;
7497 }
7498 else if (streq(p[0], "auth-user-pass-verify") && p[1])
7499 {
7500 VERIFY_PERMISSION(OPT_P_SCRIPT);
7501 if (!no_more_than_n_args(msglevel, p, 3, NM_QUOTE_HINT))
7502 {
7503 goto err;
7504 }
7505 if (p[2])
7506 {
7507 if (streq(p[2], "via-env"))
7508 {
7509 options->auth_user_pass_verify_script_via_file = false;
7510 }
7511 else if (streq(p[2], "via-file"))
7512 {
7513 options->auth_user_pass_verify_script_via_file = true;
7514 }
7515 else
7516 {
7517 msg(msglevel, "second parm to --auth-user-pass-verify must be 'via-env' or 'via-file'");
7518 goto err;
7519 }
7520 }
7521 else
7522 {
7523 msg(msglevel, "--auth-user-pass-verify requires a second parameter ('via-env' or 'via-file')");
7524 goto err;
7525 }
7526 set_user_script(options,
7527 &options->auth_user_pass_verify_script,
7528 p[1], "auth-user-pass-verify", true);
7529 }
7530 else if (streq(p[0], "auth-gen-token"))
7531 {
7532 VERIFY_PERMISSION(OPT_P_GENERAL);
7533 options->auth_token_generate = true;
7534 options->auth_token_lifetime = p[1] ? positive_atoi(p[1]) : 0;
7535
7536 for (int i = 2; i < MAX_PARMS && p[i] != NULL; i++)
7537 {
7538 /* the second parameter can be the renewal time */
7539 if (i == 2 && positive_atoi(p[i]))
7540 {
7541 options->auth_token_renewal = positive_atoi(p[i]);
7542 }
7543 else if (streq(p[i], "external-auth"))
7544 {
7545 options->auth_token_call_auth = true;
7546 }
7547 else
7548 {
7549 msg(msglevel, "Invalid argument to auth-gen-token: %s (%d)", p[i], i);
7550 }
7551 }
7552
7553 }
7554 else if (streq(p[0], "auth-gen-token-secret") && p[1] && !p[2])
7555 {
7556 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
7557 options->auth_token_secret_file = p[1];
7558 options->auth_token_secret_file_inline = is_inline;
7559
7560 }
7561 else if (streq(p[0], "client-connect") && p[1])
7562 {
7563 VERIFY_PERMISSION(OPT_P_SCRIPT);
7564 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
7565 {
7566 goto err;
7567 }
7568 set_user_script(options, &options->client_connect_script,
7569 p[1], "client-connect", true);
7570 }
7571 else if (streq(p[0], "client-crresponse") && p[1])
7572 {
7573 VERIFY_PERMISSION(OPT_P_SCRIPT);
7574 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
7575 {
7576 goto err;
7577 }
7578 set_user_script(options, &options->client_crresponse_script,
7579 p[1], "client-crresponse", true);
7580 }
7581 else if (streq(p[0], "client-disconnect") && p[1])
7582 {
7583 VERIFY_PERMISSION(OPT_P_SCRIPT);
7584 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
7585 {
7586 goto err;
7587 }
7588 set_user_script(options, &options->client_disconnect_script,
7589 p[1], "client-disconnect", true);
7590 }
7591 else if (streq(p[0], "learn-address") && p[1])
7592 {
7593 VERIFY_PERMISSION(OPT_P_SCRIPT);
7594 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
7595 {
7596 goto err;
7597 }
7598 set_user_script(options, &options->learn_address_script,
7599 p[1], "learn-address", true);
7600 }
7601 else if (streq(p[0], "tmp-dir") && p[1] && !p[2])
7602 {
7603 VERIFY_PERMISSION(OPT_P_GENERAL);
7604 options->tmp_dir = p[1];
7605 }
7606 else if (streq(p[0], "client-config-dir") && p[1] && !p[2])
7607 {
7608 VERIFY_PERMISSION(OPT_P_GENERAL);
7609 options->client_config_dir = p[1];
7610 }
7611 else if (streq(p[0], "ccd-exclusive") && !p[1])
7612 {
7613 VERIFY_PERMISSION(OPT_P_GENERAL);
7614 options->ccd_exclusive = true;
7615 }
7616 else if (streq(p[0], "bcast-buffers") && p[1] && !p[2])
7617 {
7618 int n_bcast_buf;
7619
7620 VERIFY_PERMISSION(OPT_P_GENERAL);
7621 n_bcast_buf = atoi(p[1]);
7622 if (n_bcast_buf < 1)
7623 {
7624 msg(msglevel, "--bcast-buffers parameter must be > 0");
7625 }
7626 options->n_bcast_buf = n_bcast_buf;
7627 }
7628 else if (streq(p[0], "tcp-queue-limit") && p[1] && !p[2])
7629 {
7630 int tcp_queue_limit;
7631
7632 VERIFY_PERMISSION(OPT_P_GENERAL);
7633 tcp_queue_limit = atoi(p[1]);
7634 if (tcp_queue_limit < 1)
7635 {
7636 msg(msglevel, "--tcp-queue-limit parameter must be > 0");
7637 }
7638 options->tcp_queue_limit = tcp_queue_limit;
7639 }
7640 #if PORT_SHARE
7641 else if (streq(p[0], "port-share") && p[1] && p[2] && !p[4])
7642 {
7643 VERIFY_PERMISSION(OPT_P_GENERAL);
7644 options->port_share_host = p[1];
7645 options->port_share_port = p[2];
7646 options->port_share_journal_dir = p[3];
7647 }
7648 #endif
7649 else if (streq(p[0], "client-to-client") && !p[1])
7650 {
7651 VERIFY_PERMISSION(OPT_P_GENERAL);
7652 options->enable_c2c = true;
7653 }
7654 else if (streq(p[0], "duplicate-cn") && !p[1])
7655 {
7656 VERIFY_PERMISSION(OPT_P_GENERAL);
7657 options->duplicate_cn = true;
7658 }
7659 else if (streq(p[0], "iroute") && p[1] && !p[3])
7660 {
7661 VERIFY_PERMISSION(OPT_P_INSTANCE);
7662 option_iroute(options, p[1], p[2], msglevel);
7663 }
7664 else if (streq(p[0], "iroute-ipv6") && p[1] && !p[2])
7665 {
7666 VERIFY_PERMISSION(OPT_P_INSTANCE);
7667 option_iroute_ipv6(options, p[1], msglevel);
7668 }
7669 else if (streq(p[0], "ifconfig-push") && p[1] && p[2] && !p[4])
7670 {
7671 in_addr_t local, remote_netmask;
7672
7673 VERIFY_PERMISSION(OPT_P_INSTANCE);
7674 local = getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[1], 0, NULL, NULL);
7675 remote_netmask = getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[2], 0, NULL, NULL);
7676 if (local && remote_netmask)
7677 {
7678 options->push_ifconfig_defined = true;
7679 options->push_ifconfig_local = local;
7680 options->push_ifconfig_remote_netmask = remote_netmask;
7681 if (p[3])
7682 {
7683 options->push_ifconfig_local_alias = getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[3], 0, NULL, NULL);
7684 }
7685 }
7686 else
7687 {
7688 msg(msglevel, "cannot parse --ifconfig-push addresses");
7689 goto err;
7690 }
7691 }
7692 else if (streq(p[0], "ifconfig-push-constraint") && p[1] && p[2] && !p[3])
7693 {
7694 in_addr_t network, netmask;
7695
7696 VERIFY_PERMISSION(OPT_P_GENERAL);
7697 network = getaddr(GETADDR_HOST_ORDER|GETADDR_RESOLVE, p[1], 0, NULL, NULL);
7698 netmask = getaddr(GETADDR_HOST_ORDER, p[2], 0, NULL, NULL);
7699 if (network && netmask)
7700 {
7701 options->push_ifconfig_constraint_defined = true;
7702 options->push_ifconfig_constraint_network = network;
7703 options->push_ifconfig_constraint_netmask = netmask;
7704 }
7705 else
7706 {
7707 msg(msglevel, "cannot parse --ifconfig-push-constraint addresses");
7708 goto err;
7709 }
7710 }
7711 else if (streq(p[0], "ifconfig-ipv6-push") && p[1] && !p[3])
7712 {
7713 struct in6_addr local, remote;
7714 unsigned int netbits;
7715
7716 VERIFY_PERMISSION(OPT_P_INSTANCE);
7717
7718 if (!get_ipv6_addr( p[1], &local, &netbits, msglevel ) )
7719 {
7720 msg(msglevel, "cannot parse --ifconfig-ipv6-push addresses");
7721 goto err;
7722 }
7723
7724 if (p[2])
7725 {
7726 if (!get_ipv6_addr( p[2], &remote, NULL, msglevel ) )
7727 {
7728 msg( msglevel, "cannot parse --ifconfig-ipv6-push addresses");
7729 goto err;
7730 }
7731 }
7732 else
7733 {
7734 if (!options->ifconfig_ipv6_local
7735 || !get_ipv6_addr( options->ifconfig_ipv6_local, &remote,
7736 NULL, msglevel ) )
7737 {
7738 msg( msglevel, "second argument to --ifconfig-ipv6-push missing and no global --ifconfig-ipv6 address set");
7739 goto err;
7740 }
7741 }
7742
7743 options->push_ifconfig_ipv6_defined = true;
7744 options->push_ifconfig_ipv6_local = local;
7745 options->push_ifconfig_ipv6_netbits = netbits;
7746 options->push_ifconfig_ipv6_remote = remote;
7747 options->push_ifconfig_ipv6_blocked = false;
7748 }
7749 else if (streq(p[0], "disable") && !p[1])
7750 {
7751 VERIFY_PERMISSION(OPT_P_INSTANCE);
7752 options->disable = true;
7753 }
7754 else if (streq(p[0], "tcp-nodelay") && !p[1])
7755 {
7756 VERIFY_PERMISSION(OPT_P_GENERAL);
7757 options->server_flags |= SF_TCP_NODELAY_HELPER;
7758 }
7759 else if (streq(p[0], "stale-routes-check") && p[1] && !p[3])
7760 {
7761 int ageing_time, check_interval;
7762
7763 VERIFY_PERMISSION(OPT_P_GENERAL);
7764 ageing_time = atoi(p[1]);
7765 if (p[2])
7766 {
7767 check_interval = atoi(p[2]);
7768 }
7769 else
7770 {
7771 check_interval = ageing_time;
7772 }
7773
7774 if (ageing_time < 1 || check_interval < 1)
7775 {
7776 msg(msglevel, "--stale-routes-check aging time and check interval must be >= 1");
7777 goto err;
7778 }
7779 options->stale_routes_ageing_time = ageing_time;
7780 options->stale_routes_check_interval = check_interval;
7781 }
7782
7783 else if (streq(p[0], "client") && !p[1])
7784 {
7785 VERIFY_PERMISSION(OPT_P_GENERAL);
7786 options->client = true;
7787 }
7788 else if (streq(p[0], "pull") && !p[1])
7789 {
7790 VERIFY_PERMISSION(OPT_P_GENERAL);
7791 options->pull = true;
7792 }
7793 else if (streq(p[0], "push-continuation") && p[1] && !p[2])
7794 {
7795 VERIFY_PERMISSION(OPT_P_PULL_MODE);
7796 options->push_continuation = atoi(p[1]);
7797 }
7798 else if (streq(p[0], "auth-user-pass") && !p[2])
7799 {
7800 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
7801 if (p[1])
7802 {
7803 options->auth_user_pass_file = p[1];
7804 options->auth_user_pass_file_inline = is_inline;
7805 }
7806 else
7807 {
7808 options->auth_user_pass_file = "stdin";
7809 }
7810 }
7811 else if (streq(p[0], "auth-retry") && p[1] && !p[2])
7812 {
7813 VERIFY_PERMISSION(OPT_P_GENERAL);
7814 auth_retry_set(msglevel, p[1]);
7815 }
7816 #ifdef ENABLE_MANAGEMENT
7817 else if (streq(p[0], "static-challenge") && p[1] && p[2] && !p[3])
7818 {
7819 VERIFY_PERMISSION(OPT_P_GENERAL);
7820 options->sc_info.challenge_text = p[1];
7821 if (atoi(p[2]))
7822 {
7823 options->sc_info.flags |= SC_ECHO;
7824 }
7825 }
7826 #endif
7827 else if (streq(p[0], "msg-channel") && p[1])
7828 {
7829 #ifdef _WIN32
7830 VERIFY_PERMISSION(OPT_P_GENERAL);
7831 HANDLE process = GetCurrentProcess();
7832 HANDLE handle = (HANDLE) atoll(p[1]);
7833 if (!DuplicateHandle(process, handle, process, &options->msg_channel, 0,
7834 FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
7835 {
7836 msg(msglevel, "could not duplicate service pipe handle");
7837 goto err;
7838 }
7839 options->route_method = ROUTE_METHOD_SERVICE;
7840 #else /* ifdef _WIN32 */
7841 msg(msglevel, "--msg-channel is only supported on Windows");
7842 goto err;
7843 #endif
7844 }
7845 #ifdef _WIN32
7846 else if (streq(p[0], "win-sys") && p[1] && !p[2])
7847 {
7848 VERIFY_PERMISSION(OPT_P_GENERAL);
7849 if (streq(p[1], "env"))
7850 {
7851 msg(M_INFO, "NOTE: --win-sys env is default from OpenVPN 2.3. "
7852 "This entry will now be ignored. "
7853 "Please remove this entry from your configuration file.");
7854 }
7855 else
7856 {
7857 set_win_sys_path(p[1], es);
7858 }
7859 }
7860 else if (streq(p[0], "route-method") && p[1] && !p[2])
7861 {
7862 VERIFY_PERMISSION(OPT_P_ROUTE_EXTRAS);
7863 if (streq(p[1], "adaptive"))
7864 {
7865 options->route_method = ROUTE_METHOD_ADAPTIVE;
7866 }
7867 else if (streq(p[1], "ipapi"))
7868 {
7869 options->route_method = ROUTE_METHOD_IPAPI;
7870 }
7871 else if (streq(p[1], "exe"))
7872 {
7873 options->route_method = ROUTE_METHOD_EXE;
7874 }
7875 else
7876 {
7877 msg(msglevel, "--route method must be 'adaptive', 'ipapi', or 'exe'");
7878 goto err;
7879 }
7880 }
7881 else if (streq(p[0], "ip-win32") && p[1] && !p[4])
7882 {
7883 const int index = ascii2ipset(p[1]);
7884 struct tuntap_options *to = &options->tuntap_options;
7885
7886 VERIFY_PERMISSION(OPT_P_DHCPDNS);
7887
7888 if (index < 0)
7889 {
7890 msg(msglevel,
7891 "Bad --ip-win32 method: '%s'. Allowed methods: %s",
7892 p[1],
7893 ipset2ascii_all(&gc));
7894 goto err;
7895 }
7896
7897 if (index == IPW32_SET_ADAPTIVE)
7898 {
7899 options->route_delay_window = IPW32_SET_ADAPTIVE_DELAY_WINDOW;
7900 }
7901
7902 if (index == IPW32_SET_DHCP_MASQ)
7903 {
7904 if (p[2])
7905 {
7906 if (!streq(p[2], "default"))
7907 {
7908 int offset = atoi(p[2]);
7909
7910 if (!(offset > -256 && offset < 256))
7911 {
7912 msg(msglevel, "--ip-win32 dynamic [offset] [lease-time]: offset (%d) must be > -256 and < 256", offset);
7913 goto err;
7914 }
7915
7916 to->dhcp_masq_custom_offset = true;
7917 to->dhcp_masq_offset = offset;
7918 }
7919
7920 if (p[3])
7921 {
7922 const int min_lease = 30;
7923 int lease_time;
7924 lease_time = atoi(p[3]);
7925 if (lease_time < min_lease)
7926 {
7927 msg(msglevel, "--ip-win32 dynamic [offset] [lease-time]: lease time parameter (%d) must be at least %d seconds", lease_time, min_lease);
7928 goto err;
7929 }
7930 to->dhcp_lease_time = lease_time;
7931 }
7932 }
7933 }
7934 to->ip_win32_type = index;
7935 to->ip_win32_defined = true;
7936 }
7937 #endif /* ifdef _WIN32 */
7938 else if (streq(p[0], "dns") && p[1])
7939 {
7940 VERIFY_PERMISSION(OPT_P_DHCPDNS);
7941
7942 if (streq(p[1], "search-domains") && p[2])
7943 {
7944 dns_domain_list_append(&options->dns_options.search_domains, &p[2], &options->dns_options.gc);
7945 }
7946 else if (streq(p[1], "server") && p[2] && p[3] && p[4])
7947 {
7948 long priority;
7949 if (!dns_server_priority_parse(&priority, p[2], pull_mode))
7950 {
7951 msg(msglevel, "--dns server: invalid priority value '%s'", p[2]);
7952 goto err;
7953 }
7954
7955 struct dns_server *server = dns_server_get(&options->dns_options.servers, priority, &options->dns_options.gc);
7956
7957 if (streq(p[3], "address") && !p[6])
7958 {
7959 for (int i = 4; p[i]; i++)
7960 {
7961 if (!dns_server_addr_parse(server, p[i]))
7962 {
7963 msg(msglevel, "--dns server %ld: malformed or duplicate address '%s'", priority, p[i]);
7964 goto err;
7965 }
7966 }
7967 }
7968 else if (streq(p[3], "resolve-domains"))
7969 {
7970 if (server->domain_type == DNS_EXCLUDE_DOMAINS)
7971 {
7972 msg(msglevel, "--dns server %ld: cannot use resolve-domains and exclude-domains", priority);
7973 goto err;
7974 }
7975 server->domain_type = DNS_RESOLVE_DOMAINS;
7976 dns_domain_list_append(&server->domains, &p[4], &options->dns_options.gc);
7977 }
7978 else if (streq(p[3], "exclude-domains"))
7979 {
7980 if (server->domain_type == DNS_RESOLVE_DOMAINS)
7981 {
7982 msg(msglevel, "--dns server %ld: cannot use exclude-domains and resolve-domains", priority);
7983 goto err;
7984 }
7985 server->domain_type = DNS_EXCLUDE_DOMAINS;
7986 dns_domain_list_append(&server->domains, &p[4], &options->dns_options.gc);
7987 }
7988 else if (streq(p[3], "dnssec") && !p[5])
7989 {
7990 if (streq(p[4], "yes"))
7991 {
7992 server->dnssec = DNS_SECURITY_YES;
7993 }
7994 else if (streq(p[4], "no"))
7995 {
7996 server->dnssec = DNS_SECURITY_NO;
7997 }
7998 else if (streq(p[4], "optional"))
7999 {
8000 server->dnssec = DNS_SECURITY_OPTIONAL;
8001 }
8002 else
8003 {
8004 msg(msglevel, "--dns server %ld: malformed dnssec value '%s'", priority, p[4]);
8005 goto err;
8006 }
8007 }
8008 else if (streq(p[3], "transport") && !p[5])
8009 {
8010 if (streq(p[4], "plain"))
8011 {
8012 server->transport = DNS_TRANSPORT_PLAIN;
8013 }
8014 else if (streq(p[4], "DoH"))
8015 {
8016 server->transport = DNS_TRANSPORT_HTTPS;
8017 }
8018 else if (streq(p[4], "DoT"))
8019 {
8020 server->transport = DNS_TRANSPORT_TLS;
8021 }
8022 else
8023 {
8024 msg(msglevel, "--dns server %ld: malformed transport value '%s'", priority, p[4]);
8025 goto err;
8026 }
8027 }
8028 else if (streq(p[3], "sni") && !p[5])
8029 {
8030 server->sni = p[4];
8031 }
8032 else
8033 {
8034 msg(msglevel, "--dns server %ld: unknown option type '%s' or missing or unknown parameter", priority, p[3]);
8035 goto err;
8036 }
8037 }
8038 else
8039 {
8040 msg(msglevel, "--dns: unknown option type '%s' or missing or unknown parameter", p[1]);
8041 goto err;
8042 }
8043 }
8044 #if defined(_WIN32) || defined(TARGET_ANDROID)
8045 else if (streq(p[0], "dhcp-option") && p[1])
8046 {
8047 struct tuntap_options *o = &options->tuntap_options;
8048 VERIFY_PERMISSION(OPT_P_DHCPDNS);
8049 bool ipv6dns = false;
8050
8051 if ((streq(p[1], "DOMAIN") || streq(p[1], "ADAPTER_DOMAIN_SUFFIX"))
8052 && p[2] && !p[3])
8053 {
8054 o->domain = p[2];
8055 }
8056 else if (streq(p[1], "NBS") && p[2] && !p[3])
8057 {
8058 o->netbios_scope = p[2];
8059 }
8060 else if (streq(p[1], "NBT") && p[2] && !p[3])
8061 {
8062 int t;
8063 t = atoi(p[2]);
8064 if (!(t == 1 || t == 2 || t == 4 || t == 8))
8065 {
8066 msg(msglevel, "--dhcp-option NBT: parameter (%d) must be 1, 2, 4, or 8", t);
8067 goto err;
8068 }
8069 o->netbios_node_type = t;
8070 }
8071 else if ((streq(p[1], "DNS") || streq(p[1], "DNS6")) && p[2] && !p[3]
8072 && (!strstr(p[2], ":") || ipv6_addr_safe(p[2])))
8073 {
8074 if (strstr(p[2], ":"))
8075 {
8076 ipv6dns = true;
8077 dhcp_option_dns6_parse(p[2], o->dns6, &o->dns6_len, msglevel);
8078 }
8079 else
8080 {
8081 dhcp_option_address_parse("DNS", p[2], o->dns, &o->dns_len, msglevel);
8082 }
8083 }
8084 else if (streq(p[1], "WINS") && p[2] && !p[3])
8085 {
8086 dhcp_option_address_parse("WINS", p[2], o->wins, &o->wins_len, msglevel);
8087 }
8088 else if (streq(p[1], "NTP") && p[2] && !p[3])
8089 {
8090 dhcp_option_address_parse("NTP", p[2], o->ntp, &o->ntp_len, msglevel);
8091 }
8092 else if (streq(p[1], "NBDD") && p[2] && !p[3])
8093 {
8094 dhcp_option_address_parse("NBDD", p[2], o->nbdd, &o->nbdd_len, msglevel);
8095 }
8096 else if (streq(p[1], "DOMAIN-SEARCH") && p[2] && !p[3])
8097 {
8098 if (o->domain_search_list_len < N_SEARCH_LIST_LEN)
8099 {
8100 o->domain_search_list[o->domain_search_list_len++] = p[2];
8101 }
8102 else
8103 {
8104 msg(msglevel, "--dhcp-option %s: maximum of %d search entries can be specified",
8105 p[1], N_SEARCH_LIST_LEN);
8106 }
8107 }
8108 else if (streq(p[1], "DISABLE-NBT") && !p[2])
8109 {
8110 o->disable_nbt = 1;
8111 }
8112 #if defined(TARGET_ANDROID)
8113 else if (streq(p[1], "PROXY_HTTP") && p[3] && !p[4])
8114 {
8115 o->http_proxy_port = atoi(p[3]);
8116 o->http_proxy = p[2];
8117 }
8118 #endif
8119 else
8120 {
8121 msg(msglevel, "--dhcp-option: unknown option type '%s' or missing or unknown parameter", p[1]);
8122 goto err;
8123 }
8124
8125 /* flag that we have options to give to the TAP driver's DHCPv4 server
8126 * - skipped for "DNS6", as that's not a DHCPv4 option
8127 */
8128 if (!ipv6dns)
8129 {
8130 o->dhcp_options = true;
8131 }
8132 }
8133 #endif /* if defined(_WIN32) || defined(TARGET_ANDROID) */
8134 #ifdef _WIN32
8135 else if (streq(p[0], "show-adapters") && !p[1])
8136 {
8137 VERIFY_PERMISSION(OPT_P_GENERAL);
8138 show_tap_win_adapters(M_INFO|M_NOPREFIX, M_WARN|M_NOPREFIX);
8139 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
8140 }
8141 else if (streq(p[0], "show-net") && !p[1])
8142 {
8143 VERIFY_PERMISSION(OPT_P_GENERAL);
8144 show_routes(M_INFO|M_NOPREFIX);
8145 show_adapters(M_INFO|M_NOPREFIX);
8146 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
8147 }
8148 else if (streq(p[0], "show-net-up") && !p[1])
8149 {
8150 VERIFY_PERMISSION(OPT_P_UP);
8151 options->show_net_up = true;
8152 }
8153 else if (streq(p[0], "tap-sleep") && p[1] && !p[2])
8154 {
8155 int s;
8156 VERIFY_PERMISSION(OPT_P_DHCPDNS);
8157 s = atoi(p[1]);
8158 if (s < 0 || s >= 256)
8159 {
8160 msg(msglevel, "--tap-sleep parameter must be between 0 and 255");
8161 goto err;
8162 }
8163 options->tuntap_options.tap_sleep = s;
8164 }
8165 else if (streq(p[0], "dhcp-renew") && !p[1])
8166 {
8167 VERIFY_PERMISSION(OPT_P_DHCPDNS);
8168 options->tuntap_options.dhcp_renew = true;
8169 }
8170 else if (streq(p[0], "dhcp-pre-release") && !p[1])
8171 {
8172 VERIFY_PERMISSION(OPT_P_DHCPDNS);
8173 options->tuntap_options.dhcp_pre_release = true;
8174 options->tuntap_options.dhcp_renew = true;
8175 }
8176 else if (streq(p[0], "dhcp-release") && !p[1])
8177 {
8178 msg(M_WARN, "Obsolete option --dhcp-release detected. This is now on by default");
8179 }
8180 else if (streq(p[0], "dhcp-internal") && p[1] && !p[2]) /* standalone method for internal use */
8181 {
8182 unsigned int adapter_index;
8183 VERIFY_PERMISSION(OPT_P_GENERAL);
8184 set_debug_level(options->verbosity, SDL_CONSTRAIN);
8185 adapter_index = atou(p[1]);
8186 sleep(options->tuntap_options.tap_sleep);
8187 if (options->tuntap_options.dhcp_pre_release)
8188 {
8189 dhcp_release_by_adapter_index(adapter_index);
8190 }
8191 if (options->tuntap_options.dhcp_renew)
8192 {
8193 dhcp_renew_by_adapter_index(adapter_index);
8194 }
8195 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
8196 }
8197 else if (streq(p[0], "register-dns") && !p[1])
8198 {
8199 VERIFY_PERMISSION(OPT_P_DHCPDNS);
8200 options->tuntap_options.register_dns = true;
8201 }
8202 else if (streq(p[0], "block-outside-dns") && !p[1])
8203 {
8204 VERIFY_PERMISSION(OPT_P_DHCPDNS);
8205 options->block_outside_dns = true;
8206 }
8207 else if (streq(p[0], "rdns-internal") && !p[1])
8208 /* standalone method for internal use
8209 *
8210 * (if --register-dns is set, openvpn needs to call itself in a
8211 * sub-process to execute the required functions in a non-blocking
8212 * way, and uses --rdns-internal to signal that to itself)
8213 */
8214 {
8215 VERIFY_PERMISSION(OPT_P_GENERAL);
8216 set_debug_level(options->verbosity, SDL_CONSTRAIN);
8217 if (options->tuntap_options.register_dns)
8218 {
8219 ipconfig_register_dns(NULL);
8220 }
8221 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
8222 }
8223 else if (streq(p[0], "show-valid-subnets") && !p[1])
8224 {
8225 VERIFY_PERMISSION(OPT_P_GENERAL);
8226 show_valid_win32_tun_subnets();
8227 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
8228 }
8229 else if (streq(p[0], "pause-exit") && !p[1])
8230 {
8231 VERIFY_PERMISSION(OPT_P_GENERAL);
8232 set_pause_exit_win32();
8233 }
8234 else if (streq(p[0], "service") && p[1] && !p[3])
8235 {
8236 VERIFY_PERMISSION(OPT_P_GENERAL);
8237 options->exit_event_name = p[1];
8238 if (p[2])
8239 {
8240 options->exit_event_initial_state = (atoi(p[2]) != 0);
8241 }
8242 }
8243 else if (streq(p[0], "allow-nonadmin") && !p[2])
8244 {
8245 VERIFY_PERMISSION(OPT_P_GENERAL);
8246 tap_allow_nonadmin_access(p[1]);
8247 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
8248 }
8249 else if (streq(p[0], "user") && p[1] && !p[2])
8250 {
8251 VERIFY_PERMISSION(OPT_P_GENERAL);
8252 msg(M_WARN, "NOTE: --user option is not implemented on Windows");
8253 }
8254 else if (streq(p[0], "group") && p[1] && !p[2])
8255 {
8256 VERIFY_PERMISSION(OPT_P_GENERAL);
8257 msg(M_WARN, "NOTE: --group option is not implemented on Windows");
8258 }
8259 #else /* ifdef _WIN32 */
8260 else if (streq(p[0], "user") && p[1] && !p[2])
8261 {
8262 VERIFY_PERMISSION(OPT_P_GENERAL);
8263 options->username = p[1];
8264 }
8265 else if (streq(p[0], "group") && p[1] && !p[2])
8266 {
8267 VERIFY_PERMISSION(OPT_P_GENERAL);
8268 options->groupname = p[1];
8269 }
8270 else if (streq(p[0], "dhcp-option") && p[1] && !p[3])
8271 {
8272 VERIFY_PERMISSION(OPT_P_DHCPDNS);
8273 setenv_foreign_option(options, (const char **)p, 3, es);
8274 }
8275 else if (streq(p[0], "route-method") && p[1] && !p[2]) /* ignore when pushed to non-Windows OS */
8276 {
8277 VERIFY_PERMISSION(OPT_P_ROUTE_EXTRAS);
8278 }
8279 #endif /* ifdef _WIN32 */
8280 #if PASSTOS_CAPABILITY
8281 else if (streq(p[0], "passtos") && !p[1])
8282 {
8283 VERIFY_PERMISSION(OPT_P_GENERAL);
8284 options->passtos = true;
8285 }
8286 #endif
8287 #if defined(USE_COMP)
8288 else if (streq(p[0], "allow-compression") && p[1] && !p[2])
8289 {
8290 VERIFY_PERMISSION(OPT_P_GENERAL);
8291
8292 if (streq(p[1], "no"))
8293 {
8294 options->comp.flags =
8295 COMP_F_ALLOW_STUB_ONLY|COMP_F_ADVERTISE_STUBS_ONLY;
8296 if (comp_non_stub_enabled(&options->comp))
8297 {
8298 msg(msglevel, "'--allow-compression no' conflicts with "
8299 " enabling compression");
8300 }
8301 }
8302 else if (options->comp.flags & COMP_F_ALLOW_STUB_ONLY)
8303 {
8304 /* Also printed on a push to hint at configuration problems */
8305 msg(msglevel, "Cannot set allow-compression to '%s' "
8306 "after set to 'no'", p[1]);
8307 goto err;
8308 }
8309 else if (streq(p[1], "asym"))
8310 {
8311 options->comp.flags &= ~COMP_F_ALLOW_COMPRESS;
8312 options->comp.flags |= COMP_F_ALLOW_ASYM;
8313 }
8314 else if (streq(p[1], "yes"))
8315 {
8316 msg(M_WARN, "WARNING: Compression for sending and receiving enabled. Compression has "
8317 "been used in the past to break encryption. Allowing compression allows "
8318 "attacks that break encryption. Using \"--allow-compression yes\" is "
8319 "strongly discouraged for common usage. See --compress in the manual "
8320 "page for more information ");
8321
8322 options->comp.flags |= COMP_F_ALLOW_COMPRESS;
8323 }
8324 else
8325 {
8326 msg(msglevel, "bad allow-compression option: %s -- "
8327 "must be 'yes', 'no', or 'asym'", p[1]);
8328 goto err;
8329 }
8330 }
8331 else if (streq(p[0], "comp-lzo") && !p[2])
8332 {
8333 VERIFY_PERMISSION(OPT_P_COMP);
8334
8335 /* All lzo variants do not use swap */
8336 options->comp.flags &= ~COMP_F_SWAP;
8337 #if defined(ENABLE_LZO)
8338 if (p[1] && streq(p[1], "no"))
8339 #endif
8340 {
8341 options->comp.alg = COMP_ALG_STUB;
8342 options->comp.flags &= ~COMP_F_ADAPTIVE;
8343 }
8344 #if defined(ENABLE_LZO)
8345 else if (options->comp.flags & COMP_F_ALLOW_STUB_ONLY)
8346 {
8347 /* Also printed on a push to hint at configuration problems */
8348 msg(msglevel, "Cannot set comp-lzo to '%s', "
8349 "allow-compression is set to 'no'", p[1]);
8350 goto err;
8351 }
8352 else if (p[1])
8353 {
8354 if (streq(p[1], "yes"))
8355 {
8356 options->comp.alg = COMP_ALG_LZO;
8357 options->comp.flags &= ~COMP_F_ADAPTIVE;
8358 }
8359 else if (streq(p[1], "adaptive"))
8360 {
8361 options->comp.alg = COMP_ALG_LZO;
8362 options->comp.flags |= COMP_F_ADAPTIVE;
8363 }
8364 else
8365 {
8366 msg(msglevel, "bad comp-lzo option: %s -- must be 'yes', 'no', or 'adaptive'", p[1]);
8367 goto err;
8368 }
8369 }
8370 else
8371 {
8372 options->comp.alg = COMP_ALG_LZO;
8373 options->comp.flags |= COMP_F_ADAPTIVE;
8374 }
8375 show_compression_warning(&options->comp);
8376 #endif /* if defined(ENABLE_LZO) */
8377 }
8378 else if (streq(p[0], "comp-noadapt") && !p[1])
8379 {
8380 /*
8381 * We do not need to check here if we allow compression since
8382 * it only modifies a flag if compression is enabled
8383 */
8384 VERIFY_PERMISSION(OPT_P_COMP);
8385 options->comp.flags &= ~COMP_F_ADAPTIVE;
8386 }
8387 else if (streq(p[0], "compress") && !p[2])
8388 {
8389 VERIFY_PERMISSION(OPT_P_COMP);
8390 if (p[1])
8391 {
8392 if (streq(p[1], "stub"))
8393 {
8394 options->comp.alg = COMP_ALG_STUB;
8395 options->comp.flags |= (COMP_F_SWAP|COMP_F_ADVERTISE_STUBS_ONLY);
8396 }
8397 else if (streq(p[1], "stub-v2"))
8398 {
8399 options->comp.alg = COMP_ALGV2_UNCOMPRESSED;
8400 options->comp.flags |= COMP_F_ADVERTISE_STUBS_ONLY;
8401 }
8402 else if (streq(p[1], "migrate"))
8403 {
8404 options->comp.alg = COMP_ALG_UNDEF;
8405 options->comp.flags = COMP_F_MIGRATE;
8406
8407 }
8408 else if (options->comp.flags & COMP_F_ALLOW_STUB_ONLY)
8409 {
8410 /* Also printed on a push to hint at configuration problems */
8411 msg(msglevel, "Cannot set compress to '%s', "
8412 "allow-compression is set to 'no'", p[1]);
8413 goto err;
8414 }
8415 #if defined(ENABLE_LZO)
8416 else if (streq(p[1], "lzo"))
8417 {
8418 options->comp.alg = COMP_ALG_LZO;
8419 options->comp.flags &= ~(COMP_F_ADAPTIVE | COMP_F_SWAP);
8420 }
8421 #endif
8422 #if defined(ENABLE_LZ4)
8423 else if (streq(p[1], "lz4"))
8424 {
8425 options->comp.alg = COMP_ALG_LZ4;
8426 options->comp.flags |= COMP_F_SWAP;
8427 }
8428 else if (streq(p[1], "lz4-v2"))
8429 {
8430 options->comp.alg = COMP_ALGV2_LZ4;
8431 }
8432 #endif
8433 else
8434 {
8435 msg(msglevel, "bad comp option: %s", p[1]);
8436 goto err;
8437 }
8438 }
8439 else
8440 {
8441 options->comp.alg = COMP_ALG_STUB;
8442 options->comp.flags |= COMP_F_SWAP;
8443 }
8444 show_compression_warning(&options->comp);
8445 }
8446 #endif /* USE_COMP */
8447 else if (streq(p[0], "show-ciphers") && !p[1])
8448 {
8449 VERIFY_PERMISSION(OPT_P_GENERAL);
8450 options->show_ciphers = true;
8451 }
8452 else if (streq(p[0], "show-digests") && !p[1])
8453 {
8454 VERIFY_PERMISSION(OPT_P_GENERAL);
8455 options->show_digests = true;
8456 }
8457 else if (streq(p[0], "show-engines") && !p[1])
8458 {
8459 VERIFY_PERMISSION(OPT_P_GENERAL);
8460 options->show_engines = true;
8461 }
8462 else if (streq(p[0], "key-direction") && p[1] && !p[2])
8463 {
8464 int key_direction;
8465
8466 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
8467
8468 key_direction = ascii2keydirection(msglevel, p[1]);
8469 if (key_direction >= 0)
8470 {
8471 if (permission_mask & OPT_P_GENERAL)
8472 {
8473 options->key_direction = key_direction;
8474 }
8475 else if (permission_mask & OPT_P_CONNECTION)
8476 {
8477 options->ce.key_direction = key_direction;
8478 }
8479 }
8480 else
8481 {
8482 goto err;
8483 }
8484 }
8485 else if (streq(p[0], "secret") && p[1] && !p[3])
8486 {
8487 msg(M_WARN, "DEPRECATED OPTION: The option --secret is deprecated.");
8488 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8489 options->shared_secret_file = p[1];
8490 options->shared_secret_file_inline = is_inline;
8491 if (!is_inline && p[2])
8492 {
8493 int key_direction;
8494
8495 key_direction = ascii2keydirection(msglevel, p[2]);
8496 if (key_direction >= 0)
8497 {
8498 options->key_direction = key_direction;
8499 }
8500 else
8501 {
8502 goto err;
8503 }
8504 }
8505 }
8506 else if (streq(p[0], "genkey") && !p[4])
8507 {
8508 VERIFY_PERMISSION(OPT_P_GENERAL);
8509 options->genkey = true;
8510 if (!p[1])
8511 {
8512 options->genkey_type = GENKEY_SECRET;
8513 }
8514 else
8515 {
8516 if (streq(p[1], "secret") || streq(p[1], "tls-auth")
8517 || streq(p[1], "tls-crypt"))
8518 {
8519 options->genkey_type = GENKEY_SECRET;
8520 }
8521 else if (streq(p[1], "tls-crypt-v2-server"))
8522 {
8523 options->genkey_type = GENKEY_TLS_CRYPTV2_SERVER;
8524 }
8525 else if (streq(p[1], "tls-crypt-v2-client"))
8526 {
8527 options->genkey_type = GENKEY_TLS_CRYPTV2_CLIENT;
8528 if (p[3])
8529 {
8530 options->genkey_extra_data = p[3];
8531 }
8532 }
8533 else if (streq(p[1], "auth-token"))
8534 {
8535 options->genkey_type = GENKEY_AUTH_TOKEN;
8536 }
8537 else
8538 {
8539 msg(msglevel, "unknown --genkey type: %s", p[1]);
8540 }
8541
8542 }
8543 if (p[2])
8544 {
8545 options->genkey_filename = p[2];
8546 }
8547 }
8548 else if (streq(p[0], "auth") && p[1] && !p[2])
8549 {
8550 VERIFY_PERMISSION(OPT_P_GENERAL);
8551 options->authname = p[1];
8552 }
8553 else if (streq(p[0], "cipher") && p[1] && !p[2])
8554 {
8555 VERIFY_PERMISSION(OPT_P_NCP|OPT_P_INSTANCE);
8556 options->ciphername = p[1];
8557 }
8558 else if (streq(p[0], "data-ciphers-fallback") && p[1] && !p[2])
8559 {
8560 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INSTANCE);
8561 options->ciphername = p[1];
8562 options->enable_ncp_fallback = true;
8563 }
8564 else if ((streq(p[0], "data-ciphers") || streq(p[0], "ncp-ciphers"))
8565 && p[1] && !p[2])
8566 {
8567 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INSTANCE);
8568 if (streq(p[0], "ncp-ciphers"))
8569 {
8570 msg(M_INFO, "Note: Treating option '--ncp-ciphers' as "
8571 " '--data-ciphers' (renamed in OpenVPN 2.5).");
8572 }
8573 options->ncp_ciphers = p[1];
8574 }
8575 else if (streq(p[0], "key-derivation") && p[1])
8576 {
8577 /* NCP only option that is pushed by the server to enable EKM,
8578 * should not be used by normal users in config files*/
8579 VERIFY_PERMISSION(OPT_P_NCP)
8580 #ifdef HAVE_EXPORT_KEYING_MATERIAL
8581 if (streq(p[1], "tls-ekm"))
8582 {
8583 options->imported_protocol_flags |= CO_USE_TLS_KEY_MATERIAL_EXPORT;
8584 }
8585 else
8586 #endif
8587 {
8588 msg(msglevel, "Unknown key-derivation method %s", p[1]);
8589 }
8590 }
8591 else if (streq(p[0], "protocol-flags") && p[1])
8592 {
8593 /* NCP only option that is pushed by the server to enable protocol
8594 * features that are negotiated, should not be used by normal users
8595 * in config files */
8596 VERIFY_PERMISSION(OPT_P_NCP)
8597 for (size_t j = 1; j < MAX_PARMS && p[j] != NULL; j++)
8598 {
8599 if (streq(p[j], "cc-exit"))
8600 {
8601 options->imported_protocol_flags |= CO_USE_CC_EXIT_NOTIFY;
8602 }
8603 #ifdef HAVE_EXPORT_KEYING_MATERIAL
8604 else if (streq(p[j], "tls-ekm"))
8605 {
8606 options->imported_protocol_flags |= CO_USE_TLS_KEY_MATERIAL_EXPORT;
8607 }
8608 #endif
8609 else
8610 {
8611 msg(msglevel, "Unknown protocol-flags flag: %s", p[j]);
8612 }
8613 }
8614 }
8615 else if (streq(p[0], "prng") && p[1] && !p[3])
8616 {
8617 msg(M_WARN, "NOTICE: --prng option ignored (SSL library PRNG is used)");
8618 }
8619 else if (streq(p[0], "no-replay") && !p[1])
8620 {
8621 VERIFY_PERMISSION(OPT_P_GENERAL);
8622 options->replay = false;
8623 }
8624 else if (streq(p[0], "replay-window") && !p[3])
8625 {
8626 VERIFY_PERMISSION(OPT_P_GENERAL);
8627 if (p[1])
8628 {
8629 int replay_window;
8630
8631 replay_window = atoi(p[1]);
8632 if (!(MIN_SEQ_BACKTRACK <= replay_window && replay_window <= MAX_SEQ_BACKTRACK))
8633 {
8634 msg(msglevel, "replay-window window size parameter (%d) must be between %d and %d",
8635 replay_window,
8636 MIN_SEQ_BACKTRACK,
8637 MAX_SEQ_BACKTRACK);
8638 goto err;
8639 }
8640 options->replay_window = replay_window;
8641
8642 if (p[2])
8643 {
8644 int replay_time;
8645
8646 replay_time = atoi(p[2]);
8647 if (!(MIN_TIME_BACKTRACK <= replay_time && replay_time <= MAX_TIME_BACKTRACK))
8648 {
8649 msg(msglevel, "replay-window time window parameter (%d) must be between %d and %d",
8650 replay_time,
8651 MIN_TIME_BACKTRACK,
8652 MAX_TIME_BACKTRACK);
8653 goto err;
8654 }
8655 options->replay_time = replay_time;
8656 }
8657 }
8658 else
8659 {
8660 msg(msglevel, "replay-window option is missing window size parameter");
8661 goto err;
8662 }
8663 }
8664 else if (streq(p[0], "mute-replay-warnings") && !p[1])
8665 {
8666 VERIFY_PERMISSION(OPT_P_GENERAL);
8667 options->mute_replay_warnings = true;
8668 }
8669 else if (streq(p[0], "replay-persist") && p[1] && !p[2])
8670 {
8671 VERIFY_PERMISSION(OPT_P_GENERAL);
8672 options->packet_id_file = p[1];
8673 }
8674 else if (streq(p[0], "test-crypto") && !p[1])
8675 {
8676 VERIFY_PERMISSION(OPT_P_GENERAL);
8677 options->test_crypto = true;
8678 }
8679 #ifndef ENABLE_CRYPTO_MBEDTLS
8680 else if (streq(p[0], "engine") && !p[2])
8681 {
8682 VERIFY_PERMISSION(OPT_P_GENERAL);
8683 if (p[1])
8684 {
8685 options->engine = p[1];
8686 }
8687 else
8688 {
8689 options->engine = "auto";
8690 }
8691 }
8692 #endif /* ENABLE_CRYPTO_MBEDTLS */
8693 else if (streq(p[0], "providers") && p[1])
8694 {
8695 for (size_t j = 1; j < MAX_PARMS && p[j] != NULL; j++)
8696 {
8697 options->providers.names[j] = p[j];
8698 }
8699 }
8700 #ifdef ENABLE_PREDICTION_RESISTANCE
8701 else if (streq(p[0], "use-prediction-resistance") && !p[1])
8702 {
8703 VERIFY_PERMISSION(OPT_P_GENERAL);
8704 options->use_prediction_resistance = true;
8705 }
8706 #endif
8707 else if (streq(p[0], "show-tls") && !p[1])
8708 {
8709 VERIFY_PERMISSION(OPT_P_GENERAL);
8710 options->show_tls_ciphers = true;
8711 }
8712 else if ((streq(p[0], "show-curves") || streq(p[0], "show-groups")) && !p[1])
8713 {
8714 VERIFY_PERMISSION(OPT_P_GENERAL);
8715 options->show_curves = true;
8716 }
8717 else if (streq(p[0], "ecdh-curve") && p[1] && !p[2])
8718 {
8719 VERIFY_PERMISSION(OPT_P_GENERAL);
8720 msg(M_WARN, "Consider setting groups/curves preference with "
8721 "tls-groups instead of forcing a specific curve with "
8722 "ecdh-curve.");
8723 options->ecdh_curve = p[1];
8724 }
8725 else if (streq(p[0], "tls-server") && !p[1])
8726 {
8727 VERIFY_PERMISSION(OPT_P_GENERAL);
8728 options->tls_server = true;
8729 }
8730 else if (streq(p[0], "tls-client") && !p[1])
8731 {
8732 VERIFY_PERMISSION(OPT_P_GENERAL);
8733 options->tls_client = true;
8734 }
8735 else if (streq(p[0], "ca") && p[1] && !p[2])
8736 {
8737 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8738 options->ca_file = p[1];
8739 options->ca_file_inline = is_inline;
8740 }
8741 #ifndef ENABLE_CRYPTO_MBEDTLS
8742 else if (streq(p[0], "capath") && p[1] && !p[2])
8743 {
8744 VERIFY_PERMISSION(OPT_P_GENERAL);
8745 options->ca_path = p[1];
8746 }
8747 #endif /* ENABLE_CRYPTO_MBEDTLS */
8748 else if (streq(p[0], "dh") && p[1] && !p[2])
8749 {
8750 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8751 options->dh_file = p[1];
8752 options->dh_file_inline = is_inline;
8753 }
8754 else if (streq(p[0], "cert") && p[1] && !p[2])
8755 {
8756 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8757 options->cert_file = p[1];
8758 options->cert_file_inline = is_inline;
8759 }
8760 else if (streq(p[0], "extra-certs") && p[1] && !p[2])
8761 {
8762 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8763 options->extra_certs_file = p[1];
8764 options->extra_certs_file_inline = is_inline;
8765 }
8766 else if ((streq(p[0], "verify-hash") && p[1] && !p[3])
8767 || (streq(p[0], "peer-fingerprint") && p[1] && !p[2]))
8768 {
8769 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8770
8771 int verify_hash_depth = 0;
8772 if (streq(p[0], "verify-hash"))
8773 {
8774 msg(M_WARN, "DEPRECATED OPTION: The option --verify-hash is deprecated. "
8775 "You should switch to the either use the level 1 certificate as "
8776 "--ca option, use --tls-verify or use --peer-fingerprint");
8777 /* verify level 1 cert, i.e. the CA that signed the leaf cert */
8778 verify_hash_depth = 1;
8779 }
8780
8781 options->verify_hash_algo = MD_SHA256;
8782
8783 int digest_len = SHA256_DIGEST_LENGTH;
8784
8785 if (options->verify_hash && options->verify_hash_depth != verify_hash_depth)
8786 {
8787 msg(msglevel, "ERROR: Setting %s not allowed. --verify-hash and"
8788 " --peer-fingerprint are mutually exclusive", p[0]);
8789 goto err;
8790 }
8791
8792 if (streq(p[0], "verify-hash"))
8793 {
8794 if ((!p[2] && !is_inline) || (p[2] && streq(p[2], "SHA1")))
8795 {
8796 options->verify_hash_algo = MD_SHA1;
8797 digest_len = SHA_DIGEST_LENGTH;
8798 }
8799 else if (p[2] && !streq(p[2], "SHA256"))
8800 {
8801 msg(msglevel, "invalid or unsupported hashing algorithm: %s "
8802 "(only SHA1 and SHA256 are supported)", p[2]);
8803 goto err;
8804 }
8805 }
8806
8807 struct verify_hash_list *newlist;
8808 newlist = parse_hash_fingerprint_multiline(p[1], digest_len,
8809 msglevel, &options->gc);
8810
8811 /* Append the new list to the end of our current list */
8812 if (!options->verify_hash)
8813 {
8814 options->verify_hash = newlist;
8815 options->verify_hash_depth = verify_hash_depth;
8816 }
8817 else
8818 {
8819 /* since both the old and new list can have multiple entries
8820 * we need to go to the end of one of them to concatenate them */
8821 struct verify_hash_list *listend = options->verify_hash;
8822 while (listend->next)
8823 {
8824 listend = listend->next;
8825 }
8826 listend->next = newlist;
8827 }
8828 }
8829 #ifdef ENABLE_CRYPTOAPI
8830 else if (streq(p[0], "cryptoapicert") && p[1] && !p[2])
8831 {
8832 VERIFY_PERMISSION(OPT_P_GENERAL);
8833 options->cryptoapi_cert = p[1];
8834 }
8835 #endif
8836 else if (streq(p[0], "key") && p[1] && !p[2])
8837 {
8838 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8839 options->priv_key_file = p[1];
8840 options->priv_key_file_inline = is_inline;
8841 }
8842 else if (streq(p[0], "tls-version-min") && p[1] && !p[3])
8843 {
8844 int ver;
8845 VERIFY_PERMISSION(OPT_P_GENERAL);
8846 ver = tls_version_parse(p[1], p[2]);
8847 if (ver == TLS_VER_BAD)
8848 {
8849 msg(msglevel, "unknown tls-version-min parameter: %s", p[1]);
8850 goto err;
8851 }
8852 options->ssl_flags &=
8853 ~(SSLF_TLS_VERSION_MIN_MASK << SSLF_TLS_VERSION_MIN_SHIFT);
8854 options->ssl_flags |= (ver << SSLF_TLS_VERSION_MIN_SHIFT);
8855 }
8856 else if (streq(p[0], "tls-version-max") && p[1] && !p[2])
8857 {
8858 int ver;
8859 VERIFY_PERMISSION(OPT_P_GENERAL);
8860 ver = tls_version_parse(p[1], NULL);
8861 if (ver == TLS_VER_BAD)
8862 {
8863 msg(msglevel, "unknown tls-version-max parameter: %s", p[1]);
8864 goto err;
8865 }
8866 options->ssl_flags &=
8867 ~(SSLF_TLS_VERSION_MAX_MASK << SSLF_TLS_VERSION_MAX_SHIFT);
8868 options->ssl_flags |= (ver << SSLF_TLS_VERSION_MAX_SHIFT);
8869 }
8870 #ifndef ENABLE_CRYPTO_MBEDTLS
8871 else if (streq(p[0], "pkcs12") && p[1] && !p[2])
8872 {
8873 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8874 options->pkcs12_file = p[1];
8875 options->pkcs12_file_inline = is_inline;
8876 }
8877 #endif /* ENABLE_CRYPTO_MBEDTLS */
8878 else if (streq(p[0], "askpass") && !p[2])
8879 {
8880 VERIFY_PERMISSION(OPT_P_GENERAL);
8881 if (p[1])
8882 {
8883 options->key_pass_file = p[1];
8884 }
8885 else
8886 {
8887 options->key_pass_file = "stdin";
8888 }
8889 }
8890 else if (streq(p[0], "auth-nocache") && !p[1])
8891 {
8892 VERIFY_PERMISSION(OPT_P_GENERAL);
8893 ssl_set_auth_nocache();
8894 }
8895 else if (streq(p[0], "auth-token") && p[1] && !p[2])
8896 {
8897 VERIFY_PERMISSION(OPT_P_ECHO);
8898 ssl_set_auth_token(p[1]);
8899 #ifdef ENABLE_MANAGEMENT
8900 if (management)
8901 {
8902 management_auth_token(management, p[1]);
8903 }
8904 #endif
8905 }
8906 else if (streq(p[0], "auth-token-user") && p[1] && !p[2])
8907 {
8908 VERIFY_PERMISSION(OPT_P_ECHO);
8909 ssl_set_auth_token_user(p[1]);
8910 }
8911 else if (streq(p[0], "single-session") && !p[1])
8912 {
8913 VERIFY_PERMISSION(OPT_P_GENERAL);
8914 options->single_session = true;
8915 }
8916 else if (streq(p[0], "push-peer-info") && !p[1])
8917 {
8918 VERIFY_PERMISSION(OPT_P_GENERAL);
8919 options->push_peer_info = true;
8920 }
8921 else if (streq(p[0], "tls-exit") && !p[1])
8922 {
8923 VERIFY_PERMISSION(OPT_P_GENERAL);
8924 options->tls_exit = true;
8925 }
8926 else if (streq(p[0], "tls-cipher") && p[1] && !p[2])
8927 {
8928 VERIFY_PERMISSION(OPT_P_GENERAL);
8929 options->cipher_list = p[1];
8930 }
8931 else if (streq(p[0], "tls-cert-profile") && p[1] && !p[2])
8932 {
8933 VERIFY_PERMISSION(OPT_P_GENERAL);
8934 options->tls_cert_profile = p[1];
8935 }
8936 else if (streq(p[0], "tls-ciphersuites") && p[1] && !p[2])
8937 {
8938 VERIFY_PERMISSION(OPT_P_GENERAL);
8939 options->cipher_list_tls13 = p[1];
8940 }
8941 else if (streq(p[0], "tls-groups") && p[1] && !p[2])
8942 {
8943 VERIFY_PERMISSION(OPT_P_GENERAL);
8944 options->tls_groups = p[1];
8945 }
8946 else if (streq(p[0], "crl-verify") && p[1] && ((p[2] && streq(p[2], "dir"))
8947 || !p[2]))
8948 {
8949 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE);
8950 if (p[2] && streq(p[2], "dir"))
8951 {
8952 options->ssl_flags |= SSLF_CRL_VERIFY_DIR;
8953 }
8954 options->crl_file = p[1];
8955 options->crl_file_inline = is_inline;
8956 }
8957 else if (streq(p[0], "tls-verify") && p[1])
8958 {
8959 VERIFY_PERMISSION(OPT_P_SCRIPT);
8960 if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
8961 {
8962 goto err;
8963 }
8964 set_user_script(options, &options->tls_verify,
8965 string_substitute(p[1], ',', ' ', &options->gc),
8966 "tls-verify", true);
8967 }
8968 #ifndef ENABLE_CRYPTO_MBEDTLS
8969 else if (streq(p[0], "tls-export-cert") && p[1] && !p[2])
8970 {
8971 VERIFY_PERMISSION(OPT_P_GENERAL);
8972 options->tls_export_cert = p[1];
8973 }
8974 #endif
8975 else if (streq(p[0], "compat-names"))
8976 {
8977 VERIFY_PERMISSION(OPT_P_GENERAL);
8978 msg(msglevel, "--compat-names was removed in OpenVPN 2.5. "
8979 "Update your configuration.");
8980 goto err;
8981 }
8982 else if (streq(p[0], "no-name-remapping") && !p[1])
8983 {
8984 VERIFY_PERMISSION(OPT_P_GENERAL);
8985 msg(msglevel, "--no-name-remapping was removed in OpenVPN 2.5. "
8986 "Update your configuration.");
8987 goto err;
8988 }
8989 else if (streq(p[0], "verify-x509-name") && p[1] && strlen(p[1]) && !p[3])
8990 {
8991 int type = VERIFY_X509_SUBJECT_DN;
8992 VERIFY_PERMISSION(OPT_P_GENERAL);
8993 if (p[2])
8994 {
8995 if (streq(p[2], "subject"))
8996 {
8997 type = VERIFY_X509_SUBJECT_DN;
8998 }
8999 else if (streq(p[2], "name"))
9000 {
9001 type = VERIFY_X509_SUBJECT_RDN;
9002 }
9003 else if (streq(p[2], "name-prefix"))
9004 {
9005 type = VERIFY_X509_SUBJECT_RDN_PREFIX;
9006 }
9007 else
9008 {
9009 msg(msglevel, "unknown X.509 name type: %s", p[2]);
9010 goto err;
9011 }
9012 }
9013 options->verify_x509_type = type;
9014 options->verify_x509_name = p[1];
9015 }
9016 else if (streq(p[0], "ns-cert-type") && p[1] && !p[2])
9017 {
9018 VERIFY_PERMISSION(OPT_P_GENERAL);
9019 if (streq(p[1], "server"))
9020 {
9021 options->ns_cert_type = NS_CERT_CHECK_SERVER;
9022 }
9023 else if (streq(p[1], "client"))
9024 {
9025 options->ns_cert_type = NS_CERT_CHECK_CLIENT;
9026 }
9027 else
9028 {
9029 msg(msglevel, "--ns-cert-type must be 'client' or 'server'");
9030 goto err;
9031 }
9032 }
9033 else if (streq(p[0], "remote-cert-ku"))
9034 {
9035 VERIFY_PERMISSION(OPT_P_GENERAL);
9036
9037 size_t j;
9038 for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
9039 {
9040 sscanf(p[j], "%x", &(options->remote_cert_ku[j-1]));
9041 }
9042 if (j == 1)
9043 {
9044 /* No specific KU required, but require KU to be present */
9045 options->remote_cert_ku[0] = OPENVPN_KU_REQUIRED;
9046 }
9047 }
9048 else if (streq(p[0], "remote-cert-eku") && p[1] && !p[2])
9049 {
9050 VERIFY_PERMISSION(OPT_P_GENERAL);
9051 options->remote_cert_eku = p[1];
9052 }
9053 else if (streq(p[0], "remote-cert-tls") && p[1] && !p[2])
9054 {
9055 VERIFY_PERMISSION(OPT_P_GENERAL);
9056
9057 if (streq(p[1], "server"))
9058 {
9059 options->remote_cert_ku[0] = OPENVPN_KU_REQUIRED;
9060 options->remote_cert_eku = "TLS Web Server Authentication";
9061 }
9062 else if (streq(p[1], "client"))
9063 {
9064 options->remote_cert_ku[0] = OPENVPN_KU_REQUIRED;
9065 options->remote_cert_eku = "TLS Web Client Authentication";
9066 }
9067 else
9068 {
9069 msg(msglevel, "--remote-cert-tls must be 'client' or 'server'");
9070 goto err;
9071 }
9072 }
9073 else if (streq(p[0], "tls-timeout") && p[1] && !p[2])
9074 {
9075 VERIFY_PERMISSION(OPT_P_TLS_PARMS);
9076 options->tls_timeout = positive_atoi(p[1]);
9077 }
9078 else if (streq(p[0], "reneg-bytes") && p[1] && !p[2])
9079 {
9080 VERIFY_PERMISSION(OPT_P_TLS_PARMS);
9081 options->renegotiate_bytes = positive_atoi(p[1]);
9082 }
9083 else if (streq(p[0], "reneg-pkts") && p[1] && !p[2])
9084 {
9085 VERIFY_PERMISSION(OPT_P_TLS_PARMS);
9086 options->renegotiate_packets = positive_atoi(p[1]);
9087 }
9088 else if (streq(p[0], "reneg-sec") && p[1] && !p[3])
9089 {
9090 VERIFY_PERMISSION(OPT_P_TLS_PARMS);
9091 options->renegotiate_seconds = positive_atoi(p[1]);
9092 if (p[2])
9093 {
9094 options->renegotiate_seconds_min = positive_atoi(p[2]);
9095 }
9096 }
9097 else if (streq(p[0], "hand-window") && p[1] && !p[2])
9098 {
9099 VERIFY_PERMISSION(OPT_P_TLS_PARMS);
9100 options->handshake_window = positive_atoi(p[1]);
9101 }
9102 else if (streq(p[0], "tran-window") && p[1] && !p[2])
9103 {
9104 VERIFY_PERMISSION(OPT_P_TLS_PARMS);
9105 options->transition_window = positive_atoi(p[1]);
9106 }
9107 else if (streq(p[0], "tls-auth") && p[1] && !p[3])
9108 {
9109 int key_direction = -1;
9110
9111 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION|OPT_P_INLINE);
9112
9113 if (permission_mask & OPT_P_GENERAL)
9114 {
9115 options->tls_auth_file = p[1];
9116 options->tls_auth_file_inline = is_inline;
9117
9118 if (!is_inline && p[2])
9119 {
9120 key_direction = ascii2keydirection(msglevel, p[2]);
9121 if (key_direction < 0)
9122 {
9123 goto err;
9124 }
9125 options->key_direction = key_direction;
9126 }
9127
9128 }
9129 else if (permission_mask & OPT_P_CONNECTION)
9130 {
9131 options->ce.tls_auth_file = p[1];
9132 options->ce.tls_auth_file_inline = is_inline;
9133 options->ce.key_direction = KEY_DIRECTION_BIDIRECTIONAL;
9134
9135 if (!is_inline && p[2])
9136 {
9137 key_direction = ascii2keydirection(msglevel, p[2]);
9138 if (key_direction < 0)
9139 {
9140 goto err;
9141 }
9142 options->ce.key_direction = key_direction;
9143 }
9144 }
9145 }
9146 else if (streq(p[0], "tls-crypt") && p[1] && !p[3])
9147 {
9148 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION|OPT_P_INLINE);
9149 if (permission_mask & OPT_P_GENERAL)
9150 {
9151 options->tls_crypt_file = p[1];
9152 options->tls_crypt_file_inline = is_inline;
9153 }
9154 else if (permission_mask & OPT_P_CONNECTION)
9155 {
9156 options->ce.tls_crypt_file = p[1];
9157 options->ce.tls_crypt_file_inline = is_inline;
9158 }
9159 }
9160 else if (streq(p[0], "tls-crypt-v2") && p[1] && !p[3])
9161 {
9162 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION|OPT_P_INLINE);
9163 if (permission_mask & OPT_P_GENERAL)
9164 {
9165 options->tls_crypt_v2_file = p[1];
9166 options->tls_crypt_v2_file_inline = is_inline;
9167 }
9168 else if (permission_mask & OPT_P_CONNECTION)
9169 {
9170 options->ce.tls_crypt_v2_file = p[1];
9171 options->ce.tls_crypt_v2_file_inline = is_inline;
9172 }
9173
9174 if (p[2] && streq(p[2], "force-cookie"))
9175 {
9176 options->ce.tls_crypt_v2_force_cookie = true;
9177 }
9178 else if (p[2] && streq(p[2], "allow-noncookie"))
9179 {
9180 options->ce.tls_crypt_v2_force_cookie = false;
9181 }
9182 else if (p[2])
9183 {
9184 msg(msglevel, "Unsupported tls-crypt-v2 argument: %s", p[2]);
9185 }
9186 }
9187 else if (streq(p[0], "tls-crypt-v2-verify") && p[1] && !p[2])
9188 {
9189 VERIFY_PERMISSION(OPT_P_GENERAL);
9190 options->tls_crypt_v2_verify_script = p[1];
9191 }
9192 else if (streq(p[0], "x509-track") && p[1] && !p[2])
9193 {
9194 VERIFY_PERMISSION(OPT_P_GENERAL);
9195 x509_track_add(&options->x509_track, p[1], msglevel, &options->gc);
9196 }
9197 #ifdef ENABLE_X509ALTUSERNAME
9198 else if (streq(p[0], "x509-username-field") && p[1])
9199 {
9200 /* This option used to automatically upcase the fieldnames passed as the
9201 * option arguments, e.g., "ou" became "OU". Now, this "helpfulness" is
9202 * fine-tuned by only upcasing Subject field attribute names which consist
9203 * of all lower-case characters. Mixed-case attributes such as
9204 * "emailAddress" are left as-is. An option parameter having the "ext:"
9205 * prefix for matching X.509v3 extended fields will also remain unchanged.
9206 */
9207 VERIFY_PERMISSION(OPT_P_GENERAL);
9208 for (size_t j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
9209 {
9210 char *s = p[j];
9211
9212 if (strncmp("ext:", s, 4) != 0)
9213 {
9214 size_t i = 0;
9215 while (s[i] && !isupper(s[i]))
9216 {
9217 i++;
9218 }
9219 if (strlen(s) == i)
9220 {
9221 while ((*s = toupper(*s)) != '\0')
9222 {
9223 s++;
9224 }
9225 msg(M_WARN, "DEPRECATED FEATURE: automatically upcased the "
9226 "--x509-username-field parameter to '%s'; please update your"
9227 "configuration", p[j]);
9228 }
9229 }
9230 else if (!x509_username_field_ext_supported(s+4))
9231 {
9232 msg(msglevel, "Unsupported x509-username-field extension: %s", s);
9233 }
9234 options->x509_username_field[j-1] = p[j];
9235 }
9236 }
9237 #endif /* ENABLE_X509ALTUSERNAME */
9238 #ifdef ENABLE_PKCS11
9239 else if (streq(p[0], "show-pkcs11-ids") && !p[3])
9240 {
9241 char *provider = p[1];
9242 bool cert_private = (p[2] == NULL ? false : ( atoi(p[2]) != 0 ));
9243
9244 #ifdef DEFAULT_PKCS11_MODULE
9245 if (!provider)
9246 {
9247 provider = DEFAULT_PKCS11_MODULE;
9248 }
9249 else if (!p[2])
9250 {
9251 char *endp = NULL;
9252 int i = strtol(provider, &endp, 10);
9253
9254 if (*endp == 0)
9255 {
9256 /* There was one argument, and it was purely numeric.
9257 * Interpret it as the cert_private argument */
9258 provider = DEFAULT_PKCS11_MODULE;
9259 cert_private = i;
9260 }
9261 }
9262 #else /* ifdef DEFAULT_PKCS11_MODULE */
9263 if (!provider)
9264 {
9265 msg(msglevel, "--show-pkcs11-ids requires a provider parameter");
9266 goto err;
9267 }
9268 #endif /* ifdef DEFAULT_PKCS11_MODULE */
9269 VERIFY_PERMISSION(OPT_P_GENERAL);
9270
9271 set_debug_level(options->verbosity, SDL_CONSTRAIN);
9272 show_pkcs11_ids(provider, cert_private);
9273 openvpn_exit(OPENVPN_EXIT_STATUS_GOOD); /* exit point */
9274 }
9275 else if (streq(p[0], "pkcs11-providers") && p[1])
9276 {
9277 int j;
9278
9279 VERIFY_PERMISSION(OPT_P_GENERAL);
9280
9281 for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
9282 {
9283 options->pkcs11_providers[j-1] = p[j];
9284 }
9285 }
9286 else if (streq(p[0], "pkcs11-protected-authentication"))
9287 {
9288 int j;
9289
9290 VERIFY_PERMISSION(OPT_P_GENERAL);
9291
9292 for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
9293 {
9294 options->pkcs11_protected_authentication[j-1] = atoi(p[j]) != 0 ? 1 : 0;
9295 }
9296 }
9297 else if (streq(p[0], "pkcs11-private-mode") && p[1])
9298 {
9299 int j;
9300
9301 VERIFY_PERMISSION(OPT_P_GENERAL);
9302
9303 for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
9304 {
9305 sscanf(p[j], "%x", &(options->pkcs11_private_mode[j-1]));
9306 }
9307 }
9308 else if (streq(p[0], "pkcs11-cert-private"))
9309 {
9310 int j;
9311
9312 VERIFY_PERMISSION(OPT_P_GENERAL);
9313
9314 for (j = 1; j < MAX_PARMS && p[j] != NULL; ++j)
9315 {
9316 options->pkcs11_cert_private[j-1] = atoi(p[j]) != 0 ? 1 : 0;
9317 }
9318 }
9319 else if (streq(p[0], "pkcs11-pin-cache") && p[1] && !p[2])
9320 {
9321 VERIFY_PERMISSION(OPT_P_GENERAL);
9322 options->pkcs11_pin_cache_period = atoi(p[1]);
9323 }
9324 else if (streq(p[0], "pkcs11-id") && p[1] && !p[2])
9325 {
9326 VERIFY_PERMISSION(OPT_P_GENERAL);
9327 options->pkcs11_id = p[1];
9328 }
9329 else if (streq(p[0], "pkcs11-id-management") && !p[1])
9330 {
9331 VERIFY_PERMISSION(OPT_P_GENERAL);
9332 options->pkcs11_id_management = true;
9333 }
9334 #endif /* ifdef ENABLE_PKCS11 */
9335 else if (streq(p[0], "rmtun") && !p[1])
9336 {
9337 VERIFY_PERMISSION(OPT_P_GENERAL);
9338 options->persist_config = true;
9339 options->persist_mode = 0;
9340 }
9341 else if (streq(p[0], "mktun") && !p[1])
9342 {
9343 VERIFY_PERMISSION(OPT_P_GENERAL);
9344 options->persist_config = true;
9345 options->persist_mode = 1;
9346 }
9347 else if (streq(p[0], "peer-id") && p[1] && !p[2])
9348 {
9349 VERIFY_PERMISSION(OPT_P_PEER_ID);
9350 options->use_peer_id = true;
9351 options->peer_id = atoi(p[1]);
9352 }
9353 #ifdef HAVE_EXPORT_KEYING_MATERIAL
9354 else if (streq(p[0], "keying-material-exporter") && p[1] && p[2])
9355 {
9356 int ekm_length = positive_atoi(p[2]);
9357
9358 VERIFY_PERMISSION(OPT_P_GENERAL);
9359
9360 if (strncmp(p[1], "EXPORTER", 8))
9361 {
9362 msg(msglevel, "Keying material exporter label must begin with "
9363 "\"EXPORTER\"");
9364 goto err;
9365 }
9366 if (streq(p[1], EXPORT_KEY_DATA_LABEL))
9367 {
9368 msg(msglevel, "Keying material exporter label must not be '"
9369 EXPORT_KEY_DATA_LABEL "'.");
9370 }
9371 if (ekm_length < 16 || ekm_length > 4095)
9372 {
9373 msg(msglevel, "Invalid keying material exporter length");
9374 goto err;
9375 }
9376
9377 options->keying_material_exporter_label = p[1];
9378 options->keying_material_exporter_length = ekm_length;
9379 }
9380 #endif /* HAVE_EXPORT_KEYING_MATERIAL */
9381 else if (streq(p[0], "allow-recursive-routing") && !p[1])
9382 {
9383 VERIFY_PERMISSION(OPT_P_GENERAL);
9384 options->allow_recursive_routing = true;
9385 }
9386 else if (streq(p[0], "vlan-tagging") && !p[1])
9387 {
9388 VERIFY_PERMISSION(OPT_P_GENERAL);
9389 options->vlan_tagging = true;
9390 }
9391 else if (streq(p[0], "vlan-accept") && p[1] && !p[2])
9392 {
9393 VERIFY_PERMISSION(OPT_P_GENERAL);
9394 if (streq(p[1], "tagged"))
9395 {
9396 options->vlan_accept = VLAN_ONLY_TAGGED;
9397 }
9398 else if (streq(p[1], "untagged"))
9399 {
9400 options->vlan_accept = VLAN_ONLY_UNTAGGED_OR_PRIORITY;
9401 }
9402 else if (streq(p[1], "all"))
9403 {
9404 options->vlan_accept = VLAN_ALL;
9405 }
9406 else
9407 {
9408 msg(msglevel, "--vlan-accept must be 'tagged', 'untagged' or 'all'");
9409 goto err;
9410 }
9411 }
9412 else if (streq(p[0], "vlan-pvid") && p[1] && !p[2])
9413 {
9414 VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INSTANCE);
9415 options->vlan_pvid = positive_atoi(p[1]);
9416 if (options->vlan_pvid < OPENVPN_8021Q_MIN_VID
9417 || options->vlan_pvid > OPENVPN_8021Q_MAX_VID)
9418 {
9419 msg(msglevel,
9420 "the parameter of --vlan-pvid parameters must be >= %u and <= %u",
9421 OPENVPN_8021Q_MIN_VID, OPENVPN_8021Q_MAX_VID);
9422 goto err;
9423 }
9424 }
9425 else
9426 {
9427 int i;
9428 int msglevel = msglevel_fc;
9429 /* Check if an option is in --ignore-unknown-option and
9430 * set warning level to non fatal */
9431 for (i = 0; options->ignore_unknown_option && options->ignore_unknown_option[i]; i++)
9432 {
9433 if (streq(p[0], options->ignore_unknown_option[i]))
9434 {
9435 msglevel = M_WARN;
9436 break;
9437 }
9438 }
9439 if (file)
9440 {
9441 msg(msglevel, "Unrecognized option or missing or extra parameter(s) in %s:%d: %s (%s)", file, line, p[0], PACKAGE_VERSION);
9442 }
9443 else
9444 {
9445 msg(msglevel, "Unrecognized option or missing or extra parameter(s): --%s (%s)", p[0], PACKAGE_VERSION);
9446 }
9447 }
9448 err:
9449 gc_free(&gc);
9450 }