From 4784fe24919c797c3dc1e03cbfa1361d8809ecd8 Mon Sep 17 00:00:00 2001 From: Valera Cogut Date: Thu, 18 Oct 2018 11:15:35 +0300 Subject: [PATCH 1/3] Hide secret information --- include/editline.h | 3 +++ src/complete.c | 15 +++++++++++++++ src/editline.c | 9 ++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/editline.h b/include/editline.h index eb1a569..c47a0bc 100644 --- a/include/editline.h +++ b/include/editline.h @@ -45,6 +45,7 @@ typedef enum { /* Editline specific types, despite rl_ prefix. From Heimdal project. */ typedef int rl_list_possib_func_t(char*, char***); +typedef int rl_check_secret_func_t(const char*); typedef el_status_t el_keymap_func_t(void); typedef int rl_hook_func_t(void); typedef int rl_getc_func_t(void); @@ -76,6 +77,7 @@ extern char *rl_complete(char *token, int *match); extern int rl_list_possib(char *token, char ***av); extern char **rl_completion_matches(const char *token, rl_compentry_func_t *generator); extern char *rl_filename_completion_function(const char *text, int state); +extern int rl_check_secret(char *source); /* For compatibility with FSF readline. */ extern int rl_point; @@ -117,6 +119,7 @@ extern int write_history (const char *filename); extern rl_completion_func_t *rl_attempted_completion_function; extern rl_complete_func_t *rl_set_complete_func (rl_complete_func_t *func); extern rl_list_possib_func_t *rl_set_list_possib_func (rl_list_possib_func_t *func); +extern rl_check_secret_func_t *rl_set_check_secret_func (rl_check_secret_func_t *func); /* Alternate interface to plain readline(), for event loops */ extern void rl_callback_handler_install (const char *prompt, rl_vcpfunc_t *lhandler); diff --git a/src/complete.c b/src/complete.c index d6b8a4b..3e67b3f 100644 --- a/src/complete.c +++ b/src/complete.c @@ -397,6 +397,21 @@ char *rl_complete(char *token, int *match) return el_filename_complete(token, match); } +static rl_check_secret_func_t *el_check_secret_func = NULL; +/* For compatibility with the Heimdal project. */ +rl_check_secret_func_t *rl_set_check_secret_func(rl_check_secret_func_t *func) +{ + rl_check_secret_func_t *old = el_check_secret_func; + el_check_secret_func = func; + return old; +} +int rl_check_secret(char *source) +{ + if (el_check_secret_func) + return el_check_secret_func(source); + return 0; +} + static rl_list_possib_func_t *el_list_possib_func = NULL; /* For compatibility with the Heimdal project. */ diff --git a/src/editline.c b/src/editline.c index 30f64f1..32ef7f1 100644 --- a/src/editline.c +++ b/src/editline.c @@ -162,7 +162,10 @@ static void tty_flush(void) return; if (!el_no_echo) { - res = write(el_outfd, Screen, ScreenCount); + if (rl_check_secret(rl_line_buffer)) + res = write(el_outfd, "", 1); + else + res = write(el_outfd, Screen, ScreenCount); if (res > 0) ScreenCount = 0; } @@ -1134,6 +1137,10 @@ static void hist_add(const char *p) if (s == NULL) return; + // Don't add secret information in history + if (rl_check_secret(s)) + return; + if (H.Size < el_hist_size) { H.Lines[H.Size++] = s; } else { From 3cd9894747b651d0bfacfa685fea8e8e577e09e2 Mon Sep 17 00:00:00 2001 From: Valera Cogut Date: Thu, 18 Oct 2018 12:58:14 +0300 Subject: [PATCH 2/3] Added example for hiding secret information --- examples/cli.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/examples/cli.c b/examples/cli.c index 9cd900b..2733209 100644 --- a/examples/cli.c +++ b/examples/cli.c @@ -21,6 +21,7 @@ #include "editline.h" #include +#include #define HISTORY "/tmp/.cli-history" @@ -116,6 +117,33 @@ el_status_t do_suspend(void) return CSstay; } +static int my_rl_check_secret(const char* source) +{ + const char* pattern = (char *)"^unlock\\s"; + regex_t regex; + + int reti; + int rez = 0; + + if (!pattern || !source) + return rez; + + /* Compile regular expression */ + reti = regcomp(®ex, pattern, 0); + if (reti) // If couldn't compile regex + return rez; + + /* Execute regular expression */ + reti = regexec(®ex, source, 0, NULL, 0); + if (!reti) // If regex match + rez = 1; + + /* Free memory allocated to the pattern buffer by regcomp() */ + regfree(®ex); + + return rez; +} + int main(void) { char *line; @@ -124,6 +152,7 @@ int main(void) /* Setup callbacks */ rl_set_complete_func(&my_rl_complete); rl_set_list_possib_func(&my_rl_list_possib); + rl_set_check_secret_func(&my_rl_check_secret); el_bind_key('?', list_possible); el_bind_key(CTL('C'), do_break); el_bind_key(CTL('D'), do_exit); From 1db83fe6c6d43c834d392e88dbe58192a109e7ca Mon Sep 17 00:00:00 2001 From: Valera Cogut Date: Thu, 18 Oct 2018 15:56:55 +0300 Subject: [PATCH 3/3] Fixed inconsistent indentation --- src/editline.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/editline.c b/src/editline.c index 32ef7f1..eb213a6 100644 --- a/src/editline.c +++ b/src/editline.c @@ -159,15 +159,16 @@ static void tty_flush(void) ssize_t res; if (!ScreenCount) - return; + return; if (!el_no_echo) { - if (rl_check_secret(rl_line_buffer)) - res = write(el_outfd, "", 1); - else - res = write(el_outfd, Screen, ScreenCount); - if (res > 0) - ScreenCount = 0; + if (rl_check_secret(rl_line_buffer)) + res = write(el_outfd, "", 1); + else + res = write(el_outfd, Screen, ScreenCount); + + if (res > 0) + ScreenCount = 0; } }