doas

https://man.openbsd.org/doas.1
git clone anongit@rnpnr.xyz:doas.git
Log | Files | Refs | Feed

readpassphrase.c (5424B)


      1 /*	$OpenBSD: readpassphrase.c,v 1.27 2019/01/25 00:19:25 millert Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000-2002, 2007, 2010
      5  *	Todd C. Miller <millert@openbsd.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  *
     19  * Sponsored in part by the Defense Advanced Research Projects
     20  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     21  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
     22  */
     23 
     24 #include <ctype.h>
     25 #include <errno.h>
     26 #include <fcntl.h>
     27 #include <paths.h>
     28 #include <pwd.h>
     29 #include <signal.h>
     30 #include <string.h>
     31 #include <termios.h>
     32 #include <unistd.h>
     33 #include <readpassphrase.h>
     34 
     35 #ifndef TCSASOFT
     36 #define TCSASOFT 0
     37 #endif
     38 
     39 static volatile sig_atomic_t signo[_NSIG];
     40 
     41 static void handler(int);
     42 
     43 char *
     44 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
     45 {
     46 	ssize_t nr;
     47 	int input, output, save_errno, i, need_restart;
     48 	char ch, *p, *end;
     49 	struct termios term, oterm;
     50 	struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
     51 	struct sigaction savetstp, savettin, savettou, savepipe;
     52 
     53 	/* I suppose we could alloc on demand in this case (XXX). */
     54 	if (bufsiz == 0) {
     55 		errno = EINVAL;
     56 		return(NULL);
     57 	}
     58 
     59 restart:
     60 	for (i = 0; i < _NSIG; i++)
     61 		signo[i] = 0;
     62 	nr = -1;
     63 	save_errno = 0;
     64 	need_restart = 0;
     65 	/*
     66 	 * Read and write to /dev/tty if available.  If not, read from
     67 	 * stdin and write to stderr unless a tty is required.
     68 	 */
     69 	if ((flags & RPP_STDIN) ||
     70 	    (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
     71 		if (flags & RPP_REQUIRE_TTY) {
     72 			errno = ENOTTY;
     73 			return(NULL);
     74 		}
     75 		input = STDIN_FILENO;
     76 		output = STDERR_FILENO;
     77 	}
     78 
     79 	/*
     80 	 * Turn off echo if possible.
     81 	 * If we are using a tty but are not the foreground pgrp this will
     82 	 * generate SIGTTOU, so do it *before* installing the signal handlers.
     83 	 */
     84 	if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
     85 		memcpy(&term, &oterm, sizeof(term));
     86 		if (!(flags & RPP_ECHO_ON))
     87 			term.c_lflag &= ~(ECHO | ECHONL);
     88 #ifdef VSTATUS
     89 		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
     90 			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
     91 #endif
     92 		(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
     93 	} else {
     94 		memset(&term, 0, sizeof(term));
     95 		term.c_lflag |= ECHO;
     96 		memset(&oterm, 0, sizeof(oterm));
     97 		oterm.c_lflag |= ECHO;
     98 	}
     99 
    100 	/*
    101 	 * Catch signals that would otherwise cause the user to end
    102 	 * up with echo turned off in the shell.  Don't worry about
    103 	 * things like SIGXCPU and SIGVTALRM for now.
    104 	 */
    105 	sigemptyset(&sa.sa_mask);
    106 	sa.sa_flags = 0;		/* don't restart system calls */
    107 	sa.sa_handler = handler;
    108 	(void)sigaction(SIGALRM, &sa, &savealrm);
    109 	(void)sigaction(SIGHUP, &sa, &savehup);
    110 	(void)sigaction(SIGINT, &sa, &saveint);
    111 	(void)sigaction(SIGPIPE, &sa, &savepipe);
    112 	(void)sigaction(SIGQUIT, &sa, &savequit);
    113 	(void)sigaction(SIGTERM, &sa, &saveterm);
    114 	(void)sigaction(SIGTSTP, &sa, &savetstp);
    115 	(void)sigaction(SIGTTIN, &sa, &savettin);
    116 	(void)sigaction(SIGTTOU, &sa, &savettou);
    117 
    118 	if (!(flags & RPP_STDIN))
    119 		(void)write(output, prompt, strlen(prompt));
    120 	end = buf + bufsiz - 1;
    121 	p = buf;
    122 	while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
    123 		if (p < end) {
    124 			if ((flags & RPP_SEVENBIT))
    125 				ch &= 0x7f;
    126 			if (isalpha((unsigned char)ch)) {
    127 				if ((flags & RPP_FORCELOWER))
    128 					ch = (char)tolower((unsigned char)ch);
    129 				if ((flags & RPP_FORCEUPPER))
    130 					ch = (char)toupper((unsigned char)ch);
    131 			}
    132 			*p++ = ch;
    133 		}
    134 	}
    135 	*p = '\0';
    136 	save_errno = errno;
    137 	if (!(term.c_lflag & ECHO))
    138 		(void)write(output, "\n", 1);
    139 
    140 	/* Restore old terminal settings and signals. */
    141 	if (memcmp(&term, &oterm, sizeof(term)) != 0) {
    142 		const int sigttou = signo[SIGTTOU];
    143 
    144 		/* Ignore SIGTTOU generated when we are not the fg pgrp. */
    145 		while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
    146 		    errno == EINTR && !signo[SIGTTOU])
    147 			continue;
    148 		signo[SIGTTOU] = sigttou;
    149 	}
    150 	(void)sigaction(SIGALRM, &savealrm, NULL);
    151 	(void)sigaction(SIGHUP, &savehup, NULL);
    152 	(void)sigaction(SIGINT, &saveint, NULL);
    153 	(void)sigaction(SIGQUIT, &savequit, NULL);
    154 	(void)sigaction(SIGPIPE, &savepipe, NULL);
    155 	(void)sigaction(SIGTERM, &saveterm, NULL);
    156 	(void)sigaction(SIGTSTP, &savetstp, NULL);
    157 	(void)sigaction(SIGTTIN, &savettin, NULL);
    158 	(void)sigaction(SIGTTOU, &savettou, NULL);
    159 	if (input != STDIN_FILENO)
    160 		(void)close(input);
    161 
    162 	/*
    163 	 * If we were interrupted by a signal, resend it to ourselves
    164 	 * now that we have restored the signal handlers.
    165 	 */
    166 	for (i = 0; i < _NSIG; i++) {
    167 		if (signo[i]) {
    168 			kill(getpid(), i);
    169 			switch (i) {
    170 			case SIGTSTP:
    171 			case SIGTTIN:
    172 			case SIGTTOU:
    173 				need_restart = 1;
    174 			}
    175 		}
    176 	}
    177 	if (need_restart)
    178 		goto restart;
    179 
    180 	if (save_errno)
    181 		errno = save_errno;
    182 	return(nr == -1 ? NULL : buf);
    183 }
    184 DEF_WEAK(readpassphrase);
    185 
    186 static void
    187 handler(int s)
    188 {
    189 	signo[s] = 1;
    190 }