1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-29 01:03:57 +03:00

getpass: Don't fail if stdin is not a tty.

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.
This commit is contained in:
Andreas Schneider
2013-04-05 14:13:37 +02:00
parent 24e94d53e9
commit 992f00b145

View File

@@ -220,31 +220,33 @@ int ssh_getpass(const char *prompt,
return -1; return -1;
} }
ZERO_STRUCT(attr); if (isatty(STDIN_FILENO)) {
ZERO_STRUCT(old_attr); ZERO_STRUCT(attr);
ZERO_STRUCT(old_attr);
/* get local terminal attributes */ /* get local terminal attributes */
if (tcgetattr(STDIN_FILENO, &attr) < 0) { if (tcgetattr(STDIN_FILENO, &attr) < 0) {
perror("tcgetattr"); perror("tcgetattr");
return -1; return -1;
} }
/* save terminal attributes */ /* save terminal attributes */
memcpy(&old_attr, &attr, sizeof(attr)); memcpy(&old_attr, &attr, sizeof(attr));
if((fd = fcntl(0, F_GETFL, 0)) < 0) { if((fd = fcntl(0, F_GETFL, 0)) < 0) {
perror("fcntl"); perror("fcntl");
return -1; return -1;
} }
/* disable echo */ /* disable echo */
if (!echo) { if (!echo) {
attr.c_lflag &= ~(ECHO); attr.c_lflag &= ~(ECHO);
} }
/* write attributes to terminal */ /* write attributes to terminal */
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
perror("tcsetattr"); perror("tcsetattr");
return -1; return -1;
}
} }
/* disable nonblocking I/O */ /* disable nonblocking I/O */
@@ -254,8 +256,10 @@ int ssh_getpass(const char *prompt,
ok = ssh_gets(prompt, buf, len, verify); ok = ssh_gets(prompt, buf, len, verify);
/* reset terminal */ if (isatty(STDIN_FILENO)) {
tcsetattr(STDIN_FILENO, TCSANOW, &old_attr); /* reset terminal */
tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);
}
/* close fd */ /* close fd */
if (fd & O_NDELAY) { if (fd & O_NDELAY) {