use uid_t / gid_t for prototype in stdlib.h, ANSIfy.
[dragonfly.git] / lib / libc / gen / readpassphrase.c
CommitLineData
984263bc
MD
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.
1de703da
MD
28 *
29 * $OpenBSD: readpassphrase.c,v 1.12 2001/12/15 05:41:00 millert Exp $
30 * $FreeBSD: src/lib/libc/gen/readpassphrase.c,v 1.6.2.2 2002/06/30 13:54:26 des Exp $
31 * $DragonFly: src/lib/libc/gen/readpassphrase.c,v 1.2 2003/06/17 04:26:42 dillon Exp $
984263bc
MD
32 */
33
984263bc
MD
34#include <ctype.h>
35#include <errno.h>
36#include <fcntl.h>
37#include <paths.h>
38#include <pwd.h>
39#include <signal.h>
40#include <string.h>
41#include <termios.h>
42#include <unistd.h>
43#include <readpassphrase.h>
44
45static volatile sig_atomic_t signo;
46
47static void handler(int);
48
49char *
50readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
51{
52 ssize_t nr;
53 int input, output, save_errno;
54 char ch, *p, *end;
55 struct termios term, oterm;
56 struct sigaction sa, saveint, savehup, savequit, saveterm;
57 struct sigaction savetstp, savettin, savettou;
58
59 /* I suppose we could alloc on demand in this case (XXX). */
60 if (bufsiz == 0) {
61 errno = EINVAL;
62 return(NULL);
63 }
64
65restart:
66 /*
67 * Read and write to /dev/tty if available. If not, read from
68 * stdin and write to stderr unless a tty is required.
69 */
70 if ((input = output = _open(_PATH_TTY, O_RDWR)) == -1) {
71 if (flags & RPP_REQUIRE_TTY) {
72 errno = ENOTTY;
73 return(NULL);
74 }
75 input = STDIN_FILENO;
76 output = STDERR_FILENO;
77 }
78
79 /*
80 * Catch signals that would otherwise cause the user to end
81 * up with echo turned off in the shell. Don't worry about
82 * things like SIGALRM and SIGPIPE for now.
83 */
84 sigemptyset(&sa.sa_mask);
85 sa.sa_flags = 0; /* don't restart system calls */
86 sa.sa_handler = handler;
87 (void)_sigaction(SIGINT, &sa, &saveint);
88 (void)_sigaction(SIGHUP, &sa, &savehup);
89 (void)_sigaction(SIGQUIT, &sa, &savequit);
90 (void)_sigaction(SIGTERM, &sa, &saveterm);
91 (void)_sigaction(SIGTSTP, &sa, &savetstp);
92 (void)_sigaction(SIGTTIN, &sa, &savettin);
93 (void)_sigaction(SIGTTOU, &sa, &savettou);
94
95 /* Turn off echo if possible. */
96 if (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);
103 } else {
104 memset(&term, 0, sizeof(term));
105 memset(&oterm, 0, sizeof(oterm));
106 }
107
108 (void)_write(output, prompt, strlen(prompt));
109 end = buf + bufsiz - 1;
110 for (p = buf; (nr = _read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) {
111 if (p < end) {
112 if ((flags & RPP_SEVENBIT))
113 ch &= 0x7f;
114 if (isalpha(ch)) {
115 if ((flags & RPP_FORCELOWER))
116 ch = tolower(ch);
117 if ((flags & RPP_FORCEUPPER))
118 ch = toupper(ch);
119 }
120 *p++ = ch;
121 }
122 }
123 *p = '\0';
124 save_errno = errno;
125 if (!(term.c_lflag & ECHO))
126 (void)_write(output, "\n", 1);
127
128 /* Restore old terminal settings and signals. */
129 if (memcmp(&term, &oterm, sizeof(term)) != 0)
130 (void)tcsetattr(input, TCSANOW|TCSASOFT, &oterm);
131 (void)_sigaction(SIGINT, &saveint, NULL);
132 (void)_sigaction(SIGHUP, &savehup, NULL);
133 (void)_sigaction(SIGQUIT, &savequit, NULL);
134 (void)_sigaction(SIGTERM, &saveterm, NULL);
135 (void)_sigaction(SIGTSTP, &savetstp, NULL);
136 (void)_sigaction(SIGTTIN, &savettin, NULL);
137 (void)_sigaction(SIGTTOU, &savettou, NULL);
138 if (input != STDIN_FILENO)
139 (void)_close(input);
140
141 /*
142 * If we were interrupted by a signal, resend it to ourselves
143 * now that we have restored the signal handlers.
144 */
145 if (signo) {
146 kill(getpid(), signo);
147 switch (signo) {
148 case SIGTSTP:
149 case SIGTTIN:
150 case SIGTTOU:
151 signo = 0;
152 goto restart;
153 }
154 }
155
156 errno = save_errno;
157 return(nr == -1 ? NULL : buf);
158}
159
160char *
161getpass(const char *prompt)
162{
163 static char buf[_PASSWORD_LEN + 1];
164
165 if (readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF) == NULL)
166 buf[0] = '\0';
167 return(buf);
168}
169
170static void handler(int s)
171{
172
173 signo = s;
174}