#!/usr/bin/python3 -I
# Usage: mirrordash-module-network [uid=address ...]
# Lets each module instance's user (features/modules/forkserver.py) reach exactly its home-network
# addresses, besides systemd-resolved's stub: the "lan-url" settings the owner typed
# (features/modules/client.py, ARCHITECTURE #41). The module host (mirrordash-modules.service) may reach
# all of them (IPAddressAllow, the cgroup's filter), and nftables keeps each to its own user: an address
# is open to the users it is given to and refused to the host's other users. Nothing given: the home
# network is closed again. The core runs it through sudo, so only the modules' users and home-network
# addresses pass, at most 64 pairs; it lasts until the next boot, and the core sets it at every start.
import ipaddress
import subprocess
import sys

MODULE_USERS = range(70000, 71000)  # forkserver.ALL_SANDBOX_UIDS
HOST_USER = 960  # mirrordash-mod: the forkserver and the control process, which reach nothing at home
MAX_ADDRESSES = 64
RESOLVED_STUB = "127.0.0.53"  # systemd-resolved: how the modules look names up
NFTABLES_TABLE = "inet mirrordash_modules"
# nftables' set of every user in the module host, as "meta skuid" takes it
ALL_HOST_USERS = f"{{ {HOST_USER}, {MODULE_USERS.start}-{MODULE_USERS.stop - 1} }}"


def parse_allowed_addresses(arguments: list[str]) -> list[tuple[int, ipaddress.IPv4Address | ipaddress.IPv6Address]]:
    """[(module user, home-network address)] from "uid=address" arguments; exits on anything else, as the
    arguments come from the core through sudo."""
    if len(arguments) > MAX_ADDRESSES:
        sys.exit(f"at most {MAX_ADDRESSES} addresses")
    allowed = []
    for argument in arguments:
        uid_text, _, address_text = argument.partition("=")
        try:
            uid, address = int(uid_text), ipaddress.ip_address(address_text)
        except ValueError:
            sys.exit(f"not a module user and an address: {argument}")
        if uid not in MODULE_USERS:
            sys.exit(f"not a module user: {uid}")
        if not address.is_private or address.is_loopback or address.is_unspecified or address.is_multicast:
            sys.exit(f"not a home-network address: {address_text}")
        # ipaddress takes any text after % as an IPv6 scope ("fe80::1%x; flush ruleset"), and the address
        # goes into nft's rules as text: none at all (the core strips it, client.lan_addresses)
        if getattr(address, "scope_id", None):
            sys.exit(f"no scope in an address: {address_text}")
        allowed.append((uid, address))
    return allowed


def nftables_family(address) -> str:
    return "ip" if address.version == 4 else "ip6"


def nftables_rules(allowed: list) -> list[str]:
    """The output chain's rules, in the order nftables tries them (the first that matches wins)."""
    # 1. Never the mirror itself, whatever address or name was typed (its own LAN address, mirrordash.local:
    # nginx, SSH, Matter): "fib daddr type local" is any address the kernel has on any interface, a DHCP
    # change included. resolved's stub stays reachable.
    rules = [f"meta skuid {ALL_HOST_USERS} fib daddr type local ip daddr != {RESOLVED_STUB} reject with icmpx admin-prohibited",
             f"meta skuid {ALL_HOST_USERS} meta nfproto ipv6 fib daddr type local reject with icmpx admin-prohibited"]
    # 2. Each user's own addresses
    rules += [f"meta skuid {uid} {nftables_family(address)} daddr {address} accept" for uid, address in allowed]
    # 3. Those addresses for every other user of the host. IPAddressAllow opens them for the whole unit, so
    # without this rule one module could reach another's camera
    for version, family in ((4, "ip"), (6, "ip6")):
        opened = sorted({str(address) for _, address in allowed if address.version == version})
        if opened:
            # admin-prohibited, not a reset: the host's self-check counts "refused" as reached
            rules.append(f"meta skuid {ALL_HOST_USERS} {family} daddr {{ {', '.join(opened)} }} reject with icmpx admin-prohibited")
    return rules


def nftables_ruleset(rules: list[str]) -> str:
    """The table made (if missing), dropped and made again in one transaction: the rules are replaced at once,
    with no moment where none hold."""
    return "\n".join([
        f"table {NFTABLES_TABLE}", f"delete table {NFTABLES_TABLE}",
        f"table {NFTABLES_TABLE} {{", "  chain out {", "    type filter hook output priority 0; policy accept;",
        *(f"    {rule}" for rule in rules), "  }", "}", ""])


def main() -> None:
    allowed = parse_allowed_addresses(sys.argv[1:])
    if subprocess.run(["nft", "-f", "-"], input=nftables_ruleset(nftables_rules(allowed)), text=True).returncode:
        sys.exit("could not set the modules' nftables rules")
    # The unit's own IP filter (cgroup BPF) lets the addresses through; nftables above sorts out which user
    ip_address_allow = " ".join([RESOLVED_STUB, *sorted({str(address) for _, address in allowed})])
    sys.exit(subprocess.run(["systemctl", "set-property", "--runtime", "mirrordash-modules.service",
                             "IPAddressAllow=", f"IPAddressAllow={ip_address_allow}"]).returncode)


main()
