sys/time.h sys/ioctl.h sys/stat.h \
sys/mman.h sys/file.h sys/wait.h \
unistd.h signal.h libgen.h stropts.h \
- syslog.h pwd.h grp.h \
+ syslog.h pwd.h grp.h termios.h \
sys/sockio.h sys/uio.h linux/sockios.h \
linux/types.h poll.h sys/epoll.h err.h \
])
AC_CHECK_FUNCS([ \
daemon chroot getpwnam setuid nice system getpid dup dup2 \
- getpass syslog openlog mlockall getrlimit getgrnam setgid \
+ syslog openlog mlockall getrlimit getgrnam setgid \
setgroups stat flock readv writev time gettimeofday \
ctime memset vsnprintf strdup \
setsid chdir putenv getpeername unlink \
#include "buffer.h"
#include "misc.h"
+#ifdef HAVE_TERMIOS_H
+#include <termios.h>
+#endif
+
#ifdef _WIN32
#include "win32.h"
#endif /* _WIN32 */
-#ifdef HAVE_GETPASS
+#ifdef HAVE_TERMIOS_H
/**
* Open the current console TTY for read/write operations
}
}
-#endif /* HAVE_GETPASS */
+#endif /* HAVE_TERMIOS_H */
/**
#if defined(_WIN32)
return get_console_input_win32(prompt, echo, input, capacity);
-#elif defined(HAVE_GETPASS)
+#elif defined(HAVE_TERMIOS_H)
+ bool restore_tty = false;
+ struct termios tty_tmp, tty_save;
/* did we --daemon'ize before asking for passwords?
* (in which case neither stdin or stderr are connected to a tty and
close(fd);
}
- if (echo)
- {
- FILE *fp;
+ FILE *fp = open_tty(true);
+ fprintf(fp, "%s", prompt);
+ fflush(fp);
+ close_tty(fp);
- fp = open_tty(true);
- fprintf(fp, "%s", prompt);
- fflush(fp);
- close_tty(fp);
+ fp = open_tty(false);
- fp = open_tty(false);
- if (fgets(input, capacity, fp) != NULL)
- {
- chomp(input);
- ret = true;
- }
- close_tty(fp);
+ if (!echo && (tcgetattr(fileno(fp), &tty_tmp) == 0))
+ {
+ tty_save = tty_tmp;
+ tty_tmp.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ISIG);
+ restore_tty = (tcsetattr(fileno(fp), TCSAFLUSH, &tty_tmp) == 0);
}
- else
+
+ if (fgets(input, capacity, fp) != NULL)
+ {
+ chomp(input);
+ ret = true;
+ }
+
+ if (restore_tty)
{
- char *gp = getpass(prompt);
- if (gp)
+ if (tcsetattr(fileno(fp), TCSAFLUSH, &tty_save) == -1)
{
- strncpynt(input, gp, capacity);
- secure_memzero(gp, strlen(gp));
- ret = true;
+ msg(M_WARN | M_ERRNO, "tcsetattr() failed to restore tty settings");
}
+
+ /* Echo the non-echoed newline */
+ close_tty(fp);
+ fp = open_tty(true);
+ fprintf(fp, "\n");
+ fflush(fp);
}
+
+ close_tty(fp);
#else /* if defined(_WIN32) */
msg(M_FATAL, "Sorry, but I can't get console input on this OS (%s)", prompt);
#endif /* if defined(_WIN32) */