2018-11-25 05:12:48 -06:00
|
|
|
#define _XOPEN_SOURCE // for crypt
|
2024-01-16 05:25:01 -06:00
|
|
|
#include <assert.h>
|
2018-09-28 05:18:54 -05:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <shadow.h>
|
2019-01-23 14:16:44 -06:00
|
|
|
#include <stdlib.h>
|
2018-09-28 05:18:54 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2018-10-06 11:17:36 -05:00
|
|
|
#ifdef __GLIBC__
|
|
|
|
// GNU, you damn slimy bastard
|
|
|
|
#include <crypt.h>
|
|
|
|
#endif
|
2019-01-16 16:35:14 -06:00
|
|
|
#include "comm.h"
|
|
|
|
#include "log.h"
|
2022-05-30 04:50:30 -05:00
|
|
|
#include "password-buffer.h"
|
2019-01-16 16:35:14 -06:00
|
|
|
#include "swaylock.h"
|
2018-09-28 05:18:54 -05:00
|
|
|
|
2024-01-16 05:25:01 -06:00
|
|
|
char *encpw = NULL;
|
2019-01-18 07:52:17 -06:00
|
|
|
|
2024-01-16 05:25:01 -06:00
|
|
|
void initialize_pw_backend(int argc, char **argv) {
|
2018-09-28 05:18:54 -05:00
|
|
|
/* This code runs as root */
|
|
|
|
struct passwd *pwent = getpwuid(getuid());
|
|
|
|
if (!pwent) {
|
2019-01-14 22:30:54 -06:00
|
|
|
swaylock_log_errno(LOG_ERROR, "failed to getpwuid");
|
2018-09-28 05:18:54 -05:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2024-01-16 05:25:01 -06:00
|
|
|
encpw = pwent->pw_passwd;
|
2018-09-28 05:18:54 -05:00
|
|
|
if (strcmp(encpw, "x") == 0) {
|
|
|
|
struct spwd *swent = getspnam(pwent->pw_name);
|
|
|
|
if (!swent) {
|
2019-01-14 22:30:54 -06:00
|
|
|
swaylock_log_errno(LOG_ERROR, "failed to getspnam");
|
2018-09-28 05:18:54 -05:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
encpw = swent->sp_pwdp;
|
|
|
|
}
|
2018-10-06 11:17:36 -05:00
|
|
|
|
|
|
|
if (setgid(getgid()) != 0) {
|
2024-01-16 05:25:01 -06:00
|
|
|
swaylock_log_errno(LOG_ERROR, "Unable to drop root");
|
2018-10-06 11:17:36 -05:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (setuid(getuid()) != 0) {
|
2024-01-16 05:25:01 -06:00
|
|
|
swaylock_log_errno(LOG_ERROR, "Unable to drop root");
|
2018-10-06 11:17:36 -05:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2024-01-16 05:25:01 -06:00
|
|
|
if (setuid(0) != -1 || setgid(0) != -1) {
|
|
|
|
swaylock_log_errno(LOG_ERROR, "Unable to drop root (we shouldn't be "
|
|
|
|
"able to restore it after setuid/setgid)");
|
2019-01-16 15:33:14 -06:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-10-06 11:17:36 -05:00
|
|
|
|
|
|
|
/* This code does not run as root */
|
2019-01-16 16:35:14 -06:00
|
|
|
swaylock_log(LOG_DEBUG, "Prepared to authorize user %s", pwent->pw_name);
|
2018-09-28 05:18:54 -05:00
|
|
|
|
2024-01-16 05:25:01 -06:00
|
|
|
if (!spawn_comm_child()) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Buffer is only used by the child */
|
|
|
|
clear_buffer(encpw, strlen(encpw));
|
|
|
|
encpw = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_pw_backend_child(void) {
|
|
|
|
assert(encpw != NULL);
|
2018-09-28 05:18:54 -05:00
|
|
|
while (1) {
|
2019-01-16 16:35:14 -06:00
|
|
|
char *buf;
|
|
|
|
ssize_t size = read_comm_request(&buf);
|
|
|
|
if (size < 0) {
|
2018-09-28 05:18:54 -05:00
|
|
|
exit(EXIT_FAILURE);
|
2019-01-16 16:35:14 -06:00
|
|
|
} else if (size == 0) {
|
|
|
|
break;
|
2018-09-28 05:18:54 -05:00
|
|
|
}
|
2019-01-16 16:35:14 -06:00
|
|
|
|
2022-09-26 12:58:04 -05:00
|
|
|
const char *c = crypt(buf, encpw);
|
2022-05-30 04:50:30 -05:00
|
|
|
password_buffer_destroy(buf, size);
|
|
|
|
buf = NULL;
|
|
|
|
|
2018-09-28 05:18:54 -05:00
|
|
|
if (c == NULL) {
|
2019-01-16 16:35:14 -06:00
|
|
|
swaylock_log_errno(LOG_ERROR, "crypt failed");
|
|
|
|
exit(EXIT_FAILURE);
|
2018-09-28 05:18:54 -05:00
|
|
|
}
|
2019-01-16 16:35:14 -06:00
|
|
|
bool success = strcmp(c, encpw) == 0;
|
|
|
|
|
|
|
|
if (!write_comm_reply(success)) {
|
2018-09-28 05:18:54 -05:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-01-16 16:35:14 -06:00
|
|
|
|
2019-01-27 12:12:54 -06:00
|
|
|
sleep(2);
|
2018-09-28 05:18:54 -05:00
|
|
|
}
|
2018-10-06 11:17:36 -05:00
|
|
|
|
|
|
|
clear_buffer(encpw, strlen(encpw));
|
2018-09-28 05:18:54 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
2022-09-26 12:58:04 -05:00
|
|
|
}
|