2021-07-13 21:33:12 +02:00
|
|
|
#include <pwd.h>
|
2021-07-13 22:39:28 +02:00
|
|
|
#include <err.h>
|
2021-07-13 21:33:12 +02:00
|
|
|
#include <shadow.h>
|
|
|
|
#include <crypt.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
2021-07-13 23:42:11 +02:00
|
|
|
#include <stdlib.h>
|
2021-07-13 21:33:12 +02:00
|
|
|
#include <string.h>
|
2021-07-13 22:23:27 +02:00
|
|
|
#include <bsd/readpassphrase.h>
|
2021-07-15 23:47:27 +02:00
|
|
|
#include "sessions.h"
|
2021-07-13 21:33:12 +02:00
|
|
|
|
2022-02-07 14:37:54 +01:00
|
|
|
#define VERSION "1.3"
|
2021-07-17 18:29:39 +02:00
|
|
|
|
2021-07-13 23:31:23 +02:00
|
|
|
void getconf(FILE* fp, const char* entry, char* result, size_t len_result) {
|
2021-07-13 23:21:34 +02:00
|
|
|
char* line = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
while (getline(&line, &len, fp) != -1) {
|
|
|
|
if (strncmp(entry, line, strlen(entry)) == 0) {
|
|
|
|
strtok(line, "=");
|
|
|
|
char* token = strtok(NULL, "=");
|
|
|
|
if (token) {
|
|
|
|
strncpy(result, token, len_result);
|
|
|
|
result[strcspn(result, "\n")] = 0;
|
2021-07-18 23:34:31 +02:00
|
|
|
free(line);
|
2021-07-13 23:31:23 +02:00
|
|
|
return;
|
2021-07-13 23:21:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 23:31:23 +02:00
|
|
|
errx(1, "Could not get '%s' entry in config", entry);
|
2021-07-13 23:21:34 +02:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:42:43 +01:00
|
|
|
void runprog(char** program_argv) {
|
2021-07-16 00:38:50 +02:00
|
|
|
if (setuid(0) < 0)
|
2021-07-14 06:12:50 +02:00
|
|
|
err(1, "Could not setuid");
|
2021-07-16 00:38:50 +02:00
|
|
|
if (setgid(0) < 0)
|
2021-07-14 06:12:50 +02:00
|
|
|
err(1, "Could not setgid");
|
2021-07-13 23:21:34 +02:00
|
|
|
|
2022-03-08 16:42:43 +01:00
|
|
|
// NOTE: this does not return when no error occurred.
|
|
|
|
execvp(program_argv[0], program_argv);
|
2021-07-15 16:17:54 +02:00
|
|
|
|
2022-03-08 16:42:43 +01:00
|
|
|
err(1, program_argv[0]);
|
2021-07-13 21:33:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2021-07-15 23:47:27 +02:00
|
|
|
char username[64], wrong_pw_sleep[64], session_ttl[64], password[128];
|
2022-03-08 17:03:14 +01:00
|
|
|
unsigned int sleep_us, tries, ts_ttl;
|
2021-07-13 23:42:11 +02:00
|
|
|
|
2021-07-17 18:29:39 +02:00
|
|
|
if (argc == 1) {
|
|
|
|
printf("RootDO version: %s\n\n", VERSION);
|
|
|
|
printf("Usage: %s [command]\n", argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-13 21:33:12 +02:00
|
|
|
|
2022-02-09 20:17:45 +01:00
|
|
|
if (geteuid() != 0)
|
|
|
|
errx(1, "The rdo binary needs to be installed as SUID.");
|
|
|
|
|
2021-07-13 21:33:12 +02:00
|
|
|
int ruid = getuid();
|
2021-07-15 16:17:54 +02:00
|
|
|
if (ruid == 0)
|
2022-03-08 16:42:43 +01:00
|
|
|
runprog(&argv[1]);
|
2021-07-15 12:46:27 +02:00
|
|
|
|
2021-07-15 14:59:24 +02:00
|
|
|
FILE* fp = fopen("/etc/rdo.conf", "r");
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
err(1, "Could not open /etc/rdo.conf");
|
|
|
|
|
2021-07-13 23:21:34 +02:00
|
|
|
getconf(fp, "username", username, sizeof(username));
|
2021-07-13 23:42:11 +02:00
|
|
|
getconf(fp, "wrong_pw_sleep", wrong_pw_sleep, sizeof(wrong_pw_sleep));
|
2021-07-15 23:47:27 +02:00
|
|
|
getconf(fp, "session_ttl", session_ttl, sizeof(session_ttl));
|
2022-03-08 17:03:14 +01:00
|
|
|
sleep_us = atoi(wrong_pw_sleep) * 1000;
|
2021-07-16 00:15:04 +02:00
|
|
|
ts_ttl = atoi(session_ttl) * 60;
|
2021-07-13 21:33:12 +02:00
|
|
|
|
2021-07-15 14:59:24 +02:00
|
|
|
fclose(fp);
|
|
|
|
|
2021-07-15 23:47:27 +02:00
|
|
|
if (getsession(getppid(), ts_ttl) == 0)
|
2022-03-08 16:42:43 +01:00
|
|
|
runprog(&argv[1]);
|
2021-07-15 23:47:27 +02:00
|
|
|
|
2021-07-13 21:33:12 +02:00
|
|
|
struct passwd* p = getpwnam(username);
|
2022-02-09 20:06:28 +01:00
|
|
|
if (!p) {
|
|
|
|
if (errno == 0)
|
|
|
|
errx(1, "The user specified in the config file does not exist.");
|
|
|
|
else
|
|
|
|
err(1, "Could not get user info");
|
|
|
|
}
|
2021-07-13 21:33:12 +02:00
|
|
|
|
|
|
|
int uid = p->pw_uid;
|
2022-03-08 16:59:45 +01:00
|
|
|
if (uid != ruid)
|
2022-02-09 20:06:28 +01:00
|
|
|
errx(1, "You are not allowed to execute rdo.");
|
2021-07-13 21:33:12 +02:00
|
|
|
|
2022-02-08 21:09:36 +01:00
|
|
|
struct spwd* shadowEntry = getspnam(p->pw_name);
|
2021-07-13 21:33:12 +02:00
|
|
|
|
2022-02-08 21:09:36 +01:00
|
|
|
if (!shadowEntry || !shadowEntry->sp_pwdp)
|
2021-07-13 22:39:28 +02:00
|
|
|
err(1, "Could not get shadow entry");
|
2021-07-13 21:33:12 +02:00
|
|
|
|
2021-07-15 16:17:54 +02:00
|
|
|
tries = 0;
|
2021-07-13 21:33:12 +02:00
|
|
|
while (tries < 3) {
|
2021-07-13 22:39:28 +02:00
|
|
|
if (!readpassphrase("(rdo) Password: ", password, sizeof(password), RPP_REQUIRE_TTY))
|
|
|
|
err(1, "Could not get passphrase");
|
2021-07-13 22:23:27 +02:00
|
|
|
|
2022-02-08 21:09:36 +01:00
|
|
|
char* hashed_pw = crypt(password, shadowEntry->sp_pwdp);
|
2022-02-07 14:37:54 +01:00
|
|
|
memset(password, 0, sizeof(password));
|
2022-02-08 21:09:36 +01:00
|
|
|
|
|
|
|
if (!hashed_pw)
|
2022-02-12 08:07:48 +01:00
|
|
|
errx(1, "Could not hash password, does your user have a password?");
|
2022-02-07 14:37:54 +01:00
|
|
|
|
2022-02-08 21:09:36 +01:00
|
|
|
if (strcmp(shadowEntry->sp_pwdp, hashed_pw) == 0) {
|
2021-07-15 23:47:27 +02:00
|
|
|
setsession(getppid(), ts_ttl);
|
2022-03-08 16:42:43 +01:00
|
|
|
runprog(&argv[1]);
|
2021-07-15 23:47:27 +02:00
|
|
|
}
|
2021-07-15 12:46:27 +02:00
|
|
|
|
2022-03-08 17:03:14 +01:00
|
|
|
usleep(sleep_us);
|
2021-07-13 21:33:12 +02:00
|
|
|
fprintf(stderr, "Wrong password.\n");
|
2021-07-13 22:14:46 +02:00
|
|
|
tries++;
|
2021-07-13 21:33:12 +02:00
|
|
|
}
|
2021-07-13 22:39:28 +02:00
|
|
|
errx(1, "Too many wrong password attempts.");
|
|
|
|
return 1;
|
2021-07-13 21:33:12 +02:00
|
|
|
}
|