#! /usr/bin/env python3 from __future__ import print_function import argparse import json import os import sys import vpngen def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) def create_variables(variables, defaults): variables_set = defaults.copy() for variable in variables: if variable == 'name' or variable == 'client': continue default = variables_set[variable] if variable in variables_set else '' print("Enter a value for '%s' [%s]: " % (variable, default), end='', flush=True) value = sys.stdin.readline()[:-1] if value != '' or variable not in variables_set: variables_set[variable] = value return variables_set def main(): parser = argparse.ArgumentParser(description='Manage OpenVPN VPNs') parser.add_argument('--vpn', help='The VPN to use', required=True) parser.add_argument('--config', dest='config', default='/etc/vpngen/vpngen.json', help='Configuration file path') parser.add_argument('--create', help='Create a VPN', action='store_true') parser.add_argument('--remove', help='Remove a VPN', action='store_true') parser.add_argument('--create-client', help='Create a client for the VPN', metavar='CLIENT') parser.add_argument('--remove-client', help='Remove a client for the VPN', metavar='CLIENT') parser.add_argument('--rebuild-client', help='Rebuild a client configuration', metavar='CLIENT') parser.add_argument('--rebuild-clients', help='Rebuild clients configurations', action='store_true') args = parser.parse_args() with open(args.config, "r") as f: config = json.load(f) vpn_name = config['vpnPrefix'] + args.vpn + config['vpnSuffix'] if args.create_client is not None: client_name = args.create_client elif args.remove_client is not None: client_name = args.remove_client elif args.rebuild_client is not None: client_name = args.rebuild_client else: client_name = None if client_name is not None: client_name = config['clientPrefix'] + client_name + config['clientSuffix'] vpng = vpngen.VpnGen(config['defaultConfigPath'], config['ovpnConfigPath']) config_path = vpng.get_vpn_variables_path(vpn_name) if os.path.exists(config_path): with open(config_path, "r") as f: data = json.load(f) config['defaults'].update(data['variables']) if args.create: default_variables = config['defaults'].copy() variables = create_variables(vpng.get_vpn_vars(), default_variables) res = vpng.create_vpn(vpn_name, variables) if res == vpngen.VpnGenError.Success: print("VPN %s created successfully" % vpn_name) else: eprint("Failed to create VPN %s: %s" % (vpn_name, res)) exit(1) elif args.remove: res = vpng.remove_vpn(vpn_name) if res == vpngen.VpnGenError.Success: print("VPN %s removed successfully" % vpn_name) else: eprint("Failed to remove VPN %s: %s" % (vpn_name, res)) exit(1) elif args.create_client: default_variables = config['defaults'].copy() default_variables.update(vpng.get_server_variables(vpn_name)) variables = create_variables(vpng.get_client_vars(vpn_name), default_variables) if variables is None: res = vpngen.VpnGenError.VpnDoesNotExists else: res = vpng.create_client(vpn_name, client_name, variables) if res == vpngen.VpnGenError.Success: print("Client %s created successfully on VPN %s" % (client_name, vpn_name)) else: eprint("Failed to create client %s on VPN %s: %s" % (client_name, vpn_name, res)) exit(1) elif args.remove_client: res = vpng.remove_client(vpn_name, client_name) if res == vpngen.VpnGenError.Success: print("Client %s removed successfully on VPN %s" % (client_name, vpn_name)) else: eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res)) exit(1) elif args.rebuild_client: default_variables = config['defaults'].copy() default_variables.update(vpng.get_server_variables(vpn_name)) default_variables.update(vpng.get_client_variables(vpn_name, client_name)) res = vpng.rebuild_client(vpn_name, client_name, default_variables) if res == vpngen.VpnGenError.Success: print("Client %s configuration rebuilt successfully on VPN %s" % (client_name, vpn_name)) else: eprint("Failed to rebuild client %s configuration on VPN %s: %s" % (client_name, vpn_name, res)) exit(1) elif args.rebuild_clients: default_variables = config['defaults'].copy() default_variables.update(vpng.get_server_variables(vpn_name)) for client_name in vpng.get_client_list(vpn_name): variables = default_variables.copy() variables.update(vpng.get_client_variables(vpn_name, client_name)) res = vpng.rebuild_client(vpn_name, client_name, variables) if res == vpngen.VpnGenError.Success: print("Client %s configuration rebuilt successfully on VPN %s" % (client_name, vpn_name)) else: eprint("Failed to rebuild client %s configuration on VPN %s: %s" % (client_name, vpn_name, res)) exit(1) main()