diff options
Diffstat (limited to 'examples/tacacs-example.c')
| -rw-r--r-- | examples/tacacs-example.c | 73 |
1 files changed, 72 insertions, 1 deletions
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 @@ | |||
| 57 | #include <pwd.h> | 57 | #include <pwd.h> |
| 58 | #include <unistd.h> | 58 | #include <unistd.h> |
| 59 | 59 | ||
| 60 | #ifdef HAVE_TERMIOS_H | ||
| 61 | # include <termios.h> | ||
| 62 | #endif | ||
| 63 | #ifdef HAVE_SGTTY_H | ||
| 64 | # include <sgtty.h> | ||
| 65 | #endif | ||
| 66 | |||
| 60 | 67 | ||
| 61 | /////////////////// | 68 | /////////////////// |
| 62 | // // | 69 | // // |
| @@ -269,6 +276,11 @@ my_error( | |||
| 269 | 276 | ||
| 270 | 277 | ||
| 271 | char * | 278 | char * |
| 279 | my_getpass( | ||
| 280 | const char * prompt ); | ||
| 281 | |||
| 282 | |||
| 283 | char * | ||
| 272 | my_ntop( | 284 | my_ntop( |
| 273 | struct sockaddr_storage * sa, | 285 | struct sockaddr_storage * sa, |
| 274 | char * buff, | 286 | char * buff, |
| @@ -504,7 +516,7 @@ my_cmd_authen( | |||
| 504 | assert(user != NULL); | 516 | assert(user != NULL); |
| 505 | assert(pass != NULL); | 517 | assert(pass != NULL); |
| 506 | 518 | ||
| 507 | str = getpass("Enter password: "); | 519 | str = my_getpass("Enter password: "); |
| 508 | strncpy(pass, str, sizeof(pass)); | 520 | strncpy(pass, str, sizeof(pass)); |
| 509 | pass[sizeof(pass)-1] = '\0'; | 521 | pass[sizeof(pass)-1] = '\0'; |
| 510 | 522 | ||
| @@ -825,6 +837,65 @@ my_error( | |||
| 825 | 837 | ||
| 826 | 838 | ||
| 827 | char * | 839 | char * |
| 840 | my_getpass( | ||
| 841 | const char * prompt ) | ||
| 842 | { | ||
| 843 | FILE * fs; | ||
| 844 | int c; | ||
| 845 | size_t pos; | ||
| 846 | static char pass[512]; | ||
| 847 | size_t passlen; | ||
| 848 | #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) | ||
| 849 | struct termios old; | ||
| 850 | struct termios new; | ||
| 851 | void (*sig)( int sig ); | ||
| 852 | #endif | ||
| 853 | |||
| 854 | passlen = sizeof(pass); | ||
| 855 | |||
| 856 | // prompts for password | ||
| 857 | prompt = ((prompt)) ? prompt : "Password: "; | ||
| 858 | if (prompt) | ||
| 859 | fprintf(stderr, "%s", prompt); | ||
| 860 | fflush(stdout); | ||
| 861 | |||
| 862 | // disables ECHO | ||
| 863 | #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) | ||
| 864 | if ((fs = fopen("/dev/tty", "r")) == NULL) | ||
| 865 | fs = stdin; | ||
| 866 | if(tcgetattr(fileno(fs), &old) == -1) | ||
| 867 | return(NULL); | ||
| 868 | sig = signal(SIGINT, SIG_IGN); | ||
| 869 | new = old; | ||
| 870 | new.c_lflag &= ~((unsigned)ECHO); | ||
| 871 | if(tcsetattr(fileno(fs), TCSANOW, &new)) | ||
| 872 | return(NULL); | ||
| 873 | #else | ||
| 874 | fs = stdin; | ||
| 875 | #endif | ||
| 876 | |||
| 877 | // reads buffer | ||
| 878 | pos = 0; | ||
| 879 | while ( ((c = getc(fs)) != EOF) && (c != '\n') && (c != '\r') ) | ||
| 880 | if (pos < (passlen-1)) | ||
| 881 | pass[pos++] = (char)c; | ||
| 882 | pass[pos] = '\0'; | ||
| 883 | |||
| 884 | // restores previous terminal | ||
| 885 | fprintf(stderr, "\n"); | ||
| 886 | #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) | ||
| 887 | fflush(stderr); | ||
| 888 | if(tcsetattr(fileno(fs), TCSANOW, &old)) | ||
| 889 | return(NULL); | ||
| 890 | signal(SIGINT, sig); | ||
| 891 | fclose(fs); | ||
| 892 | #endif | ||
| 893 | |||
| 894 | return(pass); | ||
| 895 | } | ||
| 896 | |||
| 897 | |||
| 898 | char * | ||
| 828 | my_ntop( | 899 | my_ntop( |
| 829 | struct sockaddr_storage * sa, | 900 | struct sockaddr_storage * sa, |
| 830 | char * buff, | 901 | char * buff, |
