1 /* $OpenBSD: readpassphrase.c,v 1.23 2010/05/14 13:30:34 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 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include "namespace.h"
37 #include <readpassphrase.h>
38 #include "un-namespace.h"
40 static volatile sig_atomic_t signo[NSIG];
42 static void handler(int);
45 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
48 int input, output, save_errno, i, need_restart;
50 struct termios term, oterm;
51 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
52 struct sigaction savetstp, savettin, savettou, savepipe;
54 /* I suppose we could alloc on demand in this case (XXX). */
61 for (i = 0; i < NSIG; i++)
67 * Read and write to /dev/tty if available. If not, read from
68 * stdin and write to stderr unless a tty is required.
70 if ((flags & RPP_STDIN) ||
71 (input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC)) == -1) {
72 if (flags & RPP_REQUIRE_TTY) {
77 output = STDERR_FILENO;
81 * Turn off echo if possible.
82 * If we are using a tty but are not the foreground pgrp this will
83 * generate SIGTTOU, so do it *before* installing the signal handlers.
85 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
86 memcpy(&term, &oterm, sizeof(term));
87 if (!(flags & RPP_ECHO_ON))
88 term.c_lflag &= ~(ECHO | ECHONL);
89 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
90 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
91 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
93 memset(&term, 0, sizeof(term));
95 memset(&oterm, 0, sizeof(oterm));
96 oterm.c_lflag |= ECHO;
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 if (!(flags & RPP_STDIN))
118 (void)_write(output, prompt, strlen(prompt));
119 end = buf + bufsiz - 1;
121 while ((nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
123 if ((flags & RPP_SEVENBIT))
126 if ((flags & RPP_FORCELOWER))
127 ch = (char)tolower(ch);
128 if ((flags & RPP_FORCEUPPER))
129 ch = (char)toupper(ch);
136 if (!(term.c_lflag & ECHO))
137 (void)_write(output, "\n", 1);
139 /* Restore old terminal settings and signals. */
140 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
141 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
142 errno == EINTR && !signo[SIGTTOU])
145 (void)_sigaction(SIGALRM, &savealrm, NULL);
146 (void)_sigaction(SIGHUP, &savehup, NULL);
147 (void)_sigaction(SIGINT, &saveint, NULL);
148 (void)_sigaction(SIGQUIT, &savequit, NULL);
149 (void)_sigaction(SIGPIPE, &savepipe, NULL);
150 (void)_sigaction(SIGTERM, &saveterm, NULL);
151 (void)_sigaction(SIGTSTP, &savetstp, NULL);
152 (void)_sigaction(SIGTTIN, &savettin, NULL);
153 (void)_sigaction(SIGTTOU, &savettou, NULL);
154 if (input != STDIN_FILENO)
158 * If we were interrupted by a signal, resend it to ourselves
159 * now that we have restored the signal handlers.
161 for (i = 0; i < NSIG; i++) {
177 return(nr == -1 ? NULL : buf);
181 getpass(const char *prompt)
183 static char buf[_PASSWORD_LEN + 1];
185 if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
190 static void handler(int s)