#!/usr/bin/python3
"""Maintain deployment-specific /etc trees from /usr/share/factory/etc."""

import argparse
import fcntl
import hashlib
import json
import os
import re
from pathlib import Path
import shutil
import stat
import subprocess
import sys
import tempfile


METADATA = os.environ.get("IMAGE_ETC_METADATA", ".image-etc-factory.json")
if not re.fullmatch(r"\.[A-Za-z0-9._-]+", METADATA):
    raise ValueError("Metadata name must be a hidden basename")
VERSION = 1
NEVER = {
    ".pwd.lock", "crypttab", "fstab", "group", "group-", "gshadow",
    "gshadow-", "hostname", "hosts", "locale.conf", "localtime",
    "machine-id", "passwd", "passwd-", "shadow", "shadow-", "subgid",
    "subgid-", "subuid", "subuid-", "vconsole.conf",
}


def excluded(relative: str) -> bool:
    return relative == METADATA or Path(relative).name in NEVER


def fingerprint(path: Path):
    try:
        info = path.lstat()
    except FileNotFoundError:
        return None
    if stat.S_ISLNK(info.st_mode):
        return {"kind": "link", "target": os.readlink(path)}
    if stat.S_ISREG(info.st_mode):
        digest = hashlib.sha256()
        with path.open("rb") as stream:
            for block in iter(lambda: stream.read(1024 * 1024), b""):
                digest.update(block)
        return {"kind": "file", "sha256": digest.hexdigest()}
    return None


def entries(root: Path, include_never: bool = False):
    result = {}
    if not root.is_dir():
        return result
    for directory, names, files in os.walk(root, followlinks=False):
        base = Path(directory)
        # os.walk puts symlinked directories in names. Treat them as links and
        # never traverse them.
        for name in list(names):
            path = base / name
            if path.is_symlink():
                files.append(name)
                names.remove(name)
        for name in files:
            path = base / name
            relative = path.relative_to(root).as_posix()
            if not include_never and excluded(relative):
                continue
            value = fingerprint(path)
            if value is not None:
                result[relative] = value
    return result


def load_metadata(etc: Path):
    path = etc / METADATA
    data = json.loads(path.read_text(encoding="utf-8"))
    if data.get("version") != VERSION or not isinstance(data.get("entries"), dict):
        raise ValueError("unsupported metadata format")
    return data["entries"]


def write_metadata(root: Path, managed):
    temporary = root / f"{METADATA}.new"
    temporary.write_text(
        json.dumps({"version": VERSION, "entries": managed}, sort_keys=True) + "\n",
        encoding="utf-8",
    )
    os.chmod(temporary, 0o600)
    os.replace(temporary, root / METADATA)


def remove(path: Path):
    try:
        if path.is_dir() and not path.is_symlink():
            shutil.rmtree(path)
        else:
            path.unlink()
    except FileNotFoundError:
        pass


def copy(source: Path, target: Path):
    target.parent.mkdir(parents=True, exist_ok=True)
    remove(target)
    # cp -a retains ownership, modes, ACLs and xattrs. SELinux is relabelled
    # for the target root by the deployment helper afterwards.
    subprocess.run(["cp", "-a", "--", str(source), str(target)], check=True)


def merge(etc: Path, factory: Path):
    old = load_metadata(etc)
    new = entries(factory)

    for relative, new_value in new.items():
        target = etc / relative
        old_value = old.get(relative)
        current = fingerprint(target)
        if old_value is None:
            if not os.path.lexists(target):
                copy(factory / relative, target)
        elif current == old_value and current != new_value:
            copy(factory / relative, target)

    for relative, old_value in old.items():
        if relative in new:
            continue
        target = etc / relative
        if fingerprint(target) == old_value:
            remove(target)

    write_metadata(etc, new)


def seed(factory: Path):
    write_metadata(factory, entries(factory))


def is_subvolume(path: Path) -> bool:
    if path.is_symlink() or not path.is_dir():
        return False
    result = subprocess.run(
        ["btrfs", "subvolume", "show", str(path)],
        stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
    )
    return result.returncode == 0


