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