Debian Codegraph @

lib/getpass.c

raw
1 /* Copyright (C) 1992-2001, 2003-2007, 2009-2026 Free Software Foundation, Inc.
2
3 This file is part of the GNU C Library.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #ifndef _LIBC
19 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
20 warns for the null checks on 'prompt' below. */
21 # define _GL_ARG_NONNULL(params)
22 # include <config.h>
23 #endif
24
25 #include "getpass.h"
26
27 #include <stdio.h>
28
29 #if !(defined _WIN32 && !defined __CYGWIN__)
30
31 # if HAVE_DECL___FSETLOCKING && HAVE___FSETLOCKING
32 # if HAVE_STDIO_EXT_H
33 # include <stdio_ext.h>
34 # endif
35 # else
36 # define __fsetlocking(stream, type) /* empty */
37 # endif
38
39 # if HAVE_TERMIOS_H
40 # include <termios.h>
41 # endif
42
43 # if USE_UNLOCKED_IO
44 # include "unlocked-io.h"
45 # else
46 # if !HAVE_DECL_FFLUSH_UNLOCKED
47 # undef fflush_unlocked
48 # define fflush_unlocked(x) fflush (x)
49 # endif
50 # if !HAVE_DECL_FLOCKFILE
51 # undef flockfile
52 # define flockfile(x) ((void) 0)
53 # endif
54 # if !HAVE_DECL_FUNLOCKFILE
55 # undef funlockfile
56 # define funlockfile(x) ((void) 0)
57 # endif
58 # if !HAVE_DECL_FPUTS_UNLOCKED
59 # undef fputs_unlocked
60 # define fputs_unlocked(str,stream) fputs (str, stream)
61 # endif
62 # if !HAVE_DECL_PUTC_UNLOCKED
63 # undef putc_unlocked
64 # define putc_unlocked(c,stream) putc (c, stream)
65 # endif
66 # endif
67
68 /* It is desirable to use this bit on systems that have it.
69 The only bit of terminal state we want to twiddle is echoing, which is
70 done in software; there is no need to change the state of the terminal
71 hardware. */
72
73 # ifndef TCSASOFT
74 # define TCSASOFT 0
75 # endif
76
77 static void
78 call_fclose (void *arg)
79 {
80 if (arg != NULL)
81 fclose (arg);
82 }
83
84 char *
85 getpass (const char *prompt)
86 {
87 /* Try to write to and read from the terminal if we can.
88 If we can't open the terminal, use stderr and stdin. */
89
90 FILE *tty = fopen ("/dev/tty", "w+e");
91 FILE *in, *out;
92 if (tty == NULL)
93 {
94 in = stdin;
95 out = stderr;
96 }
97 else
98 {
99 /* We do the locking ourselves. */
100 __fsetlocking (tty, FSETLOCKING_BYCALLER);
101
102 out = in = tty;
103 }
104
105 flockfile (out);
106
107 bool tty_changed = false;
108
109 /* Turn echoing off if it is on now. */
110 # if HAVE_TCGETATTR
111 struct termios s, t;
112 if (tcgetattr (fileno (in), &t) == 0)
113 {
114 /* Save the old one. */
115 s = t;
116 /* Tricky, tricky. */
117 t.c_lflag &= ~(ECHO | ISIG);
118 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &t) == 0);
119 }
120 # endif
121
122 if (prompt)
123 {
124 /* Write the prompt. */
125 fputs_unlocked (prompt, out);
126 fflush_unlocked (out);
127 }
128
129 /* Read the password. */
130 static char *buf;
131 static size_t bufsize;
132 ssize_t nread = getline (&buf, &bufsize, in);
133
134 /* According to the C standard, input may not be followed by output
135 on the same stream without an intervening call to a file
136 positioning function. Suppose in == out; then without this fseek
137 call, on Solaris, HP-UX, AIX, the previous input gets echoed,
138 whereas on IRIX, the following newline is not output as
139 it should be. POSIX imposes similar restrictions if fileno (in)
140 == fileno (out). The POSIX restrictions are tricky and change
141 from POSIX version to POSIX version, so play it safe and invoke
142 fseek even if in != out. */
143 fseeko (out, 0, SEEK_CUR);
144
145 if (buf != NULL)
146 {
147 if (nread < 0)
148 buf[0] = '\0';
149 else if (buf[nread - 1] == '\n')
150 {
151 /* Remove the newline. */
152 buf[nread - 1] = '\0';
153 if (tty_changed)
154 {
155 /* Write the newline that was not echoed. */
156 putc_unlocked ('\n', out);
157 }
158 }
159 }
160
161 /* Restore the original setting. */
162 # if HAVE_TCSETATTR
163 if (tty_changed)
164 tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &s);
165 # endif
166
167 funlockfile (out);
168
169 call_fclose (tty);
170
171 return buf;
172 }
173
174 #else /* W32 native */
175
176 /* Windows implementation by Martin Lambers <[email protected]>,
177 improved by Simon Josefsson. */
178
179 /* For PASS_MAX. */
180 # include <limits.h>
181 /* For _getch(). */
182 # include <conio.h>
183 /* For strdup(). */
184 # include <string.h>
185
186 # ifndef PASS_MAX
187 # define PASS_MAX 512
188 # endif
189
190 char *
191 getpass (const char *prompt)
192 {
193 if (prompt)
194 {
195 fputs (prompt, stderr);
196 fflush (stderr);
197 }
198
199 char getpassbuf[PASS_MAX + 1];
200 {
201 size_t i = 0;
202 for (;;)
203 {
204 int c = _getch ();
205 if (c == '\r')
206 {
207 getpassbuf[i] = '\0';
208 break;
209 }
210 else if (i < PASS_MAX)
211 {
212 getpassbuf[i++] = c;
213 }
214
215 if (i >= PASS_MAX)
216 {
217 getpassbuf[i] = '\0';
218 break;
219 }
220 }
221 }
222
223 if (prompt)
224 {
225 fputs ("\r\n", stderr);
226 fflush (stderr);
227 }
228
229 return strdup (getpassbuf);
230 }
231 #endif