def relabel_etc(etc: Path):
    # Stage the snapshot as /etc under an alternate root so setfiles matches
    # /etc policy paths, rather than treating its files as /passwd, /shadow, …
    config = (etc / "selinux/config").read_text(encoding="utf-8")
    match = re.search(r"^SELINUXTYPE=([a-zA-Z0-9_-]+)$", config, re.MULTILINE)
    policy_type = match.group(1) if match else "targeted"
    contexts = etc / f"selinux/{policy_type}/contexts/files/file_contexts"
    if not contexts.is_file():
        raise RuntimeError(f"SELinux contexts missing: {contexts}")
    policies = [p for p in (etc / f"selinux/{policy_type}/policy").glob("policy.*")
                if p.is_file() and p.name.removeprefix("policy.").isdigit()]
    if not policies:
        raise RuntimeError("Deployed SELinux binary policy is missing")
    policy = max(policies, key=lambda p: int(p.name.removeprefix("policy.")))
    with tempfile.TemporaryDirectory(prefix="image-etc-label-", dir="/run") as root:
        mounted = Path(root) / "etc"
        mounted.mkdir()
        subprocess.run(["mount", "--bind", str(etc), str(mounted)], check=True)
        try:
            subprocess.run(
                # Validate against the deployment, not the smaller initrd
                # policy.
                ["setfiles", "-c", str(policy), "-F", "-r", root,
                 str(contexts), str(mounted)], check=True,
            )
        finally:
            subprocess.run(["umount", str(mounted)], check=True)


def select_etc(state: Path, version: str, factory: Path, relabel: bool = False):
    if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._+-]*", version):
        raise ValueError(f"Unsafe image version: {version!r}")
    if not factory.is_dir():
        raise RuntimeError(f"Factory /etc missing: {factory}")
    destination = state / f"@etc-{version}"
    # Serialize selection with any future installer/provisioning callers.
    with (state / ".image-etc.lock").open("a") as lock:
        fcntl.flock(lock, fcntl.LOCK_EX)
        if os.path.lexists(destination):
            if not is_subvolume(destination):
                raise RuntimeError(f"Existing deployment /etc is not a subvolume: {destination}")
            # Existing versions are deliberately not merged on every boot:
            # rollback must reuse that version's own configuration unchanged.
        else:
            candidates = [
                path.name for path in state.glob("@etc-*")
                if re.fullmatch(r"@etc-[A-Za-z0-9][A-Za-z0-9._+-]*", path.name)
                and is_subvolume(path)
            ]
            if candidates:
                ordered = subprocess.run(
                    ["sort", "-V"], input="\n".join(candidates) + "\n",
                    text=True, capture_output=True, check=True,
                ).stdout.splitlines()
                previous = state / ordered[-1]
            else:
                previous = state / "@etc"
            if not is_subvolume(previous):
                raise RuntimeError(f"No /etc snapshot or bootstrap subvolume found in {state}")
            # Publish only after merge/relabel succeed. A failed or interrupted
            # creation cannot leave a partially migrated @etc-VERSION in use.
            with tempfile.TemporaryDirectory(prefix=".image-etc-new-", dir=state) as work:
                snapshot = Path(work) / "etc"
                try:
                    subprocess.run(
                        ["btrfs", "subvolume", "snapshot", str(previous), str(snapshot)],
                        check=True,
                    )
                    merge(snapshot, factory)
                    if relabel:
                        relabel_etc(snapshot)
                    os.rename(snapshot, destination)
                finally:
                    if snapshot.exists():
                        subprocess.run(
                            ["btrfs", "subvolume", "delete", str(snapshot)], check=True,
                        )
        # A relative symlink survives switch-root and is also useful to TIK
        # when mounting an existing installation for account migration.
        link = state / ".image-etc-current.new"
        link.unlink(missing_ok=True)
        link.symlink_to(destination.name)
        os.replace(link, state / "@etc-current")
    return destination


def sync_late(baseline: Path, source: Path, target: Path):
    before = entries(baseline, include_never=True)
    now = entries(source, include_never=True)
    for relative in sorted(set(before) | set(now)):
        if relative == METADATA or before.get(relative) == now.get(relative):
            continue
        destination = target / relative
        if relative not in now:
            remove(destination)
        else:
            copy(source / relative, destination)


