X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ftincctl.c;h=4a3682f3894d7cdcbb4371faf07b78c1f24eede8;hb=0289162552cd85375605044c696e2a3294e7aa9a;hp=a3978bcc0767ea2b7ebf32752efe75a7e2b80317;hpb=a85198d20f24cd4e20ec5c57e402611fef8b137a;p=tinc diff --git a/src/tincctl.c b/src/tincctl.c index a3978bcc..4a3682f3 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -19,8 +19,6 @@ #include "system.h" -#include - #ifdef HAVE_READLINE #include "readline/readline.h" #include "readline/history.h" @@ -42,6 +40,8 @@ #include "version.h" #include "subnet.h" #include "keys.h" +#include "random.h" +#include "pidfile.h" #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 @@ -283,7 +283,7 @@ ask_filename: } } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS if(filename[0] != '\\' && filename[0] != '/' && !strchr(filename, ':')) { #else @@ -646,14 +646,14 @@ static bool stop_tincd(void) { // 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; @@ -719,16 +719,16 @@ bool connect_tincd(bool verbose) { 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; } } - FILE *f = fopen(pidfilename, "r"); + pidfile_t *pidfile = read_pidfile(); - if(!f) { + if(!pidfile) { if(verbose) { fprintf(stderr, "Could not open pid file %s: %s\n", pidfilename, strerror(errno)); } @@ -736,21 +736,11 @@ bool connect_tincd(bool verbose) { return false; } - char host[129]; - char port[129]; - - if(fscanf(f, "%20d %1024s %128s port %128s", &pid, controlcookie, host, port) != 4) { - if(verbose) { - fprintf(stderr, "Could not parse pid file %s\n", pidfilename); - } - - fclose(f); - return false; - } - - fclose(f); + pid = pidfile->pid; + strcpy(controlcookie, pidfile->cookie); -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS + free(pidfile); if((pid == 0) || (kill(pid, 0) && (errno == ESRCH))) { fprintf(stderr, "Could not find tincd running at pid %d\n", pid); @@ -786,7 +776,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Cannot connect to UNIX socket %s: %s\n", unixsocketname, sockstrerror(sockerrno)); } - close(fd); + closesocket(fd); fd = -1; return false; } @@ -801,11 +791,12 @@ bool connect_tincd(bool verbose) { struct addrinfo *res = NULL; - if(getaddrinfo(host, port, &hints, &res) || !res) { + if(getaddrinfo(pidfile->host, pidfile->port, &hints, &res) || !res) { if(verbose) { - fprintf(stderr, "Cannot resolve %s port %s: %s\n", host, port, sockstrerror(sockerrno)); + fprintf(stderr, "Cannot resolve %s port %s: %s\n", pidfile->host, pidfile->port, sockstrerror(sockerrno)); } + free(pidfile); return false; } @@ -816,6 +807,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Cannot create TCP socket: %s\n", sockstrerror(sockerrno)); } + free(pidfile); return false; } @@ -829,14 +821,16 @@ bool connect_tincd(bool verbose) { 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)); + fprintf(stderr, "Cannot connect to %s port %s: %s\n", pidfile->host, pidfile->port, sockstrerror(sockerrno)); } - close(fd); + free(pidfile); + closesocket(fd); fd = -1; return false; } + free(pidfile); freeaddrinfo(res); #endif @@ -855,7 +849,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Cannot read greeting from control socket: %s\n", sockstrerror(sockerrno)); } - close(fd); + closesocket(fd); fd = -1; return false; } @@ -865,7 +859,7 @@ bool connect_tincd(bool verbose) { fprintf(stderr, "Could not fully establish control socket connection\n"); } - close(fd); + closesocket(fd); fd = -1; return false; } @@ -888,7 +882,7 @@ static int cmd_start(int argc, char *argv[]) { char *c; char *slash = strrchr(program_name, '/'); -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS if((c = strrchr(program_name, '\\')) > slash) { slash = c; @@ -906,7 +900,7 @@ static int cmd_start(int argc, char *argv[]) { 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) @@ -927,7 +921,7 @@ static int cmd_start(int argc, char *argv[]) { nargv[nargc++] = argv[i]; } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS int status = spawnvp(_P_WAIT, c, nargv); free(nargv); @@ -1026,7 +1020,7 @@ static int cmd_stop(int argc, char *argv[]) { return 1; } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS return remove_service() ? EXIT_SUCCESS : EXIT_FAILURE; #else @@ -1530,7 +1524,7 @@ static int cmd_log(int argc, char *argv[]) { signal(SIGINT, SIG_DFL); #endif - close(fd); + closesocket(fd); fd = -1; return 0; } @@ -1721,6 +1715,20 @@ const var_t variables[] = { {NULL, 0} }; +// Request actual port from tincd +static bool read_actual_port(void) { + pidfile_t *pidfile = read_pidfile(); + + if(pidfile) { + printf("%s\n", pidfile->port); + free(pidfile); + return true; + } else { + fprintf(stderr, "Could not get port from the pidfile.\n"); + return false; + } +} + static int cmd_config(int argc, char *argv[]) { if(argc < 2) { fprintf(stderr, "Invalid number of arguments.\n"); @@ -1731,16 +1739,17 @@ static int cmd_config(int argc, char *argv[]) { argv--, argc++; } - int action = -2; + typedef enum { GET, DEL, SET, ADD } action_t; + action_t action = GET; if(!strcasecmp(argv[1], "get")) { argv++, argc--; } else if(!strcasecmp(argv[1], "add")) { - argv++, argc--, action = 1; + argv++, argc--, action = ADD; } else if(!strcasecmp(argv[1], "del")) { - argv++, argc--, action = -1; + argv++, argc--, action = DEL; } else if(!strcasecmp(argv[1], "replace") || !strcasecmp(argv[1], "set") || !strcasecmp(argv[1], "change")) { - argv++, argc--, action = 0; + argv++, argc--, action = SET; } if(argc < 2) { @@ -1786,13 +1795,18 @@ static int cmd_config(int argc, char *argv[]) { return 1; } - if(action >= 0 && !*value) { + if((action == SET || action == ADD) && !*value) { fprintf(stderr, "No value for variable given.\n"); return 1; } - if(action < -1 && *value) { - action = 0; + if(action == GET && *value) { + action = SET; + } + + // If port is requested, try reading it from the pidfile and fall back to configs if that fails + if(action == GET && !strcasecmp(variable, "Port") && read_actual_port()) { + return 0; } /* Some simple checks. */ @@ -1823,7 +1837,7 @@ static int cmd_config(int argc, char *argv[]) { /* Discourage use of obsolete variables. */ - if(variables[i].type & VAR_OBSOLETE && action >= 0) { + if(variables[i].type & VAR_OBSOLETE && (action == SET || action == ADD)) { if(force) { fprintf(stderr, "Warning: %s is an obsolete variable!\n", variable); } else { @@ -1834,7 +1848,7 @@ static int cmd_config(int argc, char *argv[]) { /* Don't put server variables in host config files */ - if(node && !(variables[i].type & VAR_HOST) && action >= 0) { + if(node && !(variables[i].type & VAR_HOST) && (action == SET || action == ADD)) { if(force) { fprintf(stderr, "Warning: %s is not a host configuration variable!\n", variable); } else { @@ -1856,10 +1870,10 @@ static int cmd_config(int argc, char *argv[]) { /* Change "add" into "set" for variables that do not allow multiple occurrences. Turn on warnings when it seems variables might be removed unintentionally. */ - if(action == 1 && !(variables[i].type & VAR_MULTIPLE)) { + if(action == ADD && !(variables[i].type & VAR_MULTIPLE)) { warnonremove = true; - action = 0; - } else if(action == 0 && (variables[i].type & VAR_MULTIPLE)) { + action = SET; + } else if(action == SET && (variables[i].type & VAR_MULTIPLE)) { warnonremove = true; } @@ -1877,7 +1891,7 @@ static int cmd_config(int argc, char *argv[]) { } if(!found) { - if(force || action < 0) { + if(force || action == GET || action == DEL) { 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); @@ -1894,7 +1908,11 @@ static int cmd_config(int argc, char *argv[]) { 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); @@ -1914,7 +1932,7 @@ static int cmd_config(int argc, char *argv[]) { char tmpfile[PATH_MAX]; FILE *tf = NULL; - if(action >= -1) { + if(action != GET) { if((size_t)snprintf(tmpfile, sizeof(tmpfile), "%s.config.tmp", filename) >= sizeof(tmpfile)) { fprintf(stderr, "Filename too long: %s.config.tmp\n", filename); return 1; @@ -1957,19 +1975,15 @@ static int cmd_config(int argc, char *argv[]) { // Did it match? if(!strcasecmp(buf2, variable)) { - // Get - if(action < -1) { + if(action == GET) { found = true; printf("%s\n", bvalue); - // Del - } else if(action == -1) { + } else if(action == DEL) { if(!*value || !strcasecmp(bvalue, value)) { removed = true; continue; } - - // Set - } else if(action == 0) { + } else if(action == SET) { // Warn if "set" was used for variables that can occur multiple times if(warnonremove && strcasecmp(bvalue, value)) { fprintf(stderr, "Warning: removing %s = %s\n", variable, bvalue); @@ -1988,8 +2002,7 @@ static int cmd_config(int argc, char *argv[]) { set = true; continue; - // Add - } else if(action > 0) { + } else if(action == ADD) { // Check if we've already seen this variable with the same value if(!strcasecmp(bvalue, value)) { found = true; @@ -1997,7 +2010,7 @@ static int cmd_config(int argc, char *argv[]) { } } - if(action >= -1) { + if(action != GET) { // Copy original line... if(fputs(buf1, tf) < 0) { fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno)); @@ -2026,14 +2039,14 @@ static int cmd_config(int argc, char *argv[]) { } // Add new variable if necessary. - if((action > 0 && !found) || (action == 0 && !set)) { + if((action == ADD && !found) || (action == SET && !set)) { if(fprintf(tf, "%s = %s\n", variable, value) < 0) { fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno)); return 1; } } - if(action < -1) { + if(action == GET) { if(found) { return 0; } else { @@ -2049,14 +2062,14 @@ static int cmd_config(int argc, char *argv[]) { } // Could we find what we had to remove? - if(action < 0 && !removed) { + if((action == GET || action == DEL) && !removed) { remove(tmpfile); fprintf(stderr, "No configuration variables deleted.\n"); return 1; } // 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)); @@ -2232,7 +2245,7 @@ static int cmd_init(int argc, char *argv[]) { check_port(name); -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS char filename[PATH_MAX]; snprintf(filename, sizeof(filename), "%s" SLASH "tinc-up", confbase); @@ -2394,7 +2407,7 @@ static int cmd_edit(int argc, char *argv[]) { } char *command; -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS const char *editor = getenv("VISUAL"); if(!editor) { @@ -2621,7 +2634,7 @@ static int switch_network(char *name) { } if(fd >= 0) { - close(fd); + closesocket(fd); fd = -1; } @@ -3264,6 +3277,22 @@ static void cleanup(void) { free_names(); } +static int run_command(int argc, char *argv[]) { + if(optind >= argc) { + return cmd_shell(argc, argv); + } + + for(int i = 0; commands[i].command; i++) { + if(!strcasecmp(argv[optind], commands[i].command)) { + return commands[i].function(argc - optind, argv + optind); + } + } + + fprintf(stderr, "Unknown command `%s'.\n", argv[optind]); + usage(true); + return 1; +} + int main(int argc, char *argv[]) { program_name = argv[0]; orig_argv = argv; @@ -3288,7 +3317,7 @@ int main(int argc, char *argv[]) { return 0; } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS static struct WSAData wsa_state; if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) { @@ -3299,20 +3328,13 @@ int main(int argc, char *argv[]) { #endif gettimeofday(&now, NULL); + random_init(); crypto_init(); prng_init(); - if(optind >= argc) { - return cmd_shell(argc, argv); - } + int result = run_command(argc, argv); - for(int i = 0; commands[i].command; i++) { - if(!strcasecmp(argv[optind], commands[i].command)) { - return commands[i].function(argc - optind, argv + optind); - } - } + random_exit(); - fprintf(stderr, "Unknown command `%s'.\n", argv[optind]); - usage(true); - return 1; + return result; }