Coverage Report

Created: 2026-10-17 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/openbsd-compat/readpassphrase.c
Line
Count
Source
1
/*  $OpenBSD: readpassphrase.c,v 1.32 2026/10/15 16:40:50 millert Exp $ */
2
3
/*
4
 * Copyright (c) 2000-2002, 2007, 2010
5
 *  Todd C. Miller <millert@openbsd.org>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 *
19
 * Sponsored in part by the Defense Advanced Research Projects
20
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22
 */
23
24
/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
25
26
#include "includes.h"
27
28
#ifndef HAVE_READPASSPHRASE
29
30
#include <termios.h>
31
#include <signal.h>
32
#include <ctype.h>
33
#include <fcntl.h>
34
#include <readpassphrase.h>
35
#include <errno.h>
36
#include <string.h>
37
#include <unistd.h>
38
39
#ifndef TCSASOFT
40
/* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
41
0
# define TCSASOFT 0
42
#endif
43
44
/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45
#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46
#  define _POSIX_VDISABLE       VDISABLE
47
#endif
48
49
static volatile sig_atomic_t signo[_NSIG];
50
51
static void handler(int);
52
53
/* Like sigaction(2) but preserves SIG_IGN */
54
static void
55
sigaction_except_ign(int signum, struct sigaction *act, struct sigaction *old)
56
0
{
57
0
  struct sigaction sabuf;
58
59
0
  if (old == NULL)
60
0
    old = &sabuf;
61
62
0
  (void)sigaction(signum, act, old);
63
  /* Reinstate SIG_IGN; we don't want to override this */
64
0
  if (old->sa_handler == SIG_IGN && act->sa_handler != SIG_IGN)
65
0
    (void)sigaction(signum, old, NULL);
66
0
}
67
68
char *
69
readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
70
0
{
71
0
  ssize_t nr;
72
0
  int input, output, save_errno, i, need_restart;
73
0
  char ch, *p, *end;
74
0
  struct termios term, oterm;
75
0
  struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
76
0
  struct sigaction savetstp, savettin, savettou, savepipe;
77
78
  /* I suppose we could alloc on demand in this case (XXX). */
79
0
  if (bufsiz == 0) {
80
0
    errno = EINVAL;
81
0
    return(NULL);
82
0
  }
83
84
0
restart:
85
0
  for (i = 0; i < _NSIG; i++)
86
0
    signo[i] = 0;
87
0
  nr = -1;
88
0
  save_errno = 0;
89
0
  need_restart = 0;
90
  /*
91
   * Read and write to /dev/tty if available.  If not, read from
92
   * stdin and write to stderr unless a tty is required.
93
   */
94
#ifndef O_CLOEXEC
95
# define O_CLOEXEC 0
96
#endif
97
0
  if ((flags & RPP_STDIN) ||
98
0
      (input = output = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) == -1) {
99
0
    if (flags & RPP_REQUIRE_TTY) {
100
0
      errno = ENOTTY;
101
0
      return(NULL);
102
0
    }
103
0
    input = STDIN_FILENO;
104
0
    output = STDERR_FILENO;
105
0
  }
106
107
  /*
108
   * Turn off echo if possible.
109
   * If we are using a tty but are not the foreground pgrp this will
110
   * generate SIGTTOU, so do it *before* installing the signal handlers.
111
   */
112
0
  if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
113
0
    memcpy(&term, &oterm, sizeof(term));
114
0
    if (!(flags & RPP_ECHO_ON))
115
0
      term.c_lflag &= ~(ECHO | ECHONL);
116
#ifdef VSTATUS
117
    if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
118
      term.c_cc[VSTATUS] = _POSIX_VDISABLE;
119
#endif
120
0
    (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
121
0
  } else {
122
0
    memset(&term, 0, sizeof(term));
123
0
    term.c_lflag |= ECHO;
124
0
    memset(&oterm, 0, sizeof(oterm));
125
0
    oterm.c_lflag |= ECHO;
126
0
  }
127
128
  /*
129
   * Catch signals that would otherwise cause the user to end
130
   * up with echo turned off in the shell.  Don't worry about
131
   * things like SIGXCPU and SIGVTALRM for now.
132
   */
133
0
  sigemptyset(&sa.sa_mask);
134
0
  sa.sa_flags = 0;    /* don't restart system calls */
135
0
  sa.sa_handler = handler;
136
0
  sigaction(SIGINT, &sa, &saveint);
137
0
  sigaction(SIGQUIT, &sa, &savequit);
138
0
  sigaction_except_ign(SIGALRM, &sa, &savealrm);
139
0
  sigaction_except_ign(SIGHUP, &sa, &savehup);
140
0
  sigaction_except_ign(SIGPIPE, &sa, &savepipe);
141
0
  sigaction_except_ign(SIGTERM, &sa, &saveterm);
142
0
  sigaction_except_ign(SIGTSTP, &sa, &savetstp);
143
0
  sigaction_except_ign(SIGTTIN, &sa, &savettin);
144
0
  sigaction_except_ign(SIGTTOU, &sa, &savettou);
145
146
0
  if (!(flags & RPP_STDIN))
147
0
    (void)write(output, prompt, strlen(prompt));
148
0
  end = buf + bufsiz - 1;
149
0
  p = buf;
150
0
  while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
151
0
    if (p < end) {
152
0
      if ((flags & RPP_SEVENBIT))
153
0
        ch &= 0x7f;
154
0
      if (isalpha((unsigned char)ch)) {
155
0
        if ((flags & RPP_FORCELOWER))
156
0
          ch = (char)tolower((unsigned char)ch);
157
0
        if ((flags & RPP_FORCEUPPER))
158
0
          ch = (char)toupper((unsigned char)ch);
159
0
      }
160
0
      *p++ = ch;
161
0
    }
162
0
  }
