From 6f9a7783ce2f3ffae28176f8bcfcd6b86c1b41b3 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sat, 1 May 2004 01:27:30 +0000 Subject: Do not use getpass(3) --- include/libbb.h | 1 + libbb/Makefile.in | 2 +- libbb/bb_askpass.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ libbb/correct_password.c | 4 +-- loginutils/passwd.c | 6 ++-- loginutils/sulogin.c | 28 +--------------- loginutils/vlock.c | 8 ++--- 7 files changed, 98 insertions(+), 38 deletions(-) create mode 100644 libbb/bb_askpass.c diff --git a/include/libbb.h b/include/libbb.h index a01a0ca3e..b65043cf3 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -233,6 +233,7 @@ extern long my_getgrnam(const char *name); extern char * my_getpwuid(char *name, long uid); extern char * my_getgrgid(char *group, long gid); extern long my_getpwnamegid(const char *name); +extern char *bb_askpass(int timeout, const char * prompt); extern int device_open(const char *device, int mode); diff --git a/libbb/Makefile.in b/libbb/Makefile.in index 632208184..eff3224b1 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in @@ -46,7 +46,7 @@ LIBBB_SRC:= \ xgethostbyname.c xgethostbyname2.c xreadlink.c xregcomp.c xgetlarg.c \ get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \ getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \ - perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c \ + perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \ warn_ignoring_args.c concat_subpath_file.c vfork_daemon_rexec.c LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) diff --git a/libbb/bb_askpass.c b/libbb/bb_askpass.c new file mode 100644 index 000000000..1ae1520d9 --- /dev/null +++ b/libbb/bb_askpass.c @@ -0,0 +1,87 @@ +/* vi: set sw=4 ts=4: */ +/* + * Ask for a password + * I use a static buffer in this function. Plan accordingly. + * + * Copyright (C) 1999-2004 by Erik Andersen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#define PWD_BUFFER_SIZE 256 + + +/* do nothing signal handler */ +static void askpass_timeout(int ignore) +{ +} + +char *bb_askpass(int timeout, const char * prompt) +{ + char *ret; + int i, size; + struct sigaction sa; + struct termios old, new; + static char passwd[PWD_BUFFER_SIZE]; + + tcgetattr(STDIN_FILENO, &old); + + size = sizeof(passwd); + ret = passwd; + memset(passwd, 0, size); + + fputs(prompt, stdout); + fflush(stdout); + + tcgetattr(STDIN_FILENO, &new); + new.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); + new.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP); + tcsetattr(STDIN_FILENO, TCSANOW, &new); + + if (timeout) { + sa.sa_flags = 0; + sa.sa_handler = askpass_timeout; + sigaction(SIGALRM, &sa, NULL); + alarm(timeout); + } + + if (read(STDIN_FILENO, passwd, size-1) <= 0) { + ret = NULL; + } else { + for(i = 0; i < size && passwd[i]; i++) { + if (passwd[i]== '\r' || passwd[i] == '\n') { + passwd[i]= 0; + break; + } + } + } + + if (timeout) { + alarm(0); + } + + tcsetattr(STDIN_FILENO, TCSANOW, &old); + fputs("\n", stdout); + fflush(stdout); + return ret; +} + diff --git a/libbb/correct_password.c b/libbb/correct_password.c index 1da83c441..e3ff44689 100644 --- a/libbb/correct_password.c +++ b/libbb/correct_password.c @@ -66,10 +66,10 @@ int correct_password ( const struct passwd *pw ) if ( correct == 0 || correct[0] == '\0' ) return 1; - unencrypted = getpass ( "Password: " ); + unencrypted = bb_askpass ( 0, "Password: " ); if ( !unencrypted ) { - fputs ( "getpass: cannot open /dev/tty\n", stderr ); + fputs ( "cannot open /dev/tty\n", stderr ); return 0; } encrypted = crypt ( unencrypted, correct ); diff --git a/loginutils/passwd.c b/loginutils/passwd.c index 269e529f3..d0b2afc19 100644 --- a/loginutils/passwd.c +++ b/loginutils/passwd.c @@ -332,7 +332,7 @@ static int new_password(const struct passwd *pw, int amroot, int algo) time_t start, now; if (!amroot && crypt_passwd[0]) { - if (!(clear = getpass("Old password:"))) { + if (!(clear = bb_askpass(0, "Old password:"))) { /* return -1; */ return 1; } @@ -356,7 +356,7 @@ static int new_password(const struct passwd *pw, int amroot, int algo) } else { orig[0] = '\0'; } - if (! (cp=getpass("Enter the new password (minimum of 5, maximum of 8 characters)\n" + if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n" "Please use a combination of upper and lower case letters and numbers.\n" "Enter new password: "))) { @@ -375,7 +375,7 @@ static int new_password(const struct passwd *pw, int amroot, int algo) return 1; } } - if (!(cp = getpass("Re-enter new password: "))) { + if (!(cp = bb_askpass(0, "Re-enter new password: "))) { bzero(orig, sizeof orig); /* return -1; */ return 1; diff --git a/loginutils/sulogin.c b/loginutils/sulogin.c index bb4716e0d..f21b09571 100644 --- a/loginutils/sulogin.c +++ b/loginutils/sulogin.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -55,7 +54,6 @@ extern int sulogin_main(int argc, char **argv) const char *name = "root"; int timeout = 0; static char pass[BUFSIZ]; - struct termios termio; struct passwd pwent; struct passwd *pwd; time_t start, now; @@ -64,28 +62,6 @@ extern int sulogin_main(int argc, char **argv) struct spwd *spwd = NULL; #endif /* CONFIG_FEATURE_SHADOWPASSWDS */ - tcgetattr(0, &termio); - /* set control chars */ - termio.c_cc[VINTR] = 3; /* C-c */ - termio.c_cc[VQUIT] = 28; /* C-\ */ - termio.c_cc[VERASE] = 127; /* C-? */ - termio.c_cc[VKILL] = 21; /* C-u */ - termio.c_cc[VEOF] = 4; /* C-d */ - termio.c_cc[VSTART] = 17; /* C-q */ - termio.c_cc[VSTOP] = 19; /* C-s */ - termio.c_cc[VSUSP] = 26; /* C-z */ - /* use line dicipline 0 */ - termio.c_line = 0; - /* Make it be sane */ - termio.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; - termio.c_cflag |= CREAD|HUPCL|CLOCAL; - /* input modes */ - termio.c_iflag = ICRNL | IXON | IXOFF; - /* output modes */ - termio.c_oflag = OPOST | ONLCR; - /* local modes */ - termio.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN; - tcsetattr(0, TCSANOW, &termio); openlog("sulogin", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH); if (argc > 1) { if (strncmp(argv[1], "-t", 2) == 0) { @@ -132,7 +108,6 @@ extern int sulogin_main(int argc, char **argv) signal(SIGALRM, catchalarm); - alarm(timeout); if (!(pwd = getpwnam(name))) { syslog(LOG_WARNING, "No password entry for `root'\n"); bb_error_msg_and_die("No password entry for `root'\n"); @@ -150,7 +125,7 @@ extern int sulogin_main(int argc, char **argv) } #endif /* CONFIG_FEATURE_SHADOWPASSWDS */ while (1) { - cp = getpass(SULOGIN_PROMPT); + cp = bb_askpass(timeout, SULOGIN_PROMPT); if (!cp || !*cp) { puts("\n"); fflush(stdout); @@ -174,7 +149,6 @@ extern int sulogin_main(int argc, char **argv) syslog(LOG_WARNING, "Incorrect root password\n"); } bzero(pass, strlen(pass)); - alarm(0); signal(SIGALRM, SIG_DFL); puts("Entering System Maintenance Mode\n"); fflush(stdout); diff --git a/loginutils/vlock.c b/loginutils/vlock.c index 7abf120d9..def484ae6 100644 --- a/loginutils/vlock.c +++ b/loginutils/vlock.c @@ -193,10 +193,9 @@ extern int vlock_main(int argc, char **argv) snprintf(prompt, 100, "%s's password: ", pw->pw_name); - if ((pass = getpass(prompt)) == NULL) { - perror("getpass"); + if ((pass = bb_askpass(0, prompt)) == NULL) { restore_terminal(); - exit(1); + bb_perror_msg_and_die("password"); } crypt_pass = pw_encrypt(pass, pw->pw_passwd); @@ -210,9 +209,8 @@ extern int vlock_main(int argc, char **argv) memset(crypt_pass, 0, strlen(crypt_pass)); if (isatty(STDIN_FILENO) == 0) { - perror("isatty"); restore_terminal(); - exit(1); + bb_perror_msg_and_die("isatty"); } sleep(++times); -- cgit v1.2.3