From b8624e3cd05258a81a7e3cf4d95e2dcd81fe0aa4 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Wed, 21 May 2008 22:57:40 -0400 Subject: Add second cut at password support This version works even in raw mode, by buffering key presses passed from the window object, and replying to the client after the user presses enter. There are a lot of layers of function pointers getting passed around, so it may make sense to introduce an opaque type for holding the password and triggering the reply. --- src/splash-plugins/text/text.c | 72 ++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 24 deletions(-) (limited to 'src/splash-plugins/text') diff --git a/src/splash-plugins/text/text.c b/src/splash-plugins/text/text.c index d65c0be..a40e945 100644 --- a/src/splash-plugins/text/text.c +++ b/src/splash-plugins/text/text.c @@ -38,8 +38,10 @@ #include #include #include +#include #include "ply-boot-splash-plugin.h" +#include "ply-buffer.h" #include "ply-event-loop.h" #include "ply-list.h" #include "ply-logger.h" @@ -50,11 +52,18 @@ #include +#define CLEAR_LINE_SEQUENCE "\033[2K" + struct _ply_boot_splash_plugin { ply_event_loop_t *loop; int console_fd; + ply_boot_splash_password_answer_handler_t password_answer_handler; + void *password_answer_data; + + ply_buffer_t *keyboard_input_buffer; + uint32_t keyboard_input_is_hidden : 1; }; ply_boot_splash_plugin_t * @@ -65,6 +74,7 @@ create_plugin (void) ply_trace ("creating plugin"); plugin = calloc (1, sizeof (ply_boot_splash_plugin_t)); + plugin->keyboard_input_buffer = ply_buffer_new (); plugin->console_fd = -1; return plugin; @@ -78,6 +88,8 @@ destroy_plugin (ply_boot_splash_plugin_t *plugin) if (plugin == NULL) return; + ply_buffer_free (plugin->keyboard_input_buffer); + free (plugin); } @@ -151,38 +163,50 @@ hide_splash_screen (ply_boot_splash_plugin_t *plugin, } } -char * -ask_for_password (ply_boot_splash_plugin_t *plugin) +void +ask_for_password (ply_boot_splash_plugin_t *plugin, + ply_boot_splash_password_answer_handler_t answer_handler, + void *answer_data) { - char answer[1024]; - struct termios initial_term_attributes; - struct termios noecho_term_attributes; - - tcgetattr (STDIN_FILENO, &initial_term_attributes); - noecho_term_attributes = initial_term_attributes; - noecho_term_attributes.c_lflag &= ~ECHO; - - printf ("Password: "); - - if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &noecho_term_attributes) != 0) { - fprintf (stderr, "Could not set terminal attributes\n"); - return NULL; - } - - fgets (answer, sizeof (answer), stdin); - answer[strlen (answer) - 1] = '\0'; - - tcsetattr (STDIN_FILENO, TCSANOW, &initial_term_attributes); + plugin->password_answer_handler = answer_handler; + plugin->password_answer_data = answer_data; - printf ("\n"); - - return strdup (answer); + write (STDOUT_FILENO, "\nPassword: ", strlen ("\nPassword: ")); + plugin->keyboard_input_is_hidden = true; } void on_keyboard_input (ply_boot_splash_plugin_t *plugin, const char *keyboard_input) { + ssize_t character_size; + + character_size = (ssize_t) mbrlen (keyboard_input, MB_CUR_MAX, NULL); + + if (character_size < 0) + return; + + if (plugin->password_answer_handler != NULL) + { + if (character_size == 1 && keyboard_input[0] == '\r') + { + plugin->password_answer_handler (plugin->password_answer_data, + ply_buffer_get_bytes (plugin->keyboard_input_buffer)); + plugin->keyboard_input_is_hidden = false; + ply_buffer_clear (plugin->keyboard_input_buffer); + plugin->password_answer_handler = NULL; + write (STDOUT_FILENO, CLEAR_LINE_SEQUENCE, strlen (CLEAR_LINE_SEQUENCE)); + return; + } + } + + ply_buffer_append_bytes (plugin->keyboard_input_buffer, + keyboard_input, character_size); + + if (plugin->keyboard_input_is_hidden) + write (STDOUT_FILENO, "•", strlen ("•")); + else + write (STDOUT_FILENO, keyboard_input, character_size); } ply_boot_splash_plugin_interface_t * -- cgit