diff --git a/bin/omarchy-setup-security-sshd b/bin/omarchy-setup-security-sshd index 7b93936ff637..ce4ac56a7dda 100755 --- a/bin/omarchy-setup-security-sshd +++ b/bin/omarchy-setup-security-sshd @@ -57,10 +57,21 @@ if [[ -n $KEY && -n $GITHUB_USER ]]; then exit 2 fi -setup_sshd() { - echo "Installing and starting the OpenSSH server..." +install_openssh() { + echo "Installing the OpenSSH server..." omarchy-pkg-add openssh - sudo systemctl enable --now sshd.service + + # sshd validates no config without host keys, and the server is not started + # until its config has been validated. + sudo ssh-keygen -A >/dev/null +} + +# Reload rather than restart a server that is already running, so an +# administrator already connected keeps their session. +start_sshd() { + echo "Starting the OpenSSH server..." + sudo systemctl enable sshd.service + sudo systemctl reload-or-restart sshd.service } open_firewall() { @@ -181,16 +192,13 @@ CONF sudo rm -f "$config" return 1 fi - - # Reload rather than restart so an administrator already connected keeps - # their session. - sudo systemctl reload sshd.service } echo -e "\e[32mSetting up SSH server access with key-based authentication.\n\e[0m" -setup_sshd -open_firewall +# Nothing listens and no port opens until a key is authorized and passwords are +# off, so a cancelled prompt or a rejected key leaves the machine as it was. +install_openssh echo if [[ -n $KEY ]]; then @@ -206,6 +214,8 @@ else fi disable_password_auth +start_sshd +open_firewall echo -e "\e[32m\nPerfect! The SSH server is running and your key is authorized.\e[0m" echo "Password logins are off; this machine now accepts authorized keys only." diff --git a/test/shell.d/setup-security-sshd-test.sh b/test/shell.d/setup-security-sshd-test.sh index b060c5ecbc05..230916e39bbc 100755 --- a/test/shell.d/setup-security-sshd-test.sh +++ b/test/shell.d/setup-security-sshd-test.sh @@ -16,14 +16,29 @@ printf 'pkg %s\n' "$*" >>"${CALL_LOG:?}" STUB cat >"$stub_bin/omarchy-cmd-missing" <<'STUB' #!/bin/bash -exit 0 +exit 1 +STUB +cat >"$stub_bin/ufw" <<'STUB' +#!/bin/bash +printf 'ufw %s\n' "$*" >>"${CALL_LOG:?}" +[[ -e ${TEST_ROOT:?}/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || echo "exposed before hardening" >>"$CALL_LOG" STUB cat >"$stub_bin/systemctl" <<'STUB' #!/bin/bash printf 'systemctl %s\n' "$*" >>"${CALL_LOG:?}" +[[ -e ${TEST_ROOT:?}/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || echo "exposed before hardening" >>"$CALL_LOG" +STUB +cat >"$stub_bin/gum" <<'STUB' +#!/bin/bash +printf 'gum %s\n' "$1" >>"${CALL_LOG:?}" +# Esc at the first prompt. +exit 130 STUB cat >"$stub_bin/sshd" <<'STUB' #!/bin/bash +# A fresh install has no host keys until ssh-keygen -A runs, and sshd -t and +# sshd -T refuse to run without them. +[[ -e ${TEST_ROOT:?}/host-keys ]] || exit 1 case $1 in -t) [[ ${SSHD_SYNTAX_VALID:-1} == 1 ]] @@ -54,6 +69,9 @@ install) rm) /usr/bin/rm -f "${TEST_ROOT:?}${3:?}" ;; +ssh-keygen) + : >"${TEST_ROOT:?}/host-keys" + ;; *) exec "$@" ;; @@ -65,7 +83,7 @@ ssh-keygen -q -t ed25519 -N "" -f "$test_dir/key" public_key=$(<"$test_dir/key.pub") run_setup() { - local scenario="$1" + local scenario="$1" key="${2:-$public_key}" local home="$test_dir/$scenario/home" local root="$test_dir/$scenario/root" @@ -77,14 +95,27 @@ run_setup() { SSHD_PASSWORD_AUTH="${SSHD_PASSWORD_AUTH:-no}" \ SSHD_KBD_AUTH="${SSHD_KBD_AUTH:-no}" \ PATH="$stub_bin:$PATH" \ - bash "$ROOT/bin/omarchy-setup-security-sshd" --key="$public_key" + bash "$ROOT/bin/omarchy-setup-security-sshd" --key="$key" +} + +# Nothing may listen or be opened while passwords could still be accepted. +assert_nothing_exposed() { + local scenario="$1" + + ! grep -qE "^systemctl (enable|start|restart|reload)" "$test_dir/$scenario.calls" || + fail "SSH setup must not start sshd when $scenario" + ! grep -q "^ufw " "$test_dir/$scenario.calls" || + fail "SSH setup must not open the firewall when $scenario" } output=$(run_setup success) config="$test_dir/success/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf" grep -qxF "PasswordAuthentication no" "$config" || fail "SSH setup disables password authentication" grep -qxF "KbdInteractiveAuthentication no" "$config" || fail "SSH setup disables keyboard-interactive authentication" -grep -qxF "systemctl reload sshd.service" "$test_dir/success.calls" || fail "SSH setup reloads the validated config" +grep -qxF "systemctl reload-or-restart sshd.service" "$test_dir/success.calls" || fail "SSH setup starts sshd with the validated config" +grep -qxF "ufw limit 22/tcp comment omarchy-sshd" "$test_dir/success.calls" || fail "SSH setup opens the SSH port" +! grep -qxF "exposed before hardening" "$test_dir/success.calls" || fail "SSH setup starts sshd and opens the port only after passwords are off" +grep -qxF "$public_key" "$test_dir/success/home/.ssh/authorized_keys" || fail "SSH setup writes the authorized key" grep -q "Password logins are off" <<<"$output" || fail "SSH setup reports hardening after it succeeds" pass "SSH setup authorizes a key and disables password logins" @@ -99,8 +130,7 @@ if SSHD_PASSWORD_AUTH=yes run_setup ineffective >"$test_dir/ineffective.output" fi [[ ! -e $test_dir/ineffective/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || fail "SSH setup removes an ineffective hardening config" -! grep -qF "systemctl reload sshd.service" "$test_dir/ineffective.calls" || - fail "SSH setup must not reload ineffective hardening" +assert_nothing_exposed ineffective ! grep -q "Password logins are off" "$test_dir/ineffective.output" || fail "SSH setup must not claim ineffective hardening succeeded" pass "SSH setup verifies the effective daemon settings" @@ -110,8 +140,25 @@ if SSHD_SYNTAX_VALID=0 run_setup invalid >"$test_dir/invalid.output" 2>&1; then fi [[ ! -e $test_dir/invalid/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || fail "SSH setup removes a rejected hardening config" -! grep -qF "systemctl reload sshd.service" "$test_dir/invalid.calls" || - fail "SSH setup must not reload a rejected config" +assert_nothing_exposed invalid ! grep -q "Password logins are off" "$test_dir/invalid.output" || fail "SSH setup must not claim rejected hardening succeeded" pass "SSH setup fails safely when sshd rejects the config" + +if run_setup rejected-key "not-a-key" >"$test_dir/rejected-key.output" 2>&1; then + fail "SSH setup must fail when the key is rejected" +fi +assert_nothing_exposed rejected-key +[[ ! -e $test_dir/rejected-key/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || + fail "SSH setup writes no hardening config without a key" +pass "SSH setup starts nothing and opens nothing when no key is authorized" + +mkdir -p "$test_dir/cancelled/home" "$test_dir/cancelled/root" +: >"$test_dir/cancelled.calls" +if HOME="$test_dir/cancelled/home" TEST_ROOT="$test_dir/cancelled/root" CALL_LOG="$test_dir/cancelled.calls" \ + PATH="$stub_bin:$ROOT/bin:$PATH" bash "$ROOT/bin/omarchy-setup-security-sshd" >/dev/null 2>&1; then + fail "SSH setup must fail when the key prompt is cancelled" +fi +grep -q "^gum choose" "$test_dir/cancelled.calls" || fail "SSH setup reaches the key prompt" +assert_nothing_exposed cancelled +pass "SSH setup starts nothing and opens nothing when the key prompt is cancelled"