getpass.c revision 1.22 1 1.22 christos /* $NetBSD: getpass.c,v 1.22 2012/04/13 14:16:27 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.22 christos __RCSID("$NetBSD: getpass.c,v 1.22 2012/04/13 14:16:27 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.11 jtc
53 1.11 jtc #ifdef __weak_alias
54 1.19 christos __weak_alias(getpassfd,_getpassfd)
55 1.17 christos __weak_alias(getpass_r,_getpass_r)
56 1.14 mycroft __weak_alias(getpass,_getpass)
57 1.11 jtc #endif
58 1.1 cgd
59 1.17 christos /*
60 1.17 christos * Notes:
61 1.17 christos * - There is no getpass_r in POSIX
62 1.17 christos * - Historically EOF is documented to be treated as EOL, we provide a
63 1.19 christos * tunable for that GETPASS_FAIL_EOF to disable this.
64 1.17 christos * - Historically getpass ate extra characters silently, we provide
65 1.19 christos * a tunable for that GETPASS_BUF_LIMIT to disable this.
66 1.17 christos * - Historically getpass "worked" by echoing characters when turning
67 1.19 christos * off echo failed, we provide a tunable GETPASS_NEED_TTY to
68 1.17 christos * disable this.
69 1.17 christos * - Some implementations say that on interrupt the program shall
70 1.18 christos * receive an interrupt signal before the function returns. We
71 1.18 christos * send all the tty signals before we return, but we don't expect
72 1.18 christos * suspend to do something useful unless the caller calls us again.
73 1.19 christos * We also provide a tunable to disable signal delivery
74 1.19 christos * GETPASS_NO_SIGNAL.
75 1.19 christos * - GETPASS_NO_BEEP disables beeping.
76 1.20 christos * - GETPASS_ECHO_STAR will echo '*' for each character of the password
77 1.19 christos * - GETPASS_ECHO will echo the password (as pam likes it)
78 1.17 christos */
79 1.1 cgd char *
80 1.19 christos /*ARGSUSED*/
81 1.19 christos getpassfd(const char *prompt, char *buf, size_t len, int fd[], int flags)
82 1.1 cgd {
83 1.17 christos struct termios gt;
84 1.17 christos char c;
85 1.19 christos int sig;
86 1.19 christos bool lnext, havetty, allocated;
87 1.1 cgd
88 1.13 lukem _DIAGASSERT(prompt != NULL);
89 1.13 lukem
90 1.18 christos sig = 0;
91 1.17 christos
92 1.19 christos allocated = buf == NULL;
93 1.19 christos if (tcgetattr(fd[0], >) == -1) {
94 1.17 christos havetty = false;
95 1.19 christos if (flags & GETPASS_NEED_TTY)
96 1.19 christos goto out;
97 1.17 christos memset(>, -1, sizeof(gt));
98 1.17 christos } else
99 1.17 christos havetty = true;
100 1.17 christos
101 1.17 christos
102 1.17 christos if (havetty) {
103 1.17 christos struct termios st = gt;
104 1.17 christos
105 1.17 christos st.c_lflag &= ~(ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL|ISIG|ICANON);
106 1.17 christos st.c_cc[VMIN] = 1;
107 1.17 christos st.c_cc[VTIME] = 0;
108 1.19 christos if (tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, &st) == -1)
109 1.17 christos goto out;
110 1.17 christos }
111 1.8 christos
112 1.17 christos if (prompt != NULL) {
113 1.17 christos size_t plen = strlen(prompt);
114 1.19 christos (void)write(fd[1], prompt, plen);
115 1.19 christos }
116 1.19 christos
117 1.19 christos if (allocated) {
118 1.19 christos len = 1024;
119 1.19 christos if ((buf = malloc(len)) == NULL)
120 1.19 christos goto restore;
121 1.16 christos }
122 1.17 christos
123 1.17 christos c = '\1';
124 1.17 christos lnext = false;
125 1.17 christos for (size_t l = 0; c != '\0'; ) {
126 1.19 christos if (read(fd[0], &c, 1) != 1)
127 1.17 christos goto restore;
128 1.17 christos
129 1.19 christos #define beep() do \
130 1.19 christos if (flags & GETPASS_NO_BEEP) \
131 1.19 christos (void)write(fd[2], "\a", 1); \
132 1.19 christos while (/*CONSTCOND*/ 0)
133 1.19 christos #define erase() (void)write(fd[1], "\b \b", 3)
134 1.19 christos
135 1.17 christos #define C(a, b) (gt.c_cc[(a)] == _POSIX_VDISABLE ? (b) : gt.c_cc[(a)])
136 1.17 christos
137 1.17 christos if (lnext) {
138 1.17 christos lnext = false;
139 1.17 christos goto add;
140 1.17 christos }
141 1.17 christos
142 1.17 christos /* Ignored */
143 1.22 christos if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) ||
144 1.17 christos c == C(VSTOP, CTRL('s')) || c == C(VSTATUS, CTRL('t')) ||
145 1.17 christos c == C(VDISCARD, CTRL('o')))
146 1.17 christos continue;
147 1.17 christos
148 1.17 christos /* Literal next */
149 1.17 christos if (c == C(VLNEXT, CTRL('v'))) {
150 1.17 christos lnext = true;
151 1.17 christos continue;
152 1.17 christos }
153 1.17 christos
154 1.17 christos /* Line or word kill, treat as reset */
155 1.17 christos if (c == C(VKILL, CTRL('u')) || c == C(VWERASE, CTRL('w'))) {
156 1.20 christos if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR)) {
157 1.19 christos while (l--)
158 1.19 christos erase();
159 1.19 christos }
160 1.17 christos l = 0;
161 1.17 christos continue;
162 1.17 christos }
163 1.17 christos
164 1.17 christos /* Character erase */
165 1.17 christos if (c == C(VERASE, CTRL('h'))) {
166 1.17 christos if (l == 0)
167 1.17 christos beep();
168 1.19 christos else {
169 1.17 christos l--;
170 1.20 christos if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR))
171 1.19 christos erase();
172 1.19 christos }
173 1.17 christos continue;
174 1.17 christos }
175 1.17 christos
176 1.18 christos /* tty signal characters */
177 1.18 christos if (c == C(VINTR, CTRL('c'))) {
178 1.18 christos sig = SIGINT;
179 1.18 christos goto out;
180 1.18 christos }
181 1.18 christos if (c == C(VQUIT, CTRL('\\'))) {
182 1.18 christos sig = SIGQUIT;
183 1.18 christos goto out;
184 1.18 christos }
185 1.18 christos if (c == C(VSUSP, CTRL('z')) || c == C(VDSUSP, CTRL('y'))) {
186 1.18 christos sig = SIGTSTP;
187 1.18 christos goto out;
188 1.18 christos }
189 1.18 christos
190 1.18 christos /* EOF */
191 1.18 christos if (c == C(VEOF, CTRL('d'))) {
192 1.19 christos if (flags & GETPASS_FAIL_EOF) {
193 1.19 christos errno = ENODATA;
194 1.19 christos goto out;
195 1.19 christos } else {
196 1.19 christos c = '\0';
197 1.19 christos goto add;
198 1.19 christos }
199 1.17 christos }
200 1.17 christos
201 1.17 christos /* End of line */
202 1.18 christos if (c == C(VEOL, CTRL('j')) || c == C(VEOL2, CTRL('l')))
203 1.17 christos c = '\0';
204 1.17 christos add:
205 1.17 christos if (l >= len) {
206 1.19 christos if (allocated) {
207 1.22 christos size_t nlen = len + 1024;
208 1.22 christos char *nbuf = realloc(buf, nlen);
209 1.22 christos if (nbuf == NULL)
210 1.19 christos goto restore;
211 1.22 christos buf = nbuf;
212 1.22 christos len = nlen;
213 1.19 christos } else {
214 1.19 christos if (flags & GETPASS_BUF_LIMIT) {
215 1.19 christos beep();
216 1.19 christos continue;
217 1.19 christos }
218 1.19 christos if (c == '\0' && l > 0)
219 1.19 christos l--;
220 1.19 christos else
221 1.19 christos continue;
222 1.19 christos }
223 1.17 christos }
224 1.19 christos buf[l++] = c;
225 1.20 christos if (c) {
226 1.20 christos if (flags & GETPASS_ECHO_STAR)
227 1.20 christos (void)write(fd[1], "*", 1);
228 1.20 christos else if (flags & GETPASS_ECHO)
229 1.22 christos (void)write(fd[1], isprint((unsigned char)c) ?
230 1.22 christos &c : "?", 1);
231 1.20 christos }
232 1.1 cgd }
233 1.17 christos
234 1.17 christos if (havetty)
235 1.19 christos (void)tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, >);
236 1.19 christos return buf;
237 1.17 christos restore:
238 1.19 christos if (havetty) {
239 1.19 christos c = errno;
240 1.19 christos (void)tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, >);
241 1.19 christos errno = c;
242 1.19 christos }
243 1.17 christos out:
244 1.18 christos if (sig) {
245 1.19 christos if ((flags & GETPASS_NO_SIGNAL) == 0)
246 1.19 christos (void)raise(sig);
247 1.18 christos errno = EINTR;
248 1.18 christos }
249 1.19 christos memset(buf, 0, len);
250 1.19 christos if (allocated)
251 1.19 christos free(buf);
252 1.17 christos return NULL;
253 1.17 christos }
254 1.17 christos
255 1.17 christos char *
256 1.19 christos getpass_r(const char *prompt, char *buf, size_t len)
257 1.19 christos {
258 1.19 christos bool opentty;
259 1.19 christos int fd[3];
260 1.19 christos char *rv;
261 1.19 christos
262 1.19 christos /*
263 1.19 christos * Try to use /dev/tty if possible; otherwise read from stdin and
264 1.19 christos * write to stderr.
265 1.19 christos */
266 1.19 christos if ((fd[0] = fd[1] = fd[2] = open(_PATH_TTY, O_RDWR)) == -1) {
267 1.19 christos opentty = false;
268 1.19 christos fd[0] = STDIN_FILENO;
269 1.19 christos fd[1] = fd[2] = STDERR_FILENO;
270 1.19 christos } else
271 1.19 christos opentty = true;
272 1.19 christos
273 1.19 christos rv = getpassfd(prompt, buf, len, fd, 0);
274 1.19 christos
275 1.19 christos if (opentty) {
276 1.19 christos int serrno = errno;
277 1.19 christos (void)close(fd[0]);
278 1.19 christos errno = serrno;
279 1.19 christos }
280 1.19 christos return rv;
281 1.19 christos }
282 1.19 christos
283 1.19 christos char *
284 1.17 christos getpass(const char *prompt)
285 1.17 christos {
286 1.17 christos static char e[] = "";
287 1.17 christos static char *buf;
288 1.17 christos static long bufsiz;
289 1.17 christos char *rv;
290 1.17 christos
291 1.19 christos /*
292 1.19 christos * Strictly speaking we could double allocate here, if we get
293 1.19 christos * called at the same time, but this function is not re-entrant
294 1.19 christos * anyway and it is not supposed to work if called concurrently.
295 1.19 christos */
296 1.17 christos if (buf == NULL) {
297 1.17 christos if ((bufsiz = sysconf(_SC_PASS_MAX)) == -1)
298 1.17 christos return e;
299 1.17 christos if ((buf = malloc((size_t)bufsiz)) == NULL)
300 1.17 christos return e;
301 1.1 cgd }
302 1.17 christos
303 1.17 christos if ((rv = getpass_r(prompt, buf, (size_t)bufsiz)) == NULL)
304 1.17 christos return e;
305 1.17 christos
306 1.17 christos return rv;
307 1.1 cgd }
308 1.17 christos
309 1.17 christos #ifdef TEST
310 1.17 christos int
311 1.17 christos main(int argc, char *argv[])
312 1.17 christos {
313 1.17 christos char buf[28];
314 1.19 christos int fd[3] = { 0, 1, 2 };
315 1.20 christos printf("[%s]\n", getpassfd("foo>", buf, sizeof(buf), fd,
316 1.20 christos GETPASS_ECHO_STAR));
317 1.17 christos return 0;
318 1.17 christos }
319 1.17 christos #endif
320