Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 1 | /* $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $ */ |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 2 | |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 3 | /* |
Darren Tucker | 1035cb4 | 2010-01-13 18:32:59 +1100 | [diff] [blame] | 4 | * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com> |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 5 | * |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 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. |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 9 | * |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 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. |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 21 | */ |
| 22 | |
Darren Tucker | 7f24a0e | 2005-11-10 16:18:56 +1100 | [diff] [blame] | 23 | /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */ |
| 24 | |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 25 | #include "includes.h" |
| 26 | |
| 27 | #ifndef HAVE_READPASSPHRASE |
| 28 | |
| 29 | #include <termios.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <ctype.h> |
Damien Miller | a1738e4 | 2006-07-10 21:33:04 +1000 | [diff] [blame] | 32 | #include <fcntl.h> |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 33 | #include <readpassphrase.h> |
Darren Tucker | 2c1a02a | 2006-07-12 22:40:50 +1000 | [diff] [blame] | 34 | #include <errno.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <unistd.h> |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 37 | |
| 38 | #ifdef TCSASOFT |
| 39 | # define _T_FLUSH (TCSAFLUSH|TCSASOFT) |
| 40 | #else |
| 41 | # define _T_FLUSH (TCSAFLUSH) |
| 42 | #endif |
| 43 | |
Ben Lindstrom | a0bd44c | 2001-10-25 15:02:35 +0000 | [diff] [blame] | 44 | /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */ |
| 45 | #if !defined(_POSIX_VDISABLE) && defined(VDISABLE) |
| 46 | # define _POSIX_VDISABLE VDISABLE |
| 47 | #endif |
| 48 | |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 49 | static volatile sig_atomic_t signo[_NSIG]; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 50 | |
| 51 | static void handler(int); |
| 52 | |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 53 | char * |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 54 | readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 55 | { |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 56 | ssize_t nr; |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 57 | int input, output, save_errno, i, need_restart; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 58 | char ch, *p, *end; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 59 | struct termios term, oterm; |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 60 | struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; |
| 61 | struct sigaction savetstp, savettin, savettou, savepipe; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 62 | |
| 63 | /* I suppose we could alloc on demand in this case (XXX). */ |
| 64 | if (bufsiz == 0) { |
| 65 | errno = EINVAL; |
| 66 | return(NULL); |
| 67 | } |
| 68 | |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 69 | restart: |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 70 | for (i = 0; i < _NSIG; i++) |
| 71 | signo[i] = 0; |
Darren Tucker | 1035cb4 | 2010-01-13 18:32:59 +1100 | [diff] [blame] | 72 | nr = -1; |
| 73 | save_errno = 0; |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 74 | need_restart = 0; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 75 | /* |
| 76 | * Read and write to /dev/tty if available. If not, read from |
| 77 | * stdin and write to stderr unless a tty is required. |
| 78 | */ |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 79 | if ((flags & RPP_STDIN) || |
| 80 | (input = output = open(_PATH_TTY, O_RDWR)) == -1) { |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 81 | if (flags & RPP_REQUIRE_TTY) { |
| 82 | errno = ENOTTY; |
| 83 | return(NULL); |
| 84 | } |
| 85 | input = STDIN_FILENO; |
| 86 | output = STDERR_FILENO; |
| 87 | } |
| 88 | |
| 89 | /* |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 90 | * Catch signals that would otherwise cause the user to end |
| 91 | * up with echo turned off in the shell. Don't worry about |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 92 | * things like SIGXCPU and SIGVTALRM for now. |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 93 | */ |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 94 | sigemptyset(&sa.sa_mask); |
| 95 | sa.sa_flags = 0; /* don't restart system calls */ |
| 96 | sa.sa_handler = handler; |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 97 | (void)sigaction(SIGALRM, &sa, &savealrm); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 98 | (void)sigaction(SIGHUP, &sa, &savehup); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 99 | (void)sigaction(SIGINT, &sa, &saveint); |
| 100 | (void)sigaction(SIGPIPE, &sa, &savepipe); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 101 | (void)sigaction(SIGQUIT, &sa, &savequit); |
| 102 | (void)sigaction(SIGTERM, &sa, &saveterm); |
| 103 | (void)sigaction(SIGTSTP, &sa, &savetstp); |
| 104 | (void)sigaction(SIGTTIN, &sa, &savettin); |
| 105 | (void)sigaction(SIGTTOU, &sa, &savettou); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 106 | |
| 107 | /* Turn off echo if possible. */ |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 108 | if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) { |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 109 | memcpy(&term, &oterm, sizeof(term)); |
| 110 | if (!(flags & RPP_ECHO_ON)) |
| 111 | term.c_lflag &= ~(ECHO | ECHONL); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 112 | #ifdef VSTATUS |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 113 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 114 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 115 | #endif |
| 116 | (void)tcsetattr(input, _T_FLUSH, &term); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 117 | } else { |
| 118 | memset(&term, 0, sizeof(term)); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 119 | term.c_lflag |= ECHO; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 120 | memset(&oterm, 0, sizeof(oterm)); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 121 | oterm.c_lflag |= ECHO; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 122 | } |
| 123 | |
Darren Tucker | 1035cb4 | 2010-01-13 18:32:59 +1100 | [diff] [blame] | 124 | /* No I/O if we are already backgrounded. */ |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 125 | if (signo[SIGTTOU] != 1 && signo[SIGTTIN] != 1) { |
Darren Tucker | 1035cb4 | 2010-01-13 18:32:59 +1100 | [diff] [blame] | 126 | if (!(flags & RPP_STDIN)) |
| 127 | (void)write(output, prompt, strlen(prompt)); |
| 128 | end = buf + bufsiz - 1; |
| 129 | p = buf; |
| 130 | while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') { |
| 131 | if (p < end) { |
| 132 | if ((flags & RPP_SEVENBIT)) |
| 133 | ch &= 0x7f; |
| 134 | if (isalpha(ch)) { |
| 135 | if ((flags & RPP_FORCELOWER)) |
| 136 | ch = (char)tolower(ch); |
| 137 | if ((flags & RPP_FORCEUPPER)) |
| 138 | ch = (char)toupper(ch); |
| 139 | } |
| 140 | *p++ = ch; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 141 | } |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 142 | } |
Darren Tucker | 1035cb4 | 2010-01-13 18:32:59 +1100 | [diff] [blame] | 143 | *p = '\0'; |
| 144 | save_errno = errno; |
| 145 | if (!(term.c_lflag & ECHO)) |
| 146 | (void)write(output, "\n", 1); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 147 | } |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 148 | |
| 149 | /* Restore old terminal settings and signals. */ |
Damien Miller | 308c8b1 | 2005-05-24 16:39:03 +1000 | [diff] [blame] | 150 | if (memcmp(&term, &oterm, sizeof(term)) != 0) { |
Damien Miller | 101afbb | 2005-05-24 16:42:00 +1000 | [diff] [blame] | 151 | while (tcsetattr(input, _T_FLUSH, &oterm) == -1 && |
Damien Miller | 308c8b1 | 2005-05-24 16:39:03 +1000 | [diff] [blame] | 152 | errno == EINTR) |
| 153 | continue; |
| 154 | } |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 155 | (void)sigaction(SIGALRM, &savealrm, NULL); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 156 | (void)sigaction(SIGHUP, &savehup, NULL); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 157 | (void)sigaction(SIGINT, &saveint, NULL); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 158 | (void)sigaction(SIGQUIT, &savequit, NULL); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 159 | (void)sigaction(SIGPIPE, &savepipe, NULL); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 160 | (void)sigaction(SIGTERM, &saveterm, NULL); |
| 161 | (void)sigaction(SIGTSTP, &savetstp, NULL); |
| 162 | (void)sigaction(SIGTTIN, &savettin, NULL); |
Darren Tucker | ab3c2ca | 2010-01-13 18:27:32 +1100 | [diff] [blame] | 163 | (void)sigaction(SIGTTOU, &savettou, NULL); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 164 | if (input != STDIN_FILENO) |
| 165 | (void)close(input); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * If we were interrupted by a signal, resend it to ourselves |
| 169 | * now that we have restored the signal handlers. |
| 170 | */ |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 171 | for (i = 0; i < _NSIG; i++) { |
| 172 | if (signo[i]) { |
| 173 | kill(getpid(), i); |
| 174 | switch (i) { |
| 175 | case SIGTSTP: |
| 176 | case SIGTTIN: |
| 177 | case SIGTTOU: |
| 178 | need_restart = 1; |
| 179 | } |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 180 | } |
| 181 | } |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 182 | if (need_restart) |
| 183 | goto restart; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 184 | |
Darren Tucker | 1035cb4 | 2010-01-13 18:32:59 +1100 | [diff] [blame] | 185 | if (save_errno) |
| 186 | errno = save_errno; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 187 | return(nr == -1 ? NULL : buf); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 188 | } |
Darren Tucker | ab3c2ca | 2010-01-13 18:27:32 +1100 | [diff] [blame] | 189 | |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 190 | #if 0 |
| 191 | char * |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 192 | getpass(const char *prompt) |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 193 | { |
| 194 | static char buf[_PASSWORD_LEN + 1]; |
| 195 | |
| 196 | return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); |
| 197 | } |
| 198 | #endif |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 199 | |
| 200 | static void handler(int s) |
| 201 | { |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 202 | |
Darren Tucker | d59487a | 2010-01-13 21:32:44 +1100 | [diff] [blame] | 203 | signo[s] = 1; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 204 | } |
Damien Miller | 804357a | 2002-05-01 22:00:22 +1000 | [diff] [blame] | 205 | #endif /* HAVE_READPASSPHRASE */ |