|
| 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 | +# Usage: |
| 11 | +# pwsh sshd_login_grace_test.ps1 -SshdExe <path-to-wolfsshd.exe> [-Port N] [-Grace N] |
| 12 | +# (SshdExe also accepts the SSHD_PATH environment variable.) |
| 13 | + |
| 14 | +param( |
| 15 | + [string]$SshdExe = $env:SSHD_PATH, |
| 16 | + [int]$Port = 22224, |
| 17 | + [int]$Grace = 5 |
| 18 | +) |
| 19 | + |
| 20 | +$ErrorActionPreference = "Stop" |
| 21 | +$exitCode = 1 |
| 22 | + |
| 23 | +$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 24 | +$repoRoot = (Resolve-Path (Join-Path $scriptDir "..\..\..")).Path |
| 25 | +$keyPath = (Resolve-Path (Join-Path $repoRoot "keys\server-key.pem")).Path |
| 26 | +$logFile = Join-Path $scriptDir "grace_log.txt" |
| 27 | +$confFile = Join-Path $scriptDir "sshd_config_test_login_grace" |
| 28 | +$authFile = Join-Path $scriptDir "authorized_keys_test" |
| 29 | + |
| 30 | +if (-not $SshdExe -or -not (Test-Path $SshdExe)) { |
| 31 | + Write-Host "ERROR: wolfsshd.exe not found (pass -SshdExe or set SSHD_PATH)" |
| 32 | + exit 1 |
| 33 | +} |
| 34 | + |
| 35 | +@" |
| 36 | +Port $Port |
| 37 | +Protocol 2 |
| 38 | +LoginGraceTime $Grace |
| 39 | +PermitRootLogin yes |
| 40 | +PasswordAuthentication yes |
| 41 | +PermitEmptyPasswords no |
| 42 | +UseDNS no |
| 43 | +HostKey $keyPath |
| 44 | +AuthorizedKeysFile $authFile |
| 45 | +"@ | Out-File -FilePath $confFile -Encoding ASCII |
| 46 | + |
| 47 | +"" | Out-File -FilePath $authFile -Encoding ASCII |
| 48 | +if (Test-Path $logFile) { Remove-Item $logFile -Force } |
| 49 | + |
| 50 | +# Run wolfsshd in the foreground (-D) with debug logging to a file (-E). On |
| 51 | +# Windows, -D selects the non-service foreground path, which lets us read the |
| 52 | +# log just like the Unix test does. |
| 53 | +$sshd = Start-Process -FilePath $SshdExe ` |
| 54 | + -ArgumentList "-D", "-d", "-E", "`"$logFile`"", "-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 hold it open without authenticating. |
| 78 | + $stall = New-Object System.Net.Sockets.TcpClient |
| 79 | + $stall.Connect("127.0.0.1", $Port) |
| 80 | + |
| 81 | + # Wait past the grace deadline (plus margin) for the server to drop it. |
| 82 | + Start-Sleep -Seconds ($Grace + 3) |
| 83 | + $stall.Close() |
| 84 | + |
| 85 | + # Stop wolfsshd before reading the log. On Windows the running daemon keeps |
| 86 | + # the log file open, so it cannot be read concurrently; the grace-period |
| 87 | + # line is already flushed to disk by the logging callback. |
| 88 | + if ($sshd -and -not $sshd.HasExited) { |
| 89 | + Stop-Process -Id $sshd.Id -Force -ErrorAction SilentlyContinue |
| 90 | + $sshd.WaitForExit(5000) | Out-Null |
| 91 | + } |
| 92 | + |
| 93 | + if (Select-String -Path $logFile -Pattern "Failed login within grace period" -Quiet) { |
| 94 | + Write-Host "PASS: unauthenticated connection dropped at login grace deadline" |
| 95 | + $exitCode = 0 |
| 96 | + } |
| 97 | + else { |
| 98 | + Write-Host "FAIL: grace period not enforced" |
| 99 | + if (Test-Path $logFile) { Get-Content $logFile -Tail 40 } |
| 100 | + } |
| 101 | +} |
| 102 | +catch { |
| 103 | + Write-Host "ERROR: $_" |
| 104 | + $exitCode = 1 |
| 105 | +} |
| 106 | +finally { |
| 107 | + if ($sshd -and -not $sshd.HasExited) { |
| 108 | + Stop-Process -Id $sshd.Id -Force -ErrorAction SilentlyContinue |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +exit $exitCode |
0 commit comments