Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * |
| 3 | * readpass.c |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Mon Jul 10 22:08:59 1995 ylo |
| 11 | * |
| 12 | * Functions for reading passphrases and passwords. |
| 13 | * |
| 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 17 | RCSID("$Id: readpass.c,v 1.3 1999/11/25 00:54:59 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | |
| 19 | #include "xmalloc.h" |
| 20 | #include "ssh.h" |
| 21 | |
| 22 | /* Saved old terminal mode for read_passphrase. */ |
| 23 | static struct termios saved_tio; |
| 24 | |
| 25 | /* Old interrupt signal handler for read_passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 26 | static void (*old_handler) (int sig) = NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 27 | |
| 28 | /* Interrupt signal handler for read_passphrase. */ |
| 29 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 30 | void |
| 31 | intr_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 33 | /* Restore terminal modes. */ |
| 34 | tcsetattr(fileno(stdin), TCSANOW, &saved_tio); |
| 35 | /* Restore the old signal handler. */ |
| 36 | signal(sig, old_handler); |
| 37 | /* Resend the signal, with the old handler. */ |
| 38 | kill(getpid(), sig); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | } |
| 40 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 41 | /* |
| 42 | * Reads a passphrase from /dev/tty with echo turned off. Returns the |
| 43 | * passphrase (allocated with xmalloc). Exits if EOF is encountered. The |
| 44 | * passphrase if read from stdin if from_stdin is true (as is the case with |
| 45 | * ssh-keygen). |
| 46 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 47 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | char * |
| 49 | read_passphrase(const char *prompt, int from_stdin) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 51 | char buf[1024], *cp; |
| 52 | struct termios tio; |
| 53 | FILE *f; |
| 54 | |
| 55 | if (from_stdin) |
| 56 | f = stdin; |
| 57 | else { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 58 | /* |
| 59 | * Read the passphrase from /dev/tty to make it possible to |
| 60 | * ask it even when stdin has been redirected. |
| 61 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 62 | f = fopen("/dev/tty", "r"); |
| 63 | if (!f) { |
| 64 | /* No controlling terminal and no DISPLAY. Nowhere to read. */ |
| 65 | fprintf(stderr, "You have no controlling tty and no DISPLAY. Cannot read passphrase.\n"); |
| 66 | exit(1); |
| 67 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | /* Display the prompt (on stderr because stdout might be redirected). */ |
| 71 | fflush(stdout); |
| 72 | fprintf(stderr, "%s", prompt); |
| 73 | fflush(stderr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | /* Get terminal modes. */ |
| 76 | tcgetattr(fileno(f), &tio); |
| 77 | saved_tio = tio; |
| 78 | /* Save signal handler and set the new handler. */ |
| 79 | old_handler = signal(SIGINT, intr_handler); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 81 | /* Set new terminal modes disabling all echo. */ |
| 82 | tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); |
| 83 | tcsetattr(fileno(f), TCSANOW, &tio); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 84 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | /* Read the passphrase from the terminal. */ |
| 86 | if (fgets(buf, sizeof(buf), f) == NULL) { |
| 87 | /* Got EOF. Just exit. */ |
| 88 | /* Restore terminal modes. */ |
| 89 | tcsetattr(fileno(f), TCSANOW, &saved_tio); |
| 90 | /* Restore the signal handler. */ |
| 91 | signal(SIGINT, old_handler); |
| 92 | /* Print a newline (the prompt probably didn\'t have one). */ |
| 93 | fprintf(stderr, "\n"); |
| 94 | /* Close the file. */ |
| 95 | if (f != stdin) |
| 96 | fclose(f); |
| 97 | exit(1); |
| 98 | } |
| 99 | /* Restore terminal modes. */ |
| 100 | tcsetattr(fileno(f), TCSANOW, &saved_tio); |
| 101 | /* Restore the signal handler. */ |
| 102 | (void) signal(SIGINT, old_handler); |
| 103 | /* Remove newline from the passphrase. */ |
| 104 | if (strchr(buf, '\n')) |
| 105 | *strchr(buf, '\n') = 0; |
| 106 | /* Allocate a copy of the passphrase. */ |
| 107 | cp = xstrdup(buf); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 108 | /* |
| 109 | * Clear the buffer so we don\'t leave copies of the passphrase |
| 110 | * laying around. |
| 111 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | memset(buf, 0, sizeof(buf)); |
| 113 | /* Print a newline since the prompt probably didn\'t have one. */ |
| 114 | fprintf(stderr, "\n"); |
| 115 | /* Close the file. */ |
| 116 | if (f != stdin) |
| 117 | fclose(f); |
| 118 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 119 | } |