59 lines
3.9 KiB
PowerShell
59 lines
3.9 KiB
PowerShell
#requires -Version 5.1
|
|
#requires -RunAsAdministrator
|
|
[CmdletBinding()]
|
|
param([Parameter(Mandatory)][DateTimeOffset]$Until)
|
|
|
|
# Development bootstrap transport only; grants no RMM job permission.
|
|
$ErrorActionPreference = 'Stop'
|
|
if ($Until -le [DateTimeOffset]::UtcNow -or $Until -gt [DateTimeOffset]::UtcNow.AddHours(4)) {
|
|
throw 'Provide a future deadline within four hours.'
|
|
}
|
|
$root = 'C:\ProgramData\RmmDevAccess'
|
|
$config = Get-Content -LiteralPath (Join-Path $root 'bootstrap-public.json') -Raw | ConvertFrom-Json
|
|
if ($config.server -cne 'discovery.ease-technologies.de') { throw 'Unexpected project server.' }
|
|
$known = (Get-Content -LiteralPath (Join-Path $root 'known_hosts') -Raw).Trim()
|
|
if ($known -cne ('discovery.ease-technologies.de ' + $config.server_host_key)) { throw 'Original SSH pin differs.' }
|
|
$public = ((Get-Content -LiteralPath (Join-Path $root 'tunnel_ed25519.pub') -Raw).Trim() -split '\s+')[0..1] -join ' '
|
|
if ($public -cne 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJEkk35zkrDXkOqggrvgsrataMZOzrqFTSh3jWbptbSa') { throw 'Unexpected existing tunnel key.' }
|
|
$listeners = @(Get-NetTCPConnection -State Listen -LocalPort 22222)
|
|
if ($listeners.Count -ne 1 -or $listeners[0].LocalAddress -cne '127.0.0.1' -or
|
|
(Get-Service sshd).Status -ne 'Running') { throw 'Expected original loopback SSH service.' }
|
|
$existing = @(Get-CimInstance Win32_Process -Filter "Name='ssh.exe'" | Where-Object {
|
|
$_.CommandLine -like '*127.0.0.1:22240:127.0.0.1:22222*'
|
|
})
|
|
if ($existing.Count) { throw 'An existing original tunnel is active; do not launch another.' }
|
|
$user = Get-LocalUser -Name 'rmm-dev'
|
|
if (!$user.Enabled -or !$user.AccountExpires) { throw 'Expected existing enabled temporary access account.' }
|
|
$originalExpiry = $user.AccountExpires
|
|
$folder = Join-Path $root ('development-window-' + [guid]::NewGuid().ToString())
|
|
New-Item -ItemType Directory -Path $folder | Out-Null
|
|
$record = [ordered]@{ account_sid=$user.SID.Value; original_expiry_utc=$originalExpiry.ToUniversalTime().ToString('o'); until_utc=$Until.UtcDateTime.ToString('o'); process_id=$PID }
|
|
$record | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $folder 'intent.json') -Encoding UTF8
|
|
$arguments = @('-F','NUL','-N','-T','-o','BatchMode=yes','-o','IdentitiesOnly=yes',
|
|
'-o','ForwardAgent=no','-o','StrictHostKeyChecking=yes','-o','GlobalKnownHostsFile=NUL',
|
|
'-o',('UserKnownHostsFile=' + $root + '\known_hosts'),'-o','ExitOnForwardFailure=yes',
|
|
'-o','ConnectTimeout=10','-o','ServerAliveInterval=15','-o','ServerAliveCountMax=2',
|
|
'-i',($root + '\tunnel_ed25519'),'-R','127.0.0.1:22240:127.0.0.1:22222',
|
|
'rmm-win-tunnel@discovery.ease-technologies.de')
|
|
$child = $null
|
|
try {
|
|
Set-LocalUser -SID $user.SID -AccountExpires $Until.LocalDateTime
|
|
if ((Get-LocalUser -SID $user.SID).AccountExpires.ToUniversalTime() -ne $Until.UtcDateTime) { throw 'Account deadline readback differs.' }
|
|
$child = Start-Process -FilePath "$env:WINDIR\System32\OpenSSH\ssh.exe" -ArgumentList $arguments -PassThru -NoNewWindow
|
|
if ($child.WaitForExit(1500)) { throw "Original SSH process exited: $($child.ExitCode)." }
|
|
Write-Host "Development transport open until $($Until.UtcDateTime.ToString('o')). RMM approval unchanged."
|
|
while ([DateTimeOffset]::UtcNow -lt $Until) {
|
|
if ($child.WaitForExit(500)) { throw "Original SSH process exited: $($child.ExitCode)." }
|
|
}
|
|
} finally {
|
|
if ($null -ne $child) {
|
|
try { if (!$child.HasExited) { $child.Kill(); $child.WaitForExit() } } finally { $child.Dispose() }
|
|
}
|
|
$current = Get-LocalUser -SID $user.SID
|
|
if ($current.AccountExpires.ToUniversalTime() -eq $Until.UtcDateTime) {
|
|
Set-LocalUser -SID $user.SID -AccountExpires $originalExpiry
|
|
}
|
|
[ordered]@{ closed_at=[DateTimeOffset]::UtcNow.ToString('o'); expiry_utc=(Get-LocalUser -SID $user.SID).AccountExpires.ToUniversalTime().ToString('o') } |
|
|
ConvertTo-Json | Set-Content -LiteralPath (Join-Path $folder 'closed.json') -Encoding UTF8
|
|
}
|