getpass.c revision 1.17 1 1.17 christos /* $NetBSD: getpass.c,v 1.17 2012/04/12 19:36:19 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.17 christos __RCSID("$NetBSD: getpass.c,v 1.17 2012/04/12 19:36:19 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.17 christos #include <string.h>
44 1.5 cgd #include <paths.h>
45 1.17 christos #include <stdbool.h>
46 1.17 christos #include <stdlib.h>
47 1.13 lukem #include <termios.h>
48 1.1 cgd #include <unistd.h>
49 1.17 christos #include <fcntl.h>
50 1.11 jtc
51 1.11 jtc #ifdef __weak_alias
52 1.17 christos __weak_alias(getpass_r,_getpass_r)
53 1.14 mycroft __weak_alias(getpass,_getpass)
54 1.11 jtc #endif
55 1.1 cgd
56 1.17 christos /*
57 1.17 christos * Notes:
58 1.17 christos * - There is no getpass_r in POSIX
59 1.17 christos * - Historically EOF is documented to be treated as EOL, we provide a
60 1.17 christos * tunable for that DONT_TREAT_EOF_AS_EOL to disable this.
61 1.17 christos * - Historically getpass ate extra characters silently, we provide
62 1.17 christos * a tunable for that DONT_DISCARD_SILENTLY to disable this.
63 1.17 christos * - Historically getpass "worked" by echoing characters when turning
64 1.17 christos * off echo failed, we provide a tunable DONT_WORK_AND_ECHO to
65 1.17 christos * disable this.
66 1.17 christos * - Some implementations say that on interrupt the program shall
67 1.17 christos * receive an interrupt signal before the function returns. This
68 1.17 christos * does not sound useful, but it could be easy to implement using
69 1.17 christos * raise(3).
70 1.17 christos */
71 1.1 cgd char *
72 1.17 christos getpass_r(const char *prompt, char *ret, size_t len)
73 1.1 cgd {
74 1.17 christos struct termios gt;
75 1.17 christos char c;
76 1.17 christos int infd, outfd;
77 1.17 christos bool lnext, havetty;
78 1.1 cgd
79 1.13 lukem _DIAGASSERT(prompt != NULL);
80 1.13 lukem
81 1.1 cgd /*
82 1.17 christos * Try to use /dev/tty if possible; otherwise read from stdin and
83 1.17 christos * write to stderr.
84 1.1 cgd */
85 1.17 christos if ((outfd = infd = open(_PATH_TTY, O_RDWR)) == -1) {
86 1.17 christos infd = STDIN_FILENO;
87 1.17 christos outfd = STDERR_FILENO;
88 1.17 christos havetty = false;
89 1.17 christos } else
90 1.17 christos havetty = true;
91 1.17 christos
92 1.17 christos if (tcgetattr(infd, >) == -1) {
93 1.17 christos havetty = false;
94 1.17 christos #ifdef DONT_WORK_AND_ECHO
95 1.17 christos goto out;
96 1.17 christos #else
97 1.17 christos memset(>, -1, sizeof(gt));
98 1.17 christos #endif
99 1.17 christos } else
100 1.17 christos havetty = true;
101 1.17 christos
102 1.17 christos
103 1.17 christos if (havetty) {
104 1.17 christos struct termios st = gt;
105 1.17 christos
106 1.17 christos st.c_lflag &= ~(ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL|ISIG|ICANON);
107 1.17 christos st.c_cc[VMIN] = 1;
108 1.17 christos st.c_cc[VTIME] = 0;
109 1.17 christos if (tcsetattr(infd, TCSAFLUSH|TCSASOFT, &st) == -1)
110 1.17 christos goto out;
111 1.17 christos }
112 1.8 christos
113 1.17 christos if (prompt != NULL) {
114 1.17 christos size_t plen = strlen(prompt);
115 1.17 christos (void)write(outfd, prompt, plen);
116 1.16 christos }
117 1.17 christos
118 1.17 christos c = '\1';
119 1.17 christos lnext = false;
120 1.17 christos for (size_t l = 0; c != '\0'; ) {
121 1.17 christos if (read(infd, &c, 1) != 1)
122 1.17 christos goto restore;
123 1.17 christos
124 1.17 christos #define beep() write(outfd, "\a", 1)
125 1.17 christos #define C(a, b) (gt.c_cc[(a)] == _POSIX_VDISABLE ? (b) : gt.c_cc[(a)])
126 1.17 christos
127 1.17 christos if (lnext) {
128 1.17 christos lnext = false;
129 1.17 christos goto add;
130 1.17 christos }
131 1.17 christos
132 1.17 christos /* Ignored */
133 1.17 christos if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) ||
134 1.17 christos c == C(VSTOP, CTRL('s')) || c == C(VSTATUS, CTRL('t')) ||
135 1.17 christos c == C(VDISCARD, CTRL('o')))
136 1.17 christos continue;
137 1.17 christos
138 1.17 christos /* Literal next */
139 1.17 christos if (c == C(VLNEXT, CTRL('v'))) {
140 1.17 christos lnext = true;
141 1.17 christos continue;
142 1.17 christos }
143 1.17 christos
144 1.17 christos /* Line or word kill, treat as reset */
145 1.17 christos if (c == C(VKILL, CTRL('u')) || c == C(VWERASE, CTRL('w'))) {
146 1.17 christos l = 0;
147 1.17 christos continue;
148 1.17 christos }
149 1.17 christos
150 1.17 christos /* Character erase */
151 1.17 christos if (c == C(VERASE, CTRL('h'))) {
152 1.17 christos if (l == 0)
153 1.17 christos beep();
154 1.17 christos else
155 1.17 christos l--;
156 1.17 christos continue;
157 1.17 christos }
158 1.17 christos
159 1.17 christos /* EOF or tty signal characters */
160 1.17 christos if (
161 1.17 christos #ifdef DONT_TREAT_EOF_AS_EOL
162 1.17 christos c == C(VEOF, CTRL('d')) ||
163 1.17 christos #endif
164 1.17 christos c == C(VINTR, CTRL('c')) ||
165 1.17 christos c == C(VQUIT, CTRL('\\')) || c == C(VSUSP, CTRL('z')) ||
166 1.17 christos c == C(VDSUSP, CTRL('y'))) {
167 1.17 christos errno = EINTR;
168 1.17 christos goto out;
169 1.17 christos }
170 1.17 christos
171 1.17 christos /* End of line */
172 1.17 christos if (
173 1.17 christos #ifndef DONT_TREAT_EOF_AS_EOL
174 1.17 christos c == C(VEOF, CTRL('d')) ||
175 1.17 christos #endif
176 1.17 christos c == C(VEOL, CTRL('j')) || c == C(VEOL2, CTRL('l')))
177 1.17 christos c = '\0';
178 1.17 christos add:
179 1.17 christos if (l >= len) {
180 1.17 christos #ifdef DONT_DISCARD_SILENTLY
181 1.17 christos beep();
182 1.17 christos continue;
183 1.17 christos #else
184 1.17 christos if (c == '\0' && l > 0)
185 1.17 christos l--;
186 1.17 christos else
187 1.17 christos continue;
188 1.17 christos #endif
189 1.17 christos }
190 1.17 christos ret[l++] = c;
191 1.1 cgd }
192 1.17 christos
193 1.17 christos if (havetty)
194 1.17 christos (void)tcsetattr(infd, TCSAFLUSH|TCSASOFT, >);
195 1.17 christos return ret;
196 1.17 christos restore:
197 1.17 christos c = errno;
198 1.17 christos if (havetty)
199 1.17 christos (void)tcsetattr(infd, TCSAFLUSH|TCSASOFT, >);
200 1.17 christos errno = c;
201 1.17 christos out:
202 1.17 christos return NULL;
203 1.17 christos }
204 1.17 christos
205 1.17 christos char *
206 1.17 christos getpass(const char *prompt)
207 1.17 christos {
208 1.17 christos static char e[] = "";
209 1.17 christos static char *buf;
210 1.17 christos static long bufsiz;
211 1.17 christos char *rv;
212 1.17 christos
213 1.17 christos if (buf == NULL) {
214 1.17 christos if ((bufsiz = sysconf(_SC_PASS_MAX)) == -1)
215 1.17 christos return e;
216 1.17 christos if ((buf = malloc((size_t)bufsiz)) == NULL)
217 1.17 christos return e;
218 1.1 cgd }
219 1.17 christos
220 1.17 christos if ((rv = getpass_r(prompt, buf, (size_t)bufsiz)) == NULL)
221 1.17 christos return e;
222 1.17 christos
223 1.17 christos return rv;
224 1.1 cgd }
225 1.17 christos
226 1.17 christos #ifdef TEST
227 1.17 christos int
228 1.17 christos main(int argc, char *argv[])
229 1.17 christos {
230 1.17 christos char buf[28];
231 1.17 christos printf("[%s]\n", getpass_r("foo>", buf, sizeof(buf)));
232 1.17 christos return 0;
233 1.17 christos }
234 1.17 christos #endif
235