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