| 1 | """Verify Vinix filesystem policy and the network registries in a new vpac-installed root.""" |
| 2 | import argparse |
| 3 | import os |
| 4 | import stat |
| 5 | import subprocess |
| 6 | from pathlib import Path |
| 7 | |
| 8 | |
| 9 | def require(condition, message): |
| 10 | if not condition: |
| 11 | raise ValueError(message) |
| 12 | |
| 13 | |
| 14 | def check_periodic(root): |
| 15 | crontabs = root / "var/spool/cron/crontabs" |
| 16 | require(os.readlink(crontabs) == "../../../etc/crontabs", "wrong cron spool") |
| 17 | require(os.readlink(root / "var/spool/mail") == "../mail" and (root / "var/mail").is_dir(), "wrong mail spool") |
| 18 | require(stat.S_IMODE((crontabs / "root").stat().st_mode) == 0o600, "root crontab is not private") |
| 19 | jobs = [line.split() for line in (crontabs / "root").read_text().splitlines() if line and not line.startswith("#")] |
| 20 | schedules = {"15min": "*/15 * * * *", "hourly": "0 * * * *", "daily": "0 2 * * *", |
| 21 | "weekly": "0 3 * * 6", "monthly": "0 5 1 * *"} |
| 22 | require(len(jobs) == len(schedules), "unexpected number of cron schedules") |
| 23 | for job in jobs: |
| 24 | require(len(job) == 7 and job[5] == "run-parts" and (root / job[6][1:]).is_dir(), "invalid scheduled task") |
| 25 | period = job[6].removeprefix("/etc/periodic/") |
| 26 | require(schedules.pop(period, None) == " ".join(job[:5]), f"incorrect interval for {period}") |
| 27 | mounts = set() |
| 28 | for entry in (root / "etc/fstab").read_text().splitlines(): |
| 29 | if entry and not entry.startswith("#"): |
| 30 | device, mountpoint, _, flags, _, _ = entry.split() |
| 31 | require(device.startswith("/dev/") and (root / mountpoint[1:]).is_dir() and "noauto" in flags.split(","), |
| 32 | "invalid removable-media mount") |
| 33 | mounts.add(mountpoint) |
| 34 | require(mounts == {"/media/usb", "/media/cdrom"}, "missing removable-media mount") |
| 35 | script = root / "etc/profile.d/20locale.sh" |
| 36 | for env, expected in [({}, "C.UTF-8 UTF-8 C"), ({"LANG": "custom", "LC_COLLATE": "other"}, "custom UTF-8 other")]: |
| 37 | result = subprocess.check_output(["/bin/sh", "-c", '. "$1"; echo "$LANG $CHARSET $LC_COLLATE"', "sh", script], |
| 38 | env=env, text=True) |
| 39 | require(result.strip() == expected, "locale profile overwrote the caller's environment") |
| 40 | |
| 41 | |
| 42 | def check(root): |
| 43 | for path, mode in (("tmp", 0o1777), ("var/tmp", 0o1777), ("root", 0o700), |
| 44 | ("etc/shadow", 0o600), ("etc/passwd", 0o644), ("etc/group", 0o644)): |
| 45 | require(stat.S_IMODE((root / path).lstat().st_mode) == mode, f"incorrect mode on {path}") |
| 46 | for path in ("dev/pts", "dev/shm", "proc", "sys", "home", "run/lock", "var/lib", "etc/profile.d"): |
| 47 | require(stat.S_ISDIR((root / path).lstat().st_mode), f"directory missing at {path}") |
| 48 | for path, target in (("var/run", "../run"), ("var/lock", "../run/lock"), ("etc/mtab", "/proc/mounts")): |
| 49 | require(os.readlink(root / path) == target, f"unexpected link at {path}") |
| 50 | for path, field in (("etc/passwd", "root:x:0:0:root:/root:/bin/sh"), |
| 51 | ("etc/group", "root:x:0:"), ("etc/hostname", "vinix"), ("etc/shells", "/bin/sh")): |
| 52 | require(field in (root / path).read_text().splitlines(), f"missing {field} in {path}") |
| 53 | for path, expected in (("etc/services", {"domain": "53/udp", "ssh": "22/tcp"}), |
| 54 | ("etc/protocols", {"tcp": "6", "udp": "17"})): |
| 55 | entries = {tuple(row[:2]) for line in (root / path).read_text().splitlines() |
| 56 | if len(row := line.split("#")[0].split()) >= 2} |
| 57 | for entry in expected.items(): |
| 58 | require(entry in entries, f"missing {entry} in {path}") |
| 59 | print("Vinix directories, modes, links, accounts and service/protocol registries passed") |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | parser = argparse.ArgumentParser(description=__doc__) |
| 64 | parser.add_argument("root", type=Path) |
| 65 | parser.add_argument("--periodic", action="store_true", help="check the filesystem release 2 cron, locale and mount layout") |
| 66 | args = parser.parse_args() |
| 67 | check(args.root) |
| 68 | if args.periodic: |
| 69 | check_periodic(args.root) |
| 70 | |