]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
askpass: Allow reading password from stdin even if it's not a tty.
authorTimo Sirainen <tss@iki.fi>
Tue, 30 Nov 2010 00:17:20 +0000 (00:17 +0000)
committerTimo Sirainen <tss@iki.fi>
Tue, 30 Nov 2010 00:17:20 +0000 (00:17 +0000)
src/lib/askpass.c

index 528dc65c275ec85314de289da50c5108e449d1b6..4deb68b4e57f6cdd61902af17709b6b01dcccad3 100644 (file)
 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 @@ static void askpass_str(const char *prompt, buffer_t *pass)
                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)