summaryrefslogtreecommitdiff
path: root/readpass.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-11-25 00:26:21 +1100
committerDamien Miller <djm@mindrot.org>1999-11-25 00:26:21 +1100
commit95def09838fc61b37b6ea7cd5c234a465b4b129b (patch)
tree042744f76f40a326b873cb1c3690a6d7d966bc3e /readpass.c
parent4d2f15f895f4c795afc008aeff3fd2ceffbc44f4 (diff)
- Merged very large OpenBSD source code reformat
- OpenBSD CVS updates - [channels.c cipher.c compat.c log-client.c scp.c serverloop.c] [ssh.h sshd.8 sshd.c] syslog changes: * Unified Logmessage for all auth-types, for success and for failed * Standard connections get only ONE line in the LOG when level==LOG: Auth-attempts are logged only, if authentication is: a) successfull or b) with passwd or c) we had more than AUTH_FAIL_LOG failues * many log() became verbose() * old behaviour with level=VERBOSE - [readconf.c readconf.h ssh.1 ssh.h sshconnect.c sshd.c] tranfer s/key challenge/response data in SSH_SMSG_AUTH_TIS_CHALLENGE messages. allows use of s/key in windows (ttssh, securecrt) and ssh-1.2.27 clients without 'ssh -v', ok: niels@ - [sshd.8] -V, for fallback to openssh in SSH2 compatibility mode - [sshd.c] fix sigchld race; cjc5@po.cwru.edu
Diffstat (limited to 'readpass.c')
-rw-r--r--readpass.c179
1 files changed, 89 insertions, 90 deletions
diff --git a/readpass.c b/readpass.c
index 3031825e5..66ce33c97 100644
--- a/readpass.c
+++ b/readpass.c
@@ -1,20 +1,20 @@
1/* 1/*
2 2 *
3readpass.c 3 * readpass.c
4 4 *
5Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 6 *
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 All rights reserved 8 * All rights reserved
9 9 *
10Created: Mon Jul 10 22:08:59 1995 ylo 10 * Created: Mon Jul 10 22:08:59 1995 ylo
11 11 *
12Functions for reading passphrases and passwords. 12 * Functions for reading passphrases and passwords.
13 13 *
14*/ 14 */
15 15
16#include "includes.h" 16#include "includes.h"
17RCSID("$Id: readpass.c,v 1.1 1999/10/27 03:42:44 damien Exp $"); 17RCSID("$Id: readpass.c,v 1.2 1999/11/24 13:26:22 damien Exp $");
18 18
19#include "xmalloc.h" 19#include "xmalloc.h"
20#include "ssh.h" 20#include "ssh.h"
@@ -23,92 +23,91 @@ RCSID("$Id: readpass.c,v 1.1 1999/10/27 03:42:44 damien Exp $");
23static struct termios saved_tio; 23static struct termios saved_tio;
24 24
25/* Old interrupt signal handler for read_passphrase. */ 25/* Old interrupt signal handler for read_passphrase. */
26static void (*old_handler)(int sig) = NULL; 26static void (*old_handler) (int sig) = NULL;
27 27
28/* Interrupt signal handler for read_passphrase. */ 28/* Interrupt signal handler for read_passphrase. */
29 29
30void intr_handler(int sig) 30void
31intr_handler(int sig)
31{ 32{
32 /* Restore terminal modes. */ 33 /* Restore terminal modes. */
33 tcsetattr(fileno(stdin), TCSANOW, &saved_tio); 34 tcsetattr(fileno(stdin), TCSANOW, &saved_tio);
34 /* Restore the old signal handler. */ 35 /* Restore the old signal handler. */
35 signal(sig, old_handler); 36 signal(sig, old_handler);
36 /* Resend the signal, with the old handler. */ 37 /* Resend the signal, with the old handler. */
37 kill(getpid(), sig); 38 kill(getpid(), sig);
38} 39}
39 40
40/* Reads a passphrase from /dev/tty with echo turned off. Returns the 41/* Reads a passphrase from /dev/tty with echo turned off. Returns the
41 passphrase (allocated with xmalloc). Exits if EOF is encountered. 42 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 The passphrase if read from stdin if from_stdin is true (as is the
43 case with ssh-keygen). */ 44 case with ssh-keygen). */
44 45
45char *read_passphrase(const char *prompt, int from_stdin) 46char *
47read_passphrase(const char *prompt, int from_stdin)
46{ 48{
47 char buf[1024], *cp; 49 char buf[1024], *cp;
48 struct termios tio; 50 struct termios tio;
49 FILE *f; 51 FILE *f;
50 52
51 if (from_stdin) 53 if (from_stdin)
52 f = stdin; 54 f = stdin;
53 else 55 else {
54 { 56 /* Read the passphrase from /dev/tty to make it possible
55 /* Read the passphrase from /dev/tty to make it possible to ask it even 57 to ask it even when stdin has been redirected. */
56 when stdin has been redirected. */ 58 f = fopen("/dev/tty", "r");
57 f = fopen("/dev/tty", "r"); 59 if (!f) {
58 if (!f) 60 /* No controlling terminal and no DISPLAY. Nowhere to read. */
59 { 61 fprintf(stderr, "You have no controlling tty and no DISPLAY. Cannot read passphrase.\n");
60 /* No controlling terminal and no DISPLAY. Nowhere to read. */ 62 exit(1);
61 fprintf(stderr, "You have no controlling tty and no DISPLAY. Cannot read passphrase.\n"); 63 }
62 exit(1);
63 } 64 }
64 }
65
66 /* Display the prompt (on stderr because stdout might be redirected). */
67 fflush(stdout);
68 fprintf(stderr, "%s", prompt);
69 fflush(stderr);
70
71 /* Get terminal modes. */
72 tcgetattr(fileno(f), &tio);
73 saved_tio = tio;
74 /* Save signal handler and set the new handler. */
75 old_handler = signal(SIGINT, intr_handler);
76 65
77 /* Set new terminal modes disabling all echo. */ 66 /* Display the prompt (on stderr because stdout might be redirected). */
78 tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); 67 fflush(stdout);
79 tcsetattr(fileno(f), TCSANOW, &tio); 68 fprintf(stderr, "%s", prompt);
80 69 fflush(stderr);
81 /* Read the passphrase from the terminal. */ 70
82 if (fgets(buf, sizeof(buf), f) == NULL) 71 /* Get terminal modes. */
83 { 72 tcgetattr(fileno(f), &tio);
84 /* Got EOF. Just exit. */ 73 saved_tio = tio;
85 /* Restore terminal modes. */ 74 /* Save signal handler and set the new handler. */
86 tcsetattr(fileno(f), TCSANOW, &saved_tio); 75 old_handler = signal(SIGINT, intr_handler);
87 /* Restore the signal handler. */ 76
88 signal(SIGINT, old_handler); 77 /* Set new terminal modes disabling all echo. */
89 /* Print a newline (the prompt probably didn\'t have one). */ 78 tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
90 fprintf(stderr, "\n"); 79 tcsetattr(fileno(f), TCSANOW, &tio);
91 /* Close the file. */ 80
92 if (f != stdin) 81 /* Read the passphrase from the terminal. */
93 fclose(f); 82 if (fgets(buf, sizeof(buf), f) == NULL) {
94 exit(1); 83 /* Got EOF. Just exit. */
95 } 84 /* Restore terminal modes. */
96 /* Restore terminal modes. */ 85 tcsetattr(fileno(f), TCSANOW, &saved_tio);
97 tcsetattr(fileno(f), TCSANOW, &saved_tio); 86 /* Restore the signal handler. */
98 /* Restore the signal handler. */ 87 signal(SIGINT, old_handler);
99 (void)signal(SIGINT, old_handler); 88 /* Print a newline (the prompt probably didn\'t have one). */
100 /* Remove newline from the passphrase. */ 89 fprintf(stderr, "\n");
101 if (strchr(buf, '\n')) 90 /* Close the file. */
102 *strchr(buf, '\n') = 0; 91 if (f != stdin)
103 /* Allocate a copy of the passphrase. */ 92 fclose(f);
104 cp = xstrdup(buf); 93 exit(1);
105 /* Clear the buffer so we don\'t leave copies of the passphrase laying 94 }
106 around. */ 95 /* Restore terminal modes. */
107 memset(buf, 0, sizeof(buf)); 96 tcsetattr(fileno(f), TCSANOW, &saved_tio);
108 /* Print a newline since the prompt probably didn\'t have one. */ 97 /* Restore the signal handler. */
109 fprintf(stderr, "\n"); 98 (void) signal(SIGINT, old_handler);
110 /* Close the file. */ 99 /* Remove newline from the passphrase. */
111 if (f != stdin) 100 if (strchr(buf, '\n'))
112 fclose(f); 101 *strchr(buf, '\n') = 0;
113 return cp; 102 /* Allocate a copy of the passphrase. */
103 cp = xstrdup(buf);
104 /* Clear the buffer so we don\'t leave copies of the passphrase
105 laying around. */
106 memset(buf, 0, sizeof(buf));
107 /* Print a newline since the prompt probably didn\'t have one. */
108 fprintf(stderr, "\n");
109 /* Close the file. */
110 if (f != stdin)
111 fclose(f);
112 return cp;
114} 113}