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