summaryrefslogtreecommitdiff
path: root/src/conf_mode/interfaces-pppoe.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/conf_mode/interfaces-pppoe.py')
-rwxr-xr-xsrc/conf_mode/interfaces-pppoe.py82
1 files changed, 32 insertions, 50 deletions
diff --git a/src/conf_mode/interfaces-pppoe.py b/src/conf_mode/interfaces-pppoe.py
index f318614db..9c045534c 100755
--- a/src/conf_mode/interfaces-pppoe.py
+++ b/src/conf_mode/interfaces-pppoe.py
@@ -20,12 +20,10 @@ from sys import exit
from copy import deepcopy
from jinja2 import Template
from subprocess import Popen, PIPE
-from pwd import getpwnam
-from grp import getgrnam
-from stat import S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IXGRP, S_IROTH, S_IXOTH
from vyos.config import Config
from vyos.ifconfig import Interface
+from vyos.util import chown_file, chmod_x_file
from vyos import ConfigError
from netifaces import interfaces
@@ -112,15 +110,11 @@ if [ "$6" != "{{ intf }}" ]; then
exit
fi
+{% if ipv6_autoconf -%}
# add some info to syslog
DIALER_PID=$(cat /var/run/{{ intf }}.pid)
logger -t pppd[$DIALER_PID] "executing $0"
-logger -t pppd[$DIALER_PID] "configuring dialer interface $6 via $2"
-
-echo "{{ description }}" > /sys/class/net/{{ intf }}/ifalias
-
-{% if ipv6_autoconf -%}
-
+logger -t pppd[$DIALER_PID] "configuring interface {{ intf }} via $2"
# Configure interface-specific Host/Router behaviour.
# Note: It is recommended to have the same setting on all interfaces; mixed
@@ -150,12 +144,12 @@ echo 1 > /proc/sys/net/ipv6/conf/{{ intf }}/autoconfigure
{% endif %}
"""
-config_pppoe_ip_pre_up_tmpl = """#!/bin/sh
+config_pppoe_ip_up_tmpl = """#!/bin/sh
# As PPPoE is an "on demand" interface we need to re-configure it when it
# becomes up
-if [ "$6" != "pppoe0" ]; then
+if [ "$6" != "{{ intf }}" ]; then
exit
fi
@@ -163,6 +157,8 @@ fi
DIALER_PID=$(cat /var/run/{{ intf }}.pid)
logger -t pppd[$DIALER_PID] "executing $0"
+echo "{{ description }}" > /sys/class/net/{{ intf }}/ifalias
+
{% if vrf -%}
logger -t pppd[$DIALER_PID] "configuring dialer interface $6 for VRF {{ vrf }}"
ip link set dev {{ intf }} master {{ vrf }}
@@ -305,61 +301,49 @@ def verify(pppoe):
return None
def generate(pppoe):
- config_file_pppoe = f"/etc/ppp/peers/{pppoe['intf']}"
- ip_pre_up_script_file = f"/etc/ppp/ip-pre-up.d/9999-vyos-vrf-{pppoe['intf']}"
- ipv6_if_up_script_file = f"/etc/ppp/ipv6-up.d/50-vyos-{pppoe['intf']}-autoconf"
+ intf = pppoe['intf']
+ config_file_pppoe = f'/etc/ppp/peers/{intf}'
+ ip_up_script_file = f'/etc/ppp/ip-up.d/9990-vyos-vrf-{intf}'
+ ipv6_if_up_script_file = f'/etc/ppp/ipv6-up.d/9990-vyos-autoconf-{intf}'
+
+ config_files = [config_file_pppoe, ip_up_script_file, ipv6_if_up_script_file]
+
+ # Ensure directories for config files exist - otherwise create them on demand
+ for file in config_files:
+ dirname = os.path.dirname(file)
+ if not os.path.isdir(dirname):
+ os.mkdir(dirname)
# Always hang-up PPPoE connection prior generating new configuration file
- cmd = f"systemctl stop ppp@{pppoe['intf']}.service"
+ cmd = f'systemctl stop ppp@{intf}.service'
subprocess_cmd(cmd)
if pppoe['deleted']:
# Delete PPP configuration files
- if os.path.exists(config_file_pppoe):
- os.unlink(config_file_pppoe)
-
- if os.path.exists(ipv6_if_up_script_file):
- os.unlink(ipv6_if_up_script_file)
-
- if os.path.exists(ip_pre_up_script_file):
- os.unlink(ip_pre_up_script_file)
+ for file in config_files:
+ if os.path.exists(file):
+ os.unlink(file)
else:
- # PPP peers directory
- dirname = os.path.dirname(config_file_pppoe)
- if not os.path.isdir(dirname):
- os.mkdir(dirname)
-
# Create PPP configuration files
tmpl = Template(config_pppoe_tmpl)
config_text = tmpl.render(pppoe)
with open(config_file_pppoe, 'w') as f:
f.write(config_text)
- # PPP ip-pre-up.d scripting directory
- dirname = os.path.dirname(ip_pre_up_script_file)
- if not os.path.isdir(dirname):
- os.mkdir(dirname)
-
- tmpl = Template(config_pppoe_ip_pre_up_tmpl)
+ tmpl = Template(config_pppoe_ip_up_tmpl)
config_text = tmpl.render(pppoe)
- with open(ip_pre_up_script_file, 'w') as f:
+ with open(ip_up_script_file, 'w') as f:
f.write(config_text)
- # PPP ipv6-up.d scripting directory
- dirname = os.path.dirname(ipv6_if_up_script_file)
- if not os.path.isdir(dirname):
- os.mkdir(dirname)
-
tmpl = Template(config_pppoe_ipv6_up_tmpl)
config_text = tmpl.render(pppoe)
with open(ipv6_if_up_script_file, 'w') as f:
f.write(config_text)
- bitmask = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | \
- S_IROTH | S_IXOTH
- os.chmod(ip_pre_up_script_file, bitmask)
- os.chmod(ipv6_if_up_script_file, bitmask)
+ # make generated script file executable
+ chmod_x_file(ip_up_script_file)
+ chmod_x_file(ipv6_if_up_script_file)
return None
@@ -369,15 +353,13 @@ def apply(pppoe):
return None
if not pppoe['disable']:
- # dial PPPoE connection
- cmd = f"systemctl start ppp@{pppoe['intf']}.service"
+ # "dial" PPPoE connection
+ intf = pppoe['intf']
+ cmd = f'systemctl start ppp@{intf}.service'
subprocess_cmd(cmd)
# make logfile owned by root / vyattacfg
- if os.path.isfile(pppoe['logfile']):
- uid = getpwnam('root').pw_uid
- gid = getgrnam('vyattacfg').gr_gid
- os.chown(pppoe['logfile'], uid, gid)
+ chown_file(pppoe['logfile'], 'root', 'vyattacfg')
return None