xref: /openssh-portable/readpass.c (revision 46712110)
1 /* $OpenBSD: readpass.c,v 1.56 2019/11/12 22:35:02 djm Exp $ */
2 /*
3  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "includes.h"
27 
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #ifdef HAVE_PATHS_H
34 # include <paths.h>
35 #endif
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "xmalloc.h"
44 #include "misc.h"
45 #include "pathnames.h"
46 #include "log.h"
47 #include "ssh.h"
48 #include "uidswap.h"
49 
50 static char *
51 ssh_askpass(char *askpass, const char *msg)
52 {
53 	pid_t pid, ret;
54 	size_t len;
55 	char *pass;
56 	int p[2], status;
57 	char buf[1024];
58 	void (*osigchld)(int);
59 
60 	if (fflush(stdout) != 0)
61 		error("ssh_askpass: fflush: %s", strerror(errno));
62 	if (askpass == NULL)
63 		fatal("internal error: askpass undefined");
64 	if (pipe(p) == -1) {
65 		error("ssh_askpass: pipe: %s", strerror(errno));
66 		return NULL;
67 	}
68 	osigchld = signal(SIGCHLD, SIG_DFL);
69 	if ((pid = fork()) == -1) {
70 		error("ssh_askpass: fork: %s", strerror(errno));
71 		signal(SIGCHLD, osigchld);
72 		return NULL;
73 	}
74 	if (pid == 0) {
75 		close(p[0]);
76 		if (dup2(p[1], STDOUT_FILENO) == -1)
77 			fatal("ssh_askpass: dup2: %s", strerror(errno));
78 		setenv("SSH_ASKPASS_PROMPT", "confirm", 1); /* hint to UI */
79 		execlp(askpass, askpass, msg, (char *)NULL);
80 		fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
81 	}
82 	close(p[1]);
83 
84 	len = 0;
85 	do {
86 		ssize_t r = read(p[0], buf + len, sizeof(buf) - 1 - len);
87 
88 		if (r == -1 && errno == EINTR)
89 			continue;
90 		if (r <= 0)
91 			break;
92 		len += r;
93 	} while (sizeof(buf) - 1 - len > 0);
94 	buf[len] = '\0';
95 
96 	close(p[0]);
97 	while ((ret = waitpid(pid, &status, 0)) == -1)
98 		if (errno != EINTR)
99 			break;
100 	signal(SIGCHLD, osigchld);
101 	if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
102 		explicit_bzero(buf, sizeof(buf));
103 		return NULL;
104 	}
105 
106 	buf[strcspn(buf, "\r\n")] = '\0';
107 	pass = xstrdup(buf);
108 	explicit_bzero(buf, sizeof(buf));
109 	return pass;
110 }
111 
112 /*
113  * Reads a passphrase from /dev/tty with echo turned off/on.  Returns the
114  * passphrase (allocated with xmalloc).  Exits if EOF is encountered. If
115  * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
116  * tty is available
117  */
118 char *
119 read_passphrase(const char *prompt, int flags)
120 {
121 	char cr = '\r', *askpass = NULL, *ret, buf[1024];
122 	int rppflags, use_askpass = 0, ttyfd;
123 
124 	rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
125 	if (flags & RP_USE_ASKPASS)
126 		use_askpass = 1;
127 	else if (flags & RP_ALLOW_STDIN) {
128 		if (!isatty(STDIN_FILENO)) {
129 			debug("read_passphrase: stdin is not a tty");
130 			use_askpass = 1;
131 		}
132 	} else {
133 		rppflags |= RPP_REQUIRE_TTY;
134 		ttyfd = open(_PATH_TTY, O_RDWR);
135 		if (ttyfd >= 0) {
136 			/*
137 			 * If we're on a tty, ensure that show the prompt at
138 			 * the beginning of the line. This will hopefully
139 			 * clobber any password characters the user has
140 			 * optimistically typed before echo is disabled.
141 			 */
142 			(void)write(ttyfd, &cr, 1);
143 			close(ttyfd);
144 		} else {
145 			debug("read_passphrase: can't open %s: %s", _PATH_TTY,
146 			    strerror(errno));
147 			use_askpass = 1;
148 		}
149 	}
150 
151 	if ((flags & RP_USE_ASKPASS) && getenv("DISPLAY") == NULL)
152 		return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
153 
154 	if (use_askpass && getenv("DISPLAY")) {
155 		if (getenv(SSH_ASKPASS_ENV))
156 			askpass = getenv(SSH_ASKPASS_ENV);
157 		else
158 			askpass = _PATH_SSH_ASKPASS_DEFAULT;
159 		if ((ret = ssh_askpass(askpass, prompt)) == NULL)
160 			if (!(flags & RP_ALLOW_EOF))
161 				return xstrdup("");
162 		return ret;
163 	}
164 
165 	if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
166 		if (flags & RP_ALLOW_EOF)
167 			return NULL;
168 		return xstrdup("");
169 	}
170 
171 	ret = xstrdup(buf);
172 	explicit_bzero(buf, sizeof(buf));
173 	return ret;
174 }
175 
176 int
177 ask_permission(const char *fmt, ...)
178 {
179 	va_list args;
180 	char *p, prompt[1024];
181 	int allowed = 0;
182 
183 	va_start(args, fmt);
184 	vsnprintf(prompt, sizeof(prompt), fmt, args);
185 	va_end(args);
186 
187 	p = read_passphrase(prompt, RP_USE_ASKPASS|RP_ALLOW_EOF);
188 	if (p != NULL) {
189 		/*
190 		 * Accept empty responses and responses consisting
191 		 * of the word "yes" as affirmative.
192 		 */
193 		if (*p == '\0' || *p == '\n' ||
194 		    strcasecmp(p, "yes") == 0)
195 			allowed = 1;
196 		free(p);
197 	}
198 
199 	return (allowed);
200 }
201 
202 struct notifier_ctx {
203 	pid_t pid;
204 	void (*osigchld)(int);
205 };
206 
207 struct notifier_ctx *
208 notify_start(int force_askpass, const char *fmt, ...)
209 {
210 	va_list args;
211 	char *prompt = NULL;
212 	int devnull;
213 	pid_t pid;
214 	void (*osigchld)(int);
215 	const char *askpass;
216 	struct notifier_ctx *ret;
217 
218 	va_start(args, fmt);
219 	xvasprintf(&prompt, fmt, args);
220 	va_end(args);
221 
222 	if (fflush(NULL) != 0)
223 		error("%s: fflush: %s", __func__, strerror(errno));
224 	if (!force_askpass && isatty(STDERR_FILENO)) {
225 		(void)write(STDERR_FILENO, "\r", 1);
226 		(void)write(STDERR_FILENO, prompt, strlen(prompt));
227 		(void)write(STDERR_FILENO, "\r\n", 2);
228 		free(prompt);
229 		return NULL;
230 	}
231 	if (getenv("DISPLAY") == NULL ||
232 	    (askpass = getenv("SSH_ASKPASS")) == NULL || *askpass == '\0') {
233 		debug3("%s: cannot notify", __func__);
234 		free(prompt);
235 		return NULL;
236 	}
237 	osigchld = signal(SIGCHLD, SIG_DFL);
238 	if ((pid = fork()) == -1) {
239 		error("%s: fork: %s", __func__, strerror(errno));
240 		signal(SIGCHLD, osigchld);
241 		free(prompt);
242 		return NULL;
243 	}
244 	if (pid == 0) {
245 		if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
246 			fatal("%s: open %s", __func__, strerror(errno));
247 		if (dup2(devnull, STDIN_FILENO) == -1 ||
248 		    dup2(devnull, STDOUT_FILENO) == -1)
249 			fatal("%s: dup2: %s", __func__, strerror(errno));
250 		closefrom(STDERR_FILENO + 1);
251 		setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */
252 		execlp(askpass, askpass, prompt, (char *)NULL);
253 		fatal("%s: exec(%s): %s", __func__, askpass, strerror(errno));
254 		/* NOTREACHED */
255 	}
256 	if ((ret = calloc(1, sizeof(*ret))) == NULL) {
257 		kill(pid, SIGTERM);
258 		fatal("%s: calloc failed", __func__);
259 	}
260 	ret->pid = pid;
261 	ret->osigchld = osigchld;
262 	free(prompt);
263 	return ret;
264 }
265 
266 void
267 notify_complete(struct notifier_ctx *ctx)
268 {
269 	int ret;
270 
271 	if (ctx == NULL || ctx->pid <= 0) {
272 		free(ctx);
273 		return;
274 	}
275 	kill(ctx->pid, SIGTERM);
276 	while ((ret = waitpid(ctx->pid, NULL, 0)) == -1) {
277 		if (errno != EINTR)
278 			break;
279 	}
280 	if (ret == -1)
281 		fatal("%s: waitpid: %s", __func__, strerror(errno));
282 	signal(SIGCHLD, ctx->osigchld);
283 	free(ctx);
284 }
285