stdin-echo fix in server too

This commit is contained in:
Bjorn Andersson 2007-07-12 15:48:05 +00:00
parent e6286cc03c
commit 643178b207
4 changed files with 40 additions and 43 deletions

View File

@ -29,6 +29,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <termios.h>
#include "common.h" #include "common.h"
@ -86,3 +87,27 @@ do_detach()
umask(0); umask(0);
alarm(0); alarm(0);
} }
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';
}

View File

@ -50,4 +50,6 @@ void close_dns(int);
void do_chroot(char *); void do_chroot(char *);
void do_detach(); void do_detach();
void read_password(char*, size_t);
#endif #endif

View File

@ -34,7 +34,6 @@
#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"
@ -570,30 +569,6 @@ set_nameserver(const char *cp)
nameserv.sin_addr = addr; nameserv.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;

View File

@ -514,7 +514,7 @@ main(int argc, char **argv)
argv += optind; argv += optind;
if (geteuid() != 0) { if (geteuid() != 0) {
printf("Run as root and you'll be happy.\n"); warnx("Run as root and you'll be happy.\n");
usage(); usage();
} }
@ -523,33 +523,29 @@ main(int argc, char **argv)
topdomain = strdup(argv[1]); topdomain = strdup(argv[1]);
if (strlen(topdomain) > 128 || topdomain[0] == '.') { if (strlen(topdomain) > 128 || topdomain[0] == '.') {
printf("Use a topdomain max 128 chars long. Do not start it with a dot.\n"); warnx("Use a topdomain max 128 chars long. Do not start it with a dot.\n");
usage(); usage();
} }
if (username) { if (username != NULL) {
pw = getpwnam(username); if ((pw = getpwnam(username)) == NULL) {
if (!pw) { warnx("User %s does not exist!\n", username);
printf("User %s does not exist!\n", username);
usage(); usage();
} }
} }
if (mtu == 0) { if (mtu == 0) {
printf("Bad MTU given.\n"); warnx("Bad MTU given.\n");
usage(); usage();
} }
if (listen_ip == INADDR_NONE) { if (listen_ip == INADDR_NONE) {
printf("Bad IP address to listen on.\n"); warnx("Bad IP address to listen on.\n");
usage(); usage();
} }
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 cleanup0; goto cleanup0;
@ -562,22 +558,21 @@ main(int argc, char **argv)
my_mtu = mtu; my_mtu = mtu;
init_users(my_ip); init_users(my_ip);
printf("Listening to dns for domain %s\n", argv[1]); printf("Listening to dns for domain %s\n", topdomain);
if (newroot != NULL) if (newroot != NULL)
do_chroot(newroot); do_chroot(newroot);
signal(SIGINT, sigint); signal(SIGINT, sigint);
if (username) { if (username != NULL) {
if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
printf("Could not switch to user %s!\n", username); warnx("Could not switch to user %s!\n", username);
usage(); usage();
} }
} }
if (!foreground) { if (foreground == 0)
do_detach(); do_detach();
}
tunnel(tun_fd, dnsd_fd); tunnel(tun_fd, dnsd_fd);