1 /* Copyright (C) 1992-1999,2001,2003,2004,2005 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <unistd.h>
22 #include <malloc.h>
23 
24 #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
25 
26 
27 /* It is desirable to use this bit on systems that have it.
28    The only bit of terminal state we want to twiddle is echoing, which is
29    done in software; there is no need to change the state of the terminal
30    hardware.  */
31 
32 #ifndef TCSASOFT
33 #define TCSASOFT 0
34 #endif
35 #define PWD_BUFFER_SIZE 256
36 
getpass(const char * prompt)37 char * getpass (const char *prompt)
38 {
39   FILE *in, *out;
40   struct termios s, t;
41   int tty_changed;
42   static char *buf = NULL;
43   int nread;
44 
45   if (buf == NULL)
46     buf = (char *)__uc_malloc(PWD_BUFFER_SIZE);
47 
48   /* Try to write to and read from the terminal if we can.
49      If we can't open the terminal, use stderr and stdin.  */
50 
51   out = in = fopen ("/dev/tty", "r+");
52   if (in == NULL)
53     {
54       in = stdin;
55       out = stderr;
56     }
57   else
58     {
59       /* Disable buffering for read/write FILE to prevent problems with
60        * fseek and buffering for read/write auto-transitioning. */
61       setvbuf(in, NULL, _IONBF, 0);
62     }
63 
64   /* Turn echoing off if it is on now.  */
65 
66   tty_changed = 0;
67   if (tcgetattr (fileno (in), &t) == 0)
68     {
69       /* Save the old one. */
70       s = t;
71       /* Tricky, tricky. */
72       t.c_lflag &= ~(ECHO|ISIG);
73       tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
74     }
75 
76   /* Write the prompt.  */
77   fputs(prompt, out);
78   fflush(out);
79 
80   /* Read the password.  */
81   if (!fgets (buf, PWD_BUFFER_SIZE, in))
82     buf[0] = '\0';
83   nread = strlen(buf);
84   if (nread > 0 && buf[nread - 1] == '\n')
85       /* Remove the newline.  */
86       buf[nread - 1] = '\0';
87 
88   if (tty_changed)
89     {
90       /* Write the newline that was not echoed.  */
91       putc('\n', out);
92       /* Restore the original setting.  */
93       (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
94     }
95 
96   if (in != stdin)
97     /* We opened the terminal; now close it.  */
98     fclose (in);
99 
100   return buf;
101 }
102 #endif
103