Annotation of src/usr.bin/ssh/readpass.c, Revision 1.63
1.63 ! djm 1: /* $OpenBSD: readpass.c,v 1.62 2020/07/14 23:57:01 djm Exp $ */
1.1 deraadt 2: /*
1.26 markus 3: * Copyright (c) 2001 Markus Friedl. All rights reserved.
1.8 deraadt 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: *
1.26 markus 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.
1.6 deraadt 24: */
1.35 stevesk 25:
26: #include <sys/types.h>
27: #include <sys/wait.h>
1.19 markus 28:
1.42 stevesk 29: #include <errno.h>
1.40 stevesk 30: #include <fcntl.h>
1.34 stevesk 31: #include <paths.h>
1.19 markus 32: #include <readpassphrase.h>
1.48 djm 33: #include <signal.h>
1.41 stevesk 34: #include <stdarg.h>
1.46 stevesk 35: #include <stdio.h>
1.45 stevesk 36: #include <stdlib.h>
1.44 stevesk 37: #include <string.h>
1.43 stevesk 38: #include <unistd.h>
1.1 deraadt 39:
40: #include "xmalloc.h"
1.29 djm 41: #include "misc.h"
1.15 markus 42: #include "pathnames.h"
43: #include "log.h"
44: #include "ssh.h"
1.38 markus 45: #include "uidswap.h"
1.15 markus 46:
1.18 itojun 47: static char *
1.60 djm 48: ssh_askpass(char *askpass, const char *msg, const char *env_hint)
1.15 markus 49: {
1.48 djm 50: pid_t pid, ret;
1.15 markus 51: size_t len;
1.23 markus 52: char *pass;
1.48 djm 53: int p[2], status;
1.15 markus 54: char buf[1024];
1.48 djm 55: void (*osigchld)(int);
1.15 markus 56:
57: if (fflush(stdout) != 0)
1.59 djm 58: error("%s: fflush: %s", __func__, strerror(errno));
1.15 markus 59: if (askpass == NULL)
60: fatal("internal error: askpass undefined");
1.54 deraadt 61: if (pipe(p) == -1) {
1.59 djm 62: error("%s: pipe: %s", __func__, strerror(errno));
1.28 markus 63: return NULL;
1.21 markus 64: }
1.61 dtucker 65: osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.54 deraadt 66: if ((pid = fork()) == -1) {
1.59 djm 67: error("%s: fork: %s", __func__, strerror(errno));
1.61 dtucker 68: ssh_signal(SIGCHLD, osigchld);
1.28 markus 69: return NULL;
1.21 markus 70: }
1.15 markus 71: if (pid == 0) {
72: close(p[0]);
1.54 deraadt 73: if (dup2(p[1], STDOUT_FILENO) == -1)
1.59 djm 74: fatal("%s: dup2: %s", __func__, strerror(errno));
1.60 djm 75: if (env_hint != NULL)
76: setenv("SSH_ASKPASS_PROMPT", env_hint, 1);
1.51 mmcc 77: execlp(askpass, askpass, msg, (char *)NULL);
1.59 djm 78: fatal("%s: exec(%s): %s", __func__, askpass, strerror(errno));
1.15 markus 79: }
80: close(p[1]);
1.24 djm 81:
1.48 djm 82: len = 0;
1.24 djm 83: do {
1.48 djm 84: ssize_t r = read(p[0], buf + len, sizeof(buf) - 1 - len);
85:
86: if (r == -1 && errno == EINTR)
1.24 djm 87: continue;
1.48 djm 88: if (r <= 0)
1.24 djm 89: break;
1.48 djm 90: len += r;
1.24 djm 91: } while (sizeof(buf) - 1 - len > 0);
92: buf[len] = '\0';
93:
1.15 markus 94: close(p[0]);
1.54 deraadt 95: while ((ret = waitpid(pid, &status, 0)) == -1)
1.15 markus 96: if (errno != EINTR)
97: break;
1.61 dtucker 98: ssh_signal(SIGCHLD, osigchld);
1.48 djm 99: if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1.50 djm 100: explicit_bzero(buf, sizeof(buf));
1.28 markus 101: return NULL;
102: }
103:
1.23 markus 104: buf[strcspn(buf, "\r\n")] = '\0';
1.15 markus 105: pass = xstrdup(buf);
1.50 djm 106: explicit_bzero(buf, sizeof(buf));
1.15 markus 107: return pass;
108: }
109:
1.60 djm 110: /* private/internal read_passphrase flags */
111: #define RP_ASK_PERMISSION 0x8000 /* pass hint to askpass for confirm UI */
112:
1.7 markus 113: /*
1.19 markus 114: * Reads a passphrase from /dev/tty with echo turned off/on. Returns the
115: * passphrase (allocated with xmalloc). Exits if EOF is encountered. If
116: * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
117: * tty is available
1.12 markus 118: */
1.5 markus 119: char *
1.19 markus 120: read_passphrase(const char *prompt, int flags)
1.1 deraadt 121: {
1.53 tb 122: char cr = '\r', *askpass = NULL, *ret, buf[1024];
1.62 djm 123: int rppflags, ttyfd, use_askpass = 0, allow_askpass = 0;
1.60 djm 124: const char *askpass_hint = NULL;
1.62 djm 125: const char *s;
126:
127: if ((s = getenv("DISPLAY")) != NULL)
128: allow_askpass = *s != '\0';
129: if ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) != NULL) {
130: if (strcasecmp(s, "force") == 0) {
131: use_askpass = 1;
132: allow_askpass = 1;
133: } else if (strcasecmp(s, "prefer") == 0)
134: use_askpass = allow_askpass;
135: else if (strcasecmp(s, "never") == 0)
136: allow_askpass = 0;
137: }
1.15 markus 138:
1.19 markus 139: rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
1.62 djm 140: if (use_askpass)
141: debug("%s: requested to askpass", __func__);
142: else if (flags & RP_USE_ASKPASS)
1.30 djm 143: use_askpass = 1;
144: else if (flags & RP_ALLOW_STDIN) {
1.33 markus 145: if (!isatty(STDIN_FILENO)) {
1.32 dtucker 146: debug("read_passphrase: stdin is not a tty");
1.15 markus 147: use_askpass = 1;
1.33 markus 148: }
1.15 markus 149: } else {
1.19 markus 150: rppflags |= RPP_REQUIRE_TTY;
1.25 markus 151: ttyfd = open(_PATH_TTY, O_RDWR);
1.53 tb 152: if (ttyfd >= 0) {
153: /*
154: * If we're on a tty, ensure that show the prompt at
155: * the beginning of the line. This will hopefully
156: * clobber any password characters the user has
157: * optimistically typed before echo is disabled.
158: */
159: (void)write(ttyfd, &cr, 1);
1.15 markus 160: close(ttyfd);
1.53 tb 161: } else {
1.32 dtucker 162: debug("read_passphrase: can't open %s: %s", _PATH_TTY,
163: strerror(errno));
1.15 markus 164: use_askpass = 1;
1.32 dtucker 165: }
1.15 markus 166: }
1.30 djm 167:
1.62 djm 168: if ((flags & RP_USE_ASKPASS) && !allow_askpass)
1.30 djm 169: return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
1.15 markus 170:
1.62 djm 171: if (use_askpass && allow_askpass) {
1.15 markus 172: if (getenv(SSH_ASKPASS_ENV))
173: askpass = getenv(SSH_ASKPASS_ENV);
174: else
175: askpass = _PATH_SSH_ASKPASS_DEFAULT;
1.60 djm 176: if ((flags & RP_ASK_PERMISSION) != 0)
177: askpass_hint = "confirm";
178: if ((ret = ssh_askpass(askpass, prompt, askpass_hint)) == NULL)
1.28 markus 179: if (!(flags & RP_ALLOW_EOF))
180: return xstrdup("");
181: return ret;
1.15 markus 182: }
183:
1.27 markus 184: if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
185: if (flags & RP_ALLOW_EOF)
186: return NULL;
1.20 markus 187: return xstrdup("");
1.27 markus 188: }
1.19 markus 189:
190: ret = xstrdup(buf);
1.50 djm 191: explicit_bzero(buf, sizeof(buf));
1.19 markus 192: return ret;
1.31 djm 193: }
194:
195: int
196: ask_permission(const char *fmt, ...)
197: {
198: va_list args;
199: char *p, prompt[1024];
200: int allowed = 0;
201:
202: va_start(args, fmt);
203: vsnprintf(prompt, sizeof(prompt), fmt, args);
204: va_end(args);
205:
1.60 djm 206: p = read_passphrase(prompt,
207: RP_USE_ASKPASS|RP_ALLOW_EOF|RP_ASK_PERMISSION);
1.31 djm 208: if (p != NULL) {
209: /*
210: * Accept empty responses and responses consisting
211: * of the word "yes" as affirmative.
212: */
213: if (*p == '\0' || *p == '\n' ||
214: strcasecmp(p, "yes") == 0)
215: allowed = 1;
1.49 djm 216: free(p);
1.31 djm 217: }
218:
219: return (allowed);
1.55 djm 220: }
221:
222: struct notifier_ctx {
223: pid_t pid;
224: void (*osigchld)(int);
225: };
226:
227: struct notifier_ctx *
228: notify_start(int force_askpass, const char *fmt, ...)
229: {
230: va_list args;
231: char *prompt = NULL;
232: int devnull;
233: pid_t pid;
234: void (*osigchld)(int);
1.63 ! djm 235: const char *askpass, *s;
! 236: struct notifier_ctx *ret = NULL;
1.55 djm 237:
238: va_start(args, fmt);
239: xvasprintf(&prompt, fmt, args);
240: va_end(args);
241:
242: if (fflush(NULL) != 0)
243: error("%s: fflush: %s", __func__, strerror(errno));
244: if (!force_askpass && isatty(STDERR_FILENO)) {
245: (void)write(STDERR_FILENO, "\r", 1);
246: (void)write(STDERR_FILENO, prompt, strlen(prompt));
247: (void)write(STDERR_FILENO, "\r\n", 2);
1.63 ! djm 248: goto out;
1.55 djm 249: }
1.57 djm 250: if ((askpass = getenv("SSH_ASKPASS")) == NULL)
251: askpass = _PATH_SSH_ASKPASS_DEFAULT;
1.63 ! djm 252: if (*askpass == '\0') {
! 253: debug3("%s: cannot notify: no askpass", __func__);
! 254: goto out;
! 255: }
! 256: if (getenv("DISPLAY") == NULL &&
! 257: ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) == NULL ||
! 258: strcmp(s, "force") != 0)) {
! 259: debug3("%s: cannot notify: no display", __func__);
! 260: goto out;
1.55 djm 261: }
1.61 dtucker 262: osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.55 djm 263: if ((pid = fork()) == -1) {
264: error("%s: fork: %s", __func__, strerror(errno));
1.61 dtucker 265: ssh_signal(SIGCHLD, osigchld);
1.55 djm 266: free(prompt);
267: return NULL;
268: }
269: if (pid == 0) {
270: if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
271: fatal("%s: open %s", __func__, strerror(errno));
272: if (dup2(devnull, STDIN_FILENO) == -1 ||
273: dup2(devnull, STDOUT_FILENO) == -1)
274: fatal("%s: dup2: %s", __func__, strerror(errno));
275: closefrom(STDERR_FILENO + 1);
276: setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */
277: execlp(askpass, askpass, prompt, (char *)NULL);
1.58 djm 278: error("%s: exec(%s): %s", __func__, askpass, strerror(errno));
279: _exit(1);
1.55 djm 280: /* NOTREACHED */
281: }
282: if ((ret = calloc(1, sizeof(*ret))) == NULL) {
283: kill(pid, SIGTERM);
284: fatal("%s: calloc failed", __func__);
285: }
286: ret->pid = pid;
287: ret->osigchld = osigchld;
1.63 ! djm 288: out:
1.55 djm 289: free(prompt);
290: return ret;
291: }
292:
293: void
294: notify_complete(struct notifier_ctx *ctx)
295: {
296: int ret;
297:
298: if (ctx == NULL || ctx->pid <= 0) {
299: free(ctx);
300: return;
301: }
302: kill(ctx->pid, SIGTERM);
303: while ((ret = waitpid(ctx->pid, NULL, 0)) == -1) {
304: if (errno != EINTR)
305: break;
306: }
307: if (ret == -1)
308: fatal("%s: waitpid: %s", __func__, strerror(errno));
1.61 dtucker 309: ssh_signal(SIGCHLD, ctx->osigchld);
1.55 djm 310: free(ctx);
1.1 deraadt 311: }