1/*	$OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 millert Exp $	*/
2
3/*
4 * Copyright (c) 2000-2002, 2007, 2010
5 *	Todd C. Miller <Todd.Miller@courtesan.com>
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/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
25
26#include "openbsd-compat.h"
27
28#ifndef HAVE_READPASSPHRASE
29
30#include <termios.h>
31#include <signal.h>
32#include <ctype.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <string.h>
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
39#include <paths.h>
40
41#ifndef _PATH_TTY
42# define _PATH_TTY "/dev/tty"
43#endif
44
45#ifndef TCSASOFT
46/* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
47# define TCSASOFT 0
48#endif
49
50/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
51#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
52#  define _POSIX_VDISABLE       VDISABLE
53#endif
54
55static volatile sig_atomic_t signo[NSIG];
56
57static void handler(int);
58
59char *
60readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
61{
62	ssize_t nr;
63	int input, output, save_errno, i, need_restart;
64	char ch, *p, *end;
65	struct termios term, oterm;
66	struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
67	struct sigaction savetstp, savettin, savettou, savepipe;
68
69	/* I suppose we could alloc on demand in this case (XXX). */
70	if (bufsiz == 0) {
71		errno = EINVAL;
72		return(NULL);
73	}
74
75restart:
76	for (i = 0; i < NSIG; i++)
77		signo[i] = 0;
78	need_restart = 0;
79	/*
80	 * Read and write to /dev/tty if available.  If not, read from
81	 * stdin and write to stderr unless a tty is required.
82	 */
83	if ((flags & RPP_STDIN) ||
84	    (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
85		if (flags & RPP_REQUIRE_TTY) {
86			errno = ENOTTY;
87			return(NULL);
88		}
89		input = STDIN_FILENO;
90		output = STDERR_FILENO;
91	}
92
93	/*
94	 * Turn off echo if possible.
95	 * If we are using a tty but are not the foreground pgrp this will
96	 * generate SIGTTOU, so do it *before* installing the signal handlers.
97	 */
98	if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
99		memcpy(&term, &oterm, sizeof(term));
100		if (!(flags & RPP_ECHO_ON))
101			term.c_lflag &= ~(ECHO | ECHONL);
102#ifdef VSTATUS
103		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
104			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
105#endif
106		(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
107	} else {
108		memset(&term, 0, sizeof(term));
109		term.c_lflag |= ECHO;
110		memset(&oterm, 0, sizeof(oterm));
111		oterm.c_lflag |= ECHO;
112	}
113
114	/*
115	 * Catch signals that would otherwise cause the user to end
116	 * up with echo turned off in the shell.  Don't worry about
117	 * things like SIGXCPU and SIGVTALRM for now.
118	 */
119	sigemptyset(&sa.sa_mask);
120	sa.sa_flags = 0;		/* don't restart system calls */
121	sa.sa_handler = handler;
122	(void)sigaction(SIGALRM, &sa, &savealrm);
123	(void)sigaction(SIGHUP, &sa, &savehup);
124	(void)sigaction(SIGINT, &sa, &saveint);
125	(void)sigaction(SIGPIPE, &sa, &savepipe);
126	(void)sigaction(SIGQUIT, &sa, &savequit);
127	(void)sigaction(SIGTERM, &sa, &saveterm);
128	(void)sigaction(SIGTSTP, &sa, &savetstp);
129	(void)sigaction(SIGTTIN, &sa, &savettin);
130	(void)sigaction(SIGTTOU, &sa, &savettou);
131
132	if (!(flags & RPP_STDIN))
133		(void)write(output, prompt, strlen(prompt));
134	end = buf + bufsiz - 1;
135	p = buf;
136	while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
137		if (p < end) {
138			if ((flags & RPP_SEVENBIT))
139				ch &= 0x7f;
140			if (isalpha((unsigned char)ch)) {
141				if ((flags & RPP_FORCELOWER))
142					ch = (char)tolower((unsigned char)ch);
143				if ((flags & RPP_FORCEUPPER))
144					ch = (char)toupper((unsigned char)ch);
145			}
146			*p++ = ch;
147		}
148	}
149	*p = '\0';
150	save_errno = errno;
151	if (!(term.c_lflag & ECHO))
152		(void)write(output, "\n", 1);
153
154	/* Restore old terminal settings and signals. */
155	if (memcmp(&term, &oterm, sizeof(term)) != 0) {
156		const int sigttou = signo[SIGTTOU];
157
158		/* Ignore SIGTTOU generated when we are not the fg pgrp. */
159		while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
160		    errno == EINTR && !signo[SIGTTOU])
161			continue;
162		signo[SIGTTOU] = sigttou;
163	}
164	(void)sigaction(SIGALRM, &savealrm, NULL);
165	(void)sigaction(SIGHUP, &savehup, NULL);
166	(void)sigaction(SIGINT, &saveint, NULL);
167	(void)sigaction(SIGQUIT, &savequit, NULL);
168	(void)sigaction(SIGPIPE, &savepipe, NULL);
169	(void)sigaction(SIGTERM, &saveterm, NULL);
170	(void)sigaction(SIGTSTP, &savetstp, NULL);
171	(void)sigaction(SIGTTIN, &savettin, NULL);
172	(void)sigaction(SIGTTOU, &savettou, NULL);
173	if (input != STDIN_FILENO)
174		(void)close(input);
175
176	/*
177	 * If we were interrupted by a signal, resend it to ourselves
178	 * now that we have restored the signal handlers.
179	 */
180	for (i = 0; i < NSIG; i++) {
181		if (signo[i]) {
182			kill(getpid(), i);
183			switch (i) {
184			case SIGTSTP:
185			case SIGTTIN:
186			case SIGTTOU:
187				need_restart = 1;
188			}
189		}
190	}
191	if (need_restart)
192		goto restart;
193
194	if (save_errno)
195		errno = save_errno;
196	return(nr == -1 ? NULL : buf);
197}
198
199#if 0
200char *
201getpass(const char *prompt)
202{
203	static char buf[_PASSWORD_LEN + 1];
204
205	return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
206}
207#endif
208
209static void handler(int s)
210{
211
212	signo[s] = 1;
213}
214#endif /* HAVE_READPASSPHRASE */
215