44ceea7 ยท 1 month ago 7 commits
  1#define _POSIX_C_SOURCE 200809L
  2#include <pwd.h>
  3#include <security/pam_appl.h>
  4#include <stdbool.h>
  5#include <stdlib.h>
  6#include <string.h>
  7#include <unistd.h>
  8#include "comm.h"
  9#include "log.h"
 10#include "password-buffer.h"
 11#include "swaylock.h"
 12
 13static char *pw_buf = NULL;
 14
 15void initialize_pw_backend(int argc, char **argv) {
 16	if (getuid() != geteuid() || getgid() != getegid()) {
 17		swaylock_log(LOG_ERROR,
 18			"swaylock is setuid, but was compiled with the PAM"
 19			" backend. Run 'chmod a-s %s' to fix. Aborting.", argv[0]);
 20		exit(EXIT_FAILURE);
 21	}
 22	if (!spawn_comm_child()) {
 23		exit(EXIT_FAILURE);
 24	}
 25}
 26
 27static int handle_conversation(int num_msg, const struct pam_message **msg,
 28		struct pam_response **resp, void *data) {
 29	/* PAM expects an array of responses, one for each message */
 30	struct pam_response *pam_reply =
 31		calloc(num_msg, sizeof(struct pam_response));
 32	if (pam_reply == NULL) {
 33		swaylock_log(LOG_ERROR, "Allocation failed");
 34		return PAM_ABORT;
 35	}
 36	*resp = pam_reply;
 37	for (int i = 0; i < num_msg; ++i) {
 38		switch (msg[i]->msg_style) {
 39		case PAM_PROMPT_ECHO_OFF:
 40		case PAM_PROMPT_ECHO_ON:
 41			pam_reply[i].resp = strdup(pw_buf); // PAM clears and frees this
 42			if (pam_reply[i].resp == NULL) {
 43				swaylock_log(LOG_ERROR, "Allocation failed");
 44				return PAM_ABORT;
 45			}
 46			break;
 47		case PAM_ERROR_MSG:
 48		case PAM_TEXT_INFO:
 49			break;
 50		}
 51	}
 52	return PAM_SUCCESS;
 53}
 54
 55static const char *get_pam_auth_error(int pam_status) {
 56	switch (pam_status) {
 57	case PAM_AUTH_ERR:
 58		return "invalid credentials";
 59	case PAM_CRED_INSUFFICIENT:
 60		return "swaylock cannot authenticate users; check /etc/pam.d/swaylock "
 61			"has been installed properly";
 62	case PAM_AUTHINFO_UNAVAIL:
 63		return "authentication information unavailable";
 64	case PAM_MAXTRIES:
 65		return "maximum number of authentication tries exceeded";
 66	default:;
 67		static char msg[64];
 68		snprintf(msg, sizeof(msg), "unknown error (%d)", pam_status);
 69		return msg;
 70	}
 71}
 72
 73void run_pw_backend_child(void) {
 74	struct passwd *passwd = getpwuid(getuid());
 75	char *username = passwd->pw_name;
 76
 77	const struct pam_conv conv = {
 78		.conv = handle_conversation,
 79		.appdata_ptr = NULL,
 80	};
 81	pam_handle_t *auth_handle = NULL;
 82	if (pam_start("swaylock", username, &conv, &auth_handle) != PAM_SUCCESS) {
 83		swaylock_log(LOG_ERROR, "pam_start failed");
 84		exit(EXIT_FAILURE);
 85	}
 86
 87	/* This code does not run as root */
 88	swaylock_log(LOG_DEBUG, "Prepared to authorize user %s", username);
 89
 90	int pam_status = PAM_SUCCESS;
 91	while (1) {
 92		ssize_t size = read_comm_request(&pw_buf);
 93		if (size < 0) {
 94			exit(EXIT_FAILURE);
 95		} else if (size == 0) {
 96			break;
 97		}
 98
 99		int pam_status = pam_authenticate(auth_handle, 0);
100		password_buffer_destroy(pw_buf, size);
101		pw_buf = NULL;
102
103		bool success = pam_status == PAM_SUCCESS;
104		if (!success) {
105			swaylock_log(LOG_ERROR, "pam_authenticate failed: %s",
106				get_pam_auth_error(pam_status));
107		}
108
109		if (!write_comm_reply(success)) {
110			exit(EXIT_FAILURE);
111		}
112	}
113
114	pam_setcred(auth_handle, PAM_REFRESH_CRED);
115
116	if (pam_end(auth_handle, pam_status) != PAM_SUCCESS) {
117		swaylock_log(LOG_ERROR, "pam_end failed");
118		exit(EXIT_FAILURE);
119	}
120
121	exit((pam_status == PAM_SUCCESS) ? EXIT_SUCCESS : EXIT_FAILURE);
122}