1a7a8a766SBrian Feldman /* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */ 2a7a8a766SBrian Feldman 313d98e8cSBrian Feldman /* 413d98e8cSBrian Feldman * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 513d98e8cSBrian Feldman * All rights reserved. 613d98e8cSBrian Feldman * 713d98e8cSBrian Feldman * Redistribution and use in source and binary forms, with or without 813d98e8cSBrian Feldman * modification, are permitted provided that the following conditions 913d98e8cSBrian Feldman * are met: 1013d98e8cSBrian Feldman * 1. Redistributions of source code must retain the above copyright 1113d98e8cSBrian Feldman * notice, this list of conditions and the following disclaimer. 1213d98e8cSBrian Feldman * 2. Redistributions in binary form must reproduce the above copyright 1313d98e8cSBrian Feldman * notice, this list of conditions and the following disclaimer in the 1413d98e8cSBrian Feldman * documentation and/or other materials provided with the distribution. 1513d98e8cSBrian Feldman * 3. The name of the author may not be used to endorse or promote products 1613d98e8cSBrian Feldman * derived from this software without specific prior written permission. 1713d98e8cSBrian Feldman * 1813d98e8cSBrian Feldman * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1913d98e8cSBrian Feldman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2013d98e8cSBrian Feldman * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2113d98e8cSBrian Feldman * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2213d98e8cSBrian Feldman * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2313d98e8cSBrian Feldman * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2413d98e8cSBrian Feldman * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2513d98e8cSBrian Feldman * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2613d98e8cSBrian Feldman * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2713d98e8cSBrian Feldman * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2813d98e8cSBrian Feldman */ 2913d98e8cSBrian Feldman 3013d98e8cSBrian Feldman #if defined(LIBC_SCCS) && !defined(lint) 31a7a8a766SBrian Feldman static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $"; 3213d98e8cSBrian Feldman #endif /* LIBC_SCCS and not lint */ 331e450813SMax Khon #include <sys/cdefs.h> 3413d98e8cSBrian Feldman __FBSDID("$FreeBSD$"); 3513d98e8cSBrian Feldman 3670d2a9e1SBrian Feldman #include "namespace.h" 3713d98e8cSBrian Feldman #include <ctype.h> 3813d98e8cSBrian Feldman #include <errno.h> 3913d98e8cSBrian Feldman #include <fcntl.h> 4013d98e8cSBrian Feldman #include <paths.h> 4113d98e8cSBrian Feldman #include <pwd.h> 4213d98e8cSBrian Feldman #include <signal.h> 4313d98e8cSBrian Feldman #include <string.h> 4413d98e8cSBrian Feldman #include <termios.h> 4513d98e8cSBrian Feldman #include <unistd.h> 4613d98e8cSBrian Feldman #include <readpassphrase.h> 4770d2a9e1SBrian Feldman #include "un-namespace.h" 4813d98e8cSBrian Feldman 49a7a8a766SBrian Feldman static volatile sig_atomic_t signo; 50a7a8a766SBrian Feldman 51a7a8a766SBrian Feldman static void handler(int); 52a7a8a766SBrian Feldman 5313d98e8cSBrian Feldman char * 54a7a8a766SBrian Feldman readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) 5513d98e8cSBrian Feldman { 56a7a8a766SBrian Feldman ssize_t nr; 57a7a8a766SBrian Feldman int input, output, save_errno; 5813d98e8cSBrian Feldman char ch, *p, *end; 59a7a8a766SBrian Feldman struct termios term, oterm; 60a7a8a766SBrian Feldman struct sigaction sa, saveint, savehup, savequit, saveterm; 61a7a8a766SBrian Feldman struct sigaction savetstp, savettin, savettou; 6213d98e8cSBrian Feldman 6313d98e8cSBrian Feldman /* I suppose we could alloc on demand in this case (XXX). */ 6413d98e8cSBrian Feldman if (bufsiz == 0) { 6513d98e8cSBrian Feldman errno = EINVAL; 6613d98e8cSBrian Feldman return(NULL); 6713d98e8cSBrian Feldman } 6813d98e8cSBrian Feldman 69a7a8a766SBrian Feldman restart: 7013d98e8cSBrian Feldman /* 7113d98e8cSBrian Feldman * Read and write to /dev/tty if available. If not, read from 7213d98e8cSBrian Feldman * stdin and write to stderr unless a tty is required. 7313d98e8cSBrian Feldman */ 74a939ced8SBrian Feldman if ((input = output = _open(_PATH_TTY, O_RDWR)) == -1) { 7513d98e8cSBrian Feldman if (flags & RPP_REQUIRE_TTY) { 7613d98e8cSBrian Feldman errno = ENOTTY; 7713d98e8cSBrian Feldman return(NULL); 7813d98e8cSBrian Feldman } 7913d98e8cSBrian Feldman input = STDIN_FILENO; 8013d98e8cSBrian Feldman output = STDERR_FILENO; 8113d98e8cSBrian Feldman } 8213d98e8cSBrian Feldman 8313d98e8cSBrian Feldman /* 84a7a8a766SBrian Feldman * Catch signals that would otherwise cause the user to end 85a7a8a766SBrian Feldman * up with echo turned off in the shell. Don't worry about 86a7a8a766SBrian Feldman * things like SIGALRM and SIGPIPE for now. 8713d98e8cSBrian Feldman */ 88a7a8a766SBrian Feldman sigemptyset(&sa.sa_mask); 89a7a8a766SBrian Feldman sa.sa_flags = 0; /* don't restart system calls */ 90a7a8a766SBrian Feldman sa.sa_handler = handler; 91a7a8a766SBrian Feldman (void)_sigaction(SIGINT, &sa, &saveint); 92a7a8a766SBrian Feldman (void)_sigaction(SIGHUP, &sa, &savehup); 93a7a8a766SBrian Feldman (void)_sigaction(SIGQUIT, &sa, &savequit); 94a7a8a766SBrian Feldman (void)_sigaction(SIGTERM, &sa, &saveterm); 95a7a8a766SBrian Feldman (void)_sigaction(SIGTSTP, &sa, &savetstp); 96a7a8a766SBrian Feldman (void)_sigaction(SIGTTIN, &sa, &savettin); 97a7a8a766SBrian Feldman (void)_sigaction(SIGTTOU, &sa, &savettou); 9813d98e8cSBrian Feldman 9913d98e8cSBrian Feldman /* Turn off echo if possible. */ 10013d98e8cSBrian Feldman if (tcgetattr(input, &oterm) == 0) { 10113d98e8cSBrian Feldman memcpy(&term, &oterm, sizeof(term)); 102a7a8a766SBrian Feldman if (!(flags & RPP_ECHO_ON)) 103a7a8a766SBrian Feldman term.c_lflag &= ~(ECHO | ECHONL); 10413d98e8cSBrian Feldman if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) 10513d98e8cSBrian Feldman term.c_cc[VSTATUS] = _POSIX_VDISABLE; 10613d98e8cSBrian Feldman (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); 10713d98e8cSBrian Feldman } else { 10813d98e8cSBrian Feldman memset(&term, 0, sizeof(term)); 10913d98e8cSBrian Feldman memset(&oterm, 0, sizeof(oterm)); 11013d98e8cSBrian Feldman } 11113d98e8cSBrian Feldman 112a939ced8SBrian Feldman (void)_write(output, prompt, strlen(prompt)); 11313d98e8cSBrian Feldman end = buf + bufsiz - 1; 114a7a8a766SBrian Feldman for (p = buf; (nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { 11513d98e8cSBrian Feldman if (p < end) { 11613d98e8cSBrian Feldman if ((flags & RPP_SEVENBIT)) 11713d98e8cSBrian Feldman ch &= 0x7f; 11813d98e8cSBrian Feldman if (isalpha(ch)) { 11913d98e8cSBrian Feldman if ((flags & RPP_FORCELOWER)) 12013d98e8cSBrian Feldman ch = tolower(ch); 12113d98e8cSBrian Feldman if ((flags & RPP_FORCEUPPER)) 12213d98e8cSBrian Feldman ch = toupper(ch); 12313d98e8cSBrian Feldman } 12413d98e8cSBrian Feldman *p++ = ch; 12513d98e8cSBrian Feldman } 12613d98e8cSBrian Feldman } 12713d98e8cSBrian Feldman *p = '\0'; 128a7a8a766SBrian Feldman save_errno = errno; 12913d98e8cSBrian Feldman if (!(term.c_lflag & ECHO)) 130a939ced8SBrian Feldman (void)_write(output, "\n", 1); 13113d98e8cSBrian Feldman 132a7a8a766SBrian Feldman /* Restore old terminal settings and signals. */ 13313d98e8cSBrian Feldman if (memcmp(&term, &oterm, sizeof(term)) != 0) 134a7a8a766SBrian Feldman (void)tcsetattr(input, TCSANOW|TCSASOFT, &oterm); 135a7a8a766SBrian Feldman (void)_sigaction(SIGINT, &saveint, NULL); 136a7a8a766SBrian Feldman (void)_sigaction(SIGHUP, &savehup, NULL); 137a7a8a766SBrian Feldman (void)_sigaction(SIGQUIT, &savequit, NULL); 138a7a8a766SBrian Feldman (void)_sigaction(SIGTERM, &saveterm, NULL); 139a7a8a766SBrian Feldman (void)_sigaction(SIGTSTP, &savetstp, NULL); 140a7a8a766SBrian Feldman (void)_sigaction(SIGTTIN, &savettin, NULL); 141a7a8a766SBrian Feldman (void)_sigaction(SIGTTOU, &savettou, NULL); 14213d98e8cSBrian Feldman if (input != STDIN_FILENO) 143a939ced8SBrian Feldman (void)_close(input); 144a7a8a766SBrian Feldman 145a7a8a766SBrian Feldman /* 146a7a8a766SBrian Feldman * If we were interrupted by a signal, resend it to ourselves 147a7a8a766SBrian Feldman * now that we have restored the signal handlers. 148a7a8a766SBrian Feldman */ 149a7a8a766SBrian Feldman if (signo) { 150a7a8a766SBrian Feldman kill(getpid(), signo); 151a7a8a766SBrian Feldman switch (signo) { 152a7a8a766SBrian Feldman case SIGTSTP: 153a7a8a766SBrian Feldman case SIGTTIN: 154a7a8a766SBrian Feldman case SIGTTOU: 155a7a8a766SBrian Feldman signo = 0; 156a7a8a766SBrian Feldman goto restart; 157a7a8a766SBrian Feldman } 15813d98e8cSBrian Feldman } 15913d98e8cSBrian Feldman 160a7a8a766SBrian Feldman errno = save_errno; 161a7a8a766SBrian Feldman return(nr == -1 ? NULL : buf); 162a7a8a766SBrian Feldman } 163a7a8a766SBrian Feldman 164a7a8a766SBrian Feldman #if 0 16513d98e8cSBrian Feldman char * 166a7a8a766SBrian Feldman getpass(const char *prompt) 16713d98e8cSBrian Feldman { 16813d98e8cSBrian Feldman static char buf[_PASSWORD_LEN + 1]; 16913d98e8cSBrian Feldman 17013d98e8cSBrian Feldman return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); 17113d98e8cSBrian Feldman } 172a7a8a766SBrian Feldman #endif 173a7a8a766SBrian Feldman 174a7a8a766SBrian Feldman static void handler(int s) 175a7a8a766SBrian Feldman { 176a7a8a766SBrian Feldman 177a7a8a766SBrian Feldman signo = s; 178a7a8a766SBrian Feldman } 179