getpass.c revision 1.18 1 1.18 christos /* $NetBSD: getpass.c,v 1.18 2012/04/12 20:08:01 christos Exp $ */
2 1.5 cgd
3 1.17 christos /*-
4 1.17 christos * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.17 christos * All rights reserved.
6 1.17 christos *
7 1.17 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.17 christos * by Christos Zoulas.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd *
19 1.17 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.17 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.17 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.17 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.17 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.17 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.17 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.17 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.17 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.17 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.17 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 cgd */
31 1.10 christos #include <sys/cdefs.h>
32 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
33 1.18 christos __RCSID("$NetBSD: getpass.c,v 1.18 2012/04/12 20:08:01 christos Exp $");
34 1.1 cgd #endif /* LIBC_SCCS and not lint */
35 1.1 cgd
36 1.11 jtc #include "namespace.h"
37 1.5 cgd
38 1.13 lukem #include <assert.h>
39 1.17 christos #ifdef TEST
40 1.17 christos #include <stdio.h>
41 1.17 christos #endif
42 1.17 christos #include <errno.h>
43 1.18 christos #include <signal.h>
44 1.17 christos #include <string.h>
45 1.5 cgd #include <paths.h>
46 1.17 christos #include <stdbool.h>
47 1.17 christos #include <stdlib.h>
48 1.13 lukem #include <termios.h>
49 1.1 cgd #include <unistd.h>
50 1.17 christos #include <fcntl.h>
51 1.11 jtc
52 1.11 jtc #ifdef __weak_alias
53 1.17 christos __weak_alias(getpass_r,_getpass_r)
54 1.14 mycroft __weak_alias(getpass,_getpass)
55 1.11 jtc #endif
56 1.1 cgd
57 1.17 christos /*
58 1.17 christos * Notes:
59 1.17 christos * - There is no getpass_r in POSIX
60 1.17 christos * - Historically EOF is documented to be treated as EOL, we provide a
61 1.17 christos * tunable for that DONT_TREAT_EOF_AS_EOL to disable this.
62 1.17 christos * - Historically getpass ate extra characters silently, we provide
63 1.17 christos * a tunable for that DONT_DISCARD_SILENTLY to disable this.
64 1.17 christos * - Historically getpass "worked" by echoing characters when turning
65 1.17 christos * off echo failed, we provide a tunable DONT_WORK_AND_ECHO to
66 1.17 christos * disable this.
67 1.17 christos * - Some implementations say that on interrupt the program shall
68 1.18 christos * receive an interrupt signal before the function returns. We
69 1.18 christos * send all the tty signals before we return, but we don't expect
70 1.18 christos * suspend to do something useful unless the caller calls us again.
71 1.17 christos */
72 1.1 cgd char *
73 1.17 christos getpass_r(const char *prompt, char *ret, size_t len)
74 1.1 cgd {
75 1.17 christos struct termios gt;
76 1.17 christos char c;
77 1.18 christos int infd, outfd, sig;
78 1.17 christos bool lnext, havetty;
79 1.1 cgd
80 1.13 lukem _DIAGASSERT(prompt != NULL);
81 1.13 lukem
82 1.18 christos sig = 0;
83 1.1 cgd /*
84 1.17 christos * Try to use /dev/tty if possible; otherwise read from stdin and
85 1.17 christos * write to stderr.
86 1.1 cgd */
87 1.17 christos if ((outfd = infd = open(_PATH_TTY, O_RDWR)) == -1) {
88 1.17 christos infd = STDIN_FILENO;
89 1.17 christos outfd = STDERR_FILENO;
90 1.17 christos havetty = false;
91 1.17 christos } else
92 1.17 christos havetty = true;
93 1.17 christos
94 1.17 christos if (tcgetattr(infd, >) == -1) {
95 1.17 christos havetty = false;
96 1.17 christos #ifdef DONT_WORK_AND_ECHO
97 1.17 christos goto out;
98 1.17 christos #else
99 1.17 christos memset(>, -1, sizeof(gt));
100 1.17 christos #endif
101 1.17 christos } else
102 1.17 christos havetty = true;
103 1.17 christos
104 1.17 christos
105 1.17 christos if (havetty) {
106 1.17 christos struct termios st = gt;
107 1.17 christos
108 1.17 christos st.c_lflag &= ~(ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL|ISIG|ICANON);
109 1.17 christos st.c_cc[VMIN] = 1;
110 1.17 christos st.c_cc[VTIME] = 0;
111 1.17 christos if (tcsetattr(infd, TCSAFLUSH|TCSASOFT, &st) == -1)
112 1.17 christos goto out;
113 1.17 christos }
114 1.8 christos
115 1.17 christos if (prompt != NULL) {
116 1.17 christos size_t plen = strlen(prompt);
117 1.17 christos (void)write(outfd, prompt, plen);
118 1.16 christos }
119 1.17 christos
120 1.17 christos c = '\1';
121 1.17 christos lnext = false;
122 1.17 christos for (size_t l = 0; c != '\0'; ) {
123 1.17 christos if (read(infd, &c, 1) != 1)
124 1.17 christos goto restore;
125 1.17 christos
126 1.17 christos #define beep() write(outfd, "\a", 1)
127 1.17 christos #define C(a, b) (gt.c_cc[(a)] == _POSIX_VDISABLE ? (b) : gt.c_cc[(a)])
128 1.17 christos
129 1.17 christos if (lnext) {
130 1.17 christos lnext = false;
131 1.17 christos goto add;
132 1.17 christos }
133 1.17 christos
134 1.17 christos /* Ignored */
135 1.17 christos if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) ||
136 1.17 christos c == C(VSTOP, CTRL('s')) || c == C(VSTATUS, CTRL('t')) ||
137 1.17 christos c == C(VDISCARD, CTRL('o')))
138 1.17 christos continue;
139 1.17 christos
140 1.17 christos /* Literal next */
141 1.17 christos if (c == C(VLNEXT, CTRL('v'))) {
142 1.17 christos lnext = true;
143 1.17 christos continue;
144 1.17 christos }
145 1.17 christos
146 1.17 christos /* Line or word kill, treat as reset */
147 1.17 christos if (c == C(VKILL, CTRL('u')) || c == C(VWERASE, CTRL('w'))) {
148 1.17 christos l = 0;
149 1.17 christos continue;
150 1.17 christos }
151 1.17 christos
152 1.17 christos /* Character erase */
153 1.17 christos if (c == C(VERASE, CTRL('h'))) {
154 1.17 christos if (l == 0)
155 1.17 christos beep();
156 1.17 christos else
157 1.17 christos l--;
158 1.17 christos continue;
159 1.17 christos }
160 1.17 christos
161 1.18 christos /* tty signal characters */
162 1.18 christos if (c == C(VINTR, CTRL('c'))) {
163 1.18 christos sig = SIGINT;
164 1.18 christos goto out;
165 1.18 christos }
166 1.18 christos if (c == C(VQUIT, CTRL('\\'))) {
167 1.18 christos sig = SIGQUIT;
168 1.18 christos goto out;
169 1.18 christos }
170 1.18 christos if (c == C(VSUSP, CTRL('z')) || c == C(VDSUSP, CTRL('y'))) {
171 1.18 christos sig = SIGTSTP;
172 1.18 christos goto out;
173 1.18 christos }
174 1.18 christos
175 1.18 christos /* EOF */
176 1.18 christos if (c == C(VEOF, CTRL('d'))) {
177 1.17 christos #ifdef DONT_TREAT_EOF_AS_EOL
178 1.18 christos errno = ENODATA;
179 1.18 christos goto out;
180 1.18 christos #else
181 1.18 christos c = '\0';
182 1.18 christos goto add;
183 1.17 christos #endif
184 1.17 christos }
185 1.17 christos
186 1.17 christos /* End of line */
187 1.18 christos if (c == C(VEOL, CTRL('j')) || c == C(VEOL2, CTRL('l')))
188 1.17 christos c = '\0';
189 1.17 christos add:
190 1.17 christos if (l >= len) {
191 1.17 christos #ifdef DONT_DISCARD_SILENTLY
192 1.17 christos beep();
193 1.17 christos continue;
194 1.17 christos #else
195 1.17 christos if (c == '\0' && l > 0)
196 1.17 christos l--;
197 1.17 christos else
198 1.17 christos continue;
199 1.17 christos #endif
200 1.17 christos }
201 1.17 christos ret[l++] = c;
202 1.1 cgd }
203 1.17 christos
204 1.17 christos if (havetty)
205 1.17 christos (void)tcsetattr(infd, TCSAFLUSH|TCSASOFT, >);
206 1.17 christos return ret;
207 1.17 christos restore:
208 1.17 christos c = errno;
209 1.17 christos if (havetty)
210 1.17 christos (void)tcsetattr(infd, TCSAFLUSH|TCSASOFT, >);
211 1.17 christos errno = c;
212 1.17 christos out:
213 1.18 christos if (sig) {
214 1.18 christos (void)raise(sig);
215 1.18 christos errno = EINTR;
216 1.18 christos }
217 1.17 christos return NULL;
218 1.17 christos }
219 1.17 christos
220 1.17 christos char *
221 1.17 christos getpass(const char *prompt)
222 1.17 christos {
223 1.17 christos static char e[] = "";
224 1.17 christos static char *buf;
225 1.17 christos static long bufsiz;
226 1.17 christos char *rv;
227 1.17 christos
228 1.17 christos if (buf == NULL) {
229 1.17 christos if ((bufsiz = sysconf(_SC_PASS_MAX)) == -1)
230 1.17 christos return e;
231 1.17 christos if ((buf = malloc((size_t)bufsiz)) == NULL)
232 1.17 christos return e;
233 1.1 cgd }
234 1.17 christos
235 1.17 christos if ((rv = getpass_r(prompt, buf, (size_t)bufsiz)) == NULL)
236 1.17 christos return e;
237 1.17 christos
238 1.17 christos return rv;
239 1.1 cgd }
240 1.17 christos
241 1.17 christos #ifdef TEST
242 1.17 christos int
243 1.17 christos main(int argc, char *argv[])
244 1.17 christos {
245 1.17 christos char buf[28];
246 1.17 christos printf("[%s]\n", getpass_r("foo>", buf, sizeof(buf)));
247 1.17 christos return 0;
248 1.17 christos }
249 1.17 christos #endif
250