1 /* $OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $ */
4 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $
34 #ifndef HAVE_READPASSPHRASE
37 #include <readpassphrase.h>
40 # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
42 # define _T_FLUSH (TCSAFLUSH)
45 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
46 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
47 # define _POSIX_VDISABLE VDISABLE
50 static volatile sig_atomic_t signo;
52 static void handler(int);
55 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
58 int input, output, save_errno;
60 struct termios term, oterm;
61 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
62 struct sigaction savetstp, savettin, savettou, savepipe;
64 /* I suppose we could alloc on demand in this case (XXX). */
73 * Read and write to /dev/tty if available. If not, read from
74 * stdin and write to stderr unless a tty is required.
76 if ((flags & RPP_STDIN) ||
77 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
78 if (flags & RPP_REQUIRE_TTY) {
83 output = STDERR_FILENO;
87 * Catch signals that would otherwise cause the user to end
88 * up with echo turned off in the shell. Don't worry about
89 * things like SIGXCPU and SIGVTALRM for now.
91 sigemptyset(&sa.sa_mask);
92 sa.sa_flags = 0; /* don't restart system calls */
93 sa.sa_handler = handler;
94 (void)sigaction(SIGALRM, &sa, &savealrm);
95 (void)sigaction(SIGHUP, &sa, &savehup);
96 (void)sigaction(SIGINT, &sa, &saveint);
97 (void)sigaction(SIGPIPE, &sa, &savepipe);
98 (void)sigaction(SIGQUIT, &sa, &savequit);
99 (void)sigaction(SIGTERM, &sa, &saveterm);
100 (void)sigaction(SIGTSTP, &sa, &savetstp);
101 (void)sigaction(SIGTTIN, &sa, &savettin);
102 (void)sigaction(SIGTTOU, &sa, &savettou);
104 /* Turn off echo if possible. */
105 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
106 memcpy(&term, &oterm, sizeof(term));
107 if (!(flags & RPP_ECHO_ON))
108 term.c_lflag &= ~(ECHO | ECHONL);
110 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
111 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
113 (void)tcsetattr(input, _T_FLUSH, &term);
115 memset(&term, 0, sizeof(term));
116 term.c_lflag |= ECHO;
117 memset(&oterm, 0, sizeof(oterm));
118 oterm.c_lflag |= ECHO;
121 if (!(flags & RPP_STDIN))
122 (void)write(output, prompt, strlen(prompt));
123 end = buf + bufsiz - 1;
124 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
126 if ((flags & RPP_SEVENBIT))
129 if ((flags & RPP_FORCELOWER))
131 if ((flags & RPP_FORCEUPPER))
139 if (!(term.c_lflag & ECHO))
140 (void)write(output, "\n", 1);
142 /* Restore old terminal settings and signals. */
143 if (memcmp(&term, &oterm, sizeof(term)) != 0)
144 (void)tcsetattr(input, _T_FLUSH, &oterm);
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 if (input != STDIN_FILENO)
157 * If we were interrupted by a signal, resend it to ourselves
158 * now that we have restored the signal handlers.
161 kill(getpid(), signo);
171 return(nr == -1 ? NULL : buf);
176 getpass(const char *prompt)
178 static char buf[_PASSWORD_LEN + 1];
180 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
184 static void handler(int s)
188 #endif /* HAVE_READPASSPHRASE */