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