| 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 | * $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ |
| 30 | * $FreeBSD: src/lib/libc/gen/readpassphrase.c,v 1.6.2.2 2002/06/30 13:54:26 des Exp $ |
| 31 | * $DragonFly: src/lib/libc/gen/readpassphrase.c,v 1.4 2005/11/13 00:07:42 swildner Exp $ |
| 32 | */ |
| 33 | |
| 34 | #include "namespace.h" |
| 35 | #include <ctype.h> |
| 36 | #include <errno.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <paths.h> |
| 39 | #include <pwd.h> |
| 40 | #include <signal.h> |
| 41 | #include <string.h> |
| 42 | #include <termios.h> |
| 43 | #include <unistd.h> |
| 44 | #include <readpassphrase.h> |
| 45 | #include "un-namespace.h" |
| 46 | |
| 47 | static volatile sig_atomic_t signo; |
| 48 | |
| 49 | static void handler(int); |
| 50 | |
| 51 | char * |
| 52 | readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) |
| 53 | { |
| 54 | ssize_t nr; |
| 55 | int input, output, save_errno; |
| 56 | char ch, *p, *end; |
| 57 | struct termios term, oterm; |
| 58 | struct sigaction sa, saveint, savehup, savequit, saveterm; |
| 59 | struct sigaction savetstp, savettin, savettou; |
| 60 | |
| 61 | /* I suppose we could alloc on demand in this case (XXX). */ |
| 62 | if (bufsiz == 0) { |
| 63 | errno = EINVAL; |
| 64 | return(NULL); |
| 65 | } |
| 66 | |
| 67 | restart: |
| 68 | /* |
| 69 | * Read and write to /dev/tty if available. If not, read from |
| 70 | * stdin and write to stderr unless a tty is required. |
| 71 | */ |
| 72 | if ((input = output = _open(_PATH_TTY, O_RDWR)) == -1) { |
| 73 | if (flags & RPP_REQUIRE_TTY) { |
| 74 | errno = ENOTTY; |
| 75 | return(NULL); |
| 76 | } |
| 77 | input = STDIN_FILENO; |
| 78 | output = STDERR_FILENO; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Catch signals that would otherwise cause the user to end |
| 83 | * up with echo turned off in the shell. Don't worry about |
| 84 | * things like SIGALRM and SIGPIPE for now. |
| 85 | */ |
| 86 | sigemptyset(&sa.sa_mask); |
| 87 | sa.sa_flags = 0; /* don't restart system calls */ |
| 88 | sa.sa_handler = handler; |
| 89 | _sigaction(SIGINT, &sa, &saveint); |
| 90 | _sigaction(SIGHUP, &sa, &savehup); |
| 91 | _sigaction(SIGQUIT, &sa, &savequit); |
| 92 | _sigaction(SIGTERM, &sa, &saveterm); |
| 93 | _sigaction(SIGTSTP, &sa, &savetstp); |
| 94 | _sigaction(SIGTTIN, &sa, &savettin); |
| 95 | _sigaction(SIGTTOU, &sa, &savettou); |
| 96 | |
| 97 | /* Turn off echo if possible. */ |
| 98 | if (tcgetattr(input, &oterm) == 0) { |
| 99 | memcpy(&term, &oterm, sizeof(term)); |
| 100 | if (!(flags & RPP_ECHO_ON)) |
| 101 | term.c_lflag &= ~(ECHO | ECHONL); |
| 102 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) |
| 103 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; |
| 104 | tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); |
| 105 | } else { |
| 106 | memset(&term, 0, sizeof(term)); |
| 107 | memset(&oterm, 0, sizeof(oterm)); |
| 108 | } |
| 109 | |
| 110 | _write(output, prompt, strlen(prompt)); |
| 111 | end = buf + bufsiz - 1; |
| 112 | for (p = buf; (nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { |
| 113 | if (p < end) { |
| 114 | if ((flags & RPP_SEVENBIT)) |
| 115 | ch &= 0x7f; |
| 116 | if (isalpha(ch)) { |
| 117 | if ((flags & RPP_FORCELOWER)) |
| 118 | ch = tolower(ch); |
| 119 | if ((flags & RPP_FORCEUPPER)) |
| 120 | ch = toupper(ch); |
| 121 | } |
| 122 | *p++ = ch; |
| 123 | } |
| 124 | } |
| 125 | *p = '\0'; |
| 126 | save_errno = errno; |
| 127 | if (!(term.c_lflag & ECHO)) |
| 128 | _write(output, "\n", 1); |
| 129 | |
| 130 | /* Restore old terminal settings and signals. */ |
| 131 | if (memcmp(&term, &oterm, sizeof(term)) != 0) |
| 132 | tcsetattr(input, TCSANOW|TCSASOFT, &oterm); |
| 133 | _sigaction(SIGINT, &saveint, NULL); |
| 134 | _sigaction(SIGHUP, &savehup, NULL); |
| 135 | _sigaction(SIGQUIT, &savequit, NULL); |
| 136 | _sigaction(SIGTERM, &saveterm, NULL); |
| 137 | _sigaction(SIGTSTP, &savetstp, NULL); |
| 138 | _sigaction(SIGTTIN, &savettin, NULL); |
| 139 | _sigaction(SIGTTOU, &savettou, NULL); |
| 140 | if (input != STDIN_FILENO) |
| 141 | _close(input); |
| 142 | |
| 143 | /* |
| 144 | * If we were interrupted by a signal, resend it to ourselves |
| 145 | * now that we have restored the signal handlers. |
| 146 | */ |
| 147 | if (signo) { |
| 148 | kill(getpid(), signo); |
| 149 | switch (signo) { |
| 150 | case SIGTSTP: |
| 151 | case SIGTTIN: |
| 152 | case SIGTTOU: |
| 153 | signo = 0; |
| 154 | goto restart; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | errno = save_errno; |
| 159 | return(nr == -1 ? NULL : buf); |
| 160 | } |
| 161 | |
| 162 | char * |
| 163 | getpass(const char *prompt) |
| 164 | { |
| 165 | static char buf[_PASSWORD_LEN + 1]; |
| 166 | |
| 167 | if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL) |
| 168 | buf[0] = '\0'; |
| 169 | return(buf); |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | handler(int s) |
| 174 | { |
| 175 | signo = s; |
| 176 | } |