summaryrefslogtreecommitdiff
path: root/openbsd-compat/readpassphrase.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/readpassphrase.c')
-rw-r--r--openbsd-compat/readpassphrase.c98
1 files changed, 52 insertions, 46 deletions
diff --git a/openbsd-compat/readpassphrase.c b/openbsd-compat/readpassphrase.c
index d63cdf2f0..24aed6e46 100644
--- a/openbsd-compat/readpassphrase.c
+++ b/openbsd-compat/readpassphrase.c
@@ -1,7 +1,8 @@
1/* $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $ */ 1/* $OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 millert Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 2000-2002, 2007, 2010
5 * Todd C. Miller <Todd.Miller@courtesan.com>
5 * 6 *
6 * Permission to use, copy, modify, and distribute this software for any 7 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 8 * purpose with or without fee is hereby granted, provided that the above
@@ -35,10 +36,9 @@
35#include <string.h> 36#include <string.h>
36#include <unistd.h> 37#include <unistd.h>
37 38
38#ifdef TCSASOFT 39#ifndef TCSASOFT
39# define _T_FLUSH (TCSAFLUSH|TCSASOFT) 40/* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
40#else 41# define TCSASOFT 0
41# define _T_FLUSH (TCSAFLUSH)
42#endif 42#endif
43 43
44/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */ 44/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
@@ -95,6 +95,27 @@ restart:
95 } 95 }
96 96
97 /* 97 /*
98 * Turn off echo if possible.
99 * If we are using a tty but are not the foreground pgrp this will
100 * generate SIGTTOU, so do it *before* installing the signal handlers.
101 */
102 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
103 memcpy(&term, &oterm, sizeof(term));
104 if (!(flags & RPP_ECHO_ON))
105 term.c_lflag &= ~(ECHO | ECHONL);
106#ifdef VSTATUS
107 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
108 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
109#endif
110 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
111 } else {
112 memset(&term, 0, sizeof(term));
113 term.c_lflag |= ECHO;
114 memset(&oterm, 0, sizeof(oterm));
115 oterm.c_lflag |= ECHO;
116 }
117
118 /*
98 * Catch signals that would otherwise cause the user to end 119 * Catch signals that would otherwise cause the user to end
99 * up with echo turned off in the shell. Don't worry about 120 * up with echo turned off in the shell. Don't worry about
100 * things like SIGXCPU and SIGVTALRM for now. 121 * things like SIGXCPU and SIGVTALRM for now.
@@ -112,53 +133,37 @@ restart:
112 (void)sigaction(SIGTTIN, &sa, &savettin); 133 (void)sigaction(SIGTTIN, &sa, &savettin);
113 (void)sigaction(SIGTTOU, &sa, &savettou); 134 (void)sigaction(SIGTTOU, &sa, &savettou);
114 135
115 /* Turn off echo if possible. */ 136 if (!(flags & RPP_STDIN))
116 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) { 137 (void)write(output, prompt, strlen(prompt));
117 memcpy(&term, &oterm, sizeof(term)); 138 end = buf + bufsiz - 1;
118 if (!(flags & RPP_ECHO_ON)) 139 p = buf;
119 term.c_lflag &= ~(ECHO | ECHONL); 140 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
120#ifdef VSTATUS 141 if (p < end) {
121 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) 142 if ((flags & RPP_SEVENBIT))
122 term.c_cc[VSTATUS] = _POSIX_VDISABLE; 143 ch &= 0x7f;
123#endif 144 if (isalpha((unsigned char)ch)) {
124 (void)tcsetattr(input, _T_FLUSH, &term); 145 if ((flags & RPP_FORCELOWER))
125 } else { 146 ch = (char)tolower((unsigned char)ch);
126 memset(&term, 0, sizeof(term)); 147 if ((flags & RPP_FORCEUPPER))
127 term.c_lflag |= ECHO; 148 ch = (char)toupper((unsigned char)ch);
128 memset(&oterm, 0, sizeof(oterm));
129 oterm.c_lflag |= ECHO;
130 }
131
132 /* No I/O if we are already backgrounded. */
133 if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
134 if (!(flags & RPP_STDIN))
135 (void)write(output, prompt, strlen(prompt));
136 end = buf + bufsiz - 1;
137 p = buf;
138 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
139 if (p < end) {
140 if ((flags & RPP_SEVENBIT))
141 ch &= 0x7f;
142 if (isalpha(ch)) {
143 if ((flags & RPP_FORCELOWER))
144 ch = (char)tolower(ch);
145 if ((flags & RPP_FORCEUPPER))
146 ch = (char)toupper(ch);
147 }
148 *p++ = ch;
149 } 149 }
150 *p++ = ch;
150 } 151 }
151 *p = '\0';
152 save_errno = errno;
153 if (!(term.c_lflag & ECHO))
154 (void)write(output, "\n", 1);
155 } 152 }
153 *p = '\0';
154 save_errno = errno;
155 if (!(term.c_lflag & ECHO))
156 (void)write(output, "\n", 1);
156 157
157 /* Restore old terminal settings and signals. */ 158 /* Restore old terminal settings and signals. */
158 if (memcmp(&term, &oterm, sizeof(term)) != 0) { 159 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
159 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 && 160 const int sigttou = signo[SIGTTOU];
160 errno == EINTR) 161
162 /* Ignore SIGTTOU generated when we are not the fg pgrp. */
163 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
164 errno == EINTR && !signo[SIGTTOU])
161 continue; 165 continue;
166 signo[SIGTTOU] = sigttou;
162 } 167 }
163 (void)sigaction(SIGALRM, &savealrm, NULL); 168 (void)sigaction(SIGALRM, &savealrm, NULL);
164 (void)sigaction(SIGHUP, &savehup, NULL); 169 (void)sigaction(SIGHUP, &savehup, NULL);
@@ -194,6 +199,7 @@ restart:
194 errno = save_errno; 199 errno = save_errno;
195 return(nr == -1 ? NULL : buf); 200 return(nr == -1 ? NULL : buf);
196} 201}
202DEF_WEAK(readpassphrase);
197 203
198#if 0 204#if 0
199char * 205char *