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