diff options
Diffstat (limited to 'openbsd-compat/readpassphrase.c')
-rw-r--r-- | openbsd-compat/readpassphrase.c | 133 |
1 files changed, 75 insertions, 58 deletions
diff --git a/openbsd-compat/readpassphrase.c b/openbsd-compat/readpassphrase.c index fdef15809..4e63b6189 100644 --- a/openbsd-compat/readpassphrase.c +++ b/openbsd-compat/readpassphrase.c | |||
@@ -1,3 +1,5 @@ | |||
1 | /* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */ | ||
2 | |||
1 | /* | 3 | /* |
2 | * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> |
3 | * All rights reserved. | 5 | * All rights reserved. |
@@ -26,7 +28,7 @@ | |||
26 | */ | 28 | */ |
27 | 29 | ||
28 | #if defined(LIBC_SCCS) && !defined(lint) | 30 | #if defined(LIBC_SCCS) && !defined(lint) |
29 | static char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.5 2001/06/27 13:23:30 djm Exp $"; | 31 | static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $"; |
30 | #endif /* LIBC_SCCS and not lint */ | 32 | #endif /* LIBC_SCCS and not lint */ |
31 | 33 | ||
32 | #include "includes.h" | 34 | #include "includes.h" |
@@ -47,20 +49,19 @@ static char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.5 2001/06/27 13:23:30 djm | |||
47 | # define _POSIX_VDISABLE VDISABLE | 49 | # define _POSIX_VDISABLE VDISABLE |
48 | #endif | 50 | #endif |
49 | 51 | ||
52 | static volatile sig_atomic_t signo; | ||
53 | |||
54 | static void handler(int); | ||
55 | |||
50 | char * | 56 | char * |
51 | readpassphrase(prompt, buf, bufsiz, flags) | 57 | readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) |
52 | const char *prompt; | ||
53 | char *buf; | ||
54 | size_t bufsiz; | ||
55 | int flags; | ||
56 | { | 58 | { |
57 | struct termios term; | 59 | ssize_t nr; |
60 | int input, output, save_errno; | ||
58 | char ch, *p, *end; | 61 | char ch, *p, *end; |
59 | #ifdef _POSIX_VDISABLE | 62 | struct termios term, oterm; |
60 | u_char status; | 63 | struct sigaction sa, saveint, savehup, savequit, saveterm; |
61 | #endif | 64 | struct sigaction savetstp, savettin, savettou; |
62 | int echo, input, output; | ||
63 | sigset_t oset, nset; | ||
64 | 65 | ||
65 | /* I suppose we could alloc on demand in this case (XXX). */ | 66 | /* I suppose we could alloc on demand in this case (XXX). */ |
66 | if (bufsiz == 0) { | 67 | if (bufsiz == 0) { |
@@ -68,6 +69,7 @@ readpassphrase(prompt, buf, bufsiz, flags) | |||
68 | return(NULL); | 69 | return(NULL); |
69 | } | 70 | } |
70 | 71 | ||
72 | restart: | ||
71 | /* | 73 | /* |
72 | * Read and write to /dev/tty if available. If not, read from | 74 | * Read and write to /dev/tty if available. If not, read from |
73 | * stdin and write to stderr unless a tty is required. | 75 | * stdin and write to stderr unless a tty is required. |
@@ -82,44 +84,39 @@ readpassphrase(prompt, buf, bufsiz, flags) | |||
82 | } | 84 | } |
83 | 85 | ||
84 | /* | 86 | /* |
85 | * We block SIGINT and SIGTSTP so the terminal is not left | 87 | * Catch signals that would otherwise cause the user to end |
86 | * in an inconsistent state (ie: no echo). It would probably | 88 | * up with echo turned off in the shell. Don't worry about |
87 | * be better to simply catch these though. | 89 | * things like SIGALRM and SIGPIPE for now. |
88 | */ | 90 | */ |
89 | sigemptyset(&nset); | 91 | sigemptyset(&sa.sa_mask); |
90 | sigaddset(&nset, SIGINT); | 92 | sa.sa_flags = 0; /* don't restart system calls */ |
91 | sigaddset(&nset, SIGTSTP); | 93 | sa.sa_handler = handler; |
92 | (void)sigprocmask(SIG_BLOCK, &nset, &oset); | 94 | (void)sigaction(SIGINT, &sa, &saveint); |
95 | (void)sigaction(SIGHUP, &sa, &savehup); | ||
96 | (void)sigaction(SIGQUIT, &sa, &savequit); | ||
97 | (void)sigaction(SIGTERM, &sa, &saveterm); | ||
98 | (void)sigaction(SIGTSTP, &sa, &savetstp); | ||
99 | (void)sigaction(SIGTTIN, &sa, &savettin); | ||
100 | (void)sigaction(SIGTTOU, &sa, &savettou); | ||
93 | 101 | ||
94 | /* Turn off echo if possible. */ | 102 | /* Turn off echo if possible. */ |
95 | echo = 0; | 103 | if (tcgetattr(input, &oterm) == 0) { |
96 | #ifdef _POSIX_VDISABLE | 104 | memcpy(&term, &oterm, sizeof(term)); |
97 | status = _POSIX_VDISABLE; | 105 | if (!(flags & RPP_ECHO_ON)) |
98 | #endif | 106 | term.c_lflag &= ~(ECHO | ECHONL); |
99 | if (tcgetattr(input, &term) == 0) { | ||
100 | if (!(flags & RPP_ECHO_ON) && (term.c_lflag & ECHO)) { | ||
101 | echo = 1; | ||
102 | term.c_lflag &= ~ECHO; | ||
103 | } | ||
104 | #ifdef VSTATUS | 107 | #ifdef VSTATUS |
105 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) { | 108 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) |
106 | status = term.c_cc[VSTATUS]; | ||
107 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; | 109 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; |
108 | } | ||
109 | #endif | 110 | #endif |
110 | (void)tcsetattr(input, _T_FLUSH, &term); | 111 | (void)tcsetattr(input, _T_FLUSH, &term); |
111 | } | 112 | } else { |
112 | if (!(flags & RPP_ECHO_ON)) { | 113 | memset(&term, 0, sizeof(term)); |
113 | if (tcgetattr(input, &term) == 0 && (term.c_lflag & ECHO)) { | 114 | memset(&oterm, 0, sizeof(oterm)); |
114 | echo = 1; | ||
115 | term.c_lflag &= ~ECHO; | ||
116 | (void)tcsetattr(input, _T_FLUSH, &term); | ||
117 | } | ||
118 | } | 115 | } |
119 | 116 | ||
120 | (void)write(output, prompt, strlen(prompt)); | 117 | (void)write(output, prompt, strlen(prompt)); |
121 | end = buf + bufsiz - 1; | 118 | end = buf + bufsiz - 1; |
122 | for (p = buf; read(input, &ch, 1) == 1 && ch != '\n' && ch != '\r';) { | 119 | for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { |
123 | if (p < end) { | 120 | if (p < end) { |
124 | if ((flags & RPP_SEVENBIT)) | 121 | if ((flags & RPP_SEVENBIT)) |
125 | ch &= 0x7f; | 122 | ch &= 0x7f; |
@@ -133,35 +130,55 @@ readpassphrase(prompt, buf, bufsiz, flags) | |||
133 | } | 130 | } |
134 | } | 131 | } |
135 | *p = '\0'; | 132 | *p = '\0'; |
136 | #ifdef _POSIX_VDISABLE | 133 | save_errno = errno; |
137 | if (echo || status != _POSIX_VDISABLE) { | 134 | if (!(term.c_lflag & ECHO)) |
138 | #else | 135 | (void)write(output, "\n", 1); |
139 | if (echo) { | 136 | |
140 | #endif | 137 | /* Restore old terminal settings and signals. */ |
141 | if (echo) { | 138 | if (memcmp(&term, &oterm, sizeof(term)) != 0) |
142 | (void)write(output, "\n", 1); | ||
143 | term.c_lflag |= ECHO; | ||
144 | } | ||
145 | #ifdef VSTATUS | ||
146 | if (status != _POSIX_VDISABLE) | ||
147 | term.c_cc[VSTATUS] = status; | ||
148 | #endif | ||
149 | (void)tcsetattr(input, _T_FLUSH, &term); | 139 | (void)tcsetattr(input, _T_FLUSH, &term); |
150 | } | 140 | (void)sigaction(SIGINT, &saveint, NULL); |
151 | (void)sigprocmask(SIG_SETMASK, &oset, NULL); | 141 | (void)sigaction(SIGHUP, &savehup, NULL); |
142 | (void)sigaction(SIGQUIT, &savequit, NULL); | ||
143 | (void)sigaction(SIGTERM, &saveterm, NULL); | ||
144 | (void)sigaction(SIGTSTP, &savetstp, NULL); | ||
145 | (void)sigaction(SIGTTIN, &savettin, NULL); | ||
146 | (void)sigaction(SIGTTOU, &savettou, NULL); | ||
152 | if (input != STDIN_FILENO) | 147 | if (input != STDIN_FILENO) |
153 | (void)close(input); | 148 | (void)close(input); |
154 | return(buf); | 149 | |
150 | /* | ||
151 | * If we were interrupted by a signal, resend it to ourselves | ||
152 | * now that we have restored the signal handlers. | ||
153 | */ | ||
154 | if (signo) { | ||
155 | kill(getpid(), signo); | ||
156 | switch (signo) { | ||
157 | case SIGTSTP: | ||
158 | case SIGTTIN: | ||
159 | case SIGTTOU: | ||
160 | signo = 0; | ||
161 | goto restart; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | errno = save_errno; | ||
166 | return(nr == -1 ? NULL : buf); | ||
155 | } | 167 | } |
156 | #endif /* HAVE_READPASSPHRASE */ | 168 | #endif /* HAVE_READPASSPHRASE */ |
157 | 169 | ||
158 | #if 0 | 170 | #if 0 |
159 | char * | 171 | char * |
160 | getpass(prompt) | 172 | getpass(const char *prompt) |
161 | const char *prompt; | ||
162 | { | 173 | { |
163 | static char buf[_PASSWORD_LEN + 1]; | 174 | static char buf[_PASSWORD_LEN + 1]; |
164 | 175 | ||
165 | return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); | 176 | return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); |
166 | } | 177 | } |
167 | #endif | 178 | #endif |
179 | |||
180 | static void handler(int s) | ||
181 | { | ||
182 | |||
183 | signo = s; | ||
184 | } | ||