CI / test (push) Successful in 2m41s
- face/pixi.toml + pixi.lock (python 3.12, numpy, opencv-python-headless). `just install-face` passes the pixi on your PATH; install.py runs `pixi install --frozen` as root into /usr/local/lib/chandra/face-env (not the checkout's .pixi: the PAM module runs this Python as root, so it must not be user-writable), writes the helper's shebang for it and builds the PAM module with it as the default python. No python3-opencv needed; uninstall removes the env. - The PAM module now also refuses a Python that isn't root's; group-writable is accepted only for group root (conda/pixi files are 0775 root:root), in the module and chandra-face's checks. - Elevation re-runs on the same interpreter (sys.executable). "OpenCV is missing: run just install-face" instead of naming the distro package. Dropped a stray __pycache__. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
252 lines
8.3 KiB
C
252 lines
8.3 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/local/lib/chandra/face-env/.pixi/envs/default/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);
|
|
}
|
|
}
|
|
|
|
/* Only root can change it: root's, not writable by others, nor by a group other than root's
|
|
* (pixi environments keep conda's 0775 modes, group root). */
|
|
static int root_only(const struct stat *st) {
|
|
return st->st_uid == 0 && !(st->st_mode & 002) && (!(st->st_mode & 020) || st->st_gid == 0);
|
|
}
|
|
|
|
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 and its Python must be root's: they run as root with the user's face data. */
|
|
struct stat st;
|
|
if (stat(o.helper, &st) != 0 || !root_only(&st)) {
|
|
if (o.debug) pam_syslog(pamh, LOG_WARNING, "helper %s missing or not root's", o.helper);
|
|
return PAM_AUTHINFO_UNAVAIL;
|
|
}
|
|
if (stat(o.python, &st) != 0 || !root_only(&st)) {
|
|
if (o.debug) pam_syslog(pamh, LOG_WARNING, "python %s missing or not root's", o.python);
|
|
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;
|
|
}
|