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