1
0
mirror of https://github.com/yarrick/iodine.git synced 2024-11-30 00:46:05 +03:00

Extract login handling to function

This commit is contained in:
Erik Ekman 2007-12-01 21:55:48 +00:00
parent 212a3b93e6
commit 7f48d0d99f

View File

@ -162,18 +162,63 @@ handle_version(int dns_fd, char *in, int len)
} }
} }
static int
handle_login(int dns_fd, char *in, int len)
{
char data[64*1024];
char logindata[16];
struct in_addr tempip;
char *tmp[2];
struct user dummy;
int read;
int userid;
read = unpack_data(data, sizeof(data), &(in[1]), len - 1, b32);
/* Login phase, handle auth */
userid = data[0];
if (userid < 0 || userid >= USERS) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5);
return -1; /* illegal id */
}
users[userid].last_pkt = time(NULL);
login_calculate(logindata, 16, password, users[userid].seed);
if (dummy.q.fromlen != users[userid].addrlen ||
memcmp(&(users[userid].host), &(dummy.q.from), dummy.q.fromlen) != 0) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5);
} else {
if (read >= 18 && (memcmp(logindata, data+1, 16) == 0)) {
/* Login ok, send ip/mtu info */
tempip.s_addr = my_ip;
tmp[0] = strdup(inet_ntoa(tempip));
tempip.s_addr = users[userid].tun_ip;
tmp[1] = strdup(inet_ntoa(tempip));
read = snprintf(data, sizeof(data), "%s-%s-%d",
tmp[0], tmp[1], my_mtu);
write_dns(dns_fd, &(dummy.q), data, read);
dummy.q.id = 0;
free(tmp[1]);
free(tmp[0]);
} else {
write_dns(dns_fd, &(dummy.q), "LNAK", 4);
}
}
return userid;
}
static int static int
tunnel_dns(int tun_fd, int dns_fd) tunnel_dns(int tun_fd, int dns_fd)
{ {
struct in_addr tempip;
struct user dummy; struct user dummy;
struct ip *hdr; struct ip *hdr;
unsigned long outlen; unsigned long outlen;
char logindata[16];
char out[64*1024]; char out[64*1024];
char in[64*1024]; char in[64*1024];
char unpacked[64*1024]; char unpacked[64*1024];
char *tmp[2];
int userid; int userid;
int touser; int touser;
int read; int read;
@ -186,40 +231,9 @@ tunnel_dns(int tun_fd, int dns_fd)
if(in[0] == 'V' || in[0] == 'v') { if(in[0] == 'V' || in[0] == 'v') {
handle_version(dns_fd, in, read); handle_version(dns_fd, in, read);
} 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); userid = handle_login(dns_fd, in, read);
/* Login phase, handle auth */ if (userid == -1)
userid = unpacked[0]; return 0; /* illegal user id */
if (userid < 0 || userid >= USERS) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5);
return 0; /* illegal id */
}
users[userid].last_pkt = time(NULL);
login_calculate(logindata, 16, password, users[userid].seed);
if (dummy.q.fromlen != users[userid].addrlen ||
memcmp(&(users[userid].host), &(dummy.q.from), dummy.q.fromlen) != 0) {
write_dns(dns_fd, &(dummy.q), "BADIP", 5);
} else {
if (read >= 18 && (memcmp(logindata, unpacked+1, 16) == 0)) {
/* Login ok, send ip/mtu info */
tempip.s_addr = my_ip;
tmp[0] = strdup(inet_ntoa(tempip));
tempip.s_addr = users[userid].tun_ip;
tmp[1] = strdup(inet_ntoa(tempip));
read = snprintf(out, sizeof(out), "%s-%s-%d",
tmp[0], tmp[1], my_mtu);
write_dns(dns_fd, &(dummy.q), out, read);
dummy.q.id = 0;
free(tmp[1]);
free(tmp[0]);
} else {
write_dns(dns_fd, &(dummy.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 */