From 643178b207bb9d75725b2092a0294ad74ab1782b Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Thu, 12 Jul 2007 15:48:05 +0000 Subject: [PATCH] stdin-echo fix in server too --- src/common.c | 25 +++++++++++++++++++++++++ src/common.h | 2 ++ src/iodine.c | 25 ------------------------- src/iodined.c | 31 +++++++++++++------------------ 4 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/common.c b/src/common.c index 302bb74..73cf24d 100644 --- a/src/common.c +++ b/src/common.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "common.h" @@ -86,3 +87,27 @@ do_detach() umask(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'; +} diff --git a/src/common.h b/src/common.h index 7bd1415..f88d1d5 100644 --- a/src/common.h +++ b/src/common.h @@ -50,4 +50,6 @@ void close_dns(int); void do_chroot(char *); void do_detach(); +void read_password(char*, size_t); + #endif diff --git a/src/iodine.c b/src/iodine.c index ae131e8..2476c97 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -34,7 +34,6 @@ #ifdef DARWIN #include #endif -#include #include "common.h" #include "encoding.h" @@ -570,30 +569,6 @@ set_nameserver(const char *cp) 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 usage() { extern char *__progname; diff --git a/src/iodined.c b/src/iodined.c index 51083db..74e4a27 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -514,7 +514,7 @@ main(int argc, char **argv) argv += optind; if (geteuid() != 0) { - printf("Run as root and you'll be happy.\n"); + warnx("Run as root and you'll be happy.\n"); usage(); } @@ -523,33 +523,29 @@ main(int argc, char **argv) topdomain = strdup(argv[1]); 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(); } - if (username) { - pw = getpwnam(username); - if (!pw) { - printf("User %s does not exist!\n", username); + if (username != NULL) { + if ((pw = getpwnam(username)) == NULL) { + warnx("User %s does not exist!\n", username); usage(); } } if (mtu == 0) { - printf("Bad MTU given.\n"); + warnx("Bad MTU given.\n"); usage(); } if (listen_ip == INADDR_NONE) { - printf("Bad IP address to listen on.\n"); + warnx("Bad IP address to listen on.\n"); usage(); } - if (strlen(password) == 0) { - printf("Enter password on stdin:\n"); - scanf("%32s", password); - password[32] = 0; - } + if (strlen(password) == 0) + read_password(password, sizeof(password)); if ((tun_fd = open_tun(device)) == -1) goto cleanup0; @@ -562,22 +558,21 @@ main(int argc, char **argv) my_mtu = mtu; 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) do_chroot(newroot); signal(SIGINT, sigint); - if (username) { + if (username != NULL) { 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(); } } - if (!foreground) { + if (foreground == 0) do_detach(); - } tunnel(tun_fd, dnsd_fd);