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