From 91ee62834428e9fd0d43e5783a269b2f65460012 Mon Sep 17 00:00:00 2001 From: "David M. Syzdek" Date: Fri, 3 Jun 2022 15:59:14 -0800 Subject: tacacs-example: replacing getpass() with my_getpass() --- examples/tacacs-example.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/tacacs-example.c b/examples/tacacs-example.c index f301d6a..1f08e33 100644 --- a/examples/tacacs-example.c +++ b/examples/tacacs-example.c @@ -57,6 +57,13 @@ #include #include +#ifdef HAVE_TERMIOS_H +# include +#endif +#ifdef HAVE_SGTTY_H +# include +#endif + /////////////////// // // @@ -268,6 +275,11 @@ my_error( ... ); +char * +my_getpass( + const char * prompt ); + + char * my_ntop( struct sockaddr_storage * sa, @@ -504,7 +516,7 @@ my_cmd_authen( assert(user != NULL); assert(pass != NULL); - str = getpass("Enter password: "); + str = my_getpass("Enter password: "); strncpy(pass, str, sizeof(pass)); pass[sizeof(pass)-1] = '\0'; @@ -824,6 +836,65 @@ my_error( } +char * +my_getpass( + const char * prompt ) +{ + FILE * fs; + int c; + size_t pos; + static char pass[512]; + size_t passlen; +#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) + struct termios old; + struct termios new; + void (*sig)( int sig ); +#endif + + passlen = sizeof(pass); + + // prompts for password + prompt = ((prompt)) ? prompt : "Password: "; + if (prompt) + fprintf(stderr, "%s", prompt); + fflush(stdout); + + // disables ECHO +#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) + if ((fs = fopen("/dev/tty", "r")) == NULL) + fs = stdin; + if(tcgetattr(fileno(fs), &old) == -1) + return(NULL); + sig = signal(SIGINT, SIG_IGN); + new = old; + new.c_lflag &= ~((unsigned)ECHO); + if(tcsetattr(fileno(fs), TCSANOW, &new)) + return(NULL); +#else + fs = stdin; +#endif + + // reads buffer + pos = 0; + while ( ((c = getc(fs)) != EOF) && (c != '\n') && (c != '\r') ) + if (pos < (passlen-1)) + pass[pos++] = (char)c; + pass[pos] = '\0'; + + // restores previous terminal + fprintf(stderr, "\n"); +#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) + fflush(stderr); + if(tcsetattr(fileno(fs), TCSANOW, &old)) + return(NULL); + signal(SIGINT, sig); + fclose(fs); +#endif + + return(pass); +} + + char * my_ntop( struct sockaddr_storage * sa, -- cgit v1.2.3