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