1*0Sstevel@tonic-gate /* $OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $ */
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate /*
4*0Sstevel@tonic-gate * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com>
5*0Sstevel@tonic-gate * All rights reserved.
6*0Sstevel@tonic-gate *
7*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
8*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions
9*0Sstevel@tonic-gate * are met:
10*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
11*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
12*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
14*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
15*0Sstevel@tonic-gate * 3. The name of the author may not be used to endorse or promote products
16*0Sstevel@tonic-gate * derived from this software without specific prior written permission.
17*0Sstevel@tonic-gate *
18*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19*0Sstevel@tonic-gate * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20*0Sstevel@tonic-gate * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21*0Sstevel@tonic-gate * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22*0Sstevel@tonic-gate * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23*0Sstevel@tonic-gate * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24*0Sstevel@tonic-gate * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25*0Sstevel@tonic-gate * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0Sstevel@tonic-gate * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27*0Sstevel@tonic-gate * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0Sstevel@tonic-gate */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
31*0Sstevel@tonic-gate static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.14 2002/06/28 01:43:58 millert Exp $";
32*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include "includes.h"
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate #ifndef HAVE_READPASSPHRASE
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #include <termios.h>
39*0Sstevel@tonic-gate #include <readpassphrase.h>
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #ifdef TCSASOFT
42*0Sstevel@tonic-gate # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
43*0Sstevel@tonic-gate #else
44*0Sstevel@tonic-gate # define _T_FLUSH (TCSAFLUSH)
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
48*0Sstevel@tonic-gate #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
49*0Sstevel@tonic-gate # define _POSIX_VDISABLE VDISABLE
50*0Sstevel@tonic-gate #endif
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate static volatile sig_atomic_t signo;
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate static void handler(int);
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate char *
readpassphrase(const char * prompt,char * buf,size_t bufsiz,int flags)57*0Sstevel@tonic-gate readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate ssize_t nr;
60*0Sstevel@tonic-gate int input, output, save_errno;
61*0Sstevel@tonic-gate char ch, *p, *end;
62*0Sstevel@tonic-gate struct termios term, oterm;
63*0Sstevel@tonic-gate struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
64*0Sstevel@tonic-gate struct sigaction savetstp, savettin, savettou, savepipe;
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate /* I suppose we could alloc on demand in this case (XXX). */
67*0Sstevel@tonic-gate if (bufsiz == 0) {
68*0Sstevel@tonic-gate errno = EINVAL;
69*0Sstevel@tonic-gate return(NULL);
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate restart:
73*0Sstevel@tonic-gate signo = 0;
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate * Read and write to /dev/tty if available. If not, read from
76*0Sstevel@tonic-gate * stdin and write to stderr unless a tty is required.
77*0Sstevel@tonic-gate */
78*0Sstevel@tonic-gate if ((flags & RPP_STDIN) ||
79*0Sstevel@tonic-gate (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
80*0Sstevel@tonic-gate if (flags & RPP_REQUIRE_TTY) {
81*0Sstevel@tonic-gate errno = ENOTTY;
82*0Sstevel@tonic-gate return(NULL);
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate input = STDIN_FILENO;
85*0Sstevel@tonic-gate output = STDERR_FILENO;
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate * Catch signals that would otherwise cause the user to end
90*0Sstevel@tonic-gate * up with echo turned off in the shell. Don't worry about
91*0Sstevel@tonic-gate * things like SIGXCPU and SIGVTALRM for now.
92*0Sstevel@tonic-gate */
93*0Sstevel@tonic-gate sigemptyset(&sa.sa_mask);
94*0Sstevel@tonic-gate sa.sa_flags = 0; /* don't restart system calls */
95*0Sstevel@tonic-gate sa.sa_handler = handler;
96*0Sstevel@tonic-gate (void)sigaction(SIGALRM, &sa, &savealrm);
97*0Sstevel@tonic-gate (void)sigaction(SIGHUP, &sa, &savehup);
98*0Sstevel@tonic-gate (void)sigaction(SIGINT, &sa, &saveint);
99*0Sstevel@tonic-gate (void)sigaction(SIGPIPE, &sa, &savepipe);
100*0Sstevel@tonic-gate (void)sigaction(SIGQUIT, &sa, &savequit);
101*0Sstevel@tonic-gate (void)sigaction(SIGTERM, &sa, &saveterm);
102*0Sstevel@tonic-gate (void)sigaction(SIGTSTP, &sa, &savetstp);
103*0Sstevel@tonic-gate (void)sigaction(SIGTTIN, &sa, &savettin);
104*0Sstevel@tonic-gate (void)sigaction(SIGTTOU, &sa, &savettou);
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate /* Turn off echo if possible. */
107*0Sstevel@tonic-gate if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
108*0Sstevel@tonic-gate memcpy(&term, &oterm, sizeof(term));
109*0Sstevel@tonic-gate if (!(flags & RPP_ECHO_ON))
110*0Sstevel@tonic-gate term.c_lflag &= ~(ECHO | ECHONL);
111*0Sstevel@tonic-gate #ifdef VSTATUS
112*0Sstevel@tonic-gate if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
113*0Sstevel@tonic-gate term.c_cc[VSTATUS] = _POSIX_VDISABLE;
114*0Sstevel@tonic-gate #endif
115*0Sstevel@tonic-gate (void)tcsetattr(input, _T_FLUSH, &term);
116*0Sstevel@tonic-gate } else {
117*0Sstevel@tonic-gate memset(&term, 0, sizeof(term));
118*0Sstevel@tonic-gate term.c_lflag |= ECHO;
119*0Sstevel@tonic-gate memset(&oterm, 0, sizeof(oterm));
120*0Sstevel@tonic-gate oterm.c_lflag |= ECHO;
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate if (!(flags & RPP_STDIN))
124*0Sstevel@tonic-gate (void)write(output, prompt, strlen(prompt));
125*0Sstevel@tonic-gate end = buf + bufsiz - 1;
126*0Sstevel@tonic-gate for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
127*0Sstevel@tonic-gate if (p < end) {
128*0Sstevel@tonic-gate if ((flags & RPP_SEVENBIT))
129*0Sstevel@tonic-gate ch &= 0x7f;
130*0Sstevel@tonic-gate if (isalpha(ch)) {
131*0Sstevel@tonic-gate if ((flags & RPP_FORCELOWER))
132*0Sstevel@tonic-gate ch = tolower(ch);
133*0Sstevel@tonic-gate if ((flags & RPP_FORCEUPPER))
134*0Sstevel@tonic-gate ch = toupper(ch);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate *p++ = ch;
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate *p = '\0';
140*0Sstevel@tonic-gate save_errno = errno;
141*0Sstevel@tonic-gate if (!(term.c_lflag & ECHO))
142*0Sstevel@tonic-gate (void)write(output, "\n", 1);
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate /* Restore old terminal settings and signals. */
145*0Sstevel@tonic-gate if (memcmp(&term, &oterm, sizeof(term)) != 0)
146*0Sstevel@tonic-gate (void)tcsetattr(input, _T_FLUSH, &oterm);
147*0Sstevel@tonic-gate (void)sigaction(SIGALRM, &savealrm, NULL);
148*0Sstevel@tonic-gate (void)sigaction(SIGHUP, &savehup, NULL);
149*0Sstevel@tonic-gate (void)sigaction(SIGINT, &saveint, NULL);
150*0Sstevel@tonic-gate (void)sigaction(SIGQUIT, &savequit, NULL);
151*0Sstevel@tonic-gate (void)sigaction(SIGPIPE, &savepipe, NULL);
152*0Sstevel@tonic-gate (void)sigaction(SIGTERM, &saveterm, NULL);
153*0Sstevel@tonic-gate (void)sigaction(SIGTSTP, &savetstp, NULL);
154*0Sstevel@tonic-gate (void)sigaction(SIGTTIN, &savettin, NULL);
155*0Sstevel@tonic-gate if (input != STDIN_FILENO)
156*0Sstevel@tonic-gate (void)close(input);
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate * If we were interrupted by a signal, resend it to ourselves
160*0Sstevel@tonic-gate * now that we have restored the signal handlers.
161*0Sstevel@tonic-gate */
162*0Sstevel@tonic-gate if (signo) {
163*0Sstevel@tonic-gate kill(getpid(), signo);
164*0Sstevel@tonic-gate switch (signo) {
165*0Sstevel@tonic-gate case SIGTSTP:
166*0Sstevel@tonic-gate case SIGTTIN:
167*0Sstevel@tonic-gate case SIGTTOU:
168*0Sstevel@tonic-gate goto restart;
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate errno = save_errno;
173*0Sstevel@tonic-gate return(nr == -1 ? NULL : buf);
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate #if 0
177*0Sstevel@tonic-gate char *
178*0Sstevel@tonic-gate getpass(const char *prompt)
179*0Sstevel@tonic-gate {
180*0Sstevel@tonic-gate static char buf[_PASSWORD_LEN + 1];
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate #endif
185*0Sstevel@tonic-gate
handler(int s)186*0Sstevel@tonic-gate static void handler(int s)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate signo = s;
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate #endif /* HAVE_READPASSPHRASE */
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
193