Do not use getpass(3)

This commit is contained in:
Eric Andersen 2004-05-01 01:27:30 +00:00
parent 90d2bff4c6
commit 6f9a7783ce
7 changed files with 98 additions and 38 deletions

View File

@ -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);

View File

@ -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))

87
libbb/bb_askpass.c Normal file
View File

@ -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 <andersen@codepoet.org>
*
* 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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#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;
}

View File

@ -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 );

View File

@ -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;

View File

@ -5,7 +5,6 @@
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
#include <unistd.h>
#include <utmp.h>
#include <sys/resource.h>
@ -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);

View File

@ -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);