diff --git a/bin/omarchy-setup-security-fingerprint b/bin/omarchy-setup-security-fingerprint index a57dea4a1269..4b6afa8d97a1 100755 --- a/bin/omarchy-setup-security-fingerprint +++ b/bin/omarchy-setup-security-fingerprint @@ -6,6 +6,13 @@ set -e +fingerprints_enrolled() { + # fprintd-list states an empty enrollment in prose — "User … has no fingers + # enrolled for …" — so matching the word "finger" reads as enrolled either + # way. Enrolled fingers are listed one per line as " - #0: right-index-finger". + fprintd-list "$USER" 2>/dev/null | grep -qE '^[[:space:]]*-[[:space:]]*#[0-9]+:' +} + setup_pam_config() { # A clamshell gate runs before pam_fprintd in every stack: when the lid is # shut the reader is unreachable, so it skips fingerprint (success=1) and PAM @@ -65,6 +72,22 @@ account include system-local-login EOF } +# Verify the print, then write PAM. Detection only proves a reader is there, +# not that libfprint can drive it — an Elan MOC sensor outside the elanmoc +# table gets this far and then fails to enroll. Editing the stacks up front +# would leave those machines pointing at pam_fprintd with nothing to match. +configure_verified_fingerprint() { + echo -e "\nNow let's verify that it's working correctly.\n" + if fprintd-verify; then + setup_pam_config + setup_lock_fingerprint_pam + echo -e "\e[32m\nPerfect! Fingerprint authentication is now configured.\e[0m" + echo "You can use your fingerprint for sudo, polkit, and lock screen (Super + Ctrl + L)." + else + echo -e "\e[31m\nVerification failed. You may want to try enrolling again.\e[0m" + fi +} + echo -e "\e[32mSetting up fingerprint scanner for authentication.\n\e[0m" @@ -83,29 +106,33 @@ if omarchy-pkg-missing libfprint-git fprintd usbutils; then sudo pacman -S --needed --noconfirm --ask 4 -- libfprint-git fprintd usbutils fi -# Enroll first fingerprint -echo -e "\e[32m\nLet's setup your right index finger as the first fingerprint.\e[0m" -echo -e "Keep moving the finger around on sensor until the process completes.\n" +# A prior enroll that never reached PAM (or remove-then-setup that left prints +# in /var/lib/fprint) must not dead-end on enroll-duplicate. Offer to wire PAM +# for what is already enrolled; otherwise fall through to enroll as usual. +skip_enroll=false +if fingerprints_enrolled; then + echo -e "\e[32m\nFingerprints are already enrolled for $USER.\e[0m" + if gum confirm "Skip enrollment and configure authentication for the existing fingerprints?"; then + skip_enroll=true + fi +fi -if sudo fprintd-enroll "$USER"; then - echo -e "\e[32m\nFingerprint enrolled successfully!\e[0m" +if [[ $skip_enroll == true ]]; then + configure_verified_fingerprint +else + echo -e "\e[32m\nLet's setup your right index finger as the first fingerprint.\e[0m" + echo -e "Keep moving the finger around on sensor until the process completes.\n" - # Verify - echo -e "\nNow let's verify that it's working correctly.\n" - if fprintd-verify; then - # PAM comes last, once a print is enrolled and verified. Detection only - # proves a reader is there, not that libfprint can drive it — an Elan MOC - # sensor outside the elanmoc table gets this far and then fails to enroll. - # Editing the stacks up front would leave those machines pointing at - # pam_fprintd with nothing to match. - setup_pam_config - setup_lock_fingerprint_pam - echo -e "\e[32m\nPerfect! Fingerprint authentication is now configured.\e[0m" - echo "You can use your fingerprint for sudo, polkit, and lock screen (Super + Ctrl + L)." + if sudo fprintd-enroll "$USER"; then + echo -e "\e[32m\nFingerprint enrolled successfully!\e[0m" + configure_verified_fingerprint + elif fingerprints_enrolled; then + # enroll-duplicate (same finger again) exits non-zero even though a usable + # print is already on disk — still verify and write PAM. + echo -e "\e[33m\nEnrollment did not add a new print, but existing fingerprints were found.\e[0m" + configure_verified_fingerprint else - echo -e "\e[31m\nVerification failed. You may want to try enrolling again.\e[0m" + echo -e "\e[31m\nEnrollment failed. Please try again.\e[0m" + exit 1 fi -else - echo -e "\e[31m\nEnrollment failed. Please try again.\e[0m" - exit 1 fi diff --git a/test/shell.d/fingerprint-package-test.sh b/test/shell.d/fingerprint-package-test.sh index efc14fcc52e7..4741ec7032ac 100755 --- a/test/shell.d/fingerprint-package-test.sh +++ b/test/shell.d/fingerprint-package-test.sh @@ -45,6 +45,11 @@ case "$1" in *) printf 'pacman %s\n' "$*" >> "$CALL_LOG"; exit 99 ;; esac STUB +cat > "$scratch/bin/fprintd-list" <<'STUB' +#!/bin/bash +# No enrolled fingers — the package tests stop at enroll, before PAM. +echo "User ${1:-tester} has no fingers enrolled for Test Sensor." +STUB cat > "$scratch/bin/fprintd-enroll" <<'STUB' #!/bin/bash # Stop before verification/PAM; no host authentication files may be changed. diff --git a/test/shell.d/fingerprint-pam-reenroll-test.sh b/test/shell.d/fingerprint-pam-reenroll-test.sh new file mode 100644 index 000000000000..744b61d98f4b --- /dev/null +++ b/test/shell.d/fingerprint-pam-reenroll-test.sh @@ -0,0 +1,129 @@ +#!/bin/bash +# +# Re-running fingerprint setup must still reach PAM when a print is already +# enrolled. enroll-duplicate exits non-zero, and remove-then-setup leaves prints +# in /var/lib/fprint — either way the wizard used to exit before writing PAM. +# Privileged PAM writers are stubbed so the host stacks are never touched. + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +scratch=$(mktemp -d) +trap 'rm -rf "$scratch"' EXIT +mkdir -p "$scratch/bin" +export CALL_LOG="$scratch/calls" +export PATH="$scratch/bin:$ROOT/bin:$PATH" +export INSTALLED=$'libfprint-git\nfprintd\nusbutils' + +cat > "$scratch/bin/omarchy-hw-fingerprint" <<'STUB' +#!/bin/bash +exit 0 +STUB +cat > "$scratch/bin/pacman" <<'STUB' +#!/bin/bash +case "$1" in + -Q) grep -qx "$2" <<< "${INSTALLED:-}" ;; + *) printf 'pacman %s\n' "$*" >> "$CALL_LOG"; exit 99 ;; +esac +STUB +# Log privileged PAM writers without touching /etc. Consume tee stdin so the +# heredocs in setup_pam_config / setup_lock_fingerprint_pam do not block. +cat > "$scratch/bin/sudo" <<'STUB' +#!/bin/bash +case "$1" in + fprintd-enroll) exec "$@" ;; + tee | sed) + printf 'sudo %s\n' "$*" >> "$CALL_LOG" + [[ $1 == tee ]] && cat >/dev/null + exit 0 + ;; + *) echo "Unexpected privileged call: $*" >> "$CALL_LOG"; exit 99 ;; +esac +STUB +cat > "$scratch/bin/gum" <<'STUB' +#!/bin/bash +echo "gum $*" >> "$CALL_LOG" +exit "${GUM_ANSWER:-0}" +STUB +cat > "$scratch/bin/fprintd-list" <<'STUB' +#!/bin/bash +if [[ ${ENROLLED:-1} == 1 ]]; then + cat <<'OUT' +found 1 devices +Device at /net/reactivated/Fprint/Device/0 +Using device /net/reactivated/Fprint/Device/0 +Fingerprints for user tester on Test Sensor (press): + - #0: right-index-finger +OUT +else + cat < "$scratch/bin/fprintd-enroll" <<'STUB' +#!/bin/bash +echo enroll >> "$CALL_LOG" +echo "Enroll result: ${ENROLL_RESULT:-enroll-duplicate}" +exit "${ENROLL_STATUS:-1}" +STUB +cat > "$scratch/bin/fprintd-verify" <<'STUB' +#!/bin/bash +echo verify >> "$CALL_LOG" +exit "${VERIFY_STATUS:-0}" +STUB +chmod +x "$scratch/bin/"* + +assert_pam_configured() { + local description=$1 + grep -q 'sudo tee /etc/pam.d/omarchy-lock-fingerprint' "$CALL_LOG" || + fail "$description: lock fingerprint PAM was written" + grep -qE 'sudo tee /etc/pam.d/polkit-1|sudo sed .*/etc/pam.d/sudo' "$CALL_LOG" || + fail "$description: sudo or polkit PAM was written" +} + +run_setup() { + : > "$CALL_LOG" + env USER=tester "$@" "$ROOT/bin/omarchy-setup-security-fingerprint" > "$scratch/output" 2>&1 +} + +# Existing prints + confirm skip → no enroll, verify + PAM. +run_setup GUM_ANSWER=0 +if grep -qx enroll "$CALL_LOG"; then + fail "skipping enrollment does not call fprintd-enroll" +fi +grep -qx verify "$CALL_LOG" || fail "skipping enrollment still verifies" +grep -q 'gum confirm' "$CALL_LOG" || fail "existing prints prompt with gum confirm" +assert_pam_configured "skip enrollment configures PAM" +pass "existing prints can skip enrollment and configure PAM" + +# Existing prints + decline skip + enroll-duplicate → still verify + PAM. +run_setup GUM_ANSWER=1 ENROLL_STATUS=1 ENROLL_RESULT=enroll-duplicate +grep -qx enroll "$CALL_LOG" || fail "declining skip still attempts enrollment" +grep -qx verify "$CALL_LOG" || fail "enroll-duplicate still verifies" +assert_pam_configured "enroll-duplicate configures PAM" +pass "enroll-duplicate with existing prints still configures PAM" + +# No prints + enroll failure → exit 1, no PAM writers. +if run_setup ENROLLED=0 ENROLL_STATUS=1; then + fail "enrollment failure without prints exits non-zero" +fi +grep -qx enroll "$CALL_LOG" || fail "a machine with no prints still enrolls" +if grep -qE 'sudo tee|sudo sed' "$CALL_LOG"; then + fail "failed enrollment without prints does not write PAM" +fi +pass "enrollment failure without prints does not write PAM" + +# Fresh enroll success → verify + PAM (no gum prompt when nothing enrolled). +run_setup ENROLLED=0 ENROLL_STATUS=0 +grep -qx enroll "$CALL_LOG" || fail "a fresh machine enrolls" +grep -qx verify "$CALL_LOG" || fail "a fresh enroll verifies" +if grep -q 'gum confirm' "$CALL_LOG"; then + fail "a fresh machine does not prompt to skip enrollment" +fi +assert_pam_configured "fresh enrollment configures PAM" +pass "fresh enrollment configures PAM after verify"