1*9a1d0467SLeorize /* $OpenBSD: readpassphrase.c,v 1.20 2007/10/30 12:03:48 millert Exp $ */
2*9a1d0467SLeorize
3*9a1d0467SLeorize /*
4*9a1d0467SLeorize * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5*9a1d0467SLeorize *
6*9a1d0467SLeorize * Permission to use, copy, modify, and distribute this software for any
7*9a1d0467SLeorize * purpose with or without fee is hereby granted, provided that the above
8*9a1d0467SLeorize * copyright notice and this permission notice appear in all copies.
9*9a1d0467SLeorize *
10*9a1d0467SLeorize * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*9a1d0467SLeorize * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*9a1d0467SLeorize * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*9a1d0467SLeorize * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*9a1d0467SLeorize * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*9a1d0467SLeorize * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*9a1d0467SLeorize * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*9a1d0467SLeorize *
18*9a1d0467SLeorize * Sponsored in part by the Defense Advanced Research Projects
19*9a1d0467SLeorize * Agency (DARPA) and Air Force Research Laboratory, Air Force
20*9a1d0467SLeorize * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21*9a1d0467SLeorize */
22*9a1d0467SLeorize
23*9a1d0467SLeorize #include <ctype.h>
24*9a1d0467SLeorize #include <errno.h>
25*9a1d0467SLeorize #include <fcntl.h>
26*9a1d0467SLeorize #include <paths.h>
27*9a1d0467SLeorize #include <pwd.h>
28*9a1d0467SLeorize #include <signal.h>
29*9a1d0467SLeorize #include <string.h>
30*9a1d0467SLeorize #include <termios.h>
31*9a1d0467SLeorize #include <unistd.h>
32*9a1d0467SLeorize #include <readpassphrase.h>
33*9a1d0467SLeorize
34*9a1d0467SLeorize #ifndef TCSASOFT
35*9a1d0467SLeorize #define TCSASOFT 0
36*9a1d0467SLeorize #endif
37*9a1d0467SLeorize
38*9a1d0467SLeorize static volatile sig_atomic_t signo;
39*9a1d0467SLeorize
40*9a1d0467SLeorize static void handler(int);
41*9a1d0467SLeorize
42*9a1d0467SLeorize char *
readpassphrase(const char * prompt,char * buf,size_t bufsiz,int flags)43*9a1d0467SLeorize readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
44*9a1d0467SLeorize {
45*9a1d0467SLeorize ssize_t nr;
46*9a1d0467SLeorize int input, output, save_errno;
47*9a1d0467SLeorize char ch, *p, *end;
48*9a1d0467SLeorize struct termios term, oterm;
49*9a1d0467SLeorize struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
50*9a1d0467SLeorize struct sigaction savetstp, savettin, savettou, savepipe;
51*9a1d0467SLeorize
52*9a1d0467SLeorize /* I suppose we could alloc on demand in this case (XXX). */
53*9a1d0467SLeorize if (bufsiz == 0) {
54*9a1d0467SLeorize errno = EINVAL;
55*9a1d0467SLeorize return(NULL);
56*9a1d0467SLeorize }
57*9a1d0467SLeorize
58*9a1d0467SLeorize restart:
59*9a1d0467SLeorize signo = 0;
60*9a1d0467SLeorize nr = -1;
61*9a1d0467SLeorize save_errno = 0;
62*9a1d0467SLeorize /*
63*9a1d0467SLeorize * Read and write to /dev/tty if available. If not, read from
64*9a1d0467SLeorize * stdin and write to stderr unless a tty is required.
65*9a1d0467SLeorize */
66*9a1d0467SLeorize if ((flags & RPP_STDIN) ||
67*9a1d0467SLeorize (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
68*9a1d0467SLeorize if (flags & RPP_REQUIRE_TTY) {
69*9a1d0467SLeorize errno = ENOTTY;
70*9a1d0467SLeorize return(NULL);
71*9a1d0467SLeorize }
72*9a1d0467SLeorize input = STDIN_FILENO;
73*9a1d0467SLeorize output = STDERR_FILENO;
74*9a1d0467SLeorize }
75*9a1d0467SLeorize
76*9a1d0467SLeorize /*
77*9a1d0467SLeorize * Catch signals that would otherwise cause the user to end
78*9a1d0467SLeorize * up with echo turned off in the shell. Don't worry about
79*9a1d0467SLeorize * things like SIGXCPU and SIGVTALRM for now.
80*9a1d0467SLeorize */
81*9a1d0467SLeorize sigemptyset(&sa.sa_mask);
82*9a1d0467SLeorize sa.sa_flags = 0; /* don't restart system calls */
83*9a1d0467SLeorize sa.sa_handler = handler;
84*9a1d0467SLeorize (void)sigaction(SIGALRM, &sa, &savealrm);
85*9a1d0467SLeorize (void)sigaction(SIGHUP, &sa, &savehup);
86*9a1d0467SLeorize (void)sigaction(SIGINT, &sa, &saveint);
87*9a1d0467SLeorize (void)sigaction(SIGPIPE, &sa, &savepipe);
88*9a1d0467SLeorize (void)sigaction(SIGQUIT, &sa, &savequit);
89*9a1d0467SLeorize (void)sigaction(SIGTERM, &sa, &saveterm);
90*9a1d0467SLeorize (void)sigaction(SIGTSTP, &sa, &savetstp);
91*9a1d0467SLeorize (void)sigaction(SIGTTIN, &sa, &savettin);
92*9a1d0467SLeorize (void)sigaction(SIGTTOU, &sa, &savettou);
93*9a1d0467SLeorize
94*9a1d0467SLeorize /* Turn off echo if possible. */
95*9a1d0467SLeorize if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
96*9a1d0467SLeorize memcpy(&term, &oterm, sizeof(term));
97*9a1d0467SLeorize if (!(flags & RPP_ECHO_ON))
98*9a1d0467SLeorize term.c_lflag &= ~(ECHO | ECHONL);
99*9a1d0467SLeorize #ifdef VSTATUS
100*9a1d0467SLeorize if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
101*9a1d0467SLeorize term.c_cc[VSTATUS] = _POSIX_VDISABLE;
102*9a1d0467SLeorize #endif
103*9a1d0467SLeorize (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
104*9a1d0467SLeorize } else {
105*9a1d0467SLeorize memset(&term, 0, sizeof(term));
106*9a1d0467SLeorize term.c_lflag |= ECHO;
107*9a1d0467SLeorize memset(&oterm, 0, sizeof(oterm));
108*9a1d0467SLeorize oterm.c_lflag |= ECHO;
109*9a1d0467SLeorize }
110*9a1d0467SLeorize
111*9a1d0467SLeorize /* No I/O if we are already backgrounded. */
112*9a1d0467SLeorize if (signo != SIGTTOU && signo != SIGTTIN) {
113*9a1d0467SLeorize if (!(flags & RPP_STDIN))
114*9a1d0467SLeorize (void)write(output, prompt, strlen(prompt));
115*9a1d0467SLeorize end = buf + bufsiz - 1;
116*9a1d0467SLeorize p = buf;
117*9a1d0467SLeorize while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
118*9a1d0467SLeorize if (p < end) {
119*9a1d0467SLeorize if ((flags & RPP_SEVENBIT))
120*9a1d0467SLeorize ch &= 0x7f;
121*9a1d0467SLeorize if (isalpha(ch)) {
122*9a1d0467SLeorize if ((flags & RPP_FORCELOWER))
123*9a1d0467SLeorize ch = (char)tolower(ch);
124*9a1d0467SLeorize if ((flags & RPP_FORCEUPPER))
125*9a1d0467SLeorize ch = (char)toupper(ch);
126*9a1d0467SLeorize }
127*9a1d0467SLeorize *p++ = ch;
128*9a1d0467SLeorize }
129*9a1d0467SLeorize }
130*9a1d0467SLeorize *p = '\0';
131*9a1d0467SLeorize save_errno = errno;
132*9a1d0467SLeorize if (!(term.c_lflag & ECHO))
133*9a1d0467SLeorize (void)write(output, "\n", 1);
134*9a1d0467SLeorize }
135*9a1d0467SLeorize
136*9a1d0467SLeorize /* Restore old terminal settings and signals. */
137*9a1d0467SLeorize if (memcmp(&term, &oterm, sizeof(term)) != 0) {
138*9a1d0467SLeorize while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
139*9a1d0467SLeorize errno == EINTR)
140*9a1d0467SLeorize continue;
141*9a1d0467SLeorize }
142*9a1d0467SLeorize (void)sigaction(SIGALRM, &savealrm, NULL);
143*9a1d0467SLeorize (void)sigaction(SIGHUP, &savehup, NULL);
144*9a1d0467SLeorize (void)sigaction(SIGINT, &saveint, NULL);
145*9a1d0467SLeorize (void)sigaction(SIGQUIT, &savequit, NULL);
146*9a1d0467SLeorize (void)sigaction(SIGPIPE, &savepipe, NULL);
147*9a1d0467SLeorize (void)sigaction(SIGTERM, &saveterm, NULL);
148*9a1d0467SLeorize (void)sigaction(SIGTSTP, &savetstp, NULL);
149*9a1d0467SLeorize (void)sigaction(SIGTTIN, &savettin, NULL);
150*9a1d0467SLeorize (void)sigaction(SIGTTOU, &savettou, NULL);
151*9a1d0467SLeorize if (input != STDIN_FILENO)
152*9a1d0467SLeorize (void)close(input);
153*9a1d0467SLeorize
154*9a1d0467SLeorize /*
155*9a1d0467SLeorize * If we were interrupted by a signal, resend it to ourselves
156*9a1d0467SLeorize * now that we have restored the signal handlers.
157*9a1d0467SLeorize */
158*9a1d0467SLeorize if (signo) {
159*9a1d0467SLeorize kill(getpid(), signo);
160*9a1d0467SLeorize switch (signo) {
161*9a1d0467SLeorize case SIGTSTP:
162*9a1d0467SLeorize case SIGTTIN:
163*9a1d0467SLeorize case SIGTTOU:
164*9a1d0467SLeorize goto restart;
165*9a1d0467SLeorize }
166*9a1d0467SLeorize }
167*9a1d0467SLeorize
168*9a1d0467SLeorize if (save_errno)
169*9a1d0467SLeorize errno = save_errno;
170*9a1d0467SLeorize return(nr == -1 ? NULL : buf);
171*9a1d0467SLeorize }
172*9a1d0467SLeorize
173*9a1d0467SLeorize #if 0
174*9a1d0467SLeorize char *
175*9a1d0467SLeorize getpass(const char *prompt)
176*9a1d0467SLeorize {
177*9a1d0467SLeorize static char buf[_PASSWORD_LEN + 1];
178*9a1d0467SLeorize
179*9a1d0467SLeorize return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
180*9a1d0467SLeorize }
181*9a1d0467SLeorize #endif
182*9a1d0467SLeorize
handler(int s)183*9a1d0467SLeorize static void handler(int s)
184*9a1d0467SLeorize {
185*9a1d0467SLeorize
186*9a1d0467SLeorize signo = s;
187*9a1d0467SLeorize }
188