]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/readpassphrase.c
Libc-825.40.1.tar.gz
[apple/libc.git] / gen / FreeBSD / readpassphrase.c
CommitLineData
9385eb3d
A
1/* $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $ */
2
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)
31static const char rcsid[] = "$OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: src/lib/libc/gen/readpassphrase.c,v 1.6 2002/03/09 03:16:41 green Exp $");
35
ad3c9f2a
A
36#include "xlocale_private.h"
37
9385eb3d
A
38#include "namespace.h"
39#include <ctype.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <paths.h>
43#include <pwd.h>
44#include <signal.h>
45#include <string.h>
46#include <termios.h>
47#include <unistd.h>
48#include <readpassphrase.h>
49#include "un-namespace.h"
50
51static volatile sig_atomic_t signo;
52
53static void handler(int);
54
55char *
56readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
57{
58 ssize_t nr;
59 int input, output, save_errno;
60 char ch, *p, *end;
61 struct termios term, oterm;
62 struct sigaction sa, saveint, savehup, savequit, saveterm;
63 struct sigaction savetstp, savettin, savettou;
ad3c9f2a 64 locale_t loc = __current_locale();
9385eb3d
A
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
72restart:
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 /*
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.
90 */
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);
101
102 /* Turn off echo if possible. */
103 if (tcgetattr(input, &oterm) == 0) {
104 memcpy(&term, &oterm, sizeof(term));
105 if (!(flags & RPP_ECHO_ON))
106 term.c_lflag &= ~(ECHO | ECHONL);
107 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
108 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
109 (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
110 } else {
111 memset(&term, 0, sizeof(term));
112 memset(&oterm, 0, sizeof(oterm));
113 }
114
115 (void)_write(output, prompt, strlen(prompt));
116 end = buf + bufsiz - 1;
117 for (p = buf; (nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
118 if (p < end) {
119 if ((flags & RPP_SEVENBIT))
120 ch &= 0x7f;
ad3c9f2a 121 if (isalpha_l(ch, loc)) {
9385eb3d 122 if ((flags & RPP_FORCELOWER))
ad3c9f2a 123 ch = tolower_l(ch, loc);
9385eb3d 124 if ((flags & RPP_FORCEUPPER))
ad3c9f2a 125 ch = toupper_l(ch, loc);
9385eb3d
A
126 }
127 *p++ = ch;
128 }
129 }
130 *p = '\0';
131 save_errno = errno;
132 if (!(term.c_lflag & ECHO))
133 (void)_write(output, "\n", 1);
134
135 /* Restore old terminal settings and signals. */
136 if (memcmp(&term, &oterm, sizeof(term)) != 0)
137 (void)tcsetattr(input, TCSANOW|TCSASOFT, &oterm);
138 (void)_sigaction(SIGINT, &saveint, NULL);
139 (void)_sigaction(SIGHUP, &savehup, NULL);
140 (void)_sigaction(SIGQUIT, &savequit, NULL);
141 (void)_sigaction(SIGTERM, &saveterm, NULL);
142 (void)_sigaction(SIGTSTP, &savetstp, NULL);
143 (void)_sigaction(SIGTTIN, &savettin, NULL);
144 (void)_sigaction(SIGTTOU, &savettou, NULL);
145 if (input != STDIN_FILENO)
146 (void)_close(input);
147
148 /*
149 * If we were interrupted by a signal, resend it to ourselves
150 * now that we have restored the signal handlers.
151 */
152 if (signo) {
153 kill(getpid(), signo);
154 switch (signo) {
155 case SIGTSTP:
156 case SIGTTIN:
157 case SIGTTOU:
158 signo = 0;
159 goto restart;
160 }
161 }
162
163 errno = save_errno;
164 return(nr == -1 ? NULL : buf);
165}
166
167char *
168getpass(const char *prompt)
169{
170 static char buf[_PASSWORD_LEN + 1];
171
172 if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
173 buf[0] = '\0';
174 return(buf);
175}
176
177static void handler(int s)
178{
179
180 signo = s;
181}