1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/* $NetBSD: getpass.c,v 1.29 2014/09/18 13:58:20 christos Exp $ */
/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: getpass.c,v 1.29 2014/09/18 13:58:20 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h>
#ifdef TEST
#include <stdio.h>
#endif
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <paths.h>
#include <stdbool.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#ifdef __weak_alias
__weak_alias(getpassfd,_getpassfd)
__weak_alias(getpass_r,_getpass_r)
__weak_alias(getpass,_getpass)
#endif
/*
* Notes:
* - There is no getpass_r in POSIX
* - Historically EOF is documented to be treated as EOL, we provide a
* tunable for that GETPASS_FAIL_EOF to disable this.
* - Historically getpass ate extra characters silently, we provide
* a tunable for that GETPASS_BUF_LIMIT to disable this.
* - Historically getpass "worked" by echoing characters when turning
* off echo failed, we provide a tunable GETPASS_NEED_TTY to
* disable this.
* - Some implementations say that on interrupt the program shall
* receive an interrupt signal before the function returns. We
* send all the tty signals before we return, but we don't expect
* suspend to do something useful unless the caller calls us again.
* We also provide a tunable to disable signal delivery
* GETPASS_NO_SIGNAL.
* - GETPASS_NO_BEEP disables beeping.
* - GETPASS_ECHO_STAR will echo '*' for each character of the password
* - GETPASS_ECHO will echo the password (as pam likes it)
* - GETPASS_7BIT strips the 8th bit
* - GETPASS_FORCE_UPPER forces to uppercase
* - GETPASS_FORCE_LOWER forces to uppercase
* - GETPASS_ECHO_NL echo's a new line on success if echo was off.
*/
char *
/*ARGSUSED*/
getpassfd(const char *prompt, char *buf, size_t len, int *fd, int flags,
int tout)
{
struct termios gt;
char c;
int sig;
bool lnext, havetty, allocated, opentty, good;
int fdc[3];
_DIAGASSERT(prompt != NULL);
if (buf != NULL && len == 0) {
errno = EINVAL;
return NULL;
}
good = false;
opentty = false;
if (fd == NULL) {
/*
* Try to use /dev/tty if possible; otherwise read from stdin
* and write to stderr.
*/
fd = fdc;
if ((fd[0] = fd[1] = fd[2] = open(_PATH_TTY,
O_RDWR | O_CLOEXEC)) == -1) {
fd[0] = STDIN_FILENO;
fd[1] = fd[2] = STDERR_FILENO;
} else
opentty = true;
}
sig = 0;
allocated = buf == NULL;
if (tcgetattr(fd[0], >) == -1) {
havetty = false;
if (flags & GETPASS_NEED_TTY)
goto out;
memset(>, -1, sizeof(gt));
} else
havetty = true;
if (havetty) {
struct termios st = gt;
st.c_lflag &= ~(ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL|ISIG|ICANON);
st.c_cc[VMIN] = 1;
st.c_cc[VTIME] = 0;
if (tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, &st) == -1)
goto out;
}
if (prompt != NULL) {
size_t plen = strlen(prompt);
(void)write(fd[1], prompt, plen);
}
if (allocated) {
len = 1024;
if ((buf = malloc(len)) == NULL)
goto restore;
}
c = '\1';
lnext = false;
for (size_t l = 0; c != '\0'; ) {
if (tout) {
struct pollfd pfd;
pfd.fd = fd[0];
pfd.events = POLLIN|POLLRDNORM;
pfd.revents = 0;
switch (poll(&pfd, 1, tout * 1000)) {
case 0:
errno = ETIMEDOUT;
/*FALLTHROUGH*/
case -1:
goto restore;
default:
break;
}
}
if (read(fd[0], &c, 1) != 1)
goto restore;
#define beep() \
do \
if (flags & GETPASS_NO_BEEP) \
(void)write(fd[2], "\a", 1); \
while (/*CONSTCOND*/ 0)
#define erase() (void)write(fd[1], "\b \b", 3)
/*
* We test for both _POSIX_VDISABLE and NUL here because _POSIX_VDISABLE
* propagation does not seem to be very consistent on multiple daemon hops
* between different OS's. Perhaps we should not even bother with
* _POSIX_VDISABLE and use ~0 and 0 directly.
*/
#define C(a, b) ((gt.c_cc[(a)] == _POSIX_VDISABLE || gt.c_cc[(a)] == '\0') ? \
(b) : gt.c_cc[(a)])
if (lnext) {
lnext = false;
goto add;
}
/* Ignored */
if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) ||
c == C(VSTOP, CTRL('s')) || c == C(VSTATUS, CTRL('t')) ||
c == C(VDISCARD, CTRL('o')))
continue;
/* Literal next */
if (c == C(VLNEXT, CTRL('v'))) {
lnext = true;
continue;
}
/* Line or word kill, treat as reset */
if (c == C(VKILL, CTRL('u')) || c == C(VWERASE, CTRL('w'))) {
if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR)) {
while (l--)
erase();
}
l = 0;
continue;
}
/* Character erase */
if (c == C(VERASE, CTRL('h'))) {
if (l == 0)
beep();
else {
l--;
if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR))
erase();
}
continue;
}
/* tty signal characters */
if (c == C(VINTR, CTRL('c'))) {
sig = SIGINT;
goto out;
}
if (c == C(VQUIT, CTRL('\\'))) {
sig = SIGQUIT;
goto out;
}
if (c == C(VSUSP, CTRL('z')) || c == C(VDSUSP, CTRL('y'))) {
sig = SIGTSTP;
goto out;
}
/* EOF */
if (c == C(VEOF, CTRL('d'))) {
if (flags & GETPASS_FAIL_EOF) {
errno = ENODATA;
goto out;
} else {
c = '\0';
goto add;
}
}
/* End of line */
if (c == C(VEOL, CTRL('j')) || c == C(VEOL2, CTRL('m')))
c = '\0';
add:
if (l >= len) {
if (allocated) {
size_t nlen = len + 1024;
char *nbuf = realloc(buf, nlen);
if (nbuf == NULL)
goto restore;
buf = nbuf;
len = nlen;
} else {
if (flags & GETPASS_BUF_LIMIT) {
beep();
continue;
}
if (c == '\0' && l > 0)
l--;
else
continue;
}
}
if (flags & GETPASS_7BIT)
c &= 0x7f;
if ((flags & GETPASS_FORCE_LOWER) && isupper((unsigned char)c))
c = tolower((unsigned char)c);
if ((flags & GETPASS_FORCE_UPPER) && islower((unsigned char)c))
c = toupper((unsigned char)c);
buf[l++] = c;
if (c) {
if (flags & GETPASS_ECHO_STAR)
(void)write(fd[1], "*", 1);
else if (flags & GETPASS_ECHO)
(void)write(fd[1], isprint((unsigned char)c) ?
&c : "?", 1);
}
}
good = true;
restore:
if (havetty) {
c = errno;
(void)tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, >);
errno = c;
}
out:
if (good && (flags & GETPASS_ECHO_NL))
(void)write(fd[1], "\n", 1);
if (opentty) {
c = errno;
(void)close(fd[0]);
errno = c;
}
if (good)
return buf;
if (sig) {
if ((flags & GETPASS_NO_SIGNAL) == 0)
(void)raise(sig);
errno = EINTR;
}
memset(buf, 0, len);
if (allocated)
free(buf);
return NULL;
}
char *
getpass_r(const char *prompt, char *buf, size_t len)
{
return getpassfd(prompt, buf, len, NULL, GETPASS_ECHO_NL, 0);
}
char *
getpass(const char *prompt)
{
static char e[] = "";
static char *buf;
static long bufsiz;
char *rv;
/*
* Strictly speaking we could double allocate here, if we get
* called at the same time, but this function is not re-entrant
* anyway and it is not supposed to work if called concurrently.
*/
if (buf == NULL) {
if ((bufsiz = sysconf(_SC_PASS_MAX)) == -1)
return e;
if ((buf = malloc((size_t)bufsiz)) == NULL)
return e;
}
if ((rv = getpass_r(prompt, buf, (size_t)bufsiz)) == NULL)
return e;
return rv;
}
#ifdef TEST
int
main(int argc, char *argv[])
{
char buf[28];
printf("[%s]\n", getpassfd("foo>", buf, sizeof(buf), NULL,
GETPASS_ECHO_STAR|GETPASS_ECHO_NL, 2));
return 0;
}
#endif
|