From 604f5e7fdd98d11cd7fde9f082ccc20f26c86659 Mon Sep 17 00:00:00 2001 From: sw1tchbl4d3 Date: Tue, 13 Jul 2021 22:39:28 +0200 Subject: [PATCH] Improve error messages This commit makes rdo make use of the err.h header for more pleasant and convenient error logging. --- rdo.c | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/rdo.c b/rdo.c index 42467d4..b752146 100644 --- a/rdo.c +++ b/rdo.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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; }