#requires -Version 5.1 #requires -RunAsAdministrator [CmdletBinding()] param( [Parameter(Mandatory)][string]$Candidate, [Parameter(Mandatory)][ValidatePattern('^[0-9a-f]{64}$')][string]$ExpectedPreviousSha256, [Parameter(Mandatory)][ValidatePattern('^[0-9a-f]{64}$')][string]$ExpectedCandidateSha256, [Parameter(Mandatory)][Guid]$AgentId, [Parameter(Mandatory)][Guid[]]$VerifyReleaseIds ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest $app = Join-Path $env:ProgramFiles 'RustDiscovery' $data = Join-Path $env:ProgramData 'RustDiscovery' $installed = Join-Path $app 'rmm-updater-windows.exe' function Hash([string]$Path) { return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant() } if ((Hash $Candidate) -cne $ExpectedCandidateSha256 -or (Hash $installed) -cne $ExpectedPreviousSha256) { throw 'Updater hash mismatch.' } $svc = Get-CimInstance Win32_Service -Filter "Name='RmmDiscoveryReleasePull'" if ($svc.PathName -cne ('"' + $installed + '" --release-service') -or $svc.StartName -cne 'LocalSystem' -or $svc.State -ne 'Stopped') { throw 'Unexpected updater service.' } $identity = Get-Content (Join-Path $data 'state\identity.json') -Raw | ConvertFrom-Json $agent = Get-CimInstance Win32_Service -Filter "Name='RmmDiscoveryWindows'" if ($identity.agent_id -cne $AgentId.ToString() -or $agent.State -ne 'Running') { throw 'Unexpected running agent.' } foreach ($id in $VerifyReleaseIds) { $generation = Join-Path (Join-Path $app 'releases') $id.ToString() $result = & $Candidate --verify-bootstrap (Join-Path $data 'release-keyring.json') (Join-Path $generation 'envelope.json') (Join-Path $generation 'rmm-agent-windows.exe') (Join-Path $generation 'rmm-helper-windows.exe') if ($LASTEXITCODE -ne 0) { throw 'Release signature verification failed.' } if (($result | ConvertFrom-Json).release_id -cne $id.ToString()) { throw 'Release identity mismatch.' } } $backup = Join-Path $app ('updater-before-' + $ExpectedPreviousSha256 + '.exe') $tmp = Join-Path $app ('.stable-' + [Guid]::NewGuid().ToString('N') + '.exe') if (Test-Path $backup) { throw 'Updater backup already exists.' } try { [IO.File]::Copy($Candidate, $tmp, $false) if ((Hash $tmp) -cne $ExpectedCandidateSha256) { throw 'Staged updater hash mismatch.' } [IO.File]::Replace($tmp, $installed, $backup) } finally { if (Test-Path $tmp) { Remove-Item $tmp -Force } } if ((Hash $installed) -cne $ExpectedCandidateSha256 -or (Hash $backup) -cne $ExpectedPreviousSha256) { throw 'Updater replacement failed.' } & sc.exe failure RmmDiscoveryReleasePull reset= 86400 actions= restart/15000/restart/60000/restart/300000 | Out-Null if ($LASTEXITCODE -ne 0) { throw 'SCM recovery setup failed.' } & sc.exe failureflag RmmDiscoveryReleasePull 1 | Out-Null if ($LASTEXITCODE -ne 0) { throw 'SCM recovery flag failed.' } Start-Service RmmDiscoveryReleasePull [pscustomobject]@{ utc = [DateTime]::UtcNow.ToString('o') agent_id = $identity.agent_id previous_sha256 = $ExpectedPreviousSha256 updater_sha256 = Hash $installed updater = [string](Get-Service RmmDiscoveryReleasePull).Status backup = $backup } | ConvertTo-Json -Compress