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