diff --git a/data/templates/openvpn/server.conf.j2 b/data/templates/openvpn/server.conf.j2 index 525605240..d144529f3 100644 --- a/data/templates/openvpn/server.conf.j2 +++ b/data/templates/openvpn/server.conf.j2 @@ -1,231 +1,230 @@ ### Autogenerated by interfaces-openvpn.py ### # # See https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage # for individual keyword definition # # {{ description if description is vyos_defined }} # verb 3 dev-type {{ device_type }} dev {{ ifname }} persist-key {% if protocol is vyos_defined('tcp-active') %} proto tcp-client {% elif protocol is vyos_defined('tcp-passive') %} proto tcp-server {% else %} proto udp {% endif %} {% if local_host is vyos_defined %} local {{ local_host }} {% endif %} {% if mode is vyos_defined('server') and protocol is vyos_defined('udp') and local_host is not vyos_defined %} multihome {% endif %} {% if local_port is vyos_defined %} lport {{ local_port }} {% endif %} {% if remote_port is vyos_defined %} rport {{ remote_port }} {% endif %} {% if remote_host is vyos_defined %} {% for remote in remote_host %} remote {{ remote }} {% endfor %} {% endif %} {% if shared_secret_key is vyos_defined %} secret /run/openvpn/{{ ifname }}_shared.key {% endif %} {% if persistent_tunnel is vyos_defined %} persist-tun {% endif %} {% if replace_default_route.local is vyos_defined %} push "redirect-gateway local def1" {% elif replace_default_route is vyos_defined %} push "redirect-gateway def1" {% endif %} {% if use_lzo_compression is vyos_defined %} compress lzo {% endif %} -{% if enable_dco is not vyos_defined %} +{% if offload.dco is not vyos_defined %} disable-dco {% endif %} - {% if mode is vyos_defined('client') %} # # OpenVPN Client mode # client nobind {% elif mode is vyos_defined('server') %} # # OpenVPN Server mode # mode server tls-server {% if server is vyos_defined %} {% if server.subnet is vyos_defined %} {% if server.topology is vyos_defined('point-to-point') %} topology p2p {% elif server.topology is vyos_defined %} topology {{ server.topology }} {% endif %} {% for subnet in server.subnet %} {% if subnet | is_ipv4 %} server {{ subnet | address_from_cidr }} {{ subnet | netmask_from_cidr }} nopool {# First ip address is used as gateway. It's allows to use metrics #} {% if server.push_route is vyos_defined %} {% for route, route_config in server.push_route.items() %} {% if route | is_ipv4 %} push "route {{ route | address_from_cidr }} {{ route | netmask_from_cidr }} {{ subnet | first_host_address ~ ' ' ~ route_config.metric if route_config.metric is vyos_defined }}" {% elif route | is_ipv6 %} push "route-ipv6 {{ route }}" {% endif %} {% endfor %} {% endif %} {# OpenVPN assigns the first IP address to its local interface so the pool used #} {# in net30 topology - where each client receives a /30 must start from the second subnet #} {% if server.topology is vyos_defined('net30') %} ifconfig-pool {{ subnet | inc_ip('4') }} {{ subnet | last_host_address | dec_ip('1') }} {{ subnet | netmask_from_cidr if device_type == 'tap' else '' }} {% else %} {# OpenVPN assigns the first IP address to its local interface so the pool must #} {# start from the second address and end on the last address #} ifconfig-pool {{ subnet | first_host_address | inc_ip('1') }} {{ subnet | last_host_address | dec_ip('1') }} {{ subnet | netmask_from_cidr if device_type == 'tun' else '' }} {% endif %} {% elif subnet | is_ipv6 %} server-ipv6 {{ subnet }} {% endif %} {% endfor %} {% endif %} {% if server.client_ip_pool is vyos_defined and server.client_ip_pool.disable is not vyos_defined %} ifconfig-pool {{ server.client_ip_pool.start }} {{ server.client_ip_pool.stop }} {{ server.client_ip_pool.subnet_mask if server.client_ip_pool.subnet_mask is vyos_defined }} {% endif %} {% if server.max_connections is vyos_defined %} max-clients {{ server.max_connections }} {% endif %} {% if server.client is vyos_defined %} client-config-dir /run/openvpn/ccd/{{ ifname }} {% endif %} {% endif %} keepalive {{ keep_alive.interval }} {{ keep_alive.interval | int * keep_alive.failure_count | int }} management /run/openvpn/openvpn-mgmt-intf unix {% if server is vyos_defined %} {% if server.reject_unconfigured_clients is vyos_defined %} ccd-exclusive {% endif %} {% if server.name_server is vyos_defined %} {% for nameserver in server.name_server %} {% if nameserver | is_ipv4 %} push "dhcp-option DNS {{ nameserver }}" {% elif nameserver | is_ipv6 %} push "dhcp-option DNS6 {{ nameserver }}" {% endif %} {% endfor %} {% endif %} {% if server.domain_name is vyos_defined %} push "dhcp-option DOMAIN {{ server.domain_name }}" {% endif %} {% if server.mfa.totp is vyos_defined %} {% set totp_config = server.mfa.totp %} plugin "{{ plugin_dir }}/openvpn-otp.so" "otp_secrets=/config/auth/openvpn/{{ ifname }}-otp-secrets otp_slop={{ totp_config.slop }} totp_t0={{ totp_config.drift }} totp_step={{ totp_config.step }} totp_digits={{ totp_config.digits }} password_is_cr={{ '1' if totp_config.challenge == 'enable' else '0' }}" {% endif %} {% endif %} {% else %} # # OpenVPN site-2-site mode # ping {{ keep_alive.interval }} ping-restart {{ keep_alive.failure_count }} {% if device_type == 'tap' %} {% if local_address is vyos_defined %} {% for laddr, laddr_conf in local_address.items() if laddr | is_ipv4 %} {% if laddr_conf.subnet_mask is vyos_defined %} ifconfig {{ laddr }} {{ laddr_conf.subnet_mask }} {% endif %} {% endfor %} {% endif %} {% else %} {% for laddr in local_address if laddr | is_ipv4 %} {% for raddr in remote_address if raddr | is_ipv4 %} ifconfig {{ laddr }} {{ raddr }} {% endfor %} {% endfor %} {% for laddr in local_address if laddr | is_ipv6 %} {% for raddr in remote_address if raddr | is_ipv6 %} ifconfig-ipv6 {{ laddr }} {{ raddr }} {% endfor %} {% endfor %} {% endif %} {% endif %} {% if tls is vyos_defined %} # TLS options {% if tls.ca_certificate is vyos_defined %} ca /run/openvpn/{{ ifname }}_ca.pem {% endif %} {% if tls.certificate is vyos_defined %} cert /run/openvpn/{{ ifname }}_cert.pem {% endif %} {% if tls.private_key is vyos_defined %} key /run/openvpn/{{ ifname }}_cert.key {% endif %} {% if tls.crypt_key is vyos_defined %} tls-crypt /run/openvpn/{{ ifname }}_crypt.key {% endif %} {% if tls.crl is vyos_defined %} crl-verify /run/openvpn/{{ ifname }}_crl.pem {% endif %} {% if tls.tls_version_min is vyos_defined %} tls-version-min {{ tls.tls_version_min }} {% endif %} {% if tls.dh_params is vyos_defined %} dh /run/openvpn/{{ ifname }}_dh.pem {% elif mode is vyos_defined('server') and tls.private_key is vyos_defined %} dh none {% endif %} {% if tls.auth_key is vyos_defined %} {% if mode == 'client' %} tls-auth /run/openvpn/{{ ifname }}_auth.key 1 {% elif mode == 'server' %} tls-auth /run/openvpn/{{ ifname }}_auth.key 0 {% endif %} {% endif %} {% if tls.role is vyos_defined('active') %} tls-client {% elif tls.role is vyos_defined('passive') %} tls-server {% endif %} {% endif %} # Encryption options {% if encryption is vyos_defined %} {% if encryption.cipher is vyos_defined %} cipher {{ encryption.cipher | openvpn_cipher }} {% if encryption.cipher is vyos_defined('bf128') %} keysize 128 {% elif encryption.cipher is vyos_defined('bf256') %} keysize 256 {% endif %} {% endif %} {% if encryption.ncp_ciphers is vyos_defined %} data-ciphers {{ encryption.ncp_ciphers | openvpn_ncp_ciphers }} {% endif %} {% endif %} # https://vyos.dev/T5027 # Required to support BF-CBC (default ciphername when none given) providers legacy default {% if hash is vyos_defined %} auth {{ hash }} {% endif %} {% if authentication is vyos_defined %} auth-user-pass {{ auth_user_pass_file }} auth-retry nointeract {% endif %} diff --git a/interface-definitions/interfaces-openvpn.xml.in b/interface-definitions/interfaces-openvpn.xml.in index ca6d80f8b..127a8179b 100644 --- a/interface-definitions/interfaces-openvpn.xml.in +++ b/interface-definitions/interfaces-openvpn.xml.in @@ -1,808 +1,815 @@ <?xml version="1.0"?> <interfaceDefinition> <node name="interfaces"> <children> <tagNode name="openvpn" owner="${vyos_conf_scripts_dir}/interfaces-openvpn.py"> <properties> <help>OpenVPN Tunnel Interface</help> <priority>460</priority> <constraint> <regex>vtun[0-9]+</regex> </constraint> <constraintErrorMessage>OpenVPN tunnel interface must be named vtunN</constraintErrorMessage> <valueHelp> <format>vtunN</format> <description>OpenVPN interface name</description> </valueHelp> </properties> <children> #include <include/interface/authentication.xml.i> #include <include/generic-description.xml.i> <leafNode name="device-type"> <properties> <help>OpenVPN interface device-type</help> <completionHelp> <list>tun tap</list> </completionHelp> <valueHelp> <format>tun</format> <description>TUN device, required for OSI layer 3</description> </valueHelp> <valueHelp> <format>tap</format> <description>TAP device, required for OSI layer 2</description> </valueHelp> <constraint> <regex>(tun|tap)</regex> </constraint> </properties> <defaultValue>tun</defaultValue> </leafNode> #include <include/interface/disable.xml.i> <node name="encryption"> <properties> <help>Data Encryption settings</help> </properties> <children> <leafNode name="cipher"> <properties> <help>Standard Data Encryption Algorithm</help> <completionHelp> <list>none des 3des bf128 bf256 aes128 aes128gcm aes192 aes192gcm aes256 aes256gcm</list> </completionHelp> <valueHelp> <format>none</format> <description>Disable encryption</description> </valueHelp> <valueHelp> <format>des</format> <description>DES algorithm</description> </valueHelp> <valueHelp> <format>3des</format> <description>DES algorithm with triple encryption</description> </valueHelp> <valueHelp> <format>bf128</format> <description>Blowfish algorithm with 128-bit key</description> </valueHelp> <valueHelp> <format>bf256</format> <description>Blowfish algorithm with 256-bit key</description> </valueHelp> <valueHelp> <format>aes128</format> <description>AES algorithm with 128-bit key CBC</description> </valueHelp> <valueHelp> <format>aes128gcm</format> <description>AES algorithm with 128-bit key GCM</description> </valueHelp> <valueHelp> <format>aes192</format> <description>AES algorithm with 192-bit key CBC</description> </valueHelp> <valueHelp> <format>aes192gcm</format> <description>AES algorithm with 192-bit key GCM</description> </valueHelp> <valueHelp> <format>aes256</format> <description>AES algorithm with 256-bit key CBC</description> </valueHelp> <valueHelp> <format>aes256gcm</format> <description>AES algorithm with 256-bit key GCM</description> </valueHelp> <constraint> <regex>(none|des|3des|bf128|bf256|aes128|aes128gcm|aes192|aes192gcm|aes256|aes256gcm)</regex> </constraint> </properties> </leafNode> <leafNode name="ncp-ciphers"> <properties> <help>Cipher negotiation list for use in server or client mode</help> <completionHelp> <list>none des 3des aes128 aes128gcm aes192 aes192gcm aes256 aes256gcm</list> </completionHelp> <valueHelp> <format>none</format> <description>Disable encryption</description> </valueHelp> <valueHelp> <format>des</format> <description>DES algorithm</description> </valueHelp> <valueHelp> <format>3des</format> <description>DES algorithm with triple encryption</description> </valueHelp> <valueHelp> <format>aes128</format> <description>AES algorithm with 128-bit key CBC</description> </valueHelp> <valueHelp> <format>aes128gcm</format> <description>AES algorithm with 128-bit key GCM</description> </valueHelp> <valueHelp> <format>aes192</format> <description>AES algorithm with 192-bit key CBC</description> </valueHelp> <valueHelp> <format>aes192gcm</format> <description>AES algorithm with 192-bit key GCM</description> </valueHelp> <valueHelp> <format>aes256</format> <description>AES algorithm with 256-bit key CBC</description> </valueHelp> <valueHelp> <format>aes256gcm</format> <description>AES algorithm with 256-bit key GCM</description> </valueHelp> <constraint> <regex>(none|des|3des|aes128|aes128gcm|aes192|aes192gcm|aes256|aes256gcm)</regex> </constraint> <multi/> </properties> </leafNode> </children> </node> #include <include/interface/ipv4-options.xml.i> #include <include/interface/ipv6-options.xml.i> #include <include/interface/mirror.xml.i> <leafNode name="hash"> <properties> <help>Hashing Algorithm</help> <completionHelp> <list>md5 sha1 sha256 sha384 sha512</list> </completionHelp> <valueHelp> <format>md5</format> <description>MD5 algorithm</description> </valueHelp> <valueHelp> <format>sha1</format> <description>SHA-1 algorithm</description> </valueHelp> <valueHelp> <format>sha256</format> <description>SHA-256 algorithm</description> </valueHelp> <valueHelp> <format>sha384</format> <description>SHA-384 algorithm</description> </valueHelp> <valueHelp> <format>sha512</format> <description>SHA-512 algorithm</description> </valueHelp> <constraint> <regex>(md5|sha1|sha256|sha384|sha512)</regex> </constraint> </properties> </leafNode> <node name="keep-alive"> <properties> <help>Keepalive helper options</help> </properties> <children> <leafNode name="failure-count"> <properties> <help>Maximum number of keepalive packet failures</help> <valueHelp> <format>u32:0-1000</format> <description>Maximum number of keepalive packet failures</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-1000"/> </constraint> </properties> <defaultValue>60</defaultValue> </leafNode> <leafNode name="interval"> <properties> <help>Keepalive packet interval in seconds</help> <valueHelp> <format>u32:0-600</format> <description>Keepalive packet interval (seconds)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-600"/> </constraint> </properties> <defaultValue>10</defaultValue> </leafNode> </children> </node> <tagNode name="local-address"> <properties> <help>Local IP address of tunnel (IPv4 or IPv6)</help> <constraint> <validator name="ip-address"/> </constraint> </properties> <children> <leafNode name="subnet-mask"> <properties> <help>Subnet-mask for local IP address of tunnel (IPv4 only)</help> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> </children> </tagNode> <leafNode name="local-host"> <properties> <help>Local IP address to accept connections (all if not set)</help> <valueHelp> <format>ipv4</format> <description>Local IPv4 address</description> </valueHelp> <valueHelp> <format>ipv6</format> <description>Local IPv6 address</description> </valueHelp> <constraint> <validator name="ip-address"/> </constraint> </properties> </leafNode> <leafNode name="local-port"> <properties> <help>Local port number to accept connections</help> <valueHelp> <format>u32:1-65535</format> <description>Numeric IP port</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> </leafNode> <leafNode name="mode"> <properties> <help>OpenVPN mode of operation</help> <completionHelp> <list>site-to-site client server</list> </completionHelp> <valueHelp> <format>site-to-site</format> <description>Site-to-site mode</description> </valueHelp> <valueHelp> <format>client</format> <description>Client in client-server mode</description> </valueHelp> <valueHelp> <format>server</format> <description>Server in client-server mode</description> </valueHelp> <constraint> <regex>(site-to-site|client|server)</regex> </constraint> </properties> </leafNode> + <node name="offload"> + <properties> + <help>Configurable offload options</help> + </properties> + <children> + <leafNode name="dco"> + <properties> + <help>Enable data channel offload on this interface</help> + <valueless/> + </properties> + </leafNode> + </children> + </node> <leafNode name="openvpn-option"> <properties> <help>Additional OpenVPN options. You must use the syntax of openvpn.conf in this text-field. Using this without proper knowledge may result in a crashed OpenVPN server. Check system log to look for errors.</help> <multi/> </properties> </leafNode> <leafNode name="persistent-tunnel"> <properties> <help>Do not close and reopen interface (TUN/TAP device) on client restarts</help> <valueless/> </properties> </leafNode> <leafNode name="protocol"> <properties> <help>OpenVPN communication protocol</help> <completionHelp> <list>udp tcp-passive tcp-active</list> </completionHelp> <valueHelp> <format>udp</format> <description>UDP</description> </valueHelp> <valueHelp> <format>tcp-passive</format> <description>TCP and accepts connections passively</description> </valueHelp> <valueHelp> <format>tcp-active</format> <description>TCP and initiates connections actively</description> </valueHelp> <constraint> <regex>(udp|tcp-passive|tcp-active)</regex> </constraint> </properties> <defaultValue>udp</defaultValue> </leafNode> <leafNode name="remote-address"> <properties> <help>IP address of remote end of tunnel</help> <valueHelp> <format>ipv4</format> <description>Remote end IPv4 address</description> </valueHelp> <valueHelp> <format>ipv6</format> <description>Remote end IPv6 address</description> </valueHelp> <constraint> <validator name="ip-address"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="remote-host"> <properties> <help>Remote host to connect to (dynamic if not set)</help> <valueHelp> <format>ipv4</format> <description>IPv4 address of remote host</description> </valueHelp> <valueHelp> <format>ipv6</format> <description>IPv6 address of remote host</description> </valueHelp> <valueHelp> <format>txt</format> <description>Hostname of remote host</description> </valueHelp> <multi/> </properties> </leafNode> <leafNode name="remote-port"> <properties> <help>Remote port number to connect to</help> <valueHelp> <format>u32:1-65535</format> <description>Numeric IP port</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> </leafNode> <node name="replace-default-route"> <properties> <help>OpenVPN tunnel to be used as the default route</help> </properties> <children> <leafNode name="local"> <properties> <help>Tunnel endpoints are on the same subnet</help> </properties> </leafNode> </children> </node> <node name="server"> <properties> <help>Server-mode options</help> </properties> <children> <tagNode name="client"> <properties> <help>Client-specific settings</help> <valueHelp> <format>name</format> <description>Client common-name in the certificate</description> </valueHelp> </properties> <children> #include <include/generic-disable-node.xml.i> <leafNode name="ip"> <properties> <help>IP address of the client</help> <valueHelp> <format>ipv4</format> <description>Client IPv4 address</description> </valueHelp> <valueHelp> <format>ipv6</format> <description>Client IPv6 address</description> </valueHelp> <constraint> <validator name="ip-address"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="push-route"> <properties> <help>Route to be pushed to the client</help> <valueHelp> <format>ipv4net</format> <description>IPv4 network and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 network and prefix length</description> </valueHelp> <constraint> <validator name="ip-prefix"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="subnet"> <properties> <help>Subnet belonging to the client (iroute)</help> <valueHelp> <format>ipv4net</format> <description>IPv4 network and prefix length belonging to the client</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 network and prefix length belonging to the client</description> </valueHelp> <constraint> <validator name="ip-prefix"/> </constraint> <multi/> </properties> </leafNode> </children> </tagNode> <node name="client-ip-pool"> <properties> <help>Pool of client IPv4 addresses</help> </properties> <children> #include <include/generic-disable-node.xml.i> <leafNode name="start"> <properties> <help>First IP address in the pool</help> <constraint> <validator name="ipv4-address"/> </constraint> <valueHelp> <format>ipv4</format> <description>IPv4 address</description> </valueHelp> </properties> </leafNode> <leafNode name="stop"> <properties> <help>Last IP address in the pool</help> <constraint> <validator name="ipv4-address"/> </constraint> <valueHelp> <format>ipv4</format> <description>IPv4 address</description> </valueHelp> </properties> </leafNode> <leafNode name="subnet-mask"> <properties> <help>Subnet mask pushed to dynamic clients. If not set the server subnet mask will be used. Only used with topology subnet or device type tap. Not used with bridged interfaces.</help> <constraint> <validator name="ipv4-address"/> </constraint> <valueHelp> <format>ipv4</format> <description>IPv4 subnet mask</description> </valueHelp> </properties> </leafNode> </children> </node> <node name="client-ipv6-pool"> <properties> <help>Pool of client IPv6 addresses</help> </properties> <children> <leafNode name="base"> <properties> <help>Client IPv6 pool base address with optional prefix length</help> <valueHelp> <format>ipv6net</format> <description>Client IPv6 pool base address with optional prefix length (defaults: base = server subnet + 0x1000, prefix length = server prefix length)</description> </valueHelp> <constraint> <validator name="ipv6"/> </constraint> </properties> </leafNode> #include <include/generic-disable-node.xml.i> </children> </node> <leafNode name="domain-name"> <properties> <help>DNS suffix to be pushed to all clients</help> <valueHelp> <format>txt</format> <description>Domain Name Server suffix</description> </valueHelp> </properties> </leafNode> <leafNode name="max-connections"> <properties> <help>Number of maximum client connections</help> <valueHelp> <format>u32:1-4096</format> <description>Number of concurrent clients</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-4096"/> </constraint> </properties> </leafNode> #include <include/name-server-ipv4-ipv6.xml.i> <tagNode name="push-route"> <properties> <help>Route to be pushed to all clients</help> <valueHelp> <format>ipv4net</format> <description>IPv4 network and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 network and prefix length</description> </valueHelp> <constraint> <validator name="ip-prefix"/> </constraint> </properties> <children> <leafNode name="metric"> <properties> <help>Set metric for this route</help> <valueHelp> <format>u32:0-4294967295</format> <description>Metric for this route</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-4294967295"/> </constraint> </properties> <defaultValue>0</defaultValue> </leafNode> </children> </tagNode> <leafNode name="reject-unconfigured-clients"> <properties> <help>Reject connections from clients that are not explicitly configured</help> <valueless/> </properties> </leafNode> <leafNode name="subnet"> <properties> <help>Server-mode subnet (from which client IPs are allocated)</help> <valueHelp> <format>ipv4net</format> <description>IPv4 network and prefix length</description> </valueHelp> <valueHelp> <format>ipv6net</format> <description>IPv6 network and prefix length</description> </valueHelp> <constraint> <validator name="ip-prefix"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="topology"> <properties> <help>Topology for clients</help> <completionHelp> <list>net30 point-to-point subnet</list> </completionHelp> <valueHelp> <format>net30</format> <description>net30 topology</description> </valueHelp> <valueHelp> <format>point-to-point</format> <description>Point-to-point topology</description> </valueHelp> <valueHelp> <format>subnet</format> <description>Subnet topology</description> </valueHelp> <constraint> <regex>(subnet|point-to-point|net30)</regex> </constraint> </properties> <defaultValue>net30</defaultValue> </leafNode> <node name="mfa"> <properties> <help>multi-factor authentication</help> </properties> <children> <node name="totp"> <properties> <help>Time-based one-time passwords</help> </properties> <children> <leafNode name="slop"> <properties> <help>Maximum allowed clock slop in seconds</help> <valueHelp> <format>1-65535</format> <description>Seconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> <defaultValue>180</defaultValue> </leafNode> <leafNode name="drift"> <properties> <help>Time drift in seconds</help> <valueHelp> <format>1-65535</format> <description>Seconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> <defaultValue>0</defaultValue> </leafNode> <leafNode name="step"> <properties> <help>Step value for totp in seconds</help> <valueHelp> <format>1-65535</format> <description>Seconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> <defaultValue>30</defaultValue> </leafNode> <leafNode name="digits"> <properties> <help>Number of digits to use for totp hash</help> <valueHelp> <format>1-65535</format> <description>Seconds</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-65535"/> </constraint> </properties> <defaultValue>6</defaultValue> </leafNode> <leafNode name="challenge"> <properties> <help>Expect password as result of a challenge response protocol</help> <completionHelp> <list>disable enable</list> </completionHelp> <valueHelp> <format>disable</format> <description>Disable challenge-response</description> </valueHelp> <valueHelp> <format>enable</format> <description>Enable chalenge-response</description> </valueHelp> <constraint> <regex>(disable|enable)</regex> </constraint> </properties> <defaultValue>enable</defaultValue> </leafNode> </children> </node> </children> </node> </children> </node> <leafNode name="shared-secret-key"> <properties> <help>Secret key shared with remote end of tunnel</help> <completionHelp> <path>pki openvpn shared-secret</path> </completionHelp> </properties> </leafNode> <node name="tls"> <properties> <help>Transport Layer Security (TLS) options</help> </properties> <children> <leafNode name="auth-key"> <properties> <help>TLS shared secret key for tls-auth</help> <completionHelp> <path>pki openvpn shared-secret</path> </completionHelp> </properties> </leafNode> #include <include/pki/certificate.xml.i> #include <include/pki/ca-certificate-multi.xml.i> <leafNode name="dh-params"> <properties> <help>Diffie Hellman parameters (server only)</help> <completionHelp> <path>pki dh</path> </completionHelp> </properties> </leafNode> <leafNode name="crypt-key"> <properties> <help>Static key to use to authenticate control channel</help> <completionHelp> <path>pki openvpn shared-secret</path> </completionHelp> </properties> </leafNode> <leafNode name="tls-version-min"> <properties> <help>Specify the minimum required TLS version</help> <completionHelp> <list>1.0 1.1 1.2 1.3</list> </completionHelp> <valueHelp> <format>1.0</format> <description>TLS v1.0</description> </valueHelp> <valueHelp> <format>1.1</format> <description>TLS v1.1</description> </valueHelp> <valueHelp> <format>1.2</format> <description>TLS v1.2</description> </valueHelp> <valueHelp> <format>1.3</format> <description>TLS v1.3</description> </valueHelp> <constraint> <regex>(1.0|1.1|1.2|1.3)</regex> </constraint> </properties> </leafNode> <leafNode name="role"> <properties> <help>TLS negotiation role</help> <completionHelp> <list>active passive</list> </completionHelp> <valueHelp> <format>active</format> <description>Initiate TLS negotiation actively</description> </valueHelp> <valueHelp> <format>passive</format> <description>Wait for incoming TLS connection</description> </valueHelp> <constraint> <regex>(active|passive)</regex> </constraint> </properties> </leafNode> </children> </node> <leafNode name="use-lzo-compression"> <properties> <help>Use fast LZO compression on this TUN/TAP interface</help> <valueless/> </properties> </leafNode> - <leafNode name="enable-dco"> - <properties> - <help>Use to enable OpenVPN data channel offload on this TUN interface</help> - <valueless/> - </properties> - </leafNode> #include <include/interface/redirect.xml.i> #include <include/interface/vrf.xml.i> </children> </tagNode> </children> </node> </interfaceDefinition> diff --git a/src/conf_mode/interfaces-openvpn.py b/src/conf_mode/interfaces-openvpn.py index 653474ed0..3bef9b8f6 100755 --- a/src/conf_mode/interfaces-openvpn.py +++ b/src/conf_mode/interfaces-openvpn.py @@ -1,716 +1,730 @@ #!/usr/bin/env python3 # # Copyright (C) 2019-2023 VyOS maintainers and contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 or later as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import re import tempfile from cryptography.hazmat.primitives.asymmetric import ec from glob import glob from sys import exit from ipaddress import IPv4Address from ipaddress import IPv4Network from ipaddress import IPv6Address from ipaddress import IPv6Network from ipaddress import summarize_address_range from netifaces import interfaces from secrets import SystemRandom from shutil import rmtree from vyos.config import Config from vyos.configdict import get_interface_dict from vyos.configdict import is_node_changed from vyos.configverify import verify_vrf from vyos.configverify import verify_bridge_delete from vyos.configverify import verify_mirror_redirect from vyos.configverify import verify_bond_bridge_member from vyos.ifconfig import VTunIf from vyos.pki import load_dh_parameters from vyos.pki import load_private_key from vyos.pki import sort_ca_chain from vyos.pki import verify_ca_chain from vyos.pki import wrap_certificate from vyos.pki import wrap_crl from vyos.pki import wrap_dh_parameters from vyos.pki import wrap_openvpn_key from vyos.pki import wrap_private_key from vyos.template import render from vyos.template import is_ipv4 from vyos.template import is_ipv6 from vyos.utils.dict import dict_search from vyos.utils.dict import dict_search_args from vyos.utils.list import is_list_equal from vyos.utils.file import makedir from vyos.utils.file import read_file from vyos.utils.file import write_file from vyos.utils.kernel import check_kmod from vyos.utils.kernel import unload_kmod from vyos.utils.process import call from vyos.utils.permission import chown from vyos.utils.process import cmd from vyos.validate import is_addr_assigned from vyos import ConfigError from vyos import airbag airbag.enable() user = 'openvpn' group = 'openvpn' cfg_dir = '/run/openvpn' cfg_file = '/run/openvpn/{ifname}.conf' otp_path = '/config/auth/openvpn' otp_file = '/config/auth/openvpn/{ifname}-otp-secrets' secret_chars = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567') service_file = '/run/systemd/system/openvpn@{ifname}.service.d/20-override.conf' def get_config(config=None): """ Retrive CLI config as dictionary. Dictionary can never be empty, as at least the interface name will be added or a deleted flag """ if config: conf = config else: conf = Config() base = ['interfaces', 'openvpn'] ifname, openvpn = get_interface_dict(conf, base) openvpn['auth_user_pass_file'] = '/run/openvpn/{ifname}.pw'.format(**openvpn) if 'deleted' in openvpn: return openvpn openvpn['pki'] = conf.get_config_dict(['pki'], key_mangling=('-', '_'), get_first_key=True, no_tag_node_value_mangle=True) if is_node_changed(conf, base + [ifname, 'openvpn-option']): openvpn.update({'restart_required': {}}) if is_node_changed(conf, base + [ifname, 'enable-dco']): openvpn.update({'restart_required': {}}) # We have to get the dict using 'get_config_dict' instead of 'get_interface_dict' # as 'get_interface_dict' merges the defaults in, so we can not check for defaults in there. tmp = conf.get_config_dict(base + [openvpn['ifname']], get_first_key=True) # We have to cleanup the config dict, as default values could enable features # which are not explicitly enabled on the CLI. Example: server mfa totp # originate comes with defaults, which will enable the # totp plugin, even when not set via CLI so we # need to check this first and drop those keys if dict_search('server.mfa.totp', tmp) == None: del openvpn['server']['mfa'] + # OpenVPN Data-Channel-Offload (DCO) is a Kernel module. If loaded it applies to all + # OpenVPN interfaces. Check if DCO is used by any other interface instance. + tmp = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True) + for interface, interface_config in tmp.items(): + # If one interface has DCO configured, enable it. No need to further check + # all other OpenVPN interfaces. We must use a dedicated key to indicate + # the Kernel module must be loaded or not. The per interface "offload.dco" + # key is required per OpenVPN interface instance. + if dict_search('offload.dco', interface_config) != None: + openvpn['module_load_dco'] = {} + break + return openvpn def is_ec_private_key(pki, cert_name): if not pki or 'certificate' not in pki: return False if cert_name not in pki['certificate']: return False pki_cert = pki['certificate'][cert_name] if 'private' not in pki_cert or 'key' not in pki_cert['private']: return False key = load_private_key(pki_cert['private']['key']) return isinstance(key, ec.EllipticCurvePrivateKey) def verify_pki(openvpn): pki = openvpn['pki'] interface = openvpn['ifname'] mode = openvpn['mode'] shared_secret_key = dict_search_args(openvpn, 'shared_secret_key') tls = dict_search_args(openvpn, 'tls') if not bool(shared_secret_key) ^ bool(tls): # xor check if only one is set raise ConfigError('Must specify only one of "shared-secret-key" and "tls"') if mode in ['server', 'client'] and not tls: raise ConfigError('Must specify "tls" for server and client modes') if not pki: raise ConfigError('PKI is not configured') if shared_secret_key: if not dict_search_args(pki, 'openvpn', 'shared_secret'): raise ConfigError('There are no openvpn shared-secrets in PKI configuration') if shared_secret_key not in pki['openvpn']['shared_secret']: raise ConfigError(f'Invalid shared-secret on openvpn interface {interface}') if tls: if 'ca_certificate' not in tls: raise ConfigError(f'Must specify "tls ca-certificate" on openvpn interface {interface}') for ca_name in tls['ca_certificate']: if ca_name not in pki['ca']: raise ConfigError(f'Invalid CA certificate on openvpn interface {interface}') if len(tls['ca_certificate']) > 1: sorted_chain = sort_ca_chain(tls['ca_certificate'], pki['ca']) if not verify_ca_chain(sorted_chain, pki['ca']): raise ConfigError(f'CA certificates are not a valid chain') if mode != 'client' and 'auth_key' not in tls: if 'certificate' not in tls: raise ConfigError(f'Missing "tls certificate" on openvpn interface {interface}') if 'certificate' in tls: if tls['certificate'] not in pki['certificate']: raise ConfigError(f'Invalid certificate on openvpn interface {interface}') if dict_search_args(pki, 'certificate', tls['certificate'], 'private', 'password_protected') is not None: raise ConfigError(f'Cannot use encrypted private key on openvpn interface {interface}') if mode == 'server' and 'dh_params' not in tls and not is_ec_private_key(pki, tls['certificate']): raise ConfigError('Must specify "tls dh-params" when not using EC keys in server mode') if 'dh_params' in tls: if 'dh' not in pki: raise ConfigError('There are no DH parameters in PKI configuration') if tls['dh_params'] not in pki['dh']: raise ConfigError(f'Invalid dh-params on openvpn interface {interface}') pki_dh = pki['dh'][tls['dh_params']] dh_params = load_dh_parameters(pki_dh['parameters']) dh_numbers = dh_params.parameter_numbers() dh_bits = dh_numbers.p.bit_length() if dh_bits < 2048: raise ConfigError(f'Minimum DH key-size is 2048 bits') if 'auth_key' in tls or 'crypt_key' in tls: if not dict_search_args(pki, 'openvpn', 'shared_secret'): raise ConfigError('There are no openvpn shared-secrets in PKI configuration') if 'auth_key' in tls: if tls['auth_key'] not in pki['openvpn']['shared_secret']: raise ConfigError(f'Invalid auth-key on openvpn interface {interface}') if 'crypt_key' in tls: if tls['crypt_key'] not in pki['openvpn']['shared_secret']: raise ConfigError(f'Invalid crypt-key on openvpn interface {interface}') def verify(openvpn): if 'deleted' in openvpn: # remove totp secrets file if totp is not configured if os.path.isfile(otp_file.format(**openvpn)): os.remove(otp_file.format(**openvpn)) verify_bridge_delete(openvpn) return None if 'mode' not in openvpn: raise ConfigError('Must specify OpenVPN operation mode!') # # OpenVPN client mode - VERIFY # if openvpn['mode'] == 'client': if 'local_port' in openvpn: raise ConfigError('Cannot specify "local-port" in client mode') if 'local_host' in openvpn: raise ConfigError('Cannot specify "local-host" in client mode') if 'remote_host' not in openvpn: raise ConfigError('Must specify "remote-host" in client mode') if openvpn['protocol'] == 'tcp-passive': raise ConfigError('Protocol "tcp-passive" is not valid in client mode') if dict_search('tls.dh_params', openvpn): raise ConfigError('Cannot specify "tls dh-params" in client mode') # # OpenVPN site-to-site - VERIFY # elif openvpn['mode'] == 'site-to-site': if 'local_address' not in openvpn and 'is_bridge_member' not in openvpn: raise ConfigError('Must specify "local-address" or add interface to bridge') if 'local_address' in openvpn: if len([addr for addr in openvpn['local_address'] if is_ipv4(addr)]) > 1: raise ConfigError('Only one IPv4 local-address can be specified') if len([addr for addr in openvpn['local_address'] if is_ipv6(addr)]) > 1: raise ConfigError('Only one IPv6 local-address can be specified') if openvpn['device_type'] == 'tun': if 'remote_address' not in openvpn: raise ConfigError('Must specify "remote-address"') if 'remote_address' in openvpn: if len([addr for addr in openvpn['remote_address'] if is_ipv4(addr)]) > 1: raise ConfigError('Only one IPv4 remote-address can be specified') if len([addr for addr in openvpn['remote_address'] if is_ipv6(addr)]) > 1: raise ConfigError('Only one IPv6 remote-address can be specified') if not 'local_address' in openvpn: raise ConfigError('"remote-address" requires "local-address"') v4loAddr = [addr for addr in openvpn['local_address'] if is_ipv4(addr)] v4remAddr = [addr for addr in openvpn['remote_address'] if is_ipv4(addr)] if v4loAddr and not v4remAddr: raise ConfigError('IPv4 "local-address" requires IPv4 "remote-address"') elif v4remAddr and not v4loAddr: raise ConfigError('IPv4 "remote-address" requires IPv4 "local-address"') v6remAddr = [addr for addr in openvpn['remote_address'] if is_ipv6(addr)] v6loAddr = [addr for addr in openvpn['local_address'] if is_ipv6(addr)] if v6loAddr and not v6remAddr: raise ConfigError('IPv6 "local-address" requires IPv6 "remote-address"') elif v6remAddr and not v6loAddr: raise ConfigError('IPv6 "remote-address" requires IPv6 "local-address"') if is_list_equal(v4loAddr, v4remAddr) or is_list_equal(v6loAddr, v6remAddr): raise ConfigError('"local-address" and "remote-address" cannot be the same') if dict_search('local_host', openvpn) in dict_search('local_address', openvpn): raise ConfigError('"local-address" cannot be the same as "local-host"') if dict_search('remote_host', openvpn) in dict_search('remote_address', openvpn): raise ConfigError('"remote-address" and "remote-host" can not be the same') if openvpn['device_type'] == 'tap' and 'local_address' in openvpn: # we can only have one local_address, this is ensured above v4addr = None for laddr in openvpn['local_address']: if is_ipv4(laddr): v4addr = laddr break if v4addr in openvpn['local_address'] and 'subnet_mask' not in openvpn['local_address'][v4addr]: raise ConfigError('Must specify IPv4 "subnet-mask" for local-address') if dict_search('encryption.ncp_ciphers', openvpn): raise ConfigError('NCP ciphers can only be used in client or server mode') else: # checks for client-server or site-to-site bridged if 'local_address' in openvpn or 'remote_address' in openvpn: raise ConfigError('Cannot specify "local-address" or "remote-address" ' \ 'in client/server or bridge mode') # # OpenVPN server mode - VERIFY # if openvpn['mode'] == 'server': if openvpn['protocol'] == 'tcp-active': raise ConfigError('Protocol "tcp-active" is not valid in server mode') if dict_search('authentication.username', openvpn) or dict_search('authentication.password', openvpn): raise ConfigError('Cannot specify "authentication" in server mode') if 'remote_port' in openvpn: raise ConfigError('Cannot specify "remote-port" in server mode') if 'remote_host' in openvpn: raise ConfigError('Cannot specify "remote-host" in server mode') tmp = dict_search('server.subnet', openvpn) if tmp: v4_subnets = len([subnet for subnet in tmp if is_ipv4(subnet)]) v6_subnets = len([subnet for subnet in tmp if is_ipv6(subnet)]) if v4_subnets > 1: raise ConfigError('Cannot specify more than 1 IPv4 server subnet') if v6_subnets > 1: raise ConfigError('Cannot specify more than 1 IPv6 server subnet') if v6_subnets > 0 and v4_subnets == 0: raise ConfigError('IPv6 server requires an IPv4 server subnet') for subnet in tmp: if is_ipv4(subnet): subnet = IPv4Network(subnet) if openvpn['device_type'] == 'tun' and subnet.prefixlen > 29: raise ConfigError('Server subnets smaller than /29 with device type "tun" are not supported') elif openvpn['device_type'] == 'tap' and subnet.prefixlen > 30: raise ConfigError('Server subnets smaller than /30 with device type "tap" are not supported') for client in (dict_search('client', openvpn) or []): if client['ip'] and not IPv4Address(client['ip'][0]) in subnet: raise ConfigError(f'Client "{client["name"]}" IP {client["ip"][0]} not in server subnet {subnet}') else: if 'is_bridge_member' not in openvpn: raise ConfigError('Must specify "server subnet" or add interface to bridge in server mode') if hasattr(dict_search('server.client', openvpn), '__iter__'): for client_k, client_v in dict_search('server.client', openvpn).items(): if (client_v.get('ip') and len(client_v['ip']) > 1) or (client_v.get('ipv6_ip') and len(client_v['ipv6_ip']) > 1): raise ConfigError(f'Server client "{client_k}": cannot specify more than 1 IPv4 and 1 IPv6 IP') if dict_search('server.client_ip_pool', openvpn): if not (dict_search('server.client_ip_pool.start', openvpn) and dict_search('server.client_ip_pool.stop', openvpn)): raise ConfigError('Server client-ip-pool requires both start and stop addresses') else: v4PoolStart = IPv4Address(dict_search('server.client_ip_pool.start', openvpn)) v4PoolStop = IPv4Address(dict_search('server.client_ip_pool.stop', openvpn)) if v4PoolStart > v4PoolStop: raise ConfigError(f'Server client-ip-pool start address {v4PoolStart} is larger than stop address {v4PoolStop}') v4PoolSize = int(v4PoolStop) - int(v4PoolStart) if v4PoolSize >= 65536: raise ConfigError(f'Server client-ip-pool is too large [{v4PoolStart} -> {v4PoolStop} = {v4PoolSize}], maximum is 65536 addresses.') v4PoolNets = list(summarize_address_range(v4PoolStart, v4PoolStop)) for client in (dict_search('client', openvpn) or []): if client['ip']: for v4PoolNet in v4PoolNets: if IPv4Address(client['ip'][0]) in v4PoolNet: print(f'Warning: Client "{client["name"]}" IP {client["ip"][0]} is in server IP pool, it is not reserved for this client.') for subnet in (dict_search('server.subnet', openvpn) or []): if is_ipv6(subnet): tmp = dict_search('client_ipv6_pool.base', openvpn) if tmp: if not dict_search('server.client_ip_pool', openvpn): raise ConfigError('IPv6 server pool requires an IPv4 server pool') if int(tmp.split('/')[1]) >= 112: raise ConfigError('IPv6 server pool must be larger than /112') # # todo - weird logic # v6PoolStart = IPv6Address(tmp) v6PoolStop = IPv6Network((v6PoolStart, openvpn['server_ipv6_pool_prefixlen']), strict=False)[-1] # don't remove the parentheses, it's a 2-tuple v6PoolSize = int(v6PoolStop) - int(v6PoolStart) if int(openvpn['server_ipv6_pool_prefixlen']) > 96 else 65536 if v6PoolSize < v4PoolSize: raise ConfigError(f'IPv6 server pool must be at least as large as the IPv4 pool (current sizes: IPv6={v6PoolSize} IPv4={v4PoolSize})') v6PoolNets = list(summarize_address_range(v6PoolStart, v6PoolStop)) for client in (dict_search('client', openvpn) or []): if client['ipv6_ip']: for v6PoolNet in v6PoolNets: if IPv6Address(client['ipv6_ip'][0]) in v6PoolNet: print(f'Warning: Client "{client["name"]}" IP {client["ipv6_ip"][0]} is in server IP pool, it is not reserved for this client.') # add mfa users to the file the mfa plugin uses if dict_search('server.mfa.totp', openvpn): user_data = '' if not os.path.isfile(otp_file.format(**openvpn)): write_file(otp_file.format(**openvpn), user_data, user=user, group=group, mode=0o644) ovpn_users = read_file(otp_file.format(**openvpn)) for client in (dict_search('server.client', openvpn) or []): exists = None for ovpn_user in ovpn_users.split('\n'): if re.search('^' + client + ' ', ovpn_user): user_data += f'{ovpn_user}\n' exists = 'true' if not exists: random = SystemRandom() totp_secret = ''.join(random.choice(secret_chars) for _ in range(16)) user_data += f'{client} otp totp:sha1:base32:{totp_secret}::xxx *\n' write_file(otp_file.format(**openvpn), user_data, user=user, group=group, mode=0o644) else: # checks for both client and site-to-site go here if dict_search('server.reject_unconfigured_clients', openvpn): raise ConfigError('Option reject-unconfigured-clients only supported in server mode') if 'replace_default_route' in openvpn and 'remote_host' not in openvpn: raise ConfigError('Cannot set "replace-default-route" without "remote-host"') # # OpenVPN common verification section # not depending on any operation mode # # verify specified IP address is present on any interface on this system if 'local_host' in openvpn: if not is_addr_assigned(openvpn['local_host']): print('local-host IP address "{local_host}" not assigned' \ ' to any interface'.format(**openvpn)) # TCP active if openvpn['protocol'] == 'tcp-active': if 'local_port' in openvpn: raise ConfigError('Cannot specify "local-port" with "tcp-active"') if 'remote_host' not in openvpn: raise ConfigError('Must specify "remote-host" with "tcp-active"') # # TLS/encryption # if 'shared_secret_key' in openvpn: if dict_search('encryption.cipher', openvpn) in ['aes128gcm', 'aes192gcm', 'aes256gcm']: raise ConfigError('GCM encryption with shared-secret-key not supported') if 'tls' in openvpn: if {'auth_key', 'crypt_key'} <= set(openvpn['tls']): raise ConfigError('TLS auth and crypt keys are mutually exclusive') tmp = dict_search('tls.role', openvpn) if tmp: if openvpn['mode'] in ['client', 'server']: if not dict_search('tls.auth_key', openvpn): raise ConfigError('Cannot specify "tls role" in client-server mode') if tmp == 'active': if openvpn['protocol'] == 'tcp-passive': raise ConfigError('Cannot specify "tcp-passive" when "tls role" is "active"') if dict_search('tls.dh_params', openvpn): raise ConfigError('Cannot specify "tls dh-params" when "tls role" is "active"') elif tmp == 'passive': if openvpn['protocol'] == 'tcp-active': raise ConfigError('Cannot specify "tcp-active" when "tls role" is "passive"') if not dict_search('tls.dh_params', openvpn): raise ConfigError('Must specify "tls dh-params" when "tls role" is "passive"') if 'certificate' in openvpn['tls'] and is_ec_private_key(openvpn['pki'], openvpn['tls']['certificate']): if 'dh_params' in openvpn['tls']: print('Warning: using dh-params and EC keys simultaneously will ' \ 'lead to DH ciphers being used instead of ECDH') if dict_search('encryption.cipher', openvpn) == 'none': print('Warning: "encryption none" was specified!') print('No encryption will be performed and data is transmitted in ' \ 'plain text over the network!') verify_pki(openvpn) # # Auth user/pass # if (dict_search('authentication.username', openvpn) and not dict_search('authentication.password', openvpn)): raise ConfigError('Password for authentication is missing') if (dict_search('authentication.password', openvpn) and not dict_search('authentication.username', openvpn)): raise ConfigError('Username for authentication is missing') verify_vrf(openvpn) verify_bond_bridge_member(openvpn) verify_mirror_redirect(openvpn) return None def generate_pki_files(openvpn): pki = openvpn['pki'] if not pki: return None interface = openvpn['ifname'] shared_secret_key = dict_search_args(openvpn, 'shared_secret_key') tls = dict_search_args(openvpn, 'tls') if shared_secret_key: pki_key = pki['openvpn']['shared_secret'][shared_secret_key] key_path = os.path.join(cfg_dir, f'{interface}_shared.key') write_file(key_path, wrap_openvpn_key(pki_key['key']), user=user, group=group) if tls: if 'ca_certificate' in tls: cert_path = os.path.join(cfg_dir, f'{interface}_ca.pem') crl_path = os.path.join(cfg_dir, f'{interface}_crl.pem') if os.path.exists(cert_path): os.unlink(cert_path) if os.path.exists(crl_path): os.unlink(crl_path) for cert_name in sort_ca_chain(tls['ca_certificate'], pki['ca']): pki_ca = pki['ca'][cert_name] if 'certificate' in pki_ca: write_file(cert_path, wrap_certificate(pki_ca['certificate']) + "\n", user=user, group=group, mode=0o600, append=True) if 'crl' in pki_ca: for crl in pki_ca['crl']: write_file(crl_path, wrap_crl(crl) + "\n", user=user, group=group, mode=0o600, append=True) openvpn['tls']['crl'] = True if 'certificate' in tls: cert_name = tls['certificate'] pki_cert = pki['certificate'][cert_name] if 'certificate' in pki_cert: cert_path = os.path.join(cfg_dir, f'{interface}_cert.pem') write_file(cert_path, wrap_certificate(pki_cert['certificate']), user=user, group=group, mode=0o600) if 'private' in pki_cert and 'key' in pki_cert['private']: key_path = os.path.join(cfg_dir, f'{interface}_cert.key') write_file(key_path, wrap_private_key(pki_cert['private']['key']), user=user, group=group, mode=0o600) openvpn['tls']['private_key'] = True if 'dh_params' in tls: dh_name = tls['dh_params'] pki_dh = pki['dh'][dh_name] if 'parameters' in pki_dh: dh_path = os.path.join(cfg_dir, f'{interface}_dh.pem') write_file(dh_path, wrap_dh_parameters(pki_dh['parameters']), user=user, group=group, mode=0o600) if 'auth_key' in tls: key_name = tls['auth_key'] pki_key = pki['openvpn']['shared_secret'][key_name] if 'key' in pki_key: key_path = os.path.join(cfg_dir, f'{interface}_auth.key') write_file(key_path, wrap_openvpn_key(pki_key['key']), user=user, group=group, mode=0o600) if 'crypt_key' in tls: key_name = tls['crypt_key'] pki_key = pki['openvpn']['shared_secret'][key_name] if 'key' in pki_key: key_path = os.path.join(cfg_dir, f'{interface}_crypt.key') write_file(key_path, wrap_openvpn_key(pki_key['key']), user=user, group=group, mode=0o600) def generate(openvpn): interface = openvpn['ifname'] directory = os.path.dirname(cfg_file.format(**openvpn)) openvpn['plugin_dir'] = '/usr/lib/openvpn' # create base config directory on demand makedir(directory, user, group) # enforce proper permissions on /run/openvpn chown(directory, user, group) # we can't know in advance which clients have been removed, # thus all client configs will be removed and re-added on demand ccd_dir = os.path.join(directory, 'ccd', interface) if os.path.isdir(ccd_dir): rmtree(ccd_dir, ignore_errors=True) # Remove systemd directories with overrides service_dir = os.path.dirname(service_file.format(**openvpn)) if os.path.isdir(service_dir): rmtree(service_dir, ignore_errors=True) if 'deleted' in openvpn or 'disable' in openvpn: return None # create client config directory on demand makedir(ccd_dir, user, group) # Fix file permissons for keys generate_pki_files(openvpn) # Generate User/Password authentication file if 'authentication' in openvpn: render(openvpn['auth_user_pass_file'], 'openvpn/auth.pw.j2', openvpn, user=user, group=group, permission=0o600) else: # delete old auth file if present if os.path.isfile(openvpn['auth_user_pass_file']): os.remove(openvpn['auth_user_pass_file']) # Generate client specific configuration server_client = dict_search_args(openvpn, 'server', 'client') if server_client: for client, client_config in server_client.items(): client_file = os.path.join(ccd_dir, client) # Our client need's to know its subnet mask ... client_config['server_subnet'] = dict_search('server.subnet', openvpn) render(client_file, 'openvpn/client.conf.j2', client_config, user=user, group=group) # we need to support quoting of raw parameters from OpenVPN CLI # see https://vyos.dev/T1632 render(cfg_file.format(**openvpn), 'openvpn/server.conf.j2', openvpn, formater=lambda _: _.replace(""", '"'), user=user, group=group) # Render 20-override.conf for OpenVPN service render(service_file.format(**openvpn), 'openvpn/service-override.conf.j2', openvpn, formater=lambda _: _.replace(""", '"'), user=user, group=group) # Reload systemd services config to apply an override call(f'systemctl daemon-reload') return None def apply(openvpn): interface = openvpn['ifname'] # Do some cleanup when OpenVPN is disabled/deleted if 'deleted' in openvpn or 'disable' in openvpn: call(f'systemctl stop openvpn@{interface}.service') for cleanup_file in glob(f'/run/openvpn/{interface}.*'): if os.path.isfile(cleanup_file): os.unlink(cleanup_file) if interface in interfaces(): VTunIf(interface).remove() + # dynamically load/unload DCO Kernel extension if requested + dco_module = 'ovpn_dco_v2' + if 'module_load_dco' in openvpn: + check_kmod(dco_module) + else: + unload_kmod(dco_module) + + # Now bail out early if interface is disabled or got deleted + if 'deleted' in openvpn or 'disable' in openvpn: return None # verify specified IP address is present on any interface on this system # Allow to bind service to nonlocal address, if it virtaual-vrrp address # or if address will be assign later if 'local_host' in openvpn: if not is_addr_assigned(openvpn['local_host']): cmd('sysctl -w net.ipv4.ip_nonlocal_bind=1') - # dynamically load/unload DCO Kernel extension if requested - dco_module = 'ovpn_dco_v2' - if 'enable_dco' in openvpn: - check_kmod(dco_module) - else: - unload_kmod(dco_module) - # No matching OpenVPN process running - maybe it got killed or none # existed - nevertheless, spawn new OpenVPN process action = 'reload-or-restart' if 'restart_required' in openvpn: action = 'restart' call(f'systemctl {action} openvpn@{interface}.service') o = VTunIf(**openvpn) o.update(openvpn) return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) exit(1)