CI / test (push) Successful in 2m6s
- face/pam_chandra_face.so (`-auth sufficient`): runs `chandra-face compare` as root with a clean
environment, forwards what it says as PAM_TEXT_INFO / PAM_ERROR_MSG, and maps its exit status
to success / auth error / unavailable, so the password always still works. Skips remote
sessions (PAM_RHOST, SSH_CONNECTION), a closed lid and users without faces; refuses a helper
that isn't root's; kills one that hangs (timeout + 6 s).
- face/chandra-face (Python + OpenCV): YuNet finds the face, SFace embeds it, cosine similarity
against the enrolled embeddings (threshold 0.42, two matching frames, dark IR warm-up frames
skipped, optional require_ir). enroll / list / remove / clear / test / status / pam
enable|disable / fetch-models (sha256-pinned opencv_zoo models). Embeddings only, in
/var/lib/chandra-face/USER/faces.json (root, 0600); labels in faces.list (0644). Store, config
and models must be root's and not writable by others. Root commands re-run through run0 (or
pkexec), so polkit asks first. Settings in /etc/chandra/face.conf.
- `just install-face [--pam]`, `just uninstall-face [--purge]` (face/install.py; uninstall takes
the module out of every PAM service first). `pam enable` inserts before the first auth or
@include line (Debian's sudo has only @include common-auth), keeping a .bak.
- Password dialog: a face row ("Looking for your face…" with a sweeping ring, "Face
recognized", or why not, "Type your password instead"); typing and Enter during the scan go in
when PAM asks for the password. Howdy's pam_howdy.so messages are understood too.
- Settings > Session > Face unlock: "Use my face for passwords" (polkit-1 + sudo), the enrolled
faces (Remove takes a second click), "Add a face…", and the tool's progress. New `face-unlock`
capability: tables 24 (status) and 25 (faces), request 44, in the wasmer/wasmtime stub, the Rust
host, the MoonBit wasmhost and chandra:host/plasmoids. compositor/shell/face.mbt runs the tool
on a socket so the compositor never waits.
- Tests: face/test_face.py (fake camera of PGM frames + fake recognizer; --pam builds the module
and runs it through pamtester), dialog states and message parsing (polkit_test), the dialog via
a mock helper (auth_test, shots 14/15), Settings against a fake chandra-face (face_test).
scripts/test.sh runs them (compositor/polkit was missing from its native list), CI installs
libpam0g-dev + pamtester. Also regenerated two stale desktop-module bindings (doc comments).
- docs/face-unlock.md, including the limits of 2D face recognition.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
109 lines
4.4 KiB
Python
109 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Install (or remove) face unlock. Run as root (`just install-face` does this through
|
|
scripts/as-root):
|
|
|
|
run0 python3 face/install.py # install, fetch the face models
|
|
run0 python3 face/install.py --pam # ... and use it for polkit-1 and sudo
|
|
run0 python3 face/install.py --no-models
|
|
run0 python3 face/install.py --remove # take it out of every PAM service, uninstall
|
|
run0 python3 face/install.py --remove --purge # ... and delete the enrolled faces
|
|
|
|
Installs:
|
|
/usr/local/lib/chandra/chandra-face the helper (enroll, compare, ...), root's
|
|
/usr/local/bin/chandra-face -> the helper
|
|
<PAM module dir>/pam_chandra_face.so the PAM module (built here with cc; needs libpam headers)
|
|
/etc/chandra/face.conf settings (kept when it exists)
|
|
/var/lib/chandra-face/ enrolled faces
|
|
/usr/local/share/chandra/face/*.onnx the YuNet and SFace models (downloaded, checksummed)
|
|
|
|
PAM files are only changed with --pam (or later with `chandra-face pam enable SERVICE`); the line
|
|
is `-auth sufficient pam_chandra_face.so`, before the service's other auth lines, so the password
|
|
still works whenever the face doesn't.
|
|
"""
|
|
import glob, os, shutil, subprocess, sys
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
helper = "/usr/local/lib/chandra/chandra-face"
|
|
link = "/usr/local/bin/chandra-face"
|
|
conf = "/etc/chandra/face.conf"
|
|
store = "/var/lib/chandra-face"
|
|
models = "/usr/local/share/chandra/face"
|
|
|
|
|
|
def pam_dir():
|
|
"""Where this system's PAM modules live (next to pam_unix.so)."""
|
|
cands = ["/usr/lib/security", "/lib/security", "/usr/lib64/security", "/lib64/security"]
|
|
cands += glob.glob("/usr/lib/*-linux-gnu*/security") + glob.glob("/lib/*-linux-gnu*/security")
|
|
for d in cands:
|
|
if os.path.exists(os.path.join(d, "pam_unix.so")):
|
|
return os.path.realpath(d)
|
|
sys.exit("can't find the PAM module directory (no pam_unix.so)")
|
|
|
|
|
|
if os.geteuid() != 0:
|
|
sys.exit("run as root: run0 python3 face/install.py (or sudo), or `just install-face`")
|
|
|
|
mod = os.path.join(pam_dir(), "pam_chandra_face.so")
|
|
|
|
if "--remove" in sys.argv:
|
|
# PAM first, so no service points at a module that is about to go.
|
|
if os.path.exists(helper):
|
|
subprocess.call([sys.executable, "-I", helper, "--no-elevate", "pam", "disable"]
|
|
+ sorted(os.listdir("/etc/pam.d")))
|
|
for p in (mod, link, helper):
|
|
if os.path.lexists(p):
|
|
os.remove(p)
|
|
print("removed", p)
|
|
if os.path.isdir(models):
|
|
shutil.rmtree(models)
|
|
print("removed", models)
|
|
if "--purge" in sys.argv:
|
|
for p in (store,):
|
|
if os.path.isdir(p):
|
|
shutil.rmtree(p)
|
|
print("removed", p)
|
|
if os.path.exists(conf):
|
|
os.remove(conf)
|
|
print("removed", conf)
|
|
sys.exit(0)
|
|
|
|
os.umask(0o022)
|
|
os.makedirs(os.path.dirname(helper), exist_ok=True)
|
|
shutil.copyfile(os.path.join(here, "chandra-face"), helper + ".new")
|
|
os.chmod(helper + ".new", 0o755)
|
|
os.replace(helper + ".new", helper)
|
|
print("installed", helper)
|
|
if os.path.lexists(link):
|
|
os.remove(link)
|
|
os.symlink(helper, link)
|
|
print("linked", link, "->", helper)
|
|
|
|
cc = shutil.which("cc") or shutil.which("gcc") or shutil.which("clang")
|
|
if not cc:
|
|
sys.exit("no C compiler: install gcc (and the libpam headers: libpam0g-dev / pam-devel)")
|
|
subprocess.check_call([cc, "-O2", "-Wall", "-shared", "-fPIC", "-o", mod + ".new",
|
|
"-DCHANDRA_FACE_HELPER=\"%s\"" % helper,
|
|
os.path.join(here, "pam_chandra_face.c"), "-lpam"])
|
|
os.chmod(mod + ".new", 0o644)
|
|
os.replace(mod + ".new", mod)
|
|
print("installed", mod)
|
|
|
|
if not os.path.exists(conf):
|
|
os.makedirs(os.path.dirname(conf), exist_ok=True)
|
|
shutil.copyfile(os.path.join(here, "face.conf"), conf)
|
|
os.chmod(conf, 0o644)
|
|
print("installed", conf)
|
|
os.makedirs(store, 0o755, exist_ok=True)
|
|
os.chmod(store, 0o755)
|
|
|
|
if "--no-models" not in sys.argv:
|
|
subprocess.check_call([sys.executable, "-I", helper, "--no-elevate", "fetch-models"])
|
|
if "--pam" in sys.argv:
|
|
subprocess.check_call([sys.executable, "-I", helper, "--no-elevate", "pam", "enable"])
|
|
try:
|
|
import cv2 # noqa
|
|
except ImportError:
|
|
print("note: install OpenCV for Python (python3-opencv / python-opencv / opencv-python) "
|
|
"before enrolling a face")
|
|
print("enroll a face: Settings > Session > Face unlock, or `chandra-face enroll`")
|