/* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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; }