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
39 #include "sys-readpassphrase.h"
42 #define _PATH_TTY "/dev/tty"
46 /* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
50 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
51 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
52 # define _POSIX_VDISABLE VDISABLE
55 static volatile sig_atomic_t signo[_NSIG];
57 static void handler(int);
60 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
63 int input, output, save_errno, i, need_restart;
65 struct termios term, oterm;
66 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
67 struct sigaction savetstp, savettin, savettou, savepipe;
69 /* I suppose we could alloc on demand in this case (XXX). */
76 for (i = 0; i < _NSIG; i++)
82 * Read and write to /dev/tty if available. If not, read from
83 * stdin and write to stderr unless a tty is required.
85 if ((flags & RPP_STDIN) ||
86 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
87 if (flags & RPP_REQUIRE_TTY) {
92 output = STDERR_FILENO;
96 * Turn off echo if possible.
97 * If we are using a tty but are not the foreground pgrp this will
98 * generate SIGTTOU, so do it *before* installing the signal handlers.
100 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
101 memcpy(&term, &oterm, sizeof(term));
102 if (!(flags & RPP_ECHO_ON))
103 term.c_lflag &= ~(ECHO | ECHONL);
105 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
106 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
108 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
110 memset(&term, 0, sizeof(term));
111 term.c_lflag |= ECHO;
112 memset(&oterm, 0, sizeof(oterm));
113 oterm.c_lflag |= ECHO;
117 * Catch signals that would otherwise cause the user to end
118 * up with echo turned off in the shell. Don't worry about
119 * things like SIGXCPU and SIGVTALRM for now.
121 sigemptyset(&sa.sa_mask);
122 sa.sa_flags = 0; /* don't restart system calls */
123 sa.sa_handler = handler;
124 (void)sigaction(SIGALRM, &sa, &savealrm);
125 (void)sigaction(SIGHUP, &sa, &savehup);
126 (void)sigaction(SIGINT, &sa, &saveint);
127 (void)sigaction(SIGPIPE, &sa, &savepipe);
128 (void)sigaction(SIGQUIT, &sa, &savequit);
129 (void)sigaction(SIGTERM, &sa, &saveterm);
130 (void)sigaction(SIGTSTP, &sa, &savetstp);
131 (void)sigaction(SIGTTIN, &sa, &savettin);
132 (void)sigaction(SIGTTOU, &sa, &savettou);
134 if (!(flags & RPP_STDIN))
135 (void)write(output, prompt, strlen(prompt));
136 end = buf + bufsiz - 1;
138 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
140 if ((flags & RPP_SEVENBIT))
142 if (isalpha((unsigned char)ch)) {
143 if ((flags & RPP_FORCELOWER))
144 ch = (char)tolower((unsigned char)ch);
145 if ((flags & RPP_FORCEUPPER))
146 ch = (char)toupper((unsigned char)ch);
153 if (!(term.c_lflag & ECHO))
154 (void)write(output, "\n", 1);
156 /* Restore old terminal settings and signals. */
157 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
158 const int sigttou = signo[SIGTTOU];
160 /* Ignore SIGTTOU generated when we are not the fg pgrp. */
161 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
162 errno == EINTR && !signo[SIGTTOU])
164 signo[SIGTTOU] = sigttou;
166 (void)sigaction(SIGALRM, &savealrm, NULL);
167 (void)sigaction(SIGHUP, &savehup, NULL);
168 (void)sigaction(SIGINT, &saveint, NULL);
169 (void)sigaction(SIGQUIT, &savequit, NULL);
170 (void)sigaction(SIGPIPE, &savepipe, NULL);
171 (void)sigaction(SIGTERM, &saveterm, NULL);
172 (void)sigaction(SIGTSTP, &savetstp, NULL);
173 (void)sigaction(SIGTTIN, &savettin, NULL);
174 (void)sigaction(SIGTTOU, &savettou, NULL);
175 if (input != STDIN_FILENO)
179 * If we were interrupted by a signal, resend it to ourselves
180 * now that we have restored the signal handlers.
182 for (i = 0; i < _NSIG; i++) {
198 return(nr == -1 ? NULL : buf);
203 getpass(const char *prompt)
205 static char buf[_PASSWORD_LEN + 1];
207 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
211 static void handler(int s)
216 #endif /* HAVE_READPASSPHRASE */