2018-11-25 05:12:48 -06:00
|
|
|
#define _XOPEN_SOURCE // for crypt
|
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"
|
|
|
|
#include "swaylock.h"
|
2018-09-28 05:18:54 -05:00
|
|
|
|
2019-01-18 07:52:17 -06:00
|
|
|
void initialize_pw_backend(int argc, char **argv) {
|
|
|
|
if (geteuid() != 0) {
|
|
|
|
swaylock_log(LOG_ERROR,
|
|
|
|
"swaylock needs to be setuid to read /etc/shadow");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!spawn_comm_child()) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setgid(getgid()) != 0) {
|
|
|
|
swaylock_log_errno(LOG_ERROR, "Unable to drop root");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (setuid(getuid()) != 0) {
|
|
|
|
swaylock_log_errno(LOG_ERROR, "Unable to drop root");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (setuid(0) != -1) {
|
|
|
|
swaylock_log_errno(LOG_ERROR, "Unable to drop root (we shouldn't be "
|
|
|
|
"able to restore it after setuid)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 16:35:14 -06:00
|
|
|
void run_pw_backend_child(void) {
|
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);
|
|
|
|
}
|
|
|
|
char *encpw = pwent->pw_passwd;
|
|
|
|
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
|
|
|
|
|
|
|
/* We don't need any additional logging here because the parent process will
|
|
|
|
* also fail here and will handle logging for us. */
|
|
|
|
if (setgid(getgid()) != 0) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (setuid(getuid()) != 0) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-01-16 15:33:14 -06:00
|
|
|
if (setuid(0) != -1) {
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2018-09-28 05:18:54 -05:00
|
|
|
char *c = crypt(buf, encpw);
|
|
|
|
if (c == NULL) {
|
2019-01-16 16:35:14 -06:00
|
|
|
swaylock_log_errno(LOG_ERROR, "crypt failed");
|
|
|
|
clear_buffer(buf, size);
|
|
|
|
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-10-06 11:17:36 -05:00
|
|
|
clear_buffer(buf, size);
|
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
|
|
|
// We don't want to keep it in memory longer than necessary,
|
|
|
|
// so clear *before* sleeping.
|
2018-10-06 11:17:36 -05:00
|
|
|
clear_buffer(buf, size);
|
2018-09-28 05:18:54 -05:00
|
|
|
free(buf);
|
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);
|
2019-01-18 07:52:17 -06:00
|
|
|
}
|