/*
tincctl.c -- Controlling a running tincd
- Copyright (C) 2007-2013 Guus Sliepen <guus@tinc-vpn.org>
+ Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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 "ecdsagen.h"
#include "info.h"
#include "invitation.h"
-#include "names.h"
#include "rsagen.h"
#include "utils.h"
#include "tincctl.h"
#include "top.h"
+#ifndef MSG_NOSIGNAL
+#define MSG_NOSIGNAL 0
+#endif
+
static char **orig_argv;
static int orig_argc;
bool netnamegiven = false;
char *scriptinterpreter = NULL;
char *scriptextension = "";
+static char *prompt;
static struct option const long_options[] = {
{"config", required_argument, NULL, 'c'},
" 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");
+ printf("Report bugs to bugs@meshlink.io.\n");
}
}
/* 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));
static bool ecdsa_keygen(bool ask) {
ecdsa_t *key;
FILE *f;
- char *pubname, *privname;
+ char pubname[PATH_MAX], privname[PATH_MAX];
fprintf(stderr, "Generating ECDSA keypair:\n");
} else
fprintf(stderr, "Done.\n");
- xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase);
+ snprintf(privname,PATH_MAX, "%s" SLASH "ecdsa_key.priv", confbase);
f = ask_and_open(privname, "private ECDSA key", "a", ask, 0600);
- free(privname);
if(!f)
return false;
fclose(f);
if(name)
- xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
+ snprintf(pubname, PATH_MAX,"%s" SLASH "hosts" SLASH "%s", confbase, name);
else
- xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase);
+ snprintf(pubname, PATH_MAX,"%s" SLASH "ecdsa_key.pub", confbase);
f = ask_and_open(pubname, "public ECDSA key", "a", ask, 0666);
- free(pubname);
if(!f)
return false;
char *pubkey = ecdsa_get_base64_public_key(key);
fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
- free(pubkey);
fclose(f);
ecdsa_free(key);
static bool rsa_keygen(int bits, bool ask) {
rsa_t *key;
FILE *f;
- char *pubname, *privname;
+ char pubname[PATH_MAX], privname[PATH_MAX];
fprintf(stderr, "Generating %d bits keys:\n", bits);
} else
fprintf(stderr, "Done.\n");
- xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
+ snprintf(privname,PATH_MAX, "%s" SLASH "rsa_key.priv", confbase);
f = ask_and_open(privname, "private RSA key", "a", ask, 0600);
- free(privname);
if(!f)
return false;
fclose(f);
if(name)
- xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
+ snprintf(pubname,PATH_MAX,"%s" SLASH "hosts" SLASH "%s", confbase, name);
else
- xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
+ snprintf(pubname,PATH_MAX,"%s" SLASH "rsa_key.pub", confbase);
f = ask_and_open(pubname, "public RSA key", "a", ask, 0666);
- free(pubname);
if(!f)
return false;
blen++;
while(blen) {
- int result = send(fd, p, blen, 0);
+ int result = send(fd, p, blen, MSG_NOSIGNAL);
if(result == -1 && errno == EINTR)
continue;
else if(result <= 0)
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;
{"IffOneQueue", VAR_SERVER},
{"Interface", VAR_SERVER},
{"KeyExpire", VAR_SERVER},
+ {"ListenAddress", VAR_SERVER | VAR_MULTIPLE},
{"LocalDiscovery", VAR_SERVER},
{"MACExpire", VAR_SERVER},
{"MaxConnectionBurst", VAR_SERVER},
{"ScriptsInterpreter", VAR_SERVER},
{"StrictSubnets", VAR_SERVER},
{"TunnelServer", VAR_SERVER},
- {"UDPRcvBuf", VAR_SERVER},
- {"UDPSndBuf", VAR_SERVER},
{"VDEGroup", VAR_SERVER},
{"VDEPort", VAR_SERVER},
/* Host configuration */
}
// Open the right configuration file.
- char *filename;
+ char filename[PATH_MAX];
if(node)
- xasprintf(&filename, "%s" SLASH "%s", hosts_dir, node);
+ snprintf(filename,PATH_MAX "%s" SLASH "%s", hosts_dir, node);
else
filename = tinc_conf;
return 1;
}
- char *tmpfile = NULL;
+ char tmpfile[PATH_MAX];
FILE *tf = NULL;
if(action >= -1) {
- xasprintf(&tmpfile, "%s.config.tmp", filename);
+ snprintf(tmpfile,PATH_MAX, "%s.config.tmp", filename);
tf = fopen(tmpfile, "w");
if(!tf) {
fprintf(stderr, "Could not open temporary file %s: %s\n", tmpfile, strerror(errno));
if(action < 0 && !removed) {
remove(tmpfile);
fprintf(stderr, "No configuration variables deleted.\n");
- return *value;
+ return *value != 0;
}
// Replace the configuration file with the new one
for(int i = 0; i < 100; i++) {
int port = 0x1000 + (rand() & 0x7fff);
if(try_bind(port)) {
- char *filename;
- xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
+ char filename[PATH_MAX];
+ snprintf(filename,PATH_MAX "%s" SLASH "hosts" SLASH "%s", confbase, name);
FILE *f = fopen(filename, "a");
- free(filename);
if(!f) {
fprintf(stderr, "Please change tinc's Port manually.\n");
return 0;
} 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(strcmp(confdir, confbase) && mkdir(confdir, 0755) && errno != EEXIST) {
+ if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
return 1;
}
fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
return 1;
}
- 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
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;
+
+ 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);
+ }
+
+ return 0;
+}
+
static const struct {
const char *command;
int (*function)(int argc, char *argv[]);
{"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];
if(!parse_options(argc, argv))
return 1;
- make_names();
xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);