def uki_versions(boot: Path, image_id: str):
    if not re.fullmatch(r"[A-Za-z0-9._-]+", image_id):
        raise ValueError("Unsafe image identity")
    directory = boot / "EFI/Linux"
    # Missing/unmounted $BOOT must never mean 'delete every snapshot'.
    if not directory.is_dir():
        raise RuntimeError("Installed UKI directory is unavailable; refusing cleanup")
    pattern = re.compile(re.escape(image_id) +
                         r"_(.+)_[A-Za-z0-9-]+(?:\+\d+(?:-\d+)?)?\.efi")
    versions = set()
    for path in directory.iterdir():
        match = pattern.fullmatch(path.name)
        if path.is_file() and match:
            versions.add(match.group(1))
    if not versions:
        raise RuntimeError("No installed UKIs for this OS; refusing cleanup")
    return versions


def cleanup_etc(state: Path, image_id: str, boot: Path):
    """Retain exactly versions whose UKIs remain installed"""
    with (state / ".image-etc.lock").open("a") as lock:
        fcntl.flock(lock, fcntl.LOCK_EX)
        keep = uki_versions(boot, image_id)
        current = state / "@etc-current"
        if not current.is_symlink() or current.readlink().name.removeprefix("@etc-") not in keep:
            raise RuntimeError("Active /etc has no installed UKI; refusing cleanup")
        mounted = subprocess.run(
            ["findmnt", "-rn", "-t", "btrfs", "-o", "FSROOT"],
            text=True, capture_output=True, check=True,
        ).stdout.splitlines()
        mounted_names = {Path(path).name for path in mounted}
        for candidate in sorted(state.glob("@etc-*")):
            if not re.fullmatch(r"@etc-[A-Za-z0-9][A-Za-z0-9._+-]*", candidate.name):
                continue
            if not is_subvolume(candidate):
                continue
            # Re-read UKIs before each deletion; sysupdate may have added one
            # since initial discovery.
            if candidate.name.removeprefix("@etc-") in uki_versions(boot, image_id):
                continue
            if candidate.name in mounted_names:
                print(f"image etc: still mounted, deferring cleanup: {candidate}", file=sys.stderr)
                continue
            subprocess.run(["btrfs", "subvolume", "delete", str(candidate)], check=True)


def main():
    parser = argparse.ArgumentParser()
    commands = parser.add_subparsers(dest="command", required=True)
    seed_parser = commands.add_parser("seed")
    seed_parser.add_argument("factory", type=Path)
    merge_parser = commands.add_parser("merge")
    merge_parser.add_argument("--etc", required=True, type=Path)
    merge_parser.add_argument("--factory", required=True, type=Path)
    sync_parser = commands.add_parser("sync-late")
    sync_parser.add_argument("--baseline", required=True, type=Path)
    sync_parser.add_argument("--source", required=True, type=Path)
    sync_parser.add_argument("--target", required=True, type=Path)
    select_parser = commands.add_parser("select")
    select_parser.add_argument("--state", required=True, type=Path)
    select_parser.add_argument("--version", required=True)
    select_parser.add_argument("--factory", required=True, type=Path)
    select_parser.add_argument("--relabel", action="store_true")
    cleanup_parser = commands.add_parser("cleanup")
    cleanup_parser.add_argument("--state", required=True, type=Path)
    cleanup_parser.add_argument("--image-id", required=True)
    cleanup_parser.add_argument("--boot", required=True, type=Path)
    args = parser.parse_args()

    if args.command == "seed":
        seed(args.factory)
    elif args.command == "merge":
        merge(args.etc, args.factory)
    elif args.command == "select":
        select_etc(args.state, args.version, args.factory, args.relabel)
    elif args.command == "cleanup":
        cleanup_etc(args.state, args.image_id, args.boot)
    else:
        sync_late(args.baseline, args.source, args.target)


if __name__ == "__main__":
    try:
        main()
    except Exception as error:
        print(f"image etc factory: {error}", file=sys.stderr)
        raise
