1 /* $OpenBSD: readpassphrase.c,v 1.16 2003/06/17 21:56:23 millert Exp $ */
4 * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Sponsored in part by the Defense Advanced Research Projects
19 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 #if defined(LIBC_SCCS) && !defined(lint)
24 static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.16 2003/06/17 21:56:23 millert Exp $";
25 #endif /* LIBC_SCCS and not lint */
29 #ifndef HAVE_READPASSPHRASE
32 #include <readpassphrase.h>
35 # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
37 # define _T_FLUSH (TCSAFLUSH)
40 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
41 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
42 # define _POSIX_VDISABLE VDISABLE
45 static volatile sig_atomic_t signo;
47 static void handler(int);
50 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
53 int input, output, save_errno;
55 struct termios term, oterm;
56 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
57 struct sigaction savetstp, savettin, savettou, savepipe;
59 /* I suppose we could alloc on demand in this case (XXX). */
68 * Read and write to /dev/tty if available. If not, read from
69 * stdin and write to stderr unless a tty is required.
71 if ((flags & RPP_STDIN) ||
72 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
73 if (flags & RPP_REQUIRE_TTY) {
78 output = STDERR_FILENO;
82 * Catch signals that would otherwise cause the user to end
83 * up with echo turned off in the shell. Don't worry about
84 * things like SIGXCPU and SIGVTALRM for now.
86 sigemptyset(&sa.sa_mask);
87 sa.sa_flags = 0; /* don't restart system calls */
88 sa.sa_handler = handler;
89 (void)sigaction(SIGALRM, &sa, &savealrm);
90 (void)sigaction(SIGHUP, &sa, &savehup);
91 (void)sigaction(SIGINT, &sa, &saveint);
92 (void)sigaction(SIGPIPE, &sa, &savepipe);
93 (void)sigaction(SIGQUIT, &sa, &savequit);
94 (void)sigaction(SIGTERM, &sa, &saveterm);
95 (void)sigaction(SIGTSTP, &sa, &savetstp);
96 (void)sigaction(SIGTTIN, &sa, &savettin);
97 (void)sigaction(SIGTTOU, &sa, &savettou);
99 /* Turn off echo if possible. */
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, _T_FLUSH, &term);
110 memset(&term, 0, sizeof(term));
111 term.c_lflag |= ECHO;
112 memset(&oterm, 0, sizeof(oterm));
113 oterm.c_lflag |= ECHO;
116 if (!(flags & RPP_STDIN))
117 (void)write(output, prompt, strlen(prompt));
118 end = buf + bufsiz - 1;
119 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
121 if ((flags & RPP_SEVENBIT))
124 if ((flags & RPP_FORCELOWER))
126 if ((flags & RPP_FORCEUPPER))
134 if (!(term.c_lflag & ECHO))
135 (void)write(output, "\n", 1);
137 /* Restore old terminal settings and signals. */
138 if (memcmp(&term, &oterm, sizeof(term)) != 0)
139 (void)tcsetattr(input, _T_FLUSH, &oterm);
140 (void)sigaction(SIGALRM, &savealrm, NULL);
141 (void)sigaction(SIGHUP, &savehup, NULL);
142 (void)sigaction(SIGINT, &saveint, NULL);
143 (void)sigaction(SIGQUIT, &savequit, NULL);
144 (void)sigaction(SIGPIPE, &savepipe, NULL);
145 (void)sigaction(SIGTERM, &saveterm, NULL);
146 (void)sigaction(SIGTSTP, &savetstp, NULL);
147 (void)sigaction(SIGTTIN, &savettin, NULL);
148 if (input != STDIN_FILENO)
152 * If we were interrupted by a signal, resend it to ourselves
153 * now that we have restored the signal handlers.
156 kill(getpid(), signo);
166 return(nr == -1 ? NULL : buf);
171 getpass(const char *prompt)
173 static char buf[_PASSWORD_LEN + 1];
175 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
179 static void handler(int s)
184 #endif /* HAVE_READPASSPHRASE */