dovecot-2.0: askpass: Allow reading password from stdin even if ...

dovecot at dovecot.org dovecot at dovecot.org
Tue Nov 30 02:17:44 EET 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/719ce27f9955
changeset: 12483:719ce27f9955
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Nov 30 00:17:20 2010 +0000
description:
askpass: Allow reading password from stdin even if it's not a tty.

diffstat:

 src/lib/askpass.c |  43 ++++++++++++++++++++++++-------------------
 1 files changed, 24 insertions(+), 19 deletions(-)

diffs (67 lines):

diff -r 28db9b177087 -r 719ce27f9955 src/lib/askpass.c
--- 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)


More information about the dovecot-cvs mailing list