1 /* $OpenBSD: readpassphrase.c,v 1.24 2013/11/24 23:51:29 deraadt 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 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include "namespace.h"
37 #include <readpassphrase.h>
38 #include "un-namespace.h"
39 #include "libc_private.h"
41 static volatile sig_atomic_t signo[NSIG];
43 static void handler(int);
46 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
49 int input, output, save_errno, i, need_restart, input_is_tty;
51 struct termios term, oterm;
52 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
53 struct sigaction savetstp, savettin, savettou, savepipe;
55 /* I suppose we could alloc on demand in this case (XXX). */
62 for (i = 0; i < NSIG; i++)
68 * Read and write to /dev/tty if available. If not, read from
69 * stdin and write to stderr unless a tty is required.
72 if (!(flags & RPP_STDIN)) {
73 input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC);
75 if (flags & RPP_REQUIRE_TTY) {
80 output = STDERR_FILENO;
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_is_tty && tcgetattr(input, &oterm) == 0) {
95 memcpy(&term, &oterm, sizeof(term));
96 if (!(flags & RPP_ECHO_ON))
97 term.c_lflag &= ~(ECHO | ECHONL);
98 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
99 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
100 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
102 memset(&term, 0, sizeof(term));
103 term.c_lflag |= ECHO;
104 memset(&oterm, 0, sizeof(oterm));
105 oterm.c_lflag |= ECHO;
109 * Catch signals that would otherwise cause the user to end
110 * up with echo turned off in the shell. Don't worry about
111 * things like SIGXCPU and SIGVTALRM for now.
113 sigemptyset(&sa.sa_mask);
114 sa.sa_flags = 0; /* don't restart system calls */
115 sa.sa_handler = handler;
116 (void)__libc_sigaction(SIGALRM, &sa, &savealrm);
117 (void)__libc_sigaction(SIGHUP, &sa, &savehup);
118 (void)__libc_sigaction(SIGINT, &sa, &saveint);
119 (void)__libc_sigaction(SIGPIPE, &sa, &savepipe);
120 (void)__libc_sigaction(SIGQUIT, &sa, &savequit);
121 (void)__libc_sigaction(SIGTERM, &sa, &saveterm);
122 (void)__libc_sigaction(SIGTSTP, &sa, &savetstp);
123 (void)__libc_sigaction(SIGTTIN, &sa, &savettin);
124 (void)__libc_sigaction(SIGTTOU, &sa, &savettou);
126 if (!(flags & RPP_STDIN))
127 (void)_write(output, prompt, strlen(prompt));
128 end = buf + bufsiz - 1;
130 while ((nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
132 if ((flags & RPP_SEVENBIT))
134 if (isalpha((unsigned char)ch)) {
135 if ((flags & RPP_FORCELOWER))
136 ch = (char)tolower((unsigned char)ch);
137 if ((flags & RPP_FORCEUPPER))
138 ch = (char)toupper((unsigned char)ch);
145 if (!(term.c_lflag & ECHO))
146 (void)_write(output, "\n", 1);
148 /* Restore old terminal settings and signals. */
149 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
150 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
151 errno == EINTR && !signo[SIGTTOU])
154 (void)__libc_sigaction(SIGALRM, &savealrm, NULL);
155 (void)__libc_sigaction(SIGHUP, &savehup, NULL);
156 (void)__libc_sigaction(SIGINT, &saveint, NULL);
157 (void)__libc_sigaction(SIGQUIT, &savequit, NULL);
158 (void)__libc_sigaction(SIGPIPE, &savepipe, NULL);
159 (void)__libc_sigaction(SIGTERM, &saveterm, NULL);
160 (void)__libc_sigaction(SIGTSTP, &savetstp, NULL);
161 (void)__libc_sigaction(SIGTTIN, &savettin, NULL);
162 (void)__libc_sigaction(SIGTTOU, &savettou, NULL);
167 * If we were interrupted by a signal, resend it to ourselves
168 * now that we have restored the signal handlers.
170 for (i = 0; i < NSIG; i++) {
186 return(nr == -1 ? NULL : buf);
190 getpass(const char *prompt)
192 static char buf[_PASSWORD_LEN + 1];
194 if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
199 static void handler(int s)