[BACK]Return to getpass.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

File: [cvs.NetBSD.org] / src / lib / libc / gen / getpass.c (download)

Revision 1.30, Sun Jan 31 23:41:38 2016 UTC (8 years, 2 months ago) by christos
Branch: MAIN
CVS Tags: prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-10-base, netbsd-10-0-RELEASE, netbsd-10-0-RC6, netbsd-10-0-RC5, netbsd-10-0-RC4, netbsd-10-0-RC3, netbsd-10-0-RC2, netbsd-10-0-RC1, netbsd-10, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, is-mlppp-base, is-mlppp, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, HEAD
Changes since 1.29: +3 -3 lines

PR/50695: coypu: ctrl-c in su password input breaks shell (bash, csh)
Always restore the tty settings.

/*	$NetBSD: getpass.c,v 1.30 2016/01/31 23:41:38 christos Exp $	*/

/*-
 * Copyright (c) 2012 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Christos Zoulas.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: getpass.c,v 1.30 2016/01/31 23:41:38 christos Exp $");
#endif /* LIBC_SCCS and not lint */

#include "namespace.h"

#include <assert.h>
#ifdef TEST
#include <stdio.h>
#endif
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <paths.h>
#include <stdbool.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>

#ifdef __weak_alias
__weak_alias(getpassfd,_getpassfd)
__weak_alias(getpass_r,_getpass_r)
__weak_alias(getpass,_getpass)
#endif

/*
 * Notes:
 *	- There is no getpass_r in POSIX
 *	- Historically EOF is documented to be treated as EOL, we provide a
 *	  tunable for that GETPASS_FAIL_EOF to disable this.
 *	- Historically getpass ate extra characters silently, we provide
 *	  a tunable for that GETPASS_BUF_LIMIT to disable this.
 *	- Historically getpass "worked" by echoing characters when turning
 *	  off echo failed, we provide a tunable GETPASS_NEED_TTY to
 *	  disable this.
 *	- Some implementations say that on interrupt the program shall
 *	  receive an interrupt signal before the function returns. We
 *	  send all the tty signals before we return, but we don't expect
 *	  suspend to do something useful unless the caller calls us again.
 *	  We also provide a tunable to disable signal delivery
 *	  GETPASS_NO_SIGNAL.
 *	- GETPASS_NO_BEEP disables beeping.
 *	- GETPASS_ECHO_STAR will echo '*' for each character of the password
 *	- GETPASS_ECHO will echo the password (as pam likes it)
 *	- GETPASS_7BIT strips the 8th bit
 *	- GETPASS_FORCE_UPPER forces to uppercase
 *	- GETPASS_FORCE_LOWER forces to uppercase
 *	- GETPASS_ECHO_NL echo's a new line on success if echo was off.
 */
char *
/*ARGSUSED*/
getpassfd(const char *prompt, char *buf, size_t len, int *fd, int flags,
    int tout)
{
	struct termios gt;
	char c;
	int sig;
	bool lnext, havetty, allocated, opentty, good;
	int fdc[3];

	_DIAGASSERT(prompt != NULL);

	if (buf != NULL && len == 0) {
		errno = EINVAL;
		return NULL;
	}

	good = false;
	opentty = false;
	if (fd == NULL) {
		/*
		 * Try to use /dev/tty if possible; otherwise read from stdin
		 * and write to stderr.
		 */
		fd = fdc;
		if ((fd[0] = fd[1] = fd[2] = open(_PATH_TTY,
		    O_RDWR | O_CLOEXEC)) == -1) {
			fd[0] = STDIN_FILENO;
			fd[1] = fd[2] = STDERR_FILENO;
		} else
			opentty = true;
	}
		 
	sig = 0;
	allocated = buf == NULL;
	if (tcgetattr(fd[0], &gt) == -1) {
		havetty = false;
		if (flags & GETPASS_NEED_TTY)
			goto out;
		memset(&gt, -1, sizeof(gt));
	} else
		havetty = true;
		
	if (havetty) {
		struct termios st = gt;

		st.c_lflag &= ~(ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL|ISIG|ICANON);
		st.c_cc[VMIN] = 1;
		st.c_cc[VTIME] = 0;
		if (tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, &st) == -1)
			goto out;
	}

	if (prompt != NULL) {
		size_t plen = strlen(prompt);
		(void)write(fd[1], prompt, plen);
	}

	if (allocated) {
		len = 1024;
		if ((buf = malloc(len)) == NULL)
			goto restore;
	}

	c = '\1';
	lnext = false;
	for (size_t l = 0; c != '\0'; ) {
		if (tout) {
			struct pollfd pfd;
			pfd.fd = fd[0];
			pfd.events = POLLIN|POLLRDNORM;
			pfd.revents = 0;
			switch (poll(&pfd, 1, tout * 1000)) {
			case 0:
				errno = ETIMEDOUT;
				/*FALLTHROUGH*/
			case -1:
				goto restore;
			default:
				break;
			}
		}
		if (read(fd[0], &c, 1) != 1)
			goto restore;

#define beep() \
	do \
		if (flags & GETPASS_NO_BEEP) \
			(void)write(fd[2], "\a", 1); \
	while (/*CONSTCOND*/ 0)
#define erase() (void)write(fd[1], "\b \b", 3)
/*
 * We test for both _POSIX_VDISABLE and NUL here because _POSIX_VDISABLE
 * propagation does not seem to be very consistent on multiple daemon hops
 * between different OS's. Perhaps we should not even bother with
 * _POSIX_VDISABLE and use ~0 and 0 directly.
 */
#define C(a, b) ((gt.c_cc[(a)] == _POSIX_VDISABLE || gt.c_cc[(a)] == '\0') ? \
    (b) : gt.c_cc[(a)])
		if (lnext) {
			lnext = false;
			goto add;
		}

		/* Ignored */
		if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) ||
		    c == C(VSTOP, CTRL('s')) || c == C(VSTATUS, CTRL('t')) || 
		    c == C(VDISCARD, CTRL('o')))
			continue;

		/* Literal next */
		if (c == C(VLNEXT, CTRL('v'))) {
			lnext = true;
			continue;
		}

		/* Line or word kill, treat as reset */
		if (c == C(VKILL, CTRL('u')) || c == C(VWERASE, CTRL('w'))) {
			if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR)) {
				while (l--)
					erase();
			}
			l = 0;
			continue;
		}

		/* Character erase */
		if (c == C(VERASE, CTRL('h'))) {
			if (l == 0)
				beep();
			else {
				l--;
				if (flags & (GETPASS_ECHO | GETPASS_ECHO_STAR))
					erase();
			}
			continue;
		}

		/* tty signal characters */
		if (c == C(VINTR, CTRL('c'))) {
			sig = SIGINT;
			goto out;
		}
		if (c == C(VQUIT, CTRL('\\'))) {
			sig = SIGQUIT;
			goto out;
		}
		if (c == C(VSUSP, CTRL('z')) || c == C(VDSUSP, CTRL('y'))) {
			sig = SIGTSTP;
			goto out;
		}

		/* EOF */
		if (c == C(VEOF, CTRL('d')))  {
			if (flags & GETPASS_FAIL_EOF) {
				errno = ENODATA;
				goto out;
			} else {
				c = '\0';
				goto add;
			}
		}

		/* End of line */
		if (c == C(VEOL, CTRL('j')) || c == C(VEOL2, CTRL('m')))
			c = '\0';
