]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 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 | */ | |
8efc0c15 | 15 | |
16 | #include "includes.h" | |
17 | RCSID("$Id$"); | |
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. */ | |
5260325f | 26 | static void (*old_handler) (int sig) = NULL; |
8efc0c15 | 27 | |
28 | /* Interrupt signal handler for read_passphrase. */ | |
29 | ||
5260325f | 30 | void |
31 | intr_handler(int sig) | |
8efc0c15 | 32 | { |
5260325f | 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); | |
8efc0c15 | 39 | } |
40 | ||
aa3378df | 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 | */ | |
8efc0c15 | 47 | |
5260325f | 48 | char * |
49 | read_passphrase(const char *prompt, int from_stdin) | |
8efc0c15 | 50 | { |
5260325f | 51 | char buf[1024], *cp; |
52 | struct termios tio; | |
53 | FILE *f; | |
54 | ||
55 | if (from_stdin) | |
56 | f = stdin; | |
57 | else { | |
aa3378df | 58 | /* |
59 | * Read the passphrase from /dev/tty to make it possible to | |
60 | * ask it even when stdin has been redirected. | |
61 | */ | |
5260325f | 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 | } | |
8efc0c15 | 68 | } |
8efc0c15 | 69 | |
5260325f | 70 | /* Display the prompt (on stderr because stdout might be redirected). */ |
71 | fflush(stdout); | |
72 | fprintf(stderr, "%s", prompt); | |
73 | fflush(stderr); | |
74 | ||
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); | |
80 | ||
81 | /* Set new terminal modes disabling all echo. */ | |
82 | tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); | |
83 | tcsetattr(fileno(f), TCSANOW, &tio); | |
84 | ||
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); | |
aa3378df | 108 | /* |
109 | * Clear the buffer so we don\'t leave copies of the passphrase | |
110 | * laying around. | |
111 | */ | |
5260325f | 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; | |
8efc0c15 | 119 | } |