blob: 5b7119fd753a4d3bc3d34906cad22f3763a27469 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
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 Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller5428f641999-11-25 11:54:57 +110017RCSID("$Id: readpass.c,v 1.3 1999/11/25 00:54:59 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "xmalloc.h"
20#include "ssh.h"
21
22/* Saved old terminal mode for read_passphrase. */
23static struct termios saved_tio;
24
25/* Old interrupt signal handler for read_passphrase. */
Damien Miller95def091999-11-25 00:26:21 +110026static void (*old_handler) (int sig) = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
28/* Interrupt signal handler for read_passphrase. */
29
Damien Miller95def091999-11-25 00:26:21 +110030void
31intr_handler(int sig)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032{
Damien Miller95def091999-11-25 00:26:21 +110033 /* 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 Millerd4a8b7e1999-10-27 13:42:43 +100039}
40
Damien Miller5428f641999-11-25 11:54:57 +110041/*
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 Millerd4a8b7e1999-10-27 13:42:43 +100047
Damien Miller95def091999-11-25 00:26:21 +110048char *
49read_passphrase(const char *prompt, int from_stdin)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050{
Damien Miller95def091999-11-25 00:26:21 +110051 char buf[1024], *cp;
52 struct termios tio;
53 FILE *f;
54
55 if (from_stdin)
56 f = stdin;
57 else {
Damien Miller5428f641999-11-25 11:54:57 +110058 /*
59 * Read the passphrase from /dev/tty to make it possible to
60 * ask it even when stdin has been redirected.
61 */
Damien Miller95def091999-11-25 00:26:21 +110062 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 Millerd4a8b7e1999-10-27 13:42:43 +100068 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Miller95def091999-11-25 00:26:21 +110070 /* Display the prompt (on stderr because stdout might be redirected). */
71 fflush(stdout);
72 fprintf(stderr, "%s", prompt);
73 fflush(stderr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller95def091999-11-25 00:26:21 +110075 /* 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 Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller95def091999-11-25 00:26:21 +110081 /* Set new terminal modes disabling all echo. */
82 tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
83 tcsetattr(fileno(f), TCSANOW, &tio);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller95def091999-11-25 00:26:21 +110085 /* 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 Miller5428f641999-11-25 11:54:57 +1100108 /*
109 * Clear the buffer so we don\'t leave copies of the passphrase
110 * laying around.
111 */
Damien Miller95def091999-11-25 00:26:21 +1100112 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 Millerd4a8b7e1999-10-27 13:42:43 +1000119}