getpass.c revision 1.16.28.1 1 1.16.28.1 yamt /* $NetBSD: getpass.c,v 1.16.28.1 2012/04/17 00:05:18 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.1 yamt __RCSID("$NetBSD: getpass.c,v 1.16.28.1 2012/04/17 00:05:18 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.1 yamt #define C(a, b) (gt.c_cc[(a)] == _POSIX_VDISABLE ? (b) : gt.c_cc[(a)])
176 1.16.28.1 yamt
177 1.16.28.1 yamt if (lnext) {
178 1.16.28.1 yamt lnext = false;
179 1.16.28.1 yamt goto add;
180 1.16.28.1 yamt }
181 1.16.28.1 yamt
182 1.16.28.1 yamt /* Ignored */
183 1.16.28.1 yamt if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) ||
184 1.16.28.1 yamt c == C(VSTOP, CTRL('s')) || c == C(VSTATUS, CTRL('t')) ||
185 1.16.28.1 yamt c == C(VDISCARD, CTRL('o')))
186 1.16.28.1 yamt continue;
187 1.16.28.1 yamt
188 1.16.28.1 yamt /* Literal next */
189 1.16.28.1 yamt if (c == C(VLNEXT, CTRL('v'))) {
190 1.16.28.1 yamt lnext = true;
191 1.16.28.1 yamt continue;
192 1.16.28.1 yamt }
193 1.16.28.1 yamt
194 1.16.28.1 yamt /* Line or word kill, treat as reset */
195 1.16.28.1 yamt if (c == C(VKILL, CTRL('u')) || c == C(VWERASE, CTRL('w'))) {
196 1.16.28.1 yamt if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR)) {
197 1.16.28.1 yamt while (l--)
198 1.16.28.1 yamt erase();
199 1.16.28.1 yamt }
200 1.16.28.1 yamt l = 0;
201 1.16.28.1 yamt continue;
202 1.16.28.1 yamt }
203 1.16.28.1 yamt
204 1.16.28.1 yamt /* Character erase */
205 1.16.28.1 yamt if (c == C(VERASE, CTRL('h'))) {
206 1.16.28.1 yamt if (l == 0)
207 1.16.28.1 yamt beep();
208 1.16.28.1 yamt else {
209 1.16.28.1 yamt l--;
210 1.16.28.1 yamt if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR))
211 1.16.28.1 yamt erase();
212 1.16.28.1 yamt }
213 1.16.28.1 yamt continue;
214 1.16.28.1 yamt }
215 1.16.28.1 yamt
216 1.16.28.1 yamt /* tty signal characters */
217 1.16.28.1 yamt if (c == C(VINTR, CTRL('c'))) {
218 1.16.28.1 yamt sig = SIGINT;
219 1.16.28.1 yamt goto out;
220 1.16.28.1 yamt }
221 1.16.28.1 yamt if (c == C(VQUIT, CTRL('\\'))) {
222 1.16.28.1 yamt sig = SIGQUIT;
223 1.16.28.1 yamt goto out;
224 1.16.28.1 yamt }
225 1.16.28.1 yamt if (c == C(VSUSP, CTRL('z')) || c == C(VDSUSP, CTRL('y'))) {
226 1.16.28.1 yamt sig = SIGTSTP;
227 1.16.28.1 yamt goto out;
228 1.16.28.1 yamt }
229 1.16.28.1 yamt
230 1.16.28.1 yamt /* EOF */
231 1.16.28.1 yamt if (c == C(VEOF, CTRL('d'))) {
232 1.16.28.1 yamt if (flags & GETPASS_FAIL_EOF) {
233 1.16.28.1 yamt errno = ENODATA;
234 1.16.28.1 yamt goto out;
235 1.16.28.1 yamt } else {
236 1.16.28.1 yamt c = '\0';
237 1.16.28.1 yamt goto add;
238 1.16.28.1 yamt }
239 1.16.28.1 yamt }
240 1.16.28.1 yamt
241 1.16.28.1 yamt /* End of line */
242 1.16.28.1 yamt if (c == C(VEOL, CTRL('j')) || c == C(VEOL2, CTRL('l')))
243 1.16.28.1 yamt c = '\0';
244 1.16.28.1 yamt add:
245 1.16.28.1 yamt if (l >= len) {
246 1.16.28.1 yamt if (allocated) {
247 1.16.28.1 yamt size_t nlen = len + 1024;
248 1.16.28.1 yamt char *nbuf = realloc(buf, nlen);
249 1.16.28.1 yamt if (nbuf == NULL)
250 1.16.28.1 yamt goto restore;
251 1.16.28.1 yamt buf = nbuf;
252 1.16.28.1 yamt len = nlen;
253 1.16.28.1 yamt } else {
254 1.16.28.1 yamt if (flags & GETPASS_BUF_LIMIT) {
255 1.16.28.1 yamt beep();
256 1.16.28.1 yamt continue;
257 1.16.28.1 yamt }
258 1.16.28.1 yamt if (c == '\0' && l > 0)
259 1.16.28.1 yamt l--;
260 1.16.28.1 yamt else
261 1.16.28.1 yamt continue;
262 1.16.28.1 yamt }
263 1.16.28.1 yamt }
264 1.16.28.1 yamt
265 1.16.28.1 yamt if (flags & GETPASS_7BIT)
266 1.16.28.1 yamt c &= 0x7f;
267 1.16.28.1 yamt if ((flags & GETPASS_FORCE_LOWER) && isupper((unsigned char)c))
268 1.16.28.1 yamt c = tolower((unsigned char)c);
269 1.16.28.1 yamt if ((flags & GETPASS_FORCE_UPPER) && islower((unsigned char)c))
270 1.16.28.1 yamt c = toupper((unsigned char)c);
271 1.16.28.1 yamt
272 1.16.28.1 yamt buf[l++] = c;
273 1.16.28.1 yamt if (c) {
274 1.16.28.1 yamt if (flags & GETPASS_ECHO_STAR)
275 1.16.28.1 yamt (void)write(fd[1], "*", 1);
276 1.16.28.1 yamt else if (flags & GETPASS_ECHO)
277 1.16.28.1 yamt (void)write(fd[1], isprint((unsigned char)c) ?
278 1.16.28.1 yamt &c : "?", 1);
279 1.16.28.1 yamt }
280 1.16.28.1 yamt }
281 1.16.28.1 yamt good = true;
282 1.16.28.1 yamt
283 1.16.28.1 yamt restore:
284 1.16.28.1 yamt if (havetty) {
285 1.16.28.1 yamt c = errno;
286 1.16.28.1 yamt (void)tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, >);
287 1.16.28.1 yamt errno = c;
288 1.16.28.1 yamt }
289 1.16.28.1 yamt out:
290 1.16.28.1 yamt if (good && (flags & GETPASS_ECHO_NL))
291 1.16.28.1 yamt (void)write(fd[1], "\n", 1);
292 1.16.28.1 yamt
293 1.16.28.1 yamt if (opentty) {
294 1.16.28.1 yamt c = errno;
295 1.16.28.1 yamt (void)close(fd[0]);
296 1.16.28.1 yamt errno = c;
297 1.16.28.1 yamt }
298 1.16.28.1 yamt
299 1.16.28.1 yamt if (good)
300 1.16.28.1 yamt return buf;
301 1.16.28.1 yamt
302 1.16.28.1 yamt if (sig) {
303 1.16.28.1 yamt if ((flags & GETPASS_NO_SIGNAL) == 0)
304 1.16.28.1 yamt (void)raise(sig);
305 1.16.28.1 yamt errno = EINTR;
306 1.16.28.1 yamt }
307 1.16.28.1 yamt memset(buf, 0, len);
308 1.16.28.1 yamt if (allocated)
309 1.16.28.1 yamt free(buf);
310 1.16.28.1 yamt return NULL;
311 1.16.28.1 yamt }
312 1.16.28.1 yamt
313 1.16.28.1 yamt char *
314 1.16.28.1 yamt getpass_r(const char *prompt, char *buf, size_t len)
315 1.16.28.1 yamt {
316 1.16.28.1 yamt return getpassfd(prompt, buf, len, NULL, 0, 0);
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(const char *prompt)
321 1.16.28.1 yamt {
322 1.16.28.1 yamt static char e[] = "";
323 1.16.28.1 yamt static char *buf;
324 1.16.28.1 yamt static long bufsiz;
325 1.16.28.1 yamt char *rv;
326 1.8 christos
327 1.16 christos /*
328 1.16.28.1 yamt * Strictly speaking we could double allocate here, if we get
329 1.16.28.1 yamt * called at the same time, but this function is not re-entrant
330 1.16.28.1 yamt * anyway and it is not supposed to work if called concurrently.
331 1.16 christos */
332 1.16.28.1 yamt if (buf == NULL) {
333 1.16.28.1 yamt if ((bufsiz = sysconf(_SC_PASS_MAX)) == -1)
334 1.16.28.1 yamt return e;
335 1.16.28.1 yamt if ((buf = malloc((size_t)bufsiz)) == NULL)
336 1.16.28.1 yamt return e;
337 1.16.28.1 yamt }
338 1.16.28.1 yamt
339 1.16.28.1 yamt if ((rv = getpass_r(prompt, buf, (size_t)bufsiz)) == NULL)
340 1.16.28.1 yamt return e;
341 1.16.28.1 yamt
342 1.16.28.1 yamt return rv;
343 1.1 cgd }
344 1.16.28.1 yamt
345 1.16.28.1 yamt #ifdef TEST
346 1.16.28.1 yamt int
347 1.16.28.1 yamt main(int argc, char *argv[])
348 1.16.28.1 yamt {
349 1.16.28.1 yamt char buf[28];
350 1.16.28.1 yamt printf("[%s]\n", getpassfd("foo>", buf, sizeof(buf), NULL,
351 1.16.28.1 yamt GETPASS_ECHO_STAR|GETPASS_ECHO_NL, 2));
352 1.16.28.1 yamt return 0;
353 1.16.28.1 yamt }
354 1.16.28.1 yamt #endif
355