1
0
mirror of https://github.com/yarrick/iodine.git synced 2024-11-25 14:36:05 +03:00
iodine/src/iodine.c

410 lines
9.7 KiB
C
Raw Normal View History

2006-06-05 16:41:08 +04:00
/*
2014-06-01 10:46:42 +04:00
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
* 2006-2009 Bjorn Andersson <flex@kryo.se>
2006-06-05 16:41:08 +04:00
*
* Permission to use, copy, modify, and/or distribute this software for any
2006-06-05 16:41:08 +04:00
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
2006-06-05 18:43:04 +04:00
#include <string.h>
2006-06-05 18:46:10 +04:00
#include <signal.h>
2006-06-05 19:13:30 +04:00
#include <unistd.h>
#include <sys/types.h>
2008-07-12 16:26:41 +04:00
#include <sys/param.h>
2007-06-17 15:46:05 +04:00
#include <sys/time.h>
2006-06-06 03:34:23 +04:00
#include <fcntl.h>
#include <time.h>
#ifdef WINDOWS32
#include "windows.h"
#include <winsock2.h>
#else
#include <grp.h>
#include <pwd.h>
#include <netdb.h>
#endif
2006-06-05 16:41:08 +04:00
#include "common.h"
#include "tun.h"
#include "client.h"
#include "util.h"
2006-06-05 16:41:08 +04:00
2009-01-25 20:13:12 +03:00
#ifdef WINDOWS32
2009-02-21 13:07:10 +03:00
WORD req_version = MAKEWORD(2, 2);
2009-01-25 20:13:12 +03:00
WSADATA wsa_data;
#endif
2008-07-12 16:26:41 +04:00
#if !defined(BSD) && !defined(__GLIBC__)
static char *__progname;
#endif
2009-09-19 12:09:12 +04:00
#define PASSWORD_ENV_VAR "IODINE_PASS"
2006-06-05 18:46:10 +04:00
static void
2014-06-01 10:34:18 +04:00
sighandler(int sig)
2007-02-04 20:00:20 +03:00
{
client_stop();
}
#if defined(__GNUC__) || defined(__clang__)
/* mark as no return to help some compilers to avoid warnings
* about use of uninitialized variables */
static void usage() __attribute__((noreturn));
#endif
static void
usage() {
2006-08-25 01:23:29 +04:00
extern char *__progname;
2009-08-16 02:02:00 +04:00
fprintf(stderr, "Usage: %s [-v] [-h] [-f] [-r] [-u user] [-t chrootdir] [-d device] "
2009-12-29 23:00:57 +03:00
"[-P password] [-m maxfragsize] [-M maxlen] [-T type] [-O enc] [-L 0|1] [-I sec] "
2009-09-21 01:10:44 +04:00
"[-z context] [-F pidfile] [nameserver] topdomain\n", __progname);
exit(2);
}
2006-06-11 17:14:57 +04:00
static void
help() {
2006-08-25 01:23:29 +04:00
extern char *__progname;
fprintf(stderr, "iodine IP over DNS tunneling client\n");
2009-08-16 02:02:00 +04:00
fprintf(stderr, "Usage: %s [-v] [-h] [-f] [-r] [-u user] [-t chrootdir] [-d device] "
2009-12-29 23:00:57 +03:00
"[-P password] [-m maxfragsize] [-M maxlen] [-T type] [-O enc] [-L 0|1] [-I sec] "
2009-09-21 01:10:44 +04:00
"[-z context] [-F pidfile] [nameserver] topdomain\n", __progname);
2009-12-29 23:00:57 +03:00
fprintf(stderr, "Options to try if connection doesn't work:\n");
fprintf(stderr, " -T force dns type: NULL, PRIVATE, TXT, SRV, MX, CNAME, A (default: autodetect)\n");
2009-12-29 23:00:57 +03:00
fprintf(stderr, " -O force downstream encoding for -T other than NULL: Base32, Base64, Base64u,\n");
fprintf(stderr, " Base128, or (only for TXT:) Raw (default: autodetect)\n");
fprintf(stderr, " -I max interval between requests (default 4 sec) to prevent DNS timeouts\n");
fprintf(stderr, " -L 1: use lazy mode for low-latency (default). 0: don't (implies -I1)\n");
fprintf(stderr, " -m max size of downstream fragments (default: autodetect)\n");
fprintf(stderr, " -M max size of upstream hostnames (~100-255, default: 255)\n");
fprintf(stderr, " -r to skip raw UDP mode attempt\n");
fprintf(stderr, " -P password used for authentication (max 32 chars will be used)\n");
fprintf(stderr, "Other options:\n");
fprintf(stderr, " -v to print version info and exit\n");
fprintf(stderr, " -h to print this help and exit\n");
fprintf(stderr, " -f to keep running in foreground\n");
fprintf(stderr, " -u name to drop privileges and run as user 'name'\n");
fprintf(stderr, " -t dir to chroot to directory dir\n");
fprintf(stderr, " -d device to set tunnel device name\n");
fprintf(stderr, " -z context, to apply specified SELinux context after initialization\n");
fprintf(stderr, " -F pidfile to write pid to a file\n");
fprintf(stderr, "nameserver is the IP number/hostname of the relaying nameserver. if absent, /etc/resolv.conf is used\n");
fprintf(stderr, "topdomain is the FQDN that is delegated to the tunnel endpoint.\n");
2007-02-06 19:01:09 +03:00
2006-06-11 17:14:57 +04:00
exit(0);
}
2006-06-11 17:27:48 +04:00
static void
version() {
2007-02-06 19:01:09 +03:00
fprintf(stderr, "iodine IP over DNS tunneling client\n");
2014-01-29 23:25:45 +04:00
fprintf(stderr, "Git version: %s\n", GITREVISION);
2007-02-06 19:01:09 +03:00
2006-06-11 17:27:48 +04:00
exit(0);
}
2006-06-05 16:41:08 +04:00
int
main(int argc, char **argv)
2006-06-05 16:41:08 +04:00
{
char *nameserv_host;
char *topdomain;
char *errormsg;
#ifndef WINDOWS32
2006-08-25 01:23:29 +04:00
struct passwd *pw;
#endif
char *username;
char password[33];
2006-06-11 17:05:41 +04:00
int foreground;
2006-08-25 01:23:29 +04:00
char *newroot;
char *context;
2006-08-25 01:23:29 +04:00
char *device;
char *pidfile;
2006-08-25 01:23:29 +04:00
int choice;
2006-08-25 02:12:45 +04:00
int tun_fd;
int dns_fd;
2009-02-16 00:12:47 +03:00
int max_downstream_frag_size;
int autodetect_frag_size;
2009-06-24 20:29:30 +04:00
int retval;
2009-08-15 14:45:07 +04:00
int raw_mode;
2009-09-21 01:10:44 +04:00
int lazymode;
int selecttimeout;
2009-12-29 23:00:57 +03:00
int hostname_maxlen;
2013-05-20 21:31:39 +04:00
#ifdef OPENBSD
int rtable = 0;
2013-05-20 21:31:39 +04:00
#endif
struct sockaddr_storage nameservaddr;
int nameservaddr_len;
int nameserv_family;
nameserv_host = NULL;
topdomain = NULL;
errormsg = NULL;
2009-08-15 23:35:07 +04:00
#ifndef WINDOWS32
pw = NULL;
2009-08-15 23:35:07 +04:00
#endif
2007-02-06 19:01:09 +03:00
username = NULL;
2009-08-16 16:33:28 +04:00
memset(password, 0, 33);
2009-09-20 19:11:15 +04:00
srand(time(NULL));
2006-06-11 17:05:41 +04:00
foreground = 0;
2006-08-25 01:23:29 +04:00
newroot = NULL;
context = NULL;
2006-08-25 01:23:29 +04:00
device = NULL;
pidfile = NULL;
2007-06-09 20:18:59 +04:00
autodetect_frag_size = 1;
max_downstream_frag_size = 3072;
2009-06-24 20:29:30 +04:00
retval = 0;
raw_mode = 1;
2009-09-21 01:10:44 +04:00
lazymode = 1;
selecttimeout = 4;
2009-12-29 23:00:57 +03:00
hostname_maxlen = 0xFF;
nameserv_family = AF_UNSPEC;
2009-06-24 20:29:30 +04:00
2009-01-25 20:13:12 +03:00
#ifdef WINDOWS32
WSAStartup(req_version, &wsa_data);
#endif
srand((unsigned) time(NULL));
client_init();
2014-06-01 10:34:18 +04:00
2008-07-12 16:26:41 +04:00
#if !defined(BSD) && !defined(__GLIBC__)
__progname = strrchr(argv[0], '/');
if (__progname == NULL)
__progname = argv[0];
else
__progname++;
#endif
while ((choice = getopt(argc, argv, "46vfhru:t:d:R:P:m:M:F:T:O:L:I:")) != -1) {
switch(choice) {
case '4':
nameserv_family = AF_INET;
break;
case '6':
nameserv_family = AF_INET6;
break;
2006-06-11 17:27:48 +04:00
case 'v':
version();
2008-12-11 22:11:53 +03:00
/* NOTREACHED */
2006-06-11 17:27:48 +04:00
break;
2006-06-11 17:05:41 +04:00
case 'f':
foreground = 1;
break;
2006-06-11 17:14:57 +04:00
case 'h':
help();
2008-12-11 22:11:53 +03:00
/* NOTREACHED */
2006-06-11 17:14:57 +04:00
break;
2009-08-15 14:45:07 +04:00
case 'r':
raw_mode = 0;
break;
case 'u':
username = optarg;
break;
2006-06-11 18:29:36 +04:00
case 't':
newroot = optarg;
break;
2006-06-24 14:28:24 +04:00
case 'd':
device = optarg;
break;
2013-05-20 21:31:39 +04:00
#ifdef OPENBSD
case 'R':
rtable = atoi(optarg);
break;
2013-05-20 21:31:39 +04:00
#endif
2006-11-18 19:08:47 +03:00
case 'P':
strncpy(password, optarg, sizeof(password));
password[sizeof(password)-1] = 0;
2014-06-01 10:34:18 +04:00
/* XXX: find better way of cleaning up ps(1) */
2014-06-01 10:34:18 +04:00
memset(optarg, 0, strlen(optarg));
2006-11-18 19:08:47 +03:00
break;
case 'm':
autodetect_frag_size = 0;
max_downstream_frag_size = atoi(optarg);
break;
2009-12-29 23:00:57 +03:00
case 'M':
hostname_maxlen = atoi(optarg);
if (hostname_maxlen > 255)
hostname_maxlen = 255;
if (hostname_maxlen < 10)
hostname_maxlen = 10;
break;
case 'z':
context = optarg;
break;
case 'F':
pidfile = optarg;
2014-06-01 10:34:18 +04:00
break;
2009-09-20 19:11:15 +04:00
case 'T':
if (client_set_qtype(optarg))
errx(5, "Invalid query type '%s'", optarg);
2009-09-20 19:11:15 +04:00
break;
case 'O': /* not -D, is Debug in server */
client_set_downenc(optarg);
2009-09-20 19:11:15 +04:00
break;
2009-09-21 01:10:44 +04:00
case 'L':
lazymode = atoi(optarg);
if (lazymode > 1)
lazymode = 1;
if (lazymode < 0)
lazymode = 0;
if (!lazymode)
selecttimeout = 1;
break;
case 'I':
selecttimeout = atoi(optarg);
if (selecttimeout < 1)
selecttimeout = 1;
break;
default:
usage();
2007-02-04 20:00:20 +03:00
/* NOTREACHED */
}
}
2014-06-01 10:34:18 +04:00
check_superuser();
argc -= optind;
argv += optind;
switch (argc) {
case 1:
nameserv_host = get_resolvconf_addr();
topdomain = strdup(argv[0]);
break;
case 2:
nameserv_host = argv[0];
topdomain = strdup(argv[1]);
break;
default:
usage();
/* NOTREACHED */
}
if (max_downstream_frag_size < 1 || max_downstream_frag_size > 0xffff) {
warnx("Use a max frag size between 1 and 65535 bytes.\n");
usage();
/* NOTREACHED */
}
if (nameserv_host) {
nameservaddr_len = get_addr(nameserv_host, DNS_PORT, nameserv_family, 0, &nameservaddr);
if (nameservaddr_len < 0) {
errx(1, "Cannot lookup nameserver '%s': %s ",
nameserv_host, gai_strerror(nameservaddr_len));
}
client_set_nameserver(&nameservaddr, nameservaddr_len);
2009-03-28 20:03:42 +03:00
} else {
2009-09-20 19:11:15 +04:00
warnx("No nameserver found - not connected to any network?\n");
2009-03-28 20:03:42 +03:00
usage();
/* NOTREACHED */
2014-06-01 10:34:18 +04:00
}
if(check_topdomain(topdomain, &errormsg)) {
warnx("Invalid topdomain: %s", errormsg);
usage();
2008-12-11 22:11:53 +03:00
/* NOTREACHED */
}
2009-09-21 01:10:44 +04:00
client_set_selecttimeout(selecttimeout);
client_set_lazymode(lazymode);
client_set_topdomain(topdomain);
2009-12-29 23:00:57 +03:00
client_set_hostname_maxlen(hostname_maxlen);
2014-06-01 10:34:18 +04:00
if (username != NULL) {
#ifndef WINDOWS32
if ((pw = getpwnam(username)) == NULL) {
warnx("User %s does not exist!\n", username);
2006-06-11 16:18:49 +04:00
usage();
2008-12-11 22:11:53 +03:00
/* NOTREACHED */
2006-06-11 16:18:49 +04:00
}
#endif
2006-06-11 16:18:49 +04:00
}
2014-06-01 10:34:18 +04:00
2009-09-19 12:09:12 +04:00
if (strlen(password) == 0) {
if (NULL != getenv(PASSWORD_ENV_VAR))
snprintf(password, sizeof(password), "%s", getenv(PASSWORD_ENV_VAR));
else
read_password(password, sizeof(password));
}
2014-06-01 10:34:18 +04:00
client_set_password(password);
2006-06-11 16:18:49 +04:00
2009-06-24 20:29:30 +04:00
if ((tun_fd = open_tun(device)) == -1) {
retval = 1;
goto cleanup1;
2009-06-24 20:29:30 +04:00
}
if ((dns_fd = open_dns_from_host(NULL, 0, nameservaddr.ss_family, AI_PASSIVE)) < 0) {
2009-06-24 20:29:30 +04:00
retval = 1;
2006-06-11 18:42:19 +04:00
goto cleanup2;
2009-06-24 20:29:30 +04:00
}
#ifdef OPENBSD
if (rtable > 0)
socket_setrtable(dns_fd, rtable);
#endif
2006-06-11 23:54:23 +04:00
2006-06-11 22:07:26 +04:00
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
2006-06-11 18:29:36 +04:00
2009-12-29 23:00:57 +03:00
fprintf(stderr, "Sending DNS queries for %s to %s\n",
topdomain, format_addr(&nameservaddr, nameservaddr_len));
2009-12-29 23:00:57 +03:00
if (client_handshake(dns_fd, raw_mode, autodetect_frag_size, max_downstream_frag_size)) {
2009-06-24 20:29:30 +04:00
retval = 1;
2006-06-11 20:13:40 +04:00
goto cleanup2;
2009-06-24 20:29:30 +04:00
}
2014-06-01 10:34:18 +04:00
2009-12-29 23:00:57 +03:00
if (client_get_conn() == CONN_RAW_UDP) {
fprintf(stderr, "Sending raw traffic directly to %s\n", client_get_raw_addr());
}
2006-06-11 20:13:40 +04:00
2009-12-29 23:00:57 +03:00
fprintf(stderr, "Connection setup complete, transmitting data.\n");
2014-06-01 10:34:18 +04:00
if (foreground == 0)
do_detach();
2014-06-01 10:34:18 +04:00
if (pidfile != NULL)
do_pidfile(pidfile);
if (newroot != NULL)
do_chroot(newroot);
2014-06-01 10:34:18 +04:00
if (username != NULL) {
#ifndef WINDOWS32
gid_t gids[1];
gids[0] = pw->pw_gid;
if (setgroups(1, gids) < 0 || setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
warnx("Could not switch to user %s!\n", username);
usage();
2008-12-11 22:11:53 +03:00
/* NOTREACHED */
}
#endif
}
if (context != NULL)
do_setcon(context);
client_tunnel(tun_fd, dns_fd);
2006-06-05 16:41:08 +04:00
2006-06-11 18:42:19 +04:00
cleanup2:
2006-06-05 18:43:04 +04:00
close_dns(dns_fd);
close_tun(tun_fd);
cleanup1:
2006-06-05 16:41:08 +04:00
2009-06-24 20:29:30 +04:00
return retval;
2006-06-05 16:41:08 +04:00
}
2009-06-24 20:29:30 +04:00