Files
linux-cloud-router/debian-package/src/usr/lib/linux-manager/linuxmanager.py
T

1481 lines
70 KiB
Python

"""Shared library for the linux-manager package.
Everything derivable from the configuration file alone, with no running
network required. Used by both 'configure' and 'setup'.
"""
import contextlib
import fcntl
import ipaddress
import json
import os
import pathlib
import re
import shutil
import socket
import subprocess
import sys
import tempfile
import time
import urllib.parse
import jinja2
import ldap
LIB_DIR = pathlib.Path(__file__).resolve().parent
if str(LIB_DIR) not in sys.path:
sys.path.insert(0, str(LIB_DIR))
import linuxmanager_ldap
import linuxmanager_pki
import linuxmanager_platforms
import linuxmanager_status
from linuxmanager_net import DEFAULT_LISTEN, LISTEN_CHOICES, LOOPBACK, MANAGER_LOOPBACK_PORT, METRICS_LOOPBACK_PORT, \
WEB_LOOPBACK_PORT, ConfigError, detect_interfaces, interface_addresses, ip_output
CONFIG_FILE = pathlib.Path('/etc/linux-manager/linux-manager.json')
CONFIG_LOCK = pathlib.Path('/run/linux-manager/config.lock')
#: What setup resolved and runs, for the web service (setup's refresh_web).
WEB_STATE = pathlib.Path('/run/linux-manager/web.json')
#: The unit of linux-manager-setup, which request_setup runs.
SETUP_UNIT = 'linux-manager-setup.service'
#: The lock under which a run of setup is requested and a run of setup decides whether to run again.
SETUP_LOCK = pathlib.Path('/run/linux-manager/setup.lock')
#: The lock a run of setup holds while it applies the configuration.
SETUP_RUNNING = pathlib.Path('/run/linux-manager/setup.running')
#: The file that asks a run of setup in progress to run once more when it ends.
SETUP_REQUEST = pathlib.Path('/run/linux-manager/setup.request')
TEMPLATE_DIR = pathlib.Path('/usr/share/linux-manager/templates')
SWANCTL_CONF_D = pathlib.Path('/etc/swanctl/conf.d')
SITE_TO_SITE = SWANCTL_CONF_D / 'site-to-site.conf'
ROAD_WARRIOR = SWANCTL_CONF_D / 'road-warrior.conf'
#: The drop-in of systemd-resolved that earlier versions wrote for the remote access clients; removed when found.
P2S_FORWARDER = pathlib.Path('/etc/systemd/resolved.conf.d/p2s-forwarder.conf')
#: The configuration of dnsmasq, the resolver of the remote access clients (linux-manager-resolver.service).
RESOLVER_CONF = pathlib.Path('/etc/linux-manager/dnsmasq.conf')
WG_CONF = pathlib.Path('/etc/wireguard/wg0.conf')
#: The server's WireGuard public key, which setup writes with its private key.
WG_PUBLIC_KEY = pathlib.Path('/etc/wireguard/wg0.pub')
#: The lines around the [Peer] sections of the users' WireGuard peers in WG_CONF, which setup writes from the directory.
WG_PEERS_BEGIN = "# BEGIN WireGuard peers of the users of the server's directory"
WG_PEERS_END = "# END WireGuard peers of the users of the server's directory"
OPENVPN_CONF = pathlib.Path('/etc/openvpn/server/linux-manager.conf')
OPENVPN_LDAP = pathlib.Path('/etc/openvpn/server/linux-manager-ldap.conf')
# section -> option -> default; the type of the default is the required type.
SCHEMA = {
'general': {'platform': 'none', 'mode': 'manual'},
'router': {'enabled': False},
'vpn': {'enabled': False},
'interfaces': {'external': '', 'internal': ''},
'wan': {'local_fqdn': '', 'public_address': ''},
'local': {'cidrs': [], 'int_addr': '', 'int_gateway_ip': ''},
'site_to_site': {},
'p2s': {'enabled': False, 'address_pool': '', 'dns_listen': 'internal', 'dns_domains': []},
'wireguard': {'enabled': False, 'address': '', 'listen_port': 51820},
'openvpn': {'enabled': False, 'address_pool': '', 'port': 1194, 'protocol': 'udp'},
'docker': {'mode': 'auto', 'exposed_ports': []},
'web': {'enabled': True, 'listen': DEFAULT_LISTEN, 'port': 7081, 'database': '', 'trusted_proxies': [],
'status_content': 'basic', 'shell': False, 'shell_idle_minutes': 15, 'shell_auth_timeout': 15,
'log_files': []},
'ssh': {'ldap_login': False},
'https': {'email': '', 'web_port': 7444, 'manager_port': 7443, 'ip_sans': False,
'disable_auto_redirect': False, 'acme_staging': False, 'certificate': 'self-signed', 'self_signed_days': 365,
'acme_provider': 'letsencrypt', 'acme_url': '', 'acme_eab_key_id': '', 'acme_eab_mac_key': '',
'zerossl_api_key': ''},
'oidc': {'enabled': False},
'sites': {'enabled': False, 'upload_max_mb': 512, 'webdav': False, 'webdav_port': 7445},
'dns': {'enabled': False, 'secondary': '', 'listen': 'external', 'ttl': 300},
'pki': {'root_name': 'Linux Manager Root CA', 'root_days': 7300, 'ca_days': 3650, 'key': 'rsa-4096',
'crl_days': 7, 'root_crl_days': 180, 'p2s_name': 'Linux Manager VPN CA',
's2s_name': 'Linux Manager Site-to-Site CA', 'servers_name': 'Linux Manager Servers CA',
'issuing_cas': {}, 'templates': {},
'extended_key_usages': {}, 'root': 'local', 'simple_ca_url': ''},
'metrics': {'enabled': True, 'listen': 'internal', 'port': 9464, 'https_port': 9465, 'service_account': 'metrics',
'otlp_endpoint': '', 'otlp_username': '', 'otlp_password': '',
'keep_details': '24 hours', 'keep_hourly': '1 month', 'keep_daily': '3 months',
'keep_monthly': '1 year', 'keep_yearly': '10 years'},
'backup': {'enabled': False, 'schedule': 'daily', 'target': 'local', 'path': '/var/backups/linux-manager',
'keep_hourly': 24, 'keep_daily': 7, 'keep_weekly': 4, 'keep_monthly': 12, 'keep_yearly': 0,
'metrics': False, 'rclone_remote': '', 'rclone_mode': 'sync',
'pbs_repository': '', 'pbs_password': '', 'pbs_fingerprint': '', 'pbs_namespace': '',
'pbs_keyfile': ''},
}
#: site_to_site maps a connection name to its settings: option -> default, the type of the default being the required type.
CONNECTION = {'addrs': [], 'id': '', 'cidrs': [], 'local_cidrs': [], 'local_id_mode': 'fqdn', 'auth': 'psk', 'psk': '',
'psk_file': '', 'remote_ca': ''}
#: How a site_to_site connection authenticates: a pre-shared key, or certificates.
CONNECTION_AUTH = ('psk', 'certificate')
#: The name of a site_to_site connection, which also names its swanctl connection, child and secret.
CONNECTION_NAME = re.compile(r'^[a-z][a-z0-9-]{0,30}$')
#: The site_to_site connection the install answers fill.
FIRST_CONNECTION = 'remote-site'
#: The retention settings of the metrics, from the details to the yearly summaries.
METRICS_KEEP = ('keep_details', 'keep_hourly', 'keep_daily', 'keep_monthly', 'keep_yearly')
#: A retention period as PostgreSQL reads an interval: a count and a unit.
METRICS_INTERVAL = re.compile(r'^[1-9]\d{0,4} (hour|day|month|year)s?$')
#: How often backups run, as systemd OnCalendar shorthands, from the most to the least often.
BACKUP_SCHEDULES = ('hourly', 'daily', 'weekly')
#: Where backups go: an archive in backup.path, or a Proxmox Backup Server.
BACKUP_TARGETS = ('local', 'pbs')
#: How rclone follows backup.path in the cloud: sync mirrors it, copy deletes nothing remotely.
RCLONE_MODES = ('sync', 'copy')
#: The retention settings of local backups, from the hourly to the yearly ones.
BACKUP_KEEP = ('keep_hourly', 'keep_daily', 'keep_weekly', 'keep_monthly', 'keep_yearly')
#: A remote of rclone.conf and an optional path: remote:path.
RCLONE_REMOTE = re.compile(r'^[\w.][\w. -]*:\S*$')
#: A PBS repository: [[auth-id@]server[:port]:]datastore.
PBS_REPOSITORY = re.compile(r'^(\S+:)?[A-Za-z0-9_][A-Za-z0-9._-]*$')
#: The SHA-256 fingerprint of a server certificate, as PBS shows it.
PBS_FINGERPRINT = re.compile(r'^([0-9A-Fa-f]{2}:){31}[0-9A-Fa-f]{2}$')
#: A PBS namespace: up to seven names joined by /.
PBS_NAMESPACE = re.compile(r'^[A-Za-z0-9_][A-Za-z0-9._-]*(/[A-Za-z0-9_][A-Za-z0-9._-]*){0,6}$')
#: The port of Let's Encrypt's HTTP challenge, which Caddy answers while HTTPS is on.
ACME_HTTP_PORT = 80
#: The default port of HTTPS.
HTTPS_DEFAULT_PORT = 443
#: The most files web.log_files lists.
LOG_FILES_MAX = 32
#: The port rclone serves WebDAV on, on the loopback address, behind Caddy.
WEBDAV_LOOPBACK_PORT = 7446
#: The installable components, managed on the Manager's System tab: name -> (section, setting) of their switch and the
#: packages linux-manager-install@<name> installs from the Ubuntu archive while the switch is on.
STRONGSWAN_PACKAGES = ('strongswan-swanctl', 'charon-systemd', 'libstrongswan-extra-plugins', 'libcharon-extra-plugins')
COMPONENTS = {
'vpn': (('vpn', 'enabled'), (*STRONGSWAN_PACKAGES, 'openvpn', 'openvpn-auth-ldap', 'wireguard-tools', 'dnsmasq-base')),
'router': (('router', 'enabled'), STRONGSWAN_PACKAGES),
'dns': (('dns', 'enabled'), ('pdns-server', 'pdns-backend-pgsql')),
'webdav': (('sites', 'webdav'), ('rclone',)),
}
#: The largest sites.upload_max_mb, 10 GiB.
UPLOAD_MAX_MB = 10240
#: Settings that no longer exist, section -> options, left out when the file is read so that a file of an earlier
#: version loads: the name server is wan.local_fqdn, a site chooses the address of its records, and the CA certificates
#: and CRLs are always published, and HTTPS is always on.
RETIRED = {'dns': ('nameserver', 'addresses'), 'pki': ('publish',), 'https': ('enabled',)}
#: The shortest and the longest dns.ttl, in seconds.
DNS_TTL_RANGE = (60, 86400)
#: A DNS name without a wildcard, in lowercase, of the name servers and the domains of the name server.
ZONE_NAME = re.compile(r'^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)+$')
#: A domain of p2s.dns_domains, one label or more, and the most domains.
DNS_DOMAIN = re.compile(r'^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$')
DNS_DOMAINS_MAX = 16
#: The ports of the bootstrap service.
BOOTSTRAP_PORTS = (17081,)
#: The settings of the Manager, and its port when they set none.
MANAGER_CONFIG = pathlib.Path('/etc/linux-manager/manager.json')
MANAGER_PORT = 7080
#: The ACME directory of Let's Encrypt's staging environment, Caddy's only CA while https.acme_staging is true.
ACME_STAGING_CA = 'https://acme-staging-v02.api.letsencrypt.org/directory'
#: The ACME directory of Let's Encrypt's production CA.
ACME_PRODUCTION_CA = 'https://acme-v02.api.letsencrypt.org/directory'
#: Where the certificate of wan.local_fqdn comes from, https.certificate: ACME, a self-signed certificate, the
#: Servers Issuing CA (servers), or a certificate of another CA, imported; self-signed when not set. A new installation's
#: configuration file has it empty (generate-config) until the first setup chooses ca or self-signed
#: (linuxmanager_pki.provision).
HTTPS_CERTIFICATES = ('acme', 'self-signed', 'ca', 'external')
#: The ACME CA of the server and of the web sites with TLS acme, https.acme_provider.
ACME_PROVIDERS = ('letsencrypt', 'zerossl', 'custom')
#: The key types of the CAs, pki.key, and of the templates.
PKI_KEYS = ('rsa-2048', 'rsa-3072', 'rsa-4096', 'ec-p256', 'ec-p384')
#: Where the root CA comes from, pki.root: the server creates it, an offline CA, or a Simple CA service.
PKI_ROOTS = ('local', 'offline', 'simple-ca')
#: The key usages a template may give, as the arguments of cryptography's KeyUsage.
KEY_USAGES = ('digital_signature', 'content_commitment', 'key_encipherment', 'data_encipherment', 'key_agreement')
#: The built-in extended key usages a template may give, name -> OID; pki.extended_key_usages adds custom ones.
EXTENDED_KEY_USAGES = {
'server_auth': '1.3.6.1.5.5.7.3.1',
'client_auth': '1.3.6.1.5.5.7.3.2',
'code_signing': '1.3.6.1.5.5.7.3.3',
'email_protection': '1.3.6.1.5.5.7.3.4',
'time_stamping': '1.3.6.1.5.5.7.3.8',
'ocsp_signing': '1.3.6.1.5.5.7.3.9',
'ipsec_ike': '1.3.6.1.5.5.7.3.17',
'ike_intermediate': '1.3.6.1.5.5.8.2.2',
}
#: The name of a custom extended key usage of pki.extended_key_usages.
EKU_NAME = re.compile(r'^[a-z][a-z0-9_-]{0,30}$')
#: An object identifier in dotted form, of a custom extended key usage or a certificate policy.
OID = re.compile(r'^[0-2](\.(0|[1-9][0-9]*)){1,63}$')
#: What the common name of a template's certificates is: a DNS name, any name, or an e-mail address.
TEMPLATE_SUBJECTS = ('dns', 'name', 'email')
#: The subject alternative names a template's certificates may have.
SAN_TYPES = ('dns', 'ip', 'email')
#: A certificate template: option -> default, the type of the default being the required type.
TEMPLATE = {'key': 'rsa-3072', 'days': 365, 'key_usage': ['digital_signature'], 'extended_key_usage': [],
'subject': 'name', 'sans': [], 'policies': []}
#: The built-in templates; pki.templates changes their options and adds templates of its own.
PKI_TEMPLATES = {
'vpn-server': {'key_usage': ['digital_signature', 'key_encipherment'],
'extended_key_usage': ['server_auth', 'ike_intermediate'], 'subject': 'dns', 'sans': ['dns', 'ip']},
'vpn-user': {'extended_key_usage': ['client_auth'], 'subject': 'name', 'sans': ['dns', 'email']},
'mtls': {'key_usage': ['digital_signature', 'key_encipherment'],
'extended_key_usage': ['server_auth', 'client_auth'], 'subject': 'dns', 'sans': ['dns', 'ip']},
'server': {'key_usage': ['digital_signature', 'key_encipherment'], 'extended_key_usage': ['server_auth'],
'subject': 'dns', 'sans': ['dns', 'ip']},
'client': {'extended_key_usage': ['client_auth'], 'subject': 'name', 'sans': ['dns', 'email']},
}
#: The templates only the server issues: its VPN server certificate and the users' certificates.
ROUTER_TEMPLATES = ('vpn-server', 'vpn-user')
#: The built-in issuing CAs: name -> the pki option of the common name and the templates the CA issues: the VPN, the
#: site-to-site connections, and the Servers Issuing CA of mTLS certificates, which also signs the server's HTTPS
#: certificate.
BUILTIN_CAS = {'p2s': ('p2s_name', ['vpn-server', 'vpn-user']), 's2s': ('s2s_name', ['mtls']),
'servers': ('servers_name', ['mtls'])}
#: A general-purpose issuing CA of pki.issuing_cas: option -> default, the type of the default being the required type.
ISSUING_CA = {'name': '', 'templates': [], 'acme': False}
#: The name of an issuing CA or a template; an issuing CA's name also names its files.
PKI_NAME = re.compile(r'^[a-z][a-z0-9-]{0,30}$')
#: The longest common name of a CA, in characters.
CA_NAME_MAX = 64
#: What / and /status of the web service show, web.status_content, from the least to the most.
STATUS_CONTENTS = ('basic', 'standard', 'full')
#: The longest web.shell_idle_minutes and web.shell_auth_timeout, a day.
SHELL_IDLE_MAX = 1440
DOCKER_MODES = ('auto', 'on', 'off')
OPENVPN_PROTOCOLS = ('udp', 'tcp')
#: The tun device of the OpenVPN server.
OPENVPN_DEVICE = 'ovpn0'
#: A port or a port range as iptables multiport takes it: 443 or 8000:8100.
DOCKER_PORT = re.compile(r'^\d{1,5}(:\d{1,5})?$')
#: The multiport match takes at most 15 ports, and a range counts as two.
DOCKER_PORT_LIMIT = 15
TYPE_NAMES = {str: 'a string', list: 'a list of strings', bool: 'a boolean',
int: 'an integer', dict: 'an object'}
ID_MODES = ('fqdn', 'public_ip', 'internal_ip')
#: How much the operator supplies, and therefore how much may be detected.
#: manual - names and addresses given; nothing is detected
#: interfaces - names given; addresses and gateway derived from them
#: auto - nothing given; the interfaces are worked out too, accepting
#: that the result may be wrong
MODES = ('manual', 'interfaces', 'auto')
#: platform = none means configuration is deferred: files are installed and
#: nothing else happens until the operator says otherwise.
PLATFORM_NONE = 'none'
def warn(message):
print(f'linux-manager: warning: {message}', file=sys.stderr)
def fail(message):
"""End with status 1; SystemExit carries the message, which Python prints to standard error."""
sys.exit(f'linux-manager: error: {message}')
def read_settings(path=None):
"""The settings in the configuration file, without the defaults; {} when the file does not exist.
Raises ConfigError when the file is not JSON or not an object of sections.
"""
path = pathlib.Path(path) if path else CONFIG_FILE
try:
data = json.loads(path.read_text(encoding='utf-8'))
except FileNotFoundError:
return {}
except ValueError as exc:
raise ConfigError(f'cannot parse {path}: {exc}')
if not isinstance(data, dict):
raise ConfigError(f'cannot parse {path}: the top level is not an object')
for section, options in data.items():
if not isinstance(options, dict):
raise ConfigError(f'cannot parse {path}: {section} is not an object')
for section, options in RETIRED.items():
for option in options:
data.get(section, {}).pop(option, None)
return data
def with_defaults(data):
"""The configuration of the settings of read_settings, with the defaults for anything absent.
The result maps section -> option -> value, and site_to_site maps each connection name to
its settings filled from CONNECTION. Sections, options and connections that are not
objects are kept so that validate() can report them.
"""
cfg = {section: dict(options) for section, options in SCHEMA.items()}
for section, options in data.items():
cfg.setdefault(section, {}).update(options)
cfg['site_to_site'] = {name: {**CONNECTION, **options} if isinstance(options, dict) else options
for name, options in cfg['site_to_site'].items()}
return cfg
def load_config(path=None):
"""Read the configuration, filling in defaults for anything absent (see with_defaults).
The path is resolved when called, not when this function is defined, so
CONFIG_FILE stays overridable.
"""
try:
return with_defaults(read_settings(path))
except ConfigError as exc:
fail(str(exc))
@contextlib.contextmanager
def config_lock():
"""Hold the lock of the configuration file from reading it to writing it with save_config."""
CONFIG_LOCK.parent.mkdir(parents=True, exist_ok=True)
with open(CONFIG_LOCK, 'a', encoding='utf-8') as handle:
fcntl.flock(handle, fcntl.LOCK_EX)
yield
def setup_lock():
"""The open SETUP_LOCK, held; closing it releases the lock."""
SETUP_LOCK.parent.mkdir(parents=True, exist_ok=True)
handle = open(SETUP_LOCK, 'a', encoding='utf-8')
fcntl.flock(handle, fcntl.LOCK_EX)
return handle
def request_setup(wait=False):
"""Have linux-manager-setup apply the configuration; with wait, True when it has applied it and its last run
succeeded, without wait, True when the run was requested.
A run in progress is not cut short: it holds SETUP_RUNNING, and the request, SETUP_REQUEST, has it run once more
when it ends, however many requests it got. Without a run in progress the unit is restarted. The request is made
under SETUP_LOCK, under which a run of setup also decides whether to end, so no request is missed.
"""
with setup_lock():
SETUP_REQUEST.touch()
with open(SETUP_RUNNING, 'a', encoding='utf-8') as running:
try:
fcntl.flock(running, fcntl.LOCK_EX | fcntl.LOCK_NB)
idle = True
except BlockingIOError:
idle = False
if idle:
restart = subprocess.run(['systemctl', 'restart', '--no-block', SETUP_UNIT], capture_output=True)
if restart.returncode != 0:
return False
# The unit's start job, the restarted run or the run in progress with its further runs, is waited for.
while wait:
state = subprocess.run(['systemctl', 'show', '-p', 'Job,ActiveState,Result', SETUP_UNIT],
capture_output=True, text=True).stdout
values = dict(line.partition('=')[::2] for line in state.splitlines())
if not values.get('Job') and values.get('ActiveState') != 'activating':
return values.get('ActiveState') == 'active' and values.get('Result') == 'success'
time.sleep(1)
return True
def save_config(data):
"""Write the settings to the configuration file, indented, with mode 0600, replacing it in one step.
The caller holds config_lock.
"""
descriptor, name = tempfile.mkstemp(dir=CONFIG_FILE.parent, prefix='.linux-manager.json.')
try:
with os.fdopen(descriptor, 'w', encoding='utf-8') as handle:
handle.write(json.dumps(data, indent=2) + '\n')
os.chmod(name, 0o600)
os.replace(name, CONFIG_FILE)
except OSError:
os.unlink(name)
raise
def choose_https_certificate(cfg, ctx, source):
"""Write https.certificate, empty until then, as source into the configuration file, and into cfg and ctx for the
rest of the run: the first setup's choice of the server's HTTPS certificate."""
with config_lock():
data = read_settings()
section = data.get('https')
data['https'] = {**(section if isinstance(section, dict) else {}), 'certificate': source}
save_config(data)
cfg['https']['certificate'] = source
ctx['https_certificate'] = source
warn(f'https.certificate is set to {source}, chosen by the first setup')
def _split_list(value):
return [item.strip() for item in value.split(',') if item.strip()]
def _get(cfg, section, option):
value = cfg[section][option]
return value.strip() if isinstance(value, str) else value
def _clean(values):
return [item.strip() for item in values if item.strip()]
def _get_list(cfg, section, option):
return _clean(cfg[section][option])
def _check_options(options, defaults, prefix):
for option, value in options.items():
if option not in defaults:
raise ConfigError(f'{prefix}.{option}: unknown setting')
expected = type(defaults[option])
if type(value) is not expected or (
expected is list and
not all(isinstance(item, str) for item in value)):
raise ConfigError(
f'{prefix}.{option} must be {TYPE_NAMES[expected]}')
def _check_types(cfg):
for section, options in cfg.items():
if section not in SCHEMA:
raise ConfigError(f'{section}: unknown section')
if section != 'site_to_site':
_check_options(options, SCHEMA[section], section)
continue
for name, connection in options.items():
if not isinstance(connection, dict):
raise ConfigError(f'site_to_site.{name} must be {TYPE_NAMES[dict]}')
_check_options(connection, CONNECTION, f'site_to_site.{name}')
def _check_networks(values, name):
for item in values:
try:
ipaddress.ip_network(item, strict=False)
except ValueError:
raise ConfigError(f'{name}: {item!r} is not a valid CIDR')
def _check_address(value, name):
if not value:
return
try:
ipaddress.ip_address(value)
except ValueError:
raise ConfigError(f'{name}: {value!r} is not a valid IP address')
def validate(cfg):
"""Reject values that are present but malformed. Absent is not an error.
The exception is general.mode, which states what the operator promised to
supply, so the values that mode requires are checked for presence too.
"""
_check_types(cfg)
platform = _get(cfg, 'general', 'platform')
installed = [PLATFORM_NONE] + linuxmanager_platforms.available()
if platform and platform not in installed:
raise ConfigError(
f'general.platform: {platform!r} is not installed, expected one of '
f'{", ".join(installed)}')
mode = _get(cfg, 'general', 'mode')
if mode and mode not in MODES:
raise ConfigError(
f'general.mode: {mode!r} is not one of {", ".join(MODES)}')
router = _get(cfg, 'router', 'enabled')
if not deferred(cfg):
required = {
'manual': (('interfaces', 'external'), ('interfaces', 'internal'),
('local', 'int_addr'), ('local', 'int_gateway_ip')),
'interfaces': (('interfaces', 'external'), ('interfaces', 'internal')),
'auto': (),
}[mode or 'manual']
# Without the router the server has one interface, interfaces.external.
if not router:
required = tuple(item for item in required if item == ('interfaces', 'external'))
for section, option in required:
if not _get(cfg, section, option):
raise ConfigError(
f'{section}.{option} is required in mode {mode or "manual"}')
if cfg['site_to_site'] and not router:
raise ConfigError('site_to_site: site-to-site connections need router.enabled')
if not _get(cfg, 'vpn', 'enabled'):
for system in ('p2s', 'wireguard', 'openvpn'):
if _get(cfg, system, 'enabled'):
raise ConfigError(f'{system}.enabled needs vpn.enabled')
identities = [('wan.local_fqdn', _get(cfg, 'wan', 'local_fqdn'))]
for name, connection in cfg['site_to_site'].items():
prefix = f'site_to_site.{name}'
if not CONNECTION_NAME.match(name):
raise ConfigError(
f'{prefix}: a connection name starts with a lowercase letter and has at most 31 '
'lowercase letters, digits and hyphens')
identities.append((f'{prefix}.id', connection['id'].strip()))
id_mode = connection['local_id_mode'].strip()
if id_mode and id_mode not in ID_MODES:
raise ConfigError(
f'{prefix}.local_id_mode: {id_mode!r} is not one of {", ".join(ID_MODES)}')
_check_networks(_clean(connection['cidrs']), f'{prefix}.cidrs')
_check_networks(_clean(connection['local_cidrs']), f'{prefix}.local_cidrs')
if connection['auth'] not in CONNECTION_AUTH:
raise ConfigError(f'{prefix}.auth: {connection["auth"]!r} is not one of {", ".join(CONNECTION_AUTH)}')
if connection['remote_ca'].strip() and not linuxmanager_pki.is_ca_certificate(connection['remote_ca']):
raise ConfigError(f'{prefix}.remote_ca is not the PEM certificate of a CA')
for option, value in identities:
if value.startswith('@'):
raise ConfigError(
f'{option}: remove the leading @; strongSwan reads an '
'address as an IP identity and a name as an FQDN')
_check_networks(_get_list(cfg, 'local', 'cidrs'), 'local.cidrs')
pool = _get(cfg, 'p2s', 'address_pool')
_check_networks([pool] if pool else [], 'p2s.address_pool')
openvpn_pool = _get(cfg, 'openvpn', 'address_pool')
if openvpn_pool:
try:
if ipaddress.ip_network(openvpn_pool).version != 4:
raise ValueError
except ValueError:
raise ConfigError(f'openvpn.address_pool: {openvpn_pool!r} is not an IPv4 network')
for system, option in (('p2s', 'address_pool'), ('openvpn', 'address_pool'), ('wireguard', 'address')):
if _get(cfg, system, 'enabled') and not _get(cfg, system, option):
raise ConfigError(f'{system}.enabled needs {system}.{option}')
_check_address(_get(cfg, 'local', 'int_addr'), 'local.int_addr')
_check_address(_get(cfg, 'local', 'int_gateway_ip'), 'local.int_gateway_ip')
# In auto mode the names are worked out later, so there is nothing to check.
if mode != 'auto':
for option in ('external', 'internal') if router else ('external',):
name = _get(cfg, 'interfaces', option)
if name and not (pathlib.Path('/sys/class/net') / name).exists():
raise ConfigError(
f'interfaces.{option}: interface {name!r} does not exist')
wg_address = _get(cfg, 'wireguard', 'address')
if wg_address:
try:
interface = ipaddress.ip_interface(wg_address)
except ValueError:
raise ConfigError(
f'wireguard.address: {wg_address!r} is not an address with prefix')
# The server's own address on wg0, from which the peers' addresses of the same network are told apart.
network = interface.network
if network.num_addresses > 2 and interface.ip in (network.network_address, network.broadcast_address):
raise ConfigError(
f'wireguard.address: {wg_address} is the network or broadcast address of {network}; give the '
f'router\'s own address in it, such as {next(network.hosts())}/{network.prefixlen}')
port = _get(cfg, 'wireguard', 'listen_port')
if not 1 <= port <= 65535:
raise ConfigError(f'wireguard.listen_port: {port} is not a valid port')
openvpn_port = _get(cfg, 'openvpn', 'port')
if not 1 <= openvpn_port <= 65535:
raise ConfigError(f'openvpn.port: {openvpn_port} is not a valid port')
protocol = _get(cfg, 'openvpn', 'protocol')
if protocol not in OPENVPN_PROTOCOLS:
raise ConfigError(
f'openvpn.protocol: {protocol!r} is not one of {", ".join(OPENVPN_PROTOCOLS)}')
web_port = _get(cfg, 'web', 'port')
if isinstance(web_port, bool) or not isinstance(web_port, int) or not 1 <= web_port <= 65535:
raise ConfigError(f'web.port: {web_port!r} is not a valid port')
web_listen = _get(cfg, 'web', 'listen')
if web_listen not in LISTEN_CHOICES:
raise ConfigError(
f'web.listen: {web_listen!r} is not one of {", ".join(LISTEN_CHOICES)}')
status_content = _get(cfg, 'web', 'status_content')
if status_content not in STATUS_CONTENTS:
raise ConfigError(
f'web.status_content: {status_content!r} is not one of {", ".join(STATUS_CONTENTS)}')
shell_idle = _get(cfg, 'web', 'shell_idle_minutes')
if not 1 <= shell_idle <= SHELL_IDLE_MAX:
raise ConfigError(f'web.shell_idle_minutes: {shell_idle} is not between 1 and {SHELL_IDLE_MAX}')
shell_auth = _get(cfg, 'web', 'shell_auth_timeout')
if not 0 <= shell_auth <= SHELL_IDLE_MAX:
raise ConfigError(f'web.shell_auth_timeout: {shell_auth} is not between 0 and {SHELL_IDLE_MAX}')
https_ports = []
for option in ('web_port', 'manager_port'):
port = _get(cfg, 'https', option)
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
raise ConfigError(f'https.{option}: {port!r} is not a valid port')
if port == ACME_HTTP_PORT:
raise ConfigError(f'https.{option}: {port} is the port of the HTTP challenge')
https_ports.append(port)
if https_ports[0] == https_ports[1]:
raise ConfigError('https.web_port and https.manager_port must differ')
for item in _get_list(cfg, 'web', 'trusted_proxies'):
_check_address(item, 'web.trusted_proxies')
log_files = _get_list(cfg, 'web', 'log_files')
if len(log_files) > LOG_FILES_MAX:
raise ConfigError(f'web.log_files: at most {LOG_FILES_MAX} files')
for item in log_files:
if not item.startswith('/') or '/../' in f'{item}/':
raise ConfigError(f'web.log_files: {item!r} is not an absolute path')
if _get(cfg, 'https', 'certificate') not in ('', *HTTPS_CERTIFICATES):
raise ConfigError(f'https.certificate: {_get(cfg, "https", "certificate")!r} is not one of '
f'{", ".join(HTTPS_CERTIFICATES)}, or empty for the next setup to choose')
if not 1 <= _get(cfg, 'https', 'self_signed_days') <= 3650:
raise ConfigError('https.self_signed_days must be 1 to 3650 days')
provider = _get(cfg, 'https', 'acme_provider')
if provider not in ACME_PROVIDERS:
raise ConfigError(f'https.acme_provider: {provider!r} is not one of {", ".join(ACME_PROVIDERS)}')
acme_url = _get(cfg, 'https', 'acme_url')
if provider == 'custom' and not acme_url:
raise ConfigError('https.acme_url is required while https.acme_provider is custom')
if acme_url:
parts = urllib.parse.urlsplit(acme_url)
if parts.scheme != 'https' or not parts.hostname or parts.fragment:
raise ConfigError('https.acme_url must be an https:// URL without a fragment')
for option in ('email', 'acme_url', 'acme_eab_key_id', 'acme_eab_mac_key', 'zerossl_api_key'):
if any(character.isspace() or character in '{}"' for character in _get(cfg, 'https', option)):
raise ConfigError(f'https.{option} must not hold spaces, braces or quotes')
if _get(cfg, 'https', 'certificate') == 'acme' and not _get(cfg, 'wan', 'local_fqdn'):
raise ConfigError('https.certificate: an ACME CA issues the certificate of wan.local_fqdn, which is not set')
if _get(cfg, 'oidc', 'enabled') and not _get(cfg, 'web', 'enabled'):
raise ConfigError('oidc.enabled: the OpenID Provider is the web service served by Caddy at '
'https://<HTTPS name>:<https.web_port>, which needs web.enabled')
upload_max = _get(cfg, 'sites', 'upload_max_mb')
if not 1 <= upload_max <= UPLOAD_MAX_MB:
raise ConfigError(f'sites.upload_max_mb: {upload_max} is not between 1 and {UPLOAD_MAX_MB}')
_check_address(_get(cfg, 'wan', 'public_address'), 'wan.public_address')
dns = cfg['dns']
if dns['enabled'] and not (ZONE_NAME.match(_get(cfg, 'wan', 'local_fqdn'))
and not re.search(r'\.[0-9]+$', _get(cfg, 'wan', 'local_fqdn'))):
raise ConfigError('dns.enabled needs wan.local_fqdn, a DNS name in lowercase: the primary name server of the '
'domains')
secondary = _get(cfg, 'dns', 'secondary')
if secondary and not ZONE_NAME.match(secondary):
raise ConfigError(f'dns.secondary: {secondary!r} is not a DNS name in lowercase')
if secondary and secondary == _get(cfg, 'wan', 'local_fqdn'):
raise ConfigError('dns.secondary: the secondary name server is not wan.local_fqdn, the primary')
if dns['listen'] not in LISTEN_CHOICES:
raise ConfigError(f'dns.listen: {dns["listen"]!r} is not one of {", ".join(LISTEN_CHOICES)}')
if not DNS_TTL_RANGE[0] <= dns['ttl'] <= DNS_TTL_RANGE[1]:
raise ConfigError(f'dns.ttl: {dns["ttl"]} is not between {DNS_TTL_RANGE[0]} and {DNS_TTL_RANGE[1]} seconds')
dns_domains = _get_list(cfg, 'p2s', 'dns_domains')
if len(dns_domains) > DNS_DOMAINS_MAX or len(set(dns_domains)) != len(dns_domains):
raise ConfigError(f'p2s.dns_domains: at most {DNS_DOMAINS_MAX} different domains')
for item in dns_domains:
if not DNS_DOMAIN.match(item):
raise ConfigError(f'p2s.dns_domains: {item!r} is not a DNS name in lowercase')
resolver_listen = _get(cfg, 'p2s', 'dns_listen')
if resolver_listen not in LISTEN_CHOICES:
raise ConfigError(f'p2s.dns_listen: {resolver_listen!r} is not one of {", ".join(LISTEN_CHOICES)}')
sides = {'internal': {'internal'}, 'external': {'external'}, 'both': {'internal', 'external'}}
remote_access = any(_get(cfg, system, 'enabled') for system in ('p2s', 'openvpn', 'wireguard'))
if dns['enabled'] and remote_access and sides[dns['listen']] & sides[resolver_listen]:
raise ConfigError(f'dns.listen: port 53 of the {" and ".join(sorted(sides[dns["listen"]] & sides[resolver_listen]))} '
f'addresses is the resolver of the remote access clients (p2s.dns_listen {resolver_listen}) '
'while p2s.enabled, openvpn.enabled or wireguard.enabled is true')
for option in ('root_name', 'p2s_name', 's2s_name', 'servers_name'):
value = _get(cfg, 'pki', option)
if not value or len(value) > CA_NAME_MAX or not value.isprintable():
raise ConfigError(f'pki.{option} must have 1 to {CA_NAME_MAX} printable characters')
for option in ('root_days', 'ca_days', 'crl_days', 'root_crl_days'):
if not 1 <= _get(cfg, 'pki', option) <= 36500:
raise ConfigError(f'pki.{option} must be 1 to 36500 days')
if _get(cfg, 'pki', 'root') not in PKI_ROOTS:
raise ConfigError(f'pki.root: {_get(cfg, "pki", "root")!r} is not one of {", ".join(PKI_ROOTS)}')
simple_ca_url = _get(cfg, 'pki', 'simple_ca_url')
if _get(cfg, 'pki', 'root') == 'simple-ca' and not simple_ca_url:
raise ConfigError('pki.simple_ca_url is required while pki.root is simple-ca')
if simple_ca_url:
parts = urllib.parse.urlsplit(simple_ca_url)
if parts.scheme != 'https' or not parts.hostname or parts.query or parts.fragment:
raise ConfigError('pki.simple_ca_url must be an https:// URL without a query or a fragment')
if _get(cfg, 'pki', 'key') not in PKI_KEYS:
raise ConfigError(f'pki.key: {_get(cfg, "pki", "key")!r} is not one of {", ".join(PKI_KEYS)}')
for name, oid in cfg['pki']['extended_key_usages'].items():
prefix = f'pki.extended_key_usages.{name}'
if not EKU_NAME.match(name) or name in EXTENDED_KEY_USAGES:
raise ConfigError(f'{prefix}: an extended key usage name starts with a lowercase letter, has at most 31 '
'lowercase letters, digits, hyphens and underscores, and is not a built-in one')
if not isinstance(oid, str) or not OID.match(oid):
raise ConfigError(f'{prefix} must be an OID in dotted form, such as 1.3.6.1.4.1.311.20.2.2')
usages = extended_key_usages(cfg)
for name, template in cfg['pki']['templates'].items():
prefix = f'pki.templates.{name}'
if not PKI_NAME.match(name):
raise ConfigError(f'{prefix}: a template name starts with a lowercase letter and has at most 31 lowercase '
'letters, digits and hyphens')
if not isinstance(template, dict):
raise ConfigError(f'{prefix} must be {TYPE_NAMES[dict]}')
_check_options(template, TEMPLATE, prefix)
templates = pki_templates(cfg)
for name, template in templates.items():
prefix = f'pki.templates.{name}'
if template['key'] not in PKI_KEYS:
raise ConfigError(f'{prefix}.key: {template["key"]!r} is not one of {", ".join(PKI_KEYS)}')
if not 1 <= template['days'] <= 36500:
raise ConfigError(f'{prefix}.days must be 1 to 36500 days')
for option, allowed in (('key_usage', KEY_USAGES), ('extended_key_usage', tuple(usages)),
('sans', SAN_TYPES)):
for item in template[option]:
if item not in allowed:
raise ConfigError(f'{prefix}.{option}: {item!r} is not one of {", ".join(allowed)}')
for item in template['policies']:
if not OID.match(item):
raise ConfigError(f'{prefix}.policies: {item!r} is not an OID in dotted form, such as 1.3.6.1.4.1.32473.1')
if not template['key_usage']:
raise ConfigError(f'{prefix}.key_usage needs at least one usage')
if template['subject'] not in TEMPLATE_SUBJECTS:
raise ConfigError(f'{prefix}.subject: {template["subject"]!r} is not one of {", ".join(TEMPLATE_SUBJECTS)}')
for name, authority in cfg['pki']['issuing_cas'].items():
prefix = f'pki.issuing_cas.{name}'
if not PKI_NAME.match(name) or name in BUILTIN_CAS or name in ('root', 'upstream'):
raise ConfigError(f'{prefix}: an issuing CA name starts with a lowercase letter and has at most 31 '
f'lowercase letters, digits and hyphens, and is not root, upstream or '
f'{", ".join(BUILTIN_CAS)}')
if not isinstance(authority, dict):
raise ConfigError(f'{prefix} must be {TYPE_NAMES[dict]}')
_check_options(authority, ISSUING_CA, prefix)
for name, authority in issuing_cas(cfg).items():
prefix = f'pki.issuing_cas.{name}'
if authority['builtin']:
continue
common_name = authority['name'].strip()
if not common_name or len(common_name) > CA_NAME_MAX or not common_name.isprintable():
raise ConfigError(f'{prefix}.name must have 1 to {CA_NAME_MAX} printable characters')
if not authority['templates']:
raise ConfigError(f'{prefix}.templates needs at least one template')
for template in authority['templates']:
if template not in templates:
raise ConfigError(f'{prefix}.templates: {template!r} is not a template')
if template in ROUTER_TEMPLATES:
raise ConfigError(f'{prefix}.templates: {template} is issued by the P2S CA only')
taken = {web_port: 'web.port', ACME_HTTP_PORT: 'the HTTP challenge',
https_ports[0]: 'https.web_port', https_ports[1]: 'https.manager_port'}
# Caddy serves the Manager's plain HTTP port and calls the services on their loopback ports.
for port, owner in ((manager_port(), f"the Manager's port in {MANAGER_CONFIG}"),
(WEB_LOOPBACK_PORT, 'the web service behind Caddy'),
(MANAGER_LOOPBACK_PORT, 'the Manager behind Caddy'),
(METRICS_LOOPBACK_PORT, 'the metrics service behind Caddy'),
(WEBDAV_LOOPBACK_PORT, 'WebDAV behind Caddy')):
if port in taken:
raise ConfigError(f'{taken[port]}: {port} is the port of {owner}')
taken[port] = owner
for section_name, option in (('metrics', 'port'), ('metrics', 'https_port'), ('sites', 'webdav_port')):
port = _get(cfg, section_name, option)
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
raise ConfigError(f'{section_name}.{option}: {port!r} is not a valid port')
if port in taken:
raise ConfigError(f'{section_name}.{option}: {port} is the port of {taken[port]}')
taken[port] = f'{section_name}.{option}'
metrics_listen = _get(cfg, 'metrics', 'listen')
if metrics_listen not in LISTEN_CHOICES:
raise ConfigError(
f'metrics.listen: {metrics_listen!r} is not one of {", ".join(LISTEN_CHOICES)}')
endpoint = _get(cfg, 'metrics', 'otlp_endpoint')
if endpoint and urllib.parse.urlsplit(endpoint).scheme not in ('http', 'https'):
raise ConfigError(f'metrics.otlp_endpoint: {endpoint!r} is not an http or https URL')
if bool(_get(cfg, 'metrics', 'otlp_username')) != bool(_get(cfg, 'metrics', 'otlp_password')):
raise ConfigError('metrics.otlp_username and metrics.otlp_password are given together')
for option in METRICS_KEEP:
keep = _get(cfg, 'metrics', option)
if not METRICS_INTERVAL.match(keep):
raise ConfigError(
f'metrics.{option}: {keep!r} is not a count and a unit, such as "24 hours" or "3 months"')
schedule = _get(cfg, 'backup', 'schedule')
if schedule not in BACKUP_SCHEDULES:
raise ConfigError(
f'backup.schedule: {schedule!r} is not one of {", ".join(BACKUP_SCHEDULES)}')
target = _get(cfg, 'backup', 'target')
if target not in BACKUP_TARGETS:
raise ConfigError(f'backup.target: {target!r} is not one of {", ".join(BACKUP_TARGETS)}')
for option in ('path', 'pbs_keyfile'):
value = _get(cfg, 'backup', option)
if value and not value.startswith('/'):
raise ConfigError(f'backup.{option}: {value!r} is not an absolute path')
if not _get(cfg, 'backup', 'path'):
raise ConfigError('backup.path is required')
for option in BACKUP_KEEP:
if _get(cfg, 'backup', option) < 0:
raise ConfigError(f'backup.{option} must not be negative')
if target == 'local' and not any(_get(cfg, 'backup', option) for option in backup_keeps(schedule)):
raise ConfigError(
f'backup: with the {schedule} schedule, at least one of {", ".join(backup_keeps(schedule))} '
'is above 0')
remote = _get(cfg, 'backup', 'rclone_remote')
if remote and not RCLONE_REMOTE.match(remote):
raise ConfigError(f'backup.rclone_remote: {remote!r} is not a remote and a path, such as azure:backups/router')
rclone_mode = _get(cfg, 'backup', 'rclone_mode')
if rclone_mode not in RCLONE_MODES:
raise ConfigError(f'backup.rclone_mode: {rclone_mode!r} is not one of {", ".join(RCLONE_MODES)}')
if target == 'pbs':
repository = _get(cfg, 'backup', 'pbs_repository')
if not repository or not _get(cfg, 'backup', 'pbs_password'):
raise ConfigError('backup.pbs_repository and backup.pbs_password are required with the pbs target')
if not PBS_REPOSITORY.match(repository):
raise ConfigError(
f'backup.pbs_repository: {repository!r} is not of the form user@realm!token@host:datastore')
fingerprint = _get(cfg, 'backup', 'pbs_fingerprint')
if fingerprint and not PBS_FINGERPRINT.match(fingerprint):
raise ConfigError(f'backup.pbs_fingerprint: {fingerprint!r} is not 32 hexadecimal pairs joined by colons')
namespace = _get(cfg, 'backup', 'pbs_namespace')
if namespace and not PBS_NAMESPACE.match(namespace):
raise ConfigError(f'backup.pbs_namespace: {namespace!r} is not up to seven names joined by /')
docker_mode = _get(cfg, 'docker', 'mode')
if docker_mode not in DOCKER_MODES:
raise ConfigError(
f'docker.mode: {docker_mode!r} is not one of {", ".join(DOCKER_MODES)}')
ports = _get_list(cfg, 'docker', 'exposed_ports')
weight = 0
for item in ports:
if not DOCKER_PORT.match(item) or any(
not 1 <= int(part) <= 65535 for part in item.split(':')):
raise ConfigError(
f'docker.exposed_ports: {item!r} is not a port or a port range')
weight += 2 if ':' in item else 1
first, _, last = item.partition(':')
caddy_ports = (web_port, manager_port(), _get(cfg, 'metrics', 'port'), ACME_HTTP_PORT, *https_ports)
for port in caddy_ports:
if int(first) <= port <= int(last or first):
raise ConfigError(f'docker.exposed_ports: {item!r} takes port {port}, which Caddy serves')
if weight > DOCKER_PORT_LIMIT:
raise ConfigError(
f'docker.exposed_ports: at most {DOCKER_PORT_LIMIT} ports are allowed, '
'a range counting as two')
def backup_keeps(schedule):
"""The retention settings of local backups that apply to the schedule: none finer than it."""
return BACKUP_KEEP[BACKUP_SCHEDULES.index(schedule):]
def pki_templates(cfg):
"""The certificate templates: the built-in ones (PKI_TEMPLATES) and those of pki.templates, each filled from TEMPLATE."""
custom = cfg['pki']['templates']
return {name: {**TEMPLATE, **PKI_TEMPLATES.get(name, {}),
**(custom.get(name) if isinstance(custom.get(name), dict) else {})}
for name in [*PKI_TEMPLATES, *(name for name in custom if name not in PKI_TEMPLATES)]}
def extended_key_usages(cfg):
"""The extended key usages templates may give, name -> OID: the built-in ones (EXTENDED_KEY_USAGES) and those of
pki.extended_key_usages."""
custom = cfg['pki']['extended_key_usages']
return {**EXTENDED_KEY_USAGES, **{name: oid for name, oid in custom.items() if isinstance(oid, str)}}
def issuing_cas(cfg):
"""The issuing CAs, name -> {name, templates, acme, builtin}: p2s, s2s and servers (BUILTIN_CAS) with their common names
from the pki section, then those of pki.issuing_cas, each filled from ISSUING_CA."""
found = {name: {'name': _get(cfg, 'pki', option), 'templates': list(templates), 'acme': False, 'builtin': True}
for name, (option, templates) in BUILTIN_CAS.items()}
for name, options in cfg['pki']['issuing_cas'].items():
if isinstance(options, dict):
found[name] = {**ISSUING_CA, **options, 'builtin': False}
return found
def pki_base_urls(cfg):
"""The plain HTTP base URLs, ending in /, under which Caddy publishes the CA certificates and CRLs and the
certificates name them: the status web service's web.port while it is enabled, then the Manager's port; none without
wan.local_fqdn."""
fqdn = _get(cfg, 'wan', 'local_fqdn')
if not fqdn:
return []
ports = [*([_get(cfg, 'web', 'port')] if _get(cfg, 'web', 'enabled') else []), manager_port()]
return [f'http://{fqdn}:{port}/pki/' for port in ports]
def manager_port():
"""The port of the Manager, Caddy's plain HTTP site of it: port in its settings, or MANAGER_PORT when it is not set
or not a port."""
try:
port = json.loads(MANAGER_CONFIG.read_text(encoding='utf-8')).get('port')
except (OSError, ValueError, AttributeError):
return MANAGER_PORT
return port if isinstance(port, int) and not isinstance(port, bool) and 1 <= port <= 65535 else MANAGER_PORT
def manager_listen():
"""The interfaces the firewall opens the Manager's port on: listen in its settings, one of LISTEN_CHOICES, or
DEFAULT_LISTEN when it is not set or not one."""
try:
choice = json.loads(MANAGER_CONFIG.read_text(encoding='utf-8')).get('listen')
except (OSError, ValueError, AttributeError):
return DEFAULT_LISTEN
return choice if choice in LISTEN_CHOICES else DEFAULT_LISTEN
def oidc_issuer(cfg):
"""The issuer of the OpenID Provider: the web service's HTTPS site on the first HTTPS name, without the port when it
is HTTPS_DEFAULT_PORT."""
port = _get(cfg, 'https', 'web_port')
return f'https://{https_names(cfg)[0]}' + ('' if port == HTTPS_DEFAULT_PORT else f':{port}')
def resolve_psk(connection, name):
"""Return the pre-shared key of the site_to_site connection: psk_file wins, else psk; '' when none is configured."""
psk_file = connection['psk_file'].strip()
if psk_file:
path = pathlib.Path(psk_file)
if not path.is_file():
raise ConfigError(f'site_to_site.{name}.psk_file: {psk_file} does not exist')
return path.read_text(encoding='utf-8').strip()
return connection['psk']
def platform_module(cfg):
"""Import the configured platform module, falling back to the default."""
name = _get(cfg, 'general', 'platform') or linuxmanager_platforms.DEFAULT
if name == PLATFORM_NONE:
# Not normally reached: configure() returns before this when deferred.
name = linuxmanager_platforms.DEFAULT
try:
return linuxmanager_platforms.load(name)
except ModuleNotFoundError:
warn(f'platform {name!r} is not installed, using '
f'{linuxmanager_platforms.DEFAULT}')
return linuxmanager_platforms.load(linuxmanager_platforms.DEFAULT)
def _join(*groups):
items = []
for group in groups:
for item in group:
if item not in items:
items.append(item)
return ', '.join(items)
def deferred(cfg):
"""True when platform is none, meaning nothing is to be configured yet."""
return (_get(cfg, 'general', 'platform') or PLATFORM_NONE) == PLATFORM_NONE
def detect_gateway(iface, address):
"""Derive the next hop on the internal side.
The routing table is the honest source. When it has nothing to say, fall
back to the first host address of the connected subnet, which is a
convention rather than a fact - so say so.
"""
for line in ip_output('-o', 'route', 'show', 'dev', iface).splitlines():
fields = line.split()
if 'via' in fields:
return fields[fields.index('via') + 1]
for line in ip_output('-o', '-4', 'addr', 'show', 'dev', iface).splitlines():
fields = line.split()
if len(fields) >= 4 and fields[3].split('/')[0] == address:
network = ipaddress.ip_interface(fields[3]).network
gateway = str(next(network.hosts()))
warn(f'{iface}: no gateway in the routing table, assuming {gateway} '
f'as the first host of {network}')
return gateway
return ''
def resolve_interfaces(cfg):
"""Return (ext_iface, int_iface, local_addrs, int_addr, int_gateway_ip).
What is read from the system and what is taken verbatim depends entirely on
general.mode. In manual mode nothing below touches the system at all.
Without the router the server has one interface: interfaces.external is also
the internal side, with its first address and no gateway.
"""
mode = _get(cfg, 'general', 'mode') or 'manual'
router = _get(cfg, 'router', 'enabled')
ext_iface = _get(cfg, 'interfaces', 'external')
int_iface = _get(cfg, 'interfaces', 'internal')
int_addr = _get(cfg, 'local', 'int_addr')
gateway = _get(cfg, 'local', 'int_gateway_ip')
if mode == 'auto':
ext_iface, int_iface = detect_interfaces(internal=router)
local_addrs = interface_addresses(ext_iface, 'interfaces.external')
if ext_iface and not local_addrs:
warn(f'{ext_iface} has no IPv4 address yet')
if not router:
return ext_iface, ext_iface, local_addrs, local_addrs[0] if local_addrs else '', ''
if mode == 'manual':
return ext_iface, int_iface, local_addrs, int_addr, gateway
int_addrs = interface_addresses(int_iface, 'interfaces.internal')
int_addr = int_addrs[0] if int_addrs else ''
if int_iface and not int_addr:
warn(f'{int_iface} has no IPv4 address yet')
gateway = detect_gateway(int_iface, int_addr) if int_addr else ''
return ext_iface, int_iface, local_addrs, int_addr, gateway
def build_context(cfg, public_ip=None):
"""Assemble the template context from the configuration.
public_ip is the address wan.local_fqdn resolves to, the local identity of the
site_to_site connections in mode public_ip; setup resolves it once the network is up and
renders again, and without it such a connection is not ready.
"""
extras = linuxmanager_platforms.context(platform_module(cfg))
ext_iface, int_iface, local_addrs, int_addr, gateway = resolve_interfaces(cfg)
local_fqdn = _get(cfg, 'wan', 'local_fqdn')
local_cidrs = _get_list(cfg, 'local', 'cidrs')
openvpn_pool = _get(cfg, 'openvpn', 'address_pool')
openvpn_network = ipaddress.ip_network(openvpn_pool) if openvpn_pool else None
identities = {'fqdn': local_fqdn, 'public_ip': public_ip or '', 'internal_ip': int_addr}
# The addresses the remote access clients' resolver answers on, which p2s.dns_listen chooses.
resolver_addrs = [address for address in {'internal': [int_addr], 'external': local_addrs,
'both': [int_addr, *local_addrs]}[_get(cfg, 'p2s', 'dns_listen')]
if address]
connections = []
for name, connection in cfg['site_to_site'].items():
id_mode = connection['local_id_mode'].strip() or 'fqdn'
remote_cidrs = _clean(connection['cidrs'])
connection_cidrs = _clean(connection['local_cidrs'])
auth = connection['auth']
entry = {
'name': name,
'remote_addrs': ', '.join(_clean(connection['addrs'])),
'remote_id': connection['id'].strip(),
'remote_cidrs': ', '.join(remote_cidrs),
'remote_cidr_list': remote_cidrs,
'local_ts': _join(connection_cidrs, _split_list(extras['EXTRA_LOCAL_TS'])),
'local_cidr_list': connection_cidrs,
'local_id_mode': id_mode,
'local_id': identities.get(id_mode, ''),
'auth': auth,
'psk': resolve_psk(connection, name) if auth == 'psk' else '',
'remote_cacert': linuxmanager_pki.remote_ca_file(name) if connection['remote_ca'].strip()
else linuxmanager_pki.SWANCTL_CA_FILES['s2s'],
}
required = ('remote_addrs', 'remote_id', 'remote_cidrs', 'local_id', *(('psk',) if auth == 'psk' else ()))
entry['ready'] = bool(local_addrs and connection_cidrs and all(entry[key] for key in required))
connections.append(entry)
return {
'platform': _get(cfg, 'general', 'platform') or PLATFORM_NONE,
'mode': _get(cfg, 'general', 'mode') or 'manual',
'router_enabled': _get(cfg, 'router', 'enabled'),
'vpn_enabled': _get(cfg, 'vpn', 'enabled'),
'router_installed': packages_installed(COMPONENTS['router'][1]),
'vpn_installed': packages_installed(COMPONENTS['vpn'][1]),
'wan_iface': ext_iface,
'int_iface': int_iface,
'local_addrs': ', '.join(local_addrs),
'local_addr_list': local_addrs,
'local_fqdn': local_fqdn,
'local_cidrs': ', '.join(local_cidrs),
'local_cidr_list': local_cidrs,
'local_ts': _join(local_cidrs, _split_list(extras['EXTRA_LOCAL_TS'])),
'int_addr': int_addr,
'int_gateway_ip': gateway,
'connections': connections,
'p2s_enabled': _get(cfg, 'p2s', 'enabled'),
'p2s_address_pool': _get(cfg, 'p2s', 'address_pool'),
'public_ip': public_ip or '',
'p2s_resolver_addrs': resolver_addrs,
'p2s_dns_domains': _get_list(cfg, 'p2s', 'dns_domains'),
'p2s_dns': _join(resolver_addrs, _split_list(extras['EXTRA_P2S_DNS'])),
'wg_enabled': _get(cfg, 'wireguard', 'enabled'),
'wg_address': _get(cfg, 'wireguard', 'address'),
'wg_network': str(ipaddress.ip_interface(_get(cfg, 'wireguard', 'address')).network)
if _get(cfg, 'wireguard', 'address') else '',
'wg_listen_port': _get(cfg, 'wireguard', 'listen_port'),
'wg_peers_begin': WG_PEERS_BEGIN,
'wg_peers_end': WG_PEERS_END,
'openvpn_enabled': _get(cfg, 'openvpn', 'enabled'),
'openvpn_pool': openvpn_pool,
'openvpn_network': str(openvpn_network.network_address) if openvpn_network else '',
'openvpn_netmask': str(openvpn_network.netmask) if openvpn_network else '',
'openvpn_routes': [(str(network.network_address), str(network.netmask))
for network in (ipaddress.ip_network(cidr, strict=False) for cidr in local_cidrs)],
'openvpn_port': _get(cfg, 'openvpn', 'port'),
'openvpn_protocol': _get(cfg, 'openvpn', 'protocol'),
'openvpn_device': OPENVPN_DEVICE,
'openvpn_ldap_conf': str(OPENVPN_LDAP),
'openvpn_status': linuxmanager_status.OPENVPN_STATUS,
'openvpn_status_seconds': linuxmanager_status.OPENVPN_STATUS_SECONDS,
'p2s_dns_list': _split_list(_join(resolver_addrs, _split_list(extras['EXTRA_P2S_DNS']))),
'openvpn_ca': str(linuxmanager_pki.OPENVPN_CA),
'openvpn_crl': str(linuxmanager_pki.OPENVPN_CRL),
'server_cert': str(linuxmanager_pki.VPN_SERVER_CERT),
'server_key': str(linuxmanager_pki.VPN_SERVER_KEY),
'server_certificate': False,
'ldap_uri': linuxmanager_ldap.URI,
'ldap_users': linuxmanager_ldap.USERS,
'web_enabled': _get(cfg, 'web', 'enabled'),
'web_port': _get(cfg, 'web', 'port'),
'web_listen': _get(cfg, 'web', 'listen'),
'https_names': https_names(cfg),
'https_ips': https_addresses(cfg, local_addrs, int_addr),
'mdns': not local_fqdn,
'https_email': _get(cfg, 'https', 'email'),
'https_certificate': _get(cfg, 'https', 'certificate'),
'https_acme_provider': _get(cfg, 'https', 'acme_provider'),
'https_acme_url': (_get(cfg, 'https', 'acme_url') if _get(cfg, 'https', 'acme_provider') == 'custom'
else ACME_STAGING_CA if _get(cfg, 'https', 'acme_staging') else ACME_PRODUCTION_CA),
'https_acme_eab': ((_get(cfg, 'https', 'acme_eab_key_id'), _get(cfg, 'https', 'acme_eab_mac_key'))
if _get(cfg, 'https', 'acme_eab_key_id') and _get(cfg, 'https', 'acme_eab_mac_key')
else None),
'https_zerossl_api_key': _get(cfg, 'https', 'zerossl_api_key'),
'https_web_port': _get(cfg, 'https', 'web_port'),
'https_manager_port': _get(cfg, 'https', 'manager_port'),
'pki_acme': [name for name, authority in issuing_cas(cfg).items() if authority['acme']],
'metrics_enabled': _get(cfg, 'metrics', 'enabled'),
'metrics_port': _get(cfg, 'metrics', 'port'),
'metrics_listen': _get(cfg, 'metrics', 'listen'),
'metrics_https_port': _get(cfg, 'metrics', 'https_port'),
'metrics_service_account': _get(cfg, 'metrics', 'service_account'),
'sites_enabled': _get(cfg, 'sites', 'enabled'),
'webdav_enabled': _get(cfg, 'sites', 'webdav'),
'webdav_port': _get(cfg, 'sites', 'webdav_port'),
'dns_enabled': _get(cfg, 'dns', 'enabled'),
'dns_listen': _get(cfg, 'dns', 'listen'),
'loopback': LOOPBACK,
}
def s2s_ready(ctx):
"""True when the router is on and installed and any site_to_site connection is ready."""
return bool(ctx['router_enabled'] and ctx['router_installed'] and any(connection['ready'] for connection in ctx['connections']))
def routing_ready(ctx):
"""Routing and NAT need the router on, both interfaces named, plus the subnets involved."""
return bool(ctx['router_enabled'] and all([ctx['local_cidrs'], ctx['int_gateway_ip'],
ctx['int_iface'], ctx['wan_iface']]))
def p2s_ready(ctx):
"""IKEv2 access needs the VPN on and installed, its settings and the server's VPN server certificate, which the P2S
CA issues."""
return bool(ctx['vpn_enabled'] and ctx['vpn_installed'] and ctx['p2s_enabled'] and ctx['p2s_address_pool'] and ctx['local_fqdn']
and ctx['server_certificate'])
def wg_ready(ctx):
"""WireGuard needs the VPN on and installed and its settings."""
return bool(ctx['vpn_enabled'] and ctx['vpn_installed'] and ctx['wg_enabled'] and ctx['wg_address'])
def openvpn_ready(ctx):
"""OpenVPN needs the VPN on and installed, its settings and the server's VPN server certificate, which the P2S CA
issues."""
return bool(ctx['vpn_enabled'] and ctx['vpn_installed'] and ctx['openvpn_enabled'] and ctx['openvpn_pool'] and ctx['local_fqdn']
and ctx['server_certificate'])
def vpn_pools(ctx):
"""The address pools of the ready remote access systems: IKEv2, OpenVPN and WireGuard."""
return [pool for ready, pool in ((p2s_ready(ctx), ctx['p2s_address_pool']),
(openvpn_ready(ctx), ctx['openvpn_pool']),
(wg_ready(ctx), ctx['wg_network'])) if ready]
def https_names(cfg):
"""The names of the server's HTTPS certificate and sites: wan.local_fqdn alone when it is set, else the host name
and <host name>.local, which Avahi announces with mDNS."""
fqdn = _get(cfg, 'wan', 'local_fqdn').lower()
if fqdn:
return [fqdn]
host = socket.gethostname().split('.')[0].lower()
return [host, f'{host}.local']
def https_addresses(cfg, local_addrs, int_addr):
"""The IP addresses of the server's HTTPS certificate and sites while https.ip_sans is on: those of its interfaces
and wan.public_address, without link-local ones; [] while it is off."""
if not _get(cfg, 'https', 'ip_sans'):
return []
found = []
for value in (*local_addrs, int_addr, _get(cfg, 'wan', 'public_address')):
try:
address = ipaddress.ip_address(str(value).split('/')[0])
except ValueError:
continue
if not address.is_link_local and str(address) not in found:
found.append(str(address))
return found
def metrics_ready(ctx):
"""The metrics service collects, keeps and serves the metrics while metrics.enabled is true."""
return bool(ctx['metrics_enabled'])
def webdav_ready(ctx):
"""WebDAV of the sites' folders is served by Caddy, on the server's HTTPS names, while it is on."""
return bool(ctx['webdav_enabled'])
def packages_installed(packages):
"""Whether every one of the packages is installed, as dpkg knows them."""
result = subprocess.run(['dpkg-query', '-W', '-f=${db:Status-Status}\n', *packages], capture_output=True, text=True)
states = result.stdout.split()
return len(states) == len(packages) and all(state == 'installed' for state in states)
def packages_automatic(packages):
"""The packages apt marks as installed automatically, which apt autoremove removes once nothing depends on them."""
result = subprocess.run(['apt-mark', 'showauto', *packages], capture_output=True, text=True)
return result.stdout.split() if result.returncode == 0 else []
def component_on(cfg, name):
"""Whether the switch of the component of COMPONENTS is on in the configuration."""
section, setting = COMPONENTS[name][0]
return bool(cfg[section][setting])
def dns_ready(ctx):
"""The name server answers for its domains while dns.enabled is true."""
return bool(ctx['dns_enabled'])
def _jinja_env():
return jinja2.Environment(
loader=jinja2.FileSystemLoader(str(TEMPLATE_DIR)),
keep_trailing_newline=True,
undefined=jinja2.StrictUndefined,
autoescape=False,
)
def render(env, ctx, template_name, dest, mode):
"""Render a template, writing only when the content changes."""
content = env.get_template(template_name).render(ctx)
dest = pathlib.Path(dest)
if dest.exists() and dest.read_text(encoding='utf-8') == content:
os.chmod(dest, mode)
return False
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_text(content, encoding='utf-8')
os.chmod(dest, mode)
return True
def existing_peers(path):
"""The block of the users' peers and the [Peer] sections added by hand of an existing WireGuard config, each ''
without one.
The block lies between WG_PEERS_BEGIN and WG_PEERS_END; the [Peer] sections outside it are the operator's, which
re-rendering the file keeps.
"""
path = pathlib.Path(path)
if not path.is_file():
return '', ''
text = path.read_text(encoding='utf-8')
block = ''
begin, end = text.find(WG_PEERS_BEGIN), text.find(WG_PEERS_END)
if begin != -1 and end > begin:
block = text[begin:end + len(WG_PEERS_END)]
text = text[:begin] + text[end + len(WG_PEERS_END):]
lines = text.splitlines(keepends=True)
for index, line in enumerate(lines):
if line.strip().lower() == '[peer]':
return block, ''.join(lines[index:])
return block, ''
def render_wg_conf(env, ctx, dest=WG_CONF, mode=0o600):
"""Render wg0.conf: the [Interface], the block of the users' peers of ctx's wg_peers, the block the file holds while
wg_peers is None, and the peers the operator added by hand, carried across."""
content = env.get_template('wg0.conf.j2').render(ctx).rstrip('\n') + '\n'
block, own = existing_peers(dest)
if ctx['wg_peers'] is not None:
block = env.get_template('wg0-peers.conf.j2').render(ctx).strip('\n') if ctx['wg_peers'] else ''
for part in (block, own):
if part.strip():
content += '\n' + part.strip('\n') + '\n'
dest = pathlib.Path(dest)
if dest.exists() and dest.read_text(encoding='utf-8') == content:
os.chmod(dest, mode)
return False
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_text(content, encoding='utf-8')
os.chmod(dest, mode)
return True
def remove(path):
"""Remove a generated file. Returns True when something was removed."""
path = pathlib.Path(path)
if path.exists():
path.unlink()
return True
return False
def install_file(src, dest, mode):
"""Copy src to dest when the content differs. Returns True on change."""
src, dest = pathlib.Path(src), pathlib.Path(dest)
if not src.is_file():
return False
if dest.exists() and dest.read_bytes() == src.read_bytes():
os.chmod(dest, mode)
return False
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(src, dest)
os.chmod(dest, mode)
return True
def configure(cfg=None, public_ip=None):
"""Render configuration files and provision PKI.
Returns (ctx, changed) where changed is a set naming what needs reloading:
'swanctl', 'resolved', 'resolver', 'wireguard', 'openvpn'.
"""
if cfg is None:
cfg = load_config()
# platform = none: configuration is deferred. Files are installed and
# nothing is rendered, provisioned or removed until a platform is chosen.
if deferred(cfg):
return None, set()
try:
validate(cfg)
ctx = build_context(cfg, public_ip=public_ip)
except ConfigError as exc:
fail(str(exc))
changed = set()
# Without the directory, which holds the PKI, the copies of the last run stay and what needs none is configured.
with linuxmanager_pki.locked():
try:
notices = linuxmanager_pki.provision(cfg, ctx)
if linuxmanager_pki.distribute(cfg):
changed.add('swanctl')
except linuxmanager_pki.Failure as failure:
fail(str(failure))
except ldap.LDAPError as exc:
notices = [f'the PKI is left as it is: the directory cannot be reached: {exc}']
# Without the directory the first setup has no Servers Issuing CA to choose.
if not cfg['https']['certificate']:
choose_https_certificate(cfg, ctx, 'self-signed')
for notice in notices:
warn(notice)
# The WireGuard peers of the users; None keeps those of wg0.conf while the directory cannot be reached.
ctx['wg_peers'] = None
if wg_ready(ctx):
try:
ctx['wg_peers'] = linuxmanager_ldap.wireguard_peers()
except ldap.LDAPError as exc:
warn(f'the WireGuard peers of the users are left as they are: the directory cannot be reached: {exc}')
# What needs the server's own certificates.
ctx['server_certificate'] = linuxmanager_pki.VPN_SERVER_CERT.is_file()
for connection in ctx['connections']:
if connection['auth'] == 'certificate':
connection['ready'] = connection['ready'] and linuxmanager_pki.S2S_CERT.is_file()
env = _jinja_env()
if s2s_ready(ctx):
if render(env, ctx, 'site-to-site.conf.j2', SITE_TO_SITE, 0o600):
changed.add('swanctl')
elif remove(SITE_TO_SITE):
changed.add('swanctl')
if p2s_ready(ctx):
if render(env, ctx, 'road-warrior.conf.j2', ROAD_WARRIOR, 0o600):
changed.add('swanctl')
elif remove(ROAD_WARRIOR):
changed.add('swanctl')
# The resolver of the remote access clients is dnsmasq on the addresses of p2s.dns_listen; systemd-resolved keeps
# its loopback stub only, so its records of the server's own name stay on the server.
if remove(P2S_FORWARDER):
changed.add('resolved')
if (p2s_ready(ctx) or openvpn_ready(ctx) or wg_ready(ctx)) and ctx['p2s_resolver_addrs']:
if render(env, ctx, 'dnsmasq.conf.j2', RESOLVER_CONF, 0o644):
changed.add('resolver')
elif remove(RESOLVER_CONF):
changed.add('resolver')
if wg_ready(ctx):
if render_wg_conf(env, ctx):
changed.add('wireguard')
elif remove(WG_CONF):
changed.add('wireguard')
if openvpn_ready(ctx):
if render(env, ctx, 'openvpn-server.conf.j2', OPENVPN_CONF, 0o600):
changed.add('openvpn')
if render(env, ctx, 'openvpn-ldap.conf.j2', OPENVPN_LDAP, 0o600):
changed.add('openvpn')
else:
for path in (OPENVPN_CONF, OPENVPN_LDAP):
if remove(path):
changed.add('openvpn')
return ctx, changed