]>
Commit | Line | Data |
---|---|---|
851a428e | 1 | /* $OpenBSD: readpassphrase.c,v 1.21 2008/01/17 16:27:07 millert Exp $ */ |
1656cbed | 2 | |
3159d49a | 3 | /* |
851a428e | 4 | * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com> |
3159d49a | 5 | * |
510a42ce | 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. | |
3159d49a | 9 | * |
510a42ce | 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. | |
3159d49a | 21 | */ |
22 | ||
f6d4fb87 | 23 | /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */ |
24 | ||
3159d49a | 25 | #include "includes.h" |
26 | ||
27 | #ifndef HAVE_READPASSPHRASE | |
28 | ||
29 | #include <termios.h> | |
b5b88c19 | 30 | #include <signal.h> |
31 | #include <ctype.h> | |
22bbb3e6 | 32 | #include <fcntl.h> |
3159d49a | 33 | #include <readpassphrase.h> |
fec71b2f | 34 | #include <errno.h> |
28cb0a43 | 35 | #include <string.h> |
36 | #include <unistd.h> | |
3159d49a | 37 | |
38 | #ifdef TCSASOFT | |
39 | # define _T_FLUSH (TCSAFLUSH|TCSASOFT) | |
40 | #else | |
41 | # define _T_FLUSH (TCSAFLUSH) | |
42 | #endif | |
43 | ||
d321c94b | 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 | ||
1656cbed | 49 | static volatile sig_atomic_t signo; |
50 | ||
51 | static void handler(int); | |
52 | ||
3159d49a | 53 | char * |
1656cbed | 54 | readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) |
3159d49a | 55 | { |
1656cbed | 56 | ssize_t nr; |
57 | int input, output, save_errno; | |
3159d49a | 58 | char ch, *p, *end; |
1656cbed | 59 | struct termios term, oterm; |
ac8802eb | 60 | struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm; |
61 | struct sigaction savetstp, savettin, savettou, savepipe; | |
3159d49a | 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 | ||
1656cbed | 69 | restart: |
ac8802eb | 70 | signo = 0; |
851a428e | 71 | nr = -1; |
72 | save_errno = 0; | |
3159d49a | 73 | /* |
74 | * Read and write to /dev/tty if available. If not, read from | |
75 | * stdin and write to stderr unless a tty is required. | |
76 | */ | |
ac8802eb | 77 | if ((flags & RPP_STDIN) || |
78 | (input = output = open(_PATH_TTY, O_RDWR)) == -1) { | |
3159d49a | 79 | if (flags & RPP_REQUIRE_TTY) { |
80 | errno = ENOTTY; | |
81 | return(NULL); | |
82 | } | |
83 | input = STDIN_FILENO; | |
84 | output = STDERR_FILENO; | |
85 | } | |
86 | ||
87 | /* | |
1656cbed | 88 | * Catch signals that would otherwise cause the user to end |
89 | * up with echo turned off in the shell. Don't worry about | |
ac8802eb | 90 | * things like SIGXCPU and SIGVTALRM for now. |
3159d49a | 91 | */ |
1656cbed | 92 | sigemptyset(&sa.sa_mask); |
93 | sa.sa_flags = 0; /* don't restart system calls */ | |
94 | sa.sa_handler = handler; | |
ac8802eb | 95 | (void)sigaction(SIGALRM, &sa, &savealrm); |
1656cbed | 96 | (void)sigaction(SIGHUP, &sa, &savehup); |
ac8802eb | 97 | (void)sigaction(SIGINT, &sa, &saveint); |
98 | (void)sigaction(SIGPIPE, &sa, &savepipe); | |
1656cbed | 99 | (void)sigaction(SIGQUIT, &sa, &savequit); |
100 | (void)sigaction(SIGTERM, &sa, &saveterm); | |
101 | (void)sigaction(SIGTSTP, &sa, &savetstp); | |
102 | (void)sigaction(SIGTTIN, &sa, &savettin); | |
103 | (void)sigaction(SIGTTOU, &sa, &savettou); | |
3159d49a | 104 | |
105 | /* Turn off echo if possible. */ | |
ac8802eb | 106 | if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) { |
1656cbed | 107 | memcpy(&term, &oterm, sizeof(term)); |
108 | if (!(flags & RPP_ECHO_ON)) | |
109 | term.c_lflag &= ~(ECHO | ECHONL); | |
3159d49a | 110 | #ifdef VSTATUS |
1656cbed | 111 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) |
3159d49a | 112 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; |
3159d49a | 113 | #endif |
114 | (void)tcsetattr(input, _T_FLUSH, &term); | |
1656cbed | 115 | } else { |
116 | memset(&term, 0, sizeof(term)); | |
ac8802eb | 117 | term.c_lflag |= ECHO; |
1656cbed | 118 | memset(&oterm, 0, sizeof(oterm)); |
ac8802eb | 119 | oterm.c_lflag |= ECHO; |
3159d49a | 120 | } |
121 | ||
851a428e | 122 | /* No I/O if we are already backgrounded. */ |
123 | if (signo != SIGTTOU && signo != SIGTTIN) { | |
124 | if (!(flags & RPP_STDIN)) | |
125 | (void)write(output, prompt, strlen(prompt)); | |
126 | end = buf + bufsiz - 1; | |
127 | p = buf; | |
128 | while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') { | |
129 | if (p < end) { | |
130 | if ((flags & RPP_SEVENBIT)) | |
131 | ch &= 0x7f; | |
132 | if (isalpha(ch)) { | |
133 | if ((flags & RPP_FORCELOWER)) | |
134 | ch = (char)tolower(ch); | |
135 | if ((flags & RPP_FORCEUPPER)) | |
136 | ch = (char)toupper(ch); | |
137 | } | |
138 | *p++ = ch; | |
3159d49a | 139 | } |
3159d49a | 140 | } |
851a428e | 141 | *p = '\0'; |
142 | save_errno = errno; | |
143 | if (!(term.c_lflag & ECHO)) | |
144 | (void)write(output, "\n", 1); | |
3159d49a | 145 | } |
1656cbed | 146 | |
147 | /* Restore old terminal settings and signals. */ | |
2f4f4297 | 148 | if (memcmp(&term, &oterm, sizeof(term)) != 0) { |
ac9862c5 | 149 | while (tcsetattr(input, _T_FLUSH, &oterm) == -1 && |
2f4f4297 | 150 | errno == EINTR) |
151 | continue; | |
152 | } | |
ac8802eb | 153 | (void)sigaction(SIGALRM, &savealrm, NULL); |
1656cbed | 154 | (void)sigaction(SIGHUP, &savehup, NULL); |
ac8802eb | 155 | (void)sigaction(SIGINT, &saveint, NULL); |
1656cbed | 156 | (void)sigaction(SIGQUIT, &savequit, NULL); |
ac8802eb | 157 | (void)sigaction(SIGPIPE, &savepipe, NULL); |
1656cbed | 158 | (void)sigaction(SIGTERM, &saveterm, NULL); |
159 | (void)sigaction(SIGTSTP, &savetstp, NULL); | |
160 | (void)sigaction(SIGTTIN, &savettin, NULL); | |
1f4dfa18 | 161 | (void)sigaction(SIGTTOU, &savettou, NULL); |
3159d49a | 162 | if (input != STDIN_FILENO) |
163 | (void)close(input); | |
1656cbed | 164 | |
165 | /* | |
166 | * If we were interrupted by a signal, resend it to ourselves | |
167 | * now that we have restored the signal handlers. | |
168 | */ | |
169 | if (signo) { | |
ac8802eb | 170 | kill(getpid(), signo); |
1656cbed | 171 | switch (signo) { |
172 | case SIGTSTP: | |
173 | case SIGTTIN: | |
174 | case SIGTTOU: | |
1656cbed | 175 | goto restart; |
176 | } | |
177 | } | |
178 | ||
851a428e | 179 | if (save_errno) |
180 | errno = save_errno; | |
1656cbed | 181 | return(nr == -1 ? NULL : buf); |
3159d49a | 182 | } |
1f4dfa18 | 183 | |
3159d49a | 184 | #if 0 |
185 | char * | |
1656cbed | 186 | getpass(const char *prompt) |
3159d49a | 187 | { |
188 | static char buf[_PASSWORD_LEN + 1]; | |
189 | ||
190 | return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); | |
191 | } | |
192 | #endif | |
1656cbed | 193 | |
194 | static void handler(int s) | |
195 | { | |
510a42ce | 196 | |
1656cbed | 197 | signo = s; |
198 | } | |
50903cc7 | 199 | #endif /* HAVE_READPASSPHRASE */ |