[PATCH] getpass: Don't fail if stdin is not a tty
authorAlexander Bokovoy <ab@samba.org>
Thu, 11 Apr 2013 07:45:12 +0000 (09:45 +0200)
committerKarolin Seeger <kseeger@samba.org>
Thu, 11 Apr 2013 09:35:07 +0000 (11:35 +0200)
We don't need to manipulate the tty state (such as turning off
echo) when prompting for passwords if we're not reading from a tty.

Backport based on a patch provided by Stef Walter <stefw@gnome.org>.

Fix bug #9767 - "net ads join" fails when called via stdin.

Autobuild-User(v4-0-test): Karolin Seeger <kseeger@samba.org>
Autobuild-Date(v4-0-test): Thu Apr 11 11:35:07 CEST 2013 on sn-devel-104

lib/replace/getpass.c

index f95109f2b43ddd0fc7b751ffde93aae3a8b4640c..3627222ba2316d4445531517328c95a763f337e2 100644 (file)
@@ -138,7 +138,7 @@ static void gotintr_sig(int signum)
 char *rep_getpass(const char *prompt)
 {
        FILE *in, *out;
-       int echo_off;
+       int echo_off, is_a_tty;
        static char buf[256];
        static size_t bufsize = sizeof(buf);
        size_t nread;
@@ -160,8 +160,9 @@ char *rep_getpass(const char *prompt)
        setvbuf(in, NULL, _IONBF, 0);
 
        /* Turn echoing off if it is on now.  */
+       is_a_tty = isatty(fileno(in)) > 0;
 
-       if (tcgetattr (fileno (in), &t) == 0) {
+       if (is_a_tty && (tcgetattr (fileno (in), &t) == 0)) {
                if (ECHO_IS_ON(t)) {
                        TURN_ECHO_OFF(t);
                        echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
@@ -196,7 +197,7 @@ char *rep_getpass(const char *prompt)
                if (gotintr && in_fd == -1) {
                        in = fopen ("/dev/tty", "w+");
                }
-               if (in != NULL)
+               if ((in != NULL) && is_a_tty)
                        tcsetattr (fileno (in), TCSANOW, &t);
        }