1 /* Copyright (C) 1992-1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
23 #if defined(HAVE_TERMIOS_H)
24 /* POSIX terminal handling. */
26 #elif defined(HAVE_TERMIO_H)
27 /* Older SYSV terminal handling - don't use if we can avoid it. */
29 #elif defined(HAVE_SYS_TERMIO_H)
30 /* Older SYSV terminal handling - don't use if we can avoid it. */
31 #include <sys/termio.h>
34 #ifdef HAVE_SYS_WAIT_H
39 * Define additional missing types
41 #ifndef HAVE_SIG_ATOMIC_T_TYPE
42 typedef int sig_atomic_t;
46 #define SIGCLD SIGCHLD
50 #define SIGNAL_CAST (RETSIGTYPE (*)(int))
53 #ifdef REPLACE_GETPASS
57 /* SYSTEM V TERMIO HANDLING */
59 static struct termio t
;
61 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
62 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
63 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
73 static int tcgetattr(int fd
, struct termio
*_t
)
75 return ioctl(fd
, TCGETA
, _t
);
78 static int tcsetattr(int fd
, int flags
, struct termio
*_t
)
81 ioctl(fd
, TCFLSH
, TCIOFLUSH
);
82 return ioctl(fd
, TCSETS
, _t
);
85 #elif !defined(TCSAFLUSH)
87 /* BSD TERMIO HANDLING */
89 static struct sgttyb t
;
91 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
92 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
93 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
98 static int tcgetattr(int fd
, struct sgttyb
*_t
)
100 return ioctl(fd
, TIOCGETP
, (char *)_t
);
103 static int tcsetattr(int fd
, int flags
, struct sgttyb
*_t
)
105 return ioctl(fd
, TIOCSETP
, (char *)_t
);
108 #else /* POSIX TERMIO HANDLING */
109 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
110 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
111 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
113 static struct termios t
;
114 #endif /* SYSV_TERMIO */
116 static void catch_signal(int signum
,void (*handler
)(int ))
118 #ifdef HAVE_SIGACTION
119 struct sigaction act
;
120 struct sigaction oldact
;
122 memset(&act
, 0, sizeof(act
));
124 act
.sa_handler
= handler
;
127 * We *want* SIGALRM to interrupt a system call.
129 if(signum
!= SIGALRM
)
130 act
.sa_flags
= SA_RESTART
;
132 sigemptyset(&act
.sa_mask
);
133 sigaddset(&act
.sa_mask
,signum
);
134 sigaction(signum
,&act
,&oldact
);
135 return oldact
.sa_handler
;
136 #else /* !HAVE_SIGACTION */
137 /* FIXME: need to handle sigvec and systems with broken signal() */
138 return signal(signum
, handler
);
142 char *getsmbpass(const char *prompt
)
146 static char buf
[256];
147 static size_t bufsize
= sizeof(buf
);
150 /* Catch problematic signals */
151 catch_signal(SIGINT
, SIGNAL_CAST SIG_IGN
);
153 /* Try to write to and read from the terminal if we can.
154 If we can't open the terminal, use stderr and stdin. */
156 in
= fopen ("/dev/tty", "w+");
165 setvbuf(in
, NULL
, _IONBF
, 0);
167 /* Turn echoing off if it is on now. */
169 if (tcgetattr (fileno (in
), &t
) == 0)
174 echo_off
= tcsetattr (fileno (in
), TCSAFLUSH
, &t
) == 0;
183 /* Write the prompt. */
187 /* Read the password. */
189 fgets(buf
, bufsize
, in
);
191 if (buf
[nread
- 1] == '\n')
192 buf
[nread
- 1] = '\0';
194 /* Restore echoing. */
196 (void) tcsetattr (fileno (in
), TCSANOW
, &t
);
199 /* We opened the terminal; now close it. */
202 /* Catch problematic signals */
203 catch_signal(SIGINT
, SIGNAL_CAST SIG_DFL
);
210 void getsmbpasswd_dummy(void);
211 void getsmbpasswd_dummy(void) {;}