feat: add typed data and views with scoped device power
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[ValidatePattern('^[A-Za-z0-9_-]{1,128}$')][string]$DeviceId,
|
||||
[Security.SecureString]$SecretBase64,
|
||||
[ValidateSet('https', 'tailscale')][string]$Transport = 'https',
|
||||
[switch]$Plan
|
||||
)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$Transport = $Transport.ToLowerInvariant()
|
||||
$serviceName = 'HomeVisionPower'
|
||||
$serviceAccount = "NT SERVICE\$serviceName"
|
||||
$binRoot = Join-Path $env:ProgramFiles $serviceName
|
||||
$dataRoot = Join-Path $env:ProgramData $serviceName
|
||||
$stateRoot = Join-Path $dataRoot 'state'
|
||||
$binary = Join-Path $binRoot 'HomeVisionPower.exe'
|
||||
$sourceBinary = Join-Path $PSScriptRoot 'build\HomeVisionPower.exe'
|
||||
$scExe = Join-Path $env:WINDIR 'System32\sc.exe'
|
||||
|
||||
if ($Plan) {
|
||||
[pscustomobject]@{
|
||||
service = $serviceName; account = $serviceAccount; start = 'demand'; autoStartOrShutdown = $false
|
||||
privilege = 'SeShutdownPrivilege'; bin = $binRoot; data = $dataRoot
|
||||
transport = $Transport
|
||||
secretProtection = 'DPAPI LocalMachine + SYSTEM/Administrators/service read ACL'
|
||||
maintenanceLock = 'created before service registration; retained after installation'
|
||||
} | ConvertTo-Json
|
||||
return
|
||||
}
|
||||
|
||||
$principal = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
|
||||
if (!$principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Run the explicit installer in an elevated PowerShell.' }
|
||||
if (!$DeviceId) { throw '-DeviceId is required and must match the server managed asset ID.' }
|
||||
if (!(Test-Path -LiteralPath $sourceBinary -PathType Leaf)) { throw 'Run build.ps1 and review its passing self-tests first.' }
|
||||
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) { throw 'Service already exists. Stop it and review the update steps in README.md.' }
|
||||
if ((Test-Path -LiteralPath $binRoot) -or (Test-Path -LiteralPath $dataRoot)) { throw 'Installation directories already exist. Preserve the config/journal and use the documented upgrade or recovery steps.' }
|
||||
if (!$SecretBase64) { $SecretBase64 = Read-Host 'Enter the matching server secret (base64 random 32 bytes)' -AsSecureString }
|
||||
|
||||
function Set-PrivateDirectoryAcl([string]$Path, [Security.Principal.SecurityIdentifier]$Identity, [Security.AccessControl.FileSystemRights]$ServiceRights) {
|
||||
$acl = New-Object Security.AccessControl.DirectorySecurity
|
||||
$acl.SetAccessRuleProtection($true, $false)
|
||||
$inherit = [Security.AccessControl.InheritanceFlags]'ContainerInherit,ObjectInherit'
|
||||
$propagation = [Security.AccessControl.PropagationFlags]::None
|
||||
$allow = [Security.AccessControl.AccessControlType]::Allow
|
||||
foreach ($id in @('S-1-5-18', 'S-1-5-32-544')) {
|
||||
$sid = New-Object Security.Principal.SecurityIdentifier($id)
|
||||
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule($sid, 'FullControl', $inherit, $propagation, $allow)))
|
||||
}
|
||||
if ($Identity) { $acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule($Identity, $ServiceRights, $inherit, $propagation, $allow))) }
|
||||
Set-Acl -LiteralPath $Path -AclObject $acl
|
||||
}
|
||||
function Invoke-ServiceControl([string[]]$Arguments) {
|
||||
& $scExe @Arguments
|
||||
if ($LASTEXITCODE -ne 0) { throw "Service Control Manager operation failed: $($Arguments[0])" }
|
||||
}
|
||||
|
||||
# Secure directories BEFORE copying executable or writing any credential material.
|
||||
foreach ($path in @($binRoot, $dataRoot, $stateRoot)) {
|
||||
New-Item -ItemType Directory -Path $path | Out-Null
|
||||
Set-PrivateDirectoryAcl -Path $path -ServiceRights ReadAndExecute
|
||||
}
|
||||
[IO.File]::WriteAllText((Join-Path $dataRoot 'maintenance.lock'), 'Installation safety lock. Remove explicitly only after all work on this host is finished.')
|
||||
Copy-Item -LiteralPath $sourceBinary -Destination $binary
|
||||
Invoke-ServiceControl @('create', $serviceName, 'binPath=', ('"' + $binary + '" --service'), 'start=', 'demand', 'obj=', $serviceAccount, 'DisplayName=', 'Home Vision Safe Power Agent')
|
||||
try {
|
||||
Invoke-ServiceControl @('sidtype', $serviceName, 'unrestricted')
|
||||
Invoke-ServiceControl @('privs', $serviceName, 'SeShutdownPrivilege')
|
||||
Invoke-ServiceControl @('description', $serviceName, 'Outbound fixed-endpoint polling; signed shutdown-only commands; maintenance lock defaults on.')
|
||||
$serviceSid = ([Security.Principal.NTAccount]$serviceAccount).Translate([Security.Principal.SecurityIdentifier])
|
||||
Add-Type -Path (Join-Path $PSScriptRoot 'ServiceRights.cs')
|
||||
[HomeVisionServiceRights]::SetShutdownRight($serviceSid, $true)
|
||||
Set-PrivateDirectoryAcl -Path $binRoot -Identity $serviceSid -ServiceRights ReadAndExecute
|
||||
Set-PrivateDirectoryAcl -Path $dataRoot -Identity $serviceSid -ServiceRights ReadAndExecute
|
||||
Set-PrivateDirectoryAcl -Path $stateRoot -Identity $serviceSid -ServiceRights Modify
|
||||
|
||||
# Secret goes to stdin, never a process argument, command echo or plaintext file.
|
||||
$secretPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecretBase64)
|
||||
try {
|
||||
[Runtime.InteropServices.Marshal]::PtrToStringBSTR($secretPointer) | & $binary --write-config $DeviceId (Join-Path $dataRoot 'config.json') $Transport
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Protected configuration creation failed.' }
|
||||
} finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($secretPointer) }
|
||||
& $binary --check
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Installed configuration check failed.' }
|
||||
$sourceHash = (Get-FileHash -LiteralPath $sourceBinary -Algorithm SHA256).Hash
|
||||
$installedHash = (Get-FileHash -LiteralPath $binary -Algorithm SHA256).Hash
|
||||
if ($sourceHash -ne $installedHash) { throw 'Installed binary hash mismatch.' }
|
||||
Write-Output "Installed SHA256 $installedHash"
|
||||
Write-Output "Installed and STOPPED with maintenance.lock present. Review first, then explicitly Start-Service $serviceName."
|
||||
} catch {
|
||||
# Do not auto-start or remove evidence/config on partial installation.
|
||||
Invoke-ServiceControl @('config', $serviceName, 'start=', 'disabled')
|
||||
throw
|
||||
}
|
||||
Reference in New Issue
Block a user