Coverage Report

Created: 2026-02-26 06:46

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