+
+static int ask_tty(void) {
+ struct termios old_termios, new_termios;
+ char passphrase[LINE_MAX];
+ FILE *ttyf;
+
+ if (!(ttyf = fopen("/dev/tty", "w"))) {
+ log_error("Failed to open /dev/tty: %m");
+ return -errno;
+ }
+
+ fputs("\x1B[1m", ttyf);
+ fprintf(ttyf, "%s: ", arg_message);
+ fputs("\x1B[0m", ttyf);
+ fflush(ttyf);
+
+ if (tcgetattr(STDIN_FILENO, &old_termios) >= 0) {
+
+ new_termios = old_termios;
+
+ new_termios.c_lflag &= ~(ICANON|ECHO);
+ new_termios.c_cc[VMIN] = 1;
+ new_termios.c_cc[VTIME] = 0;
+
+ if (tcsetattr(STDIN_FILENO, TCSADRAIN, &new_termios) >= 0) {
+ size_t p = 0;
+ int r = 0;
+
+ for (;;) {
+ size_t k;
+ char c;
+
+ k = fread(&c, 1, 1, stdin);
+
+ if (k <= 0) {
+ r = -EIO;
+ break;
+ }
+
+ if (c == '\n')
+ break;
+ else if (c == '\b' || c == 127) {
+ if (p > 0) {
+ p--;
+ fputs("\b \b", ttyf);
+ }
+ } else {
+ passphrase[p++] = c;
+ fputc('*', ttyf);
+ }
+
+ fflush(ttyf);
+ }
+
+ fputc('\n', ttyf);
+ fclose(ttyf);
+ tcsetattr(STDIN_FILENO, TCSADRAIN, &old_termios);
+
+ if (r < 0)
+ return -EIO;
+
+ passphrase[p] = 0;
+
+ fputs(passphrase, stdout);
+ fflush(stdout);
+ return 0;
+ }
+
+ }
+
+ fclose(ttyf);
+
+ if (!fgets(passphrase, sizeof(passphrase), stdin)) {
+ log_error("Failed to read password.");
+ return -EIO;
+ }
+
+ truncate_nl(passphrase);
+ fputs(passphrase, stdout);
+ fflush(stdout);
+
+ return 0;
+}
+
+int main(int argc, char *argv[]) {
+ int r;
+
+ log_parse_environment();
+ log_open();
+
+ if ((r = parse_argv(argc, argv)) <= 0)
+ goto finish;
+
+ if (arg_use_tty && isatty(STDIN_FILENO))
+ r = ask_tty();
+ else
+ r = ask_agent();
+
+finish:
+ return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}