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