feature: HTTPS is always on, for wan.local_fqdn or, without it, the host name and its mDNS name, optionally also for the server's addresses, and the redirect to HTTPS covers the status page by name.
Release / release (push) Successful in 23s

This commit is contained in:
slawek committed 2026-11-24 19:26:51 +01:00
1 parent 7efacfa3bb
commit 64c3ae4c36
18 files changed
+317 -187

No files matched your search

+47 -41
View File
@@ -80,9 +80,15 @@ CADDY_PKI_DIR = pathlib.Path('/etc/caddy/linux-manager-pki')
#: The certificates and keys of the sites of tls file, <site>/cert.pem and key.pem.
CADDY_SITES_DIR = pathlib.Path('/etc/caddy/linux-manager-sites')
#: The certificate and key of wan.local_fqdn Caddy serves when they come from no ACME CA.
#: The certificate and key of the HTTPS names Caddy serves when they come from no ACME CA.
CADDY_HTTPS_DIR = pathlib.Path('/etc/caddy/linux-manager-https')
#: The units of Avahi, which announces <host name>.local with mDNS while wan.local_fqdn is not set.
AVAHI_UNITS = ('avahi-daemon.socket', 'avahi-daemon.service')
#: The UDP port of mDNS.
MDNS_PORT = 5353
#: The unit of dnsmasq, the resolver of the remote access clients.
RESOLVER_UNIT = 'linux-manager-resolver.service'
@@ -122,6 +128,7 @@ BLOCKS = (
('# BOOTSTRAP RULES START', '# BOOTSTRAP RULES END'),
('# MANAGER RULES START', '# MANAGER RULES END'),
('# HTTPS RULES START', '# HTTPS RULES END'),
('# MDNS RULES START', '# MDNS RULES END'),
('# METRICS RULES START', '# METRICS RULES END'),
('# SITES RULES START', '# SITES RULES END'),
('# DNS RULES START', '# DNS RULES END'),
@@ -200,19 +207,6 @@ def strip_blocks(text):
return ''.join(out)
def is_configured(ctx):
"""True once any feature is actually configured: routing, a VPN system, or a listener of the core.
While nothing is configured the package leaves the firewall alone: an
install that has not been filled in yet must not start blocking traffic on
a host that was working before.
"""
return any((linuxmanager.s2s_ready(ctx), linuxmanager.routing_ready(ctx),
linuxmanager.p2s_ready(ctx), linuxmanager.wg_ready(ctx),
linuxmanager.openvpn_ready(ctx), ctx['web_enabled'], linuxmanager.https_ready(ctx),
linuxmanager.metrics_ready(ctx), linuxmanager.dns_ready(ctx), bool(ctx['sites'])))
def filter_block(ctx):
lines = []
@@ -277,6 +271,11 @@ def filter_block(ctx):
if ctx['web_enabled']:
lines += ['', '# WEB RULES START', *opened(ctx['web_listen'], ctx['web_port']), '# WEB RULES END']
# Without wan.local_fqdn, Avahi answers mDNS queries for <host name>.local on the internal side.
if ctx['mdns'] and internal:
lines += ['', '# MDNS RULES START', f'-A ufw-before-input -i {internal} -p udp --dport {MDNS_PORT} -j ACCEPT',
'# MDNS RULES END']
if not SETUP_DONE.exists() and ctx['int_iface']:
lines += [
'',
@@ -295,7 +294,7 @@ def filter_block(ctx):
# The ACME CA reaches the HTTP challenge on the external interface while it issues the server's certificate; the
# HTTPS ports are open on both.
if linuxmanager.https_ready(ctx) and interfaces:
if interfaces:
ports = [port for wanted, port in ((ctx['web_enabled'] or ctx['pki_acme'], ctx['https_web_port']),
(True, ctx['https_manager_port']),
(linuxmanager.metrics_ready(ctx), ctx['metrics_https_port']),
@@ -431,17 +430,6 @@ def setup_ufw(ctx):
if not BEFORE_RULES.is_file():
fail(f'{BEFORE_RULES} does not exist')
# Nothing configured: remove anything left from an earlier configuration
# and touch nothing else.
if not is_configured(ctx):
original = BEFORE_RULES.read_text(encoding='utf-8')
text = strip_blocks(original)
if text == original:
return False
BEFORE_RULES.write_text(text, encoding='utf-8')
remove_stale_nat(original, text)
return True
if UFW_DEFAULTS.is_file():
text = UFW_DEFAULTS.read_text(encoding='utf-8')
updated = ''.join(
@@ -728,6 +716,16 @@ def caddy_address(host, port, tls):
return f'{"http://" if tls == "none" else ""}{host}:{port}'
def manage_mdns(ctx):
"""Run Avahi, which announces <host name>.local, one of the HTTPS names, with mDNS, while wan.local_fqdn is not set,
and stop it while it is."""
active = unit_is_active(AVAHI_UNITS[1])
if ctx['mdns'] and not active:
run(['systemctl', 'enable', '--now', *AVAHI_UNITS], check=False)
elif not ctx['mdns'] and active:
run(['systemctl', 'disable', '--now', *AVAHI_UNITS], check=False)
def manage_resolver(changed):
"""Run dnsmasq, the resolver of the remote access clients, while configure wrote its configuration, restarting it when
that changed, and stop it when not. It starts after systemd-resolved has left the addresses it takes."""
@@ -745,11 +743,14 @@ def manage_caddy(cfg, ctx):
"""Run Caddy, a dependency of the package, which serves every HTTP and HTTPS port of the server's services and the
web sites; the services listen on loopback only.
CADDYFILE serves over plain HTTP the web service on web.port while it is enabled, the manager on its port, or a
redirect (308) to its HTTPS address while HTTPS is ready and https.disable_auto_redirect is off, and while metrics
are enabled the metrics service on metrics.port. While HTTPS is ready, it serves the web service on
https.web_port, the manager on https.manager_port, while metrics are enabled the metrics service on
metrics.https_port, and while WebDAV is ready WebDAV on sites.webdav_port, all for wan.local_fqdn. Every site calls
CADDYFILE serves over plain HTTP the web service on web.port while it is enabled and the manager on its port, and
while metrics are enabled the metrics service on metrics.port. While https.disable_auto_redirect is off, a request
of the web service for an HTTPS host is redirected (308) to https.web_port, and every request of the manager to
https.manager_port, on the host it named when that is an HTTPS host, else on the first HTTPS name. It serves the
web service on https.web_port, the manager on https.manager_port, while metrics are enabled the metrics service on
metrics.https_port, and while WebDAV is ready WebDAV on sites.webdav_port, all for the HTTPS hosts: the HTTPS names
(linuxmanager.https_names) and, with https.ip_sans and a certificate of its own, the addresses its certificate
names; a client without SNI gets the certificate of the first HTTPS name. Every site calls
its service on its loopback port. The certificate comes from the ACME CA of https.acme_provider, the global issuer,
with https.certificate acme, and otherwise is the one linuxmanager_pki.https_files gives, written to
CADDY_HTTPS_DIR; while that cannot be read, the HTTPS sites are left out, so that no ACME CA is asked for it. It
@@ -774,7 +775,7 @@ def manage_caddy(cfg, ctx):
group = grp.getgrnam(CADDY_GROUP).gr_gid
except KeyError:
fail(f'the group {CADDY_GROUP} does not exist; is the caddy package installed?')
https = linuxmanager.https_ready(ctx)
https = True
own = None
if https and cfg['https']['certificate'] != 'acme':
try:
@@ -832,8 +833,12 @@ def manage_caddy(cfg, ctx):
os.chown(folder / file, 0, group)
linuxmanager_sites.ensure_folders(site['name'] for site in sites)
names = linuxmanager.issuing_cas(cfg)
hosts = [*ctx['https_names'], *(ctx['https_ips'] if own is not None else [])]
caddy_ctx = dict(ctx, https=https, manager_port=linuxmanager.manager_port(),
redirect=https and not cfg['https']['disable_auto_redirect'],
https_name=ctx['https_names'][0],
https_hosts=[f'[{host}]' if ':' in host else host for host in hosts],
redirect_hosts=[host for host in hosts if ':' not in host],
web_loopback_port=linuxmanager.WEB_LOOPBACK_PORT,
manager_loopback_port=linuxmanager.MANAGER_LOOPBACK_PORT,
metrics_loopback_port=linuxmanager.METRICS_LOOPBACK_PORT,
@@ -1043,6 +1048,8 @@ def refresh_web(cfg, ctx):
'external_addresses': ctx['local_addr_list'],
'internal_address': ctx['int_addr'],
'internal_gateway': ctx['int_gateway_ip'],
'https_names': ctx['https_names'],
'https_addresses': ctx['https_ips'],
'local_cidrs': ctx['local_cidr_list'],
'client_dns': ctx['p2s_resolver_addrs'] if linuxmanager.RESOLVER_CONF.exists() else [],
'site_to_site': [{
@@ -1136,16 +1143,13 @@ def main():
progress('running', 'firewall')
rules_changed = setup_ufw(ctx)
if is_configured(ctx):
status = run(['ufw', 'status'], check=False)
if not status.stdout.startswith('Status: active'):
# Only ever enable the firewall after making sure SSH survives it.
run(['ufw', 'allow', '22/tcp'], check=False)
run(['ufw', '--force', 'enable'])
elif rules_changed:
run(['ufw', 'reload'])
status = run(['ufw', 'status'], check=False)
if not status.stdout.startswith('Status: active'):
# Only ever enable the firewall after making sure SSH survives it.
run(['ufw', 'allow', '22/tcp'], check=False)
run(['ufw', '--force', 'enable'])
elif rules_changed:
run(['ufw', 'reload'], check=False)
run(['ufw', 'reload'])
progress('running', 'strongSwan')
manage_strongswan(ctx, changed)
@@ -1168,6 +1172,8 @@ def main():
manage_openvpn(ctx, changed)
progress('running', 'HTTPS proxy')
manage_caddy(cfg, ctx)
progress('running', 'mDNS')
manage_mdns(ctx)
progress('running', 'WebDAV')
manage_webdav(ctx)
progress('running', 'name server')