diff --git a/openvpn/connector/main.py b/openvpn/connector/main.py index 125f988..3044ce0 100644 --- a/openvpn/connector/main.py +++ b/openvpn/connector/main.py @@ -23,6 +23,7 @@ import argparse import dbus from pathlib import Path +from enum import Enum from openvpn.connector.token import DecodeToken from openvpn.connector.profile import ProfileFetch, DecryptError, DownloadError from openvpn.connector.autoload import AutoloadConfig @@ -34,10 +35,29 @@ import traceback +class ConfigModes(Enum): + UNSET = 0 + AUTOLOAD = 1 + + def to_string(v): + if ConfigModes.UNSET == v: + return '[UNSET]' + elif ConfigModes.AUTOLOAD == v: + return 'autoload' + + def parse(v): + if 'autoload' == v: + return ConfigModes.AUTOLOAD + raise ValueError('Incorrect configuration mode: "%s"' % v) + + def main(): + run_mode = ConfigModes.AUTOLOAD cli = argparse.ArgumentParser(prog='openvpn-connector-setup', description='OpenVPN Connector Setup utility', usage='%s [options]' % os.path.basename(sys.argv[0])) + cli.add_argument('--mode', metavar='MODE', nargs=1, action='store', + help='Defines how configuration profiles are imported and stored (default: %s)' % ConfigModes.to_string(run_mode)) cli.add_argument('--token', metavar='TOKEN_VALUE', nargs=1, help='This value is provided by the OpenVPN Cloud web portal.') cli.add_argument('--name', metavar='NAME', nargs=1, default=['OpenVPN Cloud',], @@ -54,6 +74,12 @@ def main(): config_name = cliopts.name[0] start_config = not cliopts.no_start + if cliopts.mode: + run_mode = ConfigModes.parse(cliopts.mode[0]) + + if 'OPENVPN_CLOUD_DEBUG' in os.environ: + print('Run mode: %s' % ConfigModes.to_string(run_mode)) + # By default the root installation directory is / # but for development and debugging, the root directory # can be put into a chroot. This is done via the @@ -63,7 +89,7 @@ def main(): if 'OPENVPN_CLOUD_ROOT_DIR' in os.environ: rootdir = os.environ['OPENVPN_CLOUD_ROOT_DIR'] - if '/' == rootdir and os.geteuid() != 0: + if ConfigModes.AUTOLOAD == run_mode and '/' == rootdir and os.geteuid() != 0: print('%s must be run as root with "%s" as top level installation directory ' % ( os.path.basename(sys.argv[0]), rootdir)) sys.exit(2) @@ -83,10 +109,6 @@ def main(): token = cliopts.token[0] try: - # Ensure proper destination directories exists - config_dir = os.path.join(rootdir, 'etc','openvpn3','autoload') - Path(config_dir).mkdir(parents=True, exist_ok=True) - # Parse the setup token. This contains # the profile name which needs to be downloaded # and a key used to decrypt the downloaded profile @@ -98,30 +120,35 @@ def main(): profile.Download() print('Done') - cfg = os.path.join(config_dir, cfg_filename) - print('Saving profile to "%s" ... ' % cfg, end='', flush=True) - profile.Save(cfg) - print('Done') - - # Generate the openvpn3-autoload configuration - autoload = AutoloadConfig(cfg) - print('Saving openvpn3-autoload config to "%s" ... ' % autoload.GetConfigFilename(), end='', flush=True) - autoload.SetName(config_name) - autoload.SetAutostart(True) - autoload.SetTunnelParams('persist', True) - autoload.Save() - print('Done') + if ConfigModes.AUTOLOAD == run_mode: + # Ensure proper destination directories exists + config_dir = os.path.join(rootdir, 'etc','openvpn3','autoload') + Path(config_dir).mkdir(parents=True, exist_ok=True) - if start_config is True and '/' == rootdir and os.geteuid() == 0: - service = SystemdServiceUnit(dbus.SystemBus(), 'openvpn3-autoload.service') - print('Enabling openvpn3-autoload.service during boot ... ', end='', flush=True) - service.Enable() + cfg = os.path.join(config_dir, cfg_filename) + print('Saving profile to "%s" ... ' % cfg, end='', flush=True) + profile.Save(cfg) print('Done') - print('Starting openvpn3-autoload.service ... ', end='', flush=True) - service.Start() + # Generate the openvpn3-autoload configuration + autoload = AutoloadConfig(cfg) + print('Saving openvpn3-autoload config to "%s" ... ' % autoload.GetConfigFilename(), end='', flush=True) + autoload.SetName(config_name) + autoload.SetAutostart(True) + autoload.SetTunnelParams('persist', True) + autoload.Save() print('Done') + if start_config is True and '/' == rootdir and os.geteuid() == 0: + service = SystemdServiceUnit(dbus.SystemBus(), 'openvpn3-autoload.service') + print('Enabling openvpn3-autoload.service during boot ... ', end='', flush=True) + service.Enable() + print('Done') + + print('Starting openvpn3-autoload.service ... ', end='', flush=True) + service.Start() + print('Done') + except DownloadError as err: print('\n** ERROR ** ' + str(err)) print('URL: ' + err.GetURL())