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 "includes.h" 27 28 #include <sys/types.h> 29 #include <sys/wait.h> 30 31 #include <errno.h> 32 #include <fcntl.h> 33 #ifdef HAVE_PATHS_H 34 # include <paths.h> 35 #endif 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(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', *askpass = NULL, *ret, buf[1024]; 126 int rppflags, use_askpass = 0, ttyfd; 127 const char *askpass_hint = NULL; 128 129 rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF; 130 if (flags & RP_USE_ASKPASS) 131 use_askpass = 1; 132 else if (flags & RP_ALLOW_STDIN) { 133 if (!isatty(STDIN_FILENO)) { 134 debug("read_passphrase: stdin is not a tty"); 135 use_askpass = 1; 136 } 137 } else { 138 rppflags |= RPP_REQUIRE_TTY; 139 ttyfd = open(_PATH_TTY, O_RDWR); 140 if (ttyfd >= 0) { 141 /* 142 * If we're on a tty, ensure that show the prompt at 143 * the beginning of the line. This will hopefully 144 * clobber any password characters the user has 145 * optimistically typed before echo is disabled. 146 */ 147 (void)write(ttyfd, &cr, 1); 148 close(ttyfd); 149 } else { 150 debug("read_passphrase: can't open %s: %s", _PATH_TTY, 151 strerror(errno)); 152 use_askpass = 1; 153 } 154 } 155 156 if ((flags & RP_USE_ASKPASS) && getenv("DISPLAY") == NULL) 157 return (flags & RP_ALLOW_EOF) ? NULL : xstrdup(""); 158 159 if (use_askpass && getenv("DISPLAY")) { 160 if (getenv(SSH_ASKPASS_ENV)) 161 askpass = getenv(SSH_ASKPASS_ENV); 162 else 163 askpass = _PATH_SSH_ASKPASS_DEFAULT; 164 if ((flags & RP_ASK_PERMISSION) != 0) 165 askpass_hint = "confirm"; 166 if ((ret = ssh_askpass(askpass, prompt, askpass_hint)) == NULL) 167 if (!(flags & RP_ALLOW_EOF)) 168 return xstrdup(""); 169 return ret; 170 } 171 172 if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) { 173 if (flags & RP_ALLOW_EOF) 174 return NULL; 175 return xstrdup(""); 176 } 177 178 ret = xstrdup(buf); 179 explicit_bzero(buf, sizeof(buf)); 180 return ret; 181 } 182 183 int 184 ask_permission(const char *fmt, ...) 185 { 186 va_list args; 187 char *p, prompt[1024]; 188 int allowed = 0; 189 190 va_start(args, fmt); 191 vsnprintf(prompt, sizeof(prompt), fmt, args); 192 va_end(args); 193 194 p = read_passphrase(prompt, 195 RP_USE_ASKPASS|RP_ALLOW_EOF|RP_ASK_PERMISSION); 196 if (p != NULL) { 197 /* 198 * Accept empty responses and responses consisting 199 * of the word "yes" as affirmative. 200 */ 201 if (*p == '\0' || *p == '\n' || 202 strcasecmp(p, "yes") == 0) 203 allowed = 1; 204 free(p); 205 } 206 207 return (allowed); 208 } 209 210 struct notifier_ctx { 211 pid_t pid; 212 void (*osigchld)(int); 213 }; 214 215 struct notifier_ctx * 216 notify_start(int force_askpass, const char *fmt, ...) 217 { 218 va_list args; 219 char *prompt = NULL; 220 int devnull; 221 pid_t pid; 222 void (*osigchld)(int); 223 const char *askpass; 224 struct notifier_ctx *ret; 225 226 va_start(args, fmt); 227 xvasprintf(&prompt, fmt, args); 228 va_end(args); 229 230 if (fflush(NULL) != 0) 231 error("%s: fflush: %s", __func__, strerror(errno)); 232 if (!force_askpass && isatty(STDERR_FILENO)) { 233 (void)write(STDERR_FILENO, "\r", 1); 234 (void)write(STDERR_FILENO, prompt, strlen(prompt)); 235 (void)write(STDERR_FILENO, "\r\n", 2); 236 free(prompt); 237 return NULL; 238 } 239 if ((askpass = getenv("SSH_ASKPASS")) == NULL) 240 askpass = _PATH_SSH_ASKPASS_DEFAULT; 241 if (getenv("DISPLAY") == NULL || *askpass == '\0') { 242 debug3("%s: cannot notify", __func__); 243 free(prompt); 244 return NULL; 245 } 246 osigchld = ssh_signal(SIGCHLD, SIG_DFL); 247 if ((pid = fork()) == -1) { 248 error("%s: fork: %s", __func__, strerror(errno)); 249 ssh_signal(SIGCHLD, osigchld); 250 free(prompt); 251 return NULL; 252 } 253 if (pid == 0) { 254 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) 255 fatal("%s: open %s", __func__, strerror(errno)); 256 if (dup2(devnull, STDIN_FILENO) == -1 || 257 dup2(devnull, STDOUT_FILENO) == -1) 258 fatal("%s: dup2: %s", __func__, strerror(errno)); 259 closefrom(STDERR_FILENO + 1); 260 setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */ 261 execlp(askpass, askpass, prompt, (char *)NULL); 262 error("%s: exec(%s): %s", __func__, askpass, strerror(errno)); 263 _exit(1); 264 /* NOTREACHED */ 265 } 266 if ((ret = calloc(1, sizeof(*ret))) == NULL) { 267 kill(pid, SIGTERM); 268 fatal("%s: calloc failed", __func__); 269 } 270 ret->pid = pid; 271 ret->osigchld = osigchld; 272 free(prompt); 273 return ret; 274 } 275 276 void 277 notify_complete(struct notifier_ctx *ctx) 278 { 279 int ret; 280 281 if (ctx == NULL || ctx->pid <= 0) { 282 free(ctx); 283 return; 284 } 285 kill(ctx->pid, SIGTERM); 286 while ((ret = waitpid(ctx->pid, NULL, 0)) == -1) { 287 if (errno != EINTR) 288 break; 289 } 290 if (ret == -1) 291 fatal("%s: waitpid: %s", __func__, strerror(errno)); 292 ssh_signal(SIGCHLD, ctx->osigchld); 293 free(ctx); 294 } 295