1 /* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $"; 32 #endif /* LIBC_SCCS and not lint */ 33 34 #include "includes.h" 35 36 #ifndef HAVE_READPASSPHRASE 37 38 #include <termios.h> 39 #include <readpassphrase.h> 40 41 #ifdef TCSASOFT 42 # define _T_FLUSH (TCSAFLUSH|TCSASOFT) 43 #else 44 # define _T_FLUSH (TCSAFLUSH) 45 #endif 46 47 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */ 48 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE) 49 # define _POSIX_VDISABLE VDISABLE 50 #endif 51 52 static volatile sig_atomic_t signo; 53 54 static void handler(int); 55 56 char * 57 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) 58 { 59 ssize_t nr; 60 int input, output, save_errno; 61 char ch, *p, *end; 62 struct termios term, oterm; 63 struct sigaction sa, saveint, savehup, savequit, saveterm; 64 struct sigaction savetstp, savettin, savettou; 65 66 /* I suppose we could alloc on demand in this case (XXX). */ 67 if (bufsiz == 0) { 68 errno = EINVAL; 69 return(NULL); 70 } 71 72 restart: 73 /* 74 * Read and write to /dev/tty if available. If not, read from 75 * stdin and write to stderr unless a tty is required. 76 */ 77 if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { 78 if (flags & RPP_REQUIRE_TTY) { 79 errno = ENOTTY; 80 return(NULL); 81 } 82 input = STDIN_FILENO; 83 output = STDERR_FILENO; 84 } 85 86 /* 87 * Catch signals that would otherwise cause the user to end 88 * up with echo turned off in the shell. Don't worry about 89 * things like SIGALRM and SIGPIPE for now. 90 */ 91 sigemptyset(&sa.sa_mask); 92 sa.sa_flags = 0; /* don't restart system calls */ 93 sa.sa_handler = handler; 94 (void)sigaction(SIGINT, &sa, &saveint); 95 (void)sigaction(SIGHUP, &sa, &savehup); 96 (void)sigaction(SIGQUIT, &sa, &savequit); 97 (void)sigaction(SIGTERM, &sa, &saveterm); 98 (void)sigaction(SIGTSTP, &sa, &savetstp); 99 (void)sigaction(SIGTTIN, &sa, &savettin); 100 (void)sigaction(SIGTTOU, &sa, &savettou); 101 102 /* Turn off echo if possible. */ 103 if (tcgetattr(input, &oterm) == 0) { 104 memcpy(&term, &oterm, sizeof(term)); 105 if (!(flags & RPP_ECHO_ON)) 106 term.c_lflag &= ~(ECHO | ECHONL); 107 #ifdef VSTATUS 108 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) 109 term.c_cc[VSTATUS] = _POSIX_VDISABLE; 110 #endif 111 (void)tcsetattr(input, _T_FLUSH, &term); 112 } else { 113 memset(&term, 0, sizeof(term)); 114 memset(&oterm, 0, sizeof(oterm)); 115 } 116 117 (void)write(output, prompt, strlen(prompt)); 118 end = buf + bufsiz - 1; 119 for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { 120 if (p < end) { 121 if ((flags & RPP_SEVENBIT)) 122 ch &= 0x7f; 123 if (isalpha(ch)) { 124 if ((flags & RPP_FORCELOWER)) 125 ch = tolower(ch); 126 if ((flags & RPP_FORCEUPPER)) 127 ch = toupper(ch); 128 } 129 *p++ = ch; 130 } 131 } 132 *p = '\0'; 133 save_errno = errno; 134 if (!(term.c_lflag & ECHO)) 135 (void)write(output, "\n", 1); 136 137 /* Restore old terminal settings and signals. */ 138 if (memcmp(&term, &oterm, sizeof(term)) != 0) 139 (void)tcsetattr(input, _T_FLUSH, &oterm); 140 (void)sigaction(SIGINT, &saveint, NULL); 141 (void)sigaction(SIGHUP, &savehup, NULL); 142 (void)sigaction(SIGQUIT, &savequit, NULL); 143 (void)sigaction(SIGTERM, &saveterm, NULL); 144 (void)sigaction(SIGTSTP, &savetstp, NULL); 145 (void)sigaction(SIGTTIN, &savettin, NULL); 146 (void)sigaction(SIGTTOU, &savettou, NULL); 147 if (input != STDIN_FILENO) 148 (void)close(input); 149 150 /* 151 * If we were interrupted by a signal, resend it to ourselves 152 * now that we have restored the signal handlers. 153 */ 154 if (signo) { 155 kill(getpid(), signo); 156 switch (signo) { 157 case SIGTSTP: 158 case SIGTTIN: 159 case SIGTTOU: 160 signo = 0; 161 goto restart; 162 } 163 } 164 165 errno = save_errno; 166 return(nr == -1 ? NULL : buf); 167 } 168 169 #if 0 170 char * 171 getpass(const char *prompt) 172 { 173 static char buf[_PASSWORD_LEN + 1]; 174 175 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); 176 } 177 #endif 178 179 static void handler(int s) 180 { 181 signo = s; 182 } 183 #endif /* HAVE_READPASSPHRASE */ 184