2018-06-25 16:22:22 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
try:
|
2019-12-17 01:04:54 +01:00
|
|
|
from ifupdown2.lib.addon import Addon
|
2018-06-25 16:22:22 +02:00
|
|
|
import ifupdown2.ifupdown.statemanager as statemanager
|
|
|
|
|
|
|
|
from ifupdown2.ifupdown.iface import *
|
|
|
|
from ifupdown2.ifupdown.utils import utils
|
|
|
|
|
|
|
|
from ifupdown2.ifupdownaddons.modulebase import moduleBase
|
2018-06-28 18:42:48 +03:00
|
|
|
|
2018-06-25 16:22:22 +02:00
|
|
|
from ifupdown2.ifupdown.exceptions import moduleNotSupported
|
|
|
|
except ImportError:
|
2019-12-17 01:04:54 +01:00
|
|
|
from lib.addon import Addon
|
2018-06-25 16:22:22 +02:00
|
|
|
import ifupdown.statemanager as statemanager
|
|
|
|
|
|
|
|
from ifupdown.iface import *
|
|
|
|
from ifupdown.utils import utils
|
|
|
|
|
|
|
|
from ifupdownaddons.modulebase import moduleBase
|
|
|
|
|
|
|
|
from ifupdown.exceptions import moduleNotSupported
|
|
|
|
|
2018-06-28 18:42:48 +03:00
|
|
|
|
2019-12-17 01:04:54 +01:00
|
|
|
class ppp(Addon, moduleBase):
|
2018-06-25 16:22:22 +02:00
|
|
|
"""
|
|
|
|
ifupdown2 addon module to configure ppp
|
|
|
|
"""
|
|
|
|
_modinfo = {
|
2018-06-28 18:42:48 +03:00
|
|
|
'mhelp': 'create/configure ppp interfaces',
|
|
|
|
'attrs': {
|
|
|
|
'provider': {
|
|
|
|
'help': 'Provider file in ppp',
|
|
|
|
'validvals': ['<text>'],
|
|
|
|
'required': True,
|
|
|
|
'example': ['dsl-provider']
|
2018-06-25 16:22:22 +02:00
|
|
|
},
|
2018-06-28 18:42:48 +03:00
|
|
|
'ppp-physdev': {
|
|
|
|
'help': 'Physical underlay device to use for ppp if any',
|
|
|
|
'validvals': ['<interface>'],
|
|
|
|
'required': False,
|
|
|
|
'example': ['ppp-physdev eth1']
|
2018-06-25 16:22:22 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, *args, **kargs):
|
2019-12-17 01:04:54 +01:00
|
|
|
Addon.__init__(self)
|
2018-06-25 16:22:22 +02:00
|
|
|
moduleBase.__init__(self, *args, **kargs)
|
|
|
|
if not os.path.exists('/usr/bin/pon'):
|
|
|
|
raise moduleNotSupported('module init failed: no /usr/bin/pon found')
|
|
|
|
|
2018-06-28 18:42:48 +03:00
|
|
|
@staticmethod
|
|
|
|
def _is_my_interface(ifaceobj):
|
|
|
|
return ifaceobj.addr_method == "ppp" and ifaceobj.get_attr_value_first('provider')
|
2018-06-25 16:22:22 +02:00
|
|
|
|
|
|
|
def _up(self, ifaceobj):
|
2018-06-28 18:42:48 +03:00
|
|
|
"""
|
2018-06-25 16:22:22 +02:00
|
|
|
Up the PPP connection
|
2018-06-28 18:42:48 +03:00
|
|
|
"""
|
2018-06-25 16:22:22 +02:00
|
|
|
provider = ifaceobj.get_attr_value_first('provider')
|
|
|
|
old_config = None
|
|
|
|
old_provider = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
ppp_file = os.path.join('/etc/ppp/peers', provider)
|
|
|
|
if not os.path.isfile(ppp_file):
|
|
|
|
self.log_warn('Invalid ppp provider file does not exist')
|
|
|
|
return
|
|
|
|
|
|
|
|
# Load state data
|
|
|
|
saved_ifaceobjs = statemanager.statemanager_api.get_ifaceobjs(ifaceobj.name)
|
|
|
|
if saved_ifaceobjs:
|
2018-06-28 18:42:48 +03:00
|
|
|
old_provider = saved_ifaceobjs[0].get_attr_value_first('provider')
|
|
|
|
old_config = saved_ifaceobjs[0].get_attr_value_first('provider_file')
|
2018-06-25 16:22:22 +02:00
|
|
|
|
|
|
|
config = hashlib.sha256(open(ppp_file, 'rb').read()).hexdigest()
|
|
|
|
# Always save the current config files hash
|
|
|
|
ifaceobj.update_config('provider_file', config)
|
|
|
|
|
2019-12-17 01:04:54 +01:00
|
|
|
if not self.cache.link_exists(ifaceobj.name):
|
2018-06-25 16:22:22 +02:00
|
|
|
try:
|
|
|
|
# This fails if not running
|
|
|
|
utils.exec_user_command('/bin/ps ax | /bin/grep pppd | /bin/grep -v grep | /bin/grep ' + provider)
|
2018-06-28 18:42:48 +03:00
|
|
|
except Exception:
|
2018-06-25 16:22:22 +02:00
|
|
|
utils.exec_commandl(['/usr/bin/pon', provider], stdout=None, stderr=None)
|
|
|
|
|
|
|
|
if old_config and old_config != config:
|
|
|
|
# Restart on config change
|
|
|
|
utils.exec_commandl(['/usr/bin/poff', provider], stdout=None, stderr=None)
|
|
|
|
utils.exec_commandl(['/usr/bin/pon', provider], stdout=None, stderr=None)
|
|
|
|
elif old_provider and old_provider != provider:
|
|
|
|
# Restart on provider change
|
|
|
|
utils.exec_commandl(['/usr/bin/poff', old_provider], stdout=None, stderr=None)
|
|
|
|
utils.exec_commandl(['/usr/bin/pon', provider], stdout=None, stderr=None)
|
|
|
|
|
2019-12-17 01:04:54 +01:00
|
|
|
except Exception as e:
|
2018-06-28 18:42:48 +03:00
|
|
|
self.log_warn(str(e))
|
2018-06-25 16:22:22 +02:00
|
|
|
|
|
|
|
def _down(self, ifaceobj):
|
2018-06-28 18:42:48 +03:00
|
|
|
"""
|
2018-06-25 16:22:22 +02:00
|
|
|
Down the PPP connection
|
2018-06-28 18:42:48 +03:00
|
|
|
"""
|
2018-06-25 16:22:22 +02:00
|
|
|
try:
|
|
|
|
provider = ifaceobj.get_attr_value_first('provider')
|
|
|
|
# This fails if not running
|
|
|
|
utils.exec_user_command('/bin/ps ax | /bin/grep pppd | /bin/grep -v grep | /bin/grep ' + provider)
|
|
|
|
utils.exec_commandl(['/usr/bin/poff', provider], stdout=None, stderr=None)
|
2019-12-17 01:04:54 +01:00
|
|
|
except Exception as e:
|
2018-06-25 16:22:22 +02:00
|
|
|
self.log_warn(str(e))
|
|
|
|
|
|
|
|
def get_dependent_ifacenames(self, ifaceobj, ifacenames_all=None):
|
|
|
|
if not self._is_my_interface(ifaceobj):
|
|
|
|
return None
|
2018-06-28 18:42:48 +03:00
|
|
|
|
|
|
|
device = ifaceobj.get_attr_value_first('ppp-physdev')
|
2018-06-25 16:22:22 +02:00
|
|
|
|
|
|
|
if device:
|
|
|
|
return [device]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-06-28 18:42:48 +03:00
|
|
|
def _query_check(self, ifaceobj, ifaceobjcurr):
|
2019-12-17 01:04:54 +01:00
|
|
|
if not self.cache.link_exists(ifaceobj.name):
|
2018-06-28 18:42:48 +03:00
|
|
|
return
|
2018-06-25 16:22:22 +02:00
|
|
|
ifaceobjcurr.status = ifaceStatus.SUCCESS
|
|
|
|
|
|
|
|
def _query_running(self, ifaceobjrunning):
|
2019-12-17 01:04:54 +01:00
|
|
|
if not self.cache.link_exists(ifaceobjrunning.name):
|
2018-06-25 16:22:22 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# Operations supported by this addon (yet).
|
|
|
|
_run_ops = {
|
2018-06-28 18:42:48 +03:00
|
|
|
'pre-up': _up,
|
|
|
|
'down': _down,
|
|
|
|
'query-checkcurr': _query_check,
|
|
|
|
'query-running': _query_running,
|
2018-06-25 16:22:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_ops(self):
|
|
|
|
return self._run_ops.keys()
|
|
|
|
|
2018-06-28 18:42:48 +03:00
|
|
|
def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
|
2018-06-25 16:22:22 +02:00
|
|
|
op_handler = self._run_ops.get(operation)
|
|
|
|
|
|
|
|
if not op_handler:
|
|
|
|
return
|
|
|
|
|
|
|
|
if operation != 'query-running' and not self._is_my_interface(ifaceobj):
|
|
|
|
return
|
|
|
|
|
|
|
|
if operation == 'query-checkcurr':
|
|
|
|
op_handler(self, ifaceobj, query_ifaceobj)
|
|
|
|
else:
|
|
|
|
op_handler(self, ifaceobj)
|