Coverage Report

Created: 2025-06-22 06:47

/src/hpn-ssh/openbsd-compat/readpassphrase.c
Line
Count
Source (jump to first uncovered line)
1
/*  $OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 millert Exp $ */
2
3
/*
4
 * Copyright (c) 2000-2002, 2007, 2010
5
 *  Todd C. Miller <Todd.Miller@courtesan.com>
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
char *
54
readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
55
0
{
56
0
  ssize_t nr;
57
0
  int input, output, save_errno, i, need_restart;
58
0
  char ch, *p, *end;
59
0
  struct termios term, oterm;
60
0
  struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
61
0
  struct sigaction savetstp, savettin, savettou, savepipe;
62
63
  /* I suppose we could alloc on demand in this case (XXX). */
64
0
  if (bufsiz == 0) {
65
0
    errno = EINVAL;
66
0
    return(NULL);
67
0
  }
68
69
0
restart:
70
0
  for (i = 0; i < _NSIG; i++)
71
0
    signo[i] = 0;
72
0
  nr = -1;
73
0
  save_errno = 0;
74
0
  need_restart = 0;
75
  /*
76
   * Read and write to /dev/tty if available.  If not, read from
77
   * stdin and write to stderr unless a tty is required.
78
   */
79
0
  if ((flags & RPP_STDIN) ||
80
0
      (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
81
0
    if (flags & RPP_REQUIRE_TTY) {
82
0
      errno = ENOTTY;
83
0
      return(NULL);
84
0
    }
85
0
    input = STDIN_FILENO;
86
0
    output = STDERR_FILENO;
87
0
  }
88
89
  /*
90
   * Turn off echo if possible.
91
   * If we are using a tty but are not the foreground pgrp this will
92
   * generate SIGTTOU, so do it *before* installing the signal handlers.
93
   */
94
0
  if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
95
0
    memcpy(&term, &oterm, sizeof(term));
96
0
    if (!(flags & RPP_ECHO_ON))
97
0
      term.c_lflag &= ~(ECHO | ECHONL);
98
#ifdef VSTATUS
99
    if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
100
      term.c_cc[VSTATUS] = _POSIX_VDISABLE;
101
#endif
102
0
    (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
103
0
  } else {
104
0
    memset(&term, 0, sizeof(term));
105
0
    term.c_lflag |= ECHO;
106
0
    memset(&oterm, 0, sizeof(oterm));
107
0
    oterm.c_lflag |= ECHO;
108
0
  }
109
110
  /*
111
   * Catch signals that would otherwise cause the user to end
112
   * up with echo turned off in the shell.  Don't worry about
113
   * things like SIGXCPU and SIGVTALRM for now.
114
   */
115
0
  sigemptyset(&sa.sa_mask);
116
0
  sa.sa_flags = 0;    /* don't restart system calls */
117
0
  sa.sa_handler = handler;
118
0
  (void)sigaction(SIGALRM, &sa, &savealrm);
119
0
  (void)sigaction(SIGHUP, &sa, &savehup);
120
0
  (void)sigaction(SIGINT, &sa, &saveint);
121
0
  (void)sigaction(SIGPIPE, &sa, &savepipe);
122
0
  (void)sigaction(SIGQUIT, &sa, &savequit);
123
0
  (void)sigaction(SIGTERM, &sa, &saveterm);
124
0
  (void)sigaction(SIGTSTP, &sa, &savetstp);
125
0
  (void)sigaction(SIGTTIN, &sa, &savettin);
126
0
  (void)sigaction(SIGTTOU, &sa, &savettou);
127
128
0
  if (!(flags & RPP_STDIN))
129
0
    (void)write(output, prompt, strlen(prompt));
130
0
  end = buf + bufsiz - 1;
131
0
  p = buf;
132
0
  while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
133
0
    if (p < end) {
134
0
      if ((flags & RPP_SEVENBIT))
135
0
        ch &= 0x7f;
136
0
      if (isalpha((unsigned char)ch)) {
137
0
        if ((flags & RPP_FORCELOWER))
138
0
          ch = (char)tolower((unsigned char)ch);
139
0
        if ((flags & RPP_FORCEUPPER))
140
0
          ch = (char)toupper((unsigned char)ch);
141
0
      }
142
0
      *p++ = ch;
143
0
    }
144
0
  }
145
0
  *p = '\0';
146
0
  save_errno = errno;
147
0
  if (!(term.c_lflag & ECHO))
148
0
    (void)write(output, "\n", 1);
149
150
  /* Restore old terminal settings and signals. */
151
0
  if (memcmp(&term, &oterm, sizeof(term)) != 0) {
152
0
    const int sigttou = signo[SIGTTOU];
153
154
    /* Ignore SIGTTOU generated when we are not the fg pgrp. */
155
0
    while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
156
0
        errno == EINTR && !signo[SIGTTOU])
157
0
      continue;
158
0
    signo[SIGTTOU] = sigttou;
159
0
  }
160
0
  (void)sigaction(SIGALRM, &savealrm, NULL);
161
0
  (void)sigaction(SIGHUP, &savehup, NULL);
162
0
  (void)sigaction(SIGINT, &saveint, NULL);
163
0
  (void)sigaction(SIGQUIT, &savequit, NULL);
164
0
  (void)sigaction(SIGPIPE, &savepipe, NULL);
165
0
  (void)sigaction(SIGTERM, &saveterm, NULL);
166
0
  (void)sigaction(SIGTSTP, &savetstp, NULL);
167
0
  (void)sigaction(SIGTTIN, &savettin, NULL);
168
0
  (void)sigaction(SIGTTOU, &savettou, NULL);
169
0
  if (input != STDIN_FILENO)
170
0
    (void)close(input);
171
172
  /*
173
   * If we were interrupted by a signal, resend it to ourselves
174
   * now that we have restored the signal handlers.
175
   */
176
0
  for (i = 0; i < _NSIG; i++) {
177
0
    if (signo[i]) {
178
0
      kill(getpid(), i);
179
0
      switch (i) {
180
0
      case SIGTSTP:
181
0
      case SIGTTIN:
182
0
      case SIGTTOU:
183
0
        need_restart = 1;
184
0
      }
185
0
    }
186
0
  }
187
0
  if (need_restart)
188
0
    goto restart;
189
190
0
  if (save_errno)
191
0
    errno = save_errno;
192
0
  return(nr == -1 ? NULL : buf);
193
0
}
194
DEF_WEAK(readpassphrase);
195
196
#if 0
197
char *
198
getpass(const char *prompt)
199
{
200
  static char buf[_PASSWORD_LEN + 1];
201
202
  return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
203
}
204
#endif
205
206
static void handler(int s)
207
0
{
208
209
0
  signo[s] = 1;
210
0
}
211
#endif /* HAVE_READPASSPHRASE */