summaryrefslogtreecommitdiffstats
path: root/src/getpass.c
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2013-04-05 14:13:37 +0200
committerAndreas Schneider <asn@cryptomilk.org>2013-04-05 14:14:22 +0200
commit992f00b1456cf9c416d9b89bf7b97f2f7e02566a (patch)
tree8c42594134171bafc0038d20726f88ab6b0cd194 /src/getpass.c
parent24e94d53e914eb90e246491892f498055c90286f (diff)
downloadlibssh-992f00b1456cf9c416d9b89bf7b97f2f7e02566a.tar.gz
libssh-992f00b1456cf9c416d9b89bf7b97f2f7e02566a.tar.xz
libssh-992f00b1456cf9c416d9b89bf7b97f2f7e02566a.zip
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.
Diffstat (limited to 'src/getpass.c')
-rw-r--r--src/getpass.c50
1 files changed, 27 insertions, 23 deletions
diff --git a/src/getpass.c b/src/getpass.c
index 4d3a404..cc6d33c 100644
--- a/src/getpass.c
+++ b/src/getpass.c
@@ -220,31 +220,33 @@ int ssh_getpass(const char *prompt,
return -1;
}
- ZERO_STRUCT(attr);
- ZERO_STRUCT(old_attr);
+ if (isatty(STDIN_FILENO)) {
+ ZERO_STRUCT(attr);
+ ZERO_STRUCT(old_attr);
- /* get local terminal attributes */
- if (tcgetattr(STDIN_FILENO, &attr) < 0) {
- perror("tcgetattr");
- return -1;
- }
+ /* get local terminal attributes */
+ if (tcgetattr(STDIN_FILENO, &attr) < 0) {
+ perror("tcgetattr");
+ return -1;
+ }
- /* save terminal attributes */
- memcpy(&old_attr, &attr, sizeof(attr));
- if((fd = fcntl(0, F_GETFL, 0)) < 0) {
- perror("fcntl");
- return -1;
- }
+ /* save terminal attributes */
+ memcpy(&old_attr, &attr, sizeof(attr));
+ if((fd = fcntl(0, F_GETFL, 0)) < 0) {
+ perror("fcntl");
+ return -1;
+ }
- /* disable echo */
- if (!echo) {
- attr.c_lflag &= ~(ECHO);
- }
+ /* disable echo */
+ if (!echo) {
+ attr.c_lflag &= ~(ECHO);
+ }
- /* write attributes to terminal */
- if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
- perror("tcsetattr");
- return -1;
+ /* write attributes to terminal */
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
+ perror("tcsetattr");
+ return -1;
+ }
}
/* disable nonblocking I/O */
@@ -254,8 +256,10 @@ int ssh_getpass(const char *prompt,
ok = ssh_gets(prompt, buf, len, verify);
- /* reset terminal */
- tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);
+ if (isatty(STDIN_FILENO)) {
+ /* reset terminal */
+ tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);
+ }
/* close fd */
if (fd & O_NDELAY) {