xref: /freebsd/lib/libc/gen/readpassphrase.c (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
1 /*	$OpenBSD: readpassphrase.c,v 1.23 2010/05/14 13:30:34 millert 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$");
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 
40 static volatile sig_atomic_t signo[NSIG];
41 
42 static void handler(int);
43 
44 char *
45 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
46 {
47 	ssize_t nr;
48 	int input, output, save_errno, i, need_restart;
49 	char ch, *p, *end;
50 	struct termios term, oterm;
51 	struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
52 	struct sigaction savetstp, savettin, savettou, savepipe;
53 
54 	/* I suppose we could alloc on demand in this case (XXX). */
55 	if (bufsiz == 0) {
56 		errno = EINVAL;
57 		return(NULL);
58 	}
59 
60 restart:
61 	for (i = 0; i < NSIG; i++)
62 		signo[i] = 0;
63 	nr = -1;
64 	save_errno = 0;
65 	need_restart = 0;
66 	/*
67 	 * Read and write to /dev/tty if available.  If not, read from
68 	 * stdin and write to stderr unless a tty is required.
69 	 */
70 	if ((flags & RPP_STDIN) ||
71 	    (input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC)) == -1) {
72 		if (flags & RPP_REQUIRE_TTY) {
73 			errno = ENOTTY;
74 			return(NULL);
75 		}
76 		input = STDIN_FILENO;
77 		output = STDERR_FILENO;
78 	}
79 
80 	/*
81 	 * Turn off echo if possible.
82 	 * If we are using a tty but are not the foreground pgrp this will
83 	 * generate SIGTTOU, so do it *before* installing the signal handlers.
84 	 */
85 	if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
86 		memcpy(&term, &oterm, sizeof(term));
87 		if (!(flags & RPP_ECHO_ON))
88 			term.c_lflag &= ~(ECHO | ECHONL);
89 		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
90 			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
91 		(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
92 	} else {
93 		memset(&term, 0, sizeof(term));
94 		term.c_lflag |= ECHO;
95 		memset(&oterm, 0, sizeof(oterm));
96 		oterm.c_lflag |= ECHO;
97 	}
98 
99 	/*
100 	 * Catch signals that would otherwise cause the user to end
101 	 * up with echo turned off in the shell.  Don't worry about
102 	 * things like SIGXCPU and SIGVTALRM for now.
103 	 */
104 	sigemptyset(&sa.sa_mask);
105 	sa.sa_flags = 0;		/* don't restart system calls */
106 	sa.sa_handler = handler;
107 	(void)_sigaction(SIGALRM, &sa, &savealrm);
108 	(void)_sigaction(SIGHUP, &sa, &savehup);
109 	(void)_sigaction(SIGINT, &sa, &saveint);
110 	(void)_sigaction(SIGPIPE, &sa, &savepipe);
111 	(void)_sigaction(SIGQUIT, &sa, &savequit);
112 	(void)_sigaction(SIGTERM, &sa, &saveterm);
113 	(void)_sigaction(SIGTSTP, &sa, &savetstp);
114 	(void)_sigaction(SIGTTIN, &sa, &savettin);
115 	(void)_sigaction(SIGTTOU, &sa, &savettou);
116 
117 	if (!(flags & RPP_STDIN))
118 		(void)_write(output, prompt, strlen(prompt));
119 	end = buf + bufsiz - 1;
120 	p = buf;
121 	while ((nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
122 		if (p < end) {
123 			if ((flags & RPP_SEVENBIT))
124 				ch &= 0x7f;
125 			if (isalpha(ch)) {
126 				if ((flags & RPP_FORCELOWER))
127 					ch = (char)tolower(ch);
128 				if ((flags & RPP_FORCEUPPER))
129 					ch = (char)toupper(ch);
130 			}
131 			*p++ = ch;
132 		}
133 	}
134 	*p = '\0';
135 	save_errno = errno;
136 	if (!(term.c_lflag & ECHO))
137 		(void)_write(output, "\n", 1);
138 
139 	/* Restore old terminal settings and signals. */
140 	if (memcmp(&term, &oterm, sizeof(term)) != 0) {
141 		while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
142 		    errno == EINTR && !signo[SIGTTOU])
143 			continue;
144 	}
145 	(void)_sigaction(SIGALRM, &savealrm, NULL);
146 	(void)_sigaction(SIGHUP, &savehup, NULL);
147 	(void)_sigaction(SIGINT, &saveint, NULL);
148 	(void)_sigaction(SIGQUIT, &savequit, NULL);
149 	(void)_sigaction(SIGPIPE, &savepipe, NULL);
150 	(void)_sigaction(SIGTERM, &saveterm, NULL);
151 	(void)_sigaction(SIGTSTP, &savetstp, NULL);
152 	(void)_sigaction(SIGTTIN, &savettin, NULL);
153 	(void)_sigaction(SIGTTOU, &savettou, NULL);
154 	if (input != STDIN_FILENO)
155 		(void)_close(input);
156 
157 	/*
158 	 * If we were interrupted by a signal, resend it to ourselves
159 	 * now that we have restored the signal handlers.
160 	 */
161 	for (i = 0; i < NSIG; i++) {
162 		if (signo[i]) {
163 			kill(getpid(), i);
164 			switch (i) {
165 			case SIGTSTP:
166 			case SIGTTIN:
167 			case SIGTTOU:
168 				need_restart = 1;
169 			}
170 		}
171 	}
172 	if (need_restart)
173 		goto restart;
174 
175 	if (save_errno)
176 		errno = save_errno;
177 	return(nr == -1 ? NULL : buf);
178 }
179 
180 char *
181 getpass(const char *prompt)
182 {
183 	static char buf[_PASSWORD_LEN + 1];
184 
185 	if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
186 		buf[0] = '\0';
187 	return(buf);
188 }
189 
190 static void handler(int s)
191 {
192 
193 	signo[s] = 1;
194 }
195