Files
rust-discovery/deploy/opnsense/enable-root.py
T
2026-11-20 08:27:13 +01:00

39 lines
1.6 KiB
Python

"""In the guest, bind a staged native helper to the existing signed job identity.
Print only the hashes used by the central operator. Never replace another policy.
"""
import hashlib
import json
import os
from pathlib import Path
import pwd
import stat
import sys
def main():
if os.getuid()!=0 or os.geteuid()!=0 or len(sys.argv)!=2:
raise SystemExit('Run as root: enable-root.py /absolute/staging/rmm-opnsense-jobs')
app=Path(sys.argv[1]).resolve(strict=True)
conf=Path('/usr/local/etc/discovery-agent')
binding=conf/'job-binding.json'
for path in (app,*app.parents,binding,*binding.parents):
info=path.lstat()
if info.st_uid!=0 or info.st_mode & 0o022 or stat.S_ISLNK(info.st_mode):
raise ValueError('untrusted provisioning path')
user=pwd.getpwnam('discovery')
if not user.pw_uid or not user.pw_gid: raise ValueError('invalid agent identity')
image=hashlib.file_digest(app.open('rb'),'sha256').hexdigest()
policy=dict(schema_version=1,profile='full_maintenance_v1',agent_uid=user.pw_uid,agent_gid=user.pw_gid,
binding_sha256=hashlib.sha256(binding.read_bytes()).hexdigest(),helper_binary_sha256=image)
raw=(json.dumps(policy,separators=(',',':'))+'\n').encode()
path=conf/'root-policy.json'
if path.exists():
if path.is_symlink() or path.read_bytes()!=raw: raise ValueError('existing policy differs')
else:
with path.open('xb') as f:
os.fchmod(f.fileno(),0o444); f.write(raw);f.flush();os.fsync(f.fileno())
print(json.dumps(dict(helper_binary_sha256=image,helper_policy_sha256=hashlib.sha256(raw).hexdigest())))
if __name__=='__main__':main()