diff options
| author | Christian Poessinger <christian@poessinger.com> | 2020-02-25 11:18:31 +0100 | 
|---|---|---|
| committer | Christian Poessinger <christian@poessinger.com> | 2020-02-25 11:18:31 +0100 | 
| commit | 9d68d23a2455cd11f77af6d94a021812771f2356 (patch) | |
| tree | 16e752084b00522f1b634f32a6a9cf7d8e0e2194 /src | |
| parent | 0023e6d51f2c7d065eac250b21af9a12544b9ab8 (diff) | |
| parent | bfe06683fd7dd50abc2ad5067a598f4ecf6b9a19 (diff) | |
| download | vyos-1x-9d68d23a2455cd11f77af6d94a021812771f2356.tar.gz vyos-1x-9d68d23a2455cd11f77af6d94a021812771f2356.zip  | |
Merge branch 'pppoe-t2070' of github.com:c-po/vyos-1x into current
* 'pppoe-t2070' of github.com:c-po/vyos-1x:
  pppoe: T2070: rewrite (dis-)connect op-mode commands in XML and Python
  gitignore: fix ignore pattern of all debhelper files
  pppoe: T2055: make logfile owned by root/vyattacfg
  pppoe: T1318: validate existing source-interface
Diffstat (limited to 'src')
| -rwxr-xr-x | src/conf_mode/interfaces-pppoe.py | 18 | ||||
| -rwxr-xr-x | src/op_mode/connect_disconnect.py | 98 | 
2 files changed, 114 insertions, 2 deletions
diff --git a/src/conf_mode/interfaces-pppoe.py b/src/conf_mode/interfaces-pppoe.py index e6e71e501..02d3cf3ce 100755 --- a/src/conf_mode/interfaces-pppoe.py +++ b/src/conf_mode/interfaces-pppoe.py @@ -21,6 +21,8 @@ from copy import deepcopy  from jinja2 import Template  from subprocess import Popen, PIPE  from time import sleep +from pwd import getpwnam +from grp import getgrnam  from vyos.config import Config  from vyos.ifconfig import Interface @@ -71,7 +73,7 @@ persist  ifname {{ intf }}  ipparam {{ intf }}  debug -logfile /var/log/vyatta/ppp_{{ intf }}.log +logfile {{ logfile }}  {% if 'auto' in default_route -%}  defaultroute  {% elif 'force' in default_route -%} @@ -94,6 +96,8 @@ rp_pppoe_service "{{ service_name }}"  """ +PPP_LOGFILE = '/var/log/vyatta/ppp_{}.log' +  default_config_data = {      'access_concentrator': '',      'auth_username': '', @@ -108,6 +112,7 @@ default_config_data = {      'ipv6_autoconf': False,      'ipv6_enable': False,      'local_address': '', +    'logfile': '',      'mtu': '1492',      'name_server': True,      'remote_address': '', @@ -127,6 +132,7 @@ def get_config():      # determine tagNode instance      try:          pppoe['intf'] = os.environ['VYOS_TAGNODE_VALUE'] +        pppoe['logfile'] = PPP_LOGFILE.format(pppoe['intf'])      except KeyError as E:          print("Interface not specified") @@ -212,6 +218,9 @@ def verify(pppoe):      if not pppoe['source_interface']:          raise ConfigError('PPPoE source interface is missing') +    if pppoe['source_interface'] not in interfaces(): +        raise ConfigError('PPPoE source interface does not exist') +      return None  def generate(pppoe): @@ -241,7 +250,12 @@ def apply(pppoe):          return None      if not pppoe['disable']: -        # Dial PPPoE connection +        # make logfile owned by root / vyattacfg +        uid = getpwnam('root').pw_uid +        gid = getgrnam('vyattacfg').gr_gid +        os.chown(pppoe['logfile'], uid, gid) + +        # dial PPPoE connection          cmd = 'systemctl start ppp@{}.service'.format(pppoe['intf'])          subprocess_cmd(cmd) diff --git a/src/op_mode/connect_disconnect.py b/src/op_mode/connect_disconnect.py new file mode 100755 index 000000000..a22615096 --- /dev/null +++ b/src/op_mode/connect_disconnect.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2020 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 argparse + +from sys import exit +from psutil import process_iter +from time import strftime, localtime, time + +PPP_LOGFILE = '/var/log/vyatta/ppp_{}.log' + +def check_interface(interface): +    if not os.path.isfile('/etc/ppp/peers/{}'.format(interface)): +        print('Interface {}: invalid!'.format(interface)) +        exit(1) + +def check_ppp_running(interface): +    """ +    Check if ppp process is running in the interface in question +    """ +    for p in process_iter(): +        if "pppd" in p.name(): +            if interface in p.cmdline(): +                return True + +    return False + +def connect(interface): +    """ +    Connect PPP interface +    """ +    check_interface(interface) + +    # Check if interface is already dialed +    if os.path.isdir('/sys/class/net/{}'.format(interface)): +        print('Interface {}: already connected!'.format(interface)) +    elif check_ppp_running(interface): +        print('Interface {}: connection is beeing established!'.format(interface)) +    else: +        print('Interface {}: connecting...'.format(interface)) +        user = os.environ['SUDO_USER'] +        tm = strftime("%a %d %b %Y %I:%M:%S %p %Z", localtime(time())) +        with open(PPP_LOGFILE.format(interface), 'a') as f: +            f.write('{}: user {} started PPP daemon for {} by connect command\n'.format(tm, user, interface)) +            cmd = 'umask 0; setsid sh -c "nohup /usr/sbin/pppd call {0} > /tmp/{0}.log 2>&1 &"'.format(interface) +            os.system(cmd) + + +def disconnect(interface): +    """ +    Disconnect PPP interface +    """ +    check_interface(interface) + +    # Check if interface is already down +    if not check_ppp_running(interface): +        print('Interface {}: connection is already down'.format(interface)) +    else: +        print('Interface {}: disconnecting...'.format(interface)) +        user = os.environ['SUDO_USER'] +        tm = strftime("%a %d %b %Y %I:%M:%S %p %Z", localtime(time())) +        with open(PPP_LOGFILE.format(interface), 'a') as f: +            f.write('{}: user {} stopped PPP daemon for {} by disconnect command\n'.format(tm, user, interface)) +            cmd = '/usr/bin/poff "{}"'.format(interface) +            os.system(cmd) + +def main(): +    parser = argparse.ArgumentParser() +    group = parser.add_mutually_exclusive_group() +    group.add_argument("--connect", help="Bring up a connection-oriented network interface", action="store") +    group.add_argument("--disconnect", help="Take down connection-oriented network interface", action="store") +    args = parser.parse_args() + +    if args.connect: +        connect(args.connect) +    elif args.disconnect: +        disconnect(args.disconnect) +    else: +        parser.print_help() + +    exit(0) + +if __name__ == '__main__': +     main()  | 
