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
242 lines
7.8 KiB
C
242 lines
7.8 KiB
C
/* pam_chandra_face.so: face unlock for PAM, in the style of Howdy (docs/face-unlock.md).
|
|
*
|
|
* -auth sufficient pam_chandra_face.so [helper=PATH] [python=PATH] [config=PATH] [timeout=SECONDS]
|
|
* [quiet] [debug]
|
|
*
|
|
* Runs `chandra-face compare --user USER` as root with a clean environment and forwards what it
|
|
* says to the application's conversation as PAM_TEXT_INFO / PAM_ERROR_MSG (Chandra's password
|
|
* dialog shows them as the face status; sudo prints them). The helper's exit status decides:
|
|
* 0 -> PAM_SUCCESS, 1 -> PAM_AUTH_ERR, anything else -> PAM_AUTHINFO_UNAVAIL. With "sufficient",
|
|
* anything but success falls through to the next module, so the password always still works.
|
|
*
|
|
* Skipped (PAM_AUTHINFO_UNAVAIL, silently) for remote logins (PAM_RHOST, or SSH_CONNECTION in the
|
|
* environment) and when every laptop lid is closed (the camera can't see anyone). A helper that
|
|
* hangs is killed after timeout + 6 seconds.
|
|
*/
|
|
#define _GNU_SOURCE
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <syslog.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#define PAM_SM_AUTH
|
|
#include <security/pam_appl.h>
|
|
#include <security/pam_ext.h>
|
|
#include <security/pam_modules.h>
|
|
|
|
#ifndef CHANDRA_FACE_HELPER
|
|
#define CHANDRA_FACE_HELPER "/usr/local/lib/chandra/chandra-face"
|
|
#endif
|
|
#ifndef CHANDRA_FACE_PYTHON
|
|
#define CHANDRA_FACE_PYTHON "/usr/bin/python3"
|
|
#endif
|
|
|
|
struct opts {
|
|
const char *python;
|
|
const char *helper;
|
|
const char *config;
|
|
int timeout;
|
|
int quiet;
|
|
int debug;
|
|
};
|
|
|
|
static void parse_opts(int argc, const char **argv, struct opts *o) {
|
|
o->python = CHANDRA_FACE_PYTHON;
|
|
o->helper = CHANDRA_FACE_HELPER;
|
|
o->config = NULL;
|
|
o->timeout = 4;
|
|
o->quiet = 0;
|
|
o->debug = 0;
|
|
for (int i = 0; i < argc; i++) {
|
|
if (!strncmp(argv[i], "helper=", 7)) o->helper = argv[i] + 7;
|
|
else if (!strncmp(argv[i], "python=", 7)) o->python = argv[i] + 7;
|
|
else if (!strncmp(argv[i], "config=", 7)) o->config = argv[i] + 7;
|
|
else if (!strncmp(argv[i], "timeout=", 8)) o->timeout = atoi(argv[i] + 8);
|
|
else if (!strcmp(argv[i], "quiet")) o->quiet = 1;
|
|
else if (!strcmp(argv[i], "debug")) o->debug = 1;
|
|
}
|
|
if (o->timeout < 1) o->timeout = 1;
|
|
if (o->timeout > 30) o->timeout = 30;
|
|
}
|
|
|
|
/* Whether a lid switch exists and every one says closed. */
|
|
static int lid_closed(void) {
|
|
DIR *d = opendir("/proc/acpi/button/lid");
|
|
if (!d) return 0;
|
|
int any = 0, open_one = 0;
|
|
struct dirent *e;
|
|
while ((e = readdir(d))) {
|
|
if (e->d_name[0] == '.') continue;
|
|
char p[512], buf[128] = {0};
|
|
snprintf(p, sizeof p, "/proc/acpi/button/lid/%s/state", e->d_name);
|
|
FILE *f = fopen(p, "r");
|
|
if (!f) continue;
|
|
if (fgets(buf, sizeof buf, f)) {
|
|
any = 1;
|
|
if (!strstr(buf, "closed")) open_one = 1;
|
|
}
|
|
fclose(f);
|
|
}
|
|
closedir(d);
|
|
return any && !open_one;
|
|
}
|
|
|
|
static int is_remote(pam_handle_t *pamh) {
|
|
const void *rhost = NULL;
|
|
if (pam_get_item(pamh, PAM_RHOST, &rhost) == PAM_SUCCESS && rhost) {
|
|
const char *h = rhost;
|
|
if (*h && strcmp(h, "localhost") && strcmp(h, "127.0.0.1") && strcmp(h, "::1")) return 1;
|
|
}
|
|
if (getenv("SSH_CONNECTION") || getenv("SSH_CLIENT") || getenv("SSHD_OPTS")) return 1;
|
|
const char *pe = pam_getenv(pamh, "SSH_CONNECTION");
|
|
return pe && *pe;
|
|
}
|
|
|
|
/* Forward one helper line ("info TEXT" / "error TEXT") to the conversation. */
|
|
static void forward(pam_handle_t *pamh, const struct opts *o, char *line) {
|
|
if (!strncmp(line, "info ", 5)) {
|
|
if (!o->quiet) pam_info(pamh, "%s", line + 5);
|
|
} else if (!strncmp(line, "error ", 6)) {
|
|
if (!o->quiet) pam_error(pamh, "%s", line + 6);
|
|
}
|
|
}
|
|
|
|
static long now_ms(void) {
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
return ts.tv_sec * 1000L + ts.tv_nsec / 1000000L;
|
|
}
|
|
|
|
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) {
|
|
(void)flags;
|
|
struct opts o;
|
|
parse_opts(argc, argv, &o);
|
|
const char *user = NULL;
|
|
if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS || !user || !*user) return PAM_USER_UNKNOWN;
|
|
if (is_remote(pamh)) return PAM_AUTHINFO_UNAVAIL;
|
|
if (lid_closed()) return PAM_AUTHINFO_UNAVAIL;
|
|
/* The helper must be root's: it runs as root with the user's face data. */
|
|
struct stat st;
|
|
if (stat(o.helper, &st) != 0 || st.st_uid != 0 || (st.st_mode & 022)) {
|
|
if (o.debug) pam_syslog(pamh, LOG_WARNING, "helper %s missing or not root's", o.helper);
|
|
return PAM_AUTHINFO_UNAVAIL;
|
|
}
|
|
int pfd[2];
|
|
if (pipe2(pfd, O_CLOEXEC) != 0) return PAM_AUTHINFO_UNAVAIL;
|
|
/* Our caller may ignore SIGCHLD (then waitpid can't see the helper exit); restore it. */
|
|
struct sigaction sa_old, sa_dfl;
|
|
memset(&sa_dfl, 0, sizeof sa_dfl);
|
|
sa_dfl.sa_handler = SIG_DFL;
|
|
sigaction(SIGCHLD, &sa_dfl, &sa_old);
|
|
pid_t pid = fork();
|
|
if (pid < 0) {
|
|
close(pfd[0]);
|
|
close(pfd[1]);
|
|
sigaction(SIGCHLD, &sa_old, NULL);
|
|
return PAM_AUTHINFO_UNAVAIL;
|
|
}
|
|
if (pid == 0) {
|
|
/* Child: become fully root (setuid callers have only euid 0), stdout to the pipe, stdin and
|
|
* stderr to /dev/null, nothing of the caller's environment. */
|
|
if (geteuid() == 0) {
|
|
if (setgid(0) != 0 || setuid(0) != 0) _exit(3);
|
|
}
|
|
int devnull = open("/dev/null", O_RDWR);
|
|
dup2(devnull >= 0 ? devnull : pfd[1], 0);
|
|
dup2(pfd[1], 1);
|
|
if (!o.debug && devnull >= 0) dup2(devnull, 2);
|
|
/* Nothing else of the caller's (its sockets, its PAM handles' files) goes to the helper. */
|
|
for (int fd = 3; fd < 1024; fd++) close(fd);
|
|
char timeout_s[16];
|
|
snprintf(timeout_s, sizeof timeout_s, "%d", o.timeout);
|
|
char *envp[] = {"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
|
"LANG=C.UTF-8", "HOME=/root", NULL};
|
|
const char *args[12];
|
|
int n = 0;
|
|
args[n++] = o.python;
|
|
args[n++] = "-I";
|
|
args[n++] = o.helper;
|
|
args[n++] = "compare";
|
|
args[n++] = "--machine";
|
|
args[n++] = "--no-elevate";
|
|
args[n++] = "--user";
|
|
args[n++] = user;
|
|
if (o.config) {
|
|
args[n++] = "--config";
|
|
args[n++] = o.config;
|
|
}
|
|
args[n] = NULL;
|
|
execve(o.python, (char *const *)args, envp);
|
|
_exit(3);
|
|
}
|
|
close(pfd[1]);
|
|
long deadline = now_ms() + (long)(o.timeout + 6) * 1000L;
|
|
char buf[1024];
|
|
size_t len = 0;
|
|
int killed = 0;
|
|
for (;;) {
|
|
long left = deadline - now_ms();
|
|
if (left <= 0) {
|
|
kill(pid, SIGKILL);
|
|
killed = 1;
|
|
break;
|
|
}
|
|
struct pollfd p = {.fd = pfd[0], .events = POLLIN};
|
|
int r = poll(&p, 1, (int)left);
|
|
if (r < 0 && errno == EINTR) continue;
|
|
if (r <= 0) continue;
|
|
ssize_t got = read(pfd[0], buf + len, sizeof buf - 1 - len);
|
|
if (got < 0 && errno == EINTR) continue;
|
|
if (got <= 0) break;
|
|
len += (size_t)got;
|
|
buf[len] = 0;
|
|
char *nl;
|
|
while ((nl = memchr(buf, '\n', len))) {
|
|
*nl = 0;
|
|
forward(pamh, &o, buf);
|
|
size_t used = (size_t)(nl - buf) + 1;
|
|
memmove(buf, nl + 1, len - used);
|
|
len -= used;
|
|
buf[len] = 0;
|
|
}
|
|
if (len == sizeof buf - 1) len = 0; /* an overlong line: drop it */
|
|
}
|
|
close(pfd[0]);
|
|
int status = 0;
|
|
while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {
|
|
}
|
|
sigaction(SIGCHLD, &sa_old, NULL);
|
|
if (killed) {
|
|
if (!o.quiet) pam_error(pamh, "Face unlock: took too long");
|
|
return PAM_AUTHINFO_UNAVAIL;
|
|
}
|
|
if (!WIFEXITED(status)) return PAM_AUTHINFO_UNAVAIL;
|
|
switch (WEXITSTATUS(status)) {
|
|
case 0:
|
|
pam_syslog(pamh, LOG_NOTICE, "face recognized for %s", user);
|
|
return PAM_SUCCESS;
|
|
case 1:
|
|
return PAM_AUTH_ERR;
|
|
default:
|
|
return PAM_AUTHINFO_UNAVAIL;
|
|
}
|
|
}
|
|
|
|
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) {
|
|
(void)pamh;
|
|
(void)flags;
|
|
(void)argc;
|
|
(void)argv;
|
|
return PAM_SUCCESS;
|
|
}
|