00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "replace.h"
00022
00023 #if defined(REPLACE_GETPASS_BY_GETPASSPHRASE)
00024
00025 #if defined(HAVE_STDIO_H)
00026 #include <stdio.h>
00027 #endif
00028
00029 char *getsmbpass(const char *prompt)
00030 {
00031 return getpassphrase(prompt);
00032 }
00033
00034 #else
00035
00036 #if defined(HAVE_TERMIOS_H)
00037
00038 #include <termios.h>
00039 #elif defined(HAVE_TERMIO_H)
00040
00041 #include <termio.h>
00042 #elif defined(HAVE_SYS_TERMIO_H)
00043
00044 #include <sys/termio.h>
00045 #endif
00046
00047 #ifdef HAVE_SYS_WAIT_H
00048 #include <sys/wait.h>
00049 #endif
00050
00051
00052
00053
00054 #ifndef HAVE_SIG_ATOMIC_T_TYPE
00055 typedef int sig_atomic_t;
00056 #endif
00057
00058 #ifndef SIGCLD
00059 #define SIGCLD SIGCHLD
00060 #endif
00061
00062 #ifndef SIGNAL_CAST
00063 #define SIGNAL_CAST (RETSIGTYPE (*)(int))
00064 #endif
00065
00066 #ifdef REPLACE_GETPASS
00067
00068 #ifdef SYSV_TERMIO
00069
00070
00071
00072 static struct termio t;
00073
00074 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
00075 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
00076 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
00077
00078 #ifndef TCSAFLUSH
00079 #define TCSAFLUSH 1
00080 #endif
00081
00082 #ifndef TCSANOW
00083 #define TCSANOW 0
00084 #endif
00085
00086 static int tcgetattr(int fd, struct termio *_t)
00087 {
00088 return ioctl(fd, TCGETA, _t);
00089 }
00090
00091 static int tcsetattr(int fd, int flags, struct termio *_t)
00092 {
00093 if(flags & TCSAFLUSH)
00094 ioctl(fd, TCFLSH, TCIOFLUSH);
00095 return ioctl(fd, TCSETS, _t);
00096 }
00097
00098 #elif !defined(TCSAFLUSH)
00099
00100
00101
00102 static struct sgttyb t;
00103
00104 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
00105 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
00106 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
00107
00108 #define TCSAFLUSH 1
00109 #define TCSANOW 0
00110
00111 static int tcgetattr(int fd, struct sgttyb *_t)
00112 {
00113 return ioctl(fd, TIOCGETP, (char *)_t);
00114 }
00115
00116 static int tcsetattr(int fd, int flags, struct sgttyb *_t)
00117 {
00118 return ioctl(fd, TIOCSETP, (char *)_t);
00119 }
00120
00121 #else
00122 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
00123 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
00124 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
00125
00126 static struct termios t;
00127 #endif
00128
00129 static void catch_signal(int signum,void (*handler)(int ))
00130 {
00131 #ifdef HAVE_SIGACTION
00132 struct sigaction act;
00133 struct sigaction oldact;
00134
00135 memset(&act, 0, sizeof(act));
00136
00137 act.sa_handler = handler;
00138 #ifdef SA_RESTART
00139
00140
00141
00142 if(signum != SIGALRM)
00143 act.sa_flags = SA_RESTART;
00144 #endif
00145 sigemptyset(&act.sa_mask);
00146 sigaddset(&act.sa_mask,signum);
00147 sigaction(signum,&act,&oldact);
00148 return oldact.sa_handler;
00149 #else
00150
00151 return signal(signum, handler);
00152 #endif
00153 }
00154
00155 char *getsmbpass(const char *prompt)
00156 {
00157 FILE *in, *out;
00158 int echo_off;
00159 static char buf[256];
00160 static size_t bufsize = sizeof(buf);
00161 size_t nread;
00162
00163
00164 catch_signal(SIGINT, SIGNAL_CAST SIG_IGN);
00165
00166
00167
00168
00169 in = fopen ("/dev/tty", "w+");
00170 if (in == NULL)
00171 {
00172 in = stdin;
00173 out = stderr;
00174 }
00175 else
00176 out = in;
00177
00178 setvbuf(in, NULL, _IONBF, 0);
00179
00180
00181
00182 if (tcgetattr (fileno (in), &t) == 0)
00183 {
00184 if (ECHO_IS_ON(t))
00185 {
00186 TURN_ECHO_OFF(t);
00187 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
00188 TURN_ECHO_ON(t);
00189 }
00190 else
00191 echo_off = 0;
00192 }
00193 else
00194 echo_off = 0;
00195
00196
00197 fputs (prompt, out);
00198 fflush (out);
00199
00200
00201 buf[0] = 0;
00202 fgets(buf, bufsize, in);
00203 nread = strlen(buf);
00204 if (buf[nread - 1] == '\n')
00205 buf[nread - 1] = '\0';
00206
00207
00208 if (echo_off)
00209 (void) tcsetattr (fileno (in), TCSANOW, &t);
00210
00211 if (in != stdin)
00212
00213 fclose (in);
00214
00215
00216 catch_signal(SIGINT, SIGNAL_CAST SIG_DFL);
00217
00218 printf("\n");
00219 return buf;
00220 }
00221
00222 #else
00223 void getsmbpasswd_dummy(void);
00224 void getsmbpasswd_dummy(void) {;}
00225 #endif
00226
00227 #endif