1 /* $OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 millert Exp $ */
4 * Copyright (c) 2000-2002, 2007, 2010
5 * Todd C. Miller <Todd.Miller@courtesan.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
28 #ifndef HAVE_READPASSPHRASE
34 #include <readpassphrase.h>
40 /* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
44 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46 # define _POSIX_VDISABLE VDISABLE
49 static volatile sig_atomic_t signo[_NSIG];
51 static void handler(int);
54 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
57 int input, output, save_errno, i, need_restart;
59 struct termios term, oterm;
60 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
61 struct sigaction savetstp, savettin, savettou, savepipe;
63 /* I suppose we could alloc on demand in this case (XXX). */
70 for (i = 0; i < _NSIG; i++)
76 * Read and write to /dev/tty if available. If not, read from
77 * stdin and write to stderr unless a tty is required.
79 if ((flags & RPP_STDIN) ||
80 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
81 if (flags & RPP_REQUIRE_TTY) {
86 output = STDERR_FILENO;
90 * Turn off echo if possible.
91 * If we are using a tty but are not the foreground pgrp this will
92 * generate SIGTTOU, so do it *before* installing the signal handlers.
94 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
95 memcpy(&term, &oterm, sizeof(term));
96 if (!(flags & RPP_ECHO_ON))
97 term.c_lflag &= ~(ECHO | ECHONL);
99 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
100 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
102 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
104 memset(&term, 0, sizeof(term));
105 term.c_lflag |= ECHO;
106 memset(&oterm, 0, sizeof(oterm));
107 oterm.c_lflag |= ECHO;
111 * Catch signals that would otherwise cause the user to end
112 * up with echo turned off in the shell. Don't worry about
113 * things like SIGXCPU and SIGVTALRM for now.
115 sigemptyset(&sa.sa_mask);
116 sa.sa_flags = 0; /* don't restart system calls */
117 sa.sa_handler = handler;
118 (void)sigaction(SIGALRM, &sa, &savealrm);
119 (void)sigaction(SIGHUP, &sa, &savehup);
120 (void)sigaction(SIGINT, &sa, &saveint);
121 (void)sigaction(SIGPIPE, &sa, &savepipe);
122 (void)sigaction(SIGQUIT, &sa, &savequit);
123 (void)sigaction(SIGTERM, &sa, &saveterm);
124 (void)sigaction(SIGTSTP, &sa, &savetstp);
125 (void)sigaction(SIGTTIN, &sa, &savettin);
126 (void)sigaction(SIGTTOU, &sa, &savettou);
128 if (!(flags & RPP_STDIN))
129 (void)write(output, prompt, strlen(prompt));
130 end = buf + bufsiz - 1;
132 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
134 if ((flags & RPP_SEVENBIT))
136 if (isalpha((unsigned char)ch)) {
137 if ((flags & RPP_FORCELOWER))
138 ch = (char)tolower((unsigned char)ch);
139 if ((flags & RPP_FORCEUPPER))
140 ch = (char)toupper((unsigned char)ch);
147 if (!(term.c_lflag & ECHO))
148 (void)write(output, "\n", 1);
150 /* Restore old terminal settings and signals. */
151 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
152 const int sigttou = signo[SIGTTOU];
154 /* Ignore SIGTTOU generated when we are not the fg pgrp. */
155 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
156 errno == EINTR && !signo[SIGTTOU])
158 signo[SIGTTOU] = sigttou;
160 (void)sigaction(SIGALRM, &savealrm, NULL);
161 (void)sigaction(SIGHUP, &savehup, NULL);
162 (void)sigaction(SIGINT, &saveint, NULL);
163 (void)sigaction(SIGQUIT, &savequit, NULL);
164 (void)sigaction(SIGPIPE, &savepipe, NULL);
165 (void)sigaction(SIGTERM, &saveterm, NULL);
166 (void)sigaction(SIGTSTP, &savetstp, NULL);
167 (void)sigaction(SIGTTIN, &savettin, NULL);
168 (void)sigaction(SIGTTOU, &savettou, NULL);
169 if (input != STDIN_FILENO)
173 * If we were interrupted by a signal, resend it to ourselves
174 * now that we have restored the signal handlers.
176 for (i = 0; i < _NSIG; i++) {
192 return(nr == -1 ? NULL : buf);
194 DEF_WEAK(readpassphrase);
198 getpass(const char *prompt)
200 static char buf[_PASSWORD_LEN + 1];
202 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
206 static void handler(int s)
211 #endif /* HAVE_READPASSPHRASE */