1 /* 2 * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 19 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #if defined(LIBC_SCCS) && !defined(lint) 29 static char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.2 2000/11/29 18:41:12 millert Exp $"; 30 #endif /* LIBC_SCCS and not lint */ 31 32 #include <ctype.h> 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <paths.h> 36 #include <pwd.h> 37 #include <signal.h> 38 #include <string.h> 39 #include <termios.h> 40 #include <unistd.h> 41 #include <readpassphrase.h> 42 43 char * 44 readpassphrase(prompt, buf, bufsiz, flags) 45 const char *prompt; 46 char *buf; 47 size_t bufsiz; 48 int flags; 49 { 50 struct termios term; 51 char ch, *p, *end; 52 int echo, input, output; 53 sigset_t oset, nset; 54 55 /* I suppose we could alloc on demand in this case (XXX). */ 56 if (bufsiz == 0) { 57 errno = EINVAL; 58 return(NULL); 59 } 60 61 /* 62 * Read and write to /dev/tty if available. If not, read from 63 * stdin and write to stderr unless a tty is required. 64 */ 65 if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { 66 if (flags & RPP_REQUIRE_TTY) { 67 errno = ENOTTY; 68 return(NULL); 69 } 70 input = STDIN_FILENO; 71 output = STDERR_FILENO; 72 } 73 74 /* 75 * We block SIGINT and SIGTSTP so the terminal is not left 76 * in an inconsistent state (ie: no echo). It would probably 77 * be better to simply catch these though. 78 */ 79 sigemptyset(&nset); 80 sigaddset(&nset, SIGINT); 81 sigaddset(&nset, SIGTSTP); 82 (void)sigprocmask(SIG_BLOCK, &nset, &oset); 83 84 /* Turn off echo if possible. */ 85 echo = 0; 86 if (!(flags & RPP_ECHO_ON)) { 87 if (tcgetattr(input, &term) == 0 && (term.c_lflag & ECHO)) { 88 echo = 1; 89 term.c_lflag &= ~ECHO; 90 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); 91 } 92 } 93 94 (void)write(output, prompt, strlen(prompt)); 95 end = buf + bufsiz - 1; 96 for (p = buf; read(input, &ch, 1) == 1 && ch != '\n' && ch != '\r';) { 97 if (p < end) { 98 if ((flags & RPP_SEVENBIT)) 99 ch = ch &= 0x7f; 100 if (isalpha(ch)) { 101 if ((flags & RPP_FORCELOWER)) 102 ch = tolower(ch); 103 if ((flags & RPP_FORCEUPPER)) 104 ch = toupper(ch); 105 } 106 *p++ = ch; 107 } 108 } 109 *p = '\0'; 110 if (echo) { 111 (void)write(output, "\n", 1); 112 term.c_lflag |= ECHO; 113 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); 114 } 115 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 116 if (input != STDIN_FILENO) 117 (void)close(input); 118 return(buf); 119 } 120 121 char * 122 getpass(prompt) 123 const char *prompt; 124 { 125 static char buf[_PASSWORD_LEN + 1]; 126 127 return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); 128 } 129