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