gsudo.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2017 K. Lange
  5. *
  6. * gsudo - graphical implementation of sudo
  7. *
  8. * probably even less secure than the original
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <toaru/auth.h>
  18. #include <toaru/yutani.h>
  19. #include <toaru/graphics.h>
  20. #include <toaru/sdf.h>
  21. #define FONT_SIZE_MAIN 20
  22. #define FONT_SIZE_PASSWD 25
  23. uint32_t child = 0;
  24. int main(int argc, char ** argv) {
  25. int fails = 0;
  26. if (argc < 2) {
  27. return 1;
  28. }
  29. int width = 300;
  30. int height = 200;
  31. int uid;
  32. int error = 0;
  33. yutani_t * yctx = yutani_init();
  34. int left = (yctx->display_width - width) / 2;
  35. int top = (yctx->display_height - height) / 2;
  36. yutani_window_t * window = yutani_window_create(yctx, width, height);
  37. yutani_window_move(yctx, window, left, top);
  38. gfx_context_t * ctx = init_graphics_yutani_double_buffer(window);
  39. while (1) {
  40. char * username = getenv("USER");
  41. char * password = calloc(sizeof(char) * 1024,1);
  42. int i = 0;
  43. while (1) {
  44. draw_fill(ctx, rgba(0,0,0,200));
  45. int h = height-1;
  46. int w = width-1;
  47. draw_line(ctx, 0,0,0,h, rgb(255,0,0));
  48. draw_line(ctx, w,w,0,h, rgb(255,0,0));
  49. draw_line(ctx, 0,w,0,0, rgb(255,0,0));
  50. draw_line(ctx, 0,w,h,h, rgb(255,0,0));
  51. char prompt_message[512];
  52. sprintf(prompt_message, "Enter password for '%s'", username);
  53. 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);
  54. sprintf(prompt_message, "requested by %s", argv[1]);
  55. 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);
  56. if (error) {
  57. sprintf(prompt_message, "Try again. %d failures.", fails);
  58. 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);
  59. }
  60. char password_circles[512] = {0};;
  61. strcpy(password_circles, "");
  62. for (unsigned int i = 0; i < strlen(password) && i < 512/4; ++i) {
  63. strcat(password_circles, "\007");
  64. }
  65. 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);
  66. flip(ctx);
  67. yutani_flip(yctx, window);
  68. yutani_msg_t * msg = yutani_poll(yctx);
  69. switch (msg->type) {
  70. case YUTANI_MSG_KEY_EVENT:
  71. {
  72. struct yutani_msg_key_event * ke = (void*)msg->data;
  73. if (ke->event.action == KEY_ACTION_DOWN) {
  74. if (ke->event.keycode == KEY_ESCAPE) {
  75. return 1;
  76. }
  77. if (ke->event.keycode == '\n') {
  78. goto _done;
  79. } else if (ke->event.key == 8) {
  80. if (i > 0) i--;
  81. password[i] = '\0';
  82. } else if (ke->event.key) {
  83. password[i] = ke->event.key;
  84. password[i+1] = '\0';
  85. i++;
  86. }
  87. }
  88. }
  89. break;
  90. }
  91. }
  92. _done:
  93. uid = toaru_auth_check_pass(username, password);
  94. if (uid < 0) {
  95. fails++;
  96. if (fails == 3) {
  97. break;
  98. }
  99. error = 1;
  100. continue;
  101. }
  102. char ** args = &argv[1];
  103. return execvp(args[0], args);
  104. }
  105. return 1;
  106. }