diff --git a/deploy/windows/Open-ConsoleMaintenanceWindow.ps1 b/deploy/windows/Open-ConsoleMaintenanceWindow.ps1 index 5d6b736..8e62362 100644 --- a/deploy/windows/Open-ConsoleMaintenanceWindow.ps1 +++ b/deploy/windows/Open-ConsoleMaintenanceWindow.ps1 @@ -3,7 +3,8 @@ [CmdletBinding()] param( [Parameter(Mandatory)][DateTimeOffset]$PreviousExpiry, - [Parameter(Mandatory)][DateTimeOffset]$Until + [Parameter(Mandatory)][DateTimeOffset]$Until, + [switch]$ResumeInterrupted ) $ErrorActionPreference = 'Stop' $utcNow = [DateTimeOffset]::UtcNow @@ -16,16 +17,18 @@ $raw = [IO.File]::ReadAllText($configPath) $cfg = $raw | ConvertFrom-Json $user = Get-LocalUser -Name 'rmm-dev' $task = Get-ScheduledTask -TaskPath '\' -TaskName 'RmmDevReverseTunnel' +$expectedState = if ($ResumeInterrupted) { 'Disabled' } else { 'Ready' } +$expectedExpiry = if ($ResumeInterrupted) { $Until } else { $PreviousExpiry } $mismatches = @( - if ($task.State -ne 'Ready') { "task state $($task.State) (expected Ready)" } + if ($task.State -ne $expectedState) { "task state $($task.State) (expected $expectedState)" } if (!$user.Enabled) { 'rmm-dev is disabled' } if (!$user.AccountExpires) { 'rmm-dev has no account expiry' - } elseif ($user.AccountExpires.ToUniversalTime() -ne $PreviousExpiry.UtcDateTime) { - "account expiry $($user.AccountExpires.ToUniversalTime().ToString('o')) (expected $($PreviousExpiry.UtcDateTime.ToString('o')))" + } elseif ($user.AccountExpires.ToUniversalTime() -ne $expectedExpiry.UtcDateTime) { + "account expiry $($user.AccountExpires.ToUniversalTime().ToString('o')) (expected $($expectedExpiry.UtcDateTime.ToString('o')))" } - if ([DateTimeOffset]::Parse($cfg.expires_at) -ne $PreviousExpiry) { - "configuration expiry $($cfg.expires_at) (expected $($PreviousExpiry.UtcDateTime.ToString('o')))" + if ([DateTimeOffset]::Parse($cfg.expires_at) -ne $expectedExpiry) { + "configuration expiry $($cfg.expires_at) (expected $($expectedExpiry.UtcDateTime.ToString('o')))" } if ($cfg.server -cne 'discovery.ease-technologies.de') { 'configured server differs' } $sshdState = (Get-Service -Name sshd).Status @@ -43,9 +46,27 @@ if ($listeners.Count -ne 1 -or $listeners[0].LocalAddress -ne '127.0.0.1') { throw 'Expected only the original loopback SSH listener.' } $backup = Join-Path $dir ('console-window-' + [guid]::NewGuid().ToString() + '.bak') +$originalRaw = $raw +if ($ResumeInterrupted) { + $owners = @(Get-CimInstance Win32_Process -Filter "Name='powershell.exe' OR Name='pwsh.exe' OR Name='ssh.exe'" | Where-Object { + $_.ProcessId -ne $PID -and ($_.CommandLine -match '(?:rmm-window-[0-9a-f-]+|Open-ConsoleMaintenanceWindow)\.ps1' -or + ($_.Name -eq 'ssh.exe' -and $_.CommandLine -match '127\.0\.0\.1:22240:127\.0\.0\.1:22222')) + }) + if ($owners.Count) { throw 'An existing maintenance console or forwarding process is still active. End the original console with Ctrl+C first.' } + $copies = @(Get-ChildItem -LiteralPath $dir -Filter 'console-window-*.bak' -File -Force) + if ($copies.Count -ne 1 -or $copies[0].Name -notmatch '^console-window-[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}\.bak$' -or + ($copies[0].Attributes -band [IO.FileAttributes]::ReparsePoint) -or $copies[0].Length -gt 32768) { + throw 'Expected one original maintenance-window backup.' + } + $backup = $copies[0].FullName + if (Test-Path -LiteralPath ($backup + '.closed')) { throw 'Backup already closed.' } + $originalRaw = [IO.File]::ReadAllText($backup) + $original = $originalRaw | ConvertFrom-Json + if ([DateTimeOffset]::Parse($original.expires_at) -ne $PreviousExpiry) { throw 'Backup expiry differs.' } + $original.expires_at = $Until.ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:sszzz') + if (($original | ConvertTo-Json -Compress) -cne $raw) { throw 'Backup does not reconstruct the current public configuration.' } +} $candidate = $backup + '.new' -$cfg.expires_at = $Until.ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:sszzz') -[IO.File]::WriteAllText($candidate, ($cfg | ConvertTo-Json -Compress), (New-Object Text.UTF8Encoding($false))) $sshArgs = @('-F','NUL','-N','-T','-o','BatchMode=yes','-o','IdentitiesOnly=yes', '-o','ForwardAgent=no','-o','StrictHostKeyChecking=yes','-o','GlobalKnownHostsFile=NUL', '-o',('UserKnownHostsFile=' + $dir + '\known_hosts'),'-o','ExitOnForwardFailure=yes', @@ -56,12 +77,20 @@ $ssh = $null $disabled = $false $accountChanged = $false try { - Disable-ScheduledTask -InputObject $task | Out-Null - $disabled = $true + if (!$ResumeInterrupted) { + $cfg.expires_at = $Until.ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:sszzz') + [IO.File]::WriteAllText($candidate, ($cfg | ConvertTo-Json -Compress), (New-Object Text.UTF8Encoding($false))) + Disable-ScheduledTask -InputObject $task | Out-Null + $disabled = $true + } if ((Get-ScheduledTask -TaskPath '\' -TaskName $task.TaskName).State -ne 'Disabled' -or [IO.File]::ReadAllText($configPath) -cne $raw) { throw 'Existing bootstrap changed.' } - [IO.File]::Replace($candidate, $configPath, $backup) - Set-LocalUser -SID $user.SID -AccountExpires $Until.LocalDateTime + if ($ResumeInterrupted) { + $disabled = $true + } else { + [IO.File]::Replace($candidate, $configPath, $backup) + Set-LocalUser -SID $user.SID -AccountExpires $Until.LocalDateTime + } $accountChanged = $true if ((Get-LocalUser -SID $user.SID).AccountExpires.ToUniversalTime() -ne $Until.UtcDateTime) { throw 'Unexpected account expiry after renewal.' @@ -77,7 +106,7 @@ try { try { if (!$ssh.HasExited) { $ssh.Kill(); $ssh.WaitForExit() } } finally { $ssh.Dispose() } } if ($accountChanged) { Set-LocalUser -SID $user.SID -AccountExpires $PreviousExpiry.LocalDateTime } - if (Test-Path -LiteralPath $backup) { [IO.File]::Replace($backup, $configPath, ($backup + '.closed')) } - if ($disabled -and [IO.File]::ReadAllText($configPath) -ceq $raw) { Enable-ScheduledTask -InputObject $task | Out-Null } - if (Test-Path -LiteralPath $candidate) { Remove-Item -LiteralPath $candidate } + if ($disabled -and (Test-Path -LiteralPath $backup)) { [IO.File]::Replace($backup, $configPath, ($backup + '.closed')) } + if ($disabled -and [IO.File]::ReadAllText($configPath) -ceq $originalRaw) { Enable-ScheduledTask -InputObject $task | Out-Null } + if (!$ResumeInterrupted -and (Test-Path -LiteralPath $candidate)) { Remove-Item -LiteralPath $candidate } } diff --git a/docs/windows-dev-access-recovery.md b/docs/windows-dev-access-recovery.md index b96bb4a..1e6cc86 100644 --- a/docs/windows-dev-access-recovery.md +++ b/docs/windows-dev-access-recovery.md @@ -321,3 +321,14 @@ Konsolen-Readback offen. Die Serverfrist wurde zu diesem Zeitpunkt nochmals mit 15:30 UTC bestätigt. Neuere Versionen des Konsolenskripts benennen die abweichenden Felder einzeln. Drei lokal simulierte Vorprüfungen bestanden (Ausgangszustand, drei abweichende Werte, alle sechs Bedingungen abweichend). + +Der Konsolenreadback vom 15. November um 15:59:11 Uhr MEZ ergab +`Disabled`, aktives Konto, Konto und Konfiguration beide mit Frist 16:30 Uhr +MEZ, passender Servername, laufender sshd und genau eine ausstehende Sicherung. +Der Bediener kann diesen konkreten Zustand mit `-ResumeInterrupted` im selben +Zeitfenster übernehmen. Das Skript prüft die ursprüngliche Frist der Sicherung +und rekonstruiert daraus die aktuelle Konfiguration. Bereits laufende +Wartungsprozesse verhindern einen Doppelstart. Bei regulärem Ende verwendet +es die ursprüngliche Sicherung zur Rücksetzung. Lokaler Parser- und Fixturetest +für passende Sicherung, aktiven Prozess, Konfigurations- und Fristabweichungen +bestanden. Die Wiederaufnahme muss auf der Windows-Konsole erfolgen.