]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - crypto/openssh/openbsd-compat/readpassphrase.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / crypto / openssh / openbsd-compat / readpassphrase.c
1 /*      $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $     */
2
3 /*
4  * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * Sponsored in part by the Defense Advanced Research Projects
19  * Agency (DARPA) and Air Force Research Laboratory, Air Force
20  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21  */
22
23 /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
24
25 #include "includes.h"
26
27 #ifndef HAVE_READPASSPHRASE
28
29 #include <termios.h>
30 #include <signal.h>
31 #include <ctype.h>
32 #include <fcntl.h>
33 #include <readpassphrase.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #ifdef TCSASOFT
39 # define _T_FLUSH       (TCSAFLUSH|TCSASOFT)
40 #else
41 # define _T_FLUSH       (TCSAFLUSH)
42 #endif
43
44 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46 #  define _POSIX_VDISABLE       VDISABLE
47 #endif
48
49 #ifndef _NSIG
50 # ifdef NSIG
51 #  define _NSIG NSIG
52 # else
53 #  define _NSIG 128
54 # endif
55 #endif
56
57 static volatile sig_atomic_t signo[_NSIG];
58
59 static void handler(int);
60
61 char *
62 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
63 {
64         ssize_t nr;
65         int input, output, save_errno, i, need_restart;
66         char ch, *p, *end;
67         struct termios term, oterm;
68         struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
69         struct sigaction savetstp, savettin, savettou, savepipe;
70
71         /* I suppose we could alloc on demand in this case (XXX). */
72         if (bufsiz == 0) {
73                 errno = EINVAL;
74                 return(NULL);
75         }
76
77 restart:
78         for (i = 0; i < _NSIG; i++)
79                 signo[i] = 0;
80         nr = -1;
81         save_errno = 0;
82         need_restart = 0;
83         /*
84          * Read and write to /dev/tty if available.  If not, read from
85          * stdin and write to stderr unless a tty is required.
86          */
87         if ((flags & RPP_STDIN) ||
88             (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
89                 if (flags & RPP_REQUIRE_TTY) {
90                         errno = ENOTTY;
91                         return(NULL);
92                 }
93                 input = STDIN_FILENO;
94                 output = STDERR_FILENO;
95         }
96
97         /*
98          * Catch signals that would otherwise cause the user to end
99          * up with echo turned off in the shell.  Don't worry about
100          * things like SIGXCPU and SIGVTALRM for now.
101          */
102         sigemptyset(&sa.sa_mask);
103         sa.sa_flags = 0;                /* don't restart system calls */
104         sa.sa_handler = handler;
105         (void)sigaction(SIGALRM, &sa, &savealrm);
106         (void)sigaction(SIGHUP, &sa, &savehup);
107         (void)sigaction(SIGINT, &sa, &saveint);
108         (void)sigaction(SIGPIPE, &sa, &savepipe);
109         (void)sigaction(SIGQUIT, &sa, &savequit);
110         (void)sigaction(SIGTERM, &sa, &saveterm);
111         (void)sigaction(SIGTSTP, &sa, &savetstp);
112         (void)sigaction(SIGTTIN, &sa, &savettin);
113         (void)sigaction(SIGTTOU, &sa, &savettou);
114
115         /* Turn off echo if possible. */
116         if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
117                 memcpy(&term, &oterm, sizeof(term));
118                 if (!(flags & RPP_ECHO_ON))
119                         term.c_lflag &= ~(ECHO | ECHONL);
120 #ifdef VSTATUS
121                 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
122                         term.c_cc[VSTATUS] = _POSIX_VDISABLE;
123 #endif
124                 (void)tcsetattr(input, _T_FLUSH, &term);
125         } else {
126                 memset(&term, 0, sizeof(term));
127                 term.c_lflag |= ECHO;
128                 memset(&oterm, 0, sizeof(oterm));
129                 oterm.c_lflag |= ECHO;
130         }
131
132         /* No I/O if we are already backgrounded. */
133         if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
134                 if (!(flags & RPP_STDIN))
135                         (void)write(output, prompt, strlen(prompt));
136                 end = buf + bufsiz - 1;
137                 p = buf;
138                 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
139                         if (p < end) {
140                                 if ((flags & RPP_SEVENBIT))
141                                         ch &= 0x7f;
142                                 if (isalpha(ch)) {
143                                         if ((flags & RPP_FORCELOWER))
144                                                 ch = (char)tolower(ch);
145                                         if ((flags & RPP_FORCEUPPER))
146                                                 ch = (char)toupper(ch);
147                                 }
148                                 *p++ = ch;
149                         }
150                 }
151                 *p = '\0';
152                 save_errno = errno;
153                 if (!(term.c_lflag & ECHO))
154                         (void)write(output, "\n", 1);
155         }
156
157         /* Restore old terminal settings and signals. */
158         if (memcmp(&term, &oterm, sizeof(term)) != 0) {
159                 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
160                     errno == EINTR)
161                         continue;
162         }
163         (void)sigaction(SIGALRM, &savealrm, NULL);
164         (void)sigaction(SIGHUP, &savehup, NULL);
165         (void)sigaction(SIGINT, &saveint, NULL);
166         (void)sigaction(SIGQUIT, &savequit, NULL);
167         (void)sigaction(SIGPIPE, &savepipe, NULL);
168         (void)sigaction(SIGTERM, &saveterm, NULL);
169         (void)sigaction(SIGTSTP, &savetstp, NULL);
170         (void)sigaction(SIGTTIN, &savettin, NULL);
171         (void)sigaction(SIGTTOU, &savettou, NULL);
172         if (input != STDIN_FILENO)
173                 (void)close(input);
174
175         /*
176          * If we were interrupted by a signal, resend it to ourselves
177          * now that we have restored the signal handlers.
178          */
179         for (i = 0; i < _NSIG; i++) {
180                 if (signo[i]) {
181                         kill(getpid(), i);
182                         switch (i) {
183                         case SIGTSTP:
184                         case SIGTTIN:
185                         case SIGTTOU:
186                                 need_restart = 1;
187                         }
188                 }
189         }
190         if (need_restart)
191                 goto restart;
192
193         if (save_errno)
194                 errno = save_errno;
195         return(nr == -1 ? NULL : buf);
196 }
197
198 #if 0
199 char *
200 getpass(const char *prompt)
201 {
202         static char buf[_PASSWORD_LEN + 1];
203
204         return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
205 }
206 #endif
207
208 static void handler(int s)
209 {
210
211         signo[s] = 1;
212 }
213 #endif /* HAVE_READPASSPHRASE */