Mercurial > dovecot > core-2.2
changeset 12483:719ce27f9955
askpass: Allow reading password from stdin even if it's not a tty.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 30 Nov 2010 00:17:20 +0000 |
parents | 28db9b177087 |
children | a131a498731f |
files | src/lib/askpass.c |
diffstat | 1 files changed, 24 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/askpass.c Tue Nov 30 00:13:45 2010 +0000 +++ b/src/lib/askpass.c Tue Nov 30 00:17:20 2010 +0000 @@ -13,27 +13,30 @@ static void askpass_str(const char *prompt, buffer_t *pass) { struct termios old_tio, tio; - bool restore_tio = FALSE; + bool tty, restore_tio = FALSE; size_t pos; char ch; int fd; - if (!isatty(STDIN_FILENO)) - i_fatal("stdin isn't a TTY"); + tty = isatty(STDIN_FILENO); + if (tty) { + fputs(prompt, stderr); + fflush(stderr); - fputs(prompt, stderr); - fflush(stderr); + fd = open("/dev/tty", O_RDONLY); + if (fd < 0) + i_fatal("open(/dev/tty) failed: %m"); - fd = open("/dev/tty", O_RDONLY); - if (fd < 0) - i_fatal("open(/dev/tty) failed: %m"); - - /* turn off echo */ - if (tcgetattr(fd, &old_tio) == 0) { - restore_tio = TRUE; - tio = old_tio; - tio.c_lflag &= ~(ECHO | ECHONL); - (void)tcsetattr(fd, TCSAFLUSH, &tio); + /* turn off echo */ + if (tcgetattr(fd, &old_tio) == 0) { + restore_tio = TRUE; + tio = old_tio; + tio.c_lflag &= ~(ECHO | ECHONL); + (void)tcsetattr(fd, TCSAFLUSH, &tio); + } + } else { + /* read it from stdin without showing a prompt */ + fd = STDIN_FILENO; } /* read the password */ @@ -44,11 +47,13 @@ buffer_append_c(pass, ch); } - if (restore_tio) - (void)tcsetattr(fd, TCSAFLUSH, &old_tio); + if (tty) { + if (restore_tio) + (void)tcsetattr(fd, TCSAFLUSH, &old_tio); - fputs("\n", stderr); fflush(stderr); - (void)close(fd); + fputs("\n", stderr); fflush(stderr); + (void)close(fd); + } } void askpass(const char *prompt, char *buf, size_t buf_size)