]>
Commit | Line | Data |
---|---|---|
1656cbed | 1 | /* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */ |
2 | ||
3159d49a | 3 | /* |
4 | * Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. The name of the author may not be used to endorse or promote products | |
16 | * derived from this software without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | |
19 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
20 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | #if defined(LIBC_SCCS) && !defined(lint) | |
1656cbed | 31 | static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $"; |
3159d49a | 32 | #endif /* LIBC_SCCS and not lint */ |
33 | ||
34 | #include "includes.h" | |
35 | ||
36 | #ifndef HAVE_READPASSPHRASE | |
37 | ||
38 | #include <termios.h> | |
39 | #include <readpassphrase.h> | |
40 | ||
41 | #ifdef TCSASOFT | |
42 | # define _T_FLUSH (TCSAFLUSH|TCSASOFT) | |
43 | #else | |
44 | # define _T_FLUSH (TCSAFLUSH) | |
45 | #endif | |
46 | ||
d321c94b | 47 | /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */ |
48 | #if !defined(_POSIX_VDISABLE) && defined(VDISABLE) | |
49 | # define _POSIX_VDISABLE VDISABLE | |
50 | #endif | |
51 | ||
1656cbed | 52 | static volatile sig_atomic_t signo; |
53 | ||
54 | static void handler(int); | |
55 | ||
3159d49a | 56 | char * |
1656cbed | 57 | readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) |
3159d49a | 58 | { |
1656cbed | 59 | ssize_t nr; |
60 | int input, output, save_errno; | |
3159d49a | 61 | char ch, *p, *end; |
1656cbed | 62 | struct termios term, oterm; |
63 | struct sigaction sa, saveint, savehup, savequit, saveterm; | |
64 | struct sigaction savetstp, savettin, savettou; | |
3159d49a | 65 | |
66 | /* I suppose we could alloc on demand in this case (XXX). */ | |
67 | if (bufsiz == 0) { | |
68 | errno = EINVAL; | |
69 | return(NULL); | |
70 | } | |
71 | ||
1656cbed | 72 | restart: |
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 | */ | |
77 | if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { | |
78 | if (flags & RPP_REQUIRE_TTY) { | |
79 | errno = ENOTTY; | |
80 | return(NULL); | |
81 | } | |
82 | input = STDIN_FILENO; | |
83 | output = STDERR_FILENO; | |
84 | } | |
85 | ||
86 | /* | |
1656cbed | 87 | * Catch signals that would otherwise cause the user to end |
88 | * up with echo turned off in the shell. Don't worry about | |
89 | * things like SIGALRM and SIGPIPE for now. | |
3159d49a | 90 | */ |
1656cbed | 91 | sigemptyset(&sa.sa_mask); |
92 | sa.sa_flags = 0; /* don't restart system calls */ | |
93 | sa.sa_handler = handler; | |
94 | (void)sigaction(SIGINT, &sa, &saveint); | |
95 | (void)sigaction(SIGHUP, &sa, &savehup); | |
96 | (void)sigaction(SIGQUIT, &sa, &savequit); | |
97 | (void)sigaction(SIGTERM, &sa, &saveterm); | |
98 | (void)sigaction(SIGTSTP, &sa, &savetstp); | |
99 | (void)sigaction(SIGTTIN, &sa, &savettin); | |
100 | (void)sigaction(SIGTTOU, &sa, &savettou); | |
3159d49a | 101 | |
102 | /* Turn off echo if possible. */ | |
1656cbed | 103 | if (tcgetattr(input, &oterm) == 0) { |
104 | memcpy(&term, &oterm, sizeof(term)); | |
105 | if (!(flags & RPP_ECHO_ON)) | |
106 | term.c_lflag &= ~(ECHO | ECHONL); | |
3159d49a | 107 | #ifdef VSTATUS |
1656cbed | 108 | if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) |
3159d49a | 109 | term.c_cc[VSTATUS] = _POSIX_VDISABLE; |
3159d49a | 110 | #endif |
111 | (void)tcsetattr(input, _T_FLUSH, &term); | |
1656cbed | 112 | } else { |
113 | memset(&term, 0, sizeof(term)); | |
114 | memset(&oterm, 0, sizeof(oterm)); | |
3159d49a | 115 | } |
116 | ||
117 | (void)write(output, prompt, strlen(prompt)); | |
118 | end = buf + bufsiz - 1; | |
1656cbed | 119 | for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { |
3159d49a | 120 | if (p < end) { |
121 | if ((flags & RPP_SEVENBIT)) | |
0eb1a22d | 122 | ch &= 0x7f; |
3159d49a | 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'; | |
1656cbed | 133 | save_errno = errno; |
134 | if (!(term.c_lflag & ECHO)) | |
135 | (void)write(output, "\n", 1); | |
136 | ||
137 | /* Restore old terminal settings and signals. */ | |
138 | if (memcmp(&term, &oterm, sizeof(term)) != 0) | |
969d6479 | 139 | (void)tcsetattr(input, _T_FLUSH, &oterm); |
1656cbed | 140 | (void)sigaction(SIGINT, &saveint, NULL); |
141 | (void)sigaction(SIGHUP, &savehup, NULL); | |
142 | (void)sigaction(SIGQUIT, &savequit, NULL); | |
143 | (void)sigaction(SIGTERM, &saveterm, NULL); | |
144 | (void)sigaction(SIGTSTP, &savetstp, NULL); | |
145 | (void)sigaction(SIGTTIN, &savettin, NULL); | |
146 | (void)sigaction(SIGTTOU, &savettou, NULL); | |
3159d49a | 147 | if (input != STDIN_FILENO) |
148 | (void)close(input); | |
1656cbed | 149 | |
150 | /* | |
151 | * If we were interrupted by a signal, resend it to ourselves | |
152 | * now that we have restored the signal handlers. | |
153 | */ | |
154 | if (signo) { | |
155 | kill(getpid(), signo); | |
156 | switch (signo) { | |
157 | case SIGTSTP: | |
158 | case SIGTTIN: | |
159 | case SIGTTOU: | |
160 | signo = 0; | |
161 | goto restart; | |
162 | } | |
163 | } | |
164 | ||
165 | errno = save_errno; | |
166 | return(nr == -1 ? NULL : buf); | |
3159d49a | 167 | } |
1656cbed | 168 | |
3159d49a | 169 | #if 0 |
170 | char * | |
1656cbed | 171 | getpass(const char *prompt) |
3159d49a | 172 | { |
173 | static char buf[_PASSWORD_LEN + 1]; | |
174 | ||
175 | return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF)); | |
176 | } | |
177 | #endif | |
1656cbed | 178 | |
179 | static void handler(int s) | |
180 | { | |
1656cbed | 181 | signo = s; |
182 | } | |
50903cc7 | 183 | #endif /* HAVE_READPASSPHRASE */ |