| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- /*
- * pam_fp.so - fingerprint OR password, whichever comes first.
- *
- * Terminal case (sudo):
- * - starts the fingerprint check (fp-pam.sh) in the background
- * - immediately prints "Touch the sensor or type your password:"
- * - waits for EITHER:
- * * sensor match -> PAM_SUCCESS
- * * submitted password -> validated via system-auth (pam_unix,
- * faillock, etc.) -> PAM_SUCCESS / fallback
- * * empty line (Enter) -> fallback to the normal password prompt
- * * finger check gives up -> fallback
- * - terminal echo is disabled while waiting and always restored
- *
- * Non-terminal (polkit GUI): sequential fingerprint check, then fallback.
- *
- * Module args: debug | command=/path | finger_timeout=SECONDS
- * Build: gcc -shared -fPIC -O2 -Wall -o pam_fp.so pam_fp.c -lpam
- */
- #define _GNU_SOURCE
- #include <errno.h>
- #include <fcntl.h>
- #include <poll.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <syslog.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <termios.h>
- #include <unistd.h>
- #include <security/pam_appl.h>
- #include <security/pam_ext.h>
- #include <security/pam_modules.h>
- #define FP_CMD_DEFAULT "/home/nebulon/projects/fingerprint/work/fp-pam.sh"
- #define FP_TIMEOUT_DEFAULT 45
- #define POLL_MS 200
- static int opt_debug;
- static const char *opt_cmd = FP_CMD_DEFAULT;
- static int opt_timeout = FP_TIMEOUT_DEFAULT;
- static volatile sig_atomic_t g_signal;
- /* ------------------------ password validation (recursive PAM) ------------- */
- struct conv_pw { const char *pw; };
- static int sub_conv(int n, const struct pam_message **msg,
- struct pam_response **resp, void *data)
- {
- struct conv_pw *cd = data;
- struct pam_response *r;
- int i;
- if (n <= 0 || !resp)
- return PAM_CONV_ERR;
- r = calloc((size_t)n, sizeof(*r));
- if (!r)
- return PAM_BUF_ERR;
- for (i = 0; i < n; i++) {
- if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF ||
- msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
- r[i].resp = cd->pw ? strdup(cd->pw) : NULL;
- if (!r[i].resp) {
- while (--i >= 0)
- free(r[i].resp);
- free(r);
- return PAM_BUF_ERR;
- }
- }
- r[i].resp_retcode = 0;
- }
- *resp = r;
- return PAM_SUCCESS;
- }
- static int validate_password(const char *user, const char *pw)
- {
- pam_handle_t *sub = NULL;
- struct conv_pw cd = { pw };
- struct pam_conv conv = { sub_conv, &cd };
- int ret;
- ret = pam_start("system-auth", user, &conv, &sub);
- if (ret != PAM_SUCCESS)
- return ret;
- pam_set_item(sub, PAM_AUTHTOK, pw);
- ret = pam_authenticate(sub, 0);
- pam_end(sub, ret);
- return ret;
- }
- /* ------------------------ fingerprint child ------------------------------ */
- static pid_t spawn_fp(void)
- {
- pid_t pid;
- char tbuf[16];
- pid = fork();
- if (pid < 0)
- return -1;
- if (pid == 0) {
- char *argv[2];
- int dn_r, dn_w;
- setsid();
- dn_r = open("/dev/null", O_RDONLY);
- dn_w = open("/dev/null", O_WRONLY);
- if (dn_r >= 0)
- dup2(dn_r, 0);
- if (dn_w >= 0) {
- dup2(dn_w, 1);
- dup2(dn_w, 2);
- }
- snprintf(tbuf, sizeof(tbuf), "%d", opt_timeout);
- setenv("FP_VERIFY_TIMEOUT", tbuf, 1);
- setenv("FP_VERIFY_QUIET", "1", 1);
- argv[0] = (char *)opt_cmd;
- argv[1] = NULL;
- execv(opt_cmd, argv);
- _exit(127);
- }
- return pid;
- }
- /* returns 1 if the child has exited (matched set), 0 if still running */
- static int child_done(pid_t pid, int *matched)
- {
- int st;
- pid_t r = waitpid(pid, &st, WNOHANG);
- if (r == 0)
- return 0;
- if (r < 0)
- return errno == EINTR ? 0 : 1;
- *matched = WIFEXITED(st) && WEXITSTATUS(st) == 0;
- return 1;
- }
- static void kill_fp(pid_t pid)
- {
- int st;
- if (pid <= 0)
- return;
- kill(-pid, SIGKILL); /* child called setsid(): kills whole tree */
- while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
- ;
- }
- /* ------------------------ tty helpers ------------------------------------ */
- static int read_line(int fd, char *buf, size_t len)
- {
- size_t off = 0;
- while (off + 1 < len) {
- ssize_t n = read(fd, buf + off, 1);
- if (n < 0) {
- if (errno == EINTR) {
- if (g_signal)
- return -2;
- continue;
- }
- return -1;
- }
- if (n == 0)
- return -2;
- if (buf[off] == '\n')
- break;
- off++;
- }
- buf[off] = '\0';
- if (off > 0 && buf[off - 1] == '\r')
- buf[off - 1] = '\0';
- return (int)strlen(buf);
- }
- static void tty_write(int fd, const char *s)
- {
- size_t off = 0, len = strlen(s);
- while (off < len) {
- ssize_t n = write(fd, s + off, len - off);
- if (n < 0) {
- if (errno == EINTR)
- continue;
- return;
- }
- off += (size_t)n;
- }
- }
- /* ------------------------ signal handling -------------------------------- */
- static void on_signal(int sig)
- {
- g_signal = sig;
- }
- /* ------------------------ module entry points ---------------------------- */
- static void parse_args(int argc, const char **argv)
- {
- int i;
- for (i = 0; i < argc; i++) {
- if (strcmp(argv[i], "debug") == 0)
- opt_debug = 1;
- else if (strncmp(argv[i], "command=", 8) == 0)
- opt_cmd = argv[i] + 8;
- else if (strncmp(argv[i], "finger_timeout=", 15) == 0)
- opt_timeout = atoi(argv[i] + 15);
- }
- }
- PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags,
- int argc, const char **argv)
- {
- const char *user = NULL;
- pid_t pid;
- int ttyfd, use_tty;
- int matched = 0, ret = PAM_AUTH_ERR;
- struct termios oldt, newt;
- struct sigaction sa, old_int, old_term;
- int have_termios = 0;
- (void)flags;
- parse_args(argc, argv);
- pam_get_user(pamh, &user, NULL);
- if (!user)
- user = "unknown";
- ttyfd = open("/dev/tty", O_RDWR | O_NOCTTY);
- if (ttyfd < 0 && isatty(STDIN_FILENO))
- ttyfd = STDIN_FILENO;
- use_tty = ttyfd >= 0 && isatty(ttyfd);
- pam_syslog(pamh, LOG_NOTICE, "pam_fp: start user=%s tty=%d timeout=%d",
- user, use_tty, opt_timeout);
- if (opt_debug)
- pam_syslog(pamh, LOG_DEBUG, "pam_fp: cmd=%s", opt_cmd);
- pid = spawn_fp();
- if (pid < 0) {
- pam_syslog(pamh, LOG_ERR, "pam_fp: cannot start fingerprint check");
- if (ttyfd >= 0 && ttyfd != STDIN_FILENO)
- close(ttyfd);
- return PAM_AUTHINFO_UNAVAIL;
- }
- if (!use_tty) {
- /* sequential: wait for the finger check only */
- int st;
- while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
- ;
- matched = WIFEXITED(st) && WEXITSTATUS(st) == 0;
- if (matched) {
- pam_syslog(pamh, LOG_NOTICE, "pam_fp: %s matched (sensor)", user);
- ret = PAM_SUCCESS;
- } else {
- pam_syslog(pamh, LOG_NOTICE, "pam_fp: %s no sensor match", user);
- ret = PAM_AUTH_ERR;
- }
- if (ttyfd >= 0 && ttyfd != STDIN_FILENO)
- close(ttyfd);
- return ret;
- }
- /* --- interactive: race password vs sensor --- */
- if (tcgetattr(ttyfd, &oldt) == 0) {
- newt = oldt;
- newt.c_lflag &= (tcflag_t)~ECHO;
- if (tcsetattr(ttyfd, TCSANOW, &newt) == 0)
- have_termios = 1;
- }
- memset(&sa, 0, sizeof(sa));
- sa.sa_handler = on_signal;
- sigemptyset(&sa.sa_mask);
- sigaction(SIGINT, &sa, &old_int);
- sigaction(SIGTERM, &sa, &old_term);
- tty_write(ttyfd, "Fingerprint: touch the sensor or type your password: ");
- while (!g_signal) {
- struct pollfd fds[1];
- int n, done;
- done = child_done(pid, &matched);
- if (done) {
- if (matched) {
- tty_write(ttyfd, "\nFingerprint matched.\n");
- tcflush(ttyfd, TCIFLUSH);
- ret = PAM_SUCCESS;
- } else {
- tty_write(ttyfd, "\nFingerprint not recognized.\n");
- ret = PAM_AUTH_ERR;
- }
- break;
- }
- fds[0].fd = ttyfd;
- fds[0].events = POLLIN;
- n = poll(fds, 1, POLL_MS);
- if (n < 0) {
- if (errno == EINTR)
- continue;
- break;
- }
- if (n > 0 && (fds[0].revents & POLLIN)) {
- char line[512];
- int len = read_line(ttyfd, line, sizeof(line));
- if (len == -2) { /* EOF/Ctrl-D: fall back */
- tty_write(ttyfd, "\n");
- ret = PAM_AUTH_ERR;
- break;
- }
- if (len <= 0) { /* just Enter: skip to password */
- tty_write(ttyfd, "\n");
- ret = PAM_AUTH_ERR;
- break;
- }
- if (validate_password(user, line) == PAM_SUCCESS) {
- tty_write(ttyfd, "\nPassword accepted.\n");
- ret = PAM_SUCCESS;
- } else {
- tty_write(ttyfd, "\nAuthentication failed.\n");
- ret = PAM_AUTH_ERR;
- }
- break;
- }
- }
- /* cleanup: always restore terminal and reap the child */
- if (have_termios)
- tcsetattr(ttyfd, TCSANOW, &oldt);
- kill_fp(pid);
- sigaction(SIGINT, &old_int, NULL);
- sigaction(SIGTERM, &old_term, NULL);
- if (ttyfd >= 0 && ttyfd != STDIN_FILENO)
- close(ttyfd);
- if (g_signal) {
- int sig = g_signal;
- pam_syslog(pamh, LOG_NOTICE, "pam_fp: %s interrupted", user);
- signal(sig, SIG_DFL);
- raise(sig);
- return PAM_AUTH_ERR; /* not reached in practice */
- }
- pam_syslog(pamh, LOG_NOTICE, "pam_fp: user=%s result=%s",
- user, ret == PAM_SUCCESS ? "success" : "fallback");
- return ret;
- }
- 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;
- }
|