"""Verify Vinix filesystem policy and the network registries in a new vpac-installed root.""" import argparse import os import stat import subprocess from pathlib import Path def require(condition, message): if not condition: raise ValueError(message) def check_periodic(root): crontabs = root / "var/spool/cron/crontabs" require(os.readlink(crontabs) == "../../../etc/crontabs", "wrong cron spool") require(os.readlink(root / "var/spool/mail") == "../mail" and (root / "var/mail").is_dir(), "wrong mail spool") require(stat.S_IMODE((crontabs / "root").stat().st_mode) == 0o600, "root crontab is not private") jobs = [line.split() for line in (crontabs / "root").read_text().splitlines() if line and not line.startswith("#")] schedules = {"15min": "*/15 * * * *", "hourly": "0 * * * *", "daily": "0 2 * * *", "weekly": "0 3 * * 6", "monthly": "0 5 1 * *"} require(len(jobs) == len(schedules), "unexpected number of cron schedules") for job in jobs: require(len(job) == 7 and job[5] == "run-parts" and (root / job[6][1:]).is_dir(), "invalid scheduled task") period = job[6].removeprefix("/etc/periodic/") require(schedules.pop(period, None) == " ".join(job[:5]), f"incorrect interval for {period}") mounts = set() for entry in (root / "etc/fstab").read_text().splitlines(): if entry and not entry.startswith("#"): device, mountpoint, _, flags, _, _ = entry.split() require(device.startswith("/dev/") and (root / mountpoint[1:]).is_dir() and "noauto" in flags.split(","), "invalid removable-media mount") mounts.add(mountpoint) require(mounts == {"/media/usb", "/media/cdrom"}, "missing removable-media mount") script = root / "etc/profile.d/20locale.sh" for env, expected in [({}, "C.UTF-8 UTF-8 C"), ({"LANG": "custom", "LC_COLLATE": "other"}, "custom UTF-8 other")]: result = subprocess.check_output(["/bin/sh", "-c", '. "$1"; echo "$LANG $CHARSET $LC_COLLATE"', "sh", script], env=env, text=True) require(result.strip() == expected, "locale profile overwrote the caller's environment") def check(root): for path, mode in (("tmp", 0o1777), ("var/tmp", 0o1777), ("root", 0o700), ("etc/shadow", 0o600), ("etc/passwd", 0o644), ("etc/group", 0o644)): require(stat.S_IMODE((root / path).lstat().st_mode) == mode, f"incorrect mode on {path}") for path in ("dev/pts", "dev/shm", "proc", "sys", "home", "run/lock", "var/lib", "etc/profile.d"): require(stat.S_ISDIR((root / path).lstat().st_mode), f"directory missing at {path}") for path, target in (("var/run", "../run"), ("var/lock", "../run/lock"), ("etc/mtab", "/proc/mounts")): require(os.readlink(root / path) == target, f"unexpected link at {path}") for path, field in (("etc/passwd", "root:x:0:0:root:/root:/bin/sh"), ("etc/group", "root:x:0:"), ("etc/hostname", "vinix"), ("etc/shells", "/bin/sh")): require(field in (root / path).read_text().splitlines(), f"missing {field} in {path}") for path, expected in (("etc/services", {"domain": "53/udp", "ssh": "22/tcp"}), ("etc/protocols", {"tcp": "6", "udp": "17"})): entries = {tuple(row[:2]) for line in (root / path).read_text().splitlines() if len(row := line.split("#")[0].split()) >= 2} for entry in expected.items(): require(entry in entries, f"missing {entry} in {path}") print("Vinix directories, modes, links, accounts and service/protocol registries passed") if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("root", type=Path) parser.add_argument("--periodic", action="store_true", help="check the filesystem release 2 cron, locale and mount layout") args = parser.parse_args() check(args.root) if args.periodic: check_periodic(args.root)