gsudo: act like regular sudo
This commit is contained in:
parent
d63c98b6bf
commit
004c792e91
167
apps/gsudo.c
167
apps/gsudo.c
@ -21,114 +21,101 @@
|
||||
#include <toaru/graphics.h>
|
||||
#include <toaru/sdf.h>
|
||||
|
||||
#define main __main_unused
|
||||
#include "sudo.c"
|
||||
#undef main
|
||||
|
||||
#define FONT_SIZE_MAIN 20
|
||||
#define FONT_SIZE_PASSWD 25
|
||||
|
||||
uint32_t child = 0;
|
||||
static yutani_t * yctx;
|
||||
static gfx_context_t * ctx;
|
||||
static yutani_window_t * window;
|
||||
|
||||
static int graphical_callback(char * username, char * password, int fails, char * argv[]) {
|
||||
int i = 0;
|
||||
while (1) {
|
||||
draw_fill(ctx, rgba(0,0,0,200));
|
||||
int h = ctx->height-1;
|
||||
int w = ctx->width-1;
|
||||
draw_line(ctx, 0,0,0,h, rgb(255,0,0));
|
||||
draw_line(ctx, w,w,0,h, rgb(255,0,0));
|
||||
draw_line(ctx, 0,w,0,0, rgb(255,0,0));
|
||||
draw_line(ctx, 0,w,h,h, rgb(255,0,0));
|
||||
|
||||
char prompt_message[512];
|
||||
sprintf(prompt_message, "Enter password for '%s'", username);
|
||||
draw_sdf_string(ctx, (ctx->width - draw_sdf_string_width(prompt_message, FONT_SIZE_MAIN, SDF_FONT_THIN)) / 2, 20, prompt_message, FONT_SIZE_MAIN, rgb(255, 255, 255), SDF_FONT_THIN);
|
||||
|
||||
sprintf(prompt_message, "requested by %s", argv[1]);
|
||||
draw_sdf_string(ctx, (ctx->width - draw_sdf_string_width(prompt_message, FONT_SIZE_MAIN, SDF_FONT_THIN)) / 2, 150, prompt_message, FONT_SIZE_MAIN, rgb(255, 255, 255), SDF_FONT_THIN);
|
||||
|
||||
if (fails) {
|
||||
sprintf(prompt_message, "Try again. %d failures.", fails);
|
||||
draw_sdf_string(ctx, (ctx->width - draw_sdf_string_width(prompt_message, FONT_SIZE_MAIN, SDF_FONT_THIN)) / 2, 50, prompt_message, FONT_SIZE_MAIN, rgb(255, 0, 0), SDF_FONT_THIN);
|
||||
|
||||
}
|
||||
|
||||
char password_circles[512] = {0};;
|
||||
strcpy(password_circles, "");
|
||||
for (unsigned int i = 0; i < strlen(password) && i < 512/4; ++i) {
|
||||
strcat(password_circles, "\007");
|
||||
}
|
||||
draw_sdf_string(ctx, (ctx->width - draw_sdf_string_width(password_circles, FONT_SIZE_PASSWD, SDF_FONT_THIN)) / 2, 80, password_circles, FONT_SIZE_PASSWD, rgb(255, 255, 255), SDF_FONT_THIN);
|
||||
|
||||
flip(ctx);
|
||||
yutani_flip(yctx, window);
|
||||
|
||||
yutani_msg_t * msg = yutani_poll(yctx);
|
||||
|
||||
switch (msg->type) {
|
||||
case YUTANI_MSG_KEY_EVENT:
|
||||
{
|
||||
struct yutani_msg_key_event * ke = (void*)msg->data;
|
||||
if (ke->event.action == KEY_ACTION_DOWN) {
|
||||
if (ke->event.keycode == KEY_ESCAPE) {
|
||||
return 1;
|
||||
}
|
||||
if (ke->event.keycode == '\n') {
|
||||
return 0;
|
||||
} else if (ke->event.key == 8) {
|
||||
if (i > 0) i--;
|
||||
password[i] = '\0';
|
||||
} else if (ke->event.key) {
|
||||
password[i] = ke->event.key;
|
||||
password[i+1] = '\0';
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
int fails = 0;
|
||||
|
||||
if (argc < 2) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
yctx = yutani_init();
|
||||
|
||||
if (!yctx) {
|
||||
fprintf(stderr, "%s: could not connect to compositor\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int width = 300;
|
||||
int height = 200;
|
||||
|
||||
int uid;
|
||||
int error = 0;
|
||||
|
||||
yutani_t * yctx = yutani_init();
|
||||
|
||||
int left = (yctx->display_width - width) / 2;
|
||||
int top = (yctx->display_height - height) / 2;
|
||||
|
||||
yutani_window_t * window = yutani_window_create(yctx, width, height);
|
||||
window = yutani_window_create(yctx, width, height);
|
||||
yutani_window_move(yctx, window, left, top);
|
||||
|
||||
gfx_context_t * ctx = init_graphics_yutani_double_buffer(window);
|
||||
ctx = init_graphics_yutani_double_buffer(window);
|
||||
|
||||
while (1) {
|
||||
char * username = getenv("USER");
|
||||
char * password = calloc(sizeof(char) * 1024,1);
|
||||
int i = 0;
|
||||
|
||||
while (1) {
|
||||
draw_fill(ctx, rgba(0,0,0,200));
|
||||
int h = height-1;
|
||||
int w = width-1;
|
||||
draw_line(ctx, 0,0,0,h, rgb(255,0,0));
|
||||
draw_line(ctx, w,w,0,h, rgb(255,0,0));
|
||||
draw_line(ctx, 0,w,0,0, rgb(255,0,0));
|
||||
draw_line(ctx, 0,w,h,h, rgb(255,0,0));
|
||||
|
||||
char prompt_message[512];
|
||||
sprintf(prompt_message, "Enter password for '%s'", username);
|
||||
draw_sdf_string(ctx, (width - draw_sdf_string_width(prompt_message, FONT_SIZE_MAIN, SDF_FONT_THIN)) / 2, 20, prompt_message, FONT_SIZE_MAIN, rgb(255, 255, 255), SDF_FONT_THIN);
|
||||
|
||||
sprintf(prompt_message, "requested by %s", argv[1]);
|
||||
draw_sdf_string(ctx, (width - draw_sdf_string_width(prompt_message, FONT_SIZE_MAIN, SDF_FONT_THIN)) / 2, 150, prompt_message, FONT_SIZE_MAIN, rgb(255, 255, 255), SDF_FONT_THIN);
|
||||
|
||||
if (error) {
|
||||
sprintf(prompt_message, "Try again. %d failures.", fails);
|
||||
draw_sdf_string(ctx, (width - draw_sdf_string_width(prompt_message, FONT_SIZE_MAIN, SDF_FONT_THIN)) / 2, 50, prompt_message, FONT_SIZE_MAIN, rgb(255, 0, 0), SDF_FONT_THIN);
|
||||
|
||||
}
|
||||
|
||||
char password_circles[512] = {0};;
|
||||
strcpy(password_circles, "");
|
||||
for (unsigned int i = 0; i < strlen(password) && i < 512/4; ++i) {
|
||||
strcat(password_circles, "\007");
|
||||
}
|
||||
draw_sdf_string(ctx, (width - draw_sdf_string_width(password_circles, FONT_SIZE_PASSWD, SDF_FONT_THIN)) / 2, 80, password_circles, FONT_SIZE_PASSWD, rgb(255, 255, 255), SDF_FONT_THIN);
|
||||
|
||||
flip(ctx);
|
||||
yutani_flip(yctx, window);
|
||||
|
||||
yutani_msg_t * msg = yutani_poll(yctx);
|
||||
|
||||
switch (msg->type) {
|
||||
case YUTANI_MSG_KEY_EVENT:
|
||||
{
|
||||
struct yutani_msg_key_event * ke = (void*)msg->data;
|
||||
if (ke->event.action == KEY_ACTION_DOWN) {
|
||||
if (ke->event.keycode == KEY_ESCAPE) {
|
||||
return 1;
|
||||
}
|
||||
if (ke->event.keycode == '\n') {
|
||||
goto _done;
|
||||
} else if (ke->event.key == 8) {
|
||||
if (i > 0) i--;
|
||||
password[i] = '\0';
|
||||
} else if (ke->event.key) {
|
||||
password[i] = ke->event.key;
|
||||
password[i+1] = '\0';
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_done:
|
||||
uid = toaru_auth_check_pass(username, password);
|
||||
|
||||
if (uid < 0) {
|
||||
fails++;
|
||||
if (fails == 3) {
|
||||
break;
|
||||
}
|
||||
error = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
char ** args = &argv[1];
|
||||
return execvp(args[0], args);
|
||||
}
|
||||
|
||||
return 1;
|
||||
return sudo_loop(graphical_callback, argv);
|
||||
}
|
||||
|
73
apps/sudo.c
73
apps/sudo.c
@ -26,21 +26,10 @@
|
||||
|
||||
#define SUDO_TIME 5 MINUTES
|
||||
|
||||
uint32_t child = 0;
|
||||
|
||||
void usage(int argc, char * argv[]) {
|
||||
fprintf(stderr, "usage: %s [command]\n", argv[0]);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
static int sudo_loop(int (*prompt_callback)(char * username, char * password, int failures, char * argv[]), char * argv[]) {
|
||||
|
||||
int fails = 0;
|
||||
|
||||
if (argc < 2) {
|
||||
usage(argc, argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct stat buf;
|
||||
if (stat("/var/sudoers", &buf)) {
|
||||
mkdir("/var/sudoers", 0700);
|
||||
@ -77,29 +66,21 @@ int main(int argc, char ** argv) {
|
||||
}
|
||||
|
||||
if (need_password) {
|
||||
char * password = malloc(sizeof(char) * 1024);
|
||||
fprintf(stderr, "[%s] password for %s: ", argv[0], username);
|
||||
fflush(stderr);
|
||||
char * password = calloc(sizeof(char) * 1024, 1);
|
||||
|
||||
/* 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(stderr, "\n");
|
||||
if (prompt_callback(username, password, fails, argv)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int uid = toaru_auth_check_pass(username, password);
|
||||
|
||||
free(password);
|
||||
|
||||
if (uid < 0) {
|
||||
fails++;
|
||||
if (fails == 3) {
|
||||
fprintf(stderr, "%s: %d incorrect password attempts\n", argv[0], fails);
|
||||
break;
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "Sorry, try again.\n");
|
||||
continue;
|
||||
@ -161,5 +142,41 @@ int main(int argc, char ** argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int basic_callback(char * username, char * password, int fails, char * argv[]) {
|
||||
fprintf(stderr, "[%s] password for %s: ", argv[0], username);
|
||||
fflush(stderr);
|
||||
|
||||
/* 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);
|
||||
if (feof(stdin)) return 1;
|
||||
|
||||
password[strlen(password)-1] = '\0';
|
||||
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void usage(int argc, char * argv[]) {
|
||||
fprintf(stderr, "usage: %s [command]\n", argv[0]);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
if (argc < 2) {
|
||||
usage(argc, argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return sudo_loop(basic_callback, argv);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user