use termios to not echo password when entered on stdin

This commit is contained in:
Bjorn Andersson 2007-07-12 13:24:55 +00:00
parent 04487efe96
commit 1f9a0273db

View File

@ -34,6 +34,7 @@
#ifdef DARWIN #ifdef DARWIN
#include <arpa/nameser8_compat.h> #include <arpa/nameser8_compat.h>
#endif #endif
#include <termios.h>
#include "common.h" #include "common.h"
#include "encoding.h" #include "encoding.h"
@ -569,6 +570,30 @@ set_nameserver(const char *cp)
peer.sin_addr = addr; peer.sin_addr = addr;
} }
static void
read_password(char *buf, size_t len)
{
struct termios old;
struct termios tp;
char pwd[80];
tcgetattr(0, &tp);
old = tp;
tp.c_lflag &= (~ECHO);
tcsetattr(0, TCSANOW, &tp);
printf("Enter password: ");
fflush(stdout);
scanf("%79s", pwd);
printf("\n");
tcsetattr(0, TCSANOW, &old);
strncpy(buf, pwd, len);
buf[len-1] = '\0';
}
static void static void
usage() { usage() {
extern char *__progname; extern char *__progname;
@ -700,11 +725,8 @@ main(int argc, char **argv)
} }
} }
if (strlen(password) == 0) { if (strlen(password) == 0)
printf("Enter password on stdin:\n"); read_password(password, sizeof(password));
scanf("%32s", password);
password[32] = 0;
}
if ((tun_fd = open_tun(device)) == -1) if ((tun_fd = open_tun(device)) == -1)
goto cleanup1; goto cleanup1;