#include "includes.h"
#ifdef REPLACE_GETPASS
#ifdef SYSV_TERMIO
static struct termio t;
#define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
#define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
#define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
#ifndef TCSAFLUSH
#define TCSAFLUSH 1
#endif
#ifndef TCSANOW
#define TCSANOW 0
#endif
static int tcgetattr(int fd, struct termio *t)
{
return ioctl(fd, TCGETA, t);
}
static int tcsetattr(int fd, int flags, struct termio *t)
{
if(flags & TCSAFLUSH)
ioctl(fd, TCFLSH, TCIOFLUSH);
return ioctl(fd, TCSETS, t);
}
#elif !defined(TCSAFLUSH)
static struct sgttyb t;
#define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
#define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
#define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
#define TCSAFLUSH 1
#define TCSANOW 0
static int tcgetattr(int fd, struct sgttyb *t)
{
return ioctl(fd, TIOCGETP, (char *)t);
}
static int tcsetattr(int fd, int flags, struct sgttyb *t)
{
return ioctl(fd, TIOCSETP, (char *)t);
}
#else
#define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
#define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
#define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
static struct termios t;
#endif
static SIG_ATOMIC_T gotintr;
static int in_fd = -1;
static void gotintr_sig(void)
{
gotintr = 1;
if (in_fd != -1)
close(in_fd);
in_fd = -1;
}
char *getsmbpass(const char *prompt)
{
FILE *in, *out;
int echo_off;
static char buf[256];
static size_t bufsize = sizeof(buf);
size_t nread;
CatchSignal(SIGINT, SIGNAL_CAST gotintr_sig);
in = fopen ("/dev/tty", "w+");
if (in == NULL) {
in = stdin;
out = stderr;
} else {
out = in;
}
setvbuf(in, NULL, _IONBF, 0);
if (tcgetattr (fileno (in), &t) == 0) {
if (ECHO_IS_ON(t)) {
TURN_ECHO_OFF(t);
echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
TURN_ECHO_ON(t);
} else {
echo_off = 0;
}
} else {
echo_off = 0;
}
fputs(prompt, out);
fflush(out);
buf[0] = 0;
if (!gotintr) {
in_fd = fileno(in);
fgets(buf, bufsize, in);
}
nread = strlen(buf);
if (buf[nread - 1] == '\n')
buf[nread - 1] = '\0';
if (echo_off) {
if (gotintr && in_fd == -1)
in = fopen ("/dev/tty", "w+");
if (in != NULL)
tcsetattr (fileno (in), TCSANOW, &t);
}
fprintf(out, "\n");
fflush(out);
if (in != stdin)
fclose(in);
CatchSignal(SIGINT, SIGNAL_CAST SIG_DFL);
if (gotintr) {
printf("Interupted by signal.\n");
fflush(stdout);
exit(1);
}
return buf;
}
#else
void getsmbpasswd_dummy(void);
void getsmbpasswd_dummy(void) {;}
#endif