nrelease - fix/improve livecd
[dragonfly.git] / lib / libc / gen / readpassphrase.c
CommitLineData
984263bc 1/*
f02fcc43 2 * Copyright (c) 2000-2002, 2007, 2010
3 * Todd C. Miller <Todd.Miller@courtesan.com>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
984263bc 8 *
f02fcc43 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
984263bc 16 *
f02fcc43 17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1de703da 20 *
f02fcc43 21 * $OpenBSD: readpassphrase.c,v 1.24 2013/11/24 23:51:29 deraadt Exp $
22 * $FreeBSD: head/lib/libc/gen/readpassphrase.c 294691 2016-01-24 22:20:13Z sobomax $
984263bc
MD
23 */
24
c778e4e0 25#include "namespace.h"
984263bc
MD
26#include <ctype.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <paths.h>
30#include <pwd.h>
31#include <signal.h>
32#include <string.h>
33#include <termios.h>
34#include <unistd.h>
35#include <readpassphrase.h>
c778e4e0 36#include "un-namespace.h"
984263bc 37
f02fcc43 38static volatile sig_atomic_t signo[NSIG];
984263bc
MD
39
40static void handler(int);
41
42char *
43readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
44{
45 ssize_t nr;
f02fcc43 46 int input, output, save_errno, i, need_restart, input_is_tty;
984263bc
MD
47 char ch, *p, *end;
48 struct termios term, oterm;
f02fcc43 49 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
50 struct sigaction savetstp, savettin, savettou, savepipe;
984263bc
MD
51
52 /* I suppose we could alloc on demand in this case (XXX). */
53 if (bufsiz == 0) {
54 errno = EINVAL;
55 return(NULL);
56 }
57
58restart:
f02fcc43 59 for (i = 0; i < NSIG; i++)
60 signo[i] = 0;
61 nr = -1;
62 save_errno = 0;
63 need_restart = 0;
984263bc
MD
64 /*
65 * Read and write to /dev/tty if available. If not, read from
66 * stdin and write to stderr unless a tty is required.
67 */
f02fcc43 68 input_is_tty = 0;
69 if (!(flags & RPP_STDIN)) {
70 input = output = _open(_PATH_TTY, O_RDWR | O_CLOEXEC);
71 if (input == -1) {
72 if (flags & RPP_REQUIRE_TTY) {
73 errno = ENOTTY;
74 return(NULL);
75 }
76 input = STDIN_FILENO;
77 output = STDERR_FILENO;
78 } else {
79 input_is_tty = 1;
984263bc 80 }
f02fcc43 81 } else {
984263bc
MD
82 input = STDIN_FILENO;
83 output = STDERR_FILENO;
84 }
85
f02fcc43 86 /*
87 * Turn off echo if possible.
88 * If we are using a tty but are not the foreground pgrp this will
89 * generate SIGTTOU, so do it *before* installing the signal handlers.
90 */
91 if (input_is_tty && tcgetattr(input, &oterm) == 0) {
92 memcpy(&term, &oterm, sizeof(term));
93 if (!(flags & RPP_ECHO_ON))
94 term.c_lflag &= ~(ECHO | ECHONL);
95 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
96 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
97 tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
98 } else {
99 memset(&term, 0, sizeof(term));
100 term.c_lflag |= ECHO;
101 memset(&oterm, 0, sizeof(oterm));
102 oterm.c_lflag |= ECHO;
103 }
104
984263bc
MD
105 /*
106 * Catch signals that would otherwise cause the user to end
107 * up with echo turned off in the shell. Don't worry about
f02fcc43 108 * things like SIGXCPU and SIGVTALRM for now.
984263bc
MD
109 */
110 sigemptyset(&sa.sa_mask);
111 sa.sa_flags = 0; /* don't restart system calls */
112 sa.sa_handler = handler;
f02fcc43 113 _sigaction(SIGALRM, &sa, &savealrm);
0f52d283 114 _sigaction(SIGHUP, &sa, &savehup);
f02fcc43 115 _sigaction(SIGINT, &sa, &saveint);
116 _sigaction(SIGPIPE, &sa, &savepipe);
0f52d283
SW
117 _sigaction(SIGQUIT, &sa, &savequit);
118 _sigaction(SIGTERM, &sa, &saveterm);
119 _sigaction(SIGTSTP, &sa, &savetstp);
120 _sigaction(SIGTTIN, &sa, &savettin);
121 _sigaction(SIGTTOU, &sa, &savettou);
984263bc 122
f02fcc43 123 if (!(flags & RPP_STDIN))
124 _write(output, prompt, strlen(prompt));
984263bc 125 end = buf + bufsiz - 1;
f02fcc43 126 p = buf;
127 while ((nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
984263bc
MD
128 if (p < end) {
129 if ((flags & RPP_SEVENBIT))
130 ch &= 0x7f;
f02fcc43 131 if (isalpha((unsigned char)ch)) {
984263bc 132 if ((flags & RPP_FORCELOWER))
f02fcc43 133 ch = (char)tolower((unsigned char)ch);
984263bc 134 if ((flags & RPP_FORCEUPPER))
f02fcc43 135 ch = (char)toupper((unsigned char)ch);
984263bc
MD
136 }
137 *p++ = ch;
138 }
139 }
140 *p = '\0';
141 save_errno = errno;
142 if (!(term.c_lflag & ECHO))
0f52d283 143 _write(output, "\n", 1);
984263bc
MD
144
145 /* Restore old terminal settings and signals. */
f02fcc43 146 if (memcmp(&term, &oterm, sizeof(term)) != 0) {
147 while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
148 errno == EINTR && !signo[SIGTTOU])
149 continue;
150 }
151 _sigaction(SIGALRM, &savealrm, NULL);
0f52d283 152 _sigaction(SIGHUP, &savehup, NULL);
f02fcc43 153 _sigaction(SIGINT, &saveint, NULL);
0f52d283 154 _sigaction(SIGQUIT, &savequit, NULL);
f02fcc43 155 _sigaction(SIGPIPE, &savepipe, NULL);
0f52d283
SW
156 _sigaction(SIGTERM, &saveterm, NULL);
157 _sigaction(SIGTSTP, &savetstp, NULL);
158 _sigaction(SIGTTIN, &savettin, NULL);
159 _sigaction(SIGTTOU, &savettou, NULL);
f02fcc43 160 if (input_is_tty)
0f52d283 161 _close(input);
984263bc
MD
162
163 /*
164 * If we were interrupted by a signal, resend it to ourselves
165 * now that we have restored the signal handlers.
166 */
f02fcc43 167 for (i = 0; i < NSIG; i++) {
168 if (signo[i]) {
169 kill(getpid(), i);
170 switch (i) {
171 case SIGTSTP:
172 case SIGTTIN:
173 case SIGTTOU:
174 need_restart = 1;
175 }
984263bc
MD
176 }
177 }
f02fcc43 178 if (need_restart)
179 goto restart;
984263bc 180
f02fcc43 181 if (save_errno)
182 errno = save_errno;
984263bc
MD
183 return(nr == -1 ? NULL : buf);
184}
185
186char *
187getpass(const char *prompt)
188{
189 static char buf[_PASSWORD_LEN + 1];
190
191 if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
192 buf[0] = '\0';
193 return(buf);
194}
195
0f52d283
SW
196static void
197handler(int s)
984263bc 198{
f02fcc43 199 signo[s] = 1;
984263bc 200}