1 /* $OpenBSD: readpass.c,v 1.70 2022/05/27 04:27:49 dtucker Exp $ */
3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
45 #include "pathnames.h"
51 ssh_askpass(char *askpass, const char *msg, const char *env_hint)
58 void (*osigchld)(int);
60 if (fflush(stdout) != 0)
61 error_f("fflush: %s", strerror(errno));
63 fatal("internal error: askpass undefined");
65 error_f("pipe: %s", strerror(errno));
68 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
69 if ((pid = fork()) == -1) {
70 error_f("fork: %s", strerror(errno));
71 ssh_signal(SIGCHLD, osigchld);
76 if (dup2(p[1], STDOUT_FILENO) == -1)
77 fatal_f("dup2: %s", strerror(errno));
79 setenv("SSH_ASKPASS_PROMPT", env_hint, 1);
80 execlp(askpass, askpass, msg, (char *)NULL);
81 fatal_f("exec(%s): %s", askpass, strerror(errno));
87 ssize_t r = read(p[0], buf + len, sizeof(buf) - 1 - len);
89 if (r == -1 && errno == EINTR)
94 } while (sizeof(buf) - 1 - len > 0);
98 while ((ret = waitpid(pid, &status, 0)) == -1)
101 ssh_signal(SIGCHLD, osigchld);
102 if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
103 explicit_bzero(buf, sizeof(buf));
107 buf[strcspn(buf, "\r\n")] = '\0';
109 explicit_bzero(buf, sizeof(buf));
113 /* private/internal read_passphrase flags */
114 #define RP_ASK_PERMISSION 0x8000 /* pass hint to askpass for confirm UI */
117 * Reads a passphrase from /dev/tty with echo turned off/on. Returns the
118 * passphrase (allocated with xmalloc). Exits if EOF is encountered. If
119 * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
120 * tty is or askpass program is available
123 read_passphrase(const char *prompt, int flags)
125 char cr = '\r', *askpass = NULL, *ret, buf[1024];
126 int rppflags, ttyfd, use_askpass = 0, allow_askpass = 0;
127 const char *askpass_hint = NULL;
130 if ((s = getenv("DISPLAY")) != NULL)
131 allow_askpass = *s != '\0';
132 if ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) != NULL) {
133 if (strcasecmp(s, "force") == 0) {
136 } else if (strcasecmp(s, "prefer") == 0)
137 use_askpass = allow_askpass;
138 else if (strcasecmp(s, "never") == 0)
142 rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
144 debug_f("requested to askpass");
145 else if (flags & RP_USE_ASKPASS)
147 else if (flags & RP_ALLOW_STDIN) {
148 if (!isatty(STDIN_FILENO)) {
149 debug_f("stdin is not a tty");
153 rppflags |= RPP_REQUIRE_TTY;
154 ttyfd = open(_PATH_TTY, O_RDWR);
157 * If we're on a tty, ensure that show the prompt at
158 * the beginning of the line. This will hopefully
159 * clobber any password characters the user has
160 * optimistically typed before echo is disabled.
162 (void)write(ttyfd, &cr, 1);
165 debug_f("can't open %s: %s", _PATH_TTY,
171 if ((flags & RP_USE_ASKPASS) && !allow_askpass)
172 return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
174 if (use_askpass && allow_askpass) {
175 if (getenv(SSH_ASKPASS_ENV))
176 askpass = getenv(SSH_ASKPASS_ENV);
178 askpass = _PATH_SSH_ASKPASS_DEFAULT;
179 if ((flags & RP_ASK_PERMISSION) != 0)
180 askpass_hint = "confirm";
181 if ((ret = ssh_askpass(askpass, prompt, askpass_hint)) == NULL)
182 if (!(flags & RP_ALLOW_EOF))
187 if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
188 if (flags & RP_ALLOW_EOF)
194 explicit_bzero(buf, sizeof(buf));
199 ask_permission(const char *fmt, ...)
202 char *p, prompt[1024];
206 vsnprintf(prompt, sizeof(prompt), fmt, args);
209 p = read_passphrase(prompt,
210 RP_USE_ASKPASS|RP_ALLOW_EOF|RP_ASK_PERMISSION);
213 * Accept empty responses and responses consisting
214 * of the word "yes" as affirmative.
216 if (*p == '\0' || *p == '\n' ||
217 strcasecmp(p, "yes") == 0)
226 writemsg(const char *msg)
228 (void)write(STDERR_FILENO, "\r", 1);
229 (void)write(STDERR_FILENO, msg, strlen(msg));
230 (void)write(STDERR_FILENO, "\r\n", 2);
233 struct notifier_ctx {
235 void (*osigchld)(int);
238 struct notifier_ctx *
239 notify_start(int force_askpass, const char *fmt, ...)
244 void (*osigchld)(int) = NULL;
245 const char *askpass, *s;
246 struct notifier_ctx *ret = NULL;
249 xvasprintf(&prompt, fmt, args);
252 if (fflush(NULL) != 0)
253 error_f("fflush: %s", strerror(errno));
254 if (!force_askpass && isatty(STDERR_FILENO)) {
258 if ((askpass = getenv("SSH_ASKPASS")) == NULL)
259 askpass = _PATH_SSH_ASKPASS_DEFAULT;
260 if (*askpass == '\0') {
261 debug3_f("cannot notify: no askpass");
264 if (getenv("DISPLAY") == NULL &&
265 ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) == NULL ||
266 strcmp(s, "force") != 0)) {
267 debug3_f("cannot notify: no display");
270 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
271 if ((pid = fork()) == -1) {
272 error_f("fork: %s", strerror(errno));
273 ssh_signal(SIGCHLD, osigchld);
278 if (stdfd_devnull(1, 1, 0) == -1)
279 fatal_f("stdfd_devnull failed");
280 closefrom(STDERR_FILENO + 1);
281 setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */
282 execlp(askpass, askpass, prompt, (char *)NULL);
283 error_f("exec(%s): %s", askpass, strerror(errno));
288 if ((ret = calloc(1, sizeof(*ret))) == NULL) {
291 fatal_f("calloc failed");
294 ret->osigchld = osigchld;
301 notify_complete(struct notifier_ctx *ctx, const char *fmt, ...)
307 if (ctx != NULL && fmt != NULL && ctx->pid == -1) {
309 * notify_start wrote to stderr, so send conclusion message
313 xvasprintf(&msg, fmt, args);
319 if (ctx == NULL || ctx->pid <= 0) {
323 kill(ctx->pid, SIGTERM);
324 while ((ret = waitpid(ctx->pid, NULL, 0)) == -1) {
329 fatal_f("waitpid: %s", strerror(errno));
330 ssh_signal(SIGCHLD, ctx->osigchld);