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)
45 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
46 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
47 # define _POSIX_VDISABLE VDISABLE
51 readpassphrase(prompt, buf, bufsiz, flags)
59 #ifdef _POSIX_VDISABLE
62 int echo, input, output;
65 /* I suppose we could alloc on demand in this case (XXX). */
72 * Read and write to /dev/tty if available. If not, read from
73 * stdin and write to stderr unless a tty is required.
75 if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) {
76 if (flags & RPP_REQUIRE_TTY) {
81 output = STDERR_FILENO;
85 * We block SIGINT and SIGTSTP so the terminal is not left
86 * in an inconsistent state (ie: no echo). It would probably
87 * be better to simply catch these though.
90 sigaddset(&nset, SIGINT);
91 sigaddset(&nset, SIGTSTP);
92 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
94 /* Turn off echo if possible. */
96 #ifdef _POSIX_VDISABLE
97 status = _POSIX_VDISABLE;
99 if (tcgetattr(input, &term) == 0) {
100 if (!(flags & RPP_ECHO_ON) && (term.c_lflag & ECHO)) {
102 term.c_lflag &= ~ECHO;
105 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) {
106 status = term.c_cc[VSTATUS];
107 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
110 (void)tcsetattr(input, _T_FLUSH, &term);
112 if (!(flags & RPP_ECHO_ON)) {
113 if (tcgetattr(input, &term) == 0 && (term.c_lflag & ECHO)) {
115 term.c_lflag &= ~ECHO;
116 (void)tcsetattr(input, _T_FLUSH, &term);
120 (void)write(output, prompt, strlen(prompt));
121 end = buf + bufsiz - 1;
122 for (p = buf; read(input, &ch, 1) == 1 && ch != '\n' && ch != '\r';) {
124 if ((flags & RPP_SEVENBIT))
127 if ((flags & RPP_FORCELOWER))
129 if ((flags & RPP_FORCEUPPER))
136 #ifdef _POSIX_VDISABLE
137 if (echo || status != _POSIX_VDISABLE) {
142 (void)write(output, "\n", 1);
143 term.c_lflag |= ECHO;
146 if (status != _POSIX_VDISABLE)
147 term.c_cc[VSTATUS] = status;
149 (void)tcsetattr(input, _T_FLUSH, &term);
151 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
152 if (input != STDIN_FILENO)
156 #endif /* HAVE_READPASSPHRASE */
163 static char buf[_PASSWORD_LEN + 1];
165 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));