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 */
27 #ifndef HAVE_READPASSPHRASE
33 #include <readpassphrase.h>
39 # define _T_FLUSH (TCSAFLUSH|TCSASOFT)
41 # define _T_FLUSH (TCSAFLUSH)
44 /* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
45 #if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
46 # define _POSIX_VDISABLE VDISABLE
49 static volatile sig_atomic_t signo
[_NSIG
];
51 static void handler(int);
54 readpassphrase(const char *prompt
, char *buf
, size_t bufsiz
, int flags
)
57 int input
, output
, save_errno
, i
, need_restart
;
59 struct termios term
, oterm
;
60 struct sigaction sa
, savealrm
, saveint
, savehup
, savequit
, saveterm
;
61 struct sigaction savetstp
, savettin
, savettou
, savepipe
;
63 /* I suppose we could alloc on demand in this case (XXX). */
70 for (i
= 0; i
< _NSIG
; i
++)
76 * Read and write to /dev/tty if available. If not, read from
77 * stdin and write to stderr unless a tty is required.
79 if ((flags
& RPP_STDIN
) ||
80 (input
= output
= open(_PATH_TTY
, O_RDWR
)) == -1) {
81 if (flags
& RPP_REQUIRE_TTY
) {
86 output
= STDERR_FILENO
;
90 * Catch signals that would otherwise cause the user to end
91 * up with echo turned off in the shell. Don't worry about
92 * things like SIGXCPU and SIGVTALRM for now.
94 sigemptyset(&sa
.sa_mask
);
95 sa
.sa_flags
= 0; /* don't restart system calls */
96 sa
.sa_handler
= handler
;
97 (void)sigaction(SIGALRM
, &sa
, &savealrm
);
98 (void)sigaction(SIGHUP
, &sa
, &savehup
);
99 (void)sigaction(SIGINT
, &sa
, &saveint
);
100 (void)sigaction(SIGPIPE
, &sa
, &savepipe
);
101 (void)sigaction(SIGQUIT
, &sa
, &savequit
);
102 (void)sigaction(SIGTERM
, &sa
, &saveterm
);
103 (void)sigaction(SIGTSTP
, &sa
, &savetstp
);
104 (void)sigaction(SIGTTIN
, &sa
, &savettin
);
105 (void)sigaction(SIGTTOU
, &sa
, &savettou
);
107 /* Turn off echo if possible. */
108 if (input
!= STDIN_FILENO
&& tcgetattr(input
, &oterm
) == 0) {
109 memcpy(&term
, &oterm
, sizeof(term
));
110 if (!(flags
& RPP_ECHO_ON
))
111 term
.c_lflag
&= ~(ECHO
| ECHONL
);
113 if (term
.c_cc
[VSTATUS
] != _POSIX_VDISABLE
)
114 term
.c_cc
[VSTATUS
] = _POSIX_VDISABLE
;
116 (void)tcsetattr(input
, _T_FLUSH
, &term
);
118 memset(&term
, 0, sizeof(term
));
119 term
.c_lflag
|= ECHO
;
120 memset(&oterm
, 0, sizeof(oterm
));
121 oterm
.c_lflag
|= ECHO
;
124 /* No I/O if we are already backgrounded. */
125 if (signo
[SIGTTOU
] != 1 && signo
[SIGTTIN
] != 1) {
126 if (!(flags
& RPP_STDIN
))
127 (void)write(output
, prompt
, strlen(prompt
));
128 end
= buf
+ bufsiz
- 1;
130 while ((nr
= read(input
, &ch
, 1)) == 1 && ch
!= '\n' && ch
!= '\r') {
132 if ((flags
& RPP_SEVENBIT
))
135 if ((flags
& RPP_FORCELOWER
))
136 ch
= (char)tolower(ch
);
137 if ((flags
& RPP_FORCEUPPER
))
138 ch
= (char)toupper(ch
);
145 if (!(term
.c_lflag
& ECHO
))
146 (void)write(output
, "\n", 1);
149 /* Restore old terminal settings and signals. */
150 if (memcmp(&term
, &oterm
, sizeof(term
)) != 0) {
151 while (tcsetattr(input
, _T_FLUSH
, &oterm
) == -1 &&
155 (void)sigaction(SIGALRM
, &savealrm
, NULL
);
156 (void)sigaction(SIGHUP
, &savehup
, NULL
);
157 (void)sigaction(SIGINT
, &saveint
, NULL
);
158 (void)sigaction(SIGQUIT
, &savequit
, NULL
);
159 (void)sigaction(SIGPIPE
, &savepipe
, NULL
);
160 (void)sigaction(SIGTERM
, &saveterm
, NULL
);
161 (void)sigaction(SIGTSTP
, &savetstp
, NULL
);
162 (void)sigaction(SIGTTIN
, &savettin
, NULL
);
163 (void)sigaction(SIGTTOU
, &savettou
, NULL
);
164 if (input
!= STDIN_FILENO
)
168 * If we were interrupted by a signal, resend it to ourselves
169 * now that we have restored the signal handlers.
171 for (i
= 0; i
< _NSIG
; i
++) {
187 return(nr
== -1 ? NULL
: buf
);
192 getpass(const char *prompt
)
194 static char buf
[_PASSWORD_LEN
+ 1];
196 return(readpassphrase(prompt
, buf
, sizeof(buf
), RPP_ECHO_OFF
));
200 static void handler(int s
)
205 #endif /* HAVE_READPASSPHRASE */