Changes to allow handling of queries of type A, NS etc

This commit is contained in:
Erik Ekman 2008-08-05 21:53:19 +00:00
parent 468bd706cb
commit 2ab94a7991
2 changed files with 74 additions and 56 deletions

View File

@ -150,7 +150,6 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
warnx("Got NXDOMAIN as reply"); warnx("Got NXDOMAIN as reply");
break; break;
case SERVFAIL: case SERVFAIL:
warnx("Got SERVFAIL as reply"); warnx("Got SERVFAIL as reply");
break; break;
@ -178,7 +177,7 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
rv = MIN(rlen, sizeof(rdata)); rv = MIN(rlen, sizeof(rdata));
rv = readdata(packet, &data, rdata, rv); rv = readdata(packet, &data, rdata, rv);
if(type == T_NULL && rv > 2) { if(type == T_NULL && rv > 2 && buf) {
rv = MIN(rv, buflen); rv = MIN(rv, buflen);
memcpy(buf, rdata, rv); memcpy(buf, rdata, rv);
} }
@ -194,11 +193,6 @@ dns_decode(char *buf, size_t buflen, struct query *q, qr_t qr, char *packet, siz
readshort(packet, &data, &type); readshort(packet, &data, &type);
readshort(packet, &data, &class); readshort(packet, &data, &class);
if(type != T_NULL) {
rv = 0;
break;
}
strncpy(q->name, name, sizeof(q->name)); strncpy(q->name, name, sizeof(q->name));
q->name[sizeof(q->name) - 1] = '\0'; q->name[sizeof(q->name) - 1] = '\0';
q->type = type; q->type = type;

View File

@ -34,6 +34,10 @@
#include <netinet/in_systm.h> #include <netinet/in_systm.h>
#include <netinet/ip.h> #include <netinet/ip.h>
#include <zlib.h> #include <zlib.h>
#include <arpa/nameser.h>
#ifdef DARWIN
#include <arpa/nameser8_compat.h>
#endif
#include "common.h" #include "common.h"
#include "dns.h" #include "dns.h"
@ -57,7 +61,7 @@ static in_addr_t my_ip;
static char *__progname; static char *__progname;
#endif #endif
static int read_dns(int, struct query *, char *, int); static int read_dns(int, struct query *);
static void write_dns(int, struct query *, char *, int); static void write_dns(int, struct query *, char *, int);
static void static void
@ -134,34 +138,44 @@ send_version_response(int fd, version_ack_t ack, uint32_t payload, struct user *
out[5] = ((payload >> 16) & 0xff); out[5] = ((payload >> 16) & 0xff);
out[6] = ((payload >> 8) & 0xff); out[6] = ((payload >> 8) & 0xff);
out[7] = ((payload) & 0xff); out[7] = ((payload) & 0xff);
out[8] = u->id; if (u) {
out[8] = u->id;
} else {
out[8] = 0;
}
write_dns(fd, &u->q, out, sizeof(out)); write_dns(fd, &u->q, out, sizeof(out));
} }
static int static void
tunnel_dns(int tun_fd, int dns_fd) handle_null_request(int tun_fd, int dns_fd, struct query *q)
{ {
struct in_addr tempip; struct in_addr tempip;
struct user dummy;
struct ip *hdr; struct ip *hdr;
unsigned long outlen; unsigned long outlen;
char in[64*1024];
char logindata[16]; char logindata[16];
char out[64*1024]; char out[64*1024];
char in[64*1024];
char unpacked[64*1024]; char unpacked[64*1024];
char *tmp[2]; char *tmp[2];
char *domain;
int userid; int userid;
int touser; int touser;
int version; int version;
int read;
int code; int code;
int read;
userid = -1; userid = -1;
if ((read = read_dns(dns_fd, &(dummy.q), in, sizeof(in))) <= 0) domain = strstr(q->name, topdomain);
return 0; if (!domain) {
/* Not for us, discard */
return;
}
read = (int) (domain - q->name);
memcpy(in, q->name, MIN(read, sizeof(in)));
if(in[0] == 'V' || in[0] == 'v') { if(in[0] == 'V' || in[0] == 'v') {
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1, b32);
/* Version greeting, compare and send ack/nak */ /* Version greeting, compare and send ack/nak */
@ -180,33 +194,33 @@ tunnel_dns(int tun_fd, int dns_fd)
users[userid].seed = rand(); users[userid].seed = rand();
/* Store remote IP number */ /* Store remote IP number */
tempin = (struct sockaddr_in *) &(dummy.q.from); tempin = (struct sockaddr_in *) &(q->from);
memcpy(&(users[userid].host), &(tempin->sin_addr), sizeof(struct in_addr)); memcpy(&(users[userid].host), &(tempin->sin_addr), sizeof(struct in_addr));
memcpy(&(users[userid].q), &(dummy.q), sizeof(struct query)); memcpy(&(users[userid].q), q, sizeof(struct query));
users[userid].encoder = get_base32_encoder(); users[userid].encoder = get_base32_encoder();
send_version_response(dns_fd, VERSION_ACK, users[userid].seed, &users[userid]); send_version_response(dns_fd, VERSION_ACK, users[userid].seed, &users[userid]);
users[userid].q.id = 0; users[userid].q.id = 0;
} else { } else {
/* No space for another user */ /* No space for another user */
send_version_response(dns_fd, VERSION_FULL, USERS, &dummy); send_version_response(dns_fd, VERSION_FULL, USERS, NULL);
} }
} else { } else {
send_version_response(dns_fd, VERSION_NACK, VERSION, &dummy); send_version_response(dns_fd, VERSION_NACK, VERSION, NULL);
} }
} else if(in[0] == 'L' || in[0] == 'l') { } else if(in[0] == 'L' || in[0] == 'l') {
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1, b32);
/* Login phase, handle auth */ /* Login phase, handle auth */
userid = unpacked[0]; userid = unpacked[0];
if (userid < 0 || userid >= USERS) { if (userid < 0 || userid >= USERS) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5); write_dns(dns_fd, q, "BADIP", 5);
return 0; /* illegal id */ return; /* illegal id */
} }
users[userid].last_pkt = time(NULL); users[userid].last_pkt = time(NULL);
login_calculate(logindata, 16, password, users[userid].seed); login_calculate(logindata, 16, password, users[userid].seed);
if (check_ip && ip_cmp(userid, &(dummy.q)) != 0) { if (check_ip && ip_cmp(userid, q) != 0) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5); write_dns(dns_fd, q, "BADIP", 5);
} else { } else {
if (read >= 18 && (memcmp(logindata, unpacked+1, 16) == 0)) { if (read >= 18 && (memcmp(logindata, unpacked+1, 16) == 0)) {
/* Login ok, send ip/mtu info */ /* Login ok, send ip/mtu info */
@ -219,31 +233,31 @@ tunnel_dns(int tun_fd, int dns_fd)
read = snprintf(out, sizeof(out), "%s-%s-%d", read = snprintf(out, sizeof(out), "%s-%s-%d",
tmp[0], tmp[1], my_mtu); tmp[0], tmp[1], my_mtu);
write_dns(dns_fd, &(dummy.q), out, read); write_dns(dns_fd, q, out, read);
dummy.q.id = 0; q->id = 0;
free(tmp[1]); free(tmp[1]);
free(tmp[0]); free(tmp[0]);
} else { } else {
write_dns(dns_fd, &(dummy.q), "LNAK", 4); write_dns(dns_fd, q, "LNAK", 4);
} }
} }
} else if(in[0] == 'P' || in[0] == 'p') { } else if(in[0] == 'P' || in[0] == 'p') {
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1, b32); read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1, b32);
/* Ping packet, store userid */ /* Ping packet, store userid */
userid = unpacked[0]; userid = unpacked[0];
if (userid < 0 || userid >= USERS || ip_cmp(userid, &(dummy.q)) != 0) { if (userid < 0 || userid >= USERS || ip_cmp(userid, q) != 0) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5); write_dns(dns_fd, q, "BADIP", 5);
return 0; /* illegal id */ return; /* illegal id */
} }
memcpy(&(users[userid].q), &(dummy.q), sizeof(struct query)); memcpy(&(users[userid].q), q, sizeof(struct query));
users[userid].last_pkt = time(NULL); users[userid].last_pkt = time(NULL);
} else if(in[0] == 'Z' || in[0] == 'z') { } else if(in[0] == 'Z' || in[0] == 'z') {
/* Case conservation check */ /* Case conservation check */
/* Reply with received hostname as data */ /* Reply with received hostname as data */
write_dns(dns_fd, &(dummy.q), in, read); write_dns(dns_fd, q, in, read);
return 0; return;
} else if((in[0] >= '0' && in[0] <= '9') } else if((in[0] >= '0' && in[0] <= '9')
|| (in[0] >= 'a' && in[0] <= 'f') || (in[0] >= 'a' && in[0] <= 'f')
|| (in[0] >= 'A' && in[0] <= 'F')) { || (in[0] >= 'A' && in[0] <= 'F')) {
@ -256,20 +270,20 @@ tunnel_dns(int tun_fd, int dns_fd)
userid = code >> 1; userid = code >> 1;
if (userid < 0 || userid >= USERS) { if (userid < 0 || userid >= USERS) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5); write_dns(dns_fd, q, "BADIP", 5);
return 0; /* illegal id */ return; /* illegal id */
} }
/* Check sending ip number */ /* Check sending ip number */
if (check_ip && ip_cmp(userid, &(dummy.q)) != 0) { if (check_ip && ip_cmp(userid, q) != 0) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5); write_dns(dns_fd, q, "BADIP", 5);
} else { } else {
/* decode with this users encoding */ /* decode with this users encoding */
read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1, read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), read - 1,
users[userid].encoder); users[userid].encoder);
users[userid].last_pkt = time(NULL); users[userid].last_pkt = time(NULL);
memcpy(&(users[userid].q), &(dummy.q), sizeof(struct query)); memcpy(&(users[userid].q), q, sizeof(struct query));
memcpy(users[userid].inpacket.data + users[userid].inpacket.offset, unpacked, read); memcpy(users[userid].inpacket.data + users[userid].inpacket.offset, unpacked, read);
users[userid].inpacket.len += read; users[userid].inpacket.len += read;
users[userid].inpacket.offset += read; users[userid].inpacket.offset += read;
@ -298,11 +312,29 @@ tunnel_dns(int tun_fd, int dns_fd)
} }
} }
/* userid must be set for a reply to be sent */ /* userid must be set for a reply to be sent */
if (userid >= 0 && userid < USERS && ip_cmp(userid, &(dummy.q)) == 0 && users[userid].outpacket.len > 0) { if (userid >= 0 && userid < USERS && ip_cmp(userid, q) == 0 && users[userid].outpacket.len > 0) {
write_dns(dns_fd, &(dummy.q), users[userid].outpacket.data, users[userid].outpacket.len); write_dns(dns_fd, q, users[userid].outpacket.data, users[userid].outpacket.len);
users[userid].outpacket.len = 0; users[userid].outpacket.len = 0;
users[userid].q.id = 0; users[userid].q.id = 0;
} }
}
static int
tunnel_dns(int tun_fd, int dns_fd)
{
struct query q;
int read;
if ((read = read_dns(dns_fd, &q)) <= 0)
return 0;
switch (q.type) {
case T_NULL:
handle_null_request(tun_fd, dns_fd, &q);
break;
default:
break;
}
return 0; return 0;
} }
@ -363,36 +395,28 @@ tunnel(int tun_fd, int dns_fd)
} }
static int static int
read_dns(int fd, struct query *q, char *buf, int buflen) read_dns(int fd, struct query *q)
{ {
struct sockaddr_in from; struct sockaddr_in from;
char packet[64*1024]; char packet[64*1024];
char *domain;
socklen_t addrlen; socklen_t addrlen;
int rv;
int r; int r;
addrlen = sizeof(struct sockaddr); addrlen = sizeof(struct sockaddr);
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen); r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
if (r > 0) { if (r > 0) {
dns_decode(buf, buflen, q, QR_QUERY, packet, r); dns_decode(NULL, 0, q, QR_QUERY, packet, r);
domain = strstr(q->name, topdomain); memcpy((struct sockaddr*)&q->from, (struct sockaddr*)&from, addrlen);
if (domain) { q->fromlen = addrlen;
rv = (int) (domain - q->name);
memcpy(buf, q->name, MIN(rv, buflen)); return strlen(q->name);
q->fromlen = addrlen;
memcpy((struct sockaddr*)&q->from, (struct sockaddr*)&from, addrlen);
} else {
rv = 0;
}
} else if (r < 0) { } else if (r < 0) {
/* Error */ /* Error */
warn("read dns"); warn("read dns");
rv = 0;
} }
return rv; return 0;
} }
static void static void