|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +# |
| 3 | +# Windows regression test for wolfsshd LoginGraceTime enforcement. |
| 4 | +# |
| 5 | +# Opens a raw TCP connection that never authenticates and verifies that |
| 6 | +# wolfsshd drops it at the login grace deadline. No Windows user account or |
| 7 | +# authorized key is required, because the connection is closed before |
| 8 | +# authentication ever completes - this exercises the pre-auth grace timer only. |
| 9 | +# |
| 10 | +# Enforcement is checked behaviorally (the server closes the connection at the |
| 11 | +# grace deadline), not by reading the daemon log, so the test does not depend on |
| 12 | +# debug logging being compiled into the wolfSSH Windows build. |
| 13 | +# |
| 14 | +# Usage: |
| 15 | +# pwsh sshd_login_grace_test.ps1 -SshdExe <path-to-wolfsshd.exe> [-Port N] [-Grace N] |
| 16 | +# (SshdExe also accepts the SSHD_PATH environment variable.) |
| 17 | + |
| 18 | +param( |
| 19 | + [string]$SshdExe = $env:SSHD_PATH, |
| 20 | + [int]$Port = 22224, |
| 21 | + [int]$Grace = 5 |
| 22 | +) |
| 23 | + |
| 24 | +$ErrorActionPreference = "Stop" |
| 25 | +$exitCode = 1 |
| 26 | + |
| 27 | +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 28 | +$repoRoot = (Resolve-Path (Join-Path $scriptDir "..\..\..")).Path |
| 29 | +$keyPath = (Resolve-Path (Join-Path $repoRoot "keys\server-key.pem")).Path |
| 30 | +$confFile = Join-Path $scriptDir "sshd_config_test_login_grace" |
| 31 | +$authFile = Join-Path $scriptDir "authorized_keys_test" |
| 32 | + |
| 33 | +if (-not $SshdExe -or -not (Test-Path $SshdExe)) { |
| 34 | + Write-Host "ERROR: wolfsshd.exe not found (pass -SshdExe or set SSHD_PATH)" |
| 35 | + exit 1 |
| 36 | +} |
| 37 | + |
| 38 | +@" |
| 39 | +Port $Port |
| 40 | +Protocol 2 |
| 41 | +LoginGraceTime $Grace |
| 42 | +PermitRootLogin yes |
| 43 | +PasswordAuthentication yes |
| 44 | +PermitEmptyPasswords no |
| 45 | +UseDNS no |
| 46 | +HostKey $keyPath |
| 47 | +AuthorizedKeysFile $authFile |
| 48 | +"@ | Out-File -FilePath $confFile -Encoding ASCII |
| 49 | + |
| 50 | +"" | Out-File -FilePath $authFile -Encoding ASCII |
| 51 | + |
| 52 | +# Run wolfsshd in the foreground (-D selects the non-service path on Windows). |
| 53 | +$sshd = Start-Process -FilePath $SshdExe ` |
| 54 | + -ArgumentList "-D", "-f", "`"$confFile`"", "-p", "$Port" ` |
| 55 | + -NoNewWindow -PassThru |
| 56 | + |
| 57 | +try { |
| 58 | + # Wait for the listener to accept connections. |
| 59 | + $up = $false |
| 60 | + for ($i = 0; $i -lt 20; $i++) { |
| 61 | + try { |
| 62 | + $probe = New-Object System.Net.Sockets.TcpClient |
| 63 | + $probe.Connect("127.0.0.1", $Port) |
| 64 | + $probe.Close() |
| 65 | + $up = $true |
| 66 | + break |
| 67 | + } |
| 68 | + catch { |
| 69 | + Start-Sleep -Milliseconds 500 |
| 70 | + } |
| 71 | + } |
| 72 | + if (-not $up) { |
| 73 | + # throw rather than exit so the finally block still stops the daemon |
| 74 | + throw "wolfsshd did not start listening on port $Port" |
| 75 | + } |
| 76 | + |
| 77 | + # Open a raw TCP connection and never authenticate. The server sends its |
| 78 | + # banner, waits for ours, and must close the connection once the login grace |
| 79 | + # time expires. Block on Read (with a timeout well past the grace time) and |
| 80 | + # measure when the server closes the connection. |
| 81 | + $stall = New-Object System.Net.Sockets.TcpClient |
| 82 | + $stall.Connect("127.0.0.1", $Port) |
| 83 | + $stream = $stall.GetStream() |
| 84 | + $stream.ReadTimeout = ($Grace + 5) * 1000 |
| 85 | + |
| 86 | + $buf = New-Object byte[] 4096 |
| 87 | + $dropped = $false |
| 88 | + $sw = [System.Diagnostics.Stopwatch]::StartNew() |
| 89 | + try { |
| 90 | + while ($true) { |
| 91 | + $n = $stream.Read($buf, 0, $buf.Length) |
| 92 | + if ($n -le 0) { |
| 93 | + $dropped = $true # server closed the connection |
| 94 | + break |
| 95 | + } |
| 96 | + # otherwise the server sent its banner; keep waiting for the drop |
| 97 | + } |
| 98 | + } |
| 99 | + catch [System.IO.IOException] { |
| 100 | + # Read timed out: the connection was still open past the grace time. |
| 101 | + $dropped = $false |
| 102 | + } |
| 103 | + $elapsed = [math]::Round($sw.Elapsed.TotalSeconds, 1) |
| 104 | + $stall.Close() |
| 105 | + |
| 106 | + Write-Host "connection closed=$dropped after ${elapsed}s (grace=$Grace)" |
| 107 | + |
| 108 | + if ($dropped -and ($elapsed -ge ($Grace - 1)) -and ($elapsed -le ($Grace + 4))) { |
| 109 | + Write-Host "PASS: unauthenticated connection dropped at login grace deadline" |
| 110 | + $exitCode = 0 |
| 111 | + } |
| 112 | + elseif ($dropped) { |
| 113 | + Write-Host "FAIL: connection closed at ${elapsed}s, not near the grace deadline ($Grace s)" |
| 114 | + } |
| 115 | + else { |
| 116 | + Write-Host "FAIL: connection still open past the grace time (not enforced)" |
| 117 | + } |
| 118 | +} |
| 119 | +catch { |
| 120 | + Write-Host "ERROR: $_" |
| 121 | + $exitCode = 1 |
| 122 | +} |
| 123 | +finally { |
| 124 | + if ($sshd -and -not $sshd.HasExited) { |
| 125 | + Stop-Process -Id $sshd.Id -Force -ErrorAction SilentlyContinue |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +exit $exitCode |
0 commit comments