xref: /freebsd/lib/libc/gen/readpassphrase.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1dad64c97SPedro F. Giffuni /*	$OpenBSD: readpassphrase.c,v 1.24 2013/11/24 23:51:29 deraadt Exp $	*/
2a7a8a766SBrian Feldman 
313d98e8cSBrian Feldman /*
4f29af3b2SXin LI  * Copyright (c) 2000-2002, 2007, 2010
5f29af3b2SXin LI  *	Todd C. Miller <Todd.Miller@courtesan.com>
613d98e8cSBrian Feldman  *
7f29af3b2SXin LI  * Permission to use, copy, modify, and distribute this software for any
8f29af3b2SXin LI  * purpose with or without fee is hereby granted, provided that the above
9f29af3b2SXin LI  * copyright notice and this permission notice appear in all copies.
1013d98e8cSBrian Feldman  *
11f29af3b2SXin LI  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12f29af3b2SXin LI  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13f29af3b2SXin LI  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14f29af3b2SXin LI  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15f29af3b2SXin LI  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16f29af3b2SXin LI  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17f29af3b2SXin LI  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18f29af3b2SXin LI  *
19f29af3b2SXin LI  * Sponsored in part by the Defense Advanced Research Projects
20f29af3b2SXin LI  * Agency (DARPA) and Air Force Research Laboratory, Air Force
21f29af3b2SXin LI  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
2213d98e8cSBrian Feldman  */
2313d98e8cSBrian Feldman 
2470d2a9e1SBrian Feldman #include "namespace.h"
2513d98e8cSBrian Feldman #include <ctype.h>
2613d98e8cSBrian Feldman #include <errno.h>
2713d98e8cSBrian Feldman #include <fcntl.h>
2813d98e8cSBrian Feldman #include <paths.h>
2913d98e8cSBrian Feldman #include <pwd.h>
3013d98e8cSBrian Feldman #include <signal.h>
3113d98e8cSBrian Feldman #include <string.h>
3213d98e8cSBrian Feldman #include <termios.h>
3313d98e8cSBrian Feldman #include <unistd.h>
3413d98e8cSBrian Feldman #include <readpassphrase.h>
3570d2a9e1SBrian Feldman #include "un-namespace.h"
36bd6060a1SKonstantin Belousov #include "libc_private.h"
3713d98e8cSBrian Feldman 
38f29af3b2SXin LI static volatile sig_atomic_t signo[NSIG];
39a7a8a766SBrian Feldman 
40a7a8a766SBrian Feldman static void handler(int);
41a7a8a766SBrian Feldman 
4213d98e8cSBrian Feldman char *
readpassphrase(const char * prompt,char * buf,size_t bufsiz,int flags)43a7a8a766SBrian Feldman readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
4413d98e8cSBrian Feldman {
45a7a8a766SBrian Feldman 	ssize_t nr;
46*18a2ccd2SMaxim Sobolev 	int input, output, save_errno, i, need_restart, input_is_tty;
4713d98e8cSBrian Feldman 	char ch, *p, *end;
48a7a8a766SBrian Feldman 	struct termios term, oterm;
49f29af3b2SXin LI 	struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
50f29af3b2SXin LI 	struct sigaction savetstp, savettin, savettou, savepipe;
5113d98e8cSBrian Feldman 
5213d98e8cSBrian Feldman 	/* I suppose we could alloc on demand in this case (XXX). */
5313d98e8cSBrian Feldman 	if (bufsiz == 0) {
5413d98e8cSBrian Feldman 		errno = EINVAL;
5513d98e8cSBrian Feldman 		return(NULL);
5613d98e8cSBrian Feldman 	}
5713d98e8cSBrian Feldman 
58a7a8a766SBrian Feldman restart:
59f29af3b2SXin LI 	for (i = 0; i < NSIG; i++)
60f29af3b2SXin LI 		signo[i] = 0;
61f29af3b2SXin LI 	nr = -1;
62f29af3b2SXin LI 	save_errno = 0;
63f29af3b2SXin LI 	need_restart = 0;
6413d98e8cSBrian Feldman 	/*
6513d98e8cSBrian Feldman 	 * Read and write to /dev/tty if available.  If not, read from
6613d98e8cSBrian Feldman 	 * stdin and write to stderr unless a tty is required.
6713d98e8cSBrian Feldman 	 */
68*18a2ccd2SMaxim Sobolev 	input_is_tty = 0;
69*18a2ccd2SMaxim Sobolev 	if (!(flags & RPP_STDIN)) {
70*18a2ccd2SMaxim Sobolev         	input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC);
71*18a2ccd2SMaxim Sobolev 		if (input == -1) {
7213d98e8cSBrian Feldman 			if (flags & RPP_REQUIRE_TTY) {
7313d98e8cSBrian Feldman 				errno = ENOTTY;
7413d98e8cSBrian Feldman 				return(NULL);
7513d98e8cSBrian Feldman 			}
7613d98e8cSBrian Feldman 			input = STDIN_FILENO;
7713d98e8cSBrian Feldman 			output = STDERR_FILENO;
78*18a2ccd2SMaxim Sobolev 		} else {
79*18a2ccd2SMaxim Sobolev 			input_is_tty = 1;
80*18a2ccd2SMaxim Sobolev 		}
81*18a2ccd2SMaxim Sobolev 	} else {
82*18a2ccd2SMaxim Sobolev 		input = STDIN_FILENO;
83*18a2ccd2SMaxim Sobolev 		output = STDERR_FILENO;
8413d98e8cSBrian Feldman 	}
8513d98e8cSBrian Feldman 
8613d98e8cSBrian Feldman 	/*
87f29af3b2SXin LI 	 * Turn off echo if possible.
88f29af3b2SXin LI 	 * If we are using a tty but are not the foreground pgrp this will
89f29af3b2SXin LI 	 * generate SIGTTOU, so do it *before* installing the signal handlers.
9013d98e8cSBrian Feldman 	 */
91*18a2ccd2SMaxim Sobolev 	if (input_is_tty && tcgetattr(input, &oterm) == 0) {
9213d98e8cSBrian Feldman 		memcpy(&term, &oterm, sizeof(term));
93a7a8a766SBrian Feldman 		if (!(flags & RPP_ECHO_ON))
94a7a8a766SBrian Feldman 			term.c_lflag &= ~(ECHO | ECHONL);
9513d98e8cSBrian Feldman 		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
9613d98e8cSBrian Feldman 			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
9713d98e8cSBrian Feldman 		(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
9813d98e8cSBrian Feldman 	} else {
9913d98e8cSBrian Feldman 		memset(&term, 0, sizeof(term));
100f29af3b2SXin LI 		term.c_lflag |= ECHO;
10113d98e8cSBrian Feldman 		memset(&oterm, 0, sizeof(oterm));
102f29af3b2SXin LI 		oterm.c_lflag |= ECHO;
10313d98e8cSBrian Feldman 	}
10413d98e8cSBrian Feldman 
105f29af3b2SXin LI 	/*
106f29af3b2SXin LI 	 * Catch signals that would otherwise cause the user to end
107f29af3b2SXin LI 	 * up with echo turned off in the shell.  Don't worry about
108f29af3b2SXin LI 	 * things like SIGXCPU and SIGVTALRM for now.
109f29af3b2SXin LI 	 */
110f29af3b2SXin LI 	sigemptyset(&sa.sa_mask);
111f29af3b2SXin LI 	sa.sa_flags = 0;		/* don't restart system calls */
112f29af3b2SXin LI 	sa.sa_handler = handler;
113bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGALRM, &sa, &savealrm);
114bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGHUP, &sa, &savehup);
115bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGINT, &sa, &saveint);
116bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGPIPE, &sa, &savepipe);
117bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGQUIT, &sa, &savequit);
118bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTERM, &sa, &saveterm);
119bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTSTP, &sa, &savetstp);
120bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTTIN, &sa, &savettin);
121bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTTOU, &sa, &savettou);
122f29af3b2SXin LI 
123f29af3b2SXin LI 	if (!(flags & RPP_STDIN))
124a939ced8SBrian Feldman 		(void)_write(output, prompt, strlen(prompt));
12513d98e8cSBrian Feldman 	end = buf + bufsiz - 1;
126f29af3b2SXin LI 	p = buf;
127f29af3b2SXin LI 	while ((nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
12813d98e8cSBrian Feldman 		if (p < end) {
12913d98e8cSBrian Feldman 			if ((flags & RPP_SEVENBIT))
13013d98e8cSBrian Feldman 				ch &= 0x7f;
131dad64c97SPedro F. Giffuni 			if (isalpha((unsigned char)ch)) {
13213d98e8cSBrian Feldman 				if ((flags & RPP_FORCELOWER))
133dad64c97SPedro F. Giffuni 					ch = (char)tolower((unsigned char)ch);
13413d98e8cSBrian Feldman 				if ((flags & RPP_FORCEUPPER))
135dad64c97SPedro F. Giffuni 					ch = (char)toupper((unsigned char)ch);
13613d98e8cSBrian Feldman 			}
13713d98e8cSBrian Feldman 			*p++ = ch;
13813d98e8cSBrian Feldman 		}
13913d98e8cSBrian Feldman 	}
14013d98e8cSBrian Feldman 	*p = '\0';
141a7a8a766SBrian Feldman 	save_errno = errno;
14213d98e8cSBrian Feldman 	if (!(term.c_lflag & ECHO))
143a939ced8SBrian Feldman 		(void)_write(output, "\n", 1);
14413d98e8cSBrian Feldman 
145a7a8a766SBrian Feldman 	/* Restore old terminal settings and signals. */
146f29af3b2SXin LI 	if (memcmp(&term, &oterm, sizeof(term)) != 0) {
147f29af3b2SXin LI 		while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
148f29af3b2SXin LI 		    errno == EINTR && !signo[SIGTTOU])
149f29af3b2SXin LI 			continue;
150f29af3b2SXin LI 	}
151bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGALRM, &savealrm, NULL);
152bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGHUP, &savehup, NULL);
153bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGINT, &saveint, NULL);
154bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGQUIT, &savequit, NULL);
155bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGPIPE, &savepipe, NULL);
156bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTERM, &saveterm, NULL);
157bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTSTP, &savetstp, NULL);
158bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTTIN, &savettin, NULL);
159bd6060a1SKonstantin Belousov 	(void)__libc_sigaction(SIGTTOU, &savettou, NULL);
160*18a2ccd2SMaxim Sobolev 	if (input_is_tty)
161a939ced8SBrian Feldman 		(void)_close(input);
162a7a8a766SBrian Feldman 
163a7a8a766SBrian Feldman 	/*
164a7a8a766SBrian Feldman 	 * If we were interrupted by a signal, resend it to ourselves
165a7a8a766SBrian Feldman 	 * now that we have restored the signal handlers.
166a7a8a766SBrian Feldman 	 */
167f29af3b2SXin LI 	for (i = 0; i < NSIG; i++) {
168f29af3b2SXin LI 		if (signo[i]) {
169f29af3b2SXin LI 			kill(getpid(), i);
170f29af3b2SXin LI 			switch (i) {
171a7a8a766SBrian Feldman 			case SIGTSTP:
172a7a8a766SBrian Feldman 			case SIGTTIN:
173a7a8a766SBrian Feldman 			case SIGTTOU:
174f29af3b2SXin LI 				need_restart = 1;
175f29af3b2SXin LI 			}
176f29af3b2SXin LI 		}
177f29af3b2SXin LI 	}
178f29af3b2SXin LI 	if (need_restart)
179a7a8a766SBrian Feldman 		goto restart;
18013d98e8cSBrian Feldman 
181f29af3b2SXin LI 	if (save_errno)
182a7a8a766SBrian Feldman 		errno = save_errno;
183a7a8a766SBrian Feldman 	return(nr == -1 ? NULL : buf);
184a7a8a766SBrian Feldman }
185a7a8a766SBrian Feldman 
18613d98e8cSBrian Feldman char *
getpass(const char * prompt)187a7a8a766SBrian Feldman getpass(const char *prompt)
18813d98e8cSBrian Feldman {
18913d98e8cSBrian Feldman 	static char buf[_PASSWORD_LEN + 1];
19013d98e8cSBrian Feldman 
19144f7d6afSBrian Feldman 	if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
19244f7d6afSBrian Feldman 		buf[0] = '\0';
19344f7d6afSBrian Feldman 	return(buf);
19413d98e8cSBrian Feldman }
195a7a8a766SBrian Feldman 
handler(int s)196a7a8a766SBrian Feldman static void handler(int s)
197a7a8a766SBrian Feldman {
198a7a8a766SBrian Feldman 
199f29af3b2SXin LI 	signo[s] = 1;
200a7a8a766SBrian Feldman }
201