2014-08-27 09:12:33 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2014-06-08 10:13:29 +04:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2014 Kevin Lange
|
2014-05-26 08:59:51 +04:00
|
|
|
*
|
|
|
|
* sudo
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
2014-08-27 09:12:33 +04:00
|
|
|
#include "lib/toaru_auth.h"
|
2014-05-26 08:59:51 +04:00
|
|
|
|
|
|
|
uint32_t child = 0;
|
|
|
|
|
|
|
|
void usage(int argc, char * argv[]) {
|
|
|
|
fprintf(stderr, "usage: %s [command]\n", argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
|
|
|
|
int fails = 0;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
usage(argc, argv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/*
|
|
|
|
* This is not very secure, but I'm lazy and just want this to exist.
|
|
|
|
* It's not like we have file system permissions or anything like
|
|
|
|
* that sitting around anyway... So, XXX: make this not dumb.
|
|
|
|
*/
|
|
|
|
char * username = getenv("USER");
|
|
|
|
char * password = malloc(sizeof(char) * 1024);
|
|
|
|
|
|
|
|
fprintf(stdout, "[%s] password for %s: ", argv[0], username);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* Disable echo */
|
|
|
|
struct termios old, new;
|
|
|
|
tcgetattr(fileno(stdin), &old);
|
|
|
|
new = old;
|
|
|
|
new.c_lflag &= (~ECHO);
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
|
|
|
|
|
|
|
|
fgets(password, 1024, stdin);
|
|
|
|
password[strlen(password)-1] = '\0';
|
|
|
|
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
2014-08-27 09:12:33 +04:00
|
|
|
int uid = toaru_auth_check_pass(username, password);
|
2014-05-26 08:59:51 +04:00
|
|
|
|
|
|
|
if (uid < 0) {
|
|
|
|
fails++;
|
|
|
|
if (fails == 3) {
|
2015-03-17 19:08:29 +03:00
|
|
|
fprintf(stderr, "%s: %d incorrect password attempts\n", argv[0], fails);
|
2014-05-26 08:59:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Sorry, try again.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char ** args = &argv[1];
|
|
|
|
int i = execvp(args[0], args);
|
|
|
|
|
|
|
|
/* XXX: There are other things that can cause an exec to fail. */
|
|
|
|
fprintf(stderr, "%s: %s: command not found\n", argv[0], args[0]);
|
2015-07-13 01:49:16 +03:00
|
|
|
return 1;
|
2014-05-26 08:59:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|