r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[sfrench/samba-autobuild/.git] / source / lib / replace / getpass.c
1 /* Copyright (C) 1992-1998 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 License as
6 published by the Free Software Foundation; either version 3 of the
7 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 Library 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; see the file COPYING.LIB.  If
16 not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
19
20 #include "replace.h"
21
22 #if defined(HAVE_TERMIOS_H)
23 /* POSIX terminal handling. */
24 #include <termios.h>
25 #elif defined(HAVE_TERMIO_H)
26 /* Older SYSV terminal handling - don't use if we can avoid it. */
27 #include <termio.h>
28 #elif defined(HAVE_SYS_TERMIO_H)
29 /* Older SYSV terminal handling - don't use if we can avoid it. */
30 #include <sys/termio.h>
31 #endif
32
33 #ifdef HAVE_SYS_WAIT_H
34 #include <sys/wait.h>
35 #endif
36
37 /*
38  * Define additional missing types
39  */
40 #ifndef HAVE_SIG_ATOMIC_T_TYPE
41 typedef int sig_atomic_t;
42 #endif
43
44 #ifndef SIGCLD
45 #define SIGCLD SIGCHLD
46 #endif
47
48 #ifndef SIGNAL_CAST
49 #define SIGNAL_CAST (RETSIGTYPE (*)(int))
50 #endif
51
52 #ifdef REPLACE_GETPASS
53
54 #ifdef SYSV_TERMIO 
55
56 /* SYSTEM V TERMIO HANDLING */
57
58 static struct termio t;
59
60 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
61 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
62 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
63
64 #ifndef TCSAFLUSH
65 #define TCSAFLUSH 1
66 #endif
67
68 #ifndef TCSANOW
69 #define TCSANOW 0
70 #endif
71
72 static int tcgetattr(int fd, struct termio *_t)
73 {
74         return ioctl(fd, TCGETA, _t);
75 }
76
77 static int tcsetattr(int fd, int flags, struct termio *_t)
78 {
79         if(flags & TCSAFLUSH)
80                 ioctl(fd, TCFLSH, TCIOFLUSH);
81         return ioctl(fd, TCSETS, _t);
82 }
83
84 #elif !defined(TCSAFLUSH)
85
86 /* BSD TERMIO HANDLING */
87
88 static struct sgttyb t;  
89
90 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
91 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
92 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
93
94 #define TCSAFLUSH 1
95 #define TCSANOW 0
96
97 static int tcgetattr(int fd, struct sgttyb *_t)
98 {
99         return ioctl(fd, TIOCGETP, (char *)_t);
100 }
101
102 static int tcsetattr(int fd, int flags, struct sgttyb *_t)
103 {
104         return ioctl(fd, TIOCSETP, (char *)_t);
105 }
106
107 #else /* POSIX TERMIO HANDLING */
108 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
109 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
110 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
111
112 static struct termios t;
113 #endif /* SYSV_TERMIO */
114
115 static void catch_signal(int signum,void (*handler)(int ))
116 {
117 #ifdef HAVE_SIGACTION
118         struct sigaction act;
119         struct sigaction oldact;
120
121         memset(&act, 0, sizeof(act));
122
123         act.sa_handler = handler;
124 #ifdef SA_RESTART
125         /*
126          * We *want* SIGALRM to interrupt a system call.
127          */
128         if(signum != SIGALRM)
129                 act.sa_flags = SA_RESTART;
130 #endif
131         sigemptyset(&act.sa_mask);
132         sigaddset(&act.sa_mask,signum);
133         sigaction(signum,&act,&oldact);
134         return oldact.sa_handler;
135 #else /* !HAVE_SIGACTION */
136         /* FIXME: need to handle sigvec and systems with broken signal() */
137         return signal(signum, handler);
138 #endif
139 }
140
141 char *getsmbpass(const char *prompt)
142 {
143   FILE *in, *out;
144   int echo_off;
145   static char buf[256];
146   static size_t bufsize = sizeof(buf);
147   size_t nread;
148
149   /* Catch problematic signals */
150   catch_signal(SIGINT, SIGNAL_CAST SIG_IGN);
151
152   /* Try to write to and read from the terminal if we can.
153      If we can't open the terminal, use stderr and stdin.  */
154
155   in = fopen ("/dev/tty", "w+");
156   if (in == NULL)
157     {
158       in = stdin;
159       out = stderr;
160     }
161   else
162     out = in;
163
164   setvbuf(in, NULL, _IONBF, 0);
165
166   /* Turn echoing off if it is on now.  */
167
168   if (tcgetattr (fileno (in), &t) == 0)
169     {
170           if (ECHO_IS_ON(t))
171         {
172                 TURN_ECHO_OFF(t);
173                 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
174                 TURN_ECHO_ON(t);
175         }
176       else
177         echo_off = 0;
178     }
179   else
180     echo_off = 0;
181
182   /* Write the prompt.  */
183   fputs (prompt, out);
184   fflush (out);
185
186   /* Read the password.  */
187   buf[0] = 0;
188   fgets(buf, bufsize, in);
189   nread = strlen(buf);
190   if (buf[nread - 1] == '\n')
191     buf[nread - 1] = '\0';
192
193   /* Restore echoing.  */
194   if (echo_off)
195     (void) tcsetattr (fileno (in), TCSANOW, &t);
196
197   if (in != stdin)
198     /* We opened the terminal; now close it.  */
199     fclose (in);
200
201   /* Catch problematic signals */
202   catch_signal(SIGINT, SIGNAL_CAST SIG_DFL);
203
204   printf("\n");
205   return buf;
206 }
207
208 #else
209  void getsmbpasswd_dummy(void);
210  void getsmbpasswd_dummy(void) {;}
211 #endif