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