108 lines
6.1 KiB
Python
108 lines
6.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Fresh, once-only W32Time start after the user-confirmed console boot.
|
|
|
|
Reuse the previously reviewed preservation checks, with a separate admission
|
|
record. Historical start intents and reports are never reset or overwritten.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import uuid
|
|
|
|
import time_service_start as original
|
|
import auto_transport_acceptance as auto
|
|
|
|
cb = original.cb
|
|
require = original.require
|
|
BOOT = '2026-11-14T12:26:50.5000000Z'
|
|
ORIGINAL_SHA = 'ef5d4ce8ec2e8f3486da8540b3f47230406c6604157b7920e1e5202c88b97e6e'
|
|
require(original.digest(original.__file__) == ORIGINAL_SHA, 'frozen original time bootstrap')
|
|
STATE = cb.EVIDENCE / 'time-service-after-console-boot'
|
|
SCRIPT = original.SCRIPT
|
|
|
|
|
|
def substitute(old, new):
|
|
global SCRIPT
|
|
require(SCRIPT.count(old) == 1, 'exact reviewed source derivation')
|
|
SCRIPT = SCRIPT.replace(old, new)
|
|
|
|
|
|
substitute("$active=Join-Path $boot 'w32time-start-once.json'",
|
|
"$active=Join-Path $boot 'w32time-start-after-console-20261114T122650.json'")
|
|
substitute('function Snapshot{', r'''function Snapshot{
|
|
$actualBoot=(Get-CimInstance Win32_OperatingSystem -Property LastBootUpTime).LastBootUpTime.ToUniversalTime().ToString('o')
|
|
if($actualBoot-cne'2026-11-14T12:26:50.5000000Z'){throw 'Exact user-confirmed console boot required'}''')
|
|
substitute("$service=ServiceState $pair[0];if($service.state-cne'Running'-or$service.pid-le0){throw 'Retain both running RMM components'}",
|
|
"""$service=ServiceState $pair[0]
|
|
$stoppedHelper=($pair[0]-ceq'RmmDiscoverySystemHelper'-and$service.state-ceq'Stopped'-and$service.pid-eq0-and$service.definition.start_mode-ceq'Manual'-and$service.definition.account-ceq'LocalSystem')
|
|
if(!$stoppedHelper-and($service.state-cne'Running'-or$service.pid-le0)){throw 'Exact running agent or stopped manual helper required'}
|
|
if($stoppedHelper){$birth=$null;$image=$pair[1]}else{""")
|
|
substitute("finally{$proc.Dispose()}\n if(![string]", "finally{$proc.Dispose()}}\n if(![string]")
|
|
PREPARE = original.PREPARE.replace('TimeServiceStart.ps1', 'TimeServiceAfterBoot.ps1')
|
|
LAUNCH = original.LAUNCH.replace('TimeServiceStart.ps1', 'TimeServiceAfterBoot.ps1')
|
|
|
|
|
|
def remote(folder, payload):
|
|
try:
|
|
return cb.powershell(LAUNCH, {'sha256': hashlib.sha256(SCRIPT.encode()).hexdigest(),
|
|
'payload': payload}, timeout=100, remote_signed=True)
|
|
finally:
|
|
original.preserve_failure(folder)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('action', choices=('prepare', 'start', 'readback'))
|
|
parser.add_argument('--id')
|
|
args = parser.parse_args()
|
|
os.umask(0o077)
|
|
if args.action == 'prepare':
|
|
require(args.id is None, 'fresh separate case')
|
|
original.helper.private_dir(STATE)
|
|
folder = STATE / str(uuid.uuid4()); folder.mkdir(mode=0o700)
|
|
counts, flags = auto.scopes(), auto.status()
|
|
require(auto.flags_disabled(flags), 'closed test hooks')
|
|
pins = dict(agent_sha256='89004ad3475af3fb8a03bdb1e63c3ef1f2a08d31d0d97193e02fe770a1d7f676',
|
|
helper_sha256='e6aacbdd691c8c56612a2ba205c48396defe70e7d527e031aa3a76a8ae5a3662',
|
|
config_sha256='7c9f6f3642ef84337c3cd761d8a4788c4babe81522fdd987a3eb9b819bc8d851',
|
|
policy_sha256='007adf5f4932dcf68fa4c0c3ba601ec1996b350e7e3e1d14e89a31dd712e5703',
|
|
pin_sha256='126670bc6ed71aad76438ec588b739d3bc02d36ae305ce8f8b5613e4a9e11a15')
|
|
payload = dict(id=folder.name, **pins)
|
|
original.save(folder, 'intent.json', dict(payload=payload, boot=BOOT,
|
|
source_sha256=original.digest(__file__), script_sha256=hashlib.sha256(SCRIPT.encode()).hexdigest(),
|
|
server_before=dict(counts=counts, flags=flags)))
|
|
source = folder / 'TimeServiceAfterBoot.ps1'
|
|
with source.open('xb') as out:
|
|
out.write(SCRIPT.encode()); out.flush(); os.fsync(out.fileno())
|
|
original.helper.transfer(source, source.name, hashlib.sha256(SCRIPT.encode()).hexdigest(), PREPARE)
|
|
before = remote(folder, dict(payload, action='state'))
|
|
original.save(folder, 'before.json', before)
|
|
require(before['read_only'] is True and before['snapshot']['time_service']['state'] == 'Stopped', 'fresh stopped time-service baseline')
|
|
print(json.dumps(dict(case_id=folder.name, prepared=True, start_executed=False)))
|
|
return
|
|
require(args.id and str(uuid.UUID(args.id)) == args.id, 'original canonical case')
|
|
folder = STATE / args.id
|
|
intent = cb.contract.protected_json(folder / 'intent.json')
|
|
before = cb.contract.protected_json(folder / 'before.json')
|
|
require(intent['source_sha256'] == original.digest(__file__) and intent['boot'] == BOOT
|
|
and intent['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(), 'same source and user-confirmed boot')
|
|
if args.action == 'start':
|
|
require(dict(counts=auto.scopes(), flags=auto.status()) == intent['server_before'], 'same closed central context')
|
|
original.save(folder, 'start-attempt.json', dict(once=True, lost_reply_readback_only=True))
|
|
payload = intent['payload']
|
|
result = remote(folder, dict(payload, action=args.action, baseline_sha256=before['baseline_sha256'],
|
|
expected_sha256=hashlib.sha256(json.dumps(payload, sort_keys=True, separators=(',', ':')).encode()).hexdigest()))
|
|
original.save(folder, 'result.json' if args.action == 'start' else 'readback-' + str(uuid.uuid4()) + '.json', result)
|
|
require(dict(counts=auto.scopes(), flags=auto.status()) == intent['server_before'], 'central context preserved')
|
|
require(result.get('preserved') is True, 'original components and time configuration preserved')
|
|
current = result.get('current') or result.get('after')
|
|
require(current and current['time_service']['state'] == 'Running', 'actual running readback')
|
|
print(json.dumps(dict(case_id=args.id, running=True, preserved=True,
|
|
readback_only=args.action == 'readback', clock_qualified=False)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|