#!/usr/bin/env python3 """Run the actual privileged shell flow against isolated fake macOS commands.""" import json import os from pathlib import Path import shlex import subprocess import sys import tempfile import time ROOT = Path(__file__).resolve().parents[1] SOURCE = ROOT / 'libexec/lemana-vpn-dns-cleanup' FAKE = r''' import json, os, pathlib, sys, time base = pathlib.Path(__file__).resolve().parent path = base / 'system.json' state = json.loads(path.read_text()) cmd = pathlib.Path(sys.argv[0]).name if cmd == 'fake.py': cmd = sys.argv.pop(1) args = sys.argv[1:] def save(): path.write_text(json.dumps(state)) def event(text): with (base/'events').open('a') as output: output.write(text+'\n') if cmd == 'id': print('0') elif cmd == 'stat': info = pathlib.Path(args[-1]).stat() print('0:'+oct(info.st_mode & 0o777)[2:]) elif cmd == 'sysctl': print(state['boot']) elif cmd == 'ps': if state.get('ps_fail'): sys.exit(1) if state.get('ps_empty'): sys.exit(0) pid = os.environ['TEST_SHELL_PID'] parent = state['owner'] if os.environ.get('TEST_HOOK') else (200 if os.environ.get('TEST_BEGIN') else 1) rows = [(1,0,'launchd'), (pid,parent,'/bin/sh -s --')] if state.get('vpn_active') or os.environ.get('TEST_HOOK'): rows += [(state['owner'],300,'/opt/homebrew/bin/openconnect --cookie-on-stdin'), (300,200,'python /fake/openconnect-lite'), (200,1,'/bin/bash /fake/vpn-lemanapro.sh --json'), (210,200,'/bin/bash /fake/vpn-lemanapro.sh --json'), (211,200,'/bin/bash /fake/vpn-lemanapro.sh --json')] if state.get('reused_pid'): rows.append((400,1,'/usr/bin/sleep 999')) if state.get('extra_process'): rows.append((700,state.get('extra_parent',1),state['extra_process'])) if 'rows' in state: rows = [(1,0,'launchd'), (pid,parent,'/bin/sh -s --')] + state['rows'] if args[0] == '-p': print(state.get('uids', {}).get(args[1], 501)) if state.get('reuse_on_uid') == args[1]: state.setdefault('starts', {})[args[1]] = 'Wed Oct 7 13:00:00 2026' save() else: for pid, ppid, command in rows: start = state.get('starts', {}).get(str(pid), 'Wed Oct 7 12:00:00 2026') print(f'{pid} {ppid} {start} {command}') elif cmd == 'kill': assert args[0] == '-TERM', args event('signal '+args[1]) if state.get('kill_fail'): sys.exit(1) elif cmd == 'networksetup': if args[0] == '-getcurrentlocation': print(state['location']) elif args[0] == '-listallnetworkservices': print('An asterisk (*) denotes that a network service is disabled.') print('\n'.join(state['services'])) elif args[0] == '-getdnsservers': if args[1] not in state['services']: sys.exit(1) dns=state['services'][args[1]] print('\n'.join(dns) if dns else "There aren't any DNS Servers set on "+args[1]+'.') elif args[0] == '-setdnsservers': event('set '+json.dumps(args[1:])) if args[1] in state.get('fail_write',[]): sys.exit(1) state['services'][args[1]]=[] if args[2:] == ['empty'] else args[2:] save() else: raise AssertionError(args) elif cmd == 'stock': reason=os.environ['reason'] event('stock '+reason) if reason in ('connect','reconnect') and os.environ.get('INTERNAL_IP4_DNS'): assert (base/'root/journal').exists(), 'DNS changed before durable journal' if state.get('pause_stock'): (base/'entered').touch() deadline=time.monotonic()+10 while not (base/'release').exists(): assert time.monotonic()