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