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