#!/usr/bin/env python3 """One 24h renewal of the existing restricted development SSH transport. No new key, port, shell, RMM setting, download lease, stop/kill or reboot. The unchanged tunnel expires naturally; one SYSTEM worker starts its original scheduled task at original expiry+60s. This deliberately permits a short gap. Lost mutation replies allow readback only. Main config publication is Root work. """ import argparse import base64 import datetime as dt import inspect import json import os from pathlib import Path import subprocess import uuid import client_bootstrap as cb import system_pipe_upgrade as pair require, sha, canonical, save = pair.require, pair.sha, pair.canonical, pair.save STATE = cb.EVIDENCE / 'dev-access-renewal' OLD_EXPIRY = '2026-11-14T12:55:31.254999+00:00' OLD_UTC = dt.datetime.fromisoformat(OLD_EXPIRY) NEW_UTC = OLD_UTC.replace(microsecond=0) + dt.timedelta(hours=24) START_UTC = OLD_UTC.replace(microsecond=0) + dt.timedelta(seconds=60) def server(payload): import datetime as dt import re import stat root=Path('/opt/rust-discovery/devstate/windows-bootstrap') authorized=Path('/var/lib/rmm-win-tunnel/.ssh/authorized_keys') profile=Path('/etc/ssh/sshd_config.d/80-rmm-windows-tunnel.conf') ident=str(uuid.UUID(payload['id']));case=root/('renew-'+ident) require(payload['phase'] in ('state','enter','readback'),'fixed renewal phase') def check(path,mode): s=path.lstat();require(not path.is_symlink() and stat.S_ISREG(s.st_mode) and s.st_uid==0 and stat.S_IMODE(s.st_mode)==mode and s.st_size<65536,'protected original transport file') def digest(path):return hashlib.sha256(path.read_bytes()).hexdigest() check(authorized,0o644);check(profile,0o644) account=pwd.getpwnam('rmm-win-tunnel');require(account.pw_dir=='/var/lib/rmm-win-tunnel' and account.pw_shell=='/usr/sbin/nologin','existing forwarding-only account') public=' '.join(Path('/etc/ssh/ssh_host_ed25519_key.pub').read_text().split()[:2]) fingerprint=subprocess.run(['ssh-keygen','-lf','/etc/ssh/ssh_host_ed25519_key.pub'],capture_output=True,check=True,timeout=10).stdout.decode().split()[1] require(fingerprint=='SHA256:+NEin4XJc3CbhjQi7/ZpMIgxOrXn8AT2tlpTaXW5quw','confirmed original server host key') raw=authorized.read_text();match=re.fullmatch(r'restrict,port-forwarding,permitlisten="127\.0\.0\.1:22240",expiry-time="(\d{14}Z)" (ssh-ed25519 [A-Za-z0-9+/=]{68})\n',raw) require(match is not None and match[2]==payload['tunnel_public_key'],'exact unchanged forwarding key/options') effective=subprocess.run(['/usr/sbin/sshd','-T','-C','user=rmm-win-tunnel,host=localhost,addr=127.0.0.1'],capture_output=True,check=True,timeout=10).stdout for line in (b'allowtcpforwarding remote\n',b'permitlisten 127.0.0.1:22240\n',b'permitopen none\n',b'gatewayports no\n',b'maxsessions 0\n',b'passwordauthentication no\n'):require(line in effective,'same restricted forwarding-only profile') def state():return dict(read_only=True,authorized_sha256=digest(authorized),profile_sha256=digest(profile),effective_sha256=hashlib.sha256(effective).hexdigest(),server_host_key_sha256=hashlib.sha256(public.encode()).hexdigest(),tunnel_public_key_sha256=hashlib.sha256(match[2].encode()).hexdigest(),expiry=match[1],uid=account.pw_uid,gid=account.pw_gid,home=account.pw_dir,shell=account.pw_shell) if payload['phase']=='state':return state() if payload['phase']=='readback': value=state();value['case_exists']=case.exists();value['replace_intent']=case.exists() and (case/'replace-intent.json').exists();value['completed']=case.exists() and (case/'completed.json').exists();return value old=dt.datetime.fromisoformat(payload['old_expiry']).astimezone(dt.timezone.utc);new=dt.datetime.fromisoformat(payload['new_expiry']).astimezone(dt.timezone.utc) require(old.isoformat()=='2026-11-14T12:55:31.254999+00:00' and new==old.replace(microsecond=0)+dt.timedelta(hours=24) and dt.datetime.now(dt.timezone.utc)