X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ftincctl.c;h=ec4346969f04f9d09f26f52e3f75f9a6432add3d;hb=7ab400aebdc38e7ee5dafc0a2291bbbea25e3f7c;hp=b2062b2e64d9441b1992991be9a54f9768a1154c;hpb=469a261b7547f07ab26f46eb3774beca17c75cd2;p=tinc diff --git a/src/tincctl.c b/src/tincctl.c index b2062b2e..ec434696 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -1,6 +1,6 @@ /* tincctl.c -- Controlling a running tincd - Copyright (C) 2007-2018 Guus Sliepen + Copyright (C) 2007-2021 Guus Sliepen 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 @@ -40,6 +40,7 @@ #include "tincctl.h" #include "top.h" #include "version.h" +#include "subnet.h" #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 @@ -92,6 +93,17 @@ static struct option const long_options[] = { 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:" +#ifdef HAVE_READLINE + " readline" +#endif +#ifdef HAVE_CURSES + " curses" +#endif +#ifndef DISABLE_LEGACY + " 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" @@ -125,12 +137,12 @@ static void usage(bool status) { " 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 keypair.\n" + " generate-keys Generate a new Ed25519 public/private key pair.\n" #else - " 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-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 keypair.\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" @@ -178,11 +190,13 @@ static bool parse_options(int argc, char **argv) { break; case 'c': /* config file */ + free(confbase); confbase = xstrdup(optarg); confbasegiven = true; break; case 'n': /* net name given */ + free(netname); netname = xstrdup(optarg); break; @@ -195,6 +209,7 @@ static bool parse_options(int argc, char **argv) { break; case 3: /* open control socket here */ + free(pidfilename); pidfilename = xstrdup(optarg); break; @@ -204,6 +219,7 @@ static bool parse_options(int argc, char **argv) { case '?': /* wrong options */ usage(true); + free_names(); return false; default: @@ -224,6 +240,7 @@ static bool parse_options(int argc, char **argv) { if(netname && (strpbrk(netname, "\\/") || *netname == '.')) { fprintf(stderr, "Invalid character in netname!\n"); + free_names(); return false; } @@ -237,7 +254,7 @@ static bool parse_options(int argc, char **argv) { FILE *fopenmask(const char *filename, const char *mode, mode_t perms) { mode_t mask = umask(0); perms &= ~mask; - umask(~perms); + umask(~perms & 0777); FILE *f = fopen(filename, mode); if(!f) { @@ -262,19 +279,21 @@ static void disable_old_keys(const char *filename, const char *what) { bool disabled = false; bool block = false; bool error = false; - FILE *r, *w; - r = fopen(filename, "r"); + FILE *r = fopen(filename, "r"); + FILE *w = NULL; if(!r) { return; } - snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename); + int result = snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename); - struct stat st = {.st_mode = 0600}; - fstat(fileno(r), &st); - w = fopenmask(tmpfile, "w", st.st_mode); + if(result < sizeof(tmpfile)) { + 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)) { @@ -416,7 +435,7 @@ ask_filename: } /* - Generate a public/private Ed25519 keypair, and ask for a file to store + Generate a public/private Ed25519 key pair, and ask for a file to store them in. */ static bool ed25519_keygen(bool ask) { @@ -424,7 +443,7 @@ static bool ed25519_keygen(bool ask) { FILE *f; char fname[PATH_MAX]; - fprintf(stderr, "Generating Ed25519 keypair:\n"); + fprintf(stderr, "Generating Ed25519 key pair:\n"); if(!(key = ecdsa_generate())) { fprintf(stderr, "Error during key generation!\n"); @@ -480,7 +499,7 @@ error: #ifndef DISABLE_LEGACY /* - Generate a public/private RSA keypair, and ask for a file to store + Generate a public/private RSA key pair, and ask for a file to store them in. */ static bool rsa_keygen(int bits, bool ask) { @@ -725,6 +744,24 @@ static void logcontrol(int fd, FILE *out, int level) { } } +static bool stop_tincd(void) { + if(!connect_tincd(true)) { + return false; + } + + sendline(fd, "%d %d", CONTROL, REQ_STOP); + + while(recvline(fd, line, sizeof(line))) { + // wait for tincd to close the connection... + } + + close(fd); + pid = 0; + fd = -1; + + return true; +} + #ifdef HAVE_MINGW static bool remove_service(void) { SC_HANDLE manager = NULL; @@ -742,7 +779,12 @@ static bool remove_service(void) { service = OpenService(manager, identname, SERVICE_ALL_ACCESS); if(!service) { - fprintf(stderr, "Could not open %s service: %s\n", identname, winerror(GetLastError())); + if(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) { + success = stop_tincd(); + } else { + fprintf(stderr, "Could not open %s service: %s\n", identname, winerror(GetLastError())); + } + goto exit; } @@ -827,14 +869,17 @@ bool connect_tincd(bool verbose) { return false; } - struct sockaddr_un sa; + struct sockaddr_un sa = { + .sun_family = AF_UNIX, + }; - sa.sun_family = AF_UNIX; + if(strlen(unixsocketname) >= sizeof(sa.sun_path)) { + fprintf(stderr, "UNIX socket filename %s is too long!", unixsocketname); + return false; + } strncpy(sa.sun_path, unixsocketname, sizeof(sa.sun_path)); - sa.sun_path[sizeof(sa.sun_path) - 1] = 0; - fd = socket(AF_UNIX, SOCK_STREAM, 0); if(fd < 0) { @@ -883,7 +928,6 @@ bool connect_tincd(bool verbose) { return false; } -#ifdef HAVE_MINGW unsigned long arg = 0; if(ioctlsocket(fd, FIONBIO, &arg) != 0) { @@ -892,8 +936,6 @@ bool connect_tincd(bool verbose) { } } -#endif - if(connect(fd, res->ai_addr, res->ai_addrlen) < 0) { if(verbose) { fprintf(stderr, "Cannot connect to %s port %s: %s\n", host, port, sockstrerror(sockerrno)); @@ -963,10 +1005,12 @@ static int cmd_start(int argc, char *argv[]) { #endif + char *default_c = "tincd"; + if(slash++) { xasprintf(&c, "%.*stincd", (int)(slash - program_name), program_name); } else { - c = "tincd"; + c = default_c; } int nargc = 0; @@ -997,6 +1041,12 @@ static int cmd_start(int argc, char *argv[]) { #ifdef HAVE_MINGW int status = spawnvp(_P_WAIT, c, nargv); + free(nargv); + + if(c != default_c) { + free(c); + } + if(status == -1) { fprintf(stderr, "Error starting %s: %s\n", c, strerror(errno)); return 1; @@ -1009,6 +1059,11 @@ static int cmd_start(int argc, char *argv[]) { 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); + } + return 1; } @@ -1017,6 +1072,11 @@ static int cmd_start(int argc, char *argv[]) { if(pid == -1) { fprintf(stderr, "Could not fork: %s\n", strerror(errno)); free(nargv); + + if(c != default_c) { + free(c); + } + return 1; } @@ -1066,12 +1126,17 @@ static int cmd_start(int argc, char *argv[]) { signal(SIGINT, SIG_DFL); #endif - if(failure || result != pid || !WIFEXITED(status) || WEXITSTATUS(status)) { + bool failed = failure || result != pid || !WIFEXITED(status) || WEXITSTATUS(status); + + if(failed) { fprintf(stderr, "Error starting %s\n", c); - return 1; } - return 0; + if(c != default_c) { + free(c); + } + + return failed ? EXIT_FAILURE : EXIT_SUCCESS; #endif } @@ -1083,9 +1148,11 @@ static int cmd_stop(int argc, char *argv[]) { return 1; } -#ifndef HAVE_MINGW +#ifdef HAVE_MINGW + return remove_service() ? EXIT_SUCCESS : EXIT_FAILURE; +#else - if(!connect_tincd(true)) { + if(!stop_tincd()) { if(pid) { if(kill(pid, SIGTERM)) { fprintf(stderr, "Could not send TERM signal to process with PID %d: %s\n", pid, strerror(errno)); @@ -1100,24 +1167,8 @@ static int cmd_stop(int argc, char *argv[]) { return 1; } - sendline(fd, "%d %d", CONTROL, REQ_STOP); - - while(recvline(fd, line, sizeof(line))) { - // Wait for tincd to close the connection... - } - -#else - - if(!remove_service()) { - return 1; - } - -#endif - close(fd); - pid = 0; - fd = -1; - return 0; +#endif } static int cmd_restart(int argc, char *argv[]) { @@ -1346,7 +1397,7 @@ static int cmd_dump(int argc, char *argv[]) { color = "green"; } - printf(" %s [label = \"%s\", color = \"%s\"%s];\n", node, node, color, strcmp(host, "MYSELF") ? "" : ", style = \"filled\""); + printf(" \"%s\" [label = \"%s\", color = \"%s\"%s];\n", node, node, color, strcmp(host, "MYSELF") ? "" : ", style = \"filled\""); } else { if(only_reachable && !status.reachable) { continue; @@ -1376,9 +1427,9 @@ static int cmd_dump(int argc, char *argv[]) { float w = 1 + 65536.0 / weight; if(do_graph == 1 && strcmp(node1, node2) > 0) { - printf(" %s -- %s [w = %f, weight = %f];\n", node1, node2, w, w); + printf(" \"%s\" -- \"%s\" [w = %f, weight = %f];\n", node1, node2, w, w); } else if(do_graph == 2) { - printf(" %s -> %s [w = %f, weight = %f];\n", node1, node2, w, w); + printf(" \"%s\" -> \"%s\" [w = %f, weight = %f];\n", node1, node2, w, w); } } else { 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); @@ -1833,7 +1884,7 @@ static int cmd_config(int argc, char *argv[]) { char *node = NULL; char *variable; char *value; - int len; + size_t len; len = strcspn(line, "\t ="); value = line + len; @@ -1880,6 +1931,19 @@ static int cmd_config(int argc, char *argv[]) { found = true; variable = (char *)variables[i].name; + if(!strcasecmp(variable, "Subnet")) { + subnet_t s = {0}; + + if(!str2net(&s, value)) { + fprintf(stderr, "Malformed subnet definition %s\n", value); + } + + if(!subnetcheck(s)) { + fprintf(stderr, "Network address and prefix length do not match: %s\n", value); + return 1; + } + } + /* Discourage use of obsolete variables. */ if(variables[i].type & VAR_OBSOLETE && action >= 0) { @@ -1927,6 +1991,11 @@ static int cmd_config(int argc, char *argv[]) { if(node && !check_id(node)) { fprintf(stderr, "Invalid name for node.\n"); + + if(node != line) { + free(node); + } + return 1; } @@ -1935,6 +2004,11 @@ static int cmd_config(int argc, char *argv[]) { fprintf(stderr, "Warning: %s is not a known configuration variable!\n", variable); } else { fprintf(stderr, "%s: is not a known configuration variable! Use --force to use it anyway.\n", variable); + + if(node && node != line) { + free(node); + } + return 1; } } @@ -1944,6 +2018,11 @@ static int cmd_config(int argc, char *argv[]) { if(node) { snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, node); + + if(node != line) { + free(node); + node = NULL; + } } else { snprintf(filename, sizeof(filename), "%s", tinc_conf); } @@ -3303,6 +3382,11 @@ static int cmd_shell(int argc, char *argv[]) { return result; } +static void cleanup() { + free(tinc_conf); + free(hosts_dir); + free_names(); +} int main(int argc, char *argv[]) { program_name = argv[0]; @@ -3317,6 +3401,7 @@ int main(int argc, char *argv[]) { make_names(false); xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase); xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase); + atexit(cleanup); if(show_version) { version(); @@ -3338,7 +3423,8 @@ int main(int argc, char *argv[]) { #endif - srand(time(NULL)); + gettimeofday(&now, NULL); + srand(now.tv_sec + now.tv_usec); crypto_init(); if(optind >= argc) {