Damien Miller | 3db2e4d | 2003-11-24 13:33:34 +1100 | [diff] [blame] | 1 | /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */ |
| 2 | |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 3 | /* $OpenBSD: readpassphrase.c,v 1.16 2003/06/17 21:56:23 millert Exp $ */ |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 4 | |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 5 | /* |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 6 | * Copyright (c) 2000-2002 Todd C. Miller <Todd.Miller@courtesan.com> |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 7 | * |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 8 | * Permission to use, copy, modify, and distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 11 | * |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | * |
| 20 | * Sponsored in part by the Defense Advanced Research Projects |
| 21 | * Agency (DARPA) and Air Force Research Laboratory, Air Force |
| 22 | * Materiel Command, USAF, under agreement number F39502-99-1-0512. |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #if defined(LIBC_SCCS) && !defined(lint) |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 26 | static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.16 2003/06/17 21:56:23 millert Exp $"; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 27 | #endif /* LIBC_SCCS and not lint */ |
| 28 | |
| 29 | #include "includes.h" |
| 30 | |
| 31 | #ifndef HAVE_READPASSPHRASE |
| 32 | |
| 33 | #include <termios.h> |
| 34 | #include <readpassphrase.h> |
| 35 | |
| 36 | #ifdef TCSASOFT |
| 37 | # define _T_FLUSH (TCSAFLUSH|TCSASOFT) |
| 38 | #else |
| 39 | # define _T_FLUSH (TCSAFLUSH) |
| 40 | #endif |
| 41 | |
Ben Lindstrom | a0bd44c | 2001-10-25 15:02:35 +0000 | [diff] [blame] | 42 | /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */ |
| 43 | #if !defined(_POSIX_VDISABLE) && defined(VDISABLE) |
| 44 | # define _POSIX_VDISABLE VDISABLE |
| 45 | #endif |
| 46 | |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 47 | static volatile sig_atomic_t signo; |
| 48 | |
| 49 | static void handler(int); |
| 50 | |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 51 | char * |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 52 | readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 53 | { |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 54 | ssize_t nr; |
| 55 | int input, output, save_errno; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 56 | char ch, *p, *end; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 57 | struct termios term, oterm; |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 58 | struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; |
| 59 | struct sigaction savetstp, savettin, savettou, savepipe; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 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 | |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 67 | restart: |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 68 | signo = 0; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 69 | /* |
| 70 | * Read and write to /dev/tty if available. If not, read from |
| 71 | * stdin and write to stderr unless a tty is required. |
| 72 | */ |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 73 | if ((flags & RPP_STDIN) || |
| 74 | (input = output = open(_PATH_TTY, O_RDWR)) == -1) { |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 75 | if (flags & RPP_REQUIRE_TTY) { |
| 76 | errno = ENOTTY; |
| 77 | return(NULL); |
| 78 | } |
| 79 | input = STDIN_FILENO; |
| 80 | output = STDERR_FILENO; |
| 81 | } |
| 82 | |
| 83 | /* |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 84 | * Catch signals that would otherwise cause the user to end |
| 85 | * up with echo turned off in the shell. Don't worry about |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 86 | * things like SIGXCPU and SIGVTALRM for now. |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 87 | */ |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 88 | sigemptyset(&sa.sa_mask); |
| 89 | sa.sa_flags = 0; /* don't restart system calls */ |
| 90 | sa.sa_handler = handler; |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 91 | (void)sigaction(SIGALRM, &sa, &savealrm); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 92 | (void)sigaction(SIGHUP, &sa, &savehup); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 93 | (void)sigaction(SIGINT, &sa, &saveint); |
| 94 | (void)sigaction(SIGPIPE, &sa, &savepipe); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 95 | (void)sigaction(SIGQUIT, &sa, &savequit); |
| 96 | (void)sigaction(SIGTERM, &sa, &saveterm); |
| 97 | (void)sigaction(SIGTSTP, &sa, &savetstp); |
| 98 | (void)sigaction(SIGTTIN, &sa, &savettin); |
| 99 | (void)sigaction(SIGTTOU, &sa, &savettou); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 100 | |
| 101 | /* Turn off echo if possible. */ |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 102 | if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) { |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 103 | memcpy(&term, &oterm, sizeof(term)); |
| 104 | if (!(flags & RPP_ECHO_ON)) |
| 105 | term.c_lflag &= ~(ECHO | ECHONL); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 106 | #ifdef VSTATUS |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 107 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 108 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 109 | #endif |
| 110 | (void)tcsetattr(input, _T_FLUSH, &term); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 111 | } else { |
| 112 | memset(&term, 0, sizeof(term)); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 113 | term.c_lflag |= ECHO; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 114 | memset(&oterm, 0, sizeof(oterm)); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 115 | oterm.c_lflag |= ECHO; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 116 | } |
| 117 | |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 118 | if (!(flags & RPP_STDIN)) |
| 119 | (void)write(output, prompt, strlen(prompt)); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 120 | end = buf + bufsiz - 1; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 121 | 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] | 122 | if (p < end) { |
| 123 | if ((flags & RPP_SEVENBIT)) |
Damien Miller | b90416b | 2001-06-27 23:26:38 +1000 | [diff] [blame] | 124 | ch &= 0x7f; |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 125 | if (isalpha(ch)) { |
| 126 | if ((flags & RPP_FORCELOWER)) |
| 127 | ch = tolower(ch); |
| 128 | if ((flags & RPP_FORCEUPPER)) |
| 129 | ch = toupper(ch); |
| 130 | } |
| 131 | *p++ = ch; |
| 132 | } |
| 133 | } |
| 134 | *p = '\0'; |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 135 | save_errno = errno; |
| 136 | if (!(term.c_lflag & ECHO)) |
| 137 | (void)write(output, "\n", 1); |
| 138 | |
| 139 | /* Restore old terminal settings and signals. */ |
| 140 | if (memcmp(&term, &oterm, sizeof(term)) != 0) |
Damien Miller | 89a1b9f | 2002-02-13 16:43:52 +1100 | [diff] [blame] | 141 | (void)tcsetattr(input, _T_FLUSH, &oterm); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 142 | (void)sigaction(SIGALRM, &savealrm, NULL); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 143 | (void)sigaction(SIGHUP, &savehup, NULL); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 144 | (void)sigaction(SIGINT, &saveint, NULL); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 145 | (void)sigaction(SIGQUIT, &savequit, NULL); |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 146 | (void)sigaction(SIGPIPE, &savepipe, NULL); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 147 | (void)sigaction(SIGTERM, &saveterm, NULL); |
| 148 | (void)sigaction(SIGTSTP, &savetstp, NULL); |
| 149 | (void)sigaction(SIGTTIN, &savettin, NULL); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 150 | if (input != STDIN_FILENO) |
| 151 | (void)close(input); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * If we were interrupted by a signal, resend it to ourselves |
| 155 | * now that we have restored the signal handlers. |
| 156 | */ |
| 157 | if (signo) { |
Damien Miller | 71eb0c1 | 2002-09-11 10:29:11 +1000 | [diff] [blame] | 158 | kill(getpid(), signo); |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 159 | switch (signo) { |
| 160 | case SIGTSTP: |
| 161 | case SIGTTIN: |
| 162 | case SIGTTOU: |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 163 | goto restart; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | errno = save_errno; |
| 168 | return(nr == -1 ? NULL : buf); |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 169 | } |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 170 | |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 171 | #if 0 |
| 172 | char * |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 173 | getpass(const char *prompt) |
Damien Miller | c8a3868 | 2001-06-25 18:09:16 +1000 | [diff] [blame] | 174 | { |
| 175 | static char buf[_PASSWORD_LEN + 1]; |
| 176 | |
| 177 | return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); |
| 178 | } |
| 179 | #endif |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 180 | |
| 181 | static void handler(int s) |
| 182 | { |
Ben Lindstrom | af4a6c3 | 2003-08-25 01:10:51 +0000 | [diff] [blame] | 183 | |
Damien Miller | 8e3bdca | 2002-02-13 16:00:15 +1100 | [diff] [blame] | 184 | signo = s; |
| 185 | } |
Damien Miller | 804357a | 2002-05-01 22:00:22 +1000 | [diff] [blame] | 186 | #endif /* HAVE_READPASSPHRASE */ |