2 * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if defined(LIBC_SCCS) && !defined(lint)
29 static char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.5 2001/06/27 13:23:30 djm Exp $";
30 #endif /* LIBC_SCCS and not lint */
34 #ifndef HAVE_READPASSPHRASE
37 #include <readpassphrase.h>
40 # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
42 # define _T_FLUSH (TCSAFLUSH)
46 readpassphrase(prompt, buf, bufsiz, flags)
54 #ifdef _POSIX_VDISABLE
57 int echo, input, output;
60 /* I suppose we could alloc on demand in this case (XXX). */
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 ((input = output = open(_PATH_TTY, O_RDWR)) == -1) {
71 if (flags & RPP_REQUIRE_TTY) {
76 output = STDERR_FILENO;
80 * We block SIGINT and SIGTSTP so the terminal is not left
81 * in an inconsistent state (ie: no echo). It would probably
82 * be better to simply catch these though.
85 sigaddset(&nset, SIGINT);
86 sigaddset(&nset, SIGTSTP);
87 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
89 /* Turn off echo if possible. */
91 #ifdef _POSIX_VDISABLE
92 status = _POSIX_VDISABLE;
94 if (tcgetattr(input, &term) == 0) {
95 if (!(flags & RPP_ECHO_ON) && (term.c_lflag & ECHO)) {
97 term.c_lflag &= ~ECHO;
100 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) {
101 status = term.c_cc[VSTATUS];
102 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
105 (void)tcsetattr(input, _T_FLUSH, &term);
107 if (!(flags & RPP_ECHO_ON)) {
108 if (tcgetattr(input, &term) == 0 && (term.c_lflag & ECHO)) {
110 term.c_lflag &= ~ECHO;
111 (void)tcsetattr(input, _T_FLUSH, &term);
115 (void)write(output, prompt, strlen(prompt));
116 end = buf + bufsiz - 1;
117 for (p = buf; read(input, &ch, 1) == 1 && ch != '\n' && ch != '\r';) {
119 if ((flags & RPP_SEVENBIT))
122 if ((flags & RPP_FORCELOWER))
124 if ((flags & RPP_FORCEUPPER))
131 #ifdef _POSIX_VDISABLE
132 if (echo || status != _POSIX_VDISABLE) {
137 (void)write(output, "\n", 1);
138 term.c_lflag |= ECHO;
141 if (status != _POSIX_VDISABLE)
142 term.c_cc[VSTATUS] = status;
144 (void)tcsetattr(input, _T_FLUSH, &term);
146 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
147 if (input != STDIN_FILENO)
151 #endif /* HAVE_READPASSPHRASE */
158 static char buf[_PASSWORD_LEN + 1];
160 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));