#!/usr/bin/python3 """Fresh native NTS observation with fixed-source, destination-limited egress.""" import ctypes import hashlib import json import os from pathlib import Path import pwd import socket import subprocess import sys import time assert os.geteuid()==0 and Path('/run/.containerenv').exists() assert socket.gethostname()=='c01-nts-composed' root=Path('/opt/c01-nts') sys.path.insert(0,str(root)) import c01_nts subprocess.run(['/usr/sbin/nft','-f',str(root/'egress.nft')],check=True) policy=c01_nts.Policy(frozenset({'time.cloudflare.com','nts.netnod.se'})) c01_nts.CONFIG.parent.mkdir(parents=True,exist_ok=True) c01_nts.CONFIG.write_bytes(c01_nts.configuration(policy)) c01_nts.CONFIG.chmod(0o644) account=pwd.getpwnam(c01_nts.OBSERVER_USER) c01_nts.SOCKET.parent.mkdir(mode=0o700,exist_ok=True) os.chown(c01_nts.SOCKET.parent,account.pw_uid,account.pw_gid) c01_nts.SOCKET.parent.chmod(0o700) libc=ctypes.CDLL(None) def no_privileges(): if libc.prctl(38,1,0,0,0)!=0: os._exit(111) log=(root/'chronyd.log').open('wb') daemon=subprocess.Popen([str(c01_nts.CHRONYD),'-x','-U','-n','-u',c01_nts.OBSERVER_USER, '-f',str(c01_nts.CONFIG)],env={'PATH':'/usr/bin:/bin','LC_ALL':'C'},stdout=log,stderr=log, preexec_fn=no_privileges) failures=[] try: end=time.monotonic()+240 while time.monotonic()