diff --git a/interface-definitions/interfaces-openvpn.xml b/interface-definitions/interfaces-openvpn.xml index 2c77bcf37..2c2556f45 100644 --- a/interface-definitions/interfaces-openvpn.xml +++ b/interface-definitions/interfaces-openvpn.xml @@ -1,716 +1,677 @@ <?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 name</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> <node name="authentication"> <properties> <help>Authentication options</help> </properties> <children> <leafNode name="password"> <properties> <help>OpenVPN password used for authentication</help> </properties> </leafNode> <leafNode name="username"> <properties> <help>OpenVPN username used for authentication</help> </properties> </leafNode> </children> </node> - <node name="bridge-group"> - <properties> - <help>Interface to be added to a bridge group</help> - </properties> - <children> - <leafNode name="bridge"> - <properties> - <help>Interface to a bridge-group</help> - <completionHelp> - <script>${vyos_completion_dir}/list_interfaces.py --type bridge</script> - </completionHelp> - </properties> - </leafNode> - <leafNode name="cost"> - <properties> - <help>Path cost for this port</help> - <valueHelp> - <format>0-2147483647</format> - <description>Path cost for this port</description> - </valueHelp> - <constraint> - <validator name="numeric" argument="--range 0-2147483647"/> - </constraint> - </properties> - </leafNode> - <leafNode name="cost"> - <properties> - <help>Path priority for this port</help> - <valueHelp> - <format>0-255</format> - <description>Path priority for this port</description> - </valueHelp> - <constraint> - <validator name="numeric" argument="--range 0-255"/> - </constraint> - </properties> - </leafNode> - </children> - </node> <leafNode name="description"> <properties> <help>Description</help> </properties> </leafNode> <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> </leafNode> <leafNode name="disable"> <properties> <help>Disable interface</help> <valueless/> </properties> </leafNode> <node name="encryption"> <properties> <help>Data Encryption settings</help> </properties> <children> <leafNode name="cipher"> <properties> <help>Standard Data Encryption Algorithm</help> <completionHelp> <list>des 3des bf128 bf256 aes128 aes128gcm aes192 aes192gcm aes256 aes256gcm</list> </completionHelp> <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>(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>des 3des aes128 aes128gcm aes192 aes192gcm aes256 aes256gcm</list> </completionHelp> <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>(des|3des|aes128|aes128gcm|aes192|aes192gcm|aes256|aes256gcm)</regex> </constraint> <multi/> </properties> </leafNode> <leafNode name="disable-ncp"> <properties> <help>Disable support for ncp-ciphers</help> <valueless/> </properties> </leafNode> </children> </node> <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 [default 6]</help> <valueHelp> <format>0-1000</format> <description>Maximum number of keepalive packet failures</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-1000"/> </constraint> </properties> </leafNode> <leafNode name="interval"> <properties> <help>Keepalive packet interval (seconds) [default 10]</help> <valueHelp> <format>0-600</format> <description>Keepalive packet interval (seconds)</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 0-600"/> </constraint> </properties> </leafNode> </children> </node> <tagNode name="local-address"> <properties> <help>Local IP address of tunnel</help> <constraint> <validator name="ipv4-address"/> </constraint> </properties> <children> <leafNode name="subnet-mask"> <properties> <help>Subnet-mask for local IP address of tunnel</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> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> <leafNode name="local-port"> <properties> <help>Local port number to accept connections</help> <valueHelp> <format>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> <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>Site-to-site mode</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> </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> <constraint> <validator name="ipv4-address"/> </constraint> </properties> </leafNode> <leafNode name="remote-host"> <properties> <help>Remote host to connect to (dynamic if not set)</help> <valueHelp> <format>ipv4</format> <description>IP 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>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> <leafNode name="disable"> <properties> <help>Option to disable client connection</help> <valueless/> </properties> </leafNode> <leafNode name="ip"> <properties> <help>IP address of the client</help> <valueHelp> <format>ipv4</format> <description>Client IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> </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> <constraint> <validator name="ipv4-prefix"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="subnet"> <properties> <help>Subnet belonging to the client</help> <valueHelp> <format>ipv4net</format> <description>IPv4 network and prefix length belonging to the client</description> </valueHelp> <constraint> <validator name="ipv4-prefix"/> </constraint> <multi/> </properties> </leafNode> </children> </tagNode> <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>1-4096</format> <description>Number of concurrent clients</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-4096"/> </constraint> </properties> </leafNode> <leafNode name="name-server"> <properties> <help>Domain Name Server (DNS)</help> <valueHelp> <format>ipv4</format> <description>DNS server IPv4 address</description> </valueHelp> <constraint> <validator name="ipv4-address"/> </constraint> <multi/> </properties> </leafNode> <leafNode 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> <constraint> <validator name="ipv4-prefix"/> </constraint> <multi/> </properties> </leafNode> <leafNode name="reject-unconfigured-clients"> <properties> <help>Reject connections from clients that are not explicitly configured</help> </properties> </leafNode> <leafNode name="subnet"> <properties> <help>Server-mode subnet (from which client IPs are allocated)</help> <valueHelp> <format>ipv4net</format> <description>IPv4 address and prefix length</description> </valueHelp> <constraint> <validator name="ipv4-prefix"/> </constraint> </properties> </leafNode> <leafNode name="topology"> <properties> <help>Topology for clients</help> <completionHelp> <list>point-to-point subnet</list> </completionHelp> <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)</regex> </constraint> </properties> </leafNode> </children> </node> <leafNode name="shared-secret-key-file"> <properties> <help>File containing the secret key shared with remote end of tunnel</help> <valueHelp> <format>file</format> <description>File in /config/auth directory</description> </valueHelp> <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> </properties> </leafNode> <node name="tls"> <properties> <help>Transport Layer Security (TLS) options</help> </properties> <children> <leafNode name="auth-file"> <properties> <help>File containing tls static key for tls-auth</help> <valueHelp> <format>file</format> <description>File in /config/auth directory</description> </valueHelp> <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> </properties> </leafNode> <leafNode name="ca-cert-file"> <properties> <help>File containing certificate for Certificate Authority (CA)</help> <valueHelp> <format>file</format> <description>File in /config/auth directory</description> </valueHelp> <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> </properties> </leafNode> <leafNode name="cert-file"> <properties> <help>File containing certificate for this host</help> <valueHelp> <format>file</format> <description>File in /config/auth directory</description> </valueHelp> <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> </properties> </leafNode> <leafNode name="crl-file"> <properties> <help>File containing certificate revocation list (CRL) for this host</help> <valueHelp> <format>file</format> <description>File in /config/auth directory</description> </valueHelp> <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> </properties> </leafNode> <leafNode name="dh-file"> <properties> <help>File containing Diffie Hellman parameters (server only)</help> <valueHelp> <format>file</format> <description>File in /config/auth directory</description> </valueHelp> <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> </properties> </leafNode> <leafNode name="key-file"> <properties> <help>File containing this host's private key</help> <valueHelp> <format>file</format> <description>File in /config/auth directory</description> </valueHelp> <constraint> <validator name="file-exists" argument="--directory /config/auth"/> </constraint> </properties> </leafNode> <leafNode name="tls-version-min"> <properties> <help>Specify the minimum required TLS version</help> <completionHelp> <list>1.0 1.1 1.2</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> <constraint> <regex>(1.0|1.1|1.2)</regex> </constraint> </properties> </leafNode> <leafNode name="role"> <properties> <help>File containing this host's private key</help> <completionHelp> <list>active passive</list> </completionHelp> <valueHelp> <format>active</format> <description>Initiate TLS negotiation actively</description> </valueHelp> <valueHelp> <format>passive</format> <description>Waiting for TLS connections passively</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> </children> </tagNode> </children> </node> </interfaceDefinition> diff --git a/interface-definitions/syslog.xml b/interface-definitions/system-syslog.xml similarity index 98% rename from interface-definitions/syslog.xml rename to interface-definitions/system-syslog.xml index d5ea4511e..8f4b105c8 100644 --- a/interface-definitions/syslog.xml +++ b/interface-definitions/system-syslog.xml @@ -1,932 +1,945 @@ <?xml version="1.0"?> <interfaceDefinition> <node name="system"> <children> - <node name="syslog" owner="${vyos_conf_scripts_dir}/syslog.py"> + <node name="syslog" owner="${vyos_conf_scripts_dir}/system-syslog.py"> <properties> <help>System logging</help> <priority>400</priority> </properties> <children> <tagNode name="user"> <properties> <help>Logging to specific user's terminal</help> <constraint> <regex>^[a-z_][a-z0-9_-]{1,31}[$]?</regex> </constraint> <constraintErrorMessage>illegal characters in user</constraintErrorMessage> <valueHelp> <format>username</format> <description>user login name</description> </valueHelp> </properties> <children> <tagNode name="facility"> <properties> <help>Facility for logging</help> <completionHelp> <list>auth authpriv cron daemon kern lpr mail mark news protocols security syslog user uucp local0 local1 local2 local3 local4 local5 local6 local7 all</list> </completionHelp> <constraint> <regex>(auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local0|local1|local2|local3|local4|local5|local6|local7|all)</regex> </constraint> <constraintErrorMessage>Invalid facility type</constraintErrorMessage> <valueHelp> <format>all</format> <description>All facilities excluding "mark"</description> </valueHelp> <valueHelp> <format>auth</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>authpriv</format> <description>Non-system authorization</description> </valueHelp> <valueHelp> <format>cron</format> <description>Cron daemon</description> </valueHelp> <valueHelp> <format>daemon</format> <description>System daemons</description> </valueHelp> <valueHelp> <format>kern</format> <description>Kernel</description> </valueHelp> <valueHelp> <format>lpr</format> <description>Line printer spooler</description> </valueHelp> <valueHelp> <format>mail</format> <description>Mail subsystem</description> </valueHelp> <valueHelp> <format>mark</format> <description>Timestamp</description> </valueHelp> <valueHelp> <format>news</format> <description>USENET subsystem</description> </valueHelp> <valueHelp> <format>protocols</format> <description>depricated will be set to local7</description> </valueHelp> <valueHelp> <format>security</format> <description>depricated will be set to auth</description> </valueHelp> <valueHelp> <format>syslog</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>user</format> <description>Application processes</description> </valueHelp> <valueHelp> <format>uucp</format> <description>UUCP subsystem</description> </valueHelp> <valueHelp> <format>local0</format> <description>Local facility 0</description> </valueHelp> <valueHelp> <format>local1</format> <description>Local facility 1</description> </valueHelp> <valueHelp> <format>local2</format> <description>Local facility 2</description> </valueHelp> <valueHelp> <format>local3</format> <description>Local facility 3</description> </valueHelp> <valueHelp> <format>local4</format> <description>Local facility 4</description> </valueHelp> <valueHelp> <format>local5</format> <description>Local facility 5</description> </valueHelp> <valueHelp> <format>local6</format> <description>Local facility 6</description> </valueHelp> <valueHelp> <format>local7</format> <description>Local facility 7</description> </valueHelp> </properties> <children> <leafNode name="level"> <properties> <help>Logging level</help> <completionHelp> <list>emerg alert crit err warning notice info debug all</list> </completionHelp> <constraint> <regex>(emerg|alert|crit|err|warning|notice|info|debug|all)</regex> </constraint> <constraintErrorMessage>Invalid loglevel</constraintErrorMessage> <valueHelp> <format>emerg</format> <description>Emergency messages</description> </valueHelp> <valueHelp> <format>alert</format> <description>Urgent messages</description> </valueHelp> <valueHelp> <format>crit</format> <description>Critical messages</description> </valueHelp> <valueHelp> <format>err</format> <description>Error messages</description> </valueHelp> <valueHelp> <format>warning</format> <description>Warning messages</description> </valueHelp> <valueHelp> <format>notice</format> <description>Messages for further investigation</description> </valueHelp> <valueHelp> <format>info</format> <description>Informational messages</description> </valueHelp> <valueHelp> <format>debug</format> <description>Debug messages</description> </valueHelp> <valueHelp> <format>all</format> <description>Log everything</description> </valueHelp> </properties> </leafNode> </children> </tagNode> </children> </tagNode> <tagNode name="host"> <properties> <help>Logging to a remote host</help> <constraint> <validator name="ip-address" /> <regex>(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)</regex> </constraint> <constraintErrorMessage>Invalid host FQDN or IP address</constraintErrorMessage> <valueHelp> <format>x.x.x.x or host.domain.tld</format> <description>Remote host name or IP address</description> </valueHelp> </properties> <children> + <leafNode name="port"> + <properties> + <help>Destination port</help> + <valueHelp> + <format>1-65535</format> + <description>Destination port</description> + </valueHelp> + <constraint> + <validator name="numeric" argument="--range 1-65535"/> + </constraint> + <constraintErrorMessage>Invalid destination port value</constraintErrorMessage> + </properties> + </leafNode> <tagNode name="facility"> <properties> <help>Facility for logging</help> <completionHelp> <list>auth authpriv cron daemon kern lpr mail mark news protocols security syslog user uucp local0 local1 local2 local3 local4 local5 local6 local7 all</list> </completionHelp> <constraint> <regex>(auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local0|local1|local2|local3|local4|local5|local6|local7|all)</regex> </constraint> <constraintErrorMessage>Invalid facility type</constraintErrorMessage> <valueHelp> <format>all</format> <description>All facilities excluding "mark"</description> </valueHelp> <valueHelp> <format>auth</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>authpriv</format> <description>Non-system authorization</description> </valueHelp> <valueHelp> <format>cron</format> <description>Cron daemon</description> </valueHelp> <valueHelp> <format>daemon</format> <description>System daemons</description> </valueHelp> <valueHelp> <format>kern</format> <description>Kernel</description> </valueHelp> <valueHelp> <format>lpr</format> <description>Line printer spooler</description> </valueHelp> <valueHelp> <format>mail</format> <description>Mail subsystem</description> </valueHelp> <valueHelp> <format>mark</format> <description>Timestamp</description> </valueHelp> <valueHelp> <format>news</format> <description>USENET subsystem</description> </valueHelp> <valueHelp> <format>protocols</format> <description>depricated will be set to local7</description> </valueHelp> <valueHelp> <format>security</format> <description>depricated will be set to auth</description> </valueHelp> <valueHelp> <format>syslog</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>user</format> <description>Application processes</description> </valueHelp> <valueHelp> <format>uucp</format> <description>UUCP subsystem</description> </valueHelp> <valueHelp> <format>local0</format> <description>Local facility 0</description> </valueHelp> <valueHelp> <format>local1</format> <description>Local facility 1</description> </valueHelp> <valueHelp> <format>local2</format> <description>Local facility 2</description> </valueHelp> <valueHelp> <format>local3</format> <description>Local facility 3</description> </valueHelp> <valueHelp> <format>local4</format> <description>Local facility 4</description> </valueHelp> <valueHelp> <format>local5</format> <description>Local facility 5</description> </valueHelp> <valueHelp> <format>local6</format> <description>Local facility 6</description> </valueHelp> <valueHelp> <format>local7</format> <description>Local facility 7</description> </valueHelp> </properties> <children> <leafNode name="protocol"> <properties> <help>syslog communication protocol</help> <valueHelp> <format>udp</format> <description>send log messages to remote syslog server over udp</description> </valueHelp> <valueHelp> <format>tcp</format> <description>send log messages to remote syslog server over tcp</description> </valueHelp> <completionHelp> <list>udp tcp</list> </completionHelp> <constraint> <regex>(udp|tcp)</regex> </constraint> <constraintErrorMessage>invalid protocol name</constraintErrorMessage> </properties> </leafNode> <leafNode name="level"> <properties> <help>Logging level</help> <completionHelp> <list>emerg alert crit err warning notice info debug all</list> </completionHelp> <constraint> <regex>(emerg|alert|crit|err|warning|notice|info|debug|all)</regex> </constraint> <constraintErrorMessage>Invalid loglevel</constraintErrorMessage> <valueHelp> <format>emerg</format> <description>Emergency messages</description> </valueHelp> <valueHelp> <format>alert</format> <description>Urgent messages</description> </valueHelp> <valueHelp> <format>crit</format> <description>Critical messages</description> </valueHelp> <valueHelp> <format>err</format> <description>Error messages</description> </valueHelp> <valueHelp> <format>warning</format> <description>Warning messages</description> </valueHelp> <valueHelp> <format>notice</format> <description>Messages for further investigation</description> </valueHelp> <valueHelp> <format>info</format> <description>Informational messages</description> </valueHelp> <valueHelp> <format>debug</format> <description>Debug messages</description> </valueHelp> <valueHelp> <format>all</format> <description>Log everything</description> </valueHelp> </properties> </leafNode> </children> </tagNode> </children> </tagNode> <node name="global"> <properties> <help>Logging to system standard location</help> </properties> <children> <node name="archive"> <properties> <help>Log file size and rotation characteristics</help> </properties> <children> <leafNode name="file"> <properties> <help>Number of saved files (default is 5)</help> <constraint> <regex>^[0-9]+</regex> </constraint> <constraintErrorMessage>illegal characters in number of files</constraintErrorMessage> </properties> </leafNode> <leafNode name="size"> <properties> <help>Size of log files (in kbytes, default is 256)</help> <constraint> <regex>^[0-9]+</regex> </constraint> <constraintErrorMessage>illegal characters in size</constraintErrorMessage> </properties> </leafNode> </children> </node> <tagNode name="facility"> <properties> <help>Facility for logging</help> <completionHelp> <list>auth authpriv cron daemon kern lpr mail mark news protocols security syslog user uucp local0 local1 local2 local3 local4 local5 local6 local7 all</list> </completionHelp> <constraint> <regex>(auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local0|local1|local2|local3|local4|local5|local6|local7|all)</regex> </constraint> <constraintErrorMessage>Invalid facility type</constraintErrorMessage> <valueHelp> <format>all</format> <description>All facilities excluding "mark"</description> </valueHelp> <valueHelp> <format>auth</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>authpriv</format> <description>Non-system authorization</description> </valueHelp> <valueHelp> <format>cron</format> <description>Cron daemon</description> </valueHelp> <valueHelp> <format>daemon</format> <description>System daemons</description> </valueHelp> <valueHelp> <format>kern</format> <description>Kernel</description> </valueHelp> <valueHelp> <format>lpr</format> <description>Line printer spooler</description> </valueHelp> <valueHelp> <format>mail</format> <description>Mail subsystem</description> </valueHelp> <valueHelp> <format>mark</format> <description>Timestamp</description> </valueHelp> <valueHelp> <format>news</format> <description>USENET subsystem</description> </valueHelp> <valueHelp> <format>protocols</format> <description>depricated will be set to local7</description> </valueHelp> <valueHelp> <format>security</format> <description>depricated will be set to auth</description> </valueHelp> <valueHelp> <format>syslog</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>user</format> <description>Application processes</description> </valueHelp> <valueHelp> <format>uucp</format> <description>UUCP subsystem</description> </valueHelp> <valueHelp> <format>local0</format> <description>Local facility 0</description> </valueHelp> <valueHelp> <format>local1</format> <description>Local facility 1</description> </valueHelp> <valueHelp> <format>local2</format> <description>Local facility 2</description> </valueHelp> <valueHelp> <format>local3</format> <description>Local facility 3</description> </valueHelp> <valueHelp> <format>local4</format> <description>Local facility 4</description> </valueHelp> <valueHelp> <format>local5</format> <description>Local facility 5</description> </valueHelp> <valueHelp> <format>local6</format> <description>Local facility 6</description> </valueHelp> <valueHelp> <format>local7</format> <description>Local facility 7</description> </valueHelp> </properties> <children> <leafNode name="level"> <properties> <help>Logging level</help> <completionHelp> <list>emerg alert crit err warning notice info debug all</list> </completionHelp> <constraint> <regex>(emerg|alert|crit|err|warning|notice|info|debug|all)</regex> </constraint> <constraintErrorMessage>Invalid loglevel</constraintErrorMessage> <valueHelp> <format>emerg</format> <description>Emergency messages</description> </valueHelp> <valueHelp> <format>alert</format> <description>Urgent messages</description> </valueHelp> <valueHelp> <format>crit</format> <description>Critical messages</description> </valueHelp> <valueHelp> <format>err</format> <description>Error messages</description> </valueHelp> <valueHelp> <format>warning</format> <description>Warning messages</description> </valueHelp> <valueHelp> <format>notice</format> <description>Messages for further investigation</description> </valueHelp> <valueHelp> <format>info</format> <description>Informational messages</description> </valueHelp> <valueHelp> <format>debug</format> <description>Debug messages</description> </valueHelp> <valueHelp> <format>all</format> <description>Log everything</description> </valueHelp> </properties> </leafNode> </children> </tagNode> <node name="marker"> <properties> <help>mark messages sent to syslog</help> </properties> <children> <leafNode name="interval"> <properties> <help>time interval how often a mark message is being sent in seconds (default: 1200)</help> <constraint> <validator name="numeric" argument="--positive"/> </constraint> </properties> </leafNode> </children> </node> <leafNode name ="preserve-fqdn"> <properties> <help>uses FQDN for logging</help> <valueless /> </properties> </leafNode> </children> </node> <tagNode name="file"> <properties> <help>Logging to a file</help> <constraint> <regex>^[a-zA-Z0-9\-_.]{1,255}</regex> </constraint> <constraintErrorMessage>illegal characters in filename or filename longer than 255 characters</constraintErrorMessage> </properties> <children> <node name="archive"> <properties> <help>Log file size and rotation characteristics</help> </properties> <children> <leafNode name="file"> <properties> <help>Number of saved files (default is 5)</help> <constraint> <regex>^[0-9]+</regex> </constraint> <constraintErrorMessage>illegal characters in number of files</constraintErrorMessage> </properties> </leafNode> <leafNode name="size"> <properties> <help>Size of log files (in kbytes, default is 256)</help> <constraint> <regex>^[0-9]+</regex> </constraint> <constraintErrorMessage>illegal characters in size</constraintErrorMessage> </properties> </leafNode> </children> </node> <tagNode name="facility"> <properties> <help>Facility for logging</help> <completionHelp> <list>auth authpriv cron daemon kern lpr mail mark news protocols security syslog user uucp local0 local1 local2 local3 local4 local5 local6 local7 all</list> </completionHelp> <constraint> <regex>(auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local0|local1|local2|local3|local4|local5|local6|local7|all)</regex> </constraint> <constraintErrorMessage>Invalid facility type</constraintErrorMessage> <valueHelp> <format>all</format> <description>All facilities excluding "mark"</description> </valueHelp> <valueHelp> <format>auth</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>authpriv</format> <description>Non-system authorization</description> </valueHelp> <valueHelp> <format>cron</format> <description>Cron daemon</description> </valueHelp> <valueHelp> <format>daemon</format> <description>System daemons</description> </valueHelp> <valueHelp> <format>kern</format> <description>Kernel</description> </valueHelp> <valueHelp> <format>lpr</format> <description>Line printer spooler</description> </valueHelp> <valueHelp> <format>mail</format> <description>Mail subsystem</description> </valueHelp> <valueHelp> <format>mark</format> <description>Timestamp</description> </valueHelp> <valueHelp> <format>news</format> <description>USENET subsystem</description> </valueHelp> <valueHelp> <format>protocols</format> <description>depricated will be set to local7</description> </valueHelp> <valueHelp> <format>security</format> <description>depricated will be set to auth</description> </valueHelp> <valueHelp> <format>syslog</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>user</format> <description>Application processes</description> </valueHelp> <valueHelp> <format>uucp</format> <description>UUCP subsystem</description> </valueHelp> <valueHelp> <format>local0</format> <description>Local facility 0</description> </valueHelp> <valueHelp> <format>local1</format> <description>Local facility 1</description> </valueHelp> <valueHelp> <format>local2</format> <description>Local facility 2</description> </valueHelp> <valueHelp> <format>local3</format> <description>Local facility 3</description> </valueHelp> <valueHelp> <format>local4</format> <description>Local facility 4</description> </valueHelp> <valueHelp> <format>local5</format> <description>Local facility 5</description> </valueHelp> <valueHelp> <format>local6</format> <description>Local facility 6</description> </valueHelp> <valueHelp> <format>local7</format> <description>Local facility 7</description> </valueHelp> </properties> <children> <leafNode name="level"> <properties> <help>Logging level</help> <completionHelp> <list>emerg alert crit err warning notice info debug all</list> </completionHelp> <constraint> <regex>(emerg|alert|crit|err|warning|notice|info|debug|all)</regex> </constraint> <constraintErrorMessage>Invalid loglevel</constraintErrorMessage> <valueHelp> <format>emerg</format> <description>Emergency messages</description> </valueHelp> <valueHelp> <format>alert</format> <description>Urgent messages</description> </valueHelp> <valueHelp> <format>crit</format> <description>Critical messages</description> </valueHelp> <valueHelp> <format>err</format> <description>Error messages</description> </valueHelp> <valueHelp> <format>warning</format> <description>Warning messages</description> </valueHelp> <valueHelp> <format>notice</format> <description>Messages for further investigation</description> </valueHelp> <valueHelp> <format>info</format> <description>Informational messages</description> </valueHelp> <valueHelp> <format>debug</format> <description>Debug messages</description> </valueHelp> <valueHelp> <format>all</format> <description>Log everything</description> </valueHelp> </properties> </leafNode> </children> </tagNode> </children> </tagNode> <node name="console"> <properties> <help>logging to serial console</help> </properties> <children> <tagNode name="facility"> <properties> <help>Facility for logging</help> <completionHelp> <list>auth authpriv cron daemon kern lpr mail mark news protocols security syslog user uucp local0 local1 local2 local3 local4 local5 local6 local7 all</list> </completionHelp> <constraint> <regex>(auth|authpriv|cron|daemon|kern|lpr|mail|mark|news|protocols|security|syslog|user|uucp|local0|local1|local2|local3|local4|local5|local6|local7|all)</regex> </constraint> <constraintErrorMessage>Invalid facility type</constraintErrorMessage> <valueHelp> <format>all</format> <description>All facilities excluding "mark"</description> </valueHelp> <valueHelp> <format>auth</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>authpriv</format> <description>Non-system authorization</description> </valueHelp> <valueHelp> <format>cron</format> <description>Cron daemon</description> </valueHelp> <valueHelp> <format>daemon</format> <description>System daemons</description> </valueHelp> <valueHelp> <format>kern</format> <description>Kernel</description> </valueHelp> <valueHelp> <format>lpr</format> <description>Line printer spooler</description> </valueHelp> <valueHelp> <format>mail</format> <description>Mail subsystem</description> </valueHelp> <valueHelp> <format>mark</format> <description>Timestamp</description> </valueHelp> <valueHelp> <format>news</format> <description>USENET subsystem</description> </valueHelp> <valueHelp> <format>protocols</format> <description>depricated will be set to local7</description> </valueHelp> <valueHelp> <format>security</format> <description>depricated will be set to auth</description> </valueHelp> <valueHelp> <format>syslog</format> <description>Authentication and authorization</description> </valueHelp> <valueHelp> <format>user</format> <description>Application processes</description> </valueHelp> <valueHelp> <format>uucp</format> <description>UUCP subsystem</description> </valueHelp> <valueHelp> <format>local0</format> <description>Local facility 0</description> </valueHelp> <valueHelp> <format>local1</format> <description>Local facility 1</description> </valueHelp> <valueHelp> <format>local2</format> <description>Local facility 2</description> </valueHelp> <valueHelp> <format>local3</format> <description>Local facility 3</description> </valueHelp> <valueHelp> <format>local4</format> <description>Local facility 4</description> </valueHelp> <valueHelp> <format>local5</format> <description>Local facility 5</description> </valueHelp> <valueHelp> <format>local6</format> <description>Local facility 6</description> </valueHelp> <valueHelp> <format>local7</format> <description>Local facility 7</description> </valueHelp> </properties> <children> <leafNode name="level"> <properties> <help>Logging level</help> <completionHelp> <list>emerg alert crit err warning notice info debug all</list> </completionHelp> <constraint> <regex>(emerg|alert|crit|err|warning|notice|info|debug|all)</regex> </constraint> <constraintErrorMessage>Invalid loglevel</constraintErrorMessage> <valueHelp> <format>emerg</format> <description>Emergency messages</description> </valueHelp> <valueHelp> <format>alert</format> <description>Urgent messages</description> </valueHelp> <valueHelp> <format>crit</format> <description>Critical messages</description> </valueHelp> <valueHelp> <format>err</format> <description>Error messages</description> </valueHelp> <valueHelp> <format>warning</format> <description>Warning messages</description> </valueHelp> <valueHelp> <format>notice</format> <description>Messages for further investigation</description> </valueHelp> <valueHelp> <format>info</format> <description>Informational messages</description> </valueHelp> <valueHelp> <format>debug</format> <description>Debug messages</description> </valueHelp> <valueHelp> <format>all</format> <description>Log everything</description> </valueHelp> </properties> </leafNode> </children> </tagNode> </children> </node> </children> </node> </children> </node> </interfaceDefinition> diff --git a/python/vyos/config.py b/python/vyos/config.py index 5bd8fb072..c7bd96e2f 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -1,461 +1,464 @@ # Copyright 2017, 2019 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. """ A library for reading VyOS running config data. This library is used internally by all config scripts of VyOS, but its API should be considered stable and safe to use in user scripts. Note that this module will not work outside VyOS. Node taxonomy ############# There are multiple types of config tree nodes in VyOS, each requires its own set of operations. *Leaf nodes* (such as "address" in interfaces) can have values, but cannot have children. Leaf nodes can have one value, multiple values, or no values at all. For example, "system host-name" is a single-value leaf node, "system name-server" is a multi-value leaf node (commonly abbreviated "multi node"), and "system ip disable-forwarding" is a valueless leaf node. Non-leaf nodes cannot have values, but they can have child nodes. They are divided into two classes depending on whether the names of their children are fixed or not. For example, under "system", the names of all valid child nodes are predefined ("login", "name-server" etc.). To the contrary, children of the "system task-scheduler task" node can have arbitrary names. Such nodes are called *tag nodes*. This terminology is confusing but we keep using it for lack of a better word. No one remembers if the "tag" in "task Foo" is "task" or "Foo", but the distinction is irrelevant in practice. Configuration modes ################### VyOS has two distinct modes: operational mode and configuration mode. When a user logins, the CLI is in the operational mode. In this mode, only the running (effective) config is accessible for reading. When a user enters the "configure" command, a configuration session is setup. Every config session has its *proposed* (or *session*) config built on top of the current running config. When changes are commited, if commit succeeds, the proposed config is merged into the running config. In configuration mode, "base" functions like `exists`, `return_value` return values from the session config, while functions prefixed "effective" return values from the running config. In operational mode, all functions return values from the running config. """ import os import re import json import subprocess import vyos.configtree class VyOSError(Exception): """ Raised on config access errors, most commonly if the type of a config tree node in the system does not match the type of operation. """ pass class Config(object): """ The class of config access objects. Internally, in the current implementation, this object is *almost* stateless, the only state it keeps is relative *config path* for convenient access to config subtrees. """ def __init__(self, session_env=None): self._cli_shell_api = "/bin/cli-shell-api" self._level = [] if session_env: self.__session_env = session_env else: self.__session_env = None # Running config can be obtained either from op or conf mode, it always succeeds # (if config system is initialized at all). if os.path.isfile('/tmp/vyos-config-status'): running_config_text = self._run([self._cli_shell_api, '--show-active-only', '--show-show-defaults', 'showConfig']) else: with open('/opt/vyatta/etc/config/config.boot') as f: running_config_text = f.read() # Session config ("active") only exists in conf mode. # In op mode, we'll just use the same running config for both active and session configs. if self.in_session(): session_config_text = self._run([self._cli_shell_api, '--show-working-only', '--show-show-defaults', 'showConfig']) else: session_config_text = running_config_text self._session_config = vyos.configtree.ConfigTree(session_config_text) self._running_config = vyos.configtree.ConfigTree(running_config_text) def _make_command(self, op, path): args = path.split() cmd = [self._cli_shell_api, op] + args return cmd def _make_path(self, path): # Backwards-compatibility stuff: original implementation used string paths # libvyosconfig paths are lists, but since node names cannot contain whitespace, # splitting at whitespace is reasonably safe. # It may cause problems with exists() when it's used for checking values, # since values may contain whitespace. if isinstance(path, str): path = re.split(r'\s+', path) elif isinstance(path, list): pass else: raise TypeError("Path must be a whitespace-separated string or a list") return (self._level + path) def _run(self, cmd): if self.__session_env: p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=self.__session_env) else: p = subprocess.Popen(cmd, stdout=subprocess.PIPE) out = p.stdout.read() p.wait() if p.returncode != 0: raise VyOSError() else: return out.decode('ascii') def set_level(self, path): """ Set the *edit level*, that is, a relative config tree path. Once set, all operations will be relative to this path, for example, after ``set_level("system")``, calling ``exists("name-server")`` is equivalent to calling ``exists("system name-server"`` without ``set_level``. Args: path (str): relative config path """ # Make sure there's always a space between default path (level) # and path supplied as method argument # XXX: for small strings in-place concatenation is not a problem if isinstance(path, str): - self._level = re.split(r'\s+', path) + if path: + self._level = re.split(r'\s*', path) + else: + self._level = [] elif isinstance(path, list): self._level = path else: raise TypeError("Level path must be either a whitespace-separated string or a list") def get_level(self): """ Gets the current edit level. Returns: str: current edit level """ return(self._level) def exists(self, path): """ Checks if a node with given path exists in the running or proposed config Returns: True if node exists, False otherwise Note: This function cannot be used outside a configuration sessions. In operational mode scripts, use ``exists_effective``. """ if self._session_config.exists(self._make_path(path)): return True else: # libvyosconfig exists() works only for _nodes_, not _values_ # libvyattacfg one also worked for values, so we emulate that case here path = re.split(r'\s+', path) path_without_value = path[:-1] path_str = " ".join(path_without_value) try: value = self._session_config.return_value(self._make_path(path_str)) return (value == path[-1]) except vyos.configtree.ConfigTreeError: # node doesn't exist at all return False def session_changed(self): """ Returns: True if the config session has uncommited changes, False otherwise. """ try: self._run(self._make_command('sessionChanged', '')) return True except VyOSError: return False def in_session(self): """ Returns: True if called from a configuration session, False otherwise. """ try: self._run(self._make_command('inSession', '')) return True except VyOSError: return False def show_config(self, path=[], default=None): """ Args: path (str list): Configuration tree path, or empty default (str): Default value to return Returns: str: working configuration """ if isinstance(path, list): path = " ".join(path) try: out = self._run(self._make_command('showConfig', path)) return out except VyOSError: return(default) def get_config_dict(self, path=[], effective=False): """ Args: path (str list): Configuration tree path, can be empty Returns: a dict representation of the config """ res = self.show_config(self._make_path(path)) config_tree = vyos.configtree.ConfigTree(res) config_dict = json.loads(config_tree.to_json()) return config_dict def is_multi(self, path): """ Args: path (str): Configuration tree path Returns: True if a node can have multiple values, False otherwise. Note: It also returns False if node doesn't exist. """ try: path = " ".join(self._level) + " " + path self._run(self._make_command('isMulti', path)) return True except VyOSError: return False def is_tag(self, path): """ Args: path (str): Configuration tree path Returns: True if a node is a tag node, False otherwise. Note: It also returns False if node doesn't exist. """ try: path = " ".join(self._level) + " " + path self._run(self._make_command('isTag', path)) return True except VyOSError: return False def is_leaf(self, path): """ Args: path (str): Configuration tree path Returns: True if a node is a leaf node, False otherwise. Note: It also returns False if node doesn't exist. """ try: path = " ".join(self._level) + " " + path self._run(self._make_command('isLeaf', path)) return True except VyOSError: return False def return_value(self, path, default=None): """ Retrieve a value of single-value leaf node in the running or proposed config Args: path (str): Configuration tree path default (str): Default value to return if node does not exist Returns: str: Node value, if it has any None: if node is valueless *or* if it doesn't exist Note: Due to the issue with treatment of valueless nodes by this function, valueless nodes should be checked with ``exists`` instead. This function cannot be used outside a configuration session. In operational mode scripts, use ``return_effective_value``. """ try: value = self._session_config.return_value(self._make_path(path)) except vyos.configtree.ConfigTreeError: value = None if not value: return(default) else: return(value) def return_values(self, path, default=[]): """ Retrieve all values of a multi-value leaf node in the running or proposed config Args: path (str): Configuration tree path Returns: str list: Node values, if it has any []: if node does not exist Note: This function cannot be used outside a configuration session. In operational mode scripts, use ``return_effective_values``. """ try: values = self._session_config.return_values(self._make_path(path)) except vyos.configtree.ConfigTreeError: values = [] if not values: return(default) else: return(values) def list_nodes(self, path, default=[]): """ Retrieve names of all children of a tag node in the running or proposed config Args: path (str): Configuration tree path Returns: string list: child node names """ try: nodes = self._session_config.list_nodes(self._make_path(path)) except vyos.configtree.ConfigTreeError: nodes = [] if not nodes: return(default) else: return(nodes) def exists_effective(self, path): """ Check if a node exists in the running (effective) config Args: path (str): Configuration tree path Returns: True if node exists in the running config, False otherwise Note: This function is safe to use in operational mode. In configuration mode, it ignores uncommited changes. """ return(self._running_config.exists(self._make_path(path))) def return_effective_value(self, path, default=None): """ Retrieve a values of a single-value leaf node in a running (effective) config Args: path (str): Configuration tree path default (str): Default value to return if node does not exist Returns: str: Node value """ try: value = self._running_config.return_value(self._make_path(path)) except vyos.configtree.ConfigTreeError: value = None if not value: return(default) else: return(value) def return_effective_values(self, path, default=[]): """ Retrieve all values of a multi-value node in a running (effective) config Args: path (str): Configuration tree path Returns: str list: A list of values """ try: values = self._running_config.return_values(self._make_path(path)) except vyos.configtree.ConfigTreeError: values = [] if not values: return(default) else: return(values) def list_effective_nodes(self, path, default=[]): """ Retrieve names of all children of a tag node in the running config Args: path (str): Configuration tree path Returns: str list: child node names Raises: VyOSError: if the node is not a tag node """ try: nodes = self._running_config.list_nodes(self._make_path(path)) except vyos.configtree.ConfigTreeError: nodes = [] if not nodes: return(default) else: return(nodes) diff --git a/python/vyos/ifconfig.py b/python/vyos/ifconfig.py index f487e6a5b..72f11c04d 100644 --- a/python/vyos/ifconfig.py +++ b/python/vyos/ifconfig.py @@ -1,1707 +1,1704 @@ # Copyright 2019 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. import os import re import jinja2 import json import glob import time import vyos.interfaces from vyos.validate import * from vyos.config import Config from vyos import ConfigError from ipaddress import IPv4Network, IPv6Address from netifaces import ifaddresses, AF_INET, AF_INET6 from subprocess import Popen, PIPE, STDOUT from time import sleep from os.path import isfile from tabulate import tabulate from hurry.filesize import size,alternative from datetime import timedelta dhclient_base = r'/var/lib/dhcp/dhclient_' dhcp_cfg = """ # generated by ifconfig.py option rfc3442-classless-static-routes code 121 = array of unsigned integer 8; timeout 60; retry 300; interface "{{ intf }}" { send host-name "{{ hostname }}"; {% if client_id -%} send dhcp-client-identifier "{{ client_id }}"; {% endif -%} {% if vendor_class_id -%} send vendor-class-identifier "{{ vendor_class_id }}"; {% endif -%} request subnet-mask, broadcast-address, routers, domain-name-servers, rfc3442-classless-static-routes, domain-name, interface-mtu; require subnet-mask; } """ dhcpv6_cfg = """ # generated by ifconfig.py interface "{{ intf }}" { request routers, domain-name-servers, domain-name; } """ class Interface: def __init__(self, ifname, type=None): """ This is the base interface class which supports basic IP/MAC address operations as well as DHCP(v6). Other interface which represent e.g. and ethernet bridge are implemented as derived classes adding all additional functionality. DEBUG: This class has embedded debugging (print) which can be enabled by creating the following file: vyos@vyos# touch /tmp/vyos.ifconfig.debug Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') """ self._ifname = str(ifname) if not os.path.exists('/sys/class/net/{}'.format(ifname)) and not type: raise Exception('interface "{}" not found'.format(self._ifname)) if not os.path.exists('/sys/class/net/{}'.format(self._ifname)): cmd = 'ip link add dev {} type {}'.format(self._ifname, type) self._cmd(cmd) # per interface DHCP config files self._dhcp_cfg_file = dhclient_base + self._ifname + '.conf' self._dhcp_pid_file = dhclient_base + self._ifname + '.pid' self._dhcp_lease_file = dhclient_base + self._ifname + '.leases' # per interface DHCPv6 config files self._dhcpv6_cfg_file = dhclient_base + self._ifname + '.v6conf' self._dhcpv6_pid_file = dhclient_base + self._ifname + '.v6pid' self._dhcpv6_lease_file = dhclient_base + self._ifname + '.v6leases' # DHCP options self._dhcp_options = { 'intf' : self._ifname, 'hostname' : '', 'client_id' : '', 'vendor_class_id' : '' } # DHCPv6 options self._dhcpv6_options = { 'intf' : self._ifname, 'dhcpv6_prm_only' : False, 'dhcpv6_temporary' : False } # list of assigned IP addresses self._addr = [] def _debug_msg(self, msg): if os.path.isfile('/tmp/vyos.ifconfig.debug'): print('DEBUG/{:<6} {}'.format(self._ifname, msg)) def _cmd(self, command): p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True) tmp = p.communicate()[0].strip() self._debug_msg("cmd '{}'".format(command)) if tmp.decode(): self._debug_msg("returned:\n{}".format(tmp.decode())) # do we need some error checking code here? return tmp.decode() def _read_sysfs(self, filename): """ Provide a single primitive w/ error checking for reading from sysfs. """ value = None with open(filename, 'r') as f: value = f.read().rstrip('\n') self._debug_msg("read '{}' < '{}'".format(value, filename)) return value def _write_sysfs(self, filename, value): """ Provide a single primitive w/ error checking for writing to sysfs. """ self._debug_msg("write '{}' > '{}'".format(value, filename)) with open(filename, 'w') as f: f.write(str(value)) return None def remove(self): """ Remove interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') >>> i.remove() """ # stop DHCP(v6) if running self._del_dhcp() self._del_dhcpv6() # remove all assigned IP addresses from interface - this is a bit redundant # as the kernel will remove all addresses on interface deletion, but we # can not delete ALL interfaces, see below for addr in self.get_addr(): self.del_addr(addr) # Ethernet interfaces can not be removed if type(self) == type(EthernetIf(self._ifname)): return # NOTE (Improvement): # after interface removal no other commands should be allowed # to be called and instead should raise an Exception: cmd = 'ip link del dev {}'.format(self._ifname) return self._cmd(cmd) def get_mtu(self): """ Get/set interface mtu in bytes. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').get_mtu() '1500' """ return self._read_sysfs('/sys/class/net/{}/mtu' .format(self._ifname)) def set_mtu(self, mtu): """ Get/set interface mtu in bytes. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_mtu(1400) >>> Interface('eth0').get_mtu() '1400' """ if mtu < 68 or mtu > 9000: raise ValueError('Invalid MTU size: "{}"'.format(mru)) return self._write_sysfs('/sys/class/net/{}/mtu' .format(self._ifname), mtu) def set_mac(self, mac): """ Set interface MAC (Media Access Contrl) address to given value. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_mac('00:50:ab:cd:ef:01') """ # on interface removal (ethernet) an empty string is passed - ignore it if not mac: return None # a mac address consits out of 6 octets octets = len(mac.split(':')) if octets != 6: raise ValueError('wrong number of MAC octets: {} '.format(octets)) # validate against the first mac address byte if it's a multicast # address if int(mac.split(':')[0], 16) & 1: raise ValueError('{} is a multicast MAC address'.format(mac)) # overall mac address is not allowed to be 00:00:00:00:00:00 if sum(int(i, 16) for i in mac.split(':')) == 0: raise ValueError('00:00:00:00:00:00 is not a valid MAC address') # check for VRRP mac address if mac.split(':')[0] == '0' and addr.split(':')[1] == '0' and mac.split(':')[2] == '94' and mac.split(':')[3] == '0' and mac.split(':')[4] == '1': raise ValueError('{} is a VRRP MAC address'.format(mac)) # Assemble command executed on system. Unfortunately there is no way # of altering the MAC address via sysfs cmd = 'ip link set dev {} address {}'.format(self._ifname, mac) return self._cmd(cmd) def set_arp_cache_tmo(self, tmo): """ Set ARP cache timeout value in seconds. Internal Kernel representation is in milliseconds. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_arp_cache_tmo(40) """ return self._write_sysfs('/proc/sys/net/ipv4/neigh/{0}/base_reachable_time_ms' .format(self._ifname), (int(tmo) * 1000)) def set_link_detect(self, link_filter): """ Configure kernel response in packets received on interfaces that are 'down' 0 - Allow packets to be received for the address on this interface even if interface is disabled or no carrier. 1 - Ignore packets received if interface associated with the incoming address is down. 2 - Ignore packets received if interface associated with the incoming address is down or has no carrier. Default value is 0. Note that some distributions enable it in startup scripts. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_link_detect(1) """ if int(link_filter) >= 0 and int(link_filter) <= 2: return self._write_sysfs('/proc/sys/net/ipv4/conf/{0}/link_filter' .format(self._ifname), link_filter) else: raise ValueError("Value out of range") def set_alias(self, ifalias=None): """ Set interface alias name used by e.g. SNMP Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_alias('VyOS upstream interface') to clear alias e.g. delete it use: >>> Interface('eth0').set_ifalias('') """ if not ifalias: # clear interface alias ifalias = '\0' self._write_sysfs('/sys/class/net/{}/ifalias' .format(self._ifname), ifalias) def get_state(self): """ Enable (up) / Disable (down) an interface Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').get_state() 'up' """ cmd = 'ip -json link show dev {}'.format(self._ifname) tmp = self._cmd(cmd) out = json.loads(tmp) return out[0]['operstate'].lower() def set_state(self, state): """ Enable (up) / Disable (down) an interface Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_state('down') >>> Interface('eth0').get_state() 'down' """ if state not in ['up', 'down']: raise ValueError('state must be "up" or "down"') # Assemble command executed on system. Unfortunately there is no way # to up/down an interface via sysfs cmd = 'ip link set dev {} {}'.format(self._ifname, state) return self._cmd(cmd) def set_proxy_arp(self, enable): """ Set per interface proxy ARP configuration Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_proxy_arp(1) """ if int(enable) >= 0 and int(enable) <= 1: return self._write_sysfs('/proc/sys/net/ipv4/conf/{}/proxy_arp' .format(self._ifname), enable) else: raise ValueError("Value out of range") def set_proxy_arp_pvlan(self, enable): """ Private VLAN proxy arp. Basically allow proxy arp replies back to the same interface (from which the ARP request/solicitation was received). This is done to support (ethernet) switch features, like RFC 3069, where the individual ports are NOT allowed to communicate with each other, but they are allowed to talk to the upstream router. As described in RFC 3069, it is possible to allow these hosts to communicate through the upstream router by proxy_arp'ing. Don't need to be used together with proxy_arp. This technology is known by different names: In RFC 3069 it is called VLAN Aggregation. Cisco and Allied Telesyn call it Private VLAN. Hewlett-Packard call it Source-Port filtering or port-isolation. Ericsson call it MAC-Forced Forwarding (RFC Draft). Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_proxy_arp_pvlan(1) """ if int(enable) >= 0 and int(enable) <= 1: return self._write_sysfs('/proc/sys/net/ipv4/conf/{}/proxy_arp_pvlan' .format(self._ifname), enable) else: raise ValueError("Value out of range") def get_addr(self): """ Retrieve assigned IPv4 and IPv6 addresses from given interface. This is done using the netifaces and ipaddress python modules. Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').get_addrs() ['172.16.33.30/24', 'fe80::20c:29ff:fe11:a174/64'] """ ipv4 = [] ipv6 = [] if AF_INET in ifaddresses(self._ifname).keys(): for v4_addr in ifaddresses(self._ifname)[AF_INET]: # we need to manually assemble a list of IPv4 address/prefix prefix = '/' + \ str(IPv4Network('0.0.0.0/' + v4_addr['netmask']).prefixlen) ipv4.append(v4_addr['addr'] + prefix) if AF_INET6 in ifaddresses(self._ifname).keys(): for v6_addr in ifaddresses(self._ifname)[AF_INET6]: # Note that currently expanded netmasks are not supported. That means # 2001:db00::0/24 is a valid argument while 2001:db00::0/ffff:ff00:: not. # see https://docs.python.org/3/library/ipaddress.html bits = bin( int(v6_addr['netmask'].replace(':', ''), 16)).count('1') prefix = '/' + str(bits) # we alsoneed to remove the interface suffix on link local # addresses v6_addr['addr'] = v6_addr['addr'].split('%')[0] ipv6.append(v6_addr['addr'] + prefix) return ipv4 + ipv6 def add_addr(self, addr): """ Add IP(v6) address to interface. Address is only added if it is not already assigned to that interface. addr: can be an IPv4 address, IPv6 address, dhcp or dhcpv6! IPv4: add IPv4 address to interface IPv6: add IPv6 address to interface dhcp: start dhclient (IPv4) on interface dhcpv6: start dhclient (IPv6) on interface Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.add_addr('192.0.2.1/24') >>> j.add_addr('2001:db8::ffff/64') >>> j.get_addr() ['192.0.2.1/24', '2001:db8::ffff/64'] """ # cache new IP address which is assigned to interface self._addr.append(addr) # we can not have both DHCP and static IPv4 addresses assigned to an interface if 'dhcp' in self._addr: for addr in self._addr: # do not change below 'if' ordering esle you will get an exception as: # ValueError: 'dhcp' does not appear to be an IPv4 or IPv6 address if addr != 'dhcp' and is_ipv4(addr): raise ConfigError("Can't configure both static IPv4 and DHCP address on the same interface") if addr == 'dhcp': self._set_dhcp() elif addr == 'dhcpv6': self._set_dhcpv6() else: if not is_intf_addr_assigned(self._ifname, addr): cmd = 'ip addr add "{}" dev "{}"'.format(addr, self._ifname) return self._cmd(cmd) def del_addr(self, addr): """ Delete IP(v6) address to interface. Address is only added if it is assigned to that interface. addr: can be an IPv4 address, IPv6 address, dhcp or dhcpv6! IPv4: delete IPv4 address from interface IPv6: delete IPv6 address from interface dhcp: stop dhclient (IPv4) on interface dhcpv6: stop dhclient (IPv6) on interface Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.add_addr('2001:db8::ffff/64') >>> j.add_addr('192.0.2.1/24') >>> j.get_addr() ['192.0.2.1/24', '2001:db8::ffff/64'] >>> j.del_addr('192.0.2.1/24') >>> j.get_addr() ['2001:db8::ffff/64'] """ if addr == 'dhcp': self._del_dhcp() elif addr == 'dhcpv6': self._del_dhcpv6() else: if is_intf_addr_assigned(self._ifname, addr): cmd = 'ip addr del "{}" dev "{}"'.format(addr, self._ifname) return self._cmd(cmd) def get_dhcp_options(self): """ Return dictionary with supported DHCP options. Dictionary should be altered and send back via set_dhcp_options() so those options are applied when DHCP is run. """ return self._dhcp_options def set_dhcp_options(self, options): """ Store new DHCP options used by next run of DHCP client. """ self._dhcp_options = options def get_dhcpv6_options(self): """ Return dictionary with supported DHCPv6 options. Dictionary should be altered and send back via set_dhcp_options() so those options are applied when DHCP is run. """ return self._dhcpv6_options def set_dhcpv6_options(self, options): """ Store new DHCP options used by next run of DHCP client. """ self._dhcpv6_options = options # replace dhcpv4/v6 with systemd.networkd? def _set_dhcp(self): """ Configure interface as DHCP client. The dhclient binary is automatically started in background! Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.set_dhcp() """ dhcp = self.get_dhcp_options() if not dhcp['hostname']: # read configured system hostname. # maybe change to vyos hostd client ??? with open('/etc/hostname', 'r') as f: dhcp['hostname'] = f.read().rstrip('\n') # render DHCP configuration tmpl = jinja2.Template(dhcp_cfg) dhcp_text = tmpl.render(dhcp) with open(self._dhcp_cfg_file, 'w') as f: f.write(dhcp_text) cmd = 'start-stop-daemon --start --quiet --pidfile ' + \ self._dhcp_pid_file cmd += ' --exec /sbin/dhclient --' # now pass arguments to dhclient binary cmd += ' -4 -nw -cf {} -pf {} -lf {} {}'.format( self._dhcp_cfg_file, self._dhcp_pid_file, self._dhcp_lease_file, self._ifname) return self._cmd(cmd) def _del_dhcp(self): """ De-configure interface as DHCP clinet. All auto generated files like pid, config and lease will be removed. Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.del_dhcp() """ pid = 0 if os.path.isfile(self._dhcp_pid_file): with open(self._dhcp_pid_file, 'r') as f: pid = int(f.read()) else: self._debug_msg('No DHCP client PID found') return None # stop dhclient, we need to call dhclient and tell it should release the # aquired IP address. tcpdump tells me: # 172.16.35.103.68 > 172.16.35.254.67: [bad udp cksum 0xa0cb -> 0xb943!] BOOTP/DHCP, Request from 00:50:56:9d:11:df, length 300, xid 0x620e6946, Flags [none] (0x0000) # Client-IP 172.16.35.103 # Client-Ethernet-Address 00:50:56:9d:11:df # Vendor-rfc1048 Extensions # Magic Cookie 0x63825363 # DHCP-Message Option 53, length 1: Release # Server-ID Option 54, length 4: 172.16.35.254 # Hostname Option 12, length 10: "vyos" # cmd = '/sbin/dhclient -cf {} -pf {} -lf {} -r {}'.format( self._dhcp_cfg_file, self._dhcp_pid_file, self._dhcp_lease_file, self._ifname) self._cmd(cmd) # cleanup old config file if os.path.isfile(self._dhcp_cfg_file): os.remove(self._dhcp_cfg_file) # cleanup old pid file if os.path.isfile(self._dhcp_pid_file): os.remove(self._dhcp_pid_file) # cleanup old lease file if os.path.isfile(self._dhcp_lease_file): os.remove(self._dhcp_lease_file) def _set_dhcpv6(self): """ Configure interface as DHCPv6 client. The dhclient binary is automatically started in background! Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.set_dhcpv6() """ dhcpv6 = self.get_dhcpv6_options() - import pprint - pprint.pprint(dhcpv6) # better save then sorry .. should be checked in interface script # but if you missed it we are safe! if dhcpv6['dhcpv6_prm_only'] and dhcpv6['dhcpv6_temporary']: raise Exception('DHCPv6 temporary and parameters-only options are mutually exclusive!') # render DHCP configuration tmpl = jinja2.Template(dhcpv6_cfg) dhcpv6_text = tmpl.render(dhcpv6) with open(self._dhcpv6_cfg_file, 'w') as f: f.write(dhcpv6_text) - if self.get_state() == 'up': - # https://bugs.launchpad.net/ubuntu/+source/ifupdown/+bug/1447715 - # - # wee need to wait for IPv6 DAD to finish once and interface is added - # this suxx :-( - sleep(5) - - # no longer accept router announcements on this interface - self._write_sysfs('/proc/sys/net/ipv6/conf/{}/accept_ra' - .format(self._ifname), 0) - - # assemble command-line to start DHCPv6 client (dhclient) - cmd = 'start-stop-daemon --start --quiet --pidfile ' + \ - self._dhcpv6_pid_file - cmd += ' --exec /sbin/dhclient --' - # now pass arguments to dhclient binary - cmd += ' -6 -nw -cf {} -pf {} -lf {}'.format( - self._dhcpv6_cfg_file, self._dhcpv6_pid_file, self._dhcpv6_lease_file) - - # add optional arguments - if dhcpv6['dhcpv6_prm_only']: - cmd += ' -S' - if dhcpv6['dhcpv6_temporary']: - cmd += ' -T' - - cmd += ' {}'.format(self._ifname) - return self._cmd(cmd) + # https://bugs.launchpad.net/ubuntu/+source/ifupdown/+bug/1447715 + # + # wee need to wait for IPv6 DAD to finish once and interface is added + # this suxx :-( + sleep(5) + + # no longer accept router announcements on this interface + self._write_sysfs('/proc/sys/net/ipv6/conf/{}/accept_ra' + .format(self._ifname), 0) + + # assemble command-line to start DHCPv6 client (dhclient) + cmd = 'start-stop-daemon --start --quiet --pidfile ' + \ + self._dhcpv6_pid_file + cmd += ' --exec /sbin/dhclient --' + # now pass arguments to dhclient binary + cmd += ' -6 -nw -cf {} -pf {} -lf {}'.format( + self._dhcpv6_cfg_file, self._dhcpv6_pid_file, self._dhcpv6_lease_file) + + # add optional arguments + if dhcpv6['dhcpv6_prm_only']: + cmd += ' -S' + if dhcpv6['dhcpv6_temporary']: + cmd += ' -T' + + cmd += ' {}'.format(self._ifname) + return self._cmd(cmd) def _del_dhcpv6(self): """ De-configure interface as DHCPv6 clinet. All auto generated files like pid, config and lease will be removed. Example: >>> from vyos.ifconfig import Interface >>> j = Interface('eth0') >>> j.del_dhcpv6() """ pid = 0 if os.path.isfile(self._dhcpv6_pid_file): with open(self._dhcpv6_pid_file, 'r') as f: pid = int(f.read()) else: self._debug_msg('No DHCPv6 client PID found') return None # stop dhclient cmd = 'start-stop-daemon --stop --quiet --pidfile {}'.format(self._dhcpv6_pid_file) self._cmd(cmd) # accept router announcements on this interface self._write_sysfs('/proc/sys/net/ipv6/conf/{}/accept_ra' .format(self._ifname), 1) # cleanup old config file if os.path.isfile(self._dhcpv6_cfg_file): os.remove(self._dhcpv6_cfg_file) # cleanup old pid file if os.path.isfile(self._dhcpv6_pid_file): os.remove(self._dhcpv6_pid_file) # cleanup old lease file if os.path.isfile(self._dhcpv6_lease_file): os.remove(self._dhcpv6_lease_file) def op_show_interface_stats(self): stats = self.get_interface_stats() rx = [['bytes','packets','errors','dropped','overrun','mcast'],[stats['rx_bytes'],stats['rx_packets'],stats['rx_errors'],stats['rx_dropped'],stats['rx_over_errors'],stats['multicast']]] tx = [['bytes','packets','errors','dropped','carrier','collisions'],[stats['tx_bytes'],stats['tx_packets'],stats['tx_errors'],stats['tx_dropped'],stats['tx_carrier_errors'],stats['collisions']]] output = "RX: \n" output += tabulate(rx,headers="firstrow",numalign="right",tablefmt="plain") output += "\n\nTX: \n" output += tabulate(tx,headers="firstrow",numalign="right",tablefmt="plain") print(' '.join(('\n'+output.lstrip()).splitlines(True))) def get_interface_stats(self): interface_stats = dict() devices = [f for f in glob.glob("/sys/class/net/**/statistics")] for dev_path in devices: metrics = [f for f in glob.glob(dev_path +"/**")] dev = re.findall(r"/sys/class/net/(.*)/statistics",dev_path)[0] dev_dict = dict() for metric_path in metrics: metric = metric_path.replace(dev_path+"/","") if isfile(metric_path): data = open(metric_path, 'r').read()[:-1] dev_dict[metric] = int(data) interface_stats[dev] = dev_dict return interface_stats[self._ifname] class LoopbackIf(Interface): """ The loopback device is a special, virtual network interface that your router uses to communicate with itself. """ def __init__(self, ifname): super().__init__(ifname, type='loopback') def remove(self): """ Loopback interface can not be deleted from operating system. We can only remove all assigned IP addresses. Example: >>> from vyos.ifconfig import Interface >>> i = LoopbackIf('lo').remove() """ # remove all assigned IP addresses from interface for addr in self.get_addr(): self.del_addr(addr) # question: do we also delerte the loopback address? 127.0.0.1/8 class DummyIf(Interface): """ A dummy interface is entirely virtual like, for example, the loopback interface. The purpose of a dummy interface is to provide a device to route packets through without actually transmitting them. """ def __init__(self, ifname): super().__init__(ifname, type='dummy') class STPIf(Interface): """ A spanning-tree capable interface. This applies only to bridge port member interfaces! """ def __init__(self, ifname): super().__init__(ifname) def set_path_cost(self, cost): """ Set interface path cost, only relevant for STP enabled interfaces Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_path_cost(4) """ if not os.path.isfile('/sys/class/net/{}/brport/path_cost' .format(self._ifname)): raise TypeError('{} is not a bridge port member'.format(self._ifname)) return self._write_sysfs('/sys/class/net/{}/brport/path_cost' .format(self._ifname), cost) def set_path_priority(self, priority): """ Set interface path priority, only relevant for STP enabled interfaces Example: >>> from vyos.ifconfig import Interface >>> Interface('eth0').set_path_priority(4) """ if not os.path.isfile('/sys/class/net/{}/brport/priority' .format(self._ifname)): raise TypeError('{} is not a bridge port member'.format(self._ifname)) return self._write_sysfs('/sys/class/net/{}/brport/priority' .format(self._ifname), priority) class BridgeIf(Interface): """ A bridge is a way to connect two Ethernet segments together in a protocol independent way. Packets are forwarded based on Ethernet address, rather than IP address (like a router). Since forwarding is done at Layer 2, all protocols can go transparently through a bridge. The Linux bridge code implements a subset of the ANSI/IEEE 802.1d standard. """ def __init__(self, ifname): super().__init__(ifname, type='bridge') def set_ageing_time(self, time): """ Set bridge interface MAC address aging time in seconds. Internal kernel representation is in centiseconds. Kernel default is 300 seconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').ageing_time(2) """ time = int(time) * 100 return self._write_sysfs('/sys/class/net/{}/bridge/ageing_time' .format(self._ifname), time) def set_forward_delay(self, time): """ Set bridge forwarding delay in seconds. Internal Kernel representation is in centiseconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').forward_delay(15) """ return self._write_sysfs('/sys/class/net/{}/bridge/forward_delay' .format(self._ifname), (int(time) * 100)) def set_hello_time(self, time): """ Set bridge hello time in seconds. Internal Kernel representation is in centiseconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').set_hello_time(2) """ return self._write_sysfs('/sys/class/net/{}/bridge/hello_time' .format(self._ifname), (int(time) * 100)) def set_max_age(self, time): """ Set bridge max message age in seconds. Internal Kernel representation is in centiseconds. Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').set_max_age(30) """ return self._write_sysfs('/sys/class/net/{}/bridge/max_age' .format(self._ifname), (int(time) * 100)) def set_priority(self, priority): """ Set bridge max aging time in seconds. Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').set_priority(8192) """ return self._write_sysfs('/sys/class/net/{}/bridge/priority' .format(self._ifname), priority) def set_stp(self, state): """ Set bridge STP (Spanning Tree) state. 0 -> STP disabled, 1 -> STP enabled Example: >>> from vyos.ifconfig import BridgeIf >>> BridgeIf('br0').set_stp(1) """ if int(state) >= 0 and int(state) <= 1: return self._write_sysfs('/sys/class/net/{}/bridge/stp_state' .format(self._ifname), state) else: raise ValueError("Value out of range") def set_multicast_querier(self, enable): """ Sets whether the bridge actively runs a multicast querier or not. When a bridge receives a 'multicast host membership' query from another network host, that host is tracked based on the time that the query was received plus the multicast query interval time. Use enable=1 to enable or enable=0 to disable Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').set_multicast_querier(1) """ if int(enable) >= 0 and int(enable) <= 1: return self._write_sysfs('/sys/class/net/{}/bridge/multicast_querier' .format(self._ifname), enable) else: raise ValueError("Value out of range") def add_port(self, interface): """ Add physical interface to bridge (member port) Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').add_port('eth0') >>> BridgeIf('br0').add_port('eth1') """ cmd = 'ip link set dev {} master {}'.format(interface, self._ifname) return self._cmd(cmd) def del_port(self, interface): """ Remove member port from bridge instance. Example: >>> from vyos.ifconfig import Interface >>> BridgeIf('br0').del_port('eth1') """ cmd = 'ip link set dev {} nomaster'.format(interface) return self._cmd(cmd) class VLANIf(Interface): """ This class handels the creation and removal of a VLAN interface. It serves as base class for BondIf and EthernetIf. """ def __init__(self, ifname, type=None): super().__init__(ifname, type) def remove(self): """ Remove interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') >>> i.remove() """ # Do we have sub interfaces (VLANs)? We apply a regex matching # subinterfaces (indicated by a .) of a parent interface. # # As interfaces need to be deleted "in order" starting from Q-in-Q # we delete them first. vlan_ifs = [f for f in os.listdir(r'/sys/class/net') \ if re.match(self._ifname + r'(?:\.\d+)(?:\.\d+)', f)] for vlan in vlan_ifs: Interface(vlan).remove() # After deleting all Q-in-Q interfaces delete other VLAN interfaces # which probably acted as parent to Q-in-Q or have been regular 802.1q # interface. vlan_ifs = [f for f in os.listdir(r'/sys/class/net') \ if re.match(self._ifname + r'(?:\.\d+)', f)] for vlan in vlan_ifs: Interface(vlan).remove() # All subinterfaces are now removed, continue on the physical interface super().remove() def add_vlan(self, vlan_id, ethertype='', ingress_qos='', egress_qos=''): """ A virtual LAN (VLAN) is any broadcast domain that is partitioned and isolated in a computer network at the data link layer (OSI layer 2). Use this function to create a new VLAN interface on a given physical interface. This function creates both 802.1q and 802.1ad (Q-in-Q) interfaces. Proto parameter is used to indicate VLAN type. A new object of type VLANIf is returned once the interface has been created. @param ethertype: If specified, create 802.1ad or 802.1q Q-in-Q VLAN interface @param ingress_qos: Defines a mapping of VLAN header prio field to the Linux internal packet priority on incoming frames. @param ingress_qos: Defines a mapping of Linux internal packet priority to VLAN header prio field but for outgoing frames. Example: >>> from vyos.ifconfig import VLANIf >>> i = VLANIf('eth0') >>> i.add_vlan(10) """ vlan_ifname = self._ifname + '.' + str(vlan_id) if not os.path.exists('/sys/class/net/{}'.format(vlan_ifname)): self._vlan_id = int(vlan_id) if ethertype: self._ethertype = ethertype ethertype = 'proto {}'.format(ethertype) # Optional ingress QOS mapping opt_i = '' if ingress_qos: opt_i = 'ingress-qos-map ' + ingress_qos # Optional egress QOS mapping opt_e = '' if egress_qos: opt_e = 'egress-qos-map ' + egress_qos # create interface in the system cmd = 'ip link add link {intf} name {intf}.{vlan} type vlan {proto} id {vlan} {opt_e} {opt_i}' \ .format(intf=self._ifname, vlan=self._vlan_id, proto=ethertype, opt_e=opt_e, opt_i=opt_i) self._cmd(cmd) # return new object mapping to the newly created interface # we can now work on this object for e.g. IP address setting # or interface description and so on return VLANIf(vlan_ifname) def del_vlan(self, vlan_id): """ Remove VLAN interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import VLANIf >>> i = VLANIf('eth0.10') >>> i.del_vlan() """ vlan_ifname = self._ifname + '.' + str(vlan_id) VLANIf(vlan_ifname).remove() class EthernetIf(VLANIf): """ Abstraction of a Linux Ethernet Interface """ def __init__(self, ifname): super().__init__(ifname) def get_driver_name(self): """ Return the driver name used by NIC. Some NICs don't support all features e.g. changing link-speed, duplex Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.get_driver_name() 'vmxnet3' """ link = os.readlink('/sys/class/net/{}/device/driver/module'.format(self._ifname)) return os.path.basename(link) def set_flow_control(self, enable): """ Changes the pause parameters of the specified Ethernet device. @param enable: true -> enable pause frames, false -> disable pause frames Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_flow_control(True) """ if enable not in ['on', 'off']: raise ValueError("Value out of range") if self.get_driver_name() in ['vmxnet3', 'virtio_net']: self._debug_msg('{} driver does not support changing flow control settings!' .format(self.get_driver_name())) return # Get current flow control settings: cmd = '/sbin/ethtool --show-pause {0}'.format(self._ifname) tmp = self._cmd(cmd) # The above command returns - with tabs: # # Pause parameters for eth0: # Autonegotiate: on # RX: off # TX: off if re.search("Autonegotiate:\ton", tmp): if enable == "on": # flowcontrol is already enabled - no need to re-enable it again # this will prevent the interface from flapping as applying the # flow-control settings will take the interface down and bring # it back up every time. return # Assemble command executed on system. Unfortunately there is no way # to change this setting via sysfs cmd = '/sbin/ethtool --pause {0} autoneg {1} tx {1} rx {1}'.format( self._ifname, enable) try: # An exception will be thrown if the settings are not changed return self._cmd(cmd) except CalledProcessError: pass def set_speed_duplex(self, speed, duplex): """ Set link speed in Mbit/s and duplex. @speed can be any link speed in MBit/s, e.g. 10, 100, 1000 auto @duplex can be half, full, auto Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_speed_duplex('auto', 'auto') """ if speed not in ['auto', '10', '100', '1000', '2500', '5000', '10000', '25000', '40000', '50000', '100000', '400000']: raise ValueError("Value out of range (speed)") if duplex not in ['auto', 'full', 'half']: raise ValueError("Value out of range (duplex)") if self.get_driver_name() in ['vmxnet3', 'virtio_net']: self._debug_msg('{} driver does not support changing speed/duplex settings!' .format(self.get_driver_name())) return # Get current speed and duplex settings: cmd = '/sbin/ethtool {0}'.format(self._ifname) tmp = self._cmd(cmd) if re.search("\tAuto-negotiation: on", tmp): if speed == 'auto' and duplex == 'auto': # bail out early as nothing is to change return else: # read in current speed and duplex settings cur_speed = 0 cur_duplex = '' for line in tmp.splitlines(): if line.lstrip().startswith("Speed:"): non_decimal = re.compile(r'[^\d.]+') cur_speed = non_decimal.sub('', line) continue if line.lstrip().startswith("Duplex:"): cur_duplex = line.split()[-1].lower() break if (cur_speed == speed) and (cur_duplex == duplex): # bail out early as nothing is to change return cmd = '/sbin/ethtool -s {}'.format(self._ifname) if speed == 'auto' or duplex == 'auto': cmd += ' autoneg on' else: cmd += ' speed {} duplex {} autoneg off'.format(speed, duplex) return self._cmd(cmd) def set_gro(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_gro('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} gro {}'.format(self._ifname, state) return self._cmd(cmd) def set_gso(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_gso('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} gso {}'.format(self._ifname, state) return self._cmd(cmd) def set_sg(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_sg('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} sg {}'.format(self._ifname, state) return self._cmd(cmd) def set_tso(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_tso('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} tso {}'.format(self._ifname, state) return self._cmd(cmd) def set_ufo(self, state): """ Example: >>> from vyos.ifconfig import EthernetIf >>> i = EthernetIf('eth0') >>> i.set_udp_offload('on') """ if state not in ['on', 'off']: raise ValueError('state must be "on" or "off"') cmd = '/sbin/ethtool -K {} ufo {}'.format(self._ifname, state) return self._cmd(cmd) class BondIf(VLANIf): """ The Linux bonding driver provides a method for aggregating multiple network interfaces into a single logical "bonded" interface. The behavior of the bonded interfaces depends upon the mode; generally speaking, modes provide either hot standby or load balancing services. Additionally, link integrity monitoring may be performed. """ def __init__(self, ifname): super().__init__(ifname, type='bond') def remove(self): """ Remove interface from operating system. Removing the interface deconfigures all assigned IP addresses and clear possible DHCP(v6) client processes. Example: >>> from vyos.ifconfig import Interface >>> i = Interface('eth0') >>> i.remove() """ # when a bond member gets deleted, all members are placed in A/D state # even when they are enabled inside CLI. This will make the config # and system look async. slave_list = [] for s in self.get_slaves(): slave = { 'ifname' : s, 'state': Interface(s).get_state() } slave_list.append(slave) # remove bond master which places members in disabled state super().remove() # replicate previous interface state before bond destruction back to # physical interface for slave in slave_list: i = Interface(slave['ifname']) i.set_state(slave['state']) def set_hash_policy(self, mode): """ Selects the transmit hash policy to use for slave selection in balance-xor, 802.3ad, and tlb modes. Possible values are: layer2, layer2+3, layer3+4, encap2+3, encap3+4. The default value is layer2 Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_hash_policy('layer2+3') """ if not mode in ['layer2', 'layer2+3', 'layer3+4', 'encap2+3', 'encap3+4']: raise ValueError("Value out of range") return self._write_sysfs('/sys/class/net/{}/bonding/xmit_hash_policy' .format(self._ifname), mode) def set_arp_interval(self, interval): """ Specifies the ARP link monitoring frequency in milliseconds. The ARP monitor works by periodically checking the slave devices to determine whether they have sent or received traffic recently (the precise criteria depends upon the bonding mode, and the state of the slave). Regular traffic is generated via ARP probes issued for the addresses specified by the arp_ip_target option. If ARP monitoring is used in an etherchannel compatible mode (modes 0 and 2), the switch should be configured in a mode that evenly distributes packets across all links. If the switch is configured to distribute the packets in an XOR fashion, all replies from the ARP targets will be received on the same link which could cause the other team members to fail. value of 0 disables ARP monitoring. The default value is 0. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_arp_interval('100') """ if int(interval) == 0: """ Specifies the MII link monitoring frequency in milliseconds. This determines how often the link state of each slave is inspected for link failures. A value of zero disables MII link monitoring. A value of 100 is a good starting point. """ return self._write_sysfs('/sys/class/net/{}/bonding/miimon' .format(self._ifname), interval) else: return self._write_sysfs('/sys/class/net/{}/bonding/arp_interval' .format(self._ifname), interval) def get_arp_ip_target(self): """ Specifies the IP addresses to use as ARP monitoring peers when arp_interval is > 0. These are the targets of the ARP request sent to determine the health of the link to the targets. Specify these values in ddd.ddd.ddd.ddd format. Multiple IP addresses must be separated by a comma. At least one IP address must be given for ARP monitoring to function. The maximum number of targets that can be specified is 16. The default value is no IP addresses. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').get_arp_ip_target() '192.0.2.1' """ return self._read_sysfs('/sys/class/net/{}/bonding/arp_ip_target' .format(self._ifname)) def set_arp_ip_target(self, target): """ Specifies the IP addresses to use as ARP monitoring peers when arp_interval is > 0. These are the targets of the ARP request sent to determine the health of the link to the targets. Specify these values in ddd.ddd.ddd.ddd format. Multiple IP addresses must be separated by a comma. At least one IP address must be given for ARP monitoring to function. The maximum number of targets that can be specified is 16. The default value is no IP addresses. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_arp_ip_target('192.0.2.1') >>> BondIf('bond0').get_arp_ip_target() '192.0.2.1' """ return self._write_sysfs('/sys/class/net/{}/bonding/arp_ip_target' .format(self._ifname), target) def add_port(self, interface): """ Enslave physical interface to bond. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').add_port('eth0') >>> BondIf('bond0').add_port('eth1') """ # An interface can only be added to a bond if it is in 'down' state. If # interface is in 'up' state, the following Kernel error will be thrown: # bond0: eth1 is up - this may be due to an out of date ifenslave. Interface(interface).set_state('down') return self._write_sysfs('/sys/class/net/{}/bonding/slaves' .format(self._ifname), '+' + interface) def del_port(self, interface): """ Remove physical port from bond Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').del_port('eth1') """ return self._write_sysfs('/sys/class/net/{}/bonding/slaves' .format(self._ifname), '-' + interface) def get_slaves(self): """ Return a list with all configured slave interfaces on this bond. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').get_slaves() ['eth1', 'eth2'] """ slaves = self._read_sysfs('/sys/class/net/{}/bonding/slaves' .format(self._ifname)) return list(map(str, slaves.split())) def set_primary(self, interface): """ A string (eth0, eth2, etc) specifying which slave is the primary device. The specified device will always be the active slave while it is available. Only when the primary is off-line will alternate devices be used. This is useful when one slave is preferred over another, e.g., when one slave has higher throughput than another. The primary option is only valid for active-backup, balance-tlb and balance-alb mode. Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_primary('eth2') """ if not interface: # reset primary interface interface = '\0' return self._write_sysfs('/sys/class/net/{}/bonding/primary' .format(self._ifname), interface) def set_mode(self, mode): """ Specifies one of the bonding policies. The default is balance-rr (round robin). Possible values are: balance-rr, active-backup, balance-xor, broadcast, 802.3ad, balance-tlb, balance-alb NOTE: the bonding mode can not be changed when the bond itself has slaves Example: >>> from vyos.ifconfig import BondIf >>> BondIf('bond0').set_mode('802.3ad') """ if not mode in [ 'balance-rr', 'active-backup', 'balance-xor', 'broadcast', '802.3ad', 'balance-tlb', 'balance-alb']: raise ValueError("Value out of range") return self._write_sysfs('/sys/class/net/{}/bonding/mode' .format(self._ifname), mode) class WireGuardIf(Interface): """ Wireguard interface class, contains a comnfig dictionary since wireguard VPN is being comnfigured via the wg command rather than writing the config into a file. Otherwise if a pre-shared key is used (symetric enryption key), it would we exposed within multiple files. Currently it's only within the config.boot if the config was saved. Example: >>> from vyos.ifconfig import WireGuardIf as wg_if >>> wg_intfc = wg_if("wg01") >>> print (wg_intfc.wg_config) {'private-key': None, 'keepalive': 0, 'endpoint': None, 'port': 0, 'allowed-ips': [], 'pubkey': None, 'fwmark': 0, 'psk': '/dev/null'} >>> wg_intfc.wg_config['keepalive'] = 100 >>> print (wg_intfc.wg_config) {'private-key': None, 'keepalive': 100, 'endpoint': None, 'port': 0, 'allowed-ips': [], 'pubkey': None, 'fwmark': 0, 'psk': '/dev/null'} """ def __init__(self, ifname): super().__init__(ifname, type='wireguard') self.config = { 'port': 0, 'private-key': None, 'pubkey': None, 'psk': '/dev/null', 'allowed-ips': [], 'fwmark': 0x00, 'endpoint': None, 'keepalive': 0 } def update(self): if not self.config['private-key']: raise ValueError("private key required") else: # fmask permission check? pass cmd = "wg set {} ".format(self._ifname) cmd += "listen-port {} ".format(self.config['port']) cmd += "fwmark {} ".format(str(self.config['fwmark'])) cmd += "private-key {} ".format(self.config['private-key']) cmd += "peer {} ".format(self.config['pubkey']) cmd += " preshared-key {} ".format(self.config['psk']) cmd += " allowed-ips " for aip in self.config['allowed-ips']: if aip != self.config['allowed-ips'][-1]: cmd += aip + "," else: cmd += aip if self.config['endpoint']: cmd += " endpoint {}".format(self.config['endpoint']) cmd += " persistent-keepalive {}".format(self.config['keepalive']) self._cmd(cmd) # remove psk since it isn't required anymore and is saved in the cli # config only !! if self.config['psk'] != '/dev/null': if os.path.exists(self.config['psk']): os.remove(self.config['psk']) def remove_peer(self, peerkey): """ Remove a peer of an interface, peers are identified by their public key. Giving it a readable name is a vyos feature, to remove a peer the pubkey and the interface is needed, to remove the entry. """ cmd = "wg set {0} peer {1} remove".format( self._ifname, str(peerkey)) return self._cmd(cmd) def op_show_interface(self): wgdump = vyos.interfaces.wireguard_dump().get(self._ifname,None) c = Config() c.set_level(["interfaces","wireguard",self._ifname]) description = c.return_effective_value(["description"]) ips = c.return_effective_values(["address"]) print ("interface: {}".format(self._ifname)) if (description): print (" description: {}".format(description)) if (ips): print (" address: {}".format(", ".join(ips))) print (" public key: {}".format(wgdump['public_key'])) print (" private key: (hidden)") print (" listening port: {}".format(wgdump['listen_port'])) print () for peer in c.list_effective_nodes(["peer"]): if wgdump['peers']: pubkey = c.return_effective_value(["peer",peer,"pubkey"]) if pubkey in wgdump['peers']: wgpeer = wgdump['peers'][pubkey] print (" peer: {}".format(peer)) print (" public key: {}".format(pubkey)) """ figure out if the tunnel is recently active or not """ status = "inactive" if (wgpeer['latest_handshake'] is None): """ no handshake ever """ status = "inactive" else: if int(wgpeer['latest_handshake']) > 0: delta = timedelta(seconds=int(time.time() - wgpeer['latest_handshake'])) print (" latest handshake: {}".format(delta)) if (time.time() - int(wgpeer['latest_handshake']) < (60*5)): """ Five minutes and the tunnel is still active """ status = "active" else: """ it's been longer than 5 minutes """ status = "inactive" elif int(wgpeer['latest_handshake']) == 0: """ no handshake ever """ status = "inactive" print (" status: {}".format(status)) if wgpeer['endpoint'] is not None: print (" endpoint: {}".format(wgpeer['endpoint'])) if wgpeer['allowed_ips'] is not None: print (" allowed ips: {}".format(",".join(wgpeer['allowed_ips']).replace(",",", "))) if wgpeer['transfer_rx'] > 0 or wgpeer['transfer_tx'] > 0: rx_size =size(wgpeer['transfer_rx'],system=alternative) tx_size =size(wgpeer['transfer_tx'],system=alternative) print (" transfer: {} received, {} sent".format(rx_size,tx_size)) if wgpeer['persistent_keepalive'] is not None: print (" persistent keepalive: every {} seconds".format(wgpeer['persistent_keepalive'])) print() super().op_show_interface_stats() class VXLANIf(Interface, ): """ The VXLAN protocol is a tunnelling protocol designed to solve the problem of limited VLAN IDs (4096) in IEEE 802.1q. With VXLAN the size of the identifier is expanded to 24 bits (16777216). VXLAN is described by IETF RFC 7348, and has been implemented by a number of vendors. The protocol runs over UDP using a single destination port. This document describes the Linux kernel tunnel device, there is also a separate implementation of VXLAN for Openvswitch. Unlike most tunnels, a VXLAN is a 1 to N network, not just point to point. A VXLAN device can learn the IP address of the other endpoint either dynamically in a manner similar to a learning bridge, or make use of statically-configured forwarding entries. For more information please refer to: https://www.kernel.org/doc/Documentation/networking/vxlan.txt """ def __init__(self, ifname, config=''): if config: self._ifname = ifname if not os.path.exists('/sys/class/net/{}'.format(self._ifname)): # we assume that by default a multicast interface is created group = 'group {}'.format(config['group']) # if remote host is specified we ignore the multicast address if config['remote']: group = 'remote {}'.format(config['remote']) # an underlay device is not always specified dev = '' if config['dev']: dev = 'dev {}'.format(config['dev']) cmd = 'ip link add {intf} type vxlan id {vni} {grp_rem} {dev} dstport {port}' \ .format(intf=self._ifname, vni=config['vni'], grp_rem=group, dev=dev, port=config['port']) self._cmd(cmd) super().__init__(ifname, type='vxlan') @staticmethod def get_config(): """ VXLAN interfaces require a configuration when they are added using iproute2. This static method will provide the configuration dictionary used by this class. Example: >> dict = VXLANIf().get_config() """ config = { 'vni': 0, 'dev': '', 'group': '', 'port': 8472, # The Linux implementation of VXLAN pre-dates # the IANA's selection of a standard destination port 'remote': '' } return config class GeneveIf(Interface, ): """ Geneve: Generic Network Virtualization Encapsulation For more information please refer to: https://tools.ietf.org/html/draft-gross-geneve-00 https://www.redhat.com/en/blog/what-geneve https://developers.redhat.com/blog/2019/05/17/an-introduction-to-linux-virtual-interfaces-tunnels/#geneve https://lwn.net/Articles/644938/ """ def __init__(self, ifname, config=''): if config: self._ifname = ifname if not os.path.exists('/sys/class/net/{}'.format(self._ifname)): cmd = 'ip link add name {} type geneve id {} remote {}' \ .format(self._ifname, config['vni'], config['remote']) self._cmd(cmd) super().__init__(ifname, type='geneve') @staticmethod def get_config(): """ GENEVE interfaces require a configuration when they are added using iproute2. This static method will provide the configuration dictionary used by this class. Example: >> dict = GeneveIf().get_config() """ config = { 'vni': 0, 'remote': '' } return config diff --git a/python/vyos/validate.py b/python/vyos/validate.py index 258f7f76a..3f3166022 100644 --- a/python/vyos/validate.py +++ b/python/vyos/validate.py @@ -1,146 +1,152 @@ # Copyright 2018 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. import netifaces import ipaddress def is_ip(addr): """ Check addr if it is an IPv4 or IPv6 address """ return is_ipv4(addr) or is_ipv6(addr) def is_ipv4(addr): """ Check addr if it is an IPv4 address/network. Returns True/False """ # With the below statement we can check for IPv4 networks and host # addresses at the same time - if ipaddress.ip_address(addr.split(r'/')[0]).version == 4: - return True - else: - return False + try: + if ipaddress.ip_address(addr.split(r'/')[0]).version == 4: + return True + except: + pass + + return False def is_ipv6(addr): """ Check addr if it is an IPv6 address/network. Returns True/False """ # With the below statement we can check for IPv4 networks and host # addresses at the same time - if ipaddress.ip_network(addr.split(r'/')[0]).version == 6: - return True - else: - return False + try: + if ipaddress.ip_network(addr.split(r'/')[0]).version == 6: + return True + except: + pass + + return False def is_intf_addr_assigned(intf, addr): """ Verify if the given IPv4/IPv6 address is assigned to specific interface. It can check both a single IP address (e.g. 192.0.2.1 or a assigned CIDR address 192.0.2.1/24. """ # determine IP version (AF_INET or AF_INET6) depending on passed address addr_type = netifaces.AF_INET if is_ipv6(addr): addr_type = netifaces.AF_INET6 # check if the requested address type is configured at all try: netifaces.ifaddresses(intf) except ValueError as e: print(e) return False if addr_type in netifaces.ifaddresses(intf).keys(): # Check every IP address on this interface for a match for ip in netifaces.ifaddresses(intf)[addr_type]: # Check if it matches to the address requested # If passed address contains a '/' indicating a normalized IP # address we have to take this into account, too if r'/' in addr: prefixlen = '' if is_ipv6(addr): # Note that currently expanded netmasks are not supported. That means # 2001:db00::0/24 is a valid argument while 2001:db00::0/ffff:ff00:: not. # see https://docs.python.org/3/library/ipaddress.html bits = bin( int(ip['netmask'].replace(':',''), 16) ).count('1') prefixlen = '/' + str(bits) else: prefixlen = '/' + str(ipaddress.IPv4Network('0.0.0.0/' + ip['netmask']).prefixlen) # construct temporary variable holding IPv6 address and netmask # in CIDR notation tmp = ip['addr'] + prefixlen if addr == tmp: return True elif ip['addr'] == addr: return True return False def is_addr_assigned(addr): """ Verify if the given IPv4/IPv6 address is assigned to any interface """ for intf in netifaces.interfaces(): tmp = is_intf_addr_assigned(intf, addr) if tmp == True: return True return False def is_subnet_connected(subnet, primary=False): """ Verify is the given IPv4/IPv6 subnet is connected to any interface on this system. primary check if the subnet is reachable via the primary IP address of this interface, or in other words has a broadcast address configured. ISC DHCP for instance will complain if it should listen on non broadcast interfaces. Return True/False """ # determine IP version (AF_INET or AF_INET6) depending on passed address addr_type = netifaces.AF_INET if is_ipv6(subnet): addr_type = netifaces.AF_INET6 for interface in netifaces.interfaces(): # check if the requested address type is configured at all if addr_type not in netifaces.ifaddresses(interface).keys(): continue # An interface can have multiple addresses, but some software components # only support the primary address :( if primary: ip = netifaces.ifaddresses(interface)[addr_type][0]['addr'] if ipaddress.ip_address(ip) in ipaddress.ip_network(subnet): return True else: # Check every assigned IP address if it is connected to the subnet # in question for ip in netifaces.ifaddresses(interface)[addr_type]: # remove interface extension (e.g. %eth0) that gets thrown on the end of _some_ addrs addr = ip['addr'].split('%')[0] if ipaddress.ip_address(addr) in ipaddress.ip_network(subnet): return True return False diff --git a/src/conf_mode/dynamic_dns.py b/src/conf_mode/dynamic_dns.py index 7c3b9ff6a..9ba8659a1 100755 --- a/src/conf_mode/dynamic_dns.py +++ b/src/conf_mode/dynamic_dns.py @@ -1,284 +1,289 @@ #!/usr/bin/env python3 # # Copyright (C) 2018-2019 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 sys import jinja2 from vyos.config import Config from vyos import ConfigError config_file = r'/etc/ddclient/ddclient.conf' cache_file = r'/var/cache/ddclient/ddclient.cache' pid_file = r'/var/run/ddclient/ddclient.pid' config_tmpl = """ ### Autogenerated by dynamic_dns.py ### daemon=1m syslog=yes ssl=yes pid={{ pid_file }} cache={{ cache_file }} {% for interface in interfaces -%} # # ddclient configuration for interface "{{ interface.interface }}": # {% if interface.web_url -%} use=web, web='{{ interface.web_url}}' {%- if interface.web_skip %}, web-skip='{{ interface.web_skip }}'{% endif %} {% else -%} use=if, if={{ interface.interface }} {% endif -%} {% for rfc in interface.rfc2136 -%} {% for record in rfc.record %} # RFC2136 dynamic DNS configuration for {{ record }}.{{ rfc.zone }} server={{ rfc.server }} protocol=nsupdate password={{ rfc.keyfile }} ttl={{ rfc.ttl }} zone={{ rfc.zone }} {{ record }} {% endfor -%} {% endfor -%} {% for srv in interface.service %} {% for host in srv.host %} # DynDNS provider configuration for {{ host }} protocol={{ srv.protocol }}, max-interval=28d, login={{ srv.login }}, password='{{ srv.password }}', {% if srv.server -%} server={{ srv.server }}, {% endif -%} {% if 'cloudflare' in srv.protocol -%} {% set zone = host.split('.',1) -%} zone={{ zone[1] }}, {% endif -%} {{ host }} {% endfor %} {% endfor %} {% endfor %} """ # Mapping of service name to service protocol default_service_protocol = { 'afraid': 'freedns', 'changeip': 'changeip', 'cloudflare': 'cloudflare', 'dnspark': 'dnspark', 'dslreports': 'dslreports1', 'dyndns': 'dyndns2', 'easydns': 'easydns', 'namecheap': 'namecheap', 'noip': 'noip', 'sitelutions': 'sitelutions', 'zoneedit': 'zoneedit1' } default_config_data = { 'interfaces': [], 'cache_file': cache_file, + 'deleted': False, 'pid_file': pid_file } def get_config(): dyndns = default_config_data conf = Config() if not conf.exists('service dns dynamic'): - return None + dyndns['deleted'] = True + return dyndns else: conf.set_level('service dns dynamic') for interface in conf.list_nodes('interface'): node = { 'interface': interface, 'rfc2136': [], 'service': [], 'web_skip': '', 'web_url': '' } # set config level to e.g. "service dns dynamic interface eth0" conf.set_level('service dns dynamic interface {0}'.format(interface)) # Handle RFC2136 - Dynamic Updates in the Domain Name System for rfc2136 in conf.list_nodes('rfc2136'): rfc = { 'name': rfc2136, 'keyfile': '', 'record': [], 'server': '', 'ttl': '600', 'zone': '' } if conf.exists('rfc2136 {0} key'.format(rfc2136)): rfc['keyfile'] = conf.return_value('rfc2136 {0} key'.format(rfc2136)) if conf.exists('rfc2136 {0} record'.format(rfc2136)): rfc['record'] = conf.return_values('rfc2136 {0} record'.format(rfc2136)) if conf.exists('rfc2136 {0} server'.format(rfc2136)): rfc['server'] = conf.return_value('rfc2136 {0} server'.format(rfc2136)) if conf.exists('rfc2136 {0} ttl'.format(rfc2136)): rfc['ttl'] = conf.return_value('rfc2136 {0} ttl'.format(rfc2136)) if conf.exists('rfc2136 {0} zone'.format(rfc2136)): rfc['zone'] = conf.return_value('rfc2136 {0} zone'.format(rfc2136)) node['rfc2136'].append(rfc) # Handle DynDNS service providers for service in conf.list_nodes('service'): srv = { 'provider': service, 'host': [], 'login': '', 'password': '', 'protocol': '', 'server': '', 'custom' : False } # preload protocol from default service mapping if service in default_service_protocol.keys(): srv['protocol'] = default_service_protocol[service] else: srv['custom'] = True if conf.exists('service {0} login'.format(service)): srv['login'] = conf.return_value('service {0} login'.format(service)) if conf.exists('service {0} host-name'.format(service)): srv['host'] = conf.return_values('service {0} host-name'.format(service)) if conf.exists('service {0} protocol'.format(service)): srv['protocol'] = conf.return_value('service {0} protocol'.format(service)) if conf.exists('service {0} password'.format(service)): srv['password'] = conf.return_value('service {0} password'.format(service)) if conf.exists('service {0} server'.format(service)): srv['server'] = conf.return_value('service {0} server'.format(service)) node['service'].append(srv) # Additional settings in CLI if conf.exists('use-web skip'): node['web_skip'] = conf.return_value('use-web skip') if conf.exists('use-web url'): node['web_url'] = conf.return_value('use-web url') dyndns['interfaces'].append(node) return dyndns def verify(dyndns): # bail out early - looks like removal from running config - if dyndns is None: + if dyndns['deleted']: return None # A 'node' corresponds to an interface for node in dyndns['interfaces']: # RFC2136 - configuration validation for rfc2136 in node['rfc2136']: if not rfc2136['record']: raise ConfigError('Set key for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface'])) if not rfc2136['zone']: raise ConfigError('Set zone for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface'])) if not rfc2136['keyfile']: raise ConfigError('Set keyfile for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface'])) else: if not os.path.isfile(rfc2136['keyfile']): raise ConfigError('Keyfile for service "{0}" to send DDNS updates for interface "{1}" does not exist'.format(rfc2136['name'], node['interface'])) if not rfc2136['server']: raise ConfigError('Set server for service "{0}" to send DDNS updates for interface "{1}"'.format(rfc2136['name'], node['interface'])) # DynDNS service provider - configuration validation for service in node['service']: if not service['host']: raise ConfigError('Set host-name for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface'])) if not service['login']: raise ConfigError('Set login for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface'])) if not service['password']: raise ConfigError('Set password for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface'])) if service['custom'] is True: if not service['protocol']: raise ConfigError('Set protocol for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface'])) if not service['server']: raise ConfigError('Set server for service "{0}" to send DDNS updates for interface "{1}"'.format(service['provider'], node['interface'])) return None def generate(dyndns): # bail out early - looks like removal from running config - if dyndns is None: + if dyndns['deleted']: + if os.path.exists(config_file): + os.unlink(config_file) + return None dirname = os.path.dirname(dyndns['pid_file']) if not os.path.exists(dirname): os.mkdir(dirname) dirname = os.path.dirname(config_file) if not os.path.exists(dirname): os.mkdir(dirname) tmpl = jinja2.Template(config_tmpl) config_text = tmpl.render(dyndns) with open(config_file, 'w') as f: f.write(config_text) return None def apply(dyndns): if os.path.exists(dyndns['cache_file']): os.unlink(dyndns['cache_file']) if os.path.exists('/etc/ddclient.conf'): os.unlink('/etc/ddclient.conf') - if dyndns is None: + if dyndns['deleted']: os.system('/etc/init.d/ddclient stop') if os.path.exists(dyndns['pid_file']): os.unlink(dyndns['pid_file']) else: os.system('/etc/init.d/ddclient restart') return None if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) sys.exit(1) diff --git a/src/conf_mode/interfaces-wireguard.py b/src/conf_mode/interfaces-wireguard.py index 013a07f32..cac911c8c 100755 --- a/src/conf_mode/interfaces-wireguard.py +++ b/src/conf_mode/interfaces-wireguard.py @@ -1,282 +1,287 @@ #!/usr/bin/env python3 # # Copyright (C) 2018 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 sys import os import re import subprocess from copy import deepcopy from netifaces import interfaces from vyos import ConfigError from vyos.config import Config from vyos.configdict import list_diff from vyos.ifconfig import WireGuardIf kdir = r'/config/auth/wireguard' def _check_kmod(): if not os.path.exists('/sys/module/wireguard'): if os.system('sudo modprobe wireguard') != 0: raise ConfigError("modprobe wireguard failed") def _migrate_default_keys(): if os.path.exists('{}/private.key'.format(kdir)) and not os.path.exists('{}/default/private.key'.format(kdir)): old_umask = os.umask(0o027) location = '{}/default'.format(kdir) subprocess.call(['sudo mkdir -p ' + location], shell=True) subprocess.call(['sudo chgrp vyattacfg ' + location], shell=True) subprocess.call(['sudo chmod 750 ' + location], shell=True) os.rename('{}/private.key'.format(kdir), '{}/private.key'.format(location)) os.rename('{}/public.key'.format(kdir), '{}/public.key'.format(location)) os.umask(old_umask) def get_config(): c = Config() if not c.exists('interfaces wireguard'): return None dflt_cnf = { 'intfc': '', 'addr': [], 'addr_remove': [], 'descr': '', 'lport': None, 'delete': False, 'state': 'up', 'fwmark': 0x00, 'mtu': 1420, 'peer': {}, 'peer_remove': [], 'pk': '{}/default/private.key'.format(kdir) } if os.getenv('VYOS_TAGNODE_VALUE'): ifname = str(os.environ['VYOS_TAGNODE_VALUE']) wg = deepcopy(dflt_cnf) wg['intfc'] = ifname wg['descr'] = ifname else: print("ERROR: VYOS_TAGNODE_VALUE undefined") sys.exit(1) c.set_level('interfaces wireguard') # interface removal state if not c.exists(ifname) and c.exists_effective(ifname): wg['delete'] = True if not wg['delete']: c.set_level('interfaces wireguard {}'.format(ifname)) if c.exists('address'): wg['addr'] = c.return_values('address') # determine addresses which need to be removed eff_addr = c.return_effective_values('address') wg['addr_remove'] = list_diff(eff_addr, wg['addr']) # ifalias description if c.exists('description'): wg['descr'] = c.return_value('description') # link state if c.exists('disable'): wg['state'] = 'down' # local port to listen on if c.exists('port'): wg['lport'] = c.return_value('port') # fwmark value if c.exists('fwmark'): wg['fwmark'] = c.return_value('fwmark') # mtu if c.exists('mtu'): wg['mtu'] = c.return_value('mtu') # private key if c.exists('private-key'): wg['pk'] = "{0}/{1}/private.key".format( kdir, c.return_value('private-key')) # peer removal, wg identifies peers by its pubkey peer_eff = c.list_effective_nodes('peer') peer_rem = list_diff(peer_eff, c.list_nodes('peer')) for p in peer_rem: wg['peer_remove'].append( c.return_effective_value('peer {} pubkey'.format(p))) # peer settings if c.exists('peer'): for p in c.list_nodes('peer'): if not c.exists('peer ' + p + ' disable'): wg['peer'].update( { p: { 'allowed-ips': [], 'endpoint': '', 'pubkey': '' } } ) # peer allowed-ips if c.exists('peer ' + p + ' allowed-ips'): wg['peer'][p]['allowed-ips'] = c.return_values( 'peer ' + p + ' allowed-ips') # peer endpoint if c.exists('peer ' + p + ' endpoint'): wg['peer'][p]['endpoint'] = c.return_value( 'peer ' + p + ' endpoint') # persistent-keepalive if c.exists('peer ' + p + ' persistent-keepalive'): wg['peer'][p]['persistent-keepalive'] = c.return_value( 'peer ' + p + ' persistent-keepalive') # preshared-key if c.exists('peer ' + p + ' preshared-key'): wg['peer'][p]['psk'] = c.return_value( 'peer ' + p + ' preshared-key') # peer pubkeys key_eff = c.return_effective_value( 'peer {peer} pubkey'.format(peer=p)) key_cfg = c.return_value( 'peer {peer} pubkey'.format(peer=p)) wg['peer'][p]['pubkey'] = key_cfg # on a pubkey change we need to remove the pubkey first # peers are identified by pubkey, so key update means # peer removal and re-add if key_eff != key_cfg and key_eff != None: wg['peer_remove'].append(key_cfg) + # if a peer is disabled, we have to exec a remove for it's pubkey + else: + peer_key = c.return_value('peer {peer} pubkey'.format(peer=p)) + wg['peer_remove'].append(peer_key) return wg def verify(c): if not c: return None if not os.path.exists(c['pk']): raise ConfigError( "No keys found, generate them by executing: \'run generate wireguard [keypair|named-keypairs]\'") if not c['delete']: if not c['addr']: raise ConfigError("ERROR: IP address required") if not c['peer']: raise ConfigError("ERROR: peer required") for p in c['peer']: if not c['peer'][p]['allowed-ips']: raise ConfigError("ERROR: allowed-ips required for peer " + p) if not c['peer'][p]['pubkey']: raise ConfigError("peer pubkey required for peer " + p) + def apply(c): # no wg configs left, remove all interface from system # maybe move it into ifconfig.py if not c: net_devs = os.listdir('/sys/class/net/') for dev in net_devs: if os.path.isdir('/sys/class/net/' + dev): buf = open('/sys/class/net/' + dev + '/uevent', 'r').read() if re.search("DEVTYPE=wireguard", buf, re.I | re.M): wg_intf = re.sub("INTERFACE=", "", re.search( "INTERFACE=.*", buf, re.I | re.M).group(0)) subprocess.call( ['ip l d dev ' + wg_intf + ' >/dev/null'], shell=True) return None # init wg class intfc = WireGuardIf(c['intfc']) # single interface removal if c['delete']: intfc.remove() return None # remove IP addresses for ip in c['addr_remove']: intfc.del_addr(ip) # add IP addresses for ip in c['addr']: intfc.add_addr(ip) # interface mtu intfc.set_mtu(int(c['mtu'])) # ifalias for snmp from description intfc.set_alias(str(c['descr'])) # remove peers if c['peer_remove']: for pkey in c['peer_remove']: intfc.remove_peer(pkey) # peer pubkey # setting up the wg interface intfc.config['private-key'] = c['pk'] for p in c['peer']: # peer pubkey intfc.config['pubkey'] = str(c['peer'][p]['pubkey']) # peer allowed-ips intfc.config['allowed-ips'] = c['peer'][p]['allowed-ips'] # local listen port if c['lport']: intfc.config['port'] = c['lport'] # fwmark if c['fwmark']: intfc.config['fwmark'] = c['fwmark'] # endpoint if c['peer'][p]['endpoint']: intfc.config['endpoint'] = c['peer'][p]['endpoint'] # persistent-keepalive if 'persistent-keepalive' in c['peer'][p]: intfc.config['keepalive'] = c['peer'][p]['persistent-keepalive'] # maybe move it into ifconfig.py # preshared-key - needs to be read from a file if 'psk' in c['peer'][p]: psk_file = '/config/auth/wireguard/psk' old_umask = os.umask(0o077) open(psk_file, 'w').write(str(c['peer'][p]['psk'])) os.umask(old_umask) intfc.config['psk'] = psk_file intfc.update() # interface state intfc.set_state(c['state']) return None if __name__ == '__main__': try: _check_kmod() _migrate_default_keys() c = get_config() verify(c) apply(c) except ConfigError as e: print(e) sys.exit(1) diff --git a/src/conf_mode/ipsec-settings.py b/src/conf_mode/ipsec-settings.py index 156bb2edd..331a62316 100755 --- a/src/conf_mode/ipsec-settings.py +++ b/src/conf_mode/ipsec-settings.py @@ -1,260 +1,273 @@ #!/usr/bin/env python3 # # Copyright (C) 2018 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 sys import re import os import jinja2 import syslog as sl +import time import vyos.config import vyos.defaults from vyos import ConfigError ra_conn_name = "remote-access" charon_conf_file = "/etc/strongswan.d/charon.conf" ipsec_secrets_flie = "/etc/ipsec.secrets" ipsec_ra_conn_file = "/etc/ipsec.d/tunnels/"+ra_conn_name ipsec_conf_flie = "/etc/ipsec.conf" ca_cert_path = '/etc/ipsec.d/cacerts' server_cert_path = '/etc/ipsec.d/certs' server_key_path = '/etc/ipsec.d/private' delim_ipsec_l2tp_begin = "### VyOS L2TP VPN Begin ###" delim_ipsec_l2tp_end = "### VyOS L2TP VPN End ###" +charon_pidfile = '/var/run/charon.pid' l2pt_ipsec_conf = ''' {{delim_ipsec_l2tp_begin}} include {{ipsec_ra_conn_file}} {{delim_ipsec_l2tp_end}} ''' l2pt_ipsec_secrets_conf = ''' {{delim_ipsec_l2tp_begin}} {% if ipsec_l2tp_auth_mode == 'pre-shared-secret' %} {{outside_addr}} %any : PSK "{{ipsec_l2tp_secret}}" {% elif ipsec_l2tp_auth_mode == 'x509' %} : RSA {{server_key_file_copied}} {% endif%} {{delim_ipsec_l2tp_end}} ''' l2tp_ipsec_ra_conn_conf = ''' {{delim_ipsec_l2tp_begin}} conn {{ra_conn_name}} type=transport left={{outside_addr}} leftsubnet=%dynamic[/1701] rightsubnet=%dynamic mark_in=%unique auto=add ike=aes256-sha1-modp1024,3des-sha1-modp1024,3des-sha1-modp1024! dpddelay=15 dpdtimeout=45 dpdaction=clear esp=aes256-sha1,3des-sha1! rekey=no {% if ipsec_l2tp_auth_mode == 'pre-shared-secret' %} authby=secret leftauth=psk rightauth=psk {% elif ipsec_l2tp_auth_mode == 'x509' %} authby=rsasig leftrsasigkey=%cert rightrsasigkey=%cert rightca=%same leftcert={{server_cert_file_copied}} {% endif %} ikelifetime={{ipsec_l2tp_ike_lifetime}} keylife={{ipsec_l2tp_lifetime}} {{delim_ipsec_l2tp_end}} ''' def get_config(): config = vyos.config.Config() data = {"install_routes": "yes"} if config.exists("vpn ipsec options disable-route-autoinstall"): data["install_routes"] = "no" if config.exists("vpn ipsec ipsec-interfaces interface"): data["ipsec_interfaces"] = config.return_values("vpn ipsec ipsec-interfaces interface") # Init config variables data["delim_ipsec_l2tp_begin"] = delim_ipsec_l2tp_begin data["delim_ipsec_l2tp_end"] = delim_ipsec_l2tp_end data["ipsec_ra_conn_file"] = ipsec_ra_conn_file data["ra_conn_name"] = ra_conn_name # Get l2tp ipsec settings data["ipsec_l2tp"] = False conf_ipsec_command = "vpn l2tp remote-access ipsec-settings " #last space is useful if config.exists(conf_ipsec_command): data["ipsec_l2tp"] = True # Authentication params if config.exists(conf_ipsec_command + "authentication mode"): data["ipsec_l2tp_auth_mode"] = config.return_value(conf_ipsec_command + "authentication mode") if config.exists(conf_ipsec_command + "authentication pre-shared-secret"): data["ipsec_l2tp_secret"] = config.return_value(conf_ipsec_command + "authentication pre-shared-secret") # mode x509 if config.exists(conf_ipsec_command + "authentication x509 ca-cert-file"): data["ipsec_l2tp_x509_ca_cert_file"] = config.return_value(conf_ipsec_command + "authentication x509 ca-cert-file") if config.exists(conf_ipsec_command + "authentication x509 crl-file"): data["ipsec_l2tp_x509_crl_file"] = config.return_value(conf_ipsec_command + "authentication x509 crl-file") if config.exists(conf_ipsec_command + "authentication x509 server-cert-file"): data["ipsec_l2tp_x509_server_cert_file"] = config.return_value(conf_ipsec_command + "authentication x509 server-cert-file") data["server_cert_file_copied"] = server_cert_path+"/"+re.search('\w+(?:\.\w+)*$', config.return_value(conf_ipsec_command + "authentication x509 server-cert-file")).group(0) if config.exists(conf_ipsec_command + "authentication x509 server-key-file"): data["ipsec_l2tp_x509_server_key_file"] = config.return_value(conf_ipsec_command + "authentication x509 server-key-file") data["server_key_file_copied"] = server_key_path+"/"+re.search('\w+(?:\.\w+)*$', config.return_value(conf_ipsec_command + "authentication x509 server-key-file")).group(0) if config.exists(conf_ipsec_command + "authentication x509 server-key-password"): data["ipsec_l2tp_x509_server_key_password"] = config.return_value(conf_ipsec_command + "authentication x509 server-key-password") # Common l2tp ipsec params if config.exists(conf_ipsec_command + "ike-lifetime"): data["ipsec_l2tp_ike_lifetime"] = config.return_value(conf_ipsec_command + "ike-lifetime") else: data["ipsec_l2tp_ike_lifetime"] = "3600" if config.exists(conf_ipsec_command + "lifetime"): data["ipsec_l2tp_lifetime"] = config.return_value(conf_ipsec_command + "lifetime") else: data["ipsec_l2tp_lifetime"] = "3600" if config.exists("vpn l2tp remote-access outside-address"): data['outside_addr'] = config.return_value('vpn l2tp remote-access outside-address') return data ### ipsec secret l2tp def write_ipsec_secrets(c): tmpl = jinja2.Template(l2pt_ipsec_secrets_conf, trim_blocks=True) l2pt_ipsec_secrets_txt = tmpl.render(c) old_umask = os.umask(0o077) open(ipsec_secrets_flie,'w').write(l2pt_ipsec_secrets_txt) os.umask(old_umask) sl.syslog(sl.LOG_NOTICE, ipsec_secrets_flie + ' written') ### ipsec remote access connection config def write_ipsec_ra_conn(c): tmpl = jinja2.Template(l2tp_ipsec_ra_conn_conf, trim_blocks=True) ipsec_ra_conn_txt = tmpl.render(c) old_umask = os.umask(0o077) open(ipsec_ra_conn_file,'w').write(ipsec_ra_conn_txt) os.umask(old_umask) sl.syslog(sl.LOG_NOTICE, ipsec_ra_conn_file + ' written') ### Remove config from file by delimiter def remove_confs(delim_begin, delim_end, conf_file): os.system("sed -i '/"+delim_begin+"/,/"+delim_end+"/d' "+conf_file) ### Append "include /path/to/ra_conn" to ipsec conf file def append_ipsec_conf(c): tmpl = jinja2.Template(l2pt_ipsec_conf, trim_blocks=True) l2pt_ipsec_conf_txt = tmpl.render(c) old_umask = os.umask(0o077) open(ipsec_conf_flie,'a').write(l2pt_ipsec_conf_txt) os.umask(old_umask) sl.syslog(sl.LOG_NOTICE, ipsec_conf_flie + ' written') ### Checking certificate storage and notice if certificate not in /config directory def check_cert_file_store(cert_name, file_path, dts_path): if not re.search('^\/config\/.+', file_path): print("Warning: \"" + file_path + "\" lies outside of /config/auth directory. It will not get preserved during image upgrade.") #Checking file existence if not os.path.isfile(file_path): raise ConfigError("L2TP VPN configuration error: Invalid "+cert_name+" \""+file_path+"\"") else: ### Cpy file to /etc/ipsec.d/certs/ /etc/ipsec.d/cacerts/ # todo make check ret = os.system('cp -f '+file_path+' '+dts_path) if ret: raise ConfigError("L2TP VPN configuration error: Cannot copy "+file_path) else: sl.syslog(sl.LOG_NOTICE, file_path + ' copied to '+dts_path) def verify(data): # l2tp ipsec check if data["ipsec_l2tp"]: # Checking dependecies for "authentication mode pre-shared-secret" if data.get("ipsec_l2tp_auth_mode") == "pre-shared-secret": if not data.get("ipsec_l2tp_secret"): raise ConfigError("pre-shared-secret required") if not data.get("outside_addr"): raise ConfigError("outside-address not defined") # Checking dependecies for "authentication mode x509" if data.get("ipsec_l2tp_auth_mode") == "x509": if not data.get("ipsec_l2tp_x509_server_key_file"): raise ConfigError("L2TP VPN configuration error: \"server-key-file\" not defined.") else: check_cert_file_store("server-key-file", data['ipsec_l2tp_x509_server_key_file'], server_key_path) if not data.get("ipsec_l2tp_x509_server_cert_file"): raise ConfigError("L2TP VPN configuration error: \"server-cert-file\" not defined.") else: check_cert_file_store("server-cert-file", data['ipsec_l2tp_x509_server_cert_file'], server_cert_path) if not data.get("ipsec_l2tp_x509_ca_cert_file"): raise ConfigError("L2TP VPN configuration error: \"ca-cert-file\" must be defined for X.509") else: check_cert_file_store("ca-cert-file", data['ipsec_l2tp_x509_ca_cert_file'], ca_cert_path) if not data.get('ipsec_interfaces'): raise ConfigError("L2TP VPN configuration error: \"vpn ipsec ipsec-interfaces\" must be specified.") def generate(data): tmpl_path = os.path.join(vyos.defaults.directories["data"], "templates", "ipsec") fs_loader = jinja2.FileSystemLoader(tmpl_path) env = jinja2.Environment(loader=fs_loader) charon_conf_tmpl = env.get_template("charon.tmpl") charon_conf = charon_conf_tmpl.render(data) with open(charon_conf_file, 'w') as f: f.write(charon_conf) if data["ipsec_l2tp"]: remove_confs(delim_ipsec_l2tp_begin, delim_ipsec_l2tp_end, ipsec_conf_flie) write_ipsec_secrets(data) write_ipsec_ra_conn(data) append_ipsec_conf(data) else: remove_confs(delim_ipsec_l2tp_begin, delim_ipsec_l2tp_end, ipsec_ra_conn_file) remove_confs(delim_ipsec_l2tp_begin, delim_ipsec_l2tp_end, ipsec_secrets_flie) remove_confs(delim_ipsec_l2tp_begin, delim_ipsec_l2tp_end, ipsec_conf_flie) -def apply(data): - # Do nothing - # StrongSWAN should only be restarted when actual tunnels are configured - # Restart ipsec for l2tp +def restart_ipsec(): os.system("ipsec restart >&/dev/null") + # counter for apply swanctl config + counter = 10 + while counter <= 10: + if os.path.exists(charon_pidfile): + os.system("swanctl -q >&/dev/null") + break + counter -=1 + time.sleep(1) + if counter == 0: + raise ConfigError('VPN configuration error: IPSec is not running.') + +def apply(data): + # Restart IPSec daemon + restart_ipsec() if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) sys.exit(1) diff --git a/src/conf_mode/syslog.py b/src/conf_mode/system-syslog.py similarity index 95% rename from src/conf_mode/syslog.py rename to src/conf_mode/system-syslog.py index c4f3d2c9c..4f0a54962 100755 --- a/src/conf_mode/syslog.py +++ b/src/conf_mode/system-syslog.py @@ -1,320 +1,334 @@ #!/usr/bin/env python3 # # Copyright (C) 2018 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 sys import os import re import jinja2 from vyos.config import Config from vyos import ConfigError # config templates # /etc/rsyslog.d/vyos-rsyslog.conf ### configs = ''' ## generated by syslog.py ## ## file based logging {% if files['global']['marker'] -%} $ModLoad immark {% if files['global']['marker-interval'] %} $MarkMessagePeriod {{files['global']['marker-interval']}} {% endif %} {% endif -%} {% if files['global']['preserver_fqdn'] -%} $PreserveFQDN on {% endif -%} {% for file in files %} $outchannel {{file}},{{files[file]['log-file']}},{{files[file]['max-size']}},{{files[file]['action-on-max-size']}} {{files[file]['selectors']}} :omfile:${{file}} {% endfor %} {% if console %} ## console logging {% for con in console %} {{console[con]['selectors']}} /dev/console {% endfor %} {% endif %} {% if hosts %} ## remote logging {% for host in hosts %} {% if hosts[host]['proto'] == 'tcp' %} +{% if hosts[host]['port'] %} +{{hosts[host]['selectors']}} @@{{host}}:{{hosts[host]['port']}} +{% else %} {{hosts[host]['selectors']}} @@{{host}} +{% endif %} +{% else %} +{% if hosts[host]['port'] %} +{{hosts[host]['selectors']}} @{{host}}:{{hosts[host]['port']}} {% else %} {{hosts[host]['selectors']}} @{{host}} {% endif %} +{% endif %} {% endfor %} {% endif %} {% if user %} {% for u in user %} {{user[u]['selectors']}} :omusrmsg:{{u}} {% endfor %} {% endif %} ''' logrotate_configs = ''' {% for file in files %} {{files[file]['log-file']}} { missingok notifempty create rotate {{files[file]['max-files']}} size={{files[file]['max-size']//1024}}k postrotate invoke-rc.d rsyslog rotate > /dev/null endscript } {% endfor %} ''' # config templates end def get_config(): c = Config() if not c.exists('system syslog'): return None c.set_level('system syslog') config_data = { 'files': {}, 'console': {}, 'hosts': {}, 'user': {} } # # /etc/rsyslog.d/vyos-rsyslog.conf # 'set system syslog global' # config_data['files'].update( { 'global': { 'log-file': '/var/log/messages', 'max-size': 262144, 'action-on-max-size': '/usr/sbin/logrotate /etc/logrotate.d/vyos-rsyslog', 'selectors': '*.notice;local7.debug', 'max-files': '5', 'preserver_fqdn': False } } ) if c.exists('global marker'): config_data['files']['global']['marker'] = True if c.exists('global marker interval'): config_data['files']['global'][ 'marker-interval'] = c.return_value('global marker interval') if c.exists('global facility'): config_data['files']['global'][ 'selectors'] = generate_selectors(c, 'global facility') if c.exists('global archive size'): config_data['files']['global']['max-size'] = int( c.return_value('global archive size')) * 1024 if c.exists('global archive file'): config_data['files']['global'][ 'max-files'] = c.return_value('global archive file') if c.exists('global preserve-fqdn'): config_data['files']['global']['preserver_fqdn'] = True # # set system syslog file # if c.exists('file'): filenames = c.list_nodes('file') for filename in filenames: config_data['files'].update( { filename: { 'log-file': '/var/log/user/' + filename, 'max-files': '5', 'action-on-max-size': '/usr/sbin/logrotate /etc/logrotate.d/' + filename, 'selectors': '*.err', 'max-size': 262144 } } ) if c.exists('file ' + filename + ' facility'): config_data['files'][filename]['selectors'] = generate_selectors( c, 'file ' + filename + ' facility') if c.exists('file ' + filename + ' archive size'): config_data['files'][filename]['max-size'] = int( c.return_value('file ' + filename + ' archive size')) * 1024 if c.exists('file ' + filename + ' archive files'): config_data['files'][filename]['max-files'] = c.return_value( 'file ' + filename + ' archive files') # set system syslog console if c.exists('console'): config_data['console'] = { '/dev/console': { 'selectors': '*.err' } } for f in c.list_nodes('console facility'): if c.exists('console facility ' + f + ' level'): config_data['console'] = { '/dev/console': { 'selectors': generate_selectors(c, 'console facility') } } # set system syslog host if c.exists('host'): - proto = 'udp' rhosts = c.list_nodes('host') for rhost in rhosts: for fac in c.list_nodes('host ' + rhost + ' facility'): if c.exists('host ' + rhost + ' facility ' + fac + ' protocol'): proto = c.return_value( 'host ' + rhost + ' facility ' + fac + ' protocol') + else: + proto = 'udp' config_data['hosts'].update( { rhost: { 'selectors': generate_selectors(c, 'host ' + rhost + ' facility'), 'proto': proto } } ) + if c.exists('host ' + rhost + ' port'): + config_data['hosts'][rhost][ + 'port'] = c.return_value(['host', rhost, 'port']) # set system syslog user if c.exists('user'): usrs = c.list_nodes('user') for usr in usrs: config_data['user'].update( { usr: { 'selectors': generate_selectors(c, 'user ' + usr + ' facility') } } ) return config_data def generate_selectors(c, config_node): # protocols and security are being mapped here # for backward compatibility with old configs # security and protocol mappings can be removed later if c.is_tag(config_node): nodes = c.list_nodes(config_node) selectors = "" for node in nodes: lvl = c.return_value(config_node + ' ' + node + ' level') if lvl == None: lvl = "err" if lvl == 'all': lvl = '*' if node == 'all' and node != nodes[-1]: selectors += "*." + lvl + ";" elif node == 'all': selectors += "*." + lvl elif node != nodes[-1]: if node == 'protocols': node = 'local7' if node == 'security': node = 'auth' selectors += node + "." + lvl + ";" else: if node == 'protocols': node = 'local7' if node == 'security': node = 'auth' selectors += node + "." + lvl return selectors def generate(c): if c == None: return None tmpl = jinja2.Template(configs, trim_blocks=True) config_text = tmpl.render(c) with open('/etc/rsyslog.d/vyos-rsyslog.conf', 'w') as f: f.write(config_text) # eventually write for each file its own logrotate file, since size is # defined it shouldn't matter tmpl = jinja2.Template(logrotate_configs, trim_blocks=True) config_text = tmpl.render(c) with open('/etc/logrotate.d/vyos-rsyslog', 'w') as f: f.write(config_text) def verify(c): if c == None: return None - # + + # may be obsolete # /etc/rsyslog.conf is generated somewhere and copied over the original (exists in /opt/vyatta/etc/rsyslog.conf) # it interferes with the global logging, to make sure we are using a single base, template is enforced here # if not os.path.islink('/etc/rsyslog.conf'): os.remove('/etc/rsyslog.conf') os.symlink( '/usr/share/vyos/templates/rsyslog/rsyslog.conf', '/etc/rsyslog.conf') # /var/log/vyos-rsyslog were the old files, we may want to clean those up, but currently there # is a chance that someone still needs it, so I don't automatically remove # them + # if c == None: return None fac = [ '*', 'auth', 'authpriv', 'cron', 'daemon', 'kern', 'lpr', 'mail', 'mark', 'news', 'protocols', 'security', 'syslog', 'user', 'uucp', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7'] lvl = ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', '*'] for conf in c: if c[conf]: for item in c[conf]: for s in c[conf][item]['selectors'].split(";"): f = re.sub("\..*$", "", s) if f not in fac: print (c[conf]) raise ConfigError( 'Invalid facility ' + s + ' set in ' + conf + ' ' + item) l = re.sub("^.+\.", "", s) if l not in lvl: raise ConfigError( 'Invalid logging level ' + s + ' set in ' + conf + ' ' + item) def apply(c): if not c and os.path.exists('/var/run/rsyslogd.pid'): os.system("sudo systemctl stop syslog.socket") os.system("sudo systemctl stop rsyslog") else: if not os.path.exists('/var/run/rsyslogd.pid'): os.system("sudo systemctl start rsyslog >/dev/null") else: os.system("sudo systemctl restart rsyslog >/dev/null") if __name__ == '__main__': try: c = get_config() verify(c) generate(c) apply(c) except ConfigError as e: print(e) sys.exit(1) diff --git a/src/migration-scripts/system/11-to-12 b/src/migration-scripts/system/11-to-12 new file mode 100755 index 000000000..64425e2b9 --- /dev/null +++ b/src/migration-scripts/system/11-to-12 @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +# converts 'set system syslog host <address>:<port>' +# to 'set system syslog host <address> port <port>' + +import sys +import re + +from vyos.configtree import ConfigTree + +if (len(sys.argv) < 1): + print("Must specify file name!") + sys.exit(1) + +file_name = sys.argv[1] + +with open(file_name, 'r') as f: + config_file = f.read() + +config = ConfigTree(config_file) +cbase = ['system', 'syslog', 'host'] + +if not config.exists(cbase): + sys.exit(0) + +for host in config.list_nodes(cbase): + if re.search(':[0-9]{1,5}$',host): + h = re.search('^[a-zA-Z\-0-9\.]+', host).group(0) + p = re.sub(':', '', re.search(':[0-9]+$', host).group(0)) + config.set(cbase + [h]) + config.set(cbase + [h, 'port'], value=p) + for fac in config.list_nodes(cbase + [host, 'facility']): + config.set(cbase + [h, 'facility', fac]) + config.set_tag(cbase + [h, 'facility']) + if config.exists(cbase + [host, 'facility', fac, 'protocol']): + proto = config.return_value(cbase + [host, 'facility', fac, 'protocol']) + config.set(cbase + [h, 'facility', fac, 'protocol'], value=proto) + if config.exists(cbase + [host, 'facility', fac, 'level']): + lvl = config.return_value(cbase + [host, 'facility', fac, 'level']) + config.set(cbase + [h, 'facility', fac, 'level'], value=lvl) + config.delete(cbase + [host]) + + try: + open(file_name,'w').write(config.to_string()) + except OSError as e: + print("Failed to save the modified config: {}".format(e)) + sys.exit(1) diff --git a/src/op_mode/powerctrl.py b/src/op_mode/powerctrl.py index 46ebf5ffb..8de25d752 100755 --- a/src/op_mode/powerctrl.py +++ b/src/op_mode/powerctrl.py @@ -1,166 +1,172 @@ #!/usr/bin/env python3 # # Copyright (C) 2018 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 sys import argparse import subprocess import re from datetime import datetime, timedelta, time as type_time, date as type_date from subprocess import check_output, CalledProcessError, STDOUT from vyos.util import ask_yes_no -def valid_time(s): +def parse_time(s): try: return datetime.strptime(s, "%H:%M").time() except ValueError: return None - -def valid_date(s): - try: - return datetime.strptime(s, "%d%m%Y").date() - except ValueError: +def parse_date(s): + for fmt in ["%d%m%Y", "%d/%m/%Y", "%d.%m.%Y", "%d:%m:%Y", "%Y-%m-%d"]: try: - return datetime.strptime(s, "%d/%m/%Y").date() + return datetime.strptime(s, fmt).date() except ValueError: - try: - return datetime.strptime(s, "%d.%m.%Y").date() - except ValueError: - try: - return datetime.strptime(s, "%d:%m:%Y").date() - except ValueError: - return None + continue + # If nothing matched... + return None +def get_shutdown_status(): + try: + output = check_output(["/bin/systemctl", "status", "systemd-shutdownd.service"]).decode() + return output + except CalledProcessError: + return None def check_shutdown(): - try: - cmd = check_output(["/bin/systemctl","status","systemd-shutdownd.service"]) - #Shutodwn is scheduled - r = re.findall(r'Status: \"(.*)\"\n', cmd.decode())[0] - print(r) - except CalledProcessError as e: - #Shutdown is not scheduled - print("Shutdown is not scheduled") + output = get_shutdown_status() + if output: + r = re.findall(r'Status: \"(.*)\"\n', output) + if r: + # When available, that line is like + # Status: "Shutting down at Thu 1970-01-01 00:00:00 UTC (poweroff)..." + print(r[0]) + else: + # Sometimes status string is not available immediately + # after service startup + print("Poweroff or reboot is scheduled") + else: + print("Poweroff or reboot is not scheduled") def cancel_shutdown(): - try: - timenow = datetime.now().strftime('%Y-%m-%d %H:%M:%S') - cmd = check_output(["/sbin/shutdown","-c","--no-wall"]) - message = "Reboot scheduled has been cancelled %s" % timenow - #Generate broadcast message about cancel reboot - os.system("wall %s" % message) - except CalledProcessError as e: - sys.exit("Error aborting shutdown: %s" % e) + output = get_shutdown_status() + if output: + try: + timenow = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + cmd = check_output(["/sbin/shutdown","-c","--no-wall"]) + message = "Scheduled reboot or poweroff has been cancelled %s" % timenow + os.system("wall %s" % message) + except CalledProcessError as e: + sys.exit("Could not cancel a reboot or poweroff: %s" % e) + else: + print("Reboot or poweroff is not scheduled") def execute_shutdown(time, reboot = True, ask=True): if not ask: action = "reboot" if reboot else "poweroff" if not ask_yes_no("Are you sure you want to %s this system?" % action): sys.exit(0) action = "-r" if reboot else "-P" if len(time) == 0: ### T870 legacy reboot job support chk_vyatta_based_reboots() ### cmd = check_output(["/sbin/shutdown",action,"now"],stderr=STDOUT) print(cmd.decode().split(",",1)[0]) return - - # Try to extract date from the first argument - if len(time) == 1: - time = time[0].split(" ",1) - - if len(time) == 1: - ts = valid_time(time[0]) - if time[0].isdigit() or valid_time(time[0]): - cmd = check_output(["/sbin/shutdown",action,time[0]],stderr=STDOUT) + elif len(time) == 1: + # Assume the argument is just time + ts = parse_time(time[0]) + if ts: + cmd = check_output(["/sbin/shutdown", action, time[0]], stderr=STDOUT) else: - sys.exit("Timestamp needs to be in format of 12:34") - + sys.exit("Invalid time \"{0}\". The valid format is HH:MM".format(time[0])) elif len(time) == 2: - ts = valid_time(time[0]) - ds = valid_date(time[1]) + # Assume it's date and time + ts = parse_time(time[0]) + ds = parse_date(time[1]) if ts and ds: t = datetime.combine(ds, ts) td = t - datetime.now() t2 = 1 + int(td.total_seconds())//60 # Get total minutes - cmd = check_output(["/sbin/shutdown",action,str(t2)],stderr=STDOUT) + cmd = check_output(["/sbin/shutdown", action, str(t2)], stderr=STDOUT) else: - sys.exit("Timestamp needs to be in format of 12:34\nDatestamp in the format of DD.MM.YY") + if not ts: + sys.exit("Invalid time \"{0}\". The valid format is HH:MM".format(time[0])) + else: + sys.exit("Invalid time \"{0}\". A valid format is YYYY-MM-DD [HH:MM]".format(time[1])) else: - sys.exit("Could not decode time and date") - + sys.exit("Could not decode date and time. Valids formats are HH:MM or YYYY-MM-DD HH:MM") check_shutdown() def chk_vyatta_based_reboots(): ### T870 commit-confirm is still using the vyatta code base, once gone, the code below can be removed ### legacy scheduled reboot s are using at and store the is as /var/run/<name>.job ### name is the node of scheduled the job, commit-confirm checks for that f = r'/var/run/confirm.job' - if os .path.exists(f): + if os.path.exists(f): jid = open(f).read().strip() if jid != 0: subprocess.call(['sudo', 'atrm', jid]) os.remove(f) def main(): parser = argparse.ArgumentParser() parser.add_argument("--yes", "-y", - help="dont as for shutdown", + help="Do not ask for confirmation", action="store_true", dest="yes") action = parser.add_mutually_exclusive_group(required=True) action.add_argument("--reboot", "-r", help="Reboot the system", nargs="*", metavar="Minutes|HH:MM") action.add_argument("--poweroff", "-p", help="Poweroff the system", nargs="*", metavar="Minutes|HH:MM") action.add_argument("--cancel", "-c", help="Cancel pending shutdown", action="store_true") action.add_argument("--check", help="Check pending chutdown", action="store_true") args = parser.parse_args() try: if args.reboot is not None: execute_shutdown(args.reboot, reboot=True, ask=args.yes) if args.poweroff is not None: execute_shutdown(args.poweroff, reboot=False,ask=args.yes) if args.cancel: cancel_shutdown() if args.check: check_shutdown() except KeyboardInterrupt: sys.exit("Interrupted") if __name__ == "__main__": main() +