1 /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
3 /* $OpenBSD: readpassphrase.c,v 1.16 2003/06/17 21:56:23 millert Exp $ */
6 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Sponsored in part by the Defense Advanced Research Projects
21 * Agency (DARPA) and Air Force Research Laboratory, Air Force
22 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
25 #if defined(LIBC_SCCS) && !defined(lint)
26 static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.16 2003/06/17 21:56:23 millert Exp $";
27 #endif /* LIBC_SCCS and not lint */
31 #ifndef HAVE_READPASSPHRASE
34 #include <readpassphrase.h>
37 # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
39 # define _T_FLUSH (TCSAFLUSH)
42 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
43 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
44 # define _POSIX_VDISABLE VDISABLE
47 static volatile sig_atomic_t signo;
49 static void handler(int);
52 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
55 int input, output, save_errno;
57 struct termios term, oterm;
58 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
59 struct sigaction savetstp, savettin, savettou, savepipe;
61 /* I suppose we could alloc on demand in this case (XXX). */
70 * Read and write to /dev/tty if available. If not, read from
71 * stdin and write to stderr unless a tty is required.
73 if ((flags & RPP_STDIN) ||
74 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
75 if (flags & RPP_REQUIRE_TTY) {
80 output = STDERR_FILENO;
84 * Catch signals that would otherwise cause the user to end
85 * up with echo turned off in the shell. Don't worry about
86 * things like SIGXCPU and SIGVTALRM for now.
88 sigemptyset(&sa.sa_mask);
89 sa.sa_flags = 0; /* don't restart system calls */
90 sa.sa_handler = handler;
91 (void)sigaction(SIGALRM, &sa, &savealrm);
92 (void)sigaction(SIGHUP, &sa, &savehup);
93 (void)sigaction(SIGINT, &sa, &saveint);
94 (void)sigaction(SIGPIPE, &sa, &savepipe);
95 (void)sigaction(SIGQUIT, &sa, &savequit);
96 (void)sigaction(SIGTERM, &sa, &saveterm);
97 (void)sigaction(SIGTSTP, &sa, &savetstp);
98 (void)sigaction(SIGTTIN, &sa, &savettin);
99 (void)sigaction(SIGTTOU, &sa, &savettou);
101 /* Turn off echo if possible. */
102 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
103 memcpy(&term, &oterm, sizeof(term));
104 if (!(flags & RPP_ECHO_ON))
105 term.c_lflag &= ~(ECHO | ECHONL);
107 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
108 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
110 (void)tcsetattr(input, _T_FLUSH, &term);
112 memset(&term, 0, sizeof(term));
113 term.c_lflag |= ECHO;
114 memset(&oterm, 0, sizeof(oterm));
115 oterm.c_lflag |= ECHO;
118 if (!(flags & RPP_STDIN))
119 (void)write(output, prompt, strlen(prompt));
120 end = buf + bufsiz - 1;
121 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
123 if ((flags & RPP_SEVENBIT))
126 if ((flags & RPP_FORCELOWER))
128 if ((flags & RPP_FORCEUPPER))
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, _T_FLUSH, &oterm) == -1 &&
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)
189 #endif /* HAVE_READPASSPHRASE */