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