1*1c7715ddSchristos /* $NetBSD: readpass.c,v 1.19 2024/07/08 22:33:44 christos Exp $ */
2*1c7715ddSchristos /* $OpenBSD: readpass.c,v 1.71 2024/03/30 04:27:44 djm Exp $ */
32d3b0f52Schristos
4ca32bd8dSchristos /*
5ca32bd8dSchristos * Copyright (c) 2001 Markus Friedl. All rights reserved.
6ca32bd8dSchristos *
7ca32bd8dSchristos * Redistribution and use in source and binary forms, with or without
8ca32bd8dSchristos * modification, are permitted provided that the following conditions
9ca32bd8dSchristos * are met:
10ca32bd8dSchristos * 1. Redistributions of source code must retain the above copyright
11ca32bd8dSchristos * notice, this list of conditions and the following disclaimer.
12ca32bd8dSchristos * 2. Redistributions in binary form must reproduce the above copyright
13ca32bd8dSchristos * notice, this list of conditions and the following disclaimer in the
14ca32bd8dSchristos * documentation and/or other materials provided with the distribution.
15ca32bd8dSchristos *
16ca32bd8dSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ca32bd8dSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ca32bd8dSchristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ca32bd8dSchristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ca32bd8dSchristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ca32bd8dSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ca32bd8dSchristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ca32bd8dSchristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ca32bd8dSchristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ca32bd8dSchristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ca32bd8dSchristos */
27ca32bd8dSchristos
28313c6c94Schristos #include "includes.h"
29*1c7715ddSchristos __RCSID("$NetBSD: readpass.c,v 1.19 2024/07/08 22:33:44 christos Exp $");
30ca32bd8dSchristos #include <sys/types.h>
31ca32bd8dSchristos #include <sys/wait.h>
32ca32bd8dSchristos
33ca32bd8dSchristos #include <errno.h>
34ca32bd8dSchristos #include <fcntl.h>
35ca32bd8dSchristos #include <paths.h>
36ca32bd8dSchristos #include <readpassphrase.h>
37185c8f97Schristos #include <signal.h>
38ca32bd8dSchristos #include <stdarg.h>
39ca32bd8dSchristos #include <stdio.h>
40ca32bd8dSchristos #include <stdlib.h>
41ca32bd8dSchristos #include <string.h>
42ca32bd8dSchristos #include <unistd.h>
43ca32bd8dSchristos
44ca32bd8dSchristos #include "xmalloc.h"
45ca32bd8dSchristos #include "misc.h"
46ca32bd8dSchristos #include "pathnames.h"
47ca32bd8dSchristos #include "log.h"
48ca32bd8dSchristos #include "ssh.h"
49ca32bd8dSchristos #include "uidswap.h"
50ca32bd8dSchristos
51ca32bd8dSchristos static char *
ssh_askpass(const char * askpass,const char * msg,const char * env_hint)52ed75d7a8Schristos ssh_askpass(const char *askpass, const char *msg, const char *env_hint)
53ca32bd8dSchristos {
54185c8f97Schristos pid_t pid, ret;
55ca32bd8dSchristos size_t len;
56ca32bd8dSchristos char *pass;
57185c8f97Schristos int p[2], status;
58ca32bd8dSchristos char buf[1024];
59185c8f97Schristos void (*osigchld)(int);
60ca32bd8dSchristos
61ca32bd8dSchristos if (fflush(stdout) != 0)
6217418e98Schristos error_f("fflush: %s", strerror(errno));
63ca32bd8dSchristos if (askpass == NULL)
64ca32bd8dSchristos fatal("internal error: askpass undefined");
65cd4ada6aSchristos if (pipe(p) == -1) {
6617418e98Schristos error_f("pipe: %s", strerror(errno));
67ca32bd8dSchristos return NULL;
68ca32bd8dSchristos }
69ed75d7a8Schristos osigchld = ssh_signal(SIGCHLD, SIG_DFL);
70cd4ada6aSchristos if ((pid = fork()) == -1) {
7117418e98Schristos error_f("fork: %s", strerror(errno));
72ed75d7a8Schristos ssh_signal(SIGCHLD, osigchld);
73ca32bd8dSchristos return NULL;
74ca32bd8dSchristos }
75ca32bd8dSchristos if (pid == 0) {
76ca32bd8dSchristos close(p[0]);
77cd4ada6aSchristos if (dup2(p[1], STDOUT_FILENO) == -1)
7817418e98Schristos fatal_f("dup2: %s", strerror(errno));
79ed75d7a8Schristos if (env_hint != NULL)
80ed75d7a8Schristos setenv("SSH_ASKPASS_PROMPT", env_hint, 1);
8179976551Schristos execlp(askpass, askpass, msg, (char *)NULL);
8217418e98Schristos fatal_f("exec(%s): %s", askpass, strerror(errno));
83ca32bd8dSchristos }
84ca32bd8dSchristos close(p[1]);
85ca32bd8dSchristos
86185c8f97Schristos len = 0;
87ca32bd8dSchristos do {
88185c8f97Schristos ssize_t r = read(p[0], buf + len, sizeof(buf) - 1 - len);
89185c8f97Schristos
90185c8f97Schristos if (r == -1 && errno == EINTR)
91ca32bd8dSchristos continue;
92185c8f97Schristos if (r <= 0)
93ca32bd8dSchristos break;
94185c8f97Schristos len += r;
95ca32bd8dSchristos } while (sizeof(buf) - 1 - len > 0);
96ca32bd8dSchristos buf[len] = '\0';
97ca32bd8dSchristos
98ca32bd8dSchristos close(p[0]);
99cd4ada6aSchristos while ((ret = waitpid(pid, &status, 0)) == -1)
100ca32bd8dSchristos if (errno != EINTR)
101ca32bd8dSchristos break;
102ed75d7a8Schristos ssh_signal(SIGCHLD, osigchld);
103185c8f97Schristos if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1048a4530f9Schristos explicit_bzero(buf, sizeof(buf));
105ca32bd8dSchristos return NULL;
106ca32bd8dSchristos }
107ca32bd8dSchristos
108ca32bd8dSchristos buf[strcspn(buf, "\r\n")] = '\0';
109ca32bd8dSchristos pass = xstrdup(buf);
1108a4530f9Schristos explicit_bzero(buf, sizeof(buf));
111ca32bd8dSchristos return pass;
112ca32bd8dSchristos }
113ca32bd8dSchristos
114ed75d7a8Schristos /* private/internal read_passphrase flags */
115ed75d7a8Schristos #define RP_ASK_PERMISSION 0x8000 /* pass hint to askpass for confirm UI */
116ed75d7a8Schristos
117ca32bd8dSchristos /*
118ca32bd8dSchristos * Reads a passphrase from /dev/tty with echo turned off/on. Returns the
119ca32bd8dSchristos * passphrase (allocated with xmalloc). Exits if EOF is encountered. If
120ca32bd8dSchristos * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
121b592f463Schristos * tty is or askpass program is available
122ca32bd8dSchristos */
123ca32bd8dSchristos char *
read_passphrase(const char * prompt,int flags)124ca32bd8dSchristos read_passphrase(const char *prompt, int flags)
125ca32bd8dSchristos {
126aa36fcacSchristos char cr = '\r', *ret, buf[1024];
1272d3b0f52Schristos int rppflags, ttyfd, use_askpass = 0, allow_askpass = 0;
1282d3b0f52Schristos const char *askpass_hint = NULL, *askpass = NULL;
1292d3b0f52Schristos const char *s;
1302d3b0f52Schristos
131*1c7715ddSchristos if (((s = getenv("DISPLAY")) != NULL && *s != '\0') ||
132*1c7715ddSchristos ((s = getenv("WAYLAND_DISPLAY")) != NULL && *s != '\0'))
133*1c7715ddSchristos allow_askpass = 1;
1342d3b0f52Schristos if ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) != NULL) {
1352d3b0f52Schristos if (strcasecmp(s, "force") == 0) {
1362d3b0f52Schristos use_askpass = 1;
1372d3b0f52Schristos allow_askpass = 1;
1382d3b0f52Schristos } else if (strcasecmp(s, "prefer") == 0)
1392d3b0f52Schristos use_askpass = allow_askpass;
1402d3b0f52Schristos else if (strcasecmp(s, "never") == 0)
1412d3b0f52Schristos allow_askpass = 0;
1422d3b0f52Schristos }
143ca32bd8dSchristos
144ca32bd8dSchristos rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
1452d3b0f52Schristos if (use_askpass)
14617418e98Schristos debug_f("requested to askpass");
1472d3b0f52Schristos else if (flags & RP_USE_ASKPASS)
148ca32bd8dSchristos use_askpass = 1;
149ca32bd8dSchristos else if (flags & RP_ALLOW_STDIN) {
150ca32bd8dSchristos if (!isatty(STDIN_FILENO)) {
151b592f463Schristos debug_f("stdin is not a tty");
152ca32bd8dSchristos use_askpass = 1;
153ca32bd8dSchristos }
154ca32bd8dSchristos } else {
155ca32bd8dSchristos rppflags |= RPP_REQUIRE_TTY;
156ca32bd8dSchristos ttyfd = open(_PATH_TTY, O_RDWR);
157aa36fcacSchristos if (ttyfd >= 0) {
158aa36fcacSchristos /*
159aa36fcacSchristos * If we're on a tty, ensure that show the prompt at
160aa36fcacSchristos * the beginning of the line. This will hopefully
161aa36fcacSchristos * clobber any password characters the user has
162aa36fcacSchristos * optimistically typed before echo is disabled.
163aa36fcacSchristos */
164aa36fcacSchristos (void)write(ttyfd, &cr, 1);
165ca32bd8dSchristos close(ttyfd);
166aa36fcacSchristos } else {
167b592f463Schristos debug_f("can't open %s: %s", _PATH_TTY,
168ca32bd8dSchristos strerror(errno));
169ca32bd8dSchristos use_askpass = 1;
170ca32bd8dSchristos }
171ca32bd8dSchristos }
172ca32bd8dSchristos
1732d3b0f52Schristos if ((flags & RP_USE_ASKPASS) && !allow_askpass)
174ca32bd8dSchristos return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
175ca32bd8dSchristos
1762d3b0f52Schristos if (use_askpass && allow_askpass) {
177ca32bd8dSchristos if (getenv(SSH_ASKPASS_ENV))
178ca32bd8dSchristos askpass = getenv(SSH_ASKPASS_ENV);
179ca32bd8dSchristos else
180ca32bd8dSchristos askpass = _PATH_SSH_ASKPASS_DEFAULT;
181ed75d7a8Schristos if ((flags & RP_ASK_PERMISSION) != 0)
182ed75d7a8Schristos askpass_hint = "confirm";
183ed75d7a8Schristos if ((ret = ssh_askpass(askpass, prompt, askpass_hint)) == NULL)
184ca32bd8dSchristos if (!(flags & RP_ALLOW_EOF))
185ca32bd8dSchristos return xstrdup("");
186ca32bd8dSchristos return ret;
187ca32bd8dSchristos }
188ca32bd8dSchristos
189ca32bd8dSchristos if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
190ca32bd8dSchristos if (flags & RP_ALLOW_EOF)
191ca32bd8dSchristos return NULL;
192ca32bd8dSchristos return xstrdup("");
193ca32bd8dSchristos }
194ca32bd8dSchristos
195ca32bd8dSchristos ret = xstrdup(buf);
1968a4530f9Schristos explicit_bzero(buf, sizeof(buf));
197ca32bd8dSchristos return ret;
198ca32bd8dSchristos }
199ca32bd8dSchristos
200ca32bd8dSchristos int
ask_permission(const char * fmt,...)201ca32bd8dSchristos ask_permission(const char *fmt, ...)
202ca32bd8dSchristos {
203ca32bd8dSchristos va_list args;
204ca32bd8dSchristos char *p, prompt[1024];
205ca32bd8dSchristos int allowed = 0;
206ca32bd8dSchristos
207ca32bd8dSchristos va_start(args, fmt);
208ca32bd8dSchristos vsnprintf(prompt, sizeof(prompt), fmt, args);
209ca32bd8dSchristos va_end(args);
210ca32bd8dSchristos
211ed75d7a8Schristos p = read_passphrase(prompt,
212ed75d7a8Schristos RP_USE_ASKPASS|RP_ALLOW_EOF|RP_ASK_PERMISSION);
213ca32bd8dSchristos if (p != NULL) {
214ca32bd8dSchristos /*
215ca32bd8dSchristos * Accept empty responses and responses consisting
216ca32bd8dSchristos * of the word "yes" as affirmative.
217ca32bd8dSchristos */
218ca32bd8dSchristos if (*p == '\0' || *p == '\n' ||
219ca32bd8dSchristos strcasecmp(p, "yes") == 0)
220ca32bd8dSchristos allowed = 1;
22100a838c4Schristos free(p);
222ca32bd8dSchristos }
223ca32bd8dSchristos
224ca32bd8dSchristos return (allowed);
225ca32bd8dSchristos }
226ed75d7a8Schristos
22717418e98Schristos static void
writemsg(const char * msg)22817418e98Schristos writemsg(const char *msg)
22917418e98Schristos {
23017418e98Schristos (void)write(STDERR_FILENO, "\r", 1);
23117418e98Schristos (void)write(STDERR_FILENO, msg, strlen(msg));
23217418e98Schristos (void)write(STDERR_FILENO, "\r\n", 2);
23317418e98Schristos }
23417418e98Schristos
235ed75d7a8Schristos struct notifier_ctx {
236ed75d7a8Schristos pid_t pid;
237ed75d7a8Schristos void (*osigchld)(int);
238ed75d7a8Schristos };
239ed75d7a8Schristos
240ed75d7a8Schristos struct notifier_ctx *
notify_start(int force_askpass,const char * fmt,...)241ed75d7a8Schristos notify_start(int force_askpass, const char *fmt, ...)
242ed75d7a8Schristos {
243ed75d7a8Schristos va_list args;
244ed75d7a8Schristos char *prompt = NULL;
24517418e98Schristos pid_t pid = -1;
24617418e98Schristos void (*osigchld)(int) = NULL;
2472d3b0f52Schristos const char *askpass, *s;
2482d3b0f52Schristos struct notifier_ctx *ret = NULL;
249ed75d7a8Schristos
250ed75d7a8Schristos va_start(args, fmt);
251ed75d7a8Schristos xvasprintf(&prompt, fmt, args);
252ed75d7a8Schristos va_end(args);
253ed75d7a8Schristos
254ed75d7a8Schristos if (fflush(NULL) != 0)
25517418e98Schristos error_f("fflush: %s", strerror(errno));
256ed75d7a8Schristos if (!force_askpass && isatty(STDERR_FILENO)) {
25717418e98Schristos writemsg(prompt);
25817418e98Schristos goto out_ctx;
259ed75d7a8Schristos }
260ed75d7a8Schristos if ((askpass = getenv("SSH_ASKPASS")) == NULL)
261ed75d7a8Schristos askpass = _PATH_SSH_ASKPASS_DEFAULT;
2622d3b0f52Schristos if (*askpass == '\0') {
26317418e98Schristos debug3_f("cannot notify: no askpass");
2642d3b0f52Schristos goto out;
2652d3b0f52Schristos }
266*1c7715ddSchristos if (getenv("DISPLAY") == NULL && getenv("WAYLAND_DISPLAY") == NULL &&
2672d3b0f52Schristos ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) == NULL ||
2682d3b0f52Schristos strcmp(s, "force") != 0)) {
26917418e98Schristos debug3_f("cannot notify: no display");
2702d3b0f52Schristos goto out;
271ed75d7a8Schristos }
272ed75d7a8Schristos osigchld = ssh_signal(SIGCHLD, SIG_DFL);
273ed75d7a8Schristos if ((pid = fork()) == -1) {
27417418e98Schristos error_f("fork: %s", strerror(errno));
275ed75d7a8Schristos ssh_signal(SIGCHLD, osigchld);
276ed75d7a8Schristos free(prompt);
277ed75d7a8Schristos return NULL;
278ed75d7a8Schristos }
279ed75d7a8Schristos if (pid == 0) {
28017418e98Schristos if (stdfd_devnull(1, 1, 0) == -1)
28117418e98Schristos fatal_f("stdfd_devnull failed");
282ed75d7a8Schristos closefrom(STDERR_FILENO + 1);
283ed75d7a8Schristos setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */
284ed75d7a8Schristos execlp(askpass, askpass, prompt, (char *)NULL);
28517418e98Schristos error_f("exec(%s): %s", askpass, strerror(errno));
286ed75d7a8Schristos _exit(1);
287ed75d7a8Schristos /* NOTREACHED */
288ed75d7a8Schristos }
28917418e98Schristos out_ctx:
290ed75d7a8Schristos if ((ret = calloc(1, sizeof(*ret))) == NULL) {
291e160b4e8Schristos if (pid != -1)
292ed75d7a8Schristos kill(pid, SIGTERM);
29317418e98Schristos fatal_f("calloc failed");
294ed75d7a8Schristos }
295ed75d7a8Schristos ret->pid = pid;
296ed75d7a8Schristos ret->osigchld = osigchld;
2972d3b0f52Schristos out:
298ed75d7a8Schristos free(prompt);
299ed75d7a8Schristos return ret;
300ed75d7a8Schristos }
301ed75d7a8Schristos
302ed75d7a8Schristos void
notify_complete(struct notifier_ctx * ctx,const char * fmt,...)30317418e98Schristos notify_complete(struct notifier_ctx *ctx, const char *fmt, ...)
304ed75d7a8Schristos {
305ed75d7a8Schristos int ret;
30617418e98Schristos char *msg = NULL;
30717418e98Schristos va_list args;
30817418e98Schristos
30917418e98Schristos if (ctx != NULL && fmt != NULL && ctx->pid == -1) {
31017418e98Schristos /*
31117418e98Schristos * notify_start wrote to stderr, so send conclusion message
31217418e98Schristos * there too
31317418e98Schristos */
31417418e98Schristos va_start(args, fmt);
31517418e98Schristos xvasprintf(&msg, fmt, args);
31617418e98Schristos va_end(args);
31717418e98Schristos writemsg(msg);
31817418e98Schristos free(msg);
31917418e98Schristos }
320ed75d7a8Schristos
321ed75d7a8Schristos if (ctx == NULL || ctx->pid <= 0) {
322ed75d7a8Schristos free(ctx);
323ed75d7a8Schristos return;
324ed75d7a8Schristos }
325ed75d7a8Schristos kill(ctx->pid, SIGTERM);
326ed75d7a8Schristos while ((ret = waitpid(ctx->pid, NULL, 0)) == -1) {
327ed75d7a8Schristos if (errno != EINTR)
328ed75d7a8Schristos break;
329ed75d7a8Schristos }
330ed75d7a8Schristos if (ret == -1)
33117418e98Schristos fatal_f("waitpid: %s", strerror(errno));
332ed75d7a8Schristos ssh_signal(SIGCHLD, ctx->osigchld);
333ed75d7a8Schristos free(ctx);
334ed75d7a8Schristos }
335