/*
tincctl.c -- Controlling a running tincd
- Copyright (C) 2007-2013 Guus Sliepen <guus@tinc-vpn.org>
+ Copyright (C) 2007-2014 Guus Sliepen <guus@tinc-vpn.org>
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
#include "tincctl.h"
#include "top.h"
-#ifdef HAVE_MINGW
-#define SCRIPTEXTENSION ".bat"
-#else
-#define SCRIPTEXTENSION ""
+#ifndef MSG_NOSIGNAL
+#define MSG_NOSIGNAL 0
#endif
static char **orig_argv;
bool tty = true;
bool confbasegiven = false;
bool netnamegiven = false;
-
-#ifdef HAVE_MINGW
-static struct WSAData wsa_state;
-#endif
+char *scriptinterpreter = NULL;
+char *scriptextension = "";
+static char *prompt;
static struct option const long_options[] = {
{"config", required_argument, NULL, 'c'},
" restart [tincd options] Restart tincd.\n"
" reload Partially reload configuration of running tincd.\n"
" pid Show PID of currently running tincd.\n"
- " generate-keys [bits] Generate new RSA and ECDSA public/private keypairs.\n"
+ " generate-keys [bits] Generate new RSA and Ed25519 public/private keypairs.\n"
" generate-rsa-keys [bits] Generate a new RSA public/private keypair.\n"
- " generate-ecdsa-keys Generate a new ECDSA public/private keypair.\n"
+ " generate-ed25519-keys Generate a new Ed25519 public/private keypair.\n"
" dump Dump a list of one of the following things:\n"
" [reachable] nodes - all known nodes in the VPN\n"
" edges - all known connections in the VPN\n"
" exchange-all [--force] Same as export-all followed by import\n"
" invite NODE [...] Generate an invitation for NODE\n"
" join INVITATION Join a VPN using an INVITIATION\n"
+ " network [NETNAME] List all known networks, or switch to the one named NETNAME.\n"
"\n");
printf("Report bugs to tinc@tinc-vpn.org.\n");
}
return true;
}
+/* Open a file with the desired permissions, minus the umask.
+ Also, if we want to create an executable file, we call fchmod()
+ to set the executable bits. */
+
+FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
+ mode_t mask = umask(0);
+ perms &= ~mask;
+ umask(~perms);
+ FILE *f = fopen(filename, mode);
+#ifdef HAVE_FCHMOD
+ if((perms & 0444) && f)
+ fchmod(fileno(f), perms);
+#endif
+ umask(mask);
+ return f;
+}
+
static void disable_old_keys(const char *filename, const char *what) {
char tmpfile[PATH_MAX] = "";
char buf[1024];
snprintf(tmpfile, sizeof tmpfile, "%s.tmp", filename);
- w = fopen(tmpfile, "w");
-
-#ifdef HAVE_FCHMOD
- /* Let the temporary file have the same permissions as the original. */
-
- if(w) {
- struct stat st = {.st_mode = 0600};
- fstat(fileno(r), &st);
- fchmod(fileno(w), st.st_mode);
- }
-#endif
+ struct stat st = {.st_mode = 0600};
+ fstat(fileno(r), &st);
+ w = fopenmask(tmpfile, "w", st.st_mode);
while(fgets(buf, sizeof buf, r)) {
if(!block && !strncmp(buf, "-----BEGIN ", 11)) {
- if((strstr(buf, " EC ") && strstr(what, "ECDSA")) || (strstr(buf, " RSA ") && strstr(what, "RSA"))) {
+ if((strstr(buf, " ED25519 ") && strstr(what, "Ed25519")) || (strstr(buf, " RSA ") && strstr(what, "RSA"))) {
disabled = true;
block = true;
}
}
- bool ecdsapubkey = !strncasecmp(buf, "ECDSAPublicKey", 14) && strchr(" \t=", buf[14]) && strstr(what, "ECDSA");
+ bool ed25519pubkey = !strncasecmp(buf, "Ed25519PublicKey", 16) && strchr(" \t=", buf[16]) && strstr(what, "Ed25519");
- if(ecdsapubkey)
+ if(ed25519pubkey)
disabled = true;
if(w) {
- if(block || ecdsapubkey)
+ if(block || ed25519pubkey)
fputc('#', w);
if(fputs(buf, w) < 0) {
error = true;
unlink(tmpfile);
}
-static FILE *ask_and_open(const char *filename, const char *what, const char *mode, bool ask) {
+static FILE *ask_and_open(const char *filename, const char *what, const char *mode, bool ask, mode_t perms) {
FILE *r;
char *directory;
char buf[PATH_MAX];
/* Check stdin and stdout */
if(ask && tty) {
/* Ask for a file and/or directory name. */
- fprintf(stdout, "Please enter a file to save %s to [%s]: ", what, filename);
- fflush(stdout);
+ fprintf(stderr, "Please enter a file to save %s to [%s]: ", what, filename);
if(fgets(buf, sizeof buf, stdin) == NULL) {
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
/* Open it first to keep the inode busy */
- r = fopen(filename, mode);
+ r = fopenmask(filename, mode, perms);
if(!r) {
fprintf(stderr, "Error opening file `%s': %s\n", filename, strerror(errno));
}
/*
- Generate a public/private ECDSA keypair, and ask for a file to store
+ Generate a public/private Ed25519 keypair, and ask for a file to store
them in.
*/
-static bool ecdsa_keygen(bool ask) {
+static bool ed25519_keygen(bool ask) {
ecdsa_t *key;
FILE *f;
char *pubname, *privname;
- fprintf(stderr, "Generating ECDSA keypair:\n");
+ fprintf(stderr, "Generating Ed25519 keypair:\n");
if(!(key = ecdsa_generate())) {
fprintf(stderr, "Error during key generation!\n");
} else
fprintf(stderr, "Done.\n");
- xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase);
- f = ask_and_open(privname, "private ECDSA key", "a", ask);
+ xasprintf(&privname, "%s" SLASH "ed25519_key.priv", confbase);
+ f = ask_and_open(privname, "private Ed25519 key", "a", ask, 0600);
free(privname);
if(!f)
return false;
-#ifdef HAVE_FCHMOD
- /* Make it unreadable for others. */
- fchmod(fileno(f), 0600);
-#endif
-
if(!ecdsa_write_pem_private_key(key, f)) {
fprintf(stderr, "Error writing private key!\n");
ecdsa_free(key);
if(name)
xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
else
- xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase);
+ xasprintf(&pubname, "%s" SLASH "ed25519_key.pub", confbase);
- f = ask_and_open(pubname, "public ECDSA key", "a", ask);
+ f = ask_and_open(pubname, "public Ed25519 key", "a", ask, 0666);
free(pubname);
if(!f)
return false;
char *pubkey = ecdsa_get_base64_public_key(key);
- fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
+ fprintf(f, "Ed25519PublicKey = %s\n", pubkey);
free(pubkey);
fclose(f);
FILE *f;
char *pubname, *privname;
+ // Make sure the key size is a multiple of 8 bits.
+ bits &= ~0x7;
+
+ // Force them to be between 1024 and 8192 bits long.
+ if(bits < 1024)
+ bits = 1024;
+ if(bits > 8192)
+ bits = 8192;
+
fprintf(stderr, "Generating %d bits keys:\n", bits);
if(!(key = rsa_generate(bits, 0x10001))) {
fprintf(stderr, "Done.\n");
xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
- f = ask_and_open(privname, "private RSA key", "a", ask);
+ f = ask_and_open(privname, "private RSA key", "a", ask, 0600);
free(privname);
if(!f)
return false;
-#ifdef HAVE_FCHMOD
- /* Make it unreadable for others. */
- fchmod(fileno(f), 0600);
-#endif
-
if(!rsa_write_pem_private_key(key, f)) {
fprintf(stderr, "Error writing private key!\n");
fclose(f);
else
xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
- f = ask_and_open(pubname, "public RSA key", "a", ask);
+ f = ask_and_open(pubname, "public RSA key", "a", ask, 0666);
free(pubname);
if(!f)
while(!(newline = memchr(buffer, '\n', blen))) {
int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
- if(result == -1 && errno == EINTR)
+ if(result == -1 && sockerrno == EINTR)
continue;
else if(result <= 0)
return false;
while(blen < len) {
int result = recv(fd, buffer + blen, sizeof buffer - blen, 0);
- if(result == -1 && errno == EINTR)
+ if(result == -1 && sockerrno == EINTR)
continue;
else if(result <= 0)
return false;
blen++;
while(blen) {
- int result = send(fd, p, blen, 0);
- if(result == -1 && errno == EINTR)
+ int result = send(fd, p, blen, MSG_NOSIGNAL);
+ if(result == -1 && sockerrno == EINTR)
continue;
else if(result <= 0)
return false;
fclose(f);
-#ifdef HAVE_MINGW
- if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
- if(verbose)
- fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
- return false;
- }
-#endif
-
#ifndef HAVE_MINGW
struct sockaddr_un sa;
sa.sun_family = AF_UNIX;
if(getaddrinfo(host, port, &hints, &res) || !res) {
if(verbose)
- fprintf(stderr, "Cannot resolve %s port %s: %s", host, port, strerror(errno));
+ fprintf(stderr, "Cannot resolve %s port %s: %s", host, port, sockstrerror(sockerrno));
return false;
}
freeaddrinfo(res);
#endif
+#ifdef SO_NOSIGPIPE
+ static const int one = 1;
+ setsockopt(c, SOL_SOCKET, SO_NOSIGPIPE, (void *)&one, sizeof one);
+#endif
+
char data[4096];
int version;
char subnet[4096];
char host[4096];
char port[4096];
+ char local_host[4096];
+ char local_port[4096];
char via[4096];
char nexthop[4096];
int cipher, digest, maclength, compression, distance, socket, weight;
} break;
case REQ_DUMP_EDGES: {
- int n = sscanf(line, "%*d %*d %s %s %s port %s %x %d", from, to, host, port, &options, &weight);
- if(n != 6) {
+ int n = sscanf(line, "%*d %*d %s %s %s port %s %s port %s %x %d", from, to, host, port, local_host, local_port, &options, &weight);
+ if(n != 8) {
fprintf(stderr, "Unable to parse edge dump from tincd.\n");
return 1;
}
else if(do_graph == 2)
printf(" %s -> %s [w = %f, weight = %f];\n", node1, node2, w, w);
} else {
- printf("%s to %s at %s port %s options %x weight %d\n", from, to, host, port, options, weight);
+ printf("%s to %s at %s port %s local %s port %s options %x weight %d\n", from, to, host, port, local_host, local_port, options, weight);
}
} break;
{"Device", VAR_SERVER},
{"DeviceType", VAR_SERVER},
{"DirectOnly", VAR_SERVER},
- {"ECDSAPrivateKeyFile", VAR_SERVER},
+ {"Ed25519PrivateKeyFile", VAR_SERVER},
{"ExperimentalProtocol", VAR_SERVER},
{"Forwarding", VAR_SERVER},
{"GraphDumpFile", VAR_SERVER | VAR_OBSOLETE},
{"IffOneQueue", VAR_SERVER},
{"Interface", VAR_SERVER},
{"KeyExpire", VAR_SERVER},
+ {"ListenAddress", VAR_SERVER | VAR_MULTIPLE},
{"LocalDiscovery", VAR_SERVER},
{"MACExpire", VAR_SERVER},
{"MaxConnectionBurst", VAR_SERVER},
{"ClampMSS", VAR_SERVER | VAR_HOST},
{"Compression", VAR_SERVER | VAR_HOST},
{"Digest", VAR_SERVER | VAR_HOST},
- {"ECDSAPublicKey", VAR_HOST},
- {"ECDSAPublicKeyFile", VAR_SERVER | VAR_HOST},
+ {"Ed25519PublicKey", VAR_HOST},
+ {"Ed25519PublicKeyFile", VAR_SERVER | VAR_HOST},
{"IndirectData", VAR_SERVER | VAR_HOST},
{"MACLength", VAR_SERVER | VAR_HOST},
{"PMTU", VAR_SERVER | VAR_HOST},
if(action < -1) {
if(!found)
fprintf(stderr, "No matching configuration variables found.\n");
- return 0;
+ return 1;
}
// Make sure we wrote everything...
if(action < 0 && !removed) {
remove(tmpfile);
fprintf(stderr, "No configuration variables deleted.\n");
- return *value;
+ return 1;
}
// Replace the configuration file with the new one
fprintf(stderr, "Warning: could not bind to port 655. ");
- srand(time(NULL));
-
for(int i = 0; i < 100; i++) {
int port = 0x1000 + (rand() & 0x7fff);
if(try_bind(port)) {
} else if(argc < 2) {
if(tty) {
char buf[1024];
- fprintf(stdout, "Enter the Name you want your tinc node to have: ");
- fflush(stdout);
+ fprintf(stderr, "Enter the Name you want your tinc node to have: ");
if(!fgets(buf, sizeof buf, stdin)) {
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
return 1;
return 1;
}
- if(mkdir(confdir, 0755) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", CONFDIR, strerror(errno));
+ if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
+ fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
return 1;
}
- if(mkdir(confbase, 0755) && errno != EEXIST) {
+ if(mkdir(confbase, 0777) && errno != EEXIST) {
fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
return 1;
}
- if(mkdir(hosts_dir, 0755) && errno != EEXIST) {
+ if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
return 1;
}
fprintf(f, "Name = %s\n", name);
fclose(f);
- if(!rsa_keygen(2048, false) || !ecdsa_keygen(false))
+ if(!rsa_keygen(2048, false) || !ed25519_keygen(false))
return 1;
check_port(name);
char *filename;
xasprintf(&filename, "%s" SLASH "tinc-up", confbase);
if(access(filename, F_OK)) {
- FILE *f = fopen(filename, "w");
+ FILE *f = fopenmask(filename, "w", 0777);
if(!f) {
fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
return 1;
}
- mode_t mask = umask(0);
- umask(mask);
- fchmod(fileno(f), 0755 & ~mask);
- fprintf(f, "#!/bin/sh\n\necho 'Unconfigured tinc-up script, please edit!'\n\n#ifconfig $INTERFACE <your vpn IP address> netmask <netmask of whole VPN>\n");
+ fprintf(f, "#!/bin/sh\n\necho 'Unconfigured tinc-up script, please edit '$0'!'\n\n#ifconfig $INTERFACE <your vpn IP address> netmask <netmask of whole VPN>\n");
fclose(f);
}
#endif
if(!name)
name = get_my_name(false);
- return !(rsa_keygen(argc > 1 ? atoi(argv[1]) : 2048, true) && ecdsa_keygen(true));
+ return !(rsa_keygen(argc > 1 ? atoi(argv[1]) : 2048, true) && ed25519_keygen(true));
}
static int cmd_generate_rsa_keys(int argc, char *argv[]) {
return !rsa_keygen(argc > 1 ? atoi(argv[1]) : 2048, true);
}
-static int cmd_generate_ecdsa_keys(int argc, char *argv[]) {
+static int cmd_generate_ed25519_keys(int argc, char *argv[]) {
if(argc > 1) {
fprintf(stderr, "Too many arguments!\n");
return 1;
if(!name)
name = get_my_name(false);
- return !ecdsa_keygen(true);
+ return !ed25519_keygen(true);
}
static int cmd_help(int argc, char *argv[]) {
return cmd_export_all(argc, argv) ?: cmd_import(argc, argv);
}
+static int switch_network(char *name) {
+ if(fd >= 0) {
+ close(fd);
+ fd = -1;
+ }
+
+ free(confbase);
+ confbase = NULL;
+ free(pidfilename);
+ pidfilename = NULL;
+ free(logfilename);
+ logfilename = NULL;
+ free(unixsocketname);
+ unixsocketname = NULL;
+ free(tinc_conf);
+ free(hosts_dir);
+ free(prompt);
+
+ free(netname);
+ netname = strcmp(name, ".") ? xstrdup(name) : NULL;
+
+ make_names();
+ xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
+ xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
+ xasprintf(&prompt, "%s> ", identname);
+
+ return 0;
+}
+
+static int cmd_network(int argc, char *argv[]) {
+ if(argc > 2) {
+ fprintf(stderr, "Too many arguments!\n");
+ return 1;
+ }
+
+ if(argc == 2)
+ return switch_network(argv[1]);
+
+ DIR *dir = opendir(confdir);
+ if(!dir) {
+ fprintf(stderr, "Could not read directory %s: %s\n", confdir, strerror(errno));
+ return 1;
+ }
+
+ struct dirent *ent;
+ while((ent = readdir(dir))) {
+ if(*ent->d_name == '.')
+ continue;
+
+ if(!strcmp(ent->d_name, "tinc.conf")) {
+ printf(".\n");
+ continue;
+ }
+
+ char *fname;
+ xasprintf(&fname, "%s/%s/tinc.conf", confdir, ent->d_name);
+ if(!access(fname, R_OK))
+ printf("%s\n", ent->d_name);
+ free(fname);
+ }
+
+ closedir(dir);
+
+ return 0;
+}
+
static const struct {
const char *command;
int (*function)(int argc, char *argv[]);
{"init", cmd_init},
{"generate-keys", cmd_generate_keys},
{"generate-rsa-keys", cmd_generate_rsa_keys},
- {"generate-ecdsa-keys", cmd_generate_ecdsa_keys},
+ {"generate-ed25519-keys", cmd_generate_ed25519_keys},
{"help", cmd_help},
{"version", cmd_version},
{"info", cmd_info},
{"exchange-all", cmd_exchange_all},
{"invite", cmd_invite},
{"join", cmd_join},
+ {"network", cmd_network},
{NULL, NULL},
};
#endif
static int cmd_shell(int argc, char *argv[]) {
- char *prompt;
xasprintf(&prompt, "%s> ", identname);
int result = 0;
char buf[4096];
return 0;
}
+#ifdef HAVE_MINGW
+ static struct WSAData wsa_state;
+
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+ return false;
+ }
+#endif
+
+ srand(time(NULL));
crypto_init();
tty = isatty(0) && isatty(1);