1 /* $OpenBSD: readpassphrase.c,v 1.22 2010/01/13 10:20:54 dtucker Exp $ */
4 * Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
23 /* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
33 #include <readpassphrase.h>
36 # define _PATH_TTY "/dev/tty"
40 # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
42 # define _T_FLUSH (TCSAFLUSH)
45 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
46 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
47 # define _POSIX_VDISABLE VDISABLE
58 static volatile sig_atomic_t signo
[_NSIG
];
60 static void handler(int);
63 readpassphrase(const char *prompt
, char *buf
, size_t bufsiz
, int flags
)
65 ssize_t bytes_written
= 0;
67 int input
, output
, save_errno
, i
, need_restart
;
69 struct termios term
, oterm
;
70 struct sigaction sa
, savealrm
, saveint
, savehup
, savequit
, saveterm
;
71 struct sigaction savetstp
, savettin
, savettou
, savepipe
;
73 /* I suppose we could alloc on demand in this case (XXX). */
80 for (i
= 0; i
< _NSIG
; i
++)
86 * Read and write to /dev/tty if available. If not, read from
87 * stdin and write to stderr unless a tty is required.
89 if ((flags
& RPP_STDIN
) ||
90 (input
= output
= open(_PATH_TTY
, O_RDWR
)) == -1) {
91 if (flags
& RPP_REQUIRE_TTY
) {
96 output
= STDERR_FILENO
;
100 * Catch signals that would otherwise cause the user to end
101 * up with echo turned off in the shell. Don't worry about
102 * things like SIGXCPU and SIGVTALRM for now.
104 sigemptyset(&sa
.sa_mask
);
105 sa
.sa_flags
= 0; /* don't restart system calls */
106 sa
.sa_handler
= handler
;
107 (void)sigaction(SIGALRM
, &sa
, &savealrm
);
108 (void)sigaction(SIGHUP
, &sa
, &savehup
);
109 (void)sigaction(SIGINT
, &sa
, &saveint
);
110 (void)sigaction(SIGPIPE
, &sa
, &savepipe
);
111 (void)sigaction(SIGQUIT
, &sa
, &savequit
);
112 (void)sigaction(SIGTERM
, &sa
, &saveterm
);
113 (void)sigaction(SIGTSTP
, &sa
, &savetstp
);
114 (void)sigaction(SIGTTIN
, &sa
, &savettin
);
115 (void)sigaction(SIGTTOU
, &sa
, &savettou
);
117 /* Turn off echo if possible. */
118 if (input
!= STDIN_FILENO
&& tcgetattr(input
, &oterm
) == 0) {
119 memcpy(&term
, &oterm
, sizeof(term
));
120 if (!(flags
& RPP_ECHO_ON
))
121 term
.c_lflag
&= ~(ECHO
| ECHONL
);
123 if (term
.c_cc
[VSTATUS
] != _POSIX_VDISABLE
)
124 term
.c_cc
[VSTATUS
] = _POSIX_VDISABLE
;
126 (void)tcsetattr(input
, _T_FLUSH
, &term
);
128 memset(&term
, 0, sizeof(term
));
129 term
.c_lflag
|= ECHO
;
130 memset(&oterm
, 0, sizeof(oterm
));
131 oterm
.c_lflag
|= ECHO
;
134 /* No I/O if we are already backgrounded. */
135 if (signo
[SIGTTOU
] != 1 && signo
[SIGTTIN
] != 1) {
136 if (!(flags
& RPP_STDIN
))
137 bytes_written
= write(output
, prompt
, strlen(prompt
));
138 end
= buf
+ bufsiz
- 1;
140 while ((nr
= read(input
, &ch
, 1)) == 1 && ch
!= '\n' && ch
!= '\r') {
142 if ((flags
& RPP_SEVENBIT
))
144 if (isalpha((unsigned char)ch
)) {
145 if ((flags
& RPP_FORCELOWER
))
146 ch
= (char)tolower((unsigned char)ch
);
147 if ((flags
& RPP_FORCEUPPER
))
148 ch
= (char)toupper((unsigned char)ch
);
155 if (!(term
.c_lflag
& ECHO
))
156 bytes_written
= write(output
, "\n", 1);
159 (void) bytes_written
;
161 /* Restore old terminal settings and signals. */
162 if (memcmp(&term
, &oterm
, sizeof(term
)) != 0) {
163 while (tcsetattr(input
, _T_FLUSH
, &oterm
) == -1 &&
167 (void)sigaction(SIGALRM
, &savealrm
, NULL
);
168 (void)sigaction(SIGHUP
, &savehup
, NULL
);
169 (void)sigaction(SIGINT
, &saveint
, NULL
);
170 (void)sigaction(SIGQUIT
, &savequit
, NULL
);
171 (void)sigaction(SIGPIPE
, &savepipe
, NULL
);
172 (void)sigaction(SIGTERM
, &saveterm
, NULL
);
173 (void)sigaction(SIGTSTP
, &savetstp
, NULL
);
174 (void)sigaction(SIGTTIN
, &savettin
, NULL
);
175 (void)sigaction(SIGTTOU
, &savettou
, NULL
);
176 if (input
!= STDIN_FILENO
)
180 * If we were interrupted by a signal, resend it to ourselves
181 * now that we have restored the signal handlers.
183 for (i
= 0; i
< _NSIG
; i
++) {
199 return(nr
== -1 ? NULL
: buf
);
202 static void handler(int s
)