Files
connector-popos/configure.d/lib/pam.sh
T
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

64 lines
2.5 KiB
Bash

#!/bin/bash
#
# Add and remove an auth line at the top of a PAM service's stack, for signing
# in to sudo and polkit (the password prompts of COSMIC's apps and settings)
# with a security key or a fingerprint. Each line is "sufficient": when the key
# or finger isn't there, it falls through to the password, as before.
#
# Only these two services change, not common-auth, which pam-auth-update owns
# and every service (the login screen, su, sshd) includes.
# For the tests
PAM_DIR="${PAM_DIR:-/etc/pam.d}"
PAM_VENDOR_DIR="${PAM_VENDOR_DIR:-/usr/lib/pam.d}"
# The services to sign in to this way (used by the items that source this)
# shellcheck disable=SC2034
PAM_SERVICES=(sudo polkit-1)
# True if service $1's stack uses module $2 (pam_u2f.so)
pam_has() {
grep -qE "^[^#]*[[:space:]]$2([[:space:]]|$)" "$PAM_DIR/$1" 2> /dev/null
}
# Make sure service $1 has its own file in PAM_DIR. polkit's comes in
# PAM_VENDOR_DIR, and a file in PAM_DIR replaces it.
pam_own_file() {
[ -f "$PAM_DIR/$1" ] && return 0
[ -f "$PAM_VENDOR_DIR/$1" ] || { echo "There's no PAM file for $1."; return 1; }
sudo cp "$PAM_VENDOR_DIR/$1" "$PAM_DIR/$1"
}
# The first line of a stack's auth part: an auth line, or Ubuntu's
# "@include common-auth", which is how sudo's and polkit's start
PAM_AUTH_START='^[[:space:]]*(-?auth[[:space:]]|@include[[:space:]]+common-auth([[:space:]]|$))'
# pam_add SERVICE MODULE LINE...: put the lines just before the first auth line
# of SERVICE's stack, unless it already uses MODULE
pam_add() {
local service="$1" module="$2" file
shift 2
file="$PAM_DIR/$service"
pam_has "$service" "$module" && return 0
pam_own_file "$service" || return 1
grep -qE "$PAM_AUTH_START" "$file" || { echo "$file has no auth lines."; return 1; }
# awk writes the new file, then it replaces the old one in one step, so sudo
# never sees a half-written file
PAM_LINES="$(printf '%s\n' "$@")" awk -v start="$PAM_AUTH_START" '
!done && $0 ~ start { print ENVIRON["PAM_LINES"]; done = 1 }
{ print }
' "$file" | sudo tee "$file.federated-connector" > /dev/null \
&& sudo chmod 644 "$file.federated-connector" \
&& sudo mv -f "$file.federated-connector" "$file"
}
# pam_remove SERVICE PATTERN: remove the lines matching PATTERN (an extended
# regular expression) from SERVICE's stack
pam_remove() {
local file="$PAM_DIR/$1"
[ -f "$file" ] && grep -qE "$2" "$file" || return 0
grep -vE "$2" "$file" | sudo tee "$file.federated-connector" > /dev/null \
&& sudo chmod 644 "$file.federated-connector" \
&& sudo mv -f "$file.federated-connector" "$file"
}