|
2 | 2 | #include <common/hsm_encryption.h>
|
3 | 3 | #include <sodium/utils.h>
|
4 | 4 | #include <termios.h>
|
| 5 | +#include <unistd.h> |
| 6 | +#include <stdio.h> |
5 | 7 |
|
6 | 8 | char *hsm_secret_encryption_key(const char *pass, struct secret *key)
|
7 | 9 | {
|
@@ -84,31 +86,42 @@ char *read_stdin_pass(char **reason)
|
84 | 86 | char *passwd = NULL;
|
85 | 87 | size_t passwd_size = 0;
|
86 | 88 |
|
87 |
| - /* Set a temporary term, same as current but with ECHO disabled. */ |
88 |
| - if (tcgetattr(fileno(stdin), ¤t_term) != 0) { |
89 |
| - *reason = "Could not get current terminal options."; |
90 |
| - return NULL; |
| 89 | + if (isatty(fileno(stdin))) { |
| 90 | + /* Set a temporary term, same as current but with ECHO disabled. */ |
| 91 | + if (tcgetattr(fileno(stdin), ¤t_term) != 0) { |
| 92 | + *reason = "Could not get current terminal options."; |
| 93 | + return NULL; |
| 94 | + } |
| 95 | + temp_term = current_term; |
| 96 | + temp_term.c_lflag &= ~ECHO; |
| 97 | + if (tcsetattr(fileno(stdin), TCSAFLUSH, &temp_term) != 0) { |
| 98 | + *reason = "Could not disable pass echoing."; |
| 99 | + return NULL; |
| 100 | + } |
| 101 | + |
| 102 | + /* Read the password, do not take the newline character into account. */ |
| 103 | + if (getline(&passwd, &passwd_size, stdin) < 0) { |
| 104 | + *reason = "Could not read pass from stdin."; |
| 105 | + return NULL; |
| 106 | + } |
| 107 | + if (passwd[strlen(passwd) - 1] == '\n') |
| 108 | + passwd[strlen(passwd) - 1] = '\0'; |
| 109 | + |
| 110 | + /* Restore the original terminal */ |
| 111 | + if (tcsetattr(fileno(stdin), TCSAFLUSH, ¤t_term) != 0) { |
| 112 | + *reason = "Could not restore terminal options."; |
| 113 | + free(passwd); |
| 114 | + return NULL; |
| 115 | + } |
91 | 116 | }
|
92 |
| - temp_term = current_term; |
93 |
| - temp_term.c_lflag &= ~ECHO; |
94 |
| - if (tcsetattr(fileno(stdin), TCSAFLUSH, &temp_term) != 0) { |
95 |
| - *reason = "Could not disable pass echoing."; |
96 |
| - return NULL; |
97 |
| - } |
98 |
| - |
99 |
| - /* Read the password, do not take the newline character into account. */ |
100 |
| - if (getline(&passwd, &passwd_size, stdin) < 0) { |
101 |
| - *reason = "Could not read pass from stdin."; |
102 |
| - return NULL; |
103 |
| - } |
104 |
| - if (passwd[strlen(passwd) - 1] == '\n') |
105 |
| - passwd[strlen(passwd) - 1] = '\0'; |
106 |
| - |
107 |
| - /* Restore the original terminal */ |
108 |
| - if (tcsetattr(fileno(stdin), TCSAFLUSH, ¤t_term) != 0) { |
109 |
| - *reason = "Could not restore terminal options."; |
110 |
| - free(passwd); |
111 |
| - return NULL; |
| 117 | + else { |
| 118 | + /* Read from stdin, do not take the newline character into account. */ |
| 119 | + if (getline(&passwd, &passwd_size, stdin) < 0) { |
| 120 | + *reason = "Could not read pass from stdin."; |
| 121 | + return NULL; |
| 122 | + } |
| 123 | + if (passwd[strlen(passwd) - 1] == '\n') |
| 124 | + passwd[strlen(passwd) - 1] = '\0'; |
112 | 125 | }
|
113 | 126 |
|
114 | 127 | return passwd;
|
|
0 commit comments