2 * Copyright (c) 2006 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 <netinet/in.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
30 #include <arpa/inet.h>
38 #define MAX(a,b) ((a)>(b)?(a):(b))
49 tunnel(int tun_fd, int dns_fd)
71 i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv);
83 if(FD_ISSET(tun_fd, &fds)) {
84 read = read_tun(tun_fd, in, sizeof(in));
89 compress2(out, &outlen, in, read, 9);
90 dns_handle_tun(dns_fd, out, outlen);
92 if(FD_ISSET(dns_fd, &fds)) {
93 read = dns_read(dns_fd, in, sizeof(in));
98 uncompress(out, &outlen, in, read);
100 write_tun(tun_fd, out, outlen);
112 handshake(int dns_fd)
127 for (i=0; running && i<5 ;i++) {
128 tv.tv_sec = timeout++;
131 dns_handshake(dns_fd);
134 FD_SET(dns_fd, &fds);
136 r = select(dns_fd + 1, &fds, NULL, NULL, &tv);
139 read = dns_read(dns_fd, in, sizeof(in));
147 if (sscanf(in, "%64[^-]-%64[^-]-%d",
148 server, client, &mtu) == 3) {
152 if (tun_setip(client) == 0 &&
153 tun_setmtu(mtu) == 0) {
157 warn("Received handshake with bad data");
160 printf("Received bad handshake\n");
165 printf("Retrying...\n");
173 extern char *__progname;
175 printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] "
176 "nameserver topdomain\n", __progname);
182 extern char *__progname;
184 printf("iodine IP over DNS tunneling client\n");
185 printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] "
186 "nameserver topdomain\n", __progname);
187 printf(" -v to print version info and exit\n");
188 printf(" -h to print this help and exit\n");
189 printf(" -f to keep running in foreground\n");
190 printf(" -u name to drop privileges and run as user 'name'\n");
191 printf(" -t dir to chroot to directory dir\n");
192 printf(" -d device to set tunnel device name\n");
193 printf("nameserver is the IP number of the relaying nameserver\n");
194 printf("topdomain is the FQDN that is delegated to the tunnel endpoint.\n");
200 printf("iodine IP over DNS tunneling client\n");
201 printf("version: 0.3.4 from 2006-11-08\n");
206 main(int argc, char **argv)
222 while ((choice = getopt(argc, argv, "vfhu:t:d:")) != -1) {
248 if (geteuid() != 0) {
249 printf("Run as root and you'll be happy.\n");
260 pw = getpwnam(username);
262 printf("User %s does not exist!\n", username);
267 if ((tun_fd = open_tun(device)) == -1)
269 if ((dns_fd = open_dns(argv[1], 0, INADDR_ANY)) == -1)
271 if (dns_settarget(argv[0]) == -1)
274 printf("Sending queries for %s to %s\n", argv[1], argv[0]);
276 signal(SIGINT, sighandler);
277 signal(SIGTERM, sighandler);
279 if(handshake(dns_fd))
283 if (chroot(newroot) != 0 || chdir("/") != 0)
284 err(1, "%s", newroot);
290 printf("Detaching from terminal...\n");
297 if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
298 printf("Could not switch to user %s!\n", username);
303 tunnel(tun_fd, dns_fd);