blob: d63cdf2f0e33f00841a9a5a7d2adf450cab861d8 [file] [log] [blame]
Darren Tuckerd59487a2010-01-13 21:32:44 +11001/* $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $ */
Damien Miller8e3bdca2002-02-13 16:00:15 +11002
Damien Millerc8a38682001-06-25 18:09:16 +10003/*
Darren Tucker1035cb42010-01-13 18:32:59 +11004 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
Damien Millerc8a38682001-06-25 18:09:16 +10005 *
Ben Lindstromaf4a6c32003-08-25 01:10:51 +00006 * 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.
Damien Millerc8a38682001-06-25 18:09:16 +10009 *
Ben Lindstromaf4a6c32003-08-25 01:10:51 +000010 * 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.
Damien Millerc8a38682001-06-25 18:09:16 +100021 */
22
Darren Tucker7f24a0e2005-11-10 16:18:56 +110023/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
24
Damien Millerc8a38682001-06-25 18:09:16 +100025#include "includes.h"
26
27#ifndef HAVE_READPASSPHRASE
28
29#include <termios.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110030#include <signal.h>
31#include <ctype.h>
Damien Millera1738e42006-07-10 21:33:04 +100032#include <fcntl.h>
Damien Millerc8a38682001-06-25 18:09:16 +100033#include <readpassphrase.h>
Darren Tucker2c1a02a2006-07-12 22:40:50 +100034#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100035#include <string.h>
36#include <unistd.h>
Damien Millerc8a38682001-06-25 18:09:16 +100037
38#ifdef TCSASOFT
39# define _T_FLUSH (TCSAFLUSH|TCSASOFT)
40#else
41# define _T_FLUSH (TCSAFLUSH)
42#endif
43
Ben Lindstroma0bd44c2001-10-25 15:02:35 +000044/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46# define _POSIX_VDISABLE VDISABLE
47#endif
48
Darren Tucker1734e272015-02-25 13:40:45 +110049#ifndef _NSIG
50# ifdef NSIG
51# define _NSIG NSIG
52# else
53# define _NSIG 128
54# endif
55#endif
56
Darren Tuckerd59487a2010-01-13 21:32:44 +110057static volatile sig_atomic_t signo[_NSIG];
Damien Miller8e3bdca2002-02-13 16:00:15 +110058
59static void handler(int);
60
Damien Millerc8a38682001-06-25 18:09:16 +100061char *
Damien Miller8e3bdca2002-02-13 16:00:15 +110062readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
Damien Millerc8a38682001-06-25 18:09:16 +100063{
Damien Miller8e3bdca2002-02-13 16:00:15 +110064 ssize_t nr;
Darren Tuckerd59487a2010-01-13 21:32:44 +110065 int input, output, save_errno, i, need_restart;
Damien Millerc8a38682001-06-25 18:09:16 +100066 char ch, *p, *end;
Damien Miller8e3bdca2002-02-13 16:00:15 +110067 struct termios term, oterm;
Damien Miller71eb0c12002-09-11 10:29:11 +100068 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
69 struct sigaction savetstp, savettin, savettou, savepipe;
Damien Millerc8a38682001-06-25 18:09:16 +100070
71 /* I suppose we could alloc on demand in this case (XXX). */
72 if (bufsiz == 0) {
73 errno = EINVAL;
74 return(NULL);
75 }
76
Damien Miller8e3bdca2002-02-13 16:00:15 +110077restart:
Darren Tuckerd59487a2010-01-13 21:32:44 +110078 for (i = 0; i < _NSIG; i++)
79 signo[i] = 0;
Darren Tucker1035cb42010-01-13 18:32:59 +110080 nr = -1;
81 save_errno = 0;
Darren Tuckerd59487a2010-01-13 21:32:44 +110082 need_restart = 0;
Damien Millerc8a38682001-06-25 18:09:16 +100083 /*
84 * Read and write to /dev/tty if available. If not, read from
85 * stdin and write to stderr unless a tty is required.
86 */
Damien Miller71eb0c12002-09-11 10:29:11 +100087 if ((flags & RPP_STDIN) ||
88 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
Damien Millerc8a38682001-06-25 18:09:16 +100089 if (flags & RPP_REQUIRE_TTY) {
90 errno = ENOTTY;
91 return(NULL);
92 }
93 input = STDIN_FILENO;
94 output = STDERR_FILENO;
95 }
96
97 /*
Damien Miller8e3bdca2002-02-13 16:00:15 +110098 * Catch signals that would otherwise cause the user to end
99 * up with echo turned off in the shell. Don't worry about
Damien Miller71eb0c12002-09-11 10:29:11 +1000100 * things like SIGXCPU and SIGVTALRM for now.
Damien Millerc8a38682001-06-25 18:09:16 +1000101 */
Damien Miller8e3bdca2002-02-13 16:00:15 +1100102 sigemptyset(&sa.sa_mask);
103 sa.sa_flags = 0; /* don't restart system calls */
104 sa.sa_handler = handler;
Damien Miller71eb0c12002-09-11 10:29:11 +1000105 (void)sigaction(SIGALRM, &sa, &savealrm);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100106 (void)sigaction(SIGHUP, &sa, &savehup);
Damien Miller71eb0c12002-09-11 10:29:11 +1000107 (void)sigaction(SIGINT, &sa, &saveint);
108 (void)sigaction(SIGPIPE, &sa, &savepipe);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100109 (void)sigaction(SIGQUIT, &sa, &savequit);
110 (void)sigaction(SIGTERM, &sa, &saveterm);
111 (void)sigaction(SIGTSTP, &sa, &savetstp);
112 (void)sigaction(SIGTTIN, &sa, &savettin);
113 (void)sigaction(SIGTTOU, &sa, &savettou);
Damien Millerc8a38682001-06-25 18:09:16 +1000114
115 /* Turn off echo if possible. */
Damien Miller71eb0c12002-09-11 10:29:11 +1000116 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
Damien Miller8e3bdca2002-02-13 16:00:15 +1100117 memcpy(&term, &oterm, sizeof(term));
118 if (!(flags & RPP_ECHO_ON))
119 term.c_lflag &= ~(ECHO | ECHONL);
Damien Millerc8a38682001-06-25 18:09:16 +1000120#ifdef VSTATUS
Damien Miller8e3bdca2002-02-13 16:00:15 +1100121 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
Damien Millerc8a38682001-06-25 18:09:16 +1000122 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
Damien Millerc8a38682001-06-25 18:09:16 +1000123#endif
124 (void)tcsetattr(input, _T_FLUSH, &term);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100125 } else {
126 memset(&term, 0, sizeof(term));
Damien Miller71eb0c12002-09-11 10:29:11 +1000127 term.c_lflag |= ECHO;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100128 memset(&oterm, 0, sizeof(oterm));
Damien Miller71eb0c12002-09-11 10:29:11 +1000129 oterm.c_lflag |= ECHO;
Damien Millerc8a38682001-06-25 18:09:16 +1000130 }
131
Darren Tucker1035cb42010-01-13 18:32:59 +1100132 /* No I/O if we are already backgrounded. */
Darren Tuckerd59487a2010-01-13 21:32:44 +1100133 if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) {
Darren Tucker1035cb42010-01-13 18:32:59 +1100134 if (!(flags & RPP_STDIN))
135 (void)write(output, prompt, strlen(prompt));
136 end = buf + bufsiz - 1;
137 p = buf;
138 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
139 if (p < end) {
140 if ((flags & RPP_SEVENBIT))
141 ch &= 0x7f;
142 if (isalpha(ch)) {
143 if ((flags & RPP_FORCELOWER))
144 ch = (char)tolower(ch);
145 if ((flags & RPP_FORCEUPPER))
146 ch = (char)toupper(ch);
147 }
148 *p++ = ch;
Damien Millerc8a38682001-06-25 18:09:16 +1000149 }
Damien Millerc8a38682001-06-25 18:09:16 +1000150 }
Darren Tucker1035cb42010-01-13 18:32:59 +1100151 *p = '\0';
152 save_errno = errno;
153 if (!(term.c_lflag & ECHO))
154 (void)write(output, "\n", 1);
Damien Millerc8a38682001-06-25 18:09:16 +1000155 }
Damien Miller8e3bdca2002-02-13 16:00:15 +1100156
157 /* Restore old terminal settings and signals. */
Damien Miller308c8b12005-05-24 16:39:03 +1000158 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
Damien Miller101afbb2005-05-24 16:42:00 +1000159 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
Damien Miller308c8b12005-05-24 16:39:03 +1000160 errno == EINTR)
161 continue;
162 }
Damien Miller71eb0c12002-09-11 10:29:11 +1000163 (void)sigaction(SIGALRM, &savealrm, NULL);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100164 (void)sigaction(SIGHUP, &savehup, NULL);
Damien Miller71eb0c12002-09-11 10:29:11 +1000165 (void)sigaction(SIGINT, &saveint, NULL);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100166 (void)sigaction(SIGQUIT, &savequit, NULL);
Damien Miller71eb0c12002-09-11 10:29:11 +1000167 (void)sigaction(SIGPIPE, &savepipe, NULL);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100168 (void)sigaction(SIGTERM, &saveterm, NULL);
169 (void)sigaction(SIGTSTP, &savetstp, NULL);
170 (void)sigaction(SIGTTIN, &savettin, NULL);
Darren Tuckerab3c2ca2010-01-13 18:27:32 +1100171 (void)sigaction(SIGTTOU, &savettou, NULL);
Damien Millerc8a38682001-06-25 18:09:16 +1000172 if (input != STDIN_FILENO)
173 (void)close(input);
Damien Miller8e3bdca2002-02-13 16:00:15 +1100174
175 /*
176 * If we were interrupted by a signal, resend it to ourselves
177 * now that we have restored the signal handlers.
178 */
Darren Tuckerd59487a2010-01-13 21:32:44 +1100179 for (i = 0; i < _NSIG; i++) {
180 if (signo[i]) {
181 kill(getpid(), i);
182 switch (i) {
183 case SIGTSTP:
184 case SIGTTIN:
185 case SIGTTOU:
186 need_restart = 1;
187 }
Damien Miller8e3bdca2002-02-13 16:00:15 +1100188 }
189 }
Darren Tuckerd59487a2010-01-13 21:32:44 +1100190 if (need_restart)
191 goto restart;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100192
Darren Tucker1035cb42010-01-13 18:32:59 +1100193 if (save_errno)
194 errno = save_errno;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100195 return(nr == -1 ? NULL : buf);
Damien Millerc8a38682001-06-25 18:09:16 +1000196}
Darren Tuckerab3c2ca2010-01-13 18:27:32 +1100197
Damien Millerc8a38682001-06-25 18:09:16 +1000198#if 0
199char *
Damien Miller8e3bdca2002-02-13 16:00:15 +1100200getpass(const char *prompt)
Damien Millerc8a38682001-06-25 18:09:16 +1000201{
202 static char buf[_PASSWORD_LEN + 1];
203
204 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
205}
206#endif
Damien Miller8e3bdca2002-02-13 16:00:15 +1100207
208static void handler(int s)
209{
Ben Lindstromaf4a6c32003-08-25 01:10:51 +0000210
Darren Tuckerd59487a2010-01-13 21:32:44 +1100211 signo[s] = 1;
Damien Miller8e3bdca2002-02-13 16:00:15 +1100212}
Damien Miller804357a2002-05-01 22:00:22 +1000213#endif /* HAVE_READPASSPHRASE */