1
0
Fork 0
forked from soccera/rdo

Improve error messages

This commit makes rdo make use of the err.h header
for more pleasant and convenient error logging.
This commit is contained in:
sw1tchbl4d3 2021-07-13 22:39:28 +02:00
parent 336221e5e2
commit 604f5e7fdd

43
rdo.c
View file

@ -1,4 +1,5 @@
#include <pwd.h>
#include <err.h>
#include <shadow.h>
#include <crypt.h>
#include <unistd.h>
@ -11,21 +12,18 @@ void runprog(int argc, char** argv) {
argv[i] = argv[i + 1];
setuid(0);
setgid(0);
if (execvp(argv[0], argv) != 0) perror(argv[0]);
if (execvp(argv[0], argv) != 0)
perror(argv[0]);
}
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "Please specify a program to run.\n");
return -1;
}
if (argc < 2)
errx(1, "Please specify a program to run");
FILE* fp = fopen("/etc/rdo/username", "r");
if (fp == NULL) {
fprintf(stderr, "The file /etc/rdo/username could not be read.\n");
return -2;
}
if (!fp)
err(1, "Could not open /etc/rdo/username");
int ruid = getuid();
if (ruid == 0) {
@ -40,31 +38,23 @@ int main(int argc, char** argv) {
username[strcspn(username, "\n")] = 0;
struct passwd* p = getpwnam(username);
if (!p) {
fprintf(stderr, "The user in the username file does not exist.\n");
return -3;
}
if (!p)
err(1, "Could not get user info");
int uid = p->pw_uid;
if (uid != ruid && ruid != 0) {
fprintf(stderr, "You are not in the username file.\n");
return -4;
}
if (uid != ruid && ruid != 0)
errx(1, "You are not in the username file");
struct spwd* shadowEntry = getspnam(username);
if (!shadowEntry) {
fprintf(stderr, "Could not get shadow entry, suid bit not set or username invalid?\n");
return -5;
}
if (!shadowEntry)
err(1, "Could not get shadow entry");
int tries = 0;
char password[128];
while (tries < 3) {
if (!readpassphrase("(rdo) Password: ", password, sizeof(password), RPP_REQUIRE_TTY)) {
fprintf(stderr, "Could not get passphrase, not in a TTY?\n");
return -7;
}
if (!readpassphrase("(rdo) Password: ", password, sizeof(password), RPP_REQUIRE_TTY))
err(1, "Could not get passphrase");
if (strcmp(shadowEntry->sp_pwdp, crypt(password, shadowEntry->sp_pwdp)) == 0) {
runprog(argc, argv);
@ -73,5 +63,6 @@ int main(int argc, char** argv) {
fprintf(stderr, "Wrong password.\n");
tries++;
}
return -6;
errx(1, "Too many wrong password attempts.");
return 1;
}