/*
tincctl.c -- Controlling a running tincd
- Copyright (C) 2007-2021 Guus Sliepen <guus@tinc-vpn.org>
+ Copyright (C) 2007-2022 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
bool tty = true;
bool confbasegiven = false;
char *scriptinterpreter = NULL;
-char *scriptextension = "";
+static char defaultextension[] = "";
+char *scriptextension = defaultextension;
static char *prompt;
char *device = NULL;
char *iface = NULL;
};
static void version(void) {
- printf("%s version %s (built %s %s, protocol %d.%d)\n", PACKAGE,
- BUILD_VERSION, BUILD_DATE, BUILD_TIME, PROT_MAJOR, PROT_MINOR);
- printf("Features:"
+ static const char *message =
+ "%s version %s (built %s %s, protocol %d.%d)\n"
+ "Features:"
#ifdef HAVE_READLINE
- " readline"
+ " readline"
#endif
#ifdef HAVE_CURSES
- " curses"
+ " curses"
#endif
#ifndef DISABLE_LEGACY
- " legacy_protocol"
+ " legacy_protocol"
#endif
- "\n\n");
- printf("Copyright (C) 1998-2018 Ivo Timmermans, Guus Sliepen and others.\n"
- "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"
- "see the file COPYING for details.\n");
+ "\n\n"
+ "Copyright (C) 1998-2018 Ivo Timmermans, Guus Sliepen and others.\n"
+ "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"
+ "see the file COPYING for details.\n";
+
+ printf(message, PACKAGE, BUILD_VERSION, BUILD_DATE, BUILD_TIME, PROT_MAJOR, PROT_MINOR);
}
static void usage(bool status) {
if(status) {
fprintf(stderr, "Try `%s --help\' for more information.\n", program_name);
} else {
- printf("Usage: %s [options] command\n\n", program_name);
- printf("Valid options are:\n"
- " -b, --batch Don't ask for anything (non-interactive mode).\n"
- " -c, --config=DIR Read configuration options from DIR.\n"
- " -n, --net=NETNAME Connect to net NETNAME.\n"
- " --pidfile=FILENAME Read control cookie from FILENAME.\n"
- " --force Force some commands to work despite warnings.\n"
- " --help Display this help and exit.\n"
- " --version Output version information and exit.\n"
- "\n"
- "Valid commands are:\n"
- " init [name] Create initial configuration files.\n"
- " get VARIABLE Print current value of VARIABLE\n"
- " set VARIABLE VALUE Set VARIABLE to VALUE\n"
- " add VARIABLE VALUE Add VARIABLE with the given VALUE\n"
- " del VARIABLE [VALUE] Remove VARIABLE [only ones with watching VALUE]\n"
- " start [tincd options] Start tincd.\n"
- " stop Stop tincd.\n"
- " restart [tincd options] Restart tincd.\n"
- " reload Partially reload configuration of running tincd.\n"
- " pid Show PID of currently running tincd.\n"
+ static const char *message =
+ "Usage: %s [options] command\n"
+ "\n"
+ "Valid options are:\n"
+ " -b, --batch Don't ask for anything (non-interactive mode).\n"
+ " -c, --config=DIR Read configuration options from DIR.\n"
+ " -n, --net=NETNAME Connect to net NETNAME.\n"
+ " --pidfile=FILENAME Read control cookie from FILENAME.\n"
+ " --force Force some commands to work despite warnings.\n"
+ " --help Display this help and exit.\n"
+ " --version Output version information and exit.\n"
+ "\n"
+ "Valid commands are:\n"
+ " init [name] Create initial configuration files.\n"
+ " get VARIABLE Print current value of VARIABLE\n"
+ " set VARIABLE VALUE Set VARIABLE to VALUE\n"
+ " add VARIABLE VALUE Add VARIABLE with the given VALUE\n"
+ " del VARIABLE [VALUE] Remove VARIABLE [only ones with watching VALUE]\n"
+ " start [tincd options] Start tincd.\n"
+ " stop Stop tincd.\n"
+ " restart [tincd options] Restart tincd.\n"
+ " reload Partially reload configuration of running tincd.\n"
+ " pid Show PID of currently running tincd.\n"
#ifdef DISABLE_LEGACY
- " generate-keys Generate a new Ed25519 public/private key pair.\n"
+ " generate-keys Generate a new Ed25519 public/private key pair.\n"
#else
- " generate-keys [bits] Generate new RSA and Ed25519 public/private key pairs.\n"
- " generate-rsa-keys [bits] Generate a new RSA public/private key pair.\n"
+ " generate-keys [bits] Generate new RSA and Ed25519 public/private key pairs.\n"
+ " generate-rsa-keys [bits] Generate a new RSA public/private key pair.\n"
#endif
- " generate-ed25519-keys Generate a new Ed25519 public/private key pair.\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"
- " subnets - all known subnets in the VPN\n"
- " connections - all meta connections with ourself\n"
- " [di]graph - graph of the VPN in dotty format\n"
- " invitations - outstanding invitations\n"
- " info NODE|SUBNET|ADDRESS Give information about a particular NODE, SUBNET or ADDRESS.\n"
- " purge Purge unreachable nodes\n"
- " debug N Set debug level\n"
- " retry Retry all outgoing connections\n"
- " disconnect NODE Close meta connection with NODE\n"
+ " generate-ed25519-keys Generate a new Ed25519 public/private key pair.\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"
+ " subnets - all known subnets in the VPN\n"
+ " connections - all meta connections with ourself\n"
+ " [di]graph - graph of the VPN in dotty format\n"
+ " invitations - outstanding invitations\n"
+ " info NODE|SUBNET|ADDRESS Give information about a particular NODE, SUBNET or ADDRESS.\n"
+ " purge Purge unreachable nodes\n"
+ " debug N Set debug level\n"
+ " retry Retry all outgoing connections\n"
+ " disconnect NODE Close meta connection with NODE\n"
#ifdef HAVE_CURSES
- " top Show real-time statistics\n"
+ " top Show real-time statistics\n"
#endif
- " pcap [snaplen] Dump traffic in pcap format [up to snaplen bytes per packet]\n"
- " log [level] Dump log output [up to the specified level]\n"
- " export Export host configuration of local node to standard output\n"
- " export-all Export all host configuration files to standard output\n"
- " import Import host configuration file(s) from standard input\n"
- " exchange Same as export followed by import\n"
- " exchange-all Same as export-all followed by import\n"
- " invite NODE [...] Generate an invitation for NODE\n"
- " join INVITATION Join a VPN using an INVITATION\n"
- " network [NETNAME] List all known networks, or switch to the one named NETNAME.\n"
- " fsck Check the configuration files for problems.\n"
- " sign [FILE] Generate a signed version of a file.\n"
- " verify NODE [FILE] Verify that a file was signed by the given NODE.\n"
- "\n");
- printf("Report bugs to tinc@tinc-vpn.org.\n");
+ " pcap [snaplen] Dump traffic in pcap format [up to snaplen bytes per packet]\n"
+ " log [level] Dump log output [up to the specified level]\n"
+ " export Export host configuration of local node to standard output\n"
+ " export-all Export all host configuration files to standard output\n"
+ " import Import host configuration file(s) from standard input\n"
+ " exchange Same as export followed by import\n"
+ " exchange-all Same as export-all followed by import\n"
+ " invite NODE [...] Generate an invitation for NODE\n"
+ " join INVITATION Join a VPN using an INVITATION\n"
+ " network [NETNAME] List all known networks, or switch to the one named NETNAME.\n"
+ " fsck Check the configuration files for problems.\n"
+ " sign [FILE] Generate a signed version of a file.\n"
+ " verify NODE [FILE] Verify that a file was signed by the given NODE.\n"
+ "\n"
+ "Report bugs to tinc@tinc-vpn.org.\n";
+
+ printf(message, program_name);
}
}
if(filename[0] != '/') {
#endif
+
/* The directory is a relative path or a filename. */
- getcwd(directory, sizeof(directory));
+ if(!getcwd(directory, sizeof(directory))) {
+ fprintf(stderr, "Could not get current directory: %s\n", strerror(errno));
+ return NULL;
+ }
if((size_t)snprintf(buf2, sizeof(buf2), "%s" SLASH "%s", directory, filename) >= sizeof(buf2)) {
fprintf(stderr, "Filename too long: %s" SLASH "%s\n", directory, filename);
return true;
}
-bool sendline(int fd, char *format, ...) {
+bool sendline(int fd, const char *format, ...) {
static char buffer[4096];
char *p = buffer;
ssize_t blen;
while(recvline(fd, line, sizeof(line))) {
int code, req;
- size_t len;
- int n = sscanf(line, "%d %d %zd", &code, &req, &len);
+ unsigned long len;
+ int n = sscanf(line, "%d %d %lu", &code, &req, &len);
gettimeofday(&tv, NULL);
if(n != 3 || code != CONTROL || req != REQ_PCAP || len > sizeof(data)) {
#endif
- char *default_c = "tincd";
-
if(slash++) {
xasprintf(&c, "%.*stincd", (int)(slash - program_name), program_name);
} else {
- c = default_c;
+ c = xstrdup("tincd");
}
int nargc = 0;
int status = spawnvp(_P_WAIT, c, nargv);
free(nargv);
-
- if(c != default_c) {
- free(c);
- }
+ free(c);
if(status == -1) {
fprintf(stderr, "Error starting %s: %s\n", c, strerror(errno));
if(socketpair(AF_UNIX, SOCK_STREAM, 0, pfd)) {
fprintf(stderr, "Could not create umbilical socket: %s\n", strerror(errno));
free(nargv);
-
- if(c != default_c) {
- free(c);
- }
-
+ free(c);
return 1;
}
if(pid == -1) {
fprintf(stderr, "Could not fork: %s\n", strerror(errno));
free(nargv);
-
- if(c != default_c) {
- free(c);
- }
-
+ free(c);
return 1;
}
len--;
}
- write(2, buf, len);
+ if(write(2, buf, len) != len) {
+ // Nothing we can do about it.
+ }
}
if(len) {
fprintf(stderr, "Error starting %s\n", c);
}
- if(c != default_c) {
- free(c);
- }
+ free(c);
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
#endif
while((ent = readdir(dir))) {
char buf[MAX_STRING_SIZE];
- if(b64decode(ent->d_name, buf, 24) != 18) {
+ if(b64decode_tinc(ent->d_name, buf, 24) != 18) {
continue;
}
char nexthop[4096];
int cipher, digest, maclength, compression, distance, socket, weight;
short int pmtu, minmtu, maxmtu;
- unsigned int options, status_int;
+ unsigned int options;
+ uint32_t status_int;
node_status_t status;
long int last_state_change;
int udp_ping_rtt;
switch(req) {
case REQ_DUMP_NODES: {
- int n = sscanf(line, "%*d %*d %4095s %4095s %4095s port %4095s %d %d %d %d %x %x %4095s %4095s %d %hd %hd %hd %ld %d %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_int, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change, &udp_ping_rtt, &in_packets, &in_bytes, &out_packets, &out_bytes);
+ int n = sscanf(line, "%*d %*d %4095s %4095s %4095s port %4095s %d %d %d %d %x %"PRIx32" %4095s %4095s %d %hd %hd %hd %ld %d %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, node, id, host, port, &cipher, &digest, &maclength, &compression, &options, &status_int, nexthop, via, &distance, &pmtu, &minmtu, &maxmtu, &last_state_change, &udp_ping_rtt, &in_packets, &in_bytes, &out_packets, &out_bytes);
if(n != 22) {
fprintf(stderr, "Unable to parse node dump from tincd: %s\n", line);
fprintf(stderr, "Warning: could not bind to port 655. ");
for(int i = 0; i < 100; i++) {
- int port = 0x1000 + (rand() & 0x7fff);
+ uint16_t port = 0x1000 + prng(0x8000);
if(try_bind(port)) {
char filename[PATH_MAX];
return 1;
}
- b64encode(sig, sig, 64);
+ b64encode_tinc(sig, sig, 64);
ecdsa_free(key);
fprintf(stdout, "Signature = %s %ld %s\n", name, t, sig);
fclose(fp);
- if(b64decode(sig, sig, 86) != 64 || !ecdsa_verify(key, newline, len + trailer_len - (newline - data), sig)) {
+ if(b64decode_tinc(sig, sig, 86) != 64 || !ecdsa_verify(key, newline, len + trailer_len - (newline - data), sig)) {
fprintf(stderr, "Invalid signature\n");
free(data);
ecdsa_free(key);
#ifdef HAVE_READLINE
rl_readline_name = "tinc";
+ rl_basic_word_break_characters = "\t\n ";
rl_completion_entry_function = complete_nothing;
rl_attempted_completion_function = completion;
rl_filename_completion_desired = 0;
if(tty) {
free(copy);
free(line);
- rl_basic_word_break_characters = "\t\n ";
line = readline(prompt);
copy = line ? xstrdup(line) : NULL;
} else {
return result;
}
-static void cleanup() {
+static void cleanup(void) {
free(tinc_conf);
free(hosts_dir);
free_names();
#endif
gettimeofday(&now, NULL);
- srand(now.tv_sec + now.tv_usec);
crypto_init();
+ prng_init();
if(optind >= argc) {
return cmd_shell(argc, argv);