| 113 | } |
| 114 | |
| 115 | char *read_stdin_pass_with_exit_code(char **reason, int *exit_code) |
| 116 | { |
| 117 | struct termios current_term, temp_term; |
| 118 | char *passwd = NULL; |
| 119 | size_t passwd_size = 0; |
| 120 | |
| 121 | if (isatty(fileno(stdin))) { |
| 122 | /* Set a temporary term, same as current but with ECHO disabled. */ |
| 123 | if (tcgetattr(fileno(stdin), ¤t_term) != 0) { |
| 124 | *reason = "Could not get current terminal options."; |
| 125 | *exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR; |
| 126 | return NULL; |
| 127 | } |
| 128 | temp_term = current_term; |
| 129 | temp_term.c_lflag &= ~ECHO; |
| 130 | if (tcsetattr(fileno(stdin), TCSANOW, &temp_term) != 0) { |
| 131 | *reason = "Could not disable pass echoing."; |
| 132 | *exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR; |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | if (!getline_stdin_pass(&passwd, &passwd_size)) { |
| 137 | *reason = "Could not read pass from stdin."; |
| 138 | *exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR; |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | /* Restore the original terminal */ |
| 143 | if (tcsetattr(fileno(stdin), TCSANOW, ¤t_term) != 0) { |
| 144 | *reason = "Could not restore terminal options."; |
| 145 | free(passwd); |
| 146 | *exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR; |
| 147 | return NULL; |
| 148 | } |
| 149 | } else if (!getline_stdin_pass(&passwd, &passwd_size)) { |
| 150 | *reason = "Could not read pass from stdin."; |
| 151 | *exit_code = EXITCODE_HSM_PASSWORD_INPUT_ERR; |
| 152 | return NULL; |
| 153 | } |
| 154 | return passwd; |
| 155 | } |
no test coverage detected