1/*	$OpenBSD: readpassphrase.c,v 1.20 2007/10/30 12:03:48 millert Exp $	*/
2
3/*
4 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
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.
9 *
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.
17 *
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.
21 */
22
23#include <ctype.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <paths.h>
27#include <pwd.h>
28#include <signal.h>
29#include <string.h>
30#include <termios.h>
31#include <unistd.h>
32#include <readpassphrase.h>
33
34#ifndef TCSASOFT
35#define TCSASOFT 0
36#endif
37
38static volatile sig_atomic_t signo;
39
40static void handler(int);
41
42char *
43readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
44{
45	ssize_t nr;
46	int input, output, save_errno;
47	char ch, *p, *end;
48	struct termios term, oterm;
49	struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
50	struct sigaction savetstp, savettin, savettou, savepipe;
51
52	/* I suppose we could alloc on demand in this case (XXX). */
53	if (bufsiz == 0) {
54		errno = EINVAL;
55		return(NULL);
56	}
57
58restart:
59	signo = 0;
60	nr = -1;
61	save_errno = 0;
62	/*
63	 * Read and write to /dev/tty if available.  If not, read from
64	 * stdin and write to stderr unless a tty is required.
65	 */
66	if ((flags & RPP_STDIN) ||
67	    (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
68		if (flags & RPP_REQUIRE_TTY) {
69			errno = ENOTTY;
70			return(NULL);
71		}
72		input = STDIN_FILENO;
73		output = STDERR_FILENO;
74	}
75
76	/*
77	 * Catch signals that would otherwise cause the user to end
78	 * up with echo turned off in the shell.  Don't worry about
79	 * things like SIGXCPU and SIGVTALRM for now.
80	 */
81	sigemptyset(&sa.sa_mask);
82	sa.sa_flags = 0;		/* don't restart system calls */
83	sa.sa_handler = handler;
84	(void)sigaction(SIGALRM, &sa, &savealrm);
85	(void)sigaction(SIGHUP, &sa, &savehup);
86	(void)sigaction(SIGINT, &sa, &saveint);
87	(void)sigaction(SIGPIPE, &sa, &savepipe);
88	(void)sigaction(SIGQUIT, &sa, &savequit);
89	(void)sigaction(SIGTERM, &sa, &saveterm);
90	(void)sigaction(SIGTSTP, &sa, &savetstp);
91	(void)sigaction(SIGTTIN, &sa, &savettin);
92	(void)sigaction(SIGTTOU, &sa, &savettou);
93
94	/* Turn off echo if possible. */
95	if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
96		memcpy(&term, &oterm, sizeof(term));
97		if (!(flags & RPP_ECHO_ON))
98			term.c_lflag &= ~(ECHO | ECHONL);
99#ifdef VSTATUS
100		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
101			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
102#endif
103		(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
104	} else {
105		memset(&term, 0, sizeof(term));
106		term.c_lflag |= ECHO;
107		memset(&oterm, 0, sizeof(oterm));
108		oterm.c_lflag |= ECHO;
109	}
110
111	/* No I/O if we are already backgrounded. */
112	if (signo != SIGTTOU && signo != SIGTTIN) {
113		if (!(flags & RPP_STDIN))
114			(void)write(output, prompt, strlen(prompt));
115		end = buf + bufsiz - 1;
116		p = buf;
117		while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
118			if (p < end) {
119				if ((flags & RPP_SEVENBIT))
120					ch &= 0x7f;
121				if (isalpha(ch)) {
122					if ((flags & RPP_FORCELOWER))
123						ch = (char)tolower(ch);
124					if ((flags & RPP_FORCEUPPER))
125						ch = (char)toupper(ch);
126				}
127				*p++ = ch;
128			}
129		}
130		*p = '\0';
131		save_errno = errno;
132		if (!(term.c_lflag & ECHO))
133			(void)write(output, "\n", 1);
134	}
135
136	/* Restore old terminal settings and signals. */
137	if (memcmp(&term, &oterm, sizeof(term)) != 0) {
138		while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
139		    errno == EINTR)
140			continue;
141	}
142	(void)sigaction(SIGALRM, &savealrm, NULL);
143	(void)sigaction(SIGHUP, &savehup, NULL);
144	(void)sigaction(SIGINT, &saveint, NULL);
145	(void)sigaction(SIGQUIT, &savequit, NULL);
146	(void)sigaction(SIGPIPE, &savepipe, NULL);
147	(void)sigaction(SIGTERM, &saveterm, NULL);
148	(void)sigaction(SIGTSTP, &savetstp, NULL);
149	(void)sigaction(SIGTTIN, &savettin, NULL);
150	(void)sigaction(SIGTTOU, &savettou, NULL);
151	if (input != STDIN_FILENO)
152		(void)close(input);
153
154	/*
155	 * If we were interrupted by a signal, resend it to ourselves
156	 * now that we have restored the signal handlers.
157	 */
158	if (signo) {
159		kill(getpid(), signo);
160		switch (signo) {
161		case SIGTSTP:
162		case SIGTTIN:
163		case SIGTTOU:
164			goto restart;
165		}
166	}
167
168	if (save_errno)
169		errno = save_errno;
170	return(nr == -1 ? NULL : buf);
171}
172
173#if 0
174char *
175getpass(const char *prompt)
176{
177	static char buf[_PASSWORD_LEN + 1];
178
179	return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
180}
181#endif
182
183static void handler(int s)
184{
185
186	signo = s;
187}
188