/*
tincctl.c -- Controlling a running tincd
- Copyright (C) 2007-2018 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
#include "system.h"
-#include <getopt.h>
-
#ifdef HAVE_READLINE
#include "readline/readline.h"
#include "readline/history.h"
#include "top.h"
#include "version.h"
#include "subnet.h"
+#include "keys.h"
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
static char **orig_argv;
-static int orig_argc;
/* If nonzero, display usage information and exit. */
static bool show_help = false;
bool force = false;
bool tty = true;
bool confbasegiven = false;
-bool netnamegiven = 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("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");
+ static const char *message =
+ "%s version %s (built %s %s, protocol %d.%d)\n"
+ "Features:"
+#ifdef HAVE_READLINE
+ " readline"
+#endif
+#ifdef HAVE_CURSES
+ " curses"
+#endif
+#ifndef DISABLE_LEGACY
+ " legacy_protocol"
+#endif
+ "\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 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"
- " 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);
}
}
break;
case 'c': /* config file */
+ free(confbase);
confbase = xstrdup(optarg);
confbasegiven = true;
break;
case 'n': /* net name given */
+ free(netname);
netname = xstrdup(optarg);
break;
break;
case 3: /* open control socket here */
+ free(pidfilename);
pidfilename = xstrdup(optarg);
break;
case '?': /* wrong options */
usage(true);
+ free_names();
return false;
default:
if(netname && (strpbrk(netname, "\\/") || *netname == '.')) {
fprintf(stderr, "Invalid character in netname!\n");
+ free_names();
return false;
}
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);
-
- if(!f) {
- fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
- return NULL;
- }
-
-#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];
- bool disabled = false;
- bool block = false;
- bool error = false;
- FILE *r, *w;
-
- r = fopen(filename, "r");
-
- if(!r) {
- return;
- }
-
- snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename);
-
- 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, " ED25519 ") && strstr(what, "Ed25519")) || (strstr(buf, " RSA ") && strstr(what, "RSA"))) {
- disabled = true;
- block = true;
- }
- }
-
- bool ed25519pubkey = !strncasecmp(buf, "Ed25519PublicKey", 16) && strchr(" \t=", buf[16]) && strstr(what, "Ed25519");
-
- if(ed25519pubkey) {
- disabled = true;
- }
-
- if(w) {
- if(block || ed25519pubkey) {
- fputc('#', w);
- }
-
- if(fputs(buf, w) < 0) {
- error = true;
- break;
- }
- }
-
- if(block && !strncmp(buf, "-----END ", 9)) {
- block = false;
- }
- }
-
- if(w)
- if(fclose(w) < 0) {
- error = true;
- }
-
- if(ferror(r) || fclose(r) < 0) {
- error = true;
- }
-
- if(disabled) {
- if(!w || error) {
- fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
-
- if(w) {
- unlink(tmpfile);
- }
-
- return;
- }
-
-#ifdef HAVE_MINGW
- // We cannot atomically replace files on Windows.
- char bakfile[PATH_MAX] = "";
- snprintf(bakfile, sizeof(bakfile), "%s.bak", filename);
-
- if(rename(filename, bakfile) || rename(tmpfile, filename)) {
- rename(bakfile, filename);
-#else
-
- if(rename(tmpfile, filename)) {
-#endif
- fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
- } else {
-#ifdef HAVE_MINGW
- unlink(bakfile);
-#endif
- fprintf(stderr, "Warning: old key(s) found and disabled.\n");
- }
- }
-
- unlink(tmpfile);
-}
-
static FILE *ask_and_open(const char *filename, const char *what, const char *mode, bool ask, mode_t perms) {
FILE *r;
char directory[PATH_MAX] = ".";
}
}
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) {
#else
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);
}
/*
- 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) {
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");
#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) {
}
while(!(newline = memchr(buffer, '\n', blen))) {
- int result = recv(fd, buffer + blen, sizeof(buffer) - blen, 0);
+ ssize_t nrecv = recv(fd, buffer + blen, sizeof(buffer) - blen, 0);
- if(result == -1 && sockerrno == EINTR) {
+ if(nrecv == -1 && sockerrno == EINTR) {
continue;
- } else if(result <= 0) {
+ } else if(nrecv <= 0) {
return false;
}
- blen += result;
+ blen += nrecv;
}
if((size_t)(newline - buffer) >= len) {
static bool recvdata(int fd, char *data, size_t len) {
while(blen < len) {
- int result = recv(fd, buffer + blen, sizeof(buffer) - blen, 0);
+ ssize_t nrecv = recv(fd, buffer + blen, sizeof(buffer) - blen, 0);
- if(result == -1 && sockerrno == EINTR) {
+ if(nrecv == -1 && sockerrno == EINTR) {
continue;
- } else if(result <= 0) {
+ } else if(nrecv <= 0) {
return false;
}
- blen += result;
+ blen += nrecv;
}
memcpy(data, buffer, len);
return true;
}
-bool sendline(int fd, char *format, ...) {
+bool sendline(int fd, const char *format, ...) {
static char buffer[4096];
char *p = buffer;
- int blen;
+ ssize_t blen;
va_list ap;
va_start(ap, format);
blen++;
while(blen) {
- int result = send(fd, p, blen, MSG_NOSIGNAL);
+ ssize_t nsend = send(fd, p, blen, MSG_NOSIGNAL);
- if(result == -1 && sockerrno == EINTR) {
+ if(nsend == -1 && sockerrno == EINTR) {
continue;
- } else if(result <= 0) {
+ } else if(nsend <= 0) {
return false;
}
- p += result;
- blen -= result;
+ p += nsend;
+ blen -= nsend;
}
return true;
char line[32];
while(recvline(fd, line, sizeof(line))) {
- int code, req, len;
- int n = sscanf(line, "%d %d %d", &code, &req, &len);
+ int code, req;
+ 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 < 0 || (size_t)len > sizeof(data)) {
+ if(n != 3 || code != CONTROL || req != REQ_PCAP || len > sizeof(data)) {
break;
}
// wait for tincd to close the connection...
}
- close(fd);
+ closesocket(fd);
pid = 0;
fd = -1;
return true;
}
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
static bool remove_service(void) {
SC_HANDLE manager = NULL;
SC_HANDLE service = NULL;
if(select(fd + 1, &r, NULL, NULL, &tv)) {
fprintf(stderr, "Previous connection to tincd lost, reconnecting.\n");
- close(fd);
+ closesocket(fd);
fd = -1;
} else {
return true;
fclose(f);
-#ifndef HAVE_MINGW
+#ifndef HAVE_WINDOWS
if((pid == 0) || (kill(pid, 0) && (errno == ESRCH))) {
fprintf(stderr, "Could not find tincd running at pid %d\n", pid);
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) {
fprintf(stderr, "Cannot connect to UNIX socket %s: %s\n", unixsocketname, sockstrerror(sockerrno));
}
- close(fd);
+ closesocket(fd);
fd = -1;
return false;
}
fprintf(stderr, "Cannot connect to %s port %s: %s\n", host, port, sockstrerror(sockerrno));
}
- close(fd);
+ closesocket(fd);
fd = -1;
return false;
}
fprintf(stderr, "Cannot read greeting from control socket: %s\n", sockstrerror(sockerrno));
}
- close(fd);
+ closesocket(fd);
fd = -1;
return false;
}
fprintf(stderr, "Could not fully establish control socket connection\n");
}
- close(fd);
+ closesocket(fd);
fd = -1;
return false;
}
char *c;
char *slash = strrchr(program_name, '/');
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
if((c = strrchr(program_name, '\\')) > slash) {
slash = c;
if(slash++) {
xasprintf(&c, "%.*stincd", (int)(slash - program_name), program_name);
} else {
- c = "tincd";
+ c = xstrdup("tincd");
}
int nargc = 0;
char **nargv = xzalloc((optind + argc) * sizeof(*nargv));
char *arg0 = c;
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
/*
Windows has no real concept of an "argv array". A command line is just one string.
The CRT of the new process will decode the command line string to generate argv before calling main(), and (by convention)
nargv[nargc++] = argv[i];
}
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
int status = spawnvp(_P_WAIT, c, nargv);
+ free(nargv);
+ free(c);
+
if(status == -1) {
fprintf(stderr, "Error starting %s: %s\n", c, strerror(errno));
return 1;
if(socketpair(AF_UNIX, SOCK_STREAM, 0, pfd)) {
fprintf(stderr, "Could not create umbilical socket: %s\n", strerror(errno));
free(nargv);
+ free(c);
return 1;
}
if(pid == -1) {
fprintf(stderr, "Could not fork: %s\n", strerror(errno));
free(nargv);
+ free(c);
return 1;
}
free(nargv);
- int status = -1, result;
#ifdef SIGINT
signal(SIGINT, SIG_IGN);
#endif
// Pass all log messages from the umbilical to stderr.
// A nul-byte right before closure means tincd started successfully.
bool failure = true;
- char buf[1024];
+ uint8_t buf[1024];
ssize_t len;
while((len = read(pfd[0], buf, sizeof(buf))) > 0) {
len--;
}
- write(2, buf, len);
+ if(write(2, buf, len) != len) {
+ // Nothing we can do about it.
+ }
}
if(len) {
close(pfd[0]);
// Make sure the child process is really gone.
- result = waitpid(pid, &status, 0);
+ int status = -1;
+ pid_t result = waitpid(pid, &status, 0);
#ifdef SIGINT
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;
+ free(c);
+
+ return failed ? EXIT_FAILURE : EXIT_SUCCESS;
#endif
}
return 1;
}
-#ifdef HAVE_MINGW
- return remove_service();
+#ifdef HAVE_WINDOWS
+ return remove_service() ? EXIT_SUCCESS : EXIT_FAILURE;
#else
if(!stop_tincd()) {
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;
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.value, 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);
return 1;
}
- memcpy(&status, &status_int, sizeof(status));
-
if(do_graph) {
const char *color = "black";
}
printf("%s id %s at %s port %s cipher %d digest %d maclength %d compression %d options %x status %04x nexthop %s via %s distance %d pmtu %d (min %d max %d) rx %"PRIu64" %"PRIu64" tx %"PRIu64" %"PRIu64,
- node, id, host, port, cipher, digest, maclength, compression, options, status_int, nexthop, via, distance, pmtu, minmtu, maxmtu, in_packets, in_bytes, out_packets, out_bytes);
+ node, id, host, port, cipher, digest, maclength, compression, options, status.value, nexthop, via, distance, pmtu, minmtu, maxmtu, in_packets, in_bytes, out_packets, out_bytes);
if(udp_ping_rtt != -1) {
printf(" rtt %d.%03d", udp_ping_rtt / 1000, udp_ping_rtt % 1000);
}
if(do_graph) {
- float w = 1 + 65536.0 / weight;
+ float w = 1.0f + 65536.0f / (float)weight;
if(do_graph == 1 && strcmp(node1, node2) > 0) {
printf(" \"%s\" -- \"%s\" [w = %f, weight = %f];\n", node1, node2, w, w);
break;
case REQ_DUMP_CONNECTIONS: {
- int n = sscanf(line, "%*d %*d %4095s %4095s port %4095s %x %d %x", node, host, port, &options, &socket, &status_int);
+ int n = sscanf(line, "%*d %*d %4095s %4095s port %4095s %x %d %x", node, host, port, &options, &socket, &status.value);
if(n != 6) {
fprintf(stderr, "Unable to parse connection dump from tincd.\n");
return 1;
}
- printf("%s at %s port %s options %x socket %d status %x\n", node, host, port, options, socket, status_int);
+ printf("%s at %s port %s options %x socket %d status %x\n", node, host, port, options, socket, status.value);
}
break;
signal(SIGINT, SIG_DFL);
#endif
- close(fd);
+ closesocket(fd);
fd = -1;
return 0;
}
return 0;
}
-int rstrip(char *value) {
- int len = strlen(value);
+size_t rstrip(char *value) {
+ size_t len = strlen(value);
while(len && strchr("\t\r\n ", value[len - 1])) {
value[--len] = 0;
char *value;
while(fgets(buf, sizeof(buf), f)) {
- int len = strcspn(buf, "\t =");
+ size_t len = strcspn(buf, "\t =");
value = buf + len;
value += strspn(value, "\t ");
char *value;
while(fgets(buf, sizeof(buf), f)) {
- int len = strcspn(buf, "\t =");
+ size_t len = strcspn(buf, "\t =");
value = buf + len;
value += strspn(value, "\t ");
char *node = NULL;
char *variable;
char *value;
- int len;
+ size_t len;
len = strcspn(line, "\t =");
value = line + len;
found = true;
variable = (char *)variables[i].name;
- if(!strcasecmp(variable, "Subnet")) {
+ if(!strcasecmp(variable, "Subnet") && *value) {
subnet_t s = {0};
if(!str2net(&s, value)) {
fprintf(stderr, "Malformed subnet definition %s\n", value);
+ return 1;
}
if(!subnetcheck(s)) {
if(node && !check_id(node)) {
fprintf(stderr, "Invalid name for node.\n");
+
+ if(node != line) {
+ free(node);
+ }
+
return 1;
}
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;
}
}
char filename[PATH_MAX];
if(node) {
- snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, node);
+ if((size_t)snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, node) >= sizeof(filename)) {
+ fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, node);
+ free(node);
+ return 1;
+ }
+
+ if(node != line) {
+ free(node);
+ node = NULL;
+ }
} else {
snprintf(filename, sizeof(filename), "%s", tinc_conf);
}
// Parse line in a simple way
char *bvalue;
- int len;
- len = strcspn(buf2, "\t =");
+ size_t len = strcspn(buf2, "\t =");
bvalue = buf2 + len;
bvalue += strspn(bvalue, "\t ");
}
// Replace the configuration file with the new one
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
if(remove(filename)) {
fprintf(stderr, "Error replacing file %s: %s\n", filename, strerror(errno));
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;
}
- int len = rstrip(buf);
+ size_t len = rstrip(buf);
if(!len) {
fprintf(stderr, "No name given!\n");
check_port(name);
-#ifndef HAVE_MINGW
+#ifndef HAVE_WINDOWS
char filename[PATH_MAX];
snprintf(filename, sizeof(filename), "%s" SLASH "tinc-up", confbase);
}
char *command;
-#ifndef HAVE_MINGW
+#ifndef HAVE_WINDOWS
const char *editor = getenv("VISUAL");
if(!editor) {
}
if(fd >= 0) {
- close(fd);
+ closesocket(fd);
fd = -1;
}
long t = time(NULL);
char *trailer;
xasprintf(&trailer, " %s %ld", name, t);
- int trailer_len = strlen(trailer);
+ size_t trailer_len = strlen(trailer);
data = xrealloc(data, len + trailer_len);
memcpy(data + len, trailer, trailer_len);
return 1;
}
- b64encode(sig, sig, 64);
+ b64encode_tinc(sig, sig, 64);
ecdsa_free(key);
fprintf(stdout, "Signature = %s %ld %s\n", name, t, sig);
char *trailer;
xasprintf(&trailer, " %s %ld", signer, t);
- int trailer_len = strlen(trailer);
+ size_t trailer_len = strlen(trailer);
data = xrealloc(data, len + trailer_len);
memcpy(data + len, trailer, trailer_len);
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(void) {
+ free(tinc_conf);
+ free(hosts_dir);
+ free_names();
+}
int main(int argc, char *argv[]) {
program_name = argv[0];
orig_argv = argv;
- orig_argc = argc;
tty = isatty(0) && isatty(1);
if(!parse_options(argc, 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();
return 0;
}
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
static struct WSAData wsa_state;
if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
#endif
- srand(time(NULL));
+ gettimeofday(&now, NULL);
crypto_init();
+ prng_init();
if(optind >= argc) {
return cmd_shell(argc, argv);