5 Author: Tatu Ylonen <ylo@cs.hut.fi>
7 Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 Created: Mon Jul 10 22:08:59 1995 ylo
12 Functions for reading passphrases and passwords.
22 /* Saved old terminal mode for read_passphrase. */
23 static struct termios saved_tio;
25 /* Old interrupt signal handler for read_passphrase. */
26 static void (*old_handler)(int sig) = NULL;
28 /* Interrupt signal handler for read_passphrase. */
30 void intr_handler(int sig)
32 /* Restore terminal modes. */
33 tcsetattr(fileno(stdin), TCSANOW, &saved_tio);
34 /* Restore the old signal handler. */
35 signal(sig, old_handler);
36 /* Resend the signal, with the old handler. */
40 /* Reads a passphrase from /dev/tty with echo turned off. Returns the
41 passphrase (allocated with xmalloc). Exits if EOF is encountered.
42 The passphrase if read from stdin if from_stdin is true (as is the
43 case with ssh-keygen). */
45 char *read_passphrase(const char *prompt, int from_stdin)
55 /* Read the passphrase from /dev/tty to make it possible to ask it even
56 when stdin has been redirected. */
57 f = fopen("/dev/tty", "r");
60 /* No controlling terminal and no DISPLAY. Nowhere to read. */
61 fprintf(stderr, "You have no controlling tty and no DISPLAY. Cannot read passphrase.\n");
66 /* Display the prompt (on stderr because stdout might be redirected). */
68 fprintf(stderr, "%s", prompt);
71 /* Get terminal modes. */
72 tcgetattr(fileno(f), &tio);
74 /* Save signal handler and set the new handler. */
75 old_handler = signal(SIGINT, intr_handler);
77 /* Set new terminal modes disabling all echo. */
78 tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
79 tcsetattr(fileno(f), TCSANOW, &tio);
81 /* Read the passphrase from the terminal. */
82 if (fgets(buf, sizeof(buf), f) == NULL)
84 /* Got EOF. Just exit. */
85 /* Restore terminal modes. */
86 tcsetattr(fileno(f), TCSANOW, &saved_tio);
87 /* Restore the signal handler. */
88 signal(SIGINT, old_handler);
89 /* Print a newline (the prompt probably didn\'t have one). */
90 fprintf(stderr, "\n");
96 /* Restore terminal modes. */
97 tcsetattr(fileno(f), TCSANOW, &saved_tio);
98 /* Restore the signal handler. */
99 (void)signal(SIGINT, old_handler);
100 /* Remove newline from the passphrase. */
101 if (strchr(buf, '\n'))
102 *strchr(buf, '\n') = 0;
103 /* Allocate a copy of the passphrase. */
105 /* Clear the buffer so we don\'t leave copies of the passphrase laying
107 memset(buf, 0, sizeof(buf));
108 /* Print a newline since the prompt probably didn\'t have one. */
109 fprintf(stderr, "\n");
110 /* Close the file. */