OpenCoverage

readpassphrase.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/openbsd-compat/readpassphrase.c
Source codeSwitch to Preprocessed file
LineSourceCount
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# 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-
49static volatile sig_atomic_t signo[_NSIG];-
50-
51static void handler(int);-
52-
53char *-
54readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)-
55{-
56 ssize_t nr;-
57 int input, output, save_errno, i, need_restart;-
58 char ch, *p, *end;-
59 struct termios term, oterm;-
60 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;-
61 struct sigaction savetstp, savettin, savettou, savepipe;-
62-
63 /* I suppose we could alloc on demand in this case (XXX). */-
64 if (bufsiz == 0) {
bufsiz == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
65 errno = EINVAL;-
66 return(NULL);
never executed: return( ((void *)0) );
0
67 }-
68-
69restart:
code before this statement never executed: restart:
0
70 for (i = 0; i < _NSIG; i++)
i < 65Description
TRUEnever evaluated
FALSEnever evaluated
0
71 signo[i] = 0;
never executed: signo[i] = 0;
0
72 nr = -1;-
73 save_errno = 0;-
74 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 if ((flags & RPP_STDIN) ||
(flags & 0x20)Description
TRUEnever evaluated
FALSEnever evaluated
0
80 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
(input = outpu... , 02 )) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
81 if (flags & RPP_REQUIRE_TTY) {
flags & 0x02Description
TRUEnever evaluated
FALSEnever evaluated
0
82 errno = ENOTTY;-
83 return(NULL);
never executed: return( ((void *)0) );
0
84 }-
85 input = STDIN_FILENO;-
86 output = STDERR_FILENO;-
87 }
never executed: end of block
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 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
input != 0Description
TRUEnever evaluated
FALSEnever evaluated
tcgetattr(input, &oterm) == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
95 memcpy(&term, &oterm, sizeof(term));-
96 if (!(flags & RPP_ECHO_ON))
!(flags & 0x01)Description
TRUEnever evaluated
FALSEnever evaluated
0
97 term.c_lflag &= ~(ECHO | ECHONL);
never executed: term.c_lflag &= ~( 0000010 | 0000100 );
0
98#ifdef VSTATUS-
99 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)-
100 term.c_cc[VSTATUS] = _POSIX_VDISABLE;-
101#endif-
102 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);-
103 } else {
never executed: end of block
0
104 memset(&term, 0, sizeof(term));-
105 term.c_lflag |= ECHO;-
106 memset(&oterm, 0, sizeof(oterm));-
107 oterm.c_lflag |= ECHO;-
108 }
never executed: end of block
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 sigemptyset(&sa.sa_mask);-
116 sa.sa_flags = 0; /* don't restart system calls */-
117 sa.sa_handler = handler;-
118 (void)sigaction(SIGALRM, &sa, &savealrm);-
119 (void)sigaction(SIGHUP, &sa, &savehup);-
120 (void)sigaction(SIGINT, &sa, &saveint);-
121 (void)sigaction(SIGPIPE, &sa, &savepipe);-
122 (void)sigaction(SIGQUIT, &sa, &savequit);-
123 (void)sigaction(SIGTERM, &sa, &saveterm);-
124 (void)sigaction(SIGTSTP, &sa, &savetstp);-
125 (void)sigaction(SIGTTIN, &sa, &savettin);-
126 (void)sigaction(SIGTTOU, &sa, &savettou);-
127-
128 if (!(flags & RPP_STDIN))
!(flags & 0x20)Description
TRUEnever evaluated
FALSEnever evaluated
0
129 (void)write(output, prompt, strlen(prompt));
never executed: (void)write(output, prompt, strlen(prompt));
0
130 end = buf + bufsiz - 1;-
131 p = buf;-
132 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
(nr = read(inp... &ch, 1)) == 1Description
TRUEnever evaluated
FALSEnever evaluated
ch != '\n'Description
TRUEnever evaluated
FALSEnever evaluated
ch != '\r'Description
TRUEnever evaluated
FALSEnever evaluated
0
133 if (p < end) {
p < endDescription
TRUEnever evaluated
FALSEnever evaluated
0
134 if ((flags & RPP_SEVENBIT))
(flags & 0x10)Description
TRUEnever evaluated
FALSEnever evaluated
0
135 ch &= 0x7f;
never executed: ch &= 0x7f;
0
136 if (isalpha((unsigned char)ch)) {
((*__ctype_b_l...int) _ISalpha)Description
TRUEnever evaluated
FALSEnever evaluated
0
137 if ((flags & RPP_FORCELOWER))
(flags & 0x04)Description
TRUEnever evaluated
FALSEnever evaluated
0
138 ch = (char)tolower((unsigned char)ch);
never executed: ch = (char) (__extension__ ({ int __res; if (sizeof ( (unsigned char)ch ) > 1) { if (__builtin_constant_p ( (unsigned char)ch )) { int __c = ( (unsigned char)ch ); __res = __c < -128 || __c > 255 ? __c : (*__ctype_tolower_loc ())[__c]; } else __res = tolower ( (unsigned char)ch ); } else __res = (*__ctype_tolower_loc ())[(int) ( (unsigned char)ch )]; __res; })) ;
never executed: end of block
never executed: __res = tolower ( (unsigned char)ch );
never executed: __res = (*__ctype_tolower_loc ())[(int) ( (unsigned char)ch )];
sizeof ( (unsi... char)ch ) > 1Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...gned char)ch )Description
TRUEnever evaluated
FALSEnever evaluated
__c < -128Description
TRUEnever evaluated
FALSEnever evaluated
__c > 255Description
TRUEnever evaluated
FALSEnever evaluated
0
139 if ((flags & RPP_FORCEUPPER))
(flags & 0x08)Description
TRUEnever evaluated
FALSEnever evaluated
0
140 ch = (char)toupper((unsigned char)ch);
never executed: ch = (char) (__extension__ ({ int __res; if (sizeof ( (unsigned char)ch ) > 1) { if (__builtin_constant_p ( (unsigned char)ch )) { int __c = ( (unsigned char)ch ); __res = __c < -128 || __c > 255 ? __c : (*__ctype_toupper_loc ())[__c]; } else __res = toupper ( (unsigned char)ch ); } else __res = (*__ctype_toupper_loc ())[(int) ( (unsigned char)ch )]; __res; })) ;
never executed: end of block
never executed: __res = toupper ( (unsigned char)ch );
never executed: __res = (*__ctype_toupper_loc ())[(int) ( (unsigned char)ch )];
sizeof ( (unsi... char)ch ) > 1Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...gned char)ch )Description
TRUEnever evaluated
FALSEnever evaluated
__c < -128Description
TRUEnever evaluated
FALSEnever evaluated
__c > 255Description
TRUEnever evaluated
FALSEnever evaluated
0
141 }
never executed: end of block
0
142 *p++ = ch;-
143 }
never executed: end of block
0
144 }
never executed: end of block
0
145 *p = '\0';-
146 save_errno = errno;-
147 if (!(term.c_lflag & ECHO))
!(term.c_lflag & 0000010 )Description
TRUEnever evaluated
FALSEnever evaluated
0
148 (void)write(output, "\n", 1);
never executed: (void)write(output, "\n", 1);
0
149-
150 /* Restore old terminal settings and signals. */-
151 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
memcmp(&term, ...of(term)) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
152 const int sigttou = signo[SIGTTOU];-
153-
154 /* Ignore SIGTTOU generated when we are not the fg pgrp. */-
155 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
tcsetattr(inpu... &oterm) == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
156 errno == EINTR && !signo[SIGTTOU])
(*__errno_location ()) == 4Description
TRUEnever evaluated
FALSEnever evaluated
!signo[ 22 ]Description
TRUEnever evaluated
FALSEnever evaluated
0
157 continue;
never executed: continue;
0
158 signo[SIGTTOU] = sigttou;-
159 }
never executed: end of block
0
160 (void)sigaction(SIGALRM, &savealrm, NULL);-
161 (void)sigaction(SIGHUP, &savehup, NULL);-
162 (void)sigaction(SIGINT, &saveint, NULL);-
163 (void)sigaction(SIGQUIT, &savequit, NULL);-
164 (void)sigaction(SIGPIPE, &savepipe, NULL);-
165 (void)sigaction(SIGTERM, &saveterm, NULL);-
166 (void)sigaction(SIGTSTP, &savetstp, NULL);-
167 (void)sigaction(SIGTTIN, &savettin, NULL);-
168 (void)sigaction(SIGTTOU, &savettou, NULL);-
169 if (input != STDIN_FILENO)
input != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
170 (void)close(input);
never executed: (void)close(input);
0
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 for (i = 0; i < _NSIG; i++) {
i < 65Description
TRUEnever evaluated
FALSEnever evaluated
0
177 if (signo[i]) {
signo[i]Description
TRUEnever evaluated
FALSEnever evaluated
0
178 kill(getpid(), i);-
179 switch (i) {-
180 case SIGTSTP:
never executed: case 20 :
0
181 case SIGTTIN:
never executed: case 21 :
0
182 case SIGTTOU:
never executed: case 22 :
0
183 need_restart = 1;-
184 }
never executed: end of block
0
185 }
never executed: end of block
0
186 }
never executed: end of block
0
187 if (need_restart)
need_restartDescription
TRUEnever evaluated
FALSEnever evaluated
0
188 goto restart;
never executed: goto restart;
0
189-
190 if (save_errno)
save_errnoDescription
TRUEnever evaluated
FALSEnever evaluated
0
191 errno = save_errno;
never executed: (*__errno_location ()) = save_errno;
0
192 return(nr == -1 ? NULL : buf);
never executed: return(nr == -1 ? ((void *)0) : buf);
nr == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
193}-
194DEF_WEAK(readpassphrase);-
195-
196#if 0-
197char *-
198getpass(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-
206static void handler(int s)-
207{-
208-
209 signo[s] = 1;-
210}
never executed: end of block
0
211#endif /* HAVE_READPASSPHRASE */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2