summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/vyos/ifconfig/__init__.py1
-rw-r--r--python/vyos/ifconfig/l2tpv3.py30
-rw-r--r--python/vyos/util.py57
3 files changed, 57 insertions, 31 deletions
diff --git a/python/vyos/ifconfig/__init__.py b/python/vyos/ifconfig/__init__.py
index d08a8b528..1f9956af0 100644
--- a/python/vyos/ifconfig/__init__.py
+++ b/python/vyos/ifconfig/__init__.py
@@ -36,3 +36,4 @@ from vyos.ifconfig.tunnel import IP6IP6If
from vyos.ifconfig.tunnel import SitIf
from vyos.ifconfig.tunnel import Sit6RDIf
from vyos.ifconfig.wireless import WiFiIf
+from vyos.ifconfig.l2tpv3 import L2TPv3If
diff --git a/python/vyos/ifconfig/l2tpv3.py b/python/vyos/ifconfig/l2tpv3.py
index 07f1cf8a3..f0d64a53d 100644
--- a/python/vyos/ifconfig/l2tpv3.py
+++ b/python/vyos/ifconfig/l2tpv3.py
@@ -41,25 +41,27 @@ class L2TPv3If(Interface):
}
}
options = Interface.options + \
- ['tunnel_id', 'peer_tunnel_id', 'local_port', 'remote_port', 'encapsulation', 'local_address', 'remote_address']
+ ['tunnel_id', 'peer_tunnel_id', 'local_port', 'remote_port',
+ 'encapsulation', 'local_address', 'remote_address', 'session_id',
+ 'peer_session_id']
def _create(self):
# create tunnel interface
- cmd = 'ip l2tp add tunnel tunnel_id {} '.format(config['tunnel_id'])
- cmd += 'peer_tunnel_id {} '.format(config['peer_tunnel_id'])
- cmd += 'udp_sport {} '.format(config['local_port'])
- cmd += 'udp_dport {} '.format(config['remote_port'])
- cmd += 'encap {} '.format(config['encapsulation'])
- cmd += 'local {} '.format(config['local_address'])
- cmd += 'remote {} '.format(config['remote_address'])
- self._cmd(cmd)
+ cmd = 'ip l2tp add tunnel tunnel_id {tunnel_id}'
+ cmd += ' peer_tunnel_id {peer_tunnel_id}'
+ cmd += ' udp_sport {local_port}'
+ cmd += ' udp_dport {remote_port}'
+ cmd += ' encap {encapsulation}'
+ cmd += ' local {local_address}'
+ cmd += ' remote {remote_address}'
+ self._cmd(cmd.format(**self.config))
# setup session
- cmd = 'ip l2tp add session name {} '.format(self.config['ifname'])
- cmd += 'tunnel_id {} '.format(config['tunnel_id'])
- cmd += 'session_id {} '.format(config['session_id'])
- cmd += 'peer_session_id {} '.format(config['peer_session_id'])
- self._cmd(cmd)
+ cmd = 'ip l2tp add session name {ifname}'
+ cmd += ' tunnel_id {tunnel_id}'
+ cmd += ' session_id {session_id}'
+ cmd += ' peer_session_id {peer_session_id}'
+ self._cmd(cmd.format(**self.config))
# interface is always A/D down. It needs to be enabled explicitly
self.set_admin_state('down')
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 67aa87a3a..3970b8bf1 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -16,15 +16,6 @@
import os
import re
import sys
-import psutil
-
-import vyos.defaults
-
-from getpass import getuser
-from grp import getgrnam
-from time import sleep
-from subprocess import check_output
-from ipaddress import ip_network
def read_file(path):
""" Read a file to string """
@@ -33,6 +24,27 @@ def read_file(path):
return data
+def chown_file(path, user, group):
+ """ change file owner """
+ from pwd import getpwnam
+ from grp import getgrnam
+
+ if os.path.isfile(path):
+ uid = getpwnam(user).pw_uid
+ gid = getgrnam(group).gr_gid
+ os.chown(path, uid, gid)
+
+
+def chmod_x_file(path):
+ """ make file executable """
+ from stat import S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IXGRP, S_IROTH, S_IXOTH
+
+ if os.path.isfile(path):
+ bitmask = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | \
+ S_IROTH | S_IXOTH
+ os.chmod(path, bitmask)
+
+
def colon_separated_to_dict(data_string, uniquekeys=False):
""" Converts a string containing newline-separated entries
of colon-separated key-value pairs into a dict.
@@ -84,11 +96,12 @@ def colon_separated_to_dict(data_string, uniquekeys=False):
def process_running(pid_file):
""" Checks if a process with PID in pid_file is running """
+ from psutil import pid_exists
if not os.path.isfile(pid_file):
return False
with open(pid_file, 'r') as f:
pid = f.read().strip()
- return psutil.pid_exists(int(pid))
+ return pid_exists(int(pid))
def seconds_to_human(s, separator=""):
@@ -132,7 +145,10 @@ def seconds_to_human(s, separator=""):
def get_cfg_group_id():
- group_data = getgrnam(vyos.defaults.cfg_group)
+ from grp import getgrnam
+ from vyos.defaults import cfg_group
+
+ group_data = getgrnam(cfg_group)
return group_data.gr_gid
@@ -162,18 +178,22 @@ def commit_in_progress():
# Since this will be used in scripts that modify the config outside of the CLI
# framework, those knowingly have root permissions.
# For everything else, we add a safeguard.
+ from subprocess import check_output
+ from psutil import process_iter, NoSuchProcess
+ from vyos.defaults import commit_lock
+
id = check_output(['/usr/bin/id', '-u']).decode().strip()
if id != '0':
raise OSError("This functions needs root permissions to return correct results")
- for proc in psutil.process_iter():
+ for proc in process_iter():
try:
files = proc.open_files()
if files:
for f in files:
- if f.path == vyos.defaults.commit_lock:
+ if f.path == commit_lock:
return True
- except psutil.NoSuchProcess as err:
+ except NoSuchProcess as err:
# Process died before we could examine it
pass
# Default case
@@ -182,7 +202,7 @@ def commit_in_progress():
def wait_for_commit_lock():
""" Not to be used in normal op mode scripts! """
-
+ from time import sleep
# Very synchronous approach to multiprocessing
while commit_in_progress():
sleep(1)
@@ -206,17 +226,20 @@ def ask_yes_no(question, default=False) -> bool:
def is_admin() -> bool:
"""Look if current user is in sudo group"""
+ from getpass import getuser
+ from grp import getgrnam
current_user = getuser()
(_, _, _, admin_group_members) = getgrnam('sudo')
return current_user in admin_group_members
def mac2eui64(mac, prefix=None):
- '''
+ """
Convert a MAC address to a EUI64 address or, with prefix provided, a full
IPv6 address.
Thankfully copied from https://gist.github.com/wido/f5e32576bb57b5cc6f934e177a37a0d3
- '''
+ """
+ from ipaddress import ip_network
# http://tools.ietf.org/html/rfc4291#section-2.5.1
eui64 = re.sub(r'[.:-]', '', mac).lower()
eui64 = eui64[0:6] + 'fffe' + eui64[6:]