#!/usr/bin/python3 """Write a candidate linux-manager.json to the path given on the command line. Called from postinst, which hands the result to ucf(1). ucf compares it with /etc/linux-manager/linux-manager.json and decides what to do about local changes, so this script never writes to /etc and never has to worry about destroying an administrator's edits. The candidate is the current /etc/linux-manager/linux-manager.json as it is, or the shipped defaults when there is none, with https.certificate empty for the first setup to choose, with every debconf answer that was given written over it. Without an answer that changes it, the candidate is the current file byte for byte, so an upgrade or a reconfiguration that changes nothing gives ucf nothing to ask; defaults the file leaves out are applied by linuxmanager.load_config() when the file is read. An empty answer means "leave alone", which is why clearing the pre-shared key from the debconf database after installation does not blank it on the next dpkg-reconfigure. Answers arrive as LINUX_MANAGER_* environment variables and are converted to the type of the setting they fill: comma-separated lists become arrays, "true" and "false" become booleans and digits become integers. The site-to-site answers fill the connection linuxmanager.FIRST_CONNECTION of site_to_site; the local subnets fill both its local_cidrs and local.cidrs. """ import json import os import sys sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) import linuxmanager FIRST = ('site_to_site', linuxmanager.FIRST_CONNECTION) # environment variable -> the settings it fills, each (section, option) or (site_to_site, name, option) ANSWERS = { 'LINUX_MANAGER_PLATFORM': (('general', 'platform'),), 'LINUX_MANAGER_MODE': (('general', 'mode'),), 'LINUX_MANAGER_ROUTER_ENABLED': (('router', 'enabled'),), 'LINUX_MANAGER_VPN_ENABLED': (('vpn', 'enabled'),), 'LINUX_MANAGER_INT_ADDR': (('local', 'int_addr'),), 'LINUX_MANAGER_EXTERNAL_INTERFACE': (('interfaces', 'external'),), 'LINUX_MANAGER_INTERNAL_INTERFACE': (('interfaces', 'internal'),), 'LINUX_MANAGER_LOCAL_FQDN': (('wan', 'local_fqdn'),), 'LINUX_MANAGER_HTTPS_ENABLED': (('https', 'enabled'),), 'LINUX_MANAGER_HTTPS_DISABLE_AUTO_REDIRECT': (('https', 'disable_auto_redirect'),), 'LINUX_MANAGER_LOCAL_ID_MODE': ((*FIRST, 'local_id_mode'),), 'LINUX_MANAGER_LOCAL_CIDRS': (('local', 'cidrs'), (*FIRST, 'local_cidrs')), 'LINUX_MANAGER_INT_GATEWAY_IP': (('local', 'int_gateway_ip'),), 'LINUX_MANAGER_REMOTE_ADDRS': ((*FIRST, 'addrs'),), 'LINUX_MANAGER_REMOTE_ID': ((*FIRST, 'id'),), 'LINUX_MANAGER_REMOTE_CIDRS': ((*FIRST, 'cidrs'),), 'LINUX_MANAGER_PSK': ((*FIRST, 'psk'),), 'LINUX_MANAGER_P2S_ENABLED': (('p2s', 'enabled'),), 'LINUX_MANAGER_P2S_ADDRESS_POOL': (('p2s', 'address_pool'),), 'LINUX_MANAGER_PKI_ROOT': (('pki', 'root'),), 'LINUX_MANAGER_PKI_ROOT_NAME': (('pki', 'root_name'),), 'LINUX_MANAGER_PKI_SIMPLE_CA_URL': (('pki', 'simple_ca_url'),), 'LINUX_MANAGER_WG_ENABLED': (('wireguard', 'enabled'),), 'LINUX_MANAGER_WG_ADDRESS': (('wireguard', 'address'),), 'LINUX_MANAGER_WG_LISTEN_PORT': (('wireguard', 'listen_port'),), } def main(): if len(sys.argv) != 2: print('usage: generate-config OUTPUT', file=sys.stderr) return 2 output = sys.argv[1] try: current = linuxmanager.CONFIG_FILE.read_text(encoding='utf-8') except FileNotFoundError: current = None if current is None: cfg = linuxmanager.load_config() # A new installation leaves the source of the HTTPS certificate to its first setup. cfg['https']['certificate'] = '' else: try: cfg = json.loads(current) except ValueError as exc: linuxmanager.fail(f'cannot parse {linuxmanager.CONFIG_FILE}: {exc}') if not isinstance(cfg, dict): linuxmanager.fail(f'cannot parse {linuxmanager.CONFIG_FILE}: the top level is not an object') answered = False for variable, paths in ANSWERS.items(): answer = os.environ.get(variable) if not answer: continue for path in paths: # A value that does not convert is kept as a string for validate() to report. default = linuxmanager.CONNECTION[path[2]] if path[0] == 'site_to_site' else linuxmanager.SCHEMA[path[0]][path[1]] value = answer if isinstance(default, bool): value = value == 'true' elif isinstance(default, int): value = int(value) if value.isdigit() else value elif isinstance(default, list): value = [item.strip() for item in value.split(',') if item.strip()] target = cfg for key in path[:-1]: if not isinstance(target.setdefault(key, {}), dict): linuxmanager.fail(f'cannot parse {linuxmanager.CONFIG_FILE}: {".".join(path[:-1])} is not an object') target = target[key] if target.get(path[-1]) != value: target[path[-1]] = value answered = True # Without a change the candidate is the current file byte for byte, as ucf compares checksums. with open(output, 'w', encoding='utf-8') as handle: if current is not None and not answered: handle.write(current) else: json.dump(cfg, handle, indent=2) handle.write('\n') os.chmod(output, 0o600) return 0 if __name__ == '__main__': sys.exit(main())