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