add:
		if (l >= len) {
			if (allocated) {
				size_t nlen = len + 1024;
				char *nbuf = realloc(buf, nlen);
				if (nbuf == NULL)
					goto restore;
				buf = nbuf;
				len = nlen;
			} else {
				if (flags & GETPASS_BUF_LIMIT) {
					beep();
					continue;
				}
				if (c == '\0' && l > 0)
					l--;
				else
					continue;
			}
		}

		if (flags & GETPASS_7BIT)
			c &= 0x7f;
		if ((flags & GETPASS_FORCE_LOWER) && isupper((unsigned char)c))
			c = tolower((unsigned char)c);
		if ((flags & GETPASS_FORCE_UPPER) && islower((unsigned char)c))
			c = toupper((unsigned char)c);

		buf[l++] = c;
		if (c) {
			if (flags & GETPASS_ECHO_STAR)
				(void)write(fd[1], "*", 1);
			else if (flags & GETPASS_ECHO)
				(void)write(fd[1], isprint((unsigned char)c) ?
				    &c : "?", 1);
		}
	}
	good = true;

restore:
out:
	if (havetty) {
		c = errno;
		(void)tcsetattr(fd[0], TCSAFLUSH|TCSASOFT, &gt);
		errno = c;
	}
	if (good && (flags & GETPASS_ECHO_NL))
		(void)write(fd[1], "\n", 1);

	if (opentty) {
		c = errno;
		(void)close(fd[0]);
		errno = c;
	}

	if (good)
		return buf;

	if (sig) {
		if ((flags & GETPASS_NO_SIGNAL) == 0)
			(void)raise(sig);
		errno = EINTR;
	}
	memset(buf, 0, len);
	if (allocated)
		free(buf);
	return NULL;
}

char *
getpass_r(const char *prompt, char *buf, size_t len)
{
	return getpassfd(prompt, buf, len, NULL, GETPASS_ECHO_NL, 0);
}

char *
getpass(const char *prompt)
{
	static char e[] = "";
	static char *buf;
	static long bufsiz;
	char *rv;

	/*
	 * Strictly speaking we could double allocate here, if we get
	 * called at the same time, but this function is not re-entrant
	 * anyway and it is not supposed to work if called concurrently.
	 */
	if (buf == NULL) {
		if ((bufsiz = sysconf(_SC_PASS_MAX)) == -1)
			return e;
		if ((buf = malloc((size_t)bufsiz)) == NULL)
			return e;
	}

	if ((rv = getpass_r(prompt, buf, (size_t)bufsiz)) == NULL)
		return e;

	return rv;
}

#ifdef TEST
int
main(int argc, char *argv[])
{
	char buf[28];
	printf("[%s]\n", getpassfd("foo>", buf, sizeof(buf), NULL,
	    GETPASS_ECHO_STAR|GETPASS_ECHO_NL, 2));
	return 0;
}
#endif