gsudo: act like regular sudo

This commit is contained in:
K. Lange 2018-12-08 17:02:00 +09:00
parent d63c98b6bf
commit 004c792e91
2 changed files with 122 additions and 118 deletions

View File

@ -21,45 +21,23 @@
#include <toaru/graphics.h> #include <toaru/graphics.h>
#include <toaru/sdf.h> #include <toaru/sdf.h>
#define main __main_unused
#include "sudo.c"
#undef main
#define FONT_SIZE_MAIN 20 #define FONT_SIZE_MAIN 20
#define FONT_SIZE_PASSWD 25 #define FONT_SIZE_PASSWD 25
uint32_t child = 0; static yutani_t * yctx;
static gfx_context_t * ctx;
static yutani_window_t * window;
int main(int argc, char ** argv) { static int graphical_callback(char * username, char * password, int fails, char * argv[]) {
int fails = 0;
if (argc < 2) {
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);
yutani_window_move(yctx, window, left, top);
gfx_context_t * ctx = init_graphics_yutani_double_buffer(window);
while (1) {
char * username = getenv("USER");
char * password = calloc(sizeof(char) * 1024,1);
int i = 0; int i = 0;
while (1) { while (1) {
draw_fill(ctx, rgba(0,0,0,200)); draw_fill(ctx, rgba(0,0,0,200));
int h = height-1; int h = ctx->height-1;
int w = width-1; int w = ctx->width-1;
draw_line(ctx, 0,0,0,h, rgb(255,0,0)); 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, w,w,0,h, rgb(255,0,0));
draw_line(ctx, 0,w,0,0, rgb(255,0,0)); draw_line(ctx, 0,w,0,0, rgb(255,0,0));
@ -67,14 +45,14 @@ int main(int argc, char ** argv) {
char prompt_message[512]; char prompt_message[512];
sprintf(prompt_message, "Enter password for '%s'", username); 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); 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]); 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); 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 (error) { if (fails) {
sprintf(prompt_message, "Try again. %d failures.", fails); 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); 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);
} }
@ -83,7 +61,7 @@ int main(int argc, char ** argv) {
for (unsigned int i = 0; i < strlen(password) && i < 512/4; ++i) { for (unsigned int i = 0; i < strlen(password) && i < 512/4; ++i) {
strcat(password_circles, "\007"); 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); 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); flip(ctx);
yutani_flip(yctx, window); yutani_flip(yctx, window);
@ -99,7 +77,7 @@ int main(int argc, char ** argv) {
return 1; return 1;
} }
if (ke->event.keycode == '\n') { if (ke->event.keycode == '\n') {
goto _done; return 0;
} else if (ke->event.key == 8) { } else if (ke->event.key == 8) {
if (i > 0) i--; if (i > 0) i--;
password[i] = '\0'; password[i] = '\0';
@ -113,22 +91,31 @@ int main(int argc, char ** argv) {
break; break;
} }
} }
}
_done:
uid = toaru_auth_check_pass(username, password); int main(int argc, char ** argv) {
if (uid < 0) { if (argc < 2) {
fails++; return 1;
if (fails == 3) { }
break;
} yctx = yutani_init();
error = 1;
continue; if (!yctx) {
} fprintf(stderr, "%s: could not connect to compositor\n", argv[0]);
return 1;
char ** args = &argv[1]; }
return execvp(args[0], args);
} int width = 300;
int height = 200;
return 1;
int left = (yctx->display_width - width) / 2;
int top = (yctx->display_height - height) / 2;
window = yutani_window_create(yctx, width, height);
yutani_window_move(yctx, window, left, top);
ctx = init_graphics_yutani_double_buffer(window);
return sudo_loop(graphical_callback, argv);
} }

View File

@ -26,21 +26,10 @@
#define SUDO_TIME 5 MINUTES #define SUDO_TIME 5 MINUTES
uint32_t child = 0; static int sudo_loop(int (*prompt_callback)(char * username, char * password, int failures, char * argv[]), char * argv[]) {
void usage(int argc, char * argv[]) {
fprintf(stderr, "usage: %s [command]\n", argv[0]);
}
int main(int argc, char ** argv) {
int fails = 0; int fails = 0;
if (argc < 2) {
usage(argc, argv);
return 1;
}
struct stat buf; struct stat buf;
if (stat("/var/sudoers", &buf)) { if (stat("/var/sudoers", &buf)) {
mkdir("/var/sudoers", 0700); mkdir("/var/sudoers", 0700);
@ -77,29 +66,21 @@ int main(int argc, char ** argv) {
} }
if (need_password) { if (need_password) {
char * password = malloc(sizeof(char) * 1024); char * password = calloc(sizeof(char) * 1024, 1);
fprintf(stderr, "[%s] password for %s: ", argv[0], username);
fflush(stderr);
/* Disable echo */ if (prompt_callback(username, password, fails, argv)) {
struct termios old, new; return 1;
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");
int uid = toaru_auth_check_pass(username, password); int uid = toaru_auth_check_pass(username, password);
free(password);
if (uid < 0) { if (uid < 0) {
fails++; fails++;
if (fails == 3) { if (fails == 3) {
fprintf(stderr, "%s: %d incorrect password attempts\n", argv[0], fails); fprintf(stderr, "%s: %d incorrect password attempts\n", argv[0], fails);
break; return 1;
} }
fprintf(stderr, "Sorry, try again.\n"); fprintf(stderr, "Sorry, try again.\n");
continue; continue;
@ -161,5 +142,41 @@ int main(int argc, char ** argv) {
return 1; 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);
}