Files
davidandClaude Opus 5.5 7185d6cb3e Sign in to sudo and polkit with a security key or fingerprint
Ports configure security-key and configure fingerprint from CoreDesktop's
9dc5eb1: touch a FIDO2 key or a finger instead of typing your password for
sudo and polkit, falling back to the password. Each shows only when the
hardware is there. The fingerprint skips to the password with the lid shut.

lib/pam.sh is CoreDesktop's, but also takes Ubuntu's "@include
common-auth" as the start of a stack's auth part, which is how sudo's and
polkit's begin. Only those two stacks change, not common-auth, which
pam-auth-update owns and every sign-in includes. libpam-fprintd's
pam-auth-update profile is off by default, so installing it changes no
sign-in by itself. The login and lock screens keep asking for the
password, unlike on GNOME.

Smoketest: 70-sign-in, on copies of Ubuntu's sudo and polkit stacks.
TEST.md: with a real key and reader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:03:41 -07:00

107 lines
3.4 KiB
Bash

#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="Security key"
# shellcheck disable=SC2034
DESCRIPTION="Touch a security key instead of typing your password for sudo"
source "$CONNECTOR/configure.d/lib/pam.sh"
# Root's, so nothing running as you can add a key of its own. One line per
# user: user:key:key...
U2F_MAPPINGS="${U2F_MAPPINGS:-/etc/u2f_mappings}"
# pam_u2f checks keys against the computer's name unless told otherwise, so
# renaming the computer would lock every key out. Use a name that stays.
U2F_ORIGIN="pam://federated-connector"
U2F_LINE="auth sufficient pam_u2f.so cue origin=$U2F_ORIGIN appid=$U2F_ORIGIN authfile=$U2F_MAPPINGS"
# pam_u2f, pamu2fcfg to register a key, and fido2-token to find one
U2F_PACKAGES=(libpam-u2f pamu2fcfg fido2-tools)
# For the tests
HIDRAW="${HIDRAW:-/sys/class/hidraw}"
# A FIDO2 key is a USB HID device whose report descriptor starts its usage page
# 0xF1D0 (bytes 06 d0 f1), which is how libfido2 finds them too
key_plugged_in() {
local descriptor
for descriptor in "$HIDRAW"/*/device/report_descriptor; do
[ -r "$descriptor" ] || continue
od -An -tx1 -v "$descriptor" | tr -d ' \n' | grep -q '06d0f1' && return 0
done
return 1
}
key_registered() {
grep -q "^$USER:" "$U2F_MAPPINGS" 2> /dev/null
}
# Shown when a key is plugged in, or one is set up already
is_installed() {
key_registered || pam_has sudo pam_u2f.so || key_plugged_in
}
is_configured() {
key_registered && pam_has sudo pam_u2f.so
}
register_key() {
local credential
pkg_ensure "Security keys" "${U2F_PACKAGES[@]}" || return 1
if [ -z "$(fido2-token -L 2> /dev/null)" ]; then
echo "No security key found. Plug it in (and unlock it, if it needs that), then run: configure security-key"
return 1
fi
echo "Touch your security key when it blinks (enter its PIN first if it asks)."
credential="$(pamu2fcfg -n -o "$U2F_ORIGIN" -i "$U2F_ORIGIN")" || { echo "The key wasn't registered."; return 1; }
credential="${credential#:}"
[ -n "$credential" ] || { echo "The key wasn't registered."; return 1; }
if key_registered; then
sudo sed -i "s|^$USER:.*|&:$credential|" "$U2F_MAPPINGS" || return 1
else
echo "$USER:$credential" | sudo tee -a "$U2F_MAPPINGS" > /dev/null || return 1
fi
sudo chmod 644 "$U2F_MAPPINGS"
}
use_keys() {
local service
for service in "${PAM_SERVICES[@]}"; do
pam_add "$service" pam_u2f.so "$U2F_LINE" || return 1
done
}
stop_using_keys() {
local service
for service in "${PAM_SERVICES[@]}"; do
pam_remove "$service" '[[:space:]]pam_u2f\.so([[:space:]]|$)' || return 1
done
if [ -f "$U2F_MAPPINGS" ]; then
sudo sed -i "/^$USER:/d" "$U2F_MAPPINGS" || return 1
fi
echo "sudo and the password prompts ask for your password only."
}
run_configure() {
local choice
if is_configured; then
echo "sudo and the password prompts take a touch of your security key."
echo
echo " 1) Add another key"
echo " 2) Stop using security keys"
echo " q) Leave it as it is"
echo
read -rp "Choose: " choice
case "$choice" in
1) ;;
2) stop_using_keys; return ;;
*) return 0 ;;
esac
fi
register_key || return 1
use_keys || return 1
echo "Done. sudo and the password prompts now ask you to touch your key. Without it,"
echo "they ask for your password, as before. To add a spare key: configure security-key"
}