readpassphrase.c revision 91913
191913Sgreen/* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */ 291913Sgreen 386669Sgreen/* 486669Sgreen * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 586669Sgreen * All rights reserved. 686669Sgreen * 786669Sgreen * Redistribution and use in source and binary forms, with or without 886669Sgreen * modification, are permitted provided that the following conditions 986669Sgreen * are met: 1086669Sgreen * 1. Redistributions of source code must retain the above copyright 1186669Sgreen * notice, this list of conditions and the following disclaimer. 1286669Sgreen * 2. Redistributions in binary form must reproduce the above copyright 1386669Sgreen * notice, this list of conditions and the following disclaimer in the 1486669Sgreen * documentation and/or other materials provided with the distribution. 1586669Sgreen * 3. The name of the author may not be used to endorse or promote products 1686669Sgreen * derived from this software without specific prior written permission. 1786669Sgreen * 1886669Sgreen * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 1986669Sgreen * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2086669Sgreen * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2186669Sgreen * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2286669Sgreen * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2386669Sgreen * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2486669Sgreen * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2586669Sgreen * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2686669Sgreen * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 2786669Sgreen * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2886669Sgreen */ 2986669Sgreen 3086669Sgreen#if defined(LIBC_SCCS) && !defined(lint) 3191913Sgreenstatic const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $"; 3286669Sgreen#endif /* LIBC_SCCS and not lint */ 3386750Sfjoe#include <sys/cdefs.h> 3486669Sgreen__FBSDID("$FreeBSD: head/lib/libc/gen/readpassphrase.c 91913 2002-03-08 21:14:00Z green $"); 3586669Sgreen 3686733Sgreen#include "namespace.h" 3786669Sgreen#include <ctype.h> 3886669Sgreen#include <errno.h> 3986669Sgreen#include <fcntl.h> 4086669Sgreen#include <paths.h> 4186669Sgreen#include <pwd.h> 4286669Sgreen#include <signal.h> 4386669Sgreen#include <string.h> 4486669Sgreen#include <termios.h> 4586669Sgreen#include <unistd.h> 4686669Sgreen#include <readpassphrase.h> 4786733Sgreen#include "un-namespace.h" 4886669Sgreen 4991913Sgreenstatic volatile sig_atomic_t signo; 5091913Sgreen 5191913Sgreenstatic void handler(int); 5291913Sgreen 5386669Sgreenchar * 5491913Sgreenreadpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) 5586669Sgreen{ 5691913Sgreen ssize_t nr; 5791913Sgreen int input, output, save_errno; 5891913Sgreen char ch, *p, *end; 5986669Sgreen struct termios term, oterm; 6091913Sgreen struct sigaction sa, saveint, savehup, savequit, saveterm; 6191913Sgreen struct sigaction savetstp, savettin, savettou; 6286669Sgreen 6386669Sgreen /* I suppose we could alloc on demand in this case (XXX). */ 6486669Sgreen if (bufsiz == 0) { 6586669Sgreen errno = EINVAL; 6686669Sgreen return(NULL); 6786669Sgreen } 6886669Sgreen 6991913Sgreenrestart: 7086669Sgreen /* 7186669Sgreen * Read and write to /dev/tty if available. If not, read from 7286669Sgreen * stdin and write to stderr unless a tty is required. 7386669Sgreen */ 7486732Sgreen if ((input = output = _open(_PATH_TTY, O_RDWR)) == -1) { 7586669Sgreen if (flags & RPP_REQUIRE_TTY) { 7686669Sgreen errno = ENOTTY; 7786669Sgreen return(NULL); 7886669Sgreen } 7986669Sgreen input = STDIN_FILENO; 8086669Sgreen output = STDERR_FILENO; 8186669Sgreen } 8286669Sgreen 8386669Sgreen /* 8491913Sgreen * Catch signals that would otherwise cause the user to end 8591913Sgreen * up with echo turned off in the shell. Don't worry about 8691913Sgreen * things like SIGALRM and SIGPIPE for now. 8786669Sgreen */ 8891913Sgreen sigemptyset(&sa.sa_mask); 8991913Sgreen sa.sa_flags = 0; /* don't restart system calls */ 9091913Sgreen sa.sa_handler = handler; 9191913Sgreen (void)_sigaction(SIGINT, &sa, &saveint); 9291913Sgreen (void)_sigaction(SIGHUP, &sa, &savehup); 9391913Sgreen (void)_sigaction(SIGQUIT, &sa, &savequit); 9491913Sgreen (void)_sigaction(SIGTERM, &sa, &saveterm); 9591913Sgreen (void)_sigaction(SIGTSTP, &sa, &savetstp); 9691913Sgreen (void)_sigaction(SIGTTIN, &sa, &savettin); 9791913Sgreen (void)_sigaction(SIGTTOU, &sa, &savettou); 9886669Sgreen 9986669Sgreen /* Turn off echo if possible. */ 10086669Sgreen if (tcgetattr(input, &oterm) == 0) { 10186669Sgreen memcpy(&term, &oterm, sizeof(term)); 10291913Sgreen if (!(flags & RPP_ECHO_ON)) 10391913Sgreen term.c_lflag &= ~(ECHO | ECHONL); 10486669Sgreen if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) 10586669Sgreen term.c_cc[VSTATUS] = _POSIX_VDISABLE; 10686669Sgreen (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); 10786669Sgreen } else { 10886669Sgreen memset(&term, 0, sizeof(term)); 10986669Sgreen memset(&oterm, 0, sizeof(oterm)); 11086669Sgreen } 11186669Sgreen 11286732Sgreen (void)_write(output, prompt, strlen(prompt)); 11386669Sgreen end = buf + bufsiz - 1; 11491913Sgreen for (p = buf; (nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { 11586669Sgreen if (p < end) { 11686669Sgreen if ((flags & RPP_SEVENBIT)) 11786669Sgreen ch &= 0x7f; 11886669Sgreen if (isalpha(ch)) { 11986669Sgreen if ((flags & RPP_FORCELOWER)) 12086669Sgreen ch = tolower(ch); 12186669Sgreen if ((flags & RPP_FORCEUPPER)) 12286669Sgreen ch = toupper(ch); 12386669Sgreen } 12486669Sgreen *p++ = ch; 12586669Sgreen } 12686669Sgreen } 12786669Sgreen *p = '\0'; 12891913Sgreen save_errno = errno; 12986669Sgreen if (!(term.c_lflag & ECHO)) 13086732Sgreen (void)_write(output, "\n", 1); 13186669Sgreen 13291913Sgreen /* Restore old terminal settings and signals. */ 13386669Sgreen if (memcmp(&term, &oterm, sizeof(term)) != 0) 13491913Sgreen (void)tcsetattr(input, TCSANOW|TCSASOFT, &oterm); 13591913Sgreen (void)_sigaction(SIGINT, &saveint, NULL); 13691913Sgreen (void)_sigaction(SIGHUP, &savehup, NULL); 13791913Sgreen (void)_sigaction(SIGQUIT, &savequit, NULL); 13891913Sgreen (void)_sigaction(SIGTERM, &saveterm, NULL); 13991913Sgreen (void)_sigaction(SIGTSTP, &savetstp, NULL); 14091913Sgreen (void)_sigaction(SIGTTIN, &savettin, NULL); 14191913Sgreen (void)_sigaction(SIGTTOU, &savettou, NULL); 14286669Sgreen if (input != STDIN_FILENO) 14386732Sgreen (void)_close(input); 14491913Sgreen 14591913Sgreen /* 14691913Sgreen * If we were interrupted by a signal, resend it to ourselves 14791913Sgreen * now that we have restored the signal handlers. 14891913Sgreen */ 14991913Sgreen if (signo) { 15091913Sgreen kill(getpid(), signo); 15191913Sgreen switch (signo) { 15291913Sgreen case SIGTSTP: 15391913Sgreen case SIGTTIN: 15491913Sgreen case SIGTTOU: 15591913Sgreen signo = 0; 15691913Sgreen goto restart; 15791913Sgreen } 15891913Sgreen } 15991913Sgreen 16091913Sgreen errno = save_errno; 16191913Sgreen return(nr == -1 ? NULL : buf); 16286669Sgreen} 16386669Sgreen 16491913Sgreen#if 0 16586669Sgreenchar * 16691913Sgreengetpass(const char *prompt) 16786669Sgreen{ 16886669Sgreen static char buf[_PASSWORD_LEN + 1]; 16986669Sgreen 17086669Sgreen return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); 17186669Sgreen} 17291913Sgreen#endif 17391913Sgreen 17491913Sgreenstatic void handler(int s) 17591913Sgreen{ 17691913Sgreen 17791913Sgreen signo = s; 17891913Sgreen} 179