2 * Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include <sys/types.h>
24 #include <sys/param.h>
27 #include <sys/socket.h>
34 #include <arpa/inet.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
39 #include <arpa/nameser.h>
41 #include <arpa/nameser8_compat.h>
55 static int running = 1;
56 static char *topdomain;
57 static char password[33];
58 static struct encoder *b32;
59 static int created_users;
63 static in_addr_t my_ip;
66 static in_addr_t ns_ip;
71 #if !defined(BSD) && !defined(__GLIBC__)
72 static char *__progname;
75 static int read_dns(int, struct query *);
76 static void write_dns(int, struct query *, char *, int);
85 check_user_and_ip(int userid, struct query *q)
87 struct sockaddr_in *tempin;
89 if (userid < 0 || userid >= created_users ) {
92 if (!users[userid].active) {
96 /* return early if IP checking is disabled */
101 tempin = (struct sockaddr_in *) &(q->from);
102 return memcmp(&(users[userid].host), &(tempin->sin_addr), sizeof(struct in_addr));
106 tunnel_tun(int tun_fd, int dns_fd)
108 unsigned long outlen;
115 if ((read = read_tun(tun_fd, in, sizeof(in))) <= 0)
118 /* find target ip in packet, in is padded with 4 bytes TUN header */
119 header = (struct ip*) (in + 4);
120 userid = find_user_by_ip(header->ip_dst.s_addr);
124 outlen = sizeof(out);
125 compress2((uint8_t*)out, &outlen, (uint8_t*)in, read, 9);
127 /* if another packet is queued, throw away this one. TODO build queue */
128 if (users[userid].outpacket.len == 0) {
129 memcpy(users[userid].outpacket.data, out, outlen);
130 users[userid].outpacket.len = outlen;
131 users[userid].outpacket.offset = 0;
132 users[userid].outpacket.sentlen = 0;
133 users[userid].outpacket.seqno = (++users[userid].outpacket.seqno & 7);
134 users[userid].outpacket.fragment = 0;
148 send_version_response(int fd, version_ack_t ack, uint32_t payload, int userid, struct query *q)
154 strncpy(out, "VACK", sizeof(out));
157 strncpy(out, "VNAK", sizeof(out));
160 strncpy(out, "VFUL", sizeof(out));
164 out[4] = ((payload >> 24) & 0xff);
165 out[5] = ((payload >> 16) & 0xff);
166 out[6] = ((payload >> 8) & 0xff);
167 out[7] = ((payload) & 0xff);
168 out[8] = userid & 0xff;
170 write_dns(fd, q, out, sizeof(out));
174 send_chunk(int dns_fd, int userid) {
179 datalen = MIN(users[userid].fragsize, users[userid].outpacket.len - users[userid].outpacket.offset);
181 if (datalen && users[userid].outpacket.sentlen > 0 &&
183 users[userid].outpacket.seqno != users[userid].out_acked_seqno ||
184 users[userid].outpacket.fragment != users[userid].out_acked_fragment
188 /* Still waiting on latest ack, send nothing */
191 /* TODO : count down and discard packet if no acks arrive within X queries */
193 memcpy(&pkt[2], &users[userid].outpacket.data[users[userid].outpacket.offset], datalen);
194 users[userid].outpacket.sentlen = datalen;
195 last = (users[userid].outpacket.len == users[userid].outpacket.offset + users[userid].outpacket.sentlen);
197 /* Increase fragment# when sending data with offset */
198 if (users[userid].outpacket.offset && datalen)
199 users[userid].outpacket.fragment++;
202 /* Build downstream data header (see doc/proto_xxxxxxxx.txt) */
204 /* First byte is 1 bit compression flag, 3 bits upstream seqno, 4 bits upstream fragment */
205 pkt[0] = (1<<7) | ((users[userid].inpacket.seqno & 7) << 4) | (users[userid].inpacket.fragment & 15);
206 /* Second byte is 3 bits downstream seqno, 4 bits downstream fragment, 1 bit last flag */
207 pkt[1] = ((users[userid].outpacket.seqno & 7) << 5) |
208 ((users[userid].outpacket.fragment & 15) << 1) | (last & 1);
211 printf("OUT pkt seq# %d, frag %d (last=%d), offset %d, fragsize %d, total %d, to user %d\n",
212 users[userid].outpacket.seqno & 7, users[userid].outpacket.fragment & 15,
213 last, users[userid].outpacket.offset, datalen, users[userid].outpacket.len, userid);
215 write_dns(dns_fd, &users[userid].q, pkt, datalen + 2);
216 users[userid].q.id = 0;
218 if (users[userid].outpacket.len > 0 &&
219 users[userid].outpacket.len == users[userid].outpacket.sentlen) {
221 /* Whole packet was sent in one chunk, dont wait for ack */
222 users[userid].outpacket.len = 0;
223 users[userid].outpacket.offset = 0;
224 users[userid].outpacket.sentlen = 0;
229 update_downstream_seqno(int dns_fd, int userid, int down_seq, int down_frag)
231 /* If we just read a new packet from tun we have not sent a fragment of, just send it */
232 if (users[userid].outpacket.len > 0 && users[userid].outpacket.sentlen == 0) {
233 send_chunk(dns_fd, userid);
237 /* otherwise, check if we received ack on a fragment and can send next */
238 if (users[userid].outpacket.len > 0 &&
239 users[userid].outpacket.seqno == down_seq && users[userid].outpacket.fragment == down_frag) {
241 if (down_seq != users[userid].out_acked_seqno || down_frag != users[userid].out_acked_fragment) {
242 /* Received ACK on downstream fragment */
243 users[userid].outpacket.offset += users[userid].outpacket.sentlen;
244 users[userid].outpacket.sentlen = 0;
246 /* Is packet done? */
247 if (users[userid].outpacket.offset == users[userid].outpacket.len) {
248 users[userid].outpacket.len = 0;
249 users[userid].outpacket.offset = 0;
250 users[userid].outpacket.sentlen = 0;
253 users[userid].out_acked_seqno = down_seq;
254 users[userid].out_acked_fragment = down_frag;
256 /* Send reply if waiting */
257 if (users[userid].outpacket.len > 0) {
258 send_chunk(dns_fd, userid);
265 handle_null_request(int tun_fd, int dns_fd, struct query *q, int domain_len)
267 struct in_addr tempip;
269 unsigned long outlen;
273 char unpacked[64*1024];
283 memcpy(in, q->name, MIN(domain_len, sizeof(in)));
285 if(in[0] == 'V' || in[0] == 'v') {
286 read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32);
287 /* Version greeting, compare and send ack/nak */
289 /* Received V + 32bits version */
290 version = (((unpacked[0] & 0xff) << 24) |
291 ((unpacked[1] & 0xff) << 16) |
292 ((unpacked[2] & 0xff) << 8) |
293 ((unpacked[3] & 0xff)));
296 if (version == VERSION) {
297 userid = find_available_user();
299 struct sockaddr_in *tempin;
301 users[userid].seed = rand();
302 /* Store remote IP number */
303 tempin = (struct sockaddr_in *) &(q->from);
304 memcpy(&(users[userid].host), &(tempin->sin_addr), sizeof(struct in_addr));
306 memcpy(&(users[userid].q), q, sizeof(struct query));
307 users[userid].encoder = get_base32_encoder();
308 send_version_response(dns_fd, VERSION_ACK, users[userid].seed, userid, q);
309 users[userid].q.id = 0;
311 /* No space for another user */
312 send_version_response(dns_fd, VERSION_FULL, created_users, 0, q);
315 send_version_response(dns_fd, VERSION_NACK, VERSION, 0, q);
318 } else if(in[0] == 'L' || in[0] == 'l') {
319 read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32);
320 /* Login phase, handle auth */
321 userid = unpacked[0];
323 if (check_user_and_ip(userid, q) != 0) {
324 write_dns(dns_fd, q, "BADIP", 5);
327 users[userid].last_pkt = time(NULL);
328 login_calculate(logindata, 16, password, users[userid].seed);
330 if (read >= 18 && (memcmp(logindata, unpacked+1, 16) == 0)) {
331 /* Login ok, send ip/mtu/netmask info */
333 tempip.s_addr = my_ip;
334 tmp[0] = strdup(inet_ntoa(tempip));
335 tempip.s_addr = users[userid].tun_ip;
336 tmp[1] = strdup(inet_ntoa(tempip));
338 read = snprintf(out, sizeof(out), "%s-%s-%d-%d",
339 tmp[0], tmp[1], my_mtu, netmask);
341 write_dns(dns_fd, q, out, read);
347 write_dns(dns_fd, q, "LNAK", 4);
351 } else if(in[0] == 'Z' || in[0] == 'z') {
352 /* Check for case conservation and chars not allowed according to RFC */
354 /* Reply with received hostname as data */
355 write_dns(dns_fd, q, in, domain_len);
357 } else if(in[0] == 'S' || in[0] == 's') {
360 if (domain_len != 4) { /* len = 4, example: "S15." */
361 write_dns(dns_fd, q, "BADLEN", 6);
365 userid = b32_8to5(in[1]);
367 if (check_user_and_ip(userid, q) != 0) {
368 write_dns(dns_fd, q, "BADIP", 5);
369 return; /* illegal id */
372 codec = b32_8to5(in[2]);
375 case 5: /* 5 bits per byte = base32 */
376 enc = get_base32_encoder();
377 user_switch_codec(userid, enc);
378 write_dns(dns_fd, q, enc->name, strlen(enc->name));
380 case 6: /* 6 bits per byte = base64 */
381 enc = get_base64_encoder();
382 user_switch_codec(userid, enc);
383 write_dns(dns_fd, q, enc->name, strlen(enc->name));
386 write_dns(dns_fd, q, "BADCODEC", 8);
390 } else if(in[0] == 'R' || in[0] == 'r') {
393 /* Downstream fragsize probe packet */
394 userid = (b32_8to5(in[1]) >> 1) & 15;
395 if (check_user_and_ip(userid, q) != 0) {
396 write_dns(dns_fd, q, "BADIP", 5);
397 return; /* illegal id */
400 req_frag_size = ((b32_8to5(in[1]) & 1) << 10) | ((b32_8to5(in[2]) & 31) << 5) | (b32_8to5(in[3]) & 31);
401 if (req_frag_size < 2 || req_frag_size > 2047) {
402 write_dns(dns_fd, q, "BADFRAG", 7);
406 memset(buf, 0, sizeof(buf));
407 buf[0] = (req_frag_size >> 8) & 0xff;
408 buf[1] = req_frag_size & 0xff;
409 write_dns(dns_fd, q, buf, req_frag_size);
412 } else if(in[0] == 'N' || in[0] == 'n') {
415 read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32);
416 /* Downstream fragsize packet */
417 userid = unpacked[0];
418 if (check_user_and_ip(userid, q) != 0) {
419 write_dns(dns_fd, q, "BADIP", 5);
420 return; /* illegal id */
423 max_frag_size = ((unpacked[1] & 0xff) << 8) | (unpacked[2] & 0xff);
424 if (max_frag_size < 2) {
425 write_dns(dns_fd, q, "BADFRAG", 7);
427 users[userid].fragsize = max_frag_size;
428 write_dns(dns_fd, q, &unpacked[1], 2);
431 } else if(in[0] == 'P' || in[0] == 'p') {
435 read = unpack_data(unpacked, sizeof(unpacked), &(in[1]), domain_len - 1, b32);
436 /* Ping packet, store userid */
437 userid = unpacked[0];
438 if (check_user_and_ip(userid, q) != 0) {
439 write_dns(dns_fd, q, "BADIP", 5);
440 return; /* illegal id */
444 printf("PING pkt from user %d\n", userid);
447 if (users[userid].q.id != 0) {
448 /* Send reply on earlier query before overwriting */
449 send_chunk(dns_fd, userid);
452 dn_seq = unpacked[1] >> 4;
453 dn_frag = unpacked[1] & 15;
454 memcpy(&(users[userid].q), q, sizeof(struct query));
455 users[userid].last_pkt = time(NULL);
457 /* Update seqno and maybe send immediate response packet */
458 update_downstream_seqno(dns_fd, userid, dn_seq, dn_frag);
459 } else if((in[0] >= '0' && in[0] <= '9')
460 || (in[0] >= 'a' && in[0] <= 'f')
461 || (in[0] >= 'A' && in[0] <= 'F')) {
462 if ((in[0] >= '0' && in[0] <= '9'))
464 if ((in[0] >= 'a' && in[0] <= 'f'))
465 code = in[0] - 'a' + 10;
466 if ((in[0] >= 'A' && in[0] <= 'F'))
467 code = in[0] - 'A' + 10;
470 /* Check user and sending ip number */
471 if (check_user_and_ip(userid, q) != 0) {
472 write_dns(dns_fd, q, "BADIP", 5);
474 /* Decode data header */
475 int up_seq = (b32_8to5(in[1]) >> 2) & 7;
476 int up_frag = ((b32_8to5(in[1]) & 3) << 2) | ((b32_8to5(in[2]) >> 3) & 3);
477 int dn_seq = (b32_8to5(in[2]) & 7);
478 int dn_frag = b32_8to5(in[3]) >> 1;
479 int lastfrag = b32_8to5(in[3]) & 1;
481 if (users[userid].q.id != 0) {
482 /* Send reply on earlier query before overwriting */
483 send_chunk(dns_fd, userid);
486 /* Update query and time info for user */
487 users[userid].last_pkt = time(NULL);
488 memcpy(&(users[userid].q), q, sizeof(struct query));
490 if (up_seq == users[userid].inpacket.seqno &&
491 up_frag <= users[userid].inpacket.fragment) {
492 /* Got repeated old packet, skip it */
494 printf("IN pkt seq# %d, frag %d, dropped duplicate\n",
497 /* Update seqno and maybe send immediate response packet */
498 update_downstream_seqno(dns_fd, userid, dn_seq, dn_frag);
501 if (up_seq != users[userid].inpacket.seqno) {
502 /* New packet has arrived */
503 users[userid].inpacket.seqno = up_seq;
504 users[userid].inpacket.len = 0;
505 users[userid].inpacket.offset = 0;
507 users[userid].inpacket.fragment = up_frag;
509 /* decode with this users encoding */
510 read = unpack_data(unpacked, sizeof(unpacked), &(in[4]), domain_len - 4,
511 users[userid].encoder);
513 /* copy to packet buffer, update length */
514 memcpy(users[userid].inpacket.data + users[userid].inpacket.offset, unpacked, read);
515 users[userid].inpacket.len += read;
516 users[userid].inpacket.offset += read;
519 printf("IN pkt seq# %d, frag %d (last=%d), fragsize %d, total %d, from user %d\n",
520 up_seq, up_frag, lastfrag, read, users[userid].inpacket.len, userid);
523 if (lastfrag & 1) { /* packet is complete */
525 outlen = sizeof(out);
526 ret = uncompress((uint8_t*)out, &outlen,
527 (uint8_t*)users[userid].inpacket.data, users[userid].inpacket.len);
530 hdr = (struct ip*) (out + 4);
531 touser = find_user_by_ip(hdr->ip_dst.s_addr);
534 /* send the uncompressed packet to tun device */
535 write_tun(tun_fd, out, outlen);
537 /* send the compressed packet to other client
538 * if another packet is queued, throw away this one. TODO build queue */
539 if (users[touser].outpacket.len == 0) {
540 memcpy(users[touser].outpacket.data, users[userid].inpacket.data, users[userid].inpacket.len);
541 users[touser].outpacket.len = users[userid].inpacket.len;
545 printf("Discarded data, uncompress() result: %d\n", ret);
547 users[userid].inpacket.len = users[userid].inpacket.offset = 0;
549 /* Update seqno and maybe send immediate response packet */
550 update_downstream_seqno(dns_fd, userid, dn_seq, dn_frag);
556 handle_ns_request(int dns_fd, struct query *q)
561 if (ns_ip != INADDR_ANY) {
562 memcpy(&q->destination.s_addr, &ns_ip, sizeof(in_addr_t));
565 len = dns_encode_ns_response(buf, sizeof(buf), q, topdomain);
568 struct sockaddr_in *tempin;
569 tempin = (struct sockaddr_in *) &(q->from);
570 printf("TX: client %s, type %d, name %s, %d bytes NS reply\n",
571 inet_ntoa(tempin->sin_addr), q->type, q->name, len);
573 if (sendto(dns_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) {
574 warn("ns reply send error");
579 forward_query(int bind_fd, struct query *q)
584 struct sockaddr_in *myaddr;
587 len = dns_encode(buf, sizeof(buf), q, QR_QUERY, q->name, strlen(q->name));
589 /* Store sockaddr for q->id */
590 memcpy(&(fwq.addr), &(q->from), q->fromlen);
591 fwq.addrlen = q->fromlen;
595 newaddr = inet_addr("127.0.0.1");
596 myaddr = (struct sockaddr_in *) &(q->from);
597 memcpy(&(myaddr->sin_addr), &newaddr, sizeof(in_addr_t));
598 myaddr->sin_port = htons(bind_port);
601 printf("TX: NS reply \n");
604 if (sendto(bind_fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen) <= 0) {
605 warn("forward query error");
610 tunnel_bind(int bind_fd, int dns_fd)
612 char packet[64*1024];
613 struct sockaddr_in from;
615 struct fw_query *query;
619 fromlen = sizeof(struct sockaddr);
620 r = recvfrom(bind_fd, packet, sizeof(packet), 0,
621 (struct sockaddr*)&from, &fromlen);
626 id = dns_get_id(packet, r);
629 printf("RX: Got response on query %u from DNS\n", (id & 0xFFFF));
632 /* Get sockaddr from id */
633 fw_query_get(id, &query);
634 if (!query && debug >= 2) {
635 printf("Lost sender of id %u, dropping reply\n", (id & 0xFFFF));
640 struct sockaddr_in *in;
641 in = (struct sockaddr_in *) &(query->addr);
642 printf("TX: client %s id %u, %d bytes\n",
643 inet_ntoa(in->sin_addr), (id & 0xffff), r);
646 if (sendto(dns_fd, packet, r, 0, (const struct sockaddr *) &(query->addr),
647 query->addrlen) <= 0) {
648 warn("forward reply error");
655 tunnel_dns(int tun_fd, int dns_fd, int bind_fd)
661 int inside_topdomain;
663 if ((read = read_dns(dns_fd, &q)) <= 0)
667 struct sockaddr_in *tempin;
668 tempin = (struct sockaddr_in *) &(q.from);
669 printf("RX: client %s, type %d, name %s\n",
670 inet_ntoa(tempin->sin_addr), q.type, q.name);
673 domain = strstr(q.name, topdomain);
674 inside_topdomain = 0;
676 domain_len = (int) (domain - q.name);
677 if (domain_len + strlen(topdomain) == strlen(q.name)) {
678 inside_topdomain = 1;
682 if (inside_topdomain) {
683 /* This is a query we can handle */
686 handle_null_request(tun_fd, dns_fd, &q, domain_len);
689 handle_ns_request(dns_fd, &q);
695 /* Forward query to other port ? */
697 forward_query(bind_fd, &q);
704 tunnel(int tun_fd, int dns_fd, int bind_fd)
712 if (users_waiting_on_reply()) {
722 FD_SET(dns_fd, &fds);
726 /* wait for replies from real DNS */
727 FD_SET(bind_fd, &fds);
728 maxfd = MAX(bind_fd, maxfd);
731 /* TODO : use some kind of packet queue */
732 if(!all_users_waiting_to_send()) {
733 FD_SET(tun_fd, &fds);
734 maxfd = MAX(tun_fd, maxfd);
737 i = select(maxfd + 1, &fds, NULL, NULL, &tv);
747 for (j = 0; j < USERS; j++) {
748 if (users[j].q.id != 0) {
749 send_chunk(dns_fd, j);
753 if(FD_ISSET(tun_fd, &fds)) {
754 tunnel_tun(tun_fd, dns_fd);
757 if(FD_ISSET(dns_fd, &fds)) {
758 tunnel_dns(tun_fd, dns_fd, bind_fd);
761 if(FD_ISSET(bind_fd, &fds)) {
762 tunnel_bind(bind_fd, dns_fd);
772 read_dns(int fd, struct query *q)
774 struct sockaddr_in from;
776 char packet[64*1024];
780 struct cmsghdr *cmsg;
783 addrlen = sizeof(struct sockaddr);
784 iov.iov_base = packet;
785 iov.iov_len = sizeof(packet);
787 msg.msg_name = (caddr_t) &from;
788 msg.msg_namelen = (unsigned) addrlen;
791 msg.msg_control = address;
792 msg.msg_controllen = sizeof(address);
795 r = recvmsg(fd, &msg, 0);
798 dns_decode(NULL, 0, q, QR_QUERY, packet, r);
799 memcpy((struct sockaddr*)&q->from, (struct sockaddr*)&from, addrlen);
800 q->fromlen = addrlen;
802 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
803 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
805 if (cmsg->cmsg_level == IPPROTO_IP &&
806 cmsg->cmsg_type == DSTADDR_SOCKOPT) {
808 q->destination = *dstaddr(cmsg);
813 return strlen(q->name);
823 write_dns(int fd, struct query *q, char *data, int datalen)
828 len = dns_encode(buf, sizeof(buf), q, QR_ANSWER, data, datalen);
831 struct sockaddr_in *tempin;
832 tempin = (struct sockaddr_in *) &(q->from);
833 printf("TX: client %s, type %d, name %s, %d bytes data\n",
834 inet_ntoa(tempin->sin_addr), q->type, q->name, datalen);
837 sendto(fd, buf, len, 0, (struct sockaddr*)&q->from, q->fromlen);
842 extern char *__progname;
844 printf("Usage: %s [-v] [-h] [-c] [-s] [-f] [-D] [-u user] "
845 "[-t chrootdir] [-d device] [-m mtu] "
846 "[-l ip address to listen on] [-p port] [-n external ip] [-b dnsport] [-P password]"
847 " tunnel_ip[/netmask] topdomain\n", __progname);
853 extern char *__progname;
855 printf("iodine IP over DNS tunneling server\n");
856 printf("Usage: %s [-v] [-h] [-c] [-s] [-f] [-D] [-u user] "
857 "[-t chrootdir] [-d device] [-m mtu] "
858 "[-l ip address to listen on] [-p port] [-n external ip] [-b dnsport] [-P password]"
859 " tunnel_ip[/netmask] topdomain\n", __progname);
860 printf(" -v to print version info and exit\n");
861 printf(" -h to print this help and exit\n");
862 printf(" -c to disable check of client IP/port on each request\n");
863 printf(" -s to skip creating and configuring the tun device, "
864 "which then has to be created manually\n");
865 printf(" -f to keep running in foreground\n");
866 printf(" -D to increase debug level\n");
867 printf(" -u name to drop privileges and run as user 'name'\n");
868 printf(" -t dir to chroot to directory dir\n");
869 printf(" -d device to set tunnel device name\n");
870 printf(" -m mtu to set tunnel device mtu\n");
871 printf(" -l ip address to listen on for incoming dns traffic "
872 "(default 0.0.0.0)\n");
873 printf(" -p port to listen on for incoming dns traffic (default 53)\n");
874 printf(" -n ip to respond with to NS queries\n");
875 printf(" -b port to forward normal DNS queries to (on localhost)\n");
876 printf(" -P password used for authentication (max 32 chars will be used)\n");
877 printf("tunnel_ip is the IP number of the local tunnel interface.\n");
878 printf(" /netmask sets the size of the tunnel network.\n");
879 printf("topdomain is the FQDN that is delegated to this server.\n");
885 printf("iodine IP over DNS tunneling server\n");
886 printf("version: 0.5.0 from 2009-01-23\n");
891 main(int argc, char **argv)
902 /* settings for forwarding normal DNS to
903 * local real DNS server */
920 listen_ip = INADDR_ANY;
928 b32 = get_base32_encoder();
930 #if !defined(BSD) && !defined(__GLIBC__)
931 __progname = strrchr(argv[0], '/');
932 if (__progname == NULL)
933 __progname = argv[0];
938 memset(password, 0, sizeof(password));
942 while ((choice = getopt(argc, argv, "vcsfhDu:t:d:m:l:p:n:b:P:")) != -1) {
975 listen_ip = inet_addr(optarg);
981 ns_ip = inet_addr(optarg);
985 bind_port = atoi(optarg);
988 strncpy(password, optarg, sizeof(password));
989 password[sizeof(password)-1] = 0;
991 /* XXX: find better way of cleaning up ps(1) */
992 memset(optarg, 0, strlen(optarg));
1003 if (geteuid() != 0) {
1004 warnx("Run as root and you'll be happy.\n");
1011 netsize = strchr(argv[0], '/');
1015 netmask = atoi(netsize);
1018 my_ip = inet_addr(argv[0]);
1020 if (my_ip == INADDR_NONE) {
1021 warnx("Bad IP address to use inside tunnel.\n");
1025 topdomain = strdup(argv[1]);
1026 if(strlen(topdomain) <= 128) {
1027 if(check_topdomain(topdomain)) {
1028 warnx("Topdomain contains invalid characters.\n");
1032 warnx("Use a topdomain max 128 chars long.\n");
1036 if (username != NULL) {
1037 if ((pw = getpwnam(username)) == NULL) {
1038 warnx("User %s does not exist!\n", username);
1044 warnx("Bad MTU given.\n");
1048 if(port < 1 || port > 65535) {
1049 warnx("Bad port number given.\n");
1054 if (bind_port < 1 || bind_port > 65535 || bind_port == port) {
1055 warnx("Bad DNS server port number given.\n");
1059 printf("Requests for domains outside of %s will be forwarded to port %d\n",
1060 topdomain, bind_port);
1064 printf("ALERT! Other dns servers expect you to run on port 53.\n");
1065 printf("You must manually forward port 53 to port %d for things to work.\n", port);
1069 printf("Debug level %d enabled, will stay in foreground.\n", debug);
1070 printf("Add more -D switches to set higher debug level.\n");
1074 if (listen_ip == INADDR_NONE) {
1075 warnx("Bad IP address to listen on.\n");
1079 if (ns_ip == INADDR_NONE) {
1080 warnx("Bad IP address to return as nameserver.\n");
1083 if (netmask > 30 || netmask < 8) {
1084 warnx("Bad netmask (%d bits). Use 8-30 bits.\n", netmask);
1088 if (strlen(password) == 0)
1089 read_password(password, sizeof(password));
1091 if ((tun_fd = open_tun(device)) == -1)
1094 if (tun_setip(argv[0], netmask) != 0 || tun_setmtu(mtu) != 0)
1096 if ((dnsd_fd = open_dns(port, listen_ip)) == -1)
1099 if ((bind_fd = open_dns(0, INADDR_ANY)) == -1)
1104 created_users = init_users(my_ip, netmask);
1106 if (created_users < USERS) {
1107 printf("Limiting to %d simultaneous users because of netmask /%d\n",
1108 created_users, netmask);
1110 printf("Listening to dns for domain %s\n", topdomain);
1112 if (foreground == 0)
1115 if (newroot != NULL)
1118 signal(SIGINT, sigint);
1119 if (username != NULL) {
1121 gids[0] = pw->pw_gid;
1122 if (setgroups(1, gids) < 0 || setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
1123 warnx("Could not switch to user %s!\n", username);
1128 tunnel(tun_fd, dnsd_fd, bind_fd);