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