163
0
  *p = '\0';
164
0
  save_errno = errno;
165
0
  if (!(term.c_lflag & ECHO) && !(nr == -1 && errno == EINTR))
166
0
    (void)write(output, "\n", 1);
167
168
  /* Restore old terminal settings and signals. */
169
0
  if (memcmp(&term, &oterm, sizeof(term)) != 0) {
170
0
    const int sigttou = signo[SIGTTOU];
171
172
    /* Ignore SIGTTOU generated when we are not the fg pgrp. */
173
0
    while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
174
0
        errno == EINTR && !signo[SIGTTOU])
175
0
      continue;
176
0
    signo[SIGTTOU] = sigttou;
177
0
  }
178
0
  sigaction(SIGINT, &saveint, NULL);
179
0
  sigaction(SIGQUIT, &savequit, NULL);
180
0
  sigaction_except_ign(SIGALRM, &savealrm, NULL);
181
0
  sigaction_except_ign(SIGHUP, &savehup, NULL);
182
0
  sigaction_except_ign(SIGPIPE, &savepipe, NULL);
183
0
  sigaction_except_ign(SIGTERM, &saveterm, NULL);
184
0
  sigaction_except_ign(SIGTSTP, &savetstp, NULL);
185
0
  sigaction_except_ign(SIGTTIN, &savettin, NULL);
186
0
  sigaction_except_ign(SIGTTOU, &savettou, NULL);
187
0
  if (input != STDIN_FILENO)
188
0
    (void)close(input);
189
190
  /*
191
   * If we were interrupted by a signal, resend it to ourselves
192
   * now that we have restored the signal handlers.
193
   */
194
0
  for (i = 0; i < _NSIG; i++) {
195
0
    if (signo[i]) {
196
0
      kill(getpid(), i);
197
0
      switch (i) {
198
0
      case SIGTSTP:
199
0
      case SIGTTIN:
200
0
      case SIGTTOU:
201
0
        need_restart = 1;
202
0
      }
203
0
    }
204
0
  }
205
0
  if (need_restart)
206
0
    goto restart;
207
208
0
  if (save_errno)
209
0
    errno = save_errno;
210
0
  return(nr == -1 ? NULL : buf);
211
0
}
212
DEF_WEAK(readpassphrase);
213
214
#if 0
215
char *
216
getpass(const char *prompt)
217
{
218
  static char buf[_PASSWORD_LEN + 1];
219
220
  return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
221
}
222
#endif
223
224
static void handler(int s)
225
0
{
226
227
0
  signo[s] = 1;
228
0
}
229
#endif /* HAVE_READPASSPHRASE */