readpassphrase.c revision 98937
1141240Snjl/*	$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $	*/
2141240Snjl
3141240Snjl/*
4141240Snjl * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com>
5141240Snjl * All rights reserved.
6141240Snjl *
7141240Snjl * Redistribution and use in source and binary forms, with or without
8141240Snjl * modification, are permitted provided that the following conditions
9141240Snjl * are met:
10141240Snjl * 1. Redistributions of source code must retain the above copyright
11141240Snjl *    notice, this list of conditions and the following disclaimer.
12141240Snjl * 2. Redistributions in binary form must reproduce the above copyright
13141240Snjl *    notice, this list of conditions and the following disclaimer in the
14141240Snjl *    documentation and/or other materials provided with the distribution.
15141240Snjl * 3. The name of the author may not be used to endorse or promote products
16141240Snjl *    derived from this software without specific prior written permission.
17141240Snjl *
18141240Snjl * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19141240Snjl * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20141240Snjl * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21141240Snjl * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22141240Snjl * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23141240Snjl * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24141240Snjl * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25141240Snjl * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26141240Snjl * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27141240Snjl * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28141240Snjl */
29141240Snjl
30141240Snjl#if defined(LIBC_SCCS) && !defined(lint)
31141240Snjlstatic const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $";
32141240Snjl#endif /* LIBC_SCCS and not lint */
33141240Snjl
34141240Snjl#include "includes.h"
35141240Snjl
36141240Snjl#ifndef HAVE_READPASSPHRASE
37141240Snjl
38141240Snjl#include <termios.h>
39141240Snjl#include <readpassphrase.h>
40141240Snjl
41141240Snjl#ifdef TCSASOFT
42141240Snjl# define _T_FLUSH	(TCSAFLUSH|TCSASOFT)
43141814Snjl#else
44141240Snjl# define _T_FLUSH	(TCSAFLUSH)
45141240Snjl#endif
46141240Snjl
47141240Snjl/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
48141240Snjl#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
49141240Snjl#  define _POSIX_VDISABLE       VDISABLE
50141240Snjl#endif
51141240Snjl
52141240Snjlstatic volatile sig_atomic_t signo;
53141240Snjl
54141240Snjlstatic void handler(int);
55141240Snjl
56141240Snjlchar *
57141240Snjlreadpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
58141240Snjl{
59141240Snjl	ssize_t nr;
60141923Snjl	int input, output, save_errno;
61141923Snjl	char ch, *p, *end;
62141923Snjl	struct termios term, oterm;
63141923Snjl	struct sigaction sa, saveint, savehup, savequit, saveterm;
64141413Snjl	struct sigaction savetstp, savettin, savettou;
65141240Snjl
66141240Snjl	/* I suppose we could alloc on demand in this case (XXX). */
67141240Snjl	if (bufsiz == 0) {
68141240Snjl		errno = EINVAL;
69141240Snjl		return(NULL);
70141240Snjl	}
71141240Snjl
72141240Snjlrestart:
73141240Snjl	/*
74141240Snjl	 * Read and write to /dev/tty if available.  If not, read from
75141240Snjl	 * stdin and write to stderr unless a tty is required.
76141240Snjl	 */
77141240Snjl	if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) {
78141240Snjl		if (flags & RPP_REQUIRE_TTY) {
79141240Snjl			errno = ENOTTY;
80141240Snjl			return(NULL);
81141240Snjl		}
82141240Snjl		input = STDIN_FILENO;
83141240Snjl		output = STDERR_FILENO;
84141240Snjl	}
85141413Snjl
86141240Snjl	/*
87141413Snjl	 * Catch signals that would otherwise cause the user to end
88141413Snjl	 * up with echo turned off in the shell.  Don't worry about
89141413Snjl	 * things like SIGALRM and SIGPIPE for now.
90141413Snjl	 */
91141240Snjl	sigemptyset(&sa.sa_mask);
92141240Snjl	sa.sa_flags = 0;		/* don't restart system calls */
93141240Snjl	sa.sa_handler = handler;
94141240Snjl	(void)sigaction(SIGINT, &sa, &saveint);
95141240Snjl	(void)sigaction(SIGHUP, &sa, &savehup);
96141240Snjl	(void)sigaction(SIGQUIT, &sa, &savequit);
97141240Snjl	(void)sigaction(SIGTERM, &sa, &saveterm);
98141240Snjl	(void)sigaction(SIGTSTP, &sa, &savetstp);
99141240Snjl	(void)sigaction(SIGTTIN, &sa, &savettin);
100141240Snjl	(void)sigaction(SIGTTOU, &sa, &savettou);
101141240Snjl
102141240Snjl	/* Turn off echo if possible. */
103141240Snjl	if (tcgetattr(input, &oterm) == 0) {
104141240Snjl		memcpy(&term, &oterm, sizeof(term));
105141240Snjl		if (!(flags & RPP_ECHO_ON))
106141240Snjl			term.c_lflag &= ~(ECHO | ECHONL);
107141240Snjl#ifdef VSTATUS
108141240Snjl		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
109141240Snjl			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
110141240Snjl#endif
111141240Snjl		(void)tcsetattr(input, _T_FLUSH, &term);
112141240Snjl	} else {
113141240Snjl		memset(&term, 0, sizeof(term));
114141240Snjl		memset(&oterm, 0, sizeof(oterm));
115141240Snjl	}
116141240Snjl
117141240Snjl	(void)write(output, prompt, strlen(prompt));
118141240Snjl	end = buf + bufsiz - 1;
119141240Snjl	for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
120141240Snjl		if (p < end) {
121141240Snjl			if ((flags & RPP_SEVENBIT))
122141240Snjl				ch &= 0x7f;
123141240Snjl			if (isalpha(ch)) {
124141240Snjl				if ((flags & RPP_FORCELOWER))
125141923Snjl					ch = tolower(ch);
126141240Snjl				if ((flags & RPP_FORCEUPPER))
127141240Snjl					ch = toupper(ch);
128141240Snjl			}
129141240Snjl			*p++ = ch;
130141240Snjl		}
131141240Snjl	}
132141240Snjl	*p = '\0';
133141240Snjl	save_errno = errno;
134141240Snjl	if (!(term.c_lflag & ECHO))
135141240Snjl		(void)write(output, "\n", 1);
136141240Snjl
137141240Snjl	/* Restore old terminal settings and signals. */
138141240Snjl	if (memcmp(&term, &oterm, sizeof(term)) != 0)
139141240Snjl		(void)tcsetattr(input, _T_FLUSH, &oterm);
140141240Snjl	(void)sigaction(SIGINT, &saveint, NULL);
141141240Snjl	(void)sigaction(SIGHUP, &savehup, NULL);
142141240Snjl	(void)sigaction(SIGQUIT, &savequit, NULL);
143141240Snjl	(void)sigaction(SIGTERM, &saveterm, NULL);
144141240Snjl	(void)sigaction(SIGTSTP, &savetstp, NULL);
145141240Snjl	(void)sigaction(SIGTTIN, &savettin, NULL);
146141240Snjl	(void)sigaction(SIGTTOU, &savettou, NULL);
147141240Snjl	if (input != STDIN_FILENO)
148141240Snjl		(void)close(input);
149141240Snjl
150141240Snjl	/*
151141240Snjl	 * If we were interrupted by a signal, resend it to ourselves
152141240Snjl	 * now that we have restored the signal handlers.
153141240Snjl	 */
154141240Snjl	if (signo) {
155141240Snjl		kill(getpid(), signo);
156141240Snjl		switch (signo) {
157141240Snjl		case SIGTSTP:
158141240Snjl		case SIGTTIN:
159141240Snjl		case SIGTTOU:
160141240Snjl			signo = 0;
161141240Snjl			goto restart;
162141240Snjl		}
163141240Snjl	}
164141240Snjl
165141240Snjl	errno = save_errno;
166141240Snjl	return(nr == -1 ? NULL : buf);
167141240Snjl}
168141240Snjl
169141240Snjl#if 0
170141240Snjlchar *
171141240Snjlgetpass(const char *prompt)
172141240Snjl{
173141240Snjl	static char buf[_PASSWORD_LEN + 1];
174141240Snjl
175141240Snjl	return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
176141240Snjl}
177141240Snjl#endif
178141814Snjl
179141814Snjlstatic void handler(int s)
180141240Snjl{
181141240Snjl	signo = s;
182141240Snjl}
183141814Snjl#endif /* HAVE_READPASSPHRASE */
184141814Snjl