2026-11-22 16:39:27 +01:00
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import ssl
|
|
|
|
|
import stat
|
|
|
|
|
import tomllib
|
|
|
|
|
from dataclasses import asdict, dataclass
|
|
|
|
|
from ipaddress import ip_address
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from platform import freedesktop_os_release
|
|
|
|
|
|
|
|
|
|
PLATFORMS = {
|
|
|
|
|
("ubuntu", "26.04"): ("8.5", True),
|
|
|
|
|
("debian", "13"): ("8.4", True),
|
|
|
|
|
("ubuntu", "24.04"): (None, False),
|
|
|
|
|
}
|
|
|
|
|
DNS_LABEL = re.compile(r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?")
|
|
|
|
|
FILE_PATH = re.compile(r"/[a-zA-Z0-9_./+-]+")
|
|
|
|
|
USERNAME = re.compile(r"[a-zA-Z0-9@.+_-]{1,150}")
|
2026-11-22 17:51:05 +01:00
|
|
|
SSH_USERNAME = re.compile(r"[a-z_][a-z0-9_-]{0,31}")
|
2026-11-22 16:39:27 +01:00
|
|
|
EMAIL = re.compile(r"[^\s@]+@[^\s@]+\.[^\s@]+")
|
2026-11-22 17:51:05 +01:00
|
|
|
REMOTE_MODES = {"remote", "remote-install"}
|
2026-11-22 16:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class InstallError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def keys(table, allowed, label):
|
|
|
|
|
if not isinstance(table, dict) or table.keys() - set(allowed):
|
|
|
|
|
raise InstallError(f"Check the fields in the {label} section.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def hostname(value, label):
|
|
|
|
|
if isinstance(value, str):
|
|
|
|
|
try:
|
|
|
|
|
value = value.encode("idna").decode("ascii").lower().rstrip(".")
|
|
|
|
|
labels = value.split(".")
|
|
|
|
|
if (
|
|
|
|
|
len(value) <= 253
|
|
|
|
|
and len(labels) >= 2
|
|
|
|
|
and all(DNS_LABEL.fullmatch(part) for part in labels)
|
|
|
|
|
):
|
|
|
|
|
return value
|
|
|
|
|
except UnicodeError:
|
|
|
|
|
pass
|
|
|
|
|
raise InstallError(f"Enter a fully qualified {label} hostname.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def address(value, label, *, private=False):
|
|
|
|
|
if not isinstance(value, str) or "%" in value:
|
|
|
|
|
raise InstallError(f"Enter a literal IP address for {label}.")
|
|
|
|
|
try:
|
|
|
|
|
ip = ip_address(value)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise InstallError(
|
|
|
|
|
f"Enter a literal IP address for {label}."
|
|
|
|
|
) from None
|
|
|
|
|
if (
|
|
|
|
|
ip.is_unspecified
|
|
|
|
|
or ip.is_multicast
|
|
|
|
|
or (private and (ip.is_global or not ip.is_private))
|
|
|
|
|
):
|
|
|
|
|
raise InstallError(f"Enter a usable address for {label}.")
|
|
|
|
|
return str(ip)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def port(value, label, default):
|
|
|
|
|
if value is None:
|
|
|
|
|
return default
|
|
|
|
|
if type(value) is not int or not 1 <= value <= 65535:
|
|
|
|
|
raise InstallError(f"Enter a port from 1 to 65535 for {label}.")
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def file_path(value, label):
|
|
|
|
|
if (
|
|
|
|
|
not isinstance(value, str)
|
|
|
|
|
or not FILE_PATH.fullmatch(value)
|
|
|
|
|
or ".." in Path(value).parts
|
|
|
|
|
):
|
|
|
|
|
raise InstallError(f"Enter an absolute file path for {label}.")
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def certificate(table, label):
|
|
|
|
|
chain = file_path(table.get("certificate"), f"the {label} certificate")
|
|
|
|
|
key = file_path(table.get("key"), f"the {label} certificate key")
|
|
|
|
|
try:
|
|
|
|
|
for value in (chain, key):
|
|
|
|
|
resolved = Path(value).resolve(strict=True)
|
|
|
|
|
if any(
|
|
|
|
|
resolved.is_relative_to(directory)
|
|
|
|
|
for directory in ("/root", "/home", "/run/user")
|
|
|
|
|
):
|
|
|
|
|
raise InstallError(
|
|
|
|
|
f"Place the {label} TLS files outside home directories."
|
|
|
|
|
)
|
|
|
|
|
ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER).load_cert_chain(
|
|
|
|
|
chain, key, password=""
|
|
|
|
|
)
|
|
|
|
|
except (OSError, ValueError) as error:
|
|
|
|
|
raise InstallError(
|
|
|
|
|
f"Check the certificate and key for {label}."
|
|
|
|
|
) from error
|
|
|
|
|
return chain, key
|
|
|
|
|
|
|
|
|
|
|
2026-11-22 17:51:05 +01:00
|
|
|
def tls_files(table, host, label, *, remote=False):
|
|
|
|
|
if table.get("tls", "provided") == "acme":
|
|
|
|
|
if "certificate" in table or "key" in table:
|
|
|
|
|
raise InstallError(f"Choose one certificate source for {label}.")
|
|
|
|
|
directory = Path("/etc/letsencrypt/live") / host
|
|
|
|
|
return str(directory / "fullchain.pem"), str(directory / "privkey.pem")
|
|
|
|
|
if table.get("tls", "provided") != "provided":
|
|
|
|
|
raise InstallError(f"Choose provided or acme TLS for {label}.")
|
|
|
|
|
if remote:
|
|
|
|
|
return (
|
|
|
|
|
file_path(table.get("certificate"), f"the {label} certificate"),
|
|
|
|
|
file_path(table.get("key"), f"the {label} certificate key"),
|
|
|
|
|
)
|
|
|
|
|
return certificate(table, label)
|
|
|
|
|
|
|
|
|
|
|
2026-11-22 16:39:27 +01:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class Role:
|
|
|
|
|
mode: str
|
|
|
|
|
host: str = ""
|
|
|
|
|
port: int = 22
|
|
|
|
|
host_key: str = ""
|
|
|
|
|
certificate: str = ""
|
|
|
|
|
key: str = ""
|
2026-11-22 17:51:05 +01:00
|
|
|
mail_hostname: str = ""
|
|
|
|
|
tls: str = "provided"
|
|
|
|
|
ssh_user: str = ""
|
|
|
|
|
ssh_identity: str = ""
|
2026-11-22 16:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def role(table, name):
|
|
|
|
|
if not isinstance(table, dict):
|
|
|
|
|
raise InstallError(f"Check the {name} section.")
|
|
|
|
|
mode = table.get("mode", "off")
|
|
|
|
|
if mode == "off" or (mode == "local" and name == "dns"):
|
|
|
|
|
keys(table, {"mode"}, name)
|
|
|
|
|
return Role(mode)
|
|
|
|
|
if mode == "local":
|
2026-11-22 17:51:05 +01:00
|
|
|
keys(table, {"mode", "host", "certificate", "key", "tls"}, name)
|
2026-11-22 16:39:27 +01:00
|
|
|
host = hostname(table.get("host"), "mail")
|
2026-11-22 17:51:05 +01:00
|
|
|
chain, key = tls_files(table, host, "mail")
|
|
|
|
|
return Role(
|
|
|
|
|
mode,
|
|
|
|
|
host=host,
|
|
|
|
|
certificate=chain,
|
|
|
|
|
key=key,
|
|
|
|
|
mail_hostname=host,
|
|
|
|
|
tls=table.get("tls", "provided"),
|
|
|
|
|
)
|
|
|
|
|
if mode in REMOTE_MODES:
|
|
|
|
|
allowed = {"mode", "host", "host_key", "port"}
|
|
|
|
|
if mode == "remote-install":
|
|
|
|
|
allowed |= {"ssh_user", "ssh_identity"}
|
|
|
|
|
if name == "mail":
|
|
|
|
|
allowed |= {"mail_hostname", "tls", "certificate", "key"}
|
|
|
|
|
keys(table, allowed, name)
|
2026-11-22 16:39:27 +01:00
|
|
|
host = table.get("host")
|
|
|
|
|
try:
|
|
|
|
|
normalized = address(host, f"the {name} node")
|
|
|
|
|
except InstallError:
|
|
|
|
|
normalized = hostname(host, f"{name} node")
|
|
|
|
|
host_key = file_path(table.get("host_key"), f"the {name} SSH host key")
|
|
|
|
|
try:
|
|
|
|
|
with Path(host_key).open(encoding="ascii") as source:
|
|
|
|
|
key = source.read(16385)
|
|
|
|
|
if len(key) > 16384 or len(key.strip().splitlines()) != 1:
|
|
|
|
|
raise ValueError
|
|
|
|
|
if len(key.split()) < 2:
|
|
|
|
|
raise ValueError
|
|
|
|
|
except (OSError, ValueError):
|
|
|
|
|
raise InstallError(
|
|
|
|
|
f"Read the pinned {name} SSH public key."
|
|
|
|
|
) from None
|
2026-11-22 17:51:05 +01:00
|
|
|
options = {}
|
|
|
|
|
if mode == "remote-install":
|
|
|
|
|
user = table.get("ssh_user")
|
|
|
|
|
if not isinstance(user, str) or not SSH_USERNAME.fullmatch(user):
|
|
|
|
|
raise InstallError(
|
|
|
|
|
f"Enter a valid SSH administrator for {name}."
|
|
|
|
|
)
|
|
|
|
|
options = {
|
|
|
|
|
"ssh_user": user,
|
|
|
|
|
"ssh_identity": file_path(
|
|
|
|
|
table.get("ssh_identity"), f"the {name} SSH private key"
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
if name == "mail":
|
|
|
|
|
mail_host = hostname(table.get("mail_hostname"), "mail")
|
|
|
|
|
chain, private_key = tls_files(
|
|
|
|
|
table, mail_host, "mail", remote=True
|
|
|
|
|
)
|
|
|
|
|
options.update(
|
|
|
|
|
mail_hostname=mail_host,
|
|
|
|
|
tls=table.get("tls", "provided"),
|
|
|
|
|
certificate=chain,
|
|
|
|
|
key=private_key,
|
|
|
|
|
)
|
2026-11-22 16:39:27 +01:00
|
|
|
return Role(
|
|
|
|
|
mode,
|
|
|
|
|
host=normalized,
|
|
|
|
|
port=port(table.get("port"), f"the {name} node", 22),
|
|
|
|
|
host_key=host_key,
|
2026-11-22 17:51:05 +01:00
|
|
|
**options,
|
2026-11-22 16:39:27 +01:00
|
|
|
)
|
2026-11-22 17:51:05 +01:00
|
|
|
raise InstallError(
|
|
|
|
|
f"Choose off, local, remote, or remote-install for {name}."
|
|
|
|
|
)
|
2026-11-22 16:39:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class Plan:
|
|
|
|
|
hostname: str
|
|
|
|
|
access: str
|
|
|
|
|
certificate: str
|
|
|
|
|
key: str
|
|
|
|
|
origin_ip: str
|
|
|
|
|
proxy_ip: str
|
|
|
|
|
origin_port: int
|
|
|
|
|
admin_username: str
|
|
|
|
|
admin_email: str
|
|
|
|
|
dns: Role
|
|
|
|
|
mail: Role
|
|
|
|
|
|
|
|
|
|
def description(self):
|
2026-11-22 17:51:05 +01:00
|
|
|
def target(role):
|
|
|
|
|
if role.mode in REMOTE_MODES:
|
|
|
|
|
return f"{role.mode} ({role.host}, SSH port {role.port})"
|
|
|
|
|
return role.mode
|
|
|
|
|
|
2026-11-22 16:39:27 +01:00
|
|
|
return (
|
|
|
|
|
f"https://{self.hostname}/ · {self.access} · "
|
2026-11-22 17:51:05 +01:00
|
|
|
f"DNS {target(self.dns)} · mail {target(self.mail)} · "
|
2026-11-22 16:39:27 +01:00
|
|
|
f"Administrator {self.admin_username}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def record(self):
|
|
|
|
|
return asdict(self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_plan(data):
|
|
|
|
|
keys(data, {"panel", "admin", "dns", "mail"}, "installation")
|
|
|
|
|
panel = data.get("panel")
|
|
|
|
|
admin = data.get("admin")
|
|
|
|
|
keys(admin, {"username", "email", "password_file"}, "admin")
|
|
|
|
|
username = admin.get("username", "operator")
|
|
|
|
|
email = admin.get("email", "")
|
|
|
|
|
if not isinstance(username, str) or not USERNAME.fullmatch(username):
|
|
|
|
|
raise InstallError("Enter a valid administrator username.")
|
|
|
|
|
if (
|
|
|
|
|
not isinstance(email, str)
|
|
|
|
|
or len(email) > 254
|
|
|
|
|
or not EMAIL.fullmatch(email)
|
|
|
|
|
):
|
|
|
|
|
raise InstallError("Enter a valid administrator email address.")
|
|
|
|
|
if not isinstance(panel, dict):
|
|
|
|
|
raise InstallError("Provide a panel section.")
|
|
|
|
|
host = hostname(panel.get("hostname"), "panel")
|
|
|
|
|
access = panel.get("access", "tls")
|
|
|
|
|
chain = key = origin = proxy = ""
|
|
|
|
|
origin_port = 8080
|
|
|
|
|
if access == "tls":
|
|
|
|
|
keys(panel, {"hostname", "access", "certificate", "key"}, "panel")
|
|
|
|
|
chain, key = certificate(panel, "the panel")
|
2026-11-22 17:51:05 +01:00
|
|
|
elif access == "acme":
|
|
|
|
|
keys(panel, {"hostname", "access"}, "panel")
|
|
|
|
|
chain, key = tls_files({"tls": "acme"}, host, "the panel")
|
2026-11-22 16:39:27 +01:00
|
|
|
elif access == "proxy":
|
|
|
|
|
keys(
|
|
|
|
|
panel,
|
|
|
|
|
{"hostname", "access", "origin_ip", "origin_port", "proxy_ip"},
|
|
|
|
|
"panel",
|
|
|
|
|
)
|
|
|
|
|
origin = address(panel.get("origin_ip"), "the origin", private=True)
|
|
|
|
|
proxy = address(
|
|
|
|
|
panel.get("proxy_ip"), "the reverse proxy", private=True
|
|
|
|
|
)
|
|
|
|
|
origin_port = port(panel.get("origin_port"), "the origin", 8080)
|
|
|
|
|
if origin_port in {80, 443}:
|
|
|
|
|
raise InstallError(
|
|
|
|
|
"Use a separate origin port for the reverse proxy."
|
|
|
|
|
)
|
|
|
|
|
else:
|
2026-11-22 17:51:05 +01:00
|
|
|
raise InstallError("Choose tls, acme, or proxy for panel access.")
|
2026-11-22 16:39:27 +01:00
|
|
|
return Plan(
|
|
|
|
|
host,
|
|
|
|
|
access,
|
|
|
|
|
chain,
|
|
|
|
|
key,
|
|
|
|
|
origin,
|
|
|
|
|
proxy,
|
|
|
|
|
origin_port,
|
|
|
|
|
username,
|
|
|
|
|
email,
|
|
|
|
|
role(data.get("dns", {}), "dns"),
|
|
|
|
|
role(data.get("mail", {}), "mail"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_plan(path):
|
|
|
|
|
try:
|
|
|
|
|
with Path(path).open("rb") as source:
|
|
|
|
|
raw = source.read(65537)
|
|
|
|
|
if len(raw) > 65536:
|
|
|
|
|
raise ValueError
|
|
|
|
|
return tomllib.loads(raw.decode("utf-8"))
|
|
|
|
|
except (OSError, ValueError) as error:
|
|
|
|
|
raise InstallError("Read a valid TOML installation file.") from error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_password(password):
|
|
|
|
|
if not isinstance(password, str) or not 12 <= len(password) <= 1024:
|
|
|
|
|
raise InstallError(
|
|
|
|
|
"Use an administrator password of 12 to 1024 characters."
|
|
|
|
|
)
|
|
|
|
|
if not password.isprintable():
|
|
|
|
|
raise InstallError("Use a single-line administrator password.")
|
|
|
|
|
return password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_password(path):
|
|
|
|
|
try:
|
|
|
|
|
fd = os.open(
|
|
|
|
|
file_path(path, "the administrator password file"),
|
|
|
|
|
os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK,
|
|
|
|
|
)
|
|
|
|
|
with os.fdopen(fd, "rb") as source:
|
|
|
|
|
info = os.fstat(source.fileno())
|
|
|
|
|
if (
|
|
|
|
|
not stat.S_ISREG(info.st_mode)
|
|
|
|
|
or info.st_uid != 0
|
|
|
|
|
or stat.S_IMODE(info.st_mode) != 0o600
|
|
|
|
|
or info.st_size > 4097
|
|
|
|
|
):
|
|
|
|
|
raise OSError
|
|
|
|
|
password = source.read(4098).decode("utf-8").removesuffix("\n")
|
|
|
|
|
except (OSError, ValueError):
|
|
|
|
|
raise InstallError(
|
|
|
|
|
"Use a root-owned administrator password file with mode 0600."
|
|
|
|
|
) from None
|
|
|
|
|
return check_password(password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def platform_settings():
|
|
|
|
|
try:
|
|
|
|
|
system = freedesktop_os_release()
|
|
|
|
|
return PLATFORMS[system["ID"], system["VERSION_ID"]]
|
|
|
|
|
except (KeyError, OSError):
|
|
|
|
|
raise InstallError(
|
|
|
|
|
"Use Ubuntu 26.04, Ubuntu 24.04, or Debian 13."
|
|
|
|
|
) from None
|