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