Import LibreSSL v2.4.2 to vendor branch
[dragonfly.git] / crypto / libressl / apps / nc / compat / readpassphrase.c
blobf3aa24868ec62c14e48988fe35571ff73c8b3786
1 /* $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $ */
3 /*
4 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
23 /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
25 #include <termios.h>
26 #include <signal.h>
27 #include <ctype.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <readpassphrase.h>
35 #ifndef _PATH_TTY
36 # define _PATH_TTY "/dev/tty"
37 #endif
39 #ifdef TCSASOFT
40 # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
41 #else
42 # define _T_FLUSH (TCSAFLUSH)
43 #endif
45 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
46 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
47 # define _POSIX_VDISABLE VDISABLE
48 #endif
50 #ifndef _NSIG
51 # ifdef NSIG
52 # define _NSIG NSIG
53 # else
54 # define _NSIG 128
55 # endif
56 #endif
58 static volatile sig_atomic_t signo[_NSIG];
60 static void handler(int);
62 char *
63 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
65 ssize_t bytes_written = 0;
66 ssize_t nr;
67 int input, output, save_errno, i, need_restart;
68 char ch, *p, *end;
69 struct termios term, oterm;
70 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
71 struct sigaction savetstp, savettin, savettou, savepipe;
73 /* I suppose we could alloc on demand in this case (XXX). */
74 if (bufsiz == 0) {
75 errno = EINVAL;
76 return(NULL);
79 restart:
80 for (i = 0; i < _NSIG; i++)
81 signo[i] = 0;
82 nr = -1;
83 save_errno = 0;
84 need_restart = 0;
86 * Read and write to /dev/tty if available. If not, read from
87 * stdin and write to stderr unless a tty is required.
89 if ((flags & RPP_STDIN) ||
90 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
91 if (flags & RPP_REQUIRE_TTY) {
92 errno = ENOTTY;
93 return(NULL);
95 input = STDIN_FILENO;
96 output = STDERR_FILENO;
100 * Catch signals that would otherwise cause the user to end
101 * up with echo turned off in the shell. Don't worry about
102 * things like SIGXCPU and SIGVTALRM for now.
104 sigemptyset(&sa.sa_mask);
105 sa.sa_flags = 0; /* don't restart system calls */
106 sa.sa_handler = handler;
107 (void)sigaction(SIGALRM, &sa, &savealrm);
108 (void)sigaction(SIGHUP, &sa, &savehup);
109 (void)sigaction(SIGINT, &sa, &saveint);
110 (void)sigaction(SIGPIPE, &sa, &savepipe);
111 (void)sigaction(SIGQUIT, &sa, &savequit);
112 (void)sigaction(SIGTERM, &sa, &saveterm);
113 (void)sigaction(SIGTSTP, &sa, &savetstp);
114 (void)sigaction(SIGTTIN, &sa, &savettin);
115 (void)sigaction(SIGTTOU, &sa, &savettou);
117 /* Turn off echo if possible. */
118 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
119 memcpy(&term, &oterm, sizeof(term));
120 if (!(flags & RPP_ECHO_ON))
121 term.c_lflag &= ~(ECHO | ECHONL);
122 #ifdef VSTATUS
123 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
124 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
125 #endif
126 (void)tcsetattr(input, _T_FLUSH, &term);
127 } else {
128 memset(&term, 0, sizeof(term));
129 term.c_lflag |= ECHO;
130 memset(&oterm, 0, sizeof(oterm));
131 oterm.c_lflag |= ECHO;
134 /* No I/O if we are already backgrounded. */
135 if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
136 if (!(flags & RPP_STDIN))
137 bytes_written = write(output, prompt, strlen(prompt));
138 end = buf + bufsiz - 1;
139 p = buf;
140 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
141 if (p < end) {
142 if ((flags & RPP_SEVENBIT))
143 ch &= 0x7f;
144 if (isalpha((unsigned char)ch)) {
145 if ((flags & RPP_FORCELOWER))
146 ch = (char)tolower((unsigned char)ch);
147 if ((flags & RPP_FORCEUPPER))
148 ch = (char)toupper((unsigned char)ch);
150 *p++ = ch;
153 *p = '\0';
154 save_errno = errno;
155 if (!(term.c_lflag & ECHO))
156 bytes_written = write(output, "\n", 1);
159 (void) bytes_written;
161 /* Restore old terminal settings and signals. */
162 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
163 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
164 errno == EINTR)
165 continue;
167 (void)sigaction(SIGALRM, &savealrm, NULL);
168 (void)sigaction(SIGHUP, &savehup, NULL);
169 (void)sigaction(SIGINT, &saveint, NULL);
170 (void)sigaction(SIGQUIT, &savequit, NULL);
171 (void)sigaction(SIGPIPE, &savepipe, NULL);
172 (void)sigaction(SIGTERM, &saveterm, NULL);
173 (void)sigaction(SIGTSTP, &savetstp, NULL);
174 (void)sigaction(SIGTTIN, &savettin, NULL);
175 (void)sigaction(SIGTTOU, &savettou, NULL);
176 if (input != STDIN_FILENO)
177 (void)close(input);
180 * If we were interrupted by a signal, resend it to ourselves
181 * now that we have restored the signal handlers.
183 for (i = 0; i < _NSIG; i++) {
184 if (signo[i]) {
185 kill(getpid(), i);
186 switch (i) {
187 case SIGTSTP:
188 case SIGTTIN:
189 case SIGTTOU:
190 need_restart = 1;
194 if (need_restart)
195 goto restart;
197 if (save_errno)
198 errno = save_errno;
199 return(nr == -1 ? NULL : buf);
202 static void handler(int s)
204 signo[s] = 1;