readpassphrase.c (5501B)
1 /* $OpenBSD: readpassphrase.c,v 1.27 2019/01/25 00:19:25 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2000-2002, 2007, 2010 5 * Todd C. Miller <millert@openbsd.org> 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 <ctype.h> 25 #include <errno.h> 26 #include <fcntl.h> 27 #include <paths.h> 28 #include <pwd.h> 29 #include <signal.h> 30 #include <string.h> 31 #include <termios.h> 32 #include <unistd.h> 33 #include <readpassphrase.h> 34 35 #include "bsd-signal.h" 36 37 static volatile sig_atomic_t signo[_NSIG]; 38 39 static void handler(int); 40 41 char * 42 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) 43 { 44 ssize_t nr; 45 int input, output, save_errno, i, need_restart; 46 char ch, *p, *end; 47 struct termios term, oterm; 48 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; 49 struct sigaction savetstp, savettin, savettou, savepipe; 50 51 /* I suppose we could alloc on demand in this case (XXX). */ 52 if (bufsiz == 0) { 53 errno = EINVAL; 54 return(NULL); 55 } 56 57 restart: 58 for (i = 0; i < _NSIG; i++) 59 signo[i] = 0; 60 nr = -1; 61 save_errno = 0; 62 need_restart = 0; 63 /* 64 * Read and write to /dev/tty if available. If not, read from 65 * stdin and write to stderr unless a tty is required. 66 */ 67 if ((flags & RPP_STDIN) || 68 (input = output = open(_PATH_TTY, O_RDWR)) == -1) { 69 if (flags & RPP_REQUIRE_TTY) { 70 errno = ENOTTY; 71 return(NULL); 72 } 73 input = STDIN_FILENO; 74 output = STDERR_FILENO; 75 } 76 77 /* 78 * Turn off echo if possible. 79 * If we are using a tty but are not the foreground pgrp this will 80 * generate SIGTTOU, so do it *before* installing the signal handlers. 81 */ 82 if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) { 83 memcpy(&term, &oterm, sizeof(term)); 84 if (!(flags & RPP_ECHO_ON)) 85 term.c_lflag &= ~(ECHO | ECHONL); 86 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) 87 term.c_cc[VSTATUS] = _POSIX_VDISABLE; 88 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); 89 } else { 90 memset(&term, 0, sizeof(term)); 91 term.c_lflag |= ECHO; 92 memset(&oterm, 0, sizeof(oterm)); 93 oterm.c_lflag |= ECHO; 94 } 95 96 /* 97 * Catch signals that would otherwise cause the user to end 98 * up with echo turned off in the shell. Don't worry about 99 * things like SIGXCPU and SIGVTALRM for now. 100 */ 101 sigemptyset(&sa.sa_mask); 102 sa.sa_flags = 0; /* don't restart system calls */ 103 sa.sa_handler = handler; 104 (void)sigaction(SIGALRM, &sa, &savealrm); 105 (void)sigaction(SIGHUP, &sa, &savehup); 106 (void)sigaction(SIGINT, &sa, &saveint); 107 (void)sigaction(SIGPIPE, &sa, &savepipe); 108 (void)sigaction(SIGQUIT, &sa, &savequit); 109 (void)sigaction(SIGTERM, &sa, &saveterm); 110 (void)sigaction(SIGTSTP, &sa, &savetstp); 111 (void)sigaction(SIGTTIN, &sa, &savettin); 112 (void)sigaction(SIGTTOU, &sa, &savettou); 113 114 if (!(flags & RPP_STDIN)) 115 (void)write(output, prompt, strlen(prompt)); 116 end = buf + bufsiz - 1; 117 p = buf; 118 while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') { 119 if (p < end) { 120 if ((flags & RPP_SEVENBIT)) 121 ch &= 0x7f; 122 if (isalpha((unsigned char)ch)) { 123 if ((flags & RPP_FORCELOWER)) 124 ch = (char)tolower((unsigned char)ch); 125 if ((flags & RPP_FORCEUPPER)) 126 ch = (char)toupper((unsigned char)ch); 127 } 128 *p++ = ch; 129 } 130 } 131 *p = '\0'; 132 save_errno = errno; 133 if (!(term.c_lflag & ECHO)) 134 (void)write(output, "\n", 1); 135 136 /* Restore old terminal settings and signals. */ 137 if (memcmp(&term, &oterm, sizeof(term)) != 0) { 138 const int sigttou = signo[SIGTTOU]; 139 140 /* Ignore SIGTTOU generated when we are not the fg pgrp. */ 141 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 && 142 errno == EINTR && !signo[SIGTTOU]) 143 continue; 144 signo[SIGTTOU] = sigttou; 145 } 146 (void)sigaction(SIGALRM, &savealrm, NULL); 147 (void)sigaction(SIGHUP, &savehup, NULL); 148 (void)sigaction(SIGINT, &saveint, NULL); 149 (void)sigaction(SIGQUIT, &savequit, NULL); 150 (void)sigaction(SIGPIPE, &savepipe, NULL); 151 (void)sigaction(SIGTERM, &saveterm, NULL); 152 (void)sigaction(SIGTSTP, &savetstp, NULL); 153 (void)sigaction(SIGTTIN, &savettin, NULL); 154 (void)sigaction(SIGTTOU, &savettou, NULL); 155 if (input != STDIN_FILENO) 156 (void)close(input); 157 158 /* 159 * If we were interrupted by a signal, resend it to ourselves 160 * now that we have restored the signal handlers. 161 */ 162 for (i = 0; i < _NSIG; i++) { 163 if (signo[i]) { 164 kill(getpid(), i); 165 switch (i) { 166 case SIGTSTP: 167 case SIGTTIN: 168 case SIGTTOU: 169 need_restart = 1; 170 } 171 } 172 } 173 if (need_restart) 174 goto restart; 175 176 if (save_errno) 177 errno = save_errno; 178 return(nr == -1 ? NULL : buf); 179 } 180 181 char * 182 getpass(const char *prompt) 183 { 184 static char buf[_PASSWORD_LEN + 1]; 185 186 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); 187 } 188 189 static void handler(int s) 190 { 191 192 signo[s] = 1; 193 }