97 lines
4.2 KiB
Python
97 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline, create-only package installation. Never starts/enables services.
|
|
|
|
Default staging is testable without root. A live filesystem install requires
|
|
both --root / and --install-live plus a preexisting dedicated system account.
|
|
No device/private signing keys or bearer credentials are accepted as inputs.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import pwd
|
|
import stat
|
|
|
|
|
|
def directory(path: Path, mode: int) -> None:
|
|
if not path.exists():
|
|
path.mkdir(mode=mode)
|
|
metadata = path.lstat()
|
|
if not stat.S_ISDIR(metadata.st_mode) or metadata.st_mode & 0o022:
|
|
raise ValueError("unsafe installation directory")
|
|
if metadata.st_uid != os.geteuid():
|
|
raise ValueError("installation directory has unexpected owner")
|
|
|
|
|
|
def create(path: Path, data: bytes, mode: int) -> None:
|
|
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
|
|
with os.fdopen(fd, "wb") as stream:
|
|
stream.write(data)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
fd = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
|
try:
|
|
os.fsync(fd)
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
def elf(path: Path) -> bytes:
|
|
if path.stat().st_size > 100 * 1024 * 1024:
|
|
raise ValueError("binary size limit")
|
|
data = path.read_bytes()
|
|
if len(data) < 64 or data[:6] != b"\x7fELF\x02\x01" or data[18:20] != b"\x3e\x00":
|
|
raise ValueError("expected Linux x86-64 ELF artifact")
|
|
return data
|
|
|
|
|
|
def install(root: Path, agent: Path, ca: Path, helper: Path | None, live: bool) -> None:
|
|
if not root.is_absolute() or ".." in root.parts:
|
|
raise ValueError("absolute installation root required")
|
|
if root == Path("/") and not live:
|
|
raise ValueError("live installation requires --install-live")
|
|
if live and (root != Path("/") or os.geteuid() != 0):
|
|
raise ValueError("live installation requires root and --root /")
|
|
account = pwd.getpwnam("rmm-agent") if live else None
|
|
if account is not None and (account.pw_uid == 0 or account.pw_gid == 0):
|
|
raise ValueError("rmm-agent must be a dedicated unprivileged account")
|
|
# Validate all inputs before creating anything. CA is public, never private.
|
|
agent_data = elf(agent)
|
|
helper_data = elf(helper) if helper else None
|
|
ca_data = ca.read_bytes()
|
|
if len(ca_data) > 65536 or b"-----BEGIN CERTIFICATE-----" not in ca_data or b"PRIVATE KEY" in ca_data:
|
|
raise ValueError("expected public CA certificates only")
|
|
directory(root, 0o755)
|
|
for relative in ["usr", "usr/local", "usr/local/libexec", "etc", "etc/rmm",
|
|
"etc/systemd", "etc/systemd/system", "var", "var/lib"]:
|
|
directory(root / relative, 0o755)
|
|
units = Path(__file__).parent
|
|
files = [("usr/local/libexec/rmm-agent", agent_data, 0o755),
|
|
("etc/rmm/ca.pem", ca_data, 0o644),
|
|
("etc/systemd/system/rmm-agent.service", (units / "rmm-agent.service").read_bytes(), 0o644)]
|
|
if helper_data is not None:
|
|
files += [("usr/local/libexec/rmm-privileged-helper", helper_data, 0o755),
|
|
("etc/systemd/system/rmm-privileged-helper.service", (units / "rmm-privileged-helper.service").read_bytes(), 0o644)]
|
|
# Never overwrite binaries, CA, service units, config or enrollment state.
|
|
if any((root / relative).exists() or (root / relative).is_symlink() for relative, _, _ in files):
|
|
raise ValueError("existing installation requires a separate controlled upgrade")
|
|
for relative, data, mode in files:
|
|
create(root / relative, data, mode)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--root", type=Path, required=True)
|
|
parser.add_argument("--agent-binary", type=Path, required=True)
|
|
parser.add_argument("--helper-binary", type=Path)
|
|
parser.add_argument("--ca-file", type=Path, required=True)
|
|
parser.add_argument("--install-live", action="store_true")
|
|
args = parser.parse_args()
|
|
install(args.root, args.agent_binary, args.ca_file, args.helper_binary, args.install_live)
|
|
print("Package files installed. No account, device identity or authorization config created; services not started/enabled.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|