]>
git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/readpassphrase.c
1 /* $OpenBSD: readpassphrase.c,v 1.24 2013/11/24 23:51:29 deraadt Exp $ */
4 * Copyright (c) 2000-2002, 2007, 2010
5 * Todd C. Miller <Todd.Miller@courtesan.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 #include <sys/cdefs.h>
26 #include "xlocale_private.h"
28 #include "namespace.h"
38 #include <readpassphrase.h>
39 #include "un-namespace.h"
40 #include "libc_private.h"
42 static volatile int signo
[NSIG
];
44 static void handler(int);
47 readpassphrase(const char *prompt
, char *buf
, size_t bufsiz
, int flags
)
50 int input
, output
, save_errno
, i
, need_restart
, input_is_tty
;
52 struct termios term
, oterm
;
53 struct sigaction sa
, savealrm
, saveint
, savehup
, savequit
, saveterm
;
54 struct sigaction savetstp
, savettin
, savettou
, savepipe
;
55 locale_t loc
= __current_locale();
57 /* I suppose we could alloc on demand in this case (XXX). */
64 for (i
= 0; i
< NSIG
; i
++)
70 * Read and write to /dev/tty if available. If not, read from
71 * stdin and write to stderr unless a tty is required.
74 if (!(flags
& RPP_STDIN
)) {
75 input
= output
= _open(_PATH_TTY
, O_RDWR
| O_CLOEXEC
);
77 if (flags
& RPP_REQUIRE_TTY
) {
82 output
= STDERR_FILENO
;
88 output
= STDERR_FILENO
;
92 * Turn off echo if possible.
93 * If we are using a tty but are not the foreground pgrp this will
94 * generate SIGTTOU, so do it *before* installing the signal handlers.
96 if (input_is_tty
&& tcgetattr(input
, &oterm
) == 0) {
97 memcpy(&term
, &oterm
, sizeof(term
));
98 if (!(flags
& RPP_ECHO_ON
))
99 term
.c_lflag
&= ~(ECHO
| ECHONL
);
100 if (term
.c_cc
[VSTATUS
] != _POSIX_VDISABLE
)
101 term
.c_cc
[VSTATUS
] = _POSIX_VDISABLE
;
102 (void)tcsetattr(input
, TCSAFLUSH
|TCSASOFT
, &term
);
104 memset(&term
, 0, sizeof(term
));
105 term
.c_lflag
|= ECHO
;
106 memset(&oterm
, 0, sizeof(oterm
));
107 oterm
.c_lflag
|= ECHO
;
111 * Catch signals that would otherwise cause the user to end
112 * up with echo turned off in the shell. Don't worry about
113 * things like SIGXCPU and SIGVTALRM for now.
115 sigemptyset(&sa
.sa_mask
);
116 sa
.sa_flags
= 0; /* don't restart system calls */
117 sa
.sa_handler
= handler
;
118 (void)_sigaction(SIGALRM
, &sa
, &savealrm
);
119 (void)_sigaction(SIGHUP
, &sa
, &savehup
);
120 (void)_sigaction(SIGINT
, &sa
, &saveint
);
121 (void)_sigaction(SIGPIPE
, &sa
, &savepipe
);
122 (void)_sigaction(SIGQUIT
, &sa
, &savequit
);
123 (void)_sigaction(SIGTERM
, &sa
, &saveterm
);
124 (void)_sigaction(SIGTSTP
, &sa
, &savetstp
);
125 (void)_sigaction(SIGTTIN
, &sa
, &savettin
);
126 (void)_sigaction(SIGTTOU
, &sa
, &savettou
);
128 if (!(flags
& RPP_STDIN
))
129 (void)_write(output
, prompt
, strlen(prompt
));
130 end
= buf
+ bufsiz
- 1;
132 while ((nr
= _read(input
, &ch
, 1)) == 1 && ch
!= '\n' && ch
!= '\r') {
134 if ((flags
& RPP_SEVENBIT
))
136 if (isalpha_l(ch
, loc
)) {
137 if ((flags
& RPP_FORCELOWER
))
138 ch
= (char)tolower_l(ch
, loc
);
139 if ((flags
& RPP_FORCEUPPER
))
140 ch
= (char)toupper_l(ch
, loc
);
147 if (!(term
.c_lflag
& ECHO
))
148 (void)_write(output
, "\n", 1);
150 /* Restore old terminal settings and signals. */
151 if (memcmp(&term
, &oterm
, sizeof(term
)) != 0) {
152 while (tcsetattr(input
, TCSAFLUSH
|TCSASOFT
, &oterm
) == -1 &&
153 errno
== EINTR
&& !signo
[SIGTTOU
])
156 (void)_sigaction(SIGALRM
, &savealrm
, NULL
);
157 (void)_sigaction(SIGHUP
, &savehup
, NULL
);
158 (void)_sigaction(SIGINT
, &saveint
, NULL
);
159 (void)_sigaction(SIGQUIT
, &savequit
, NULL
);
160 (void)_sigaction(SIGPIPE
, &savepipe
, NULL
);
161 (void)_sigaction(SIGTERM
, &saveterm
, NULL
);
162 (void)_sigaction(SIGTSTP
, &savetstp
, NULL
);
163 (void)_sigaction(SIGTTIN
, &savettin
, NULL
);
164 (void)_sigaction(SIGTTOU
, &savettou
, NULL
);
169 * If we were interrupted by a signal, resend it to ourselves
170 * now that we have restored the signal handlers.
172 for (i
= 0; i
< NSIG
; i
++) {
188 return(nr
== -1 ? NULL
: buf
);
192 getpass(const char *prompt
)
194 const size_t bufsiz
= _PASSWORD_LEN
+ 1;
195 static char *buf
= NULL
;
198 buf
= malloc(bufsiz
);
204 if (readpassphrase(prompt
, buf
, bufsiz
, RPP_ECHO_OFF
) == NULL
) {
210 static void handler(int s
)