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