Skip to content

Commit

Permalink
Implement the --mode=systemd-unit
Browse files Browse the repository at this point in the history
This mode will instead of configuring a openvpn3-autoload setup
import the configuration profile as a persistent configuration into
the OpenVPN 3 Configuration Manager.  It will also ensure root has the
needed access to the configuration to be able to use the
[email protected] unit file to start the session during boot.

Signed-off-by: David Sommerseth <[email protected]>
  • Loading branch information
dsommers committed Jan 11, 2022
1 parent a73b0bd commit c704ea9
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion openvpn/connector/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from openvpn.connector.token import DecodeToken
from openvpn.connector.profile import ProfileFetch, DecryptError, DownloadError
from openvpn.connector.autoload import AutoloadConfig
from openvpn.connector.configmgr import ConfigImport
from openvpn.connector.systemd import SystemdServiceUnit

# Add the traceback module if we're in debugging mode.
Expand All @@ -37,16 +38,21 @@
class ConfigModes(Enum):
UNSET = 0
AUTOLOAD = 1
UNITFILE = 2

def to_string(v):
if ConfigModes.UNSET == v:
return '[UNSET]'
elif ConfigModes.AUTOLOAD == v:
return 'autoload'
elif ConfigModes.UNITFILE == v:
return 'systemd-unit'

def parse(v):
if 'autoload' == v:
return ConfigModes.AUTOLOAD
elif 'systemd-unit' == v:
return ConfigModes.UNITFILE
raise ValueError('Incorrect configuration mode: "%s"' % v)


Expand Down Expand Up @@ -119,6 +125,7 @@ def main():
profile.Download()
print('Done')

systembus = dbus.SystemBus()
if ConfigModes.AUTOLOAD == run_mode:
# Generate the openvpn3-autoload configuration
autoload = AutoloadConfig(profile, rootdir, autoload_prefix)
Expand All @@ -128,7 +135,7 @@ def main():
autoload.Save()

if start_config is True and '/' == rootdir and os.geteuid() == 0:
service = SystemdServiceUnit(dbus.SystemBus(), 'openvpn3-autoload.service')
service = SystemdServiceUnit(systembus, 'openvpn3-autoload.service')
print('Enabling openvpn3-autoload.service during boot ... ', end='', flush=True)
service.Enable()
print('Done')
Expand All @@ -137,6 +144,33 @@ def main():
service.Start()
print('Done')

elif ConfigModes.UNITFILE == run_mode:
cfgimport = ConfigImport(systembus, config_name)
cfgimport.Import(profile)

if os.geteuid() != 0:
cfgimport.EnableOwnershipTransfer()

if start_config is True:
print('\n** INFO ** You did not run this command as root, so it will not\n'
+ ' start the connection automatically during boot. To start\n'
+ ' at boot time, as root, run this command: \n\n'
+ ' # systemctl enable --now openvpn3-session@%s.service\n' %
cfgimport.GetConfigName())
elif os.geteuid() == 0 and start_config is True:
service = SystemdServiceUnit(systembus,
'openvpn3-session@%s.service' % cfgimport.GetConfigName())

print('Enabling openvpn3-session@%s.service during boot ... ' % cfgimport.GetConfigName(),
end='', flush=True)
service.Enable()
print('Done')

print('Starting openvpn3-session@%s.service ... ' % cfgimport.GetConfigName(),
end='', flush=True)
service.Start()
print('Done')

except DownloadError as err:
print('\n** ERROR ** ' + str(err))
print('URL: ' + err.GetURL())
Expand Down

0 comments on commit c704ea9

Please sign in to comment.