| 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 | from pathlib import Path |
| 6 | |
| 7 | |
| 8 | def require(condition, message): |
| 9 | if not condition: |
| 10 | raise ValueError(message) |
| 11 | |
| 12 | |
| 13 | def check(root): |
| 14 | for path, mode in (("tmp", 0o1777), ("var/tmp", 0o1777), ("root", 0o700), |
| 15 | ("etc/shadow", 0o600), ("etc/passwd", 0o644), ("etc/group", 0o644)): |
| 16 | require(stat.S_IMODE((root / path).lstat().st_mode) == mode, f"incorrect mode on {path}") |
| 17 | for path in ("dev/pts", "dev/shm", "proc", "sys", "home", "run/lock", "var/lib", "etc/profile.d"): |
| 18 | require(stat.S_ISDIR((root / path).lstat().st_mode), f"directory missing at {path}") |
| 19 | for path, target in (("var/run", "../run"), ("var/lock", "../run/lock"), ("etc/mtab", "/proc/mounts")): |
| 20 | require(os.readlink(root / path) == target, f"unexpected link at {path}") |
| 21 | for path, field in (("etc/passwd", "root:x:0:0:root:/root:/bin/sh"), |
| 22 | ("etc/group", "root:x:0:"), ("etc/hostname", "vinix"), ("etc/shells", "/bin/sh")): |
| 23 | require(field in (root / path).read_text().splitlines(), f"missing {field} in {path}") |
| 24 | for path, expected in (("etc/services", {"domain": "53/udp", "ssh": "22/tcp"}), |
| 25 | ("etc/protocols", {"tcp": "6", "udp": "17"})): |
| 26 | entries = {tuple(row[:2]) for line in (root / path).read_text().splitlines() |
| 27 | if len(row := line.split("#")[0].split()) >= 2} |
| 28 | for entry in expected.items(): |
| 29 | require(entry in entries, f"missing {entry} in {path}") |
| 30 | print("Vinix directories, modes, links, accounts and service/protocol registries passed") |
| 31 | |
| 32 | |
| 33 | if __name__ == "__main__": |
| 34 | parser = argparse.ArgumentParser(description=__doc__) |
| 35 | parser.add_argument("root", type=Path) |
| 36 | check(parser.parse_args().root) |
| 37 | |