pam_fp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * pam_fp.so - fingerprint OR password, whichever comes first.
  3. *
  4. * Terminal case (sudo):
  5. * - starts the fingerprint check (fp-pam.sh) in the background
  6. * - immediately prints "Touch the sensor or type your password:"
  7. * - waits for EITHER:
  8. * * sensor match -> PAM_SUCCESS
  9. * * submitted password -> validated via system-auth (pam_unix,
  10. * faillock, etc.) -> PAM_SUCCESS / fallback
  11. * * empty line (Enter) -> fallback to the normal password prompt
  12. * * finger check gives up -> fallback
  13. * - terminal echo is disabled while waiting and always restored
  14. *
  15. * Non-terminal (polkit GUI): sequential fingerprint check, then fallback.
  16. *
  17. * Module args: debug | command=/path | finger_timeout=SECONDS
  18. * Build: gcc -shared -fPIC -O2 -Wall -o pam_fp.so pam_fp.c -lpam
  19. */
  20. #define _GNU_SOURCE
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <poll.h>
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <syslog.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <termios.h>
  32. #include <unistd.h>
  33. #include <security/pam_appl.h>
  34. #include <security/pam_ext.h>
  35. #include <security/pam_modules.h>
  36. #define FP_CMD_DEFAULT "/home/nebulon/projects/fingerprint/work/fp-pam.sh"
  37. #define FP_TIMEOUT_DEFAULT 45
  38. #define POLL_MS 200
  39. static int opt_debug;
  40. static const char *opt_cmd = FP_CMD_DEFAULT;
  41. static int opt_timeout = FP_TIMEOUT_DEFAULT;
  42. static volatile sig_atomic_t g_signal;
  43. /* ------------------------ password validation (recursive PAM) ------------- */
  44. struct conv_pw { const char *pw; };
  45. static int sub_conv(int n, const struct pam_message **msg,
  46. struct pam_response **resp, void *data)
  47. {
  48. struct conv_pw *cd = data;
  49. struct pam_response *r;
  50. int i;
  51. if (n <= 0 || !resp)
  52. return PAM_CONV_ERR;
  53. r = calloc((size_t)n, sizeof(*r));
  54. if (!r)
  55. return PAM_BUF_ERR;
  56. for (i = 0; i < n; i++) {
  57. if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF ||
  58. msg[i]->msg_style == PAM_PROMPT_ECHO_ON) {
  59. r[i].resp = cd->pw ? strdup(cd->pw) : NULL;
  60. if (!r[i].resp) {
  61. while (--i >= 0)
  62. free(r[i].resp);
  63. free(r);
  64. return PAM_BUF_ERR;
  65. }
  66. }
  67. r[i].resp_retcode = 0;
  68. }
  69. *resp = r;
  70. return PAM_SUCCESS;
  71. }
  72. static int validate_password(const char *user, const char *pw)
  73. {
  74. pam_handle_t *sub = NULL;
  75. struct conv_pw cd = { pw };
  76. struct pam_conv conv = { sub_conv, &cd };
  77. int ret;
  78. ret = pam_start("system-auth", user, &conv, &sub);
  79. if (ret != PAM_SUCCESS)
  80. return ret;
  81. pam_set_item(sub, PAM_AUTHTOK, pw);
  82. ret = pam_authenticate(sub, 0);
  83. pam_end(sub, ret);
  84. return ret;
  85. }
  86. /* ------------------------ fingerprint child ------------------------------ */
  87. static pid_t spawn_fp(void)
  88. {
  89. pid_t pid;
  90. char tbuf[16];
  91. pid = fork();
  92. if (pid < 0)
  93. return -1;
  94. if (pid == 0) {
  95. char *argv[2];
  96. int dn_r, dn_w;
  97. setsid();
  98. dn_r = open("/dev/null", O_RDONLY);
  99. dn_w = open("/dev/null", O_WRONLY);
  100. if (dn_r >= 0)
  101. dup2(dn_r, 0);
  102. if (dn_w >= 0) {
  103. dup2(dn_w, 1);
  104. dup2(dn_w, 2);
  105. }
  106. snprintf(tbuf, sizeof(tbuf), "%d", opt_timeout);
  107. setenv("FP_VERIFY_TIMEOUT", tbuf, 1);
  108. setenv("FP_VERIFY_QUIET", "1", 1);
  109. argv[0] = (char *)opt_cmd;
  110. argv[1] = NULL;
  111. execv(opt_cmd, argv);
  112. _exit(127);
  113. }
  114. return pid;
  115. }
  116. /* returns 1 if the child has exited (matched set), 0 if still running */
  117. static int child_done(pid_t pid, int *matched)
  118. {
  119. int st;
  120. pid_t r = waitpid(pid, &st, WNOHANG);
  121. if (r == 0)
  122. return 0;
  123. if (r < 0)
  124. return errno == EINTR ? 0 : 1;
  125. *matched = WIFEXITED(st) && WEXITSTATUS(st) == 0;
  126. return 1;
  127. }
  128. static void kill_fp(pid_t pid)
  129. {
  130. int st;
  131. if (pid <= 0)
  132. return;
  133. kill(-pid, SIGKILL); /* child called setsid(): kills whole tree */
  134. while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
  135. ;
  136. }
  137. /* ------------------------ tty helpers ------------------------------------ */
  138. static int read_line(int fd, char *buf, size_t len)
  139. {
  140. size_t off = 0;
  141. while (off + 1 < len) {
  142. ssize_t n = read(fd, buf + off, 1);
  143. if (n < 0) {
  144. if (errno == EINTR) {
  145. if (g_signal)
  146. return -2;
  147. continue;
  148. }
  149. return -1;
  150. }
  151. if (n == 0)
  152. return -2;
  153. if (buf[off] == '\n')
  154. break;
  155. off++;
  156. }
  157. buf[off] = '\0';
  158. if (off > 0 && buf[off - 1] == '\r')
  159. buf[off - 1] = '\0';
  160. return (int)strlen(buf);
  161. }
  162. static void tty_write(int fd, const char *s)
  163. {
  164. size_t off = 0, len = strlen(s);
  165. while (off < len) {
  166. ssize_t n = write(fd, s + off, len - off);
  167. if (n < 0) {
  168. if (errno == EINTR)
  169. continue;
  170. return;
  171. }
  172. off += (size_t)n;
  173. }
  174. }
  175. /* ------------------------ signal handling -------------------------------- */
  176. static void on_signal(int sig)
  177. {
  178. g_signal = sig;
  179. }
  180. /* ------------------------ module entry points ---------------------------- */
  181. static void parse_args(int argc, const char **argv)
  182. {
  183. int i;
  184. for (i = 0; i < argc; i++) {
  185. if (strcmp(argv[i], "debug") == 0)
  186. opt_debug = 1;
  187. else if (strncmp(argv[i], "command=", 8) == 0)
  188. opt_cmd = argv[i] + 8;
  189. else if (strncmp(argv[i], "finger_timeout=", 15) == 0)
  190. opt_timeout = atoi(argv[i] + 15);
  191. }
  192. }
  193. PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags,
  194. int argc, const char **argv)
  195. {
  196. const char *user = NULL;
  197. pid_t pid;
  198. int ttyfd, use_tty;
  199. int matched = 0, ret = PAM_AUTH_ERR;
  200. struct termios oldt, newt;
  201. struct sigaction sa, old_int, old_term;
  202. int have_termios = 0;
  203. (void)flags;
  204. parse_args(argc, argv);
  205. pam_get_user(pamh, &user, NULL);
  206. if (!user)
  207. user = "unknown";
  208. ttyfd = open("/dev/tty", O_RDWR | O_NOCTTY);
  209. if (ttyfd < 0 && isatty(STDIN_FILENO))
  210. ttyfd = STDIN_FILENO;
  211. use_tty = ttyfd >= 0 && isatty(ttyfd);
  212. pam_syslog(pamh, LOG_NOTICE, "pam_fp: start user=%s tty=%d timeout=%d",
  213. user, use_tty, opt_timeout);
  214. if (opt_debug)
  215. pam_syslog(pamh, LOG_DEBUG, "pam_fp: cmd=%s", opt_cmd);
  216. pid = spawn_fp();
  217. if (pid < 0) {
  218. pam_syslog(pamh, LOG_ERR, "pam_fp: cannot start fingerprint check");
  219. if (ttyfd >= 0 && ttyfd != STDIN_FILENO)
  220. close(ttyfd);
  221. return PAM_AUTHINFO_UNAVAIL;
  222. }
  223. if (!use_tty) {
  224. /* sequential: wait for the finger check only */
  225. int st;
  226. while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
  227. ;
  228. matched = WIFEXITED(st) && WEXITSTATUS(st) == 0;
  229. if (matched) {
  230. pam_syslog(pamh, LOG_NOTICE, "pam_fp: %s matched (sensor)", user);
  231. ret = PAM_SUCCESS;
  232. } else {
  233. pam_syslog(pamh, LOG_NOTICE, "pam_fp: %s no sensor match", user);
  234. ret = PAM_AUTH_ERR;
  235. }
  236. if (ttyfd >= 0 && ttyfd != STDIN_FILENO)
  237. close(ttyfd);
  238. return ret;
  239. }
  240. /* --- interactive: race password vs sensor --- */
  241. if (tcgetattr(ttyfd, &oldt) == 0) {
  242. newt = oldt;
  243. newt.c_lflag &= (tcflag_t)~ECHO;
  244. if (tcsetattr(ttyfd, TCSANOW, &newt) == 0)
  245. have_termios = 1;
  246. }
  247. memset(&sa, 0, sizeof(sa));
  248. sa.sa_handler = on_signal;
  249. sigemptyset(&sa.sa_mask);
  250. sigaction(SIGINT, &sa, &old_int);
  251. sigaction(SIGTERM, &sa, &old_term);
  252. tty_write(ttyfd, "Fingerprint: touch the sensor or type your password: ");
  253. while (!g_signal) {
  254. struct pollfd fds[1];
  255. int n, done;
  256. done = child_done(pid, &matched);
  257. if (done) {
  258. if (matched) {
  259. tty_write(ttyfd, "\nFingerprint matched.\n");
  260. tcflush(ttyfd, TCIFLUSH);
  261. ret = PAM_SUCCESS;
  262. } else {
  263. tty_write(ttyfd, "\nFingerprint not recognized.\n");
  264. ret = PAM_AUTH_ERR;
  265. }
  266. break;
  267. }
  268. fds[0].fd = ttyfd;
  269. fds[0].events = POLLIN;
  270. n = poll(fds, 1, POLL_MS);
  271. if (n < 0) {
  272. if (errno == EINTR)
  273. continue;
  274. break;
  275. }
  276. if (n > 0 && (fds[0].revents & POLLIN)) {
  277. char line[512];
  278. int len = read_line(ttyfd, line, sizeof(line));
  279. if (len == -2) { /* EOF/Ctrl-D: fall back */
  280. tty_write(ttyfd, "\n");
  281. ret = PAM_AUTH_ERR;
  282. break;
  283. }
  284. if (len <= 0) { /* just Enter: skip to password */
  285. tty_write(ttyfd, "\n");
  286. ret = PAM_AUTH_ERR;
  287. break;
  288. }
  289. if (validate_password(user, line) == PAM_SUCCESS) {
  290. tty_write(ttyfd, "\nPassword accepted.\n");
  291. ret = PAM_SUCCESS;
  292. } else {
  293. tty_write(ttyfd, "\nAuthentication failed.\n");
  294. ret = PAM_AUTH_ERR;
  295. }
  296. break;
  297. }
  298. }
  299. /* cleanup: always restore terminal and reap the child */
  300. if (have_termios)
  301. tcsetattr(ttyfd, TCSANOW, &oldt);
  302. kill_fp(pid);
  303. sigaction(SIGINT, &old_int, NULL);
  304. sigaction(SIGTERM, &old_term, NULL);
  305. if (ttyfd >= 0 && ttyfd != STDIN_FILENO)
  306. close(ttyfd);
  307. if (g_signal) {
  308. int sig = g_signal;
  309. pam_syslog(pamh, LOG_NOTICE, "pam_fp: %s interrupted", user);
  310. signal(sig, SIG_DFL);
  311. raise(sig);
  312. return PAM_AUTH_ERR; /* not reached in practice */
  313. }
  314. pam_syslog(pamh, LOG_NOTICE, "pam_fp: user=%s result=%s",
  315. user, ret == PAM_SUCCESS ? "success" : "fallback");
  316. return ret;
  317. }
  318. PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
  319. const char **argv)
  320. {
  321. (void)pamh;
  322. (void)flags;
  323. (void)argc;
  324. (void)argv;
  325. return PAM_SUCCESS;
  326. }