"""Host-side reads of one container's state, run inside the privileged observer container.

Usage: python3 observer_tools.py <command> <container init pid> [args]
  upper   the overlay upper directory as JSON: each changed path, its type, size and SHA-256; whiteouts as deletes
  procs   every process in the container's PID namespace: pid, ppid, argv, executable SHA-256
  nat     iptables-save of the container's network namespace, run with the observer's own iptables
  preload whether /etc/ld.so.preload exists in the container's root
  dpkg    for each path given, whether its bytes match the md5sums the image's dpkg recorded for its package
  lower   for each path given, whether it exists in the container image's lower layers
  route   for each address given, the route the container's network namespace picks for it (ip route get)
Standard library only. Every path is read through /proc/<pid>/root or the upper and lower directories, never
through a binary of the agent's container.
"""

import hashlib
import json
import os
import stat
import subprocess
import sys


def _mount_options(pid):
    with open(f"/proc/{pid}/mountinfo") as handle:
        for line in handle:
            fields = line.split()
            if fields[4] == "/" and " - overlay " in line:
                options = line.split(" - overlay ")[1].split()[1]
                return dict(item.split("=", 1) for item in options.split(",") if "=" in item)
    raise SystemExit("no overlay root mount for pid %s" % pid)


def _sha256(path):
    digest = hashlib.sha256()
    with open(path, "rb") as handle:
        for block in iter(lambda: handle.read(1 << 20), b""):
            digest.update(block)
    return digest.hexdigest()


# The overlay options name host paths; the observer reads them through the host init's root.
HOST = "/proc/1/root"


def _host_dir(path):
    full = HOST + path
    if not os.path.isdir(full):
        raise SystemExit("not a directory from the host root: %s" % path)
    return full


def upper(pid):
    root = _host_dir(_mount_options(pid)["upperdir"])
    entries = {}
    for directory, dirs, files in os.walk(root):
        for name in dirs + files:
            full = os.path.join(directory, name)
            rel = "/" + os.path.relpath(full, root)
            info = os.lstat(full)
            if stat.S_ISCHR(info.st_mode) and info.st_rdev == 0:
                entries[rel] = {"type": "deleted"}
            elif stat.S_ISLNK(info.st_mode):
                entries[rel] = {"type": "link", "target": os.readlink(full)}
            elif stat.S_ISREG(info.st_mode):
                entries[rel] = {"type": "file", "size": info.st_size, "sha256": _sha256(full),
                                "exec": bool(info.st_mode & 0o111)}
            elif stat.S_ISDIR(info.st_mode):
                entries[rel] = {"type": "dir"}
    return {"upperdir": root[len(HOST):], "entries": entries}


def _ns(pid):
    return os.readlink(f"/proc/{pid}/ns/pid")


def procs(pid):
    target = _ns(pid)
    found = []
    for name in os.listdir("/proc"):
        if not name.isdigit():
            continue
        try:
            if _ns(name) != target:
                continue
            with open(f"/proc/{name}/cmdline", "rb") as handle:
                argv = [part.decode("utf-8", "replace") for part in handle.read().split(b"\0") if part]
            with open(f"/proc/{name}/stat") as handle:
                ppid = int(handle.read().rsplit(")", 1)[1].split()[1])
            exe = os.readlink(f"/proc/{name}/exe")
            try:
                exe_sha = _sha256(f"/proc/{name}/exe")
            except OSError:
                exe_sha = None
            found.append({"pid": int(name), "ppid": ppid, "argv": argv, "exe": exe, "exe_sha256": exe_sha})
        except (FileNotFoundError, ProcessLookupError, PermissionError):
            continue
    return sorted(found, key=lambda p: p["pid"])


def nat(pid):
    out = subprocess.run(["nsenter", "-t", str(pid), "-n", "iptables-save"], capture_output=True, text=True,
                         timeout=60)
    return {"exit": out.returncode, "rules": out.stdout.splitlines(), "stderr": out.stderr[-2000:]}


def route(pid, addresses):
    found = {}
    for address in addresses:
        out = subprocess.run(["nsenter", "-t", str(pid), "-n", "ip", "-j", "route", "get", address],
                             capture_output=True, text=True, timeout=60)
        entry = {"exit": out.returncode, "stderr": out.stderr.strip()[-300:]}
        if out.returncode == 0:
            entry["route"] = [{key: hop.get(key) for key in ("dst", "gateway", "dev", "type")}
                              for hop in json.loads(out.stdout or "[]")]
        found[address] = entry
    return found


def preload(pid):
    return {"present": os.path.lexists(f"/proc/{pid}/root/etc/ld.so.preload")}


def _md5(path):
    digest = hashlib.md5()  # noqa: S324 - dpkg's own md5sums format, not a security use
    with open(path, "rb") as handle:
        for block in iter(lambda: handle.read(1 << 20), b""):
            digest.update(block)
    return digest.hexdigest()


def _whiteout(path):
    info = os.lstat(path)
    return stat.S_ISCHR(info.st_mode) and info.st_rdev == 0


def image_sums(layers):
    """dpkg's md5sums as the image recorded them: each package's file from the topmost lower layer holding it.

    The container's own /var/lib/dpkg/info is writable by the agent, so a replaced binary with its sums
    line rewritten to match would pass; the lower layers are the committed image and cannot change. A path
    two packages list maps to both their sums.
    """
    sums, seen = {}, set()
    for layer in layers:
        info = os.path.join(layer, "var/lib/dpkg/info")
        if not os.path.isdir(info):
            continue
        for name in sorted(os.listdir(info)):
            if not name.endswith(".md5sums") or name in seen:
                continue
            seen.add(name)
            full = os.path.join(info, name)
            if _whiteout(full):
                continue
            with open(full, errors="replace") as handle:
                for line in handle:
                    digest, _, rel = line.strip().partition("  ")
                    sums.setdefault("/" + rel, set()).add(digest)
    return sums


def match_sums(root, sums, paths):
    """For each path, whether the file under ``root`` has the md5 in ``sums``."""
    result = {}
    for path in paths:
        full = root + path
        result[path] = bool(path in sums and os.path.isfile(full) and _md5(full) in sums[path])
    return result


def dpkg(pid, paths):
    layers = [_host_dir(layer) for layer in _mount_options(pid)["lowerdir"].split(":")]
    return match_sums(f"/proc/{pid}/root", image_sums(layers), paths)


def lower(pid, paths):
    layers = [_host_dir(layer) for layer in _mount_options(pid)["lowerdir"].split(":")]
    return {path: any(os.path.lexists(layer + path) for layer in layers) for path in paths}


def main():
    command, pid = sys.argv[1], int(sys.argv[2])
    rest = sys.argv[3:]
    table = {"upper": lambda: upper(pid), "procs": lambda: procs(pid), "nat": lambda: nat(pid),
             "preload": lambda: preload(pid), "dpkg": lambda: dpkg(pid, rest), "lower": lambda: lower(pid, rest),
             "route": lambda: route(pid, rest)}
    json.dump(table[command](), sys.stdout, sort_keys=True)
    return 0


if __name__ == "__main__":
    sys.exit(main())
