gsudo.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 main __main_unused
  22. #include "sudo.c"
  23. #undef main
  24. #define FONT_SIZE_MAIN 20
  25. #define FONT_SIZE_PASSWD 25
  26. static yutani_t * yctx;
  27. static gfx_context_t * ctx;
  28. static yutani_window_t * window;
  29. static int graphical_callback(char * username, char * password, int fails, char * argv[]) {
  30. int i = 0;
  31. while (1) {
  32. draw_fill(ctx, rgba(0,0,0,200));
  33. int h = ctx->height-1;
  34. int w = ctx->width-1;
  35. draw_line(ctx, 0,0,0,h, rgb(255,0,0));
  36. draw_line(ctx, w,w,0,h, rgb(255,0,0));
  37. draw_line(ctx, 0,w,0,0, rgb(255,0,0));
  38. draw_line(ctx, 0,w,h,h, rgb(255,0,0));
  39. char prompt_message[512];
  40. sprintf(prompt_message, "Enter password for '%s'", username);
  41. 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);
  42. sprintf(prompt_message, "requested by %s", argv[1]);
  43. 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);
  44. if (fails) {
  45. sprintf(prompt_message, "Try again. %d failures.", fails);
  46. 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);
  47. }
  48. char password_circles[512] = {0};;
  49. strcpy(password_circles, "");
  50. for (unsigned int i = 0; i < strlen(password) && i < 512/4; ++i) {
  51. strcat(password_circles, "\007");
  52. }
  53. 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);
  54. flip(ctx);
  55. yutani_flip(yctx, window);
  56. yutani_msg_t * msg = yutani_poll(yctx);
  57. switch (msg->type) {
  58. case YUTANI_MSG_KEY_EVENT:
  59. {
  60. struct yutani_msg_key_event * ke = (void*)msg->data;
  61. if (ke->event.action == KEY_ACTION_DOWN) {
  62. if (ke->event.keycode == KEY_ESCAPE) {
  63. return 1;
  64. }
  65. if (ke->event.keycode == '\n') {
  66. return 0;
  67. } else if (ke->event.key == 8) {
  68. if (i > 0) i--;
  69. password[i] = '\0';
  70. } else if (ke->event.key) {
  71. password[i] = ke->event.key;
  72. password[i+1] = '\0';
  73. i++;
  74. }
  75. }
  76. }
  77. break;
  78. case YUTANI_MSG_WINDOW_CLOSE:
  79. case YUTANI_MSG_SESSION_END:
  80. return 1;
  81. }
  82. }
  83. }
  84. int main(int argc, char ** argv) {
  85. if (argc < 2) {
  86. return 1;
  87. }
  88. yctx = yutani_init();
  89. if (!yctx) {
  90. fprintf(stderr, "%s: could not connect to compositor\n", argv[0]);
  91. return 1;
  92. }
  93. int width = 300;
  94. int height = 200;
  95. int left = (yctx->display_width - width) / 2;
  96. int top = (yctx->display_height - height) / 2;
  97. window = yutani_window_create(yctx, width, height);
  98. yutani_window_move(yctx, window, left, top);
  99. yutani_window_advertise(yctx, window, "gsudo");
  100. ctx = init_graphics_yutani_double_buffer(window);
  101. return sudo_loop(graphical_callback, argv);
  102. }