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