2007-05-18 16:52:34 +00:00
|
|
|
/*
|
|
|
|
tincctl.c -- Controlling a running tincd
|
2011-05-15 11:16:48 +00:00
|
|
|
Copyright (C) 2007-2011 Guus Sliepen <guus@tinc-vpn.org>
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2009-09-29 13:33:58 +00:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-05-18 16:52:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include "xalloc.h"
|
2007-11-07 02:48:33 +00:00
|
|
|
#include "protocol.h"
|
|
|
|
#include "control_common.h"
|
2008-12-14 12:47:26 +00:00
|
|
|
#include "rsagen.h"
|
2009-11-05 22:29:28 +00:00
|
|
|
#include "utils.h"
|
2011-05-15 11:16:48 +00:00
|
|
|
#include "tincctl.h"
|
|
|
|
#include "top.h"
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
/* The name this program was run with. */
|
2011-05-28 01:56:06 +00:00
|
|
|
static char *program_name = NULL;
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
/* If nonzero, display usage information and exit. */
|
2011-05-28 01:56:06 +00:00
|
|
|
static bool show_help = false;
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
/* If nonzero, print the version on standard output and exit. */
|
2011-05-28 01:56:06 +00:00
|
|
|
static bool show_version = false;
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
static char *name = NULL;
|
2007-05-19 14:55:35 +00:00
|
|
|
static char *identname = NULL; /* program name for syslog */
|
2009-11-07 22:43:25 +00:00
|
|
|
static char *controlcookiename = NULL; /* cookie file location */
|
|
|
|
static char controlcookie[1024];
|
2007-05-18 16:52:34 +00:00
|
|
|
char *netname = NULL;
|
2007-05-19 14:55:35 +00:00
|
|
|
char *confbase = NULL;
|
2011-05-29 12:41:05 +00:00
|
|
|
static char *host = NULL;
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2009-11-05 22:29:28 +00:00
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
static struct WSAData wsa_state;
|
|
|
|
#endif
|
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
static struct option const long_options[] = {
|
|
|
|
{"config", required_argument, NULL, 'c'},
|
2011-05-29 12:41:05 +00:00
|
|
|
{"host", required_argument, NULL, 'h'},
|
2007-05-18 16:52:34 +00:00
|
|
|
{"net", required_argument, NULL, 'n'},
|
|
|
|
{"help", no_argument, NULL, 1},
|
|
|
|
{"version", no_argument, NULL, 2},
|
2009-12-11 21:38:06 +00:00
|
|
|
{"controlcookie", required_argument, NULL, 5},
|
2007-05-18 16:52:34 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void usage(bool status) {
|
|
|
|
if(status)
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Try `%s --help\' for more information.\n",
|
2007-05-18 16:52:34 +00:00
|
|
|
program_name);
|
|
|
|
else {
|
2009-09-29 13:19:55 +00:00
|
|
|
printf("Usage: %s [options] command\n\n", program_name);
|
|
|
|
printf("Valid options are:\n"
|
2007-05-19 14:13:21 +00:00
|
|
|
" -c, --config=DIR Read configuration options from DIR.\n"
|
|
|
|
" -n, --net=NETNAME Connect to net NETNAME.\n"
|
2009-11-07 22:43:25 +00:00
|
|
|
" --controlcookie=FILENAME Read control socket from FILENAME.\n"
|
2007-05-19 14:13:21 +00:00
|
|
|
" --help Display this help and exit.\n"
|
|
|
|
" --version Output version information and exit.\n"
|
|
|
|
"\n"
|
2007-05-18 16:52:34 +00:00
|
|
|
"Valid commands are:\n"
|
|
|
|
" start Start tincd.\n"
|
|
|
|
" stop Stop tincd.\n"
|
|
|
|
" restart Restart tincd.\n"
|
|
|
|
" reload Reload configuration of running tincd.\n"
|
2007-05-19 15:21:26 +00:00
|
|
|
" pid Show PID of currently running tincd.\n"
|
2007-05-19 14:55:35 +00:00
|
|
|
" generate-keys [bits] Generate a new public/private keypair.\n"
|
2007-05-18 16:52:34 +00:00
|
|
|
" dump Dump a list of one of the following things:\n"
|
|
|
|
" nodes - all known nodes in the VPN\n"
|
|
|
|
" edges - all known connections in the VPN\n"
|
|
|
|
" subnets - all known subnets in the VPN\n"
|
|
|
|
" connections - all meta connections with ourself\n"
|
|
|
|
" graph - graph of the VPN in dotty format\n"
|
2007-11-07 02:49:57 +00:00
|
|
|
" purge Purge unreachable nodes\n"
|
2007-11-07 02:50:27 +00:00
|
|
|
" debug N Set debug level\n"
|
2007-11-07 02:50:58 +00:00
|
|
|
" retry Retry all outgoing connections\n"
|
2007-11-07 02:51:24 +00:00
|
|
|
" reload Partial reload of configuration\n"
|
2009-12-16 20:16:56 +00:00
|
|
|
" disconnect NODE Close meta connection with NODE\n"
|
2011-05-15 11:16:48 +00:00
|
|
|
#ifdef HAVE_CURSES
|
|
|
|
" top Show real-time statistics\n"
|
|
|
|
#endif
|
2011-05-22 12:17:30 +00:00
|
|
|
" pcap Dump traffic in pcap format\n"
|
2009-09-29 13:19:55 +00:00
|
|
|
"\n");
|
|
|
|
printf("Report bugs to tinc@tinc-vpn.org.\n");
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_options(int argc, char **argv) {
|
|
|
|
int r;
|
|
|
|
int option_index = 0;
|
|
|
|
|
2011-05-29 12:41:05 +00:00
|
|
|
while((r = getopt_long(argc, argv, "c:n:h:", long_options, &option_index)) != EOF) {
|
2007-05-18 16:52:34 +00:00
|
|
|
switch (r) {
|
|
|
|
case 0: /* long option */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c': /* config file */
|
|
|
|
confbase = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
|
2011-05-29 12:41:05 +00:00
|
|
|
case 'h': /* alternative host to connect to */
|
|
|
|
host = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
case 'n': /* net name given */
|
|
|
|
netname = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* show help */
|
|
|
|
show_help = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* show version */
|
|
|
|
show_version = true;
|
|
|
|
break;
|
|
|
|
|
2007-05-19 14:13:21 +00:00
|
|
|
case 5: /* open control socket here */
|
2009-11-07 22:43:25 +00:00
|
|
|
controlcookiename = xstrdup(optarg);
|
2007-05-18 16:52:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
usage(true);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-19 14:55:35 +00:00
|
|
|
FILE *ask_and_open(const char *filename, const char *what, const char *mode) {
|
|
|
|
FILE *r;
|
|
|
|
char *directory;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
char buf2[PATH_MAX];
|
|
|
|
|
|
|
|
/* Check stdin and stdout */
|
|
|
|
if(isatty(0) && isatty(1)) {
|
|
|
|
/* Ask for a file and/or directory name. */
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stdout, "Please enter a file to save %s to [%s]: ",
|
2007-05-19 14:55:35 +00:00
|
|
|
what, filename);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2011-05-28 01:48:07 +00:00
|
|
|
if(fgets(buf, sizeof buf, stdin) == NULL) {
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Error while reading stdin: %s\n",
|
2007-05-19 14:55:35 +00:00
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-02 16:22:26 +00:00
|
|
|
size_t len = strlen(buf);
|
2007-05-19 14:55:35 +00:00
|
|
|
if(len)
|
|
|
|
buf[--len] = 0;
|
|
|
|
|
|
|
|
if(len)
|
|
|
|
filename = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) {
|
|
|
|
#else
|
|
|
|
if(filename[0] != '/') {
|
|
|
|
#endif
|
|
|
|
/* The directory is a relative path or a filename. */
|
|
|
|
directory = get_current_dir_name();
|
|
|
|
snprintf(buf2, sizeof buf2, "%s/%s", directory, filename);
|
|
|
|
filename = buf2;
|
|
|
|
}
|
|
|
|
|
|
|
|
umask(0077); /* Disallow everything for group and other */
|
|
|
|
|
|
|
|
/* Open it first to keep the inode busy */
|
|
|
|
|
|
|
|
r = fopen(filename, mode);
|
|
|
|
|
|
|
|
if(!r) {
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Error opening file `%s': %s\n", filename, strerror(errno));
|
2007-05-19 14:55:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
/*
|
|
|
|
Generate a public/private RSA keypair, and ask for a file to store
|
|
|
|
them in.
|
|
|
|
*/
|
|
|
|
static bool keygen(int bits) {
|
2008-12-14 12:47:26 +00:00
|
|
|
rsa_t key;
|
2007-05-18 16:52:34 +00:00
|
|
|
FILE *f;
|
|
|
|
char *filename;
|
|
|
|
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Generating %d bits keys:\n", bits);
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2008-12-14 12:47:26 +00:00
|
|
|
if(!rsa_generate(&key, bits, 0x10001)) {
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Error during key generation!\n");
|
2007-05-18 16:52:34 +00:00
|
|
|
return false;
|
|
|
|
} else
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Done.\n");
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&filename, "%s/rsa_key.priv", confbase);
|
2009-09-29 13:19:55 +00:00
|
|
|
f = ask_and_open(filename, "private RSA key", "a");
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
if(!f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef HAVE_FCHMOD
|
|
|
|
/* Make it unreadable for others. */
|
|
|
|
fchmod(fileno(f), 0600);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(ftell(f))
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2008-12-14 12:47:26 +00:00
|
|
|
rsa_write_pem_private_key(&key, f);
|
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
fclose(f);
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
if(name)
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&filename, "%s/hosts/%s", confbase, name);
|
2007-05-18 16:52:34 +00:00
|
|
|
else
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&filename, "%s/rsa_key.pub", confbase);
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2009-09-29 13:19:55 +00:00
|
|
|
f = ask_and_open(filename, "public RSA key", "a");
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
if(!f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(ftell(f))
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Appending key to existing contents.\nMake sure only one key is stored in the file.\n");
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2008-12-14 12:47:26 +00:00
|
|
|
rsa_write_pem_public_key(&key, f);
|
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
fclose(f);
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Set all files and paths according to netname
|
|
|
|
*/
|
|
|
|
static void make_names(void) {
|
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
HKEY key;
|
|
|
|
char installdir[1024] = "";
|
2008-12-11 15:56:18 +00:00
|
|
|
long len = sizeof installdir;
|
2007-05-18 16:52:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if(netname)
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&identname, "tinc.%s", netname);
|
2007-05-18 16:52:34 +00:00
|
|
|
else
|
|
|
|
identname = xstrdup("tinc");
|
|
|
|
|
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
|
|
|
|
if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
|
|
|
|
if(!confbase) {
|
|
|
|
if(netname)
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&confbase, "%s/%s", installdir, netname);
|
2007-05-18 16:52:34 +00:00
|
|
|
else
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&confbase, "%s", installdir);
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-07 22:43:25 +00:00
|
|
|
if(!controlcookiename)
|
|
|
|
xasprintf(&controlcookiename, "%s/cookie", confbase);
|
2007-05-18 16:52:34 +00:00
|
|
|
RegCloseKey(key);
|
|
|
|
if(*installdir)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
if(!controlcookiename)
|
2009-12-11 21:38:06 +00:00
|
|
|
xasprintf(&controlcookiename, "%s/run/%s.cookie", LOCALSTATEDIR, identname);
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
if(netname) {
|
|
|
|
if(!confbase)
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
|
2007-05-18 16:52:34 +00:00
|
|
|
else
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Both netname and configuration directory given, using the latter...\n");
|
2007-05-18 16:52:34 +00:00
|
|
|
} else {
|
|
|
|
if(!confbase)
|
2009-09-16 18:28:30 +00:00
|
|
|
xasprintf(&confbase, CONFDIR "/tinc");
|
2007-05-18 16:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-22 12:17:30 +00:00
|
|
|
static char buffer[4096];
|
|
|
|
static size_t blen = 0;
|
|
|
|
|
2011-05-15 11:16:48 +00:00
|
|
|
bool recvline(int fd, char *line, size_t len) {
|
2009-11-07 22:43:25 +00:00
|
|
|
char *newline = NULL;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
while(!(newline = memchr(buffer, '\n', blen))) {
|
|
|
|
int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
|
|
|
|
if(result == -1 && errno == EINTR)
|
2007-11-07 02:48:33 +00:00
|
|
|
continue;
|
2009-11-07 22:43:25 +00:00
|
|
|
else if(result <= 0)
|
|
|
|
return false;
|
|
|
|
blen += result;
|
2007-11-07 02:48:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
if(newline - buffer >= len)
|
|
|
|
return false;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
len = newline - buffer;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
memcpy(line, buffer, len);
|
|
|
|
line[len] = 0;
|
|
|
|
memmove(buffer, newline + 1, blen - len - 1);
|
|
|
|
blen -= len + 1;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2011-05-22 12:17:30 +00:00
|
|
|
bool recvdata(int fd, char *data, size_t len) {
|
|
|
|
while(blen < len) {
|
|
|
|
int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
|
|
|
|
if(result == -1 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else if(result <= 0)
|
|
|
|
return false;
|
|
|
|
blen += result;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, buffer, len);
|
|
|
|
memmove(buffer, buffer + len, blen - len);
|
|
|
|
blen -= len;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-15 11:16:48 +00:00
|
|
|
bool sendline(int fd, char *format, ...) {
|
2009-11-07 22:43:25 +00:00
|
|
|
static char buffer[4096];
|
|
|
|
char *p = buffer;
|
|
|
|
size_t blen = 0;
|
|
|
|
va_list ap;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
va_start(ap, format);
|
|
|
|
blen = vsnprintf(buffer, sizeof buffer, format, ap);
|
|
|
|
va_end(ap);
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
if(blen < 0 || blen >= sizeof buffer)
|
|
|
|
return false;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
buffer[blen] = '\n';
|
|
|
|
blen++;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
while(blen) {
|
|
|
|
int result = send(fd, p, blen, 0);
|
|
|
|
if(result == -1 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else if(result <= 0);
|
|
|
|
return false;
|
|
|
|
p += result;
|
|
|
|
blen -= result;
|
2007-11-07 02:48:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
return true;
|
2007-11-07 02:48:33 +00:00
|
|
|
}
|
|
|
|
|
2011-05-22 12:17:30 +00:00
|
|
|
void pcap(int fd, FILE *out) {
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_PCAP);
|
|
|
|
char data[9018];
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint32_t magic;
|
|
|
|
uint16_t major;
|
|
|
|
uint16_t minor;
|
|
|
|
uint32_t tz_offset;
|
|
|
|
uint32_t tz_accuracy;
|
|
|
|
uint32_t snaplen;
|
|
|
|
uint32_t ll_type;
|
|
|
|
} header = {
|
|
|
|
0xa1b2c3d4,
|
|
|
|
2, 4,
|
|
|
|
0, 0,
|
|
|
|
sizeof data,
|
|
|
|
1,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint32_t tv_sec;
|
|
|
|
uint32_t tv_usec;
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t origlen;
|
|
|
|
} packet;
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
fwrite(&header, sizeof header, 1, out);
|
|
|
|
fflush(out);
|
|
|
|
|
|
|
|
char line[32];
|
|
|
|
while(recvline(fd, line, sizeof line)) {
|
|
|
|
int code, req, len;
|
|
|
|
int n = sscanf(line, "%d %d %d", &code, &req, &len);
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
if(n != 3 || code != CONTROL || req != REQ_PCAP || len < 0 || len > sizeof data)
|
|
|
|
break;
|
|
|
|
if(!recvdata(fd, data, len))
|
|
|
|
break;
|
|
|
|
packet.tv_sec = tv.tv_sec;
|
|
|
|
packet.tv_usec = tv.tv_usec;
|
|
|
|
packet.len = len;
|
|
|
|
packet.origlen = len;
|
|
|
|
fwrite(&packet, sizeof packet, 1, out);
|
|
|
|
fwrite(data, len, 1, out);
|
|
|
|
fflush(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 15:21:26 +00:00
|
|
|
int main(int argc, char *argv[], char *envp[]) {
|
2007-11-08 19:18:44 +00:00
|
|
|
int fd;
|
|
|
|
int result;
|
2011-05-29 12:41:05 +00:00
|
|
|
char port[128];
|
2009-11-07 22:43:25 +00:00
|
|
|
int pid;
|
2007-11-07 02:48:33 +00:00
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
program_name = argv[0];
|
|
|
|
|
|
|
|
if(!parse_options(argc, argv))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
make_names();
|
|
|
|
|
|
|
|
if(show_version) {
|
2009-09-29 13:19:55 +00:00
|
|
|
printf("%s version %s (built %s %s, protocol %d)\n", PACKAGE,
|
2007-05-18 16:52:34 +00:00
|
|
|
VERSION, __DATE__, __TIME__, PROT_CURRENT);
|
2009-09-29 13:19:55 +00:00
|
|
|
printf("Copyright (C) 1998-2009 Ivo Timmermans, Guus Sliepen and others.\n"
|
2007-05-18 16:52:34 +00:00
|
|
|
"See the AUTHORS file for a complete list.\n\n"
|
|
|
|
"tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
|
|
|
|
"and you are welcome to redistribute it under certain conditions;\n"
|
2009-09-29 13:19:55 +00:00
|
|
|
"see the file COPYING for details.\n");
|
2007-05-18 16:52:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(show_help) {
|
|
|
|
usage(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-19 14:55:35 +00:00
|
|
|
if(optind >= argc) {
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Not enough arguments.\n");
|
2007-05-19 14:55:35 +00:00
|
|
|
usage(true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-05-19 15:21:26 +00:00
|
|
|
// First handle commands that don't involve connecting to a running tinc daemon.
|
|
|
|
|
2007-05-19 14:55:35 +00:00
|
|
|
if(!strcasecmp(argv[optind], "generate-keys")) {
|
2009-09-16 17:55:47 +00:00
|
|
|
return !keygen(optind > argc ? atoi(argv[optind + 1]) : 2048);
|
2007-05-19 14:55:35 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 15:21:26 +00:00
|
|
|
if(!strcasecmp(argv[optind], "start")) {
|
|
|
|
argv[optind] = NULL;
|
2008-12-11 13:59:46 +00:00
|
|
|
execve(SBINDIR "/tincd", argv, envp);
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Could not start tincd: %s", strerror(errno));
|
2007-05-19 15:21:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-08 19:18:44 +00:00
|
|
|
/*
|
|
|
|
* Now handle commands that do involve connecting to a running tinc daemon.
|
|
|
|
* Authenticate the server by ensuring the parent directory can be
|
|
|
|
* traversed only by root. Note this is not totally race-free unless all
|
|
|
|
* ancestors are writable only by trusted users, which we don't verify.
|
|
|
|
*/
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
FILE *f = fopen(controlcookiename, "r");
|
|
|
|
if(!f) {
|
|
|
|
fprintf(stderr, "Could not open control socket cookie file %s: %s\n", controlcookiename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2011-06-02 16:22:26 +00:00
|
|
|
if(fscanf(f, "%1024s %128s %20d", controlcookie, port, &pid) != 3) {
|
2009-11-07 22:43:25 +00:00
|
|
|
fprintf(stderr, "Could not parse control socket cookie file %s\n", controlcookiename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-11-05 22:29:28 +00:00
|
|
|
#ifdef HAVE_MINGW
|
|
|
|
if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
|
|
|
|
fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
|
|
|
|
return 1;
|
|
|
|
}
|
2009-11-07 22:43:25 +00:00
|
|
|
#endif
|
2009-11-05 22:29:28 +00:00
|
|
|
|
2011-05-29 12:41:05 +00:00
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = AF_UNSPEC,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP,
|
|
|
|
.ai_flags = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct addrinfo *res = NULL;
|
2009-11-05 22:29:28 +00:00
|
|
|
|
2011-05-29 12:41:05 +00:00
|
|
|
if(getaddrinfo(host, port, &hints, &res) || !res) {
|
|
|
|
fprintf(stderr, "Cannot resolve %s port %s: %s", host ?: "localhost", port, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP);
|
2009-11-05 22:29:28 +00:00
|
|
|
if(fd < 0) {
|
|
|
|
fprintf(stderr, "Cannot create TCP socket: %s\n", sockstrerror(sockerrno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-11 21:24:07 +00:00
|
|
|
#ifdef HAVE_MINGW
|
2009-11-05 22:29:28 +00:00
|
|
|
unsigned long arg = 0;
|
|
|
|
|
|
|
|
if(ioctlsocket(fd, FIONBIO, &arg) != 0) {
|
|
|
|
fprintf(stderr, "ioctlsocket failed: %s", sockstrerror(sockerrno));
|
|
|
|
}
|
2009-12-11 21:24:07 +00:00
|
|
|
#endif
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2011-05-29 12:41:05 +00:00
|
|
|
if(connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
|
2011-05-29 20:10:54 +00:00
|
|
|
fprintf(stderr, "Cannot connect to %s port %s: %s\n", host ?: "localhost", port, sockstrerror(sockerrno));
|
2007-05-18 16:52:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-29 12:41:05 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
char line[4096];
|
|
|
|
char data[4096];
|
|
|
|
int code, version, req;
|
|
|
|
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %s %d", &code, data, &version) != 3 || code != 0) {
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Cannot read greeting from control socket: %s\n",
|
2009-11-05 22:29:28 +00:00
|
|
|
sockstrerror(sockerrno));
|
2007-11-07 02:48:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
sendline(fd, "%d ^%s %d", ID, controlcookie, TINC_CTL_VERSION_CURRENT);
|
|
|
|
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &version, &pid) != 3 || code != 4 || version != TINC_CTL_VERSION_CURRENT) {
|
|
|
|
fprintf(stderr, "Could not fully establish control socket connection\n");
|
2007-11-07 02:48:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-05-19 15:21:26 +00:00
|
|
|
if(!strcasecmp(argv[optind], "pid")) {
|
2009-11-07 22:43:25 +00:00
|
|
|
printf("%d\n", pid);
|
2007-05-19 15:21:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-18 16:52:34 +00:00
|
|
|
|
2007-05-19 15:21:26 +00:00
|
|
|
if(!strcasecmp(argv[optind], "stop")) {
|
2009-11-07 22:43:25 +00:00
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_STOP);
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_STOP || result) {
|
|
|
|
fprintf(stderr, "Could not stop tinc daemon\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-05-19 15:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!strcasecmp(argv[optind], "reload")) {
|
2009-11-07 22:43:25 +00:00
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_RELOAD);
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RELOAD || result) {
|
|
|
|
fprintf(stderr, "Could not reload tinc daemon\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-05-19 15:21:26 +00:00
|
|
|
}
|
2009-11-07 22:43:25 +00:00
|
|
|
|
2007-05-19 15:21:26 +00:00
|
|
|
if(!strcasecmp(argv[optind], "restart")) {
|
2009-11-07 22:43:25 +00:00
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_RESTART);
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RESTART || result) {
|
|
|
|
fprintf(stderr, "Could not restart tinc daemon\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strcasecmp(argv[optind], "retry")) {
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_RETRY);
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_RETRY || result) {
|
|
|
|
fprintf(stderr, "Could not retry outgoing connections\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-05-19 15:21:26 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 02:49:25 +00:00
|
|
|
if(!strcasecmp(argv[optind], "dump")) {
|
2007-11-07 06:45:28 +00:00
|
|
|
if(argc < optind + 2) {
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Not enough arguments.\n");
|
2007-11-07 02:49:25 +00:00
|
|
|
usage(true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
bool do_graph = false;
|
|
|
|
|
|
|
|
if(!strcasecmp(argv[optind+1], "nodes"))
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
|
|
|
|
else if(!strcasecmp(argv[optind+1], "edges"))
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
|
|
|
|
else if(!strcasecmp(argv[optind+1], "subnets"))
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_DUMP_SUBNETS);
|
|
|
|
else if(!strcasecmp(argv[optind+1], "connections"))
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_DUMP_CONNECTIONS);
|
|
|
|
else if(!strcasecmp(argv[optind+1], "graph")) {
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
|
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_DUMP_EDGES);
|
|
|
|
do_graph = true;
|
|
|
|
printf("digraph {\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown dump type '%s'.\n", argv[optind+1]);
|
|
|
|
usage(true);
|
|
|
|
return 1;
|
2007-11-07 02:49:25 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
while(recvline(fd, line, sizeof line)) {
|
|
|
|
char node1[4096], node2[4096];
|
2009-12-11 21:38:06 +00:00
|
|
|
int n = sscanf(line, "%d %d %s to %s", &code, &req, node1, node2);
|
2009-11-07 22:43:25 +00:00
|
|
|
if(n == 2) {
|
|
|
|
if(do_graph && req == REQ_DUMP_NODES)
|
|
|
|
continue;
|
|
|
|
else {
|
|
|
|
if(do_graph)
|
|
|
|
printf("}\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(n < 2)
|
|
|
|
break;
|
2007-11-07 02:49:25 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
if(!do_graph)
|
|
|
|
printf("%s\n", line + 5);
|
|
|
|
else {
|
|
|
|
if(req == REQ_DUMP_NODES)
|
|
|
|
printf(" %s [label = \"%s\"];\n", node1, node1);
|
|
|
|
else
|
|
|
|
printf(" %s -> %s;\n", node1, node2);
|
|
|
|
}
|
2007-11-07 02:49:25 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
fprintf(stderr, "Error receiving dump\n");
|
2007-11-07 02:49:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-07 02:49:57 +00:00
|
|
|
if(!strcasecmp(argv[optind], "purge")) {
|
2009-11-07 22:43:25 +00:00
|
|
|
sendline(fd, "%d %d", CONTROL, REQ_PURGE);
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_PURGE || result) {
|
|
|
|
fprintf(stderr, "Could not purge tinc daemon\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-11-07 02:49:57 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 02:50:27 +00:00
|
|
|
if(!strcasecmp(argv[optind], "debug")) {
|
2009-11-07 22:43:25 +00:00
|
|
|
int debuglevel, origlevel;
|
2007-11-07 02:50:27 +00:00
|
|
|
|
|
|
|
if(argc != optind + 2) {
|
|
|
|
fprintf(stderr, "Invalid arguments.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
debuglevel = atoi(argv[optind+1]);
|
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
sendline(fd, "%d %d %d", CONTROL, REQ_SET_DEBUG, debuglevel);
|
2009-12-11 21:38:06 +00:00
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &origlevel) != 3 || code != CONTROL || req != REQ_SET_DEBUG) {
|
2009-11-07 22:43:25 +00:00
|
|
|
fprintf(stderr, "Could not purge tinc daemon\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2007-11-07 02:50:58 +00:00
|
|
|
|
2009-11-07 22:43:25 +00:00
|
|
|
fprintf(stderr, "Old level %d, new level %d\n", origlevel, debuglevel);
|
|
|
|
return 0;
|
2007-11-07 02:51:24 +00:00
|
|
|
}
|
|
|
|
|
2009-12-16 20:16:56 +00:00
|
|
|
if(!strcasecmp(argv[optind], "connect")) {
|
|
|
|
if(argc != optind + 2) {
|
|
|
|
fprintf(stderr, "Invalid arguments.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char *name = argv[optind + 1];
|
|
|
|
|
|
|
|
sendline(fd, "%d %d %s", CONTROL, REQ_CONNECT, name);
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_CONNECT || result) {
|
|
|
|
fprintf(stderr, "Could not connect to %s\n", name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!strcasecmp(argv[optind], "disconnect")) {
|
|
|
|
if(argc != optind + 2) {
|
|
|
|
fprintf(stderr, "Invalid arguments.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char *name = argv[optind + 1];
|
|
|
|
|
|
|
|
sendline(fd, "%d %d %s", CONTROL, REQ_DISCONNECT, name);
|
|
|
|
if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_DISCONNECT || result) {
|
|
|
|
fprintf(stderr, "Could not disconnect %s\n", name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-15 11:16:48 +00:00
|
|
|
#ifdef HAVE_CURSES
|
|
|
|
if(!strcasecmp(argv[optind], "top")) {
|
|
|
|
top(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-22 12:17:30 +00:00
|
|
|
if(!strcasecmp(argv[optind], "pcap")) {
|
|
|
|
pcap(fd, stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-29 13:19:55 +00:00
|
|
|
fprintf(stderr, "Unknown command `%s'.\n", argv[optind]);
|
2007-05-19 15:21:26 +00:00
|
|
|
usage(true);
|
|
|
|
|
2007-05-18 16:52:34 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|