xref: /freebsd/lib/libc/gen/readpassphrase.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*	$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "namespace.h"
37 #include <ctype.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <paths.h>
41 #include <pwd.h>
42 #include <signal.h>
43 #include <string.h>
44 #include <termios.h>
45 #include <unistd.h>
46 #include <readpassphrase.h>
47 #include "un-namespace.h"
48 
49 static volatile sig_atomic_t signo;
50 
51 static void handler(int);
52 
53 char *
54 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
55 {
56 	ssize_t nr;
57 	int input, output, save_errno;
58 	char ch, *p, *end;
59 	struct termios term, oterm;
60 	struct sigaction sa, saveint, savehup, savequit, saveterm;
61 	struct sigaction savetstp, savettin, savettou;
62 
63 	/* I suppose we could alloc on demand in this case (XXX). */
64 	if (bufsiz == 0) {
65 		errno = EINVAL;
66 		return(NULL);
67 	}
68 
69 restart:
70 	/*
71 	 * Read and write to /dev/tty if available.  If not, read from
72 	 * stdin and write to stderr unless a tty is required.
73 	 */
74 	if ((input = output = _open(_PATH_TTY, O_RDWR)) == -1) {
75 		if (flags & RPP_REQUIRE_TTY) {
76 			errno = ENOTTY;
77 			return(NULL);
78 		}
79 		input = STDIN_FILENO;
80 		output = STDERR_FILENO;
81 	}
82 
83 	/*
84 	 * Catch signals that would otherwise cause the user to end
85 	 * up with echo turned off in the shell.  Don't worry about
86 	 * things like SIGALRM and SIGPIPE for now.
87 	 */
88 	sigemptyset(&sa.sa_mask);
89 	sa.sa_flags = 0;		/* don't restart system calls */
90 	sa.sa_handler = handler;
91 	(void)_sigaction(SIGINT, &sa, &saveint);
92 	(void)_sigaction(SIGHUP, &sa, &savehup);
93 	(void)_sigaction(SIGQUIT, &sa, &savequit);
94 	(void)_sigaction(SIGTERM, &sa, &saveterm);
95 	(void)_sigaction(SIGTSTP, &sa, &savetstp);
96 	(void)_sigaction(SIGTTIN, &sa, &savettin);
97 	(void)_sigaction(SIGTTOU, &sa, &savettou);
98 
99 	/* Turn off echo if possible. */
100 	if (tcgetattr(input, &oterm) == 0) {
101 		memcpy(&term, &oterm, sizeof(term));
102 		if (!(flags & RPP_ECHO_ON))
103 			term.c_lflag &= ~(ECHO | ECHONL);
104 		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
105 			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
106 		(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
107 	} else {
108 		memset(&term, 0, sizeof(term));
109 		memset(&oterm, 0, sizeof(oterm));
110 	}
111 
112 	(void)_write(output, prompt, strlen(prompt));
113 	end = buf + bufsiz - 1;
114 	for (p = buf; (nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
115 		if (p < end) {
116 			if ((flags & RPP_SEVENBIT))
117 				ch &= 0x7f;
118 			if (isalpha(ch)) {
119 				if ((flags & RPP_FORCELOWER))
120 					ch = tolower(ch);
121 				if ((flags & RPP_FORCEUPPER))
122 					ch = toupper(ch);
123 			}
124 			*p++ = ch;
125 		}
126 	}
127 	*p = '\0';
128 	save_errno = errno;
129 	if (!(term.c_lflag & ECHO))
130 		(void)_write(output, "\n", 1);
131 
132 	/* Restore old terminal settings and signals. */
133 	if (memcmp(&term, &oterm, sizeof(term)) != 0)
134 		(void)tcsetattr(input, TCSANOW|TCSASOFT, &oterm);
135 	(void)_sigaction(SIGINT, &saveint, NULL);
136 	(void)_sigaction(SIGHUP, &savehup, NULL);
137 	(void)_sigaction(SIGQUIT, &savequit, NULL);
138 	(void)_sigaction(SIGTERM, &saveterm, NULL);
139 	(void)_sigaction(SIGTSTP, &savetstp, NULL);
140 	(void)_sigaction(SIGTTIN, &savettin, NULL);
141 	(void)_sigaction(SIGTTOU, &savettou, NULL);
142 	if (input != STDIN_FILENO)
143 		(void)_close(input);
144 
145 	/*
146 	 * If we were interrupted by a signal, resend it to ourselves
147 	 * now that we have restored the signal handlers.
148 	 */
149 	if (signo) {
150 		kill(getpid(), signo);
151 		switch (signo) {
152 		case SIGTSTP:
153 		case SIGTTIN:
154 		case SIGTTOU:
155 			signo = 0;
156 			goto restart;
157 		}
158 	}
159 
160 	errno = save_errno;
161 	return(nr == -1 ? NULL : buf);
162 }
163 
164 char *
165 getpass(const char *prompt)
166 {
167 	static char buf[_PASSWORD_LEN + 1];
168 
169 	if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
170 		buf[0] = '\0';
171 	return(buf);
172 }
173 
174 static void handler(int s)
175 {
176 
177 	signo = s;
178 }
179