X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ftincctl.c;h=057ff7c93f92d797b7e6b29faaaf6d360b68b62e;hb=c52c46f8717aac6904f32766d774fa3fdf9611d8;hp=b2a6561f1654eb80955ba12fd4e7577708fc0a8f;hpb=439069bda62b25baaabeb765ac0557efa57b6cfb;p=tinc diff --git a/src/tincctl.c b/src/tincctl.c index b2a6561f..057ff7c9 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -25,6 +25,7 @@ #include "protocol.h" #include "control_common.h" #include "ecdsagen.h" +#include "info.h" #include "rsagen.h" #include "utils.h" #include "tincctl.h" @@ -55,6 +56,7 @@ static char line[4096]; static int code; static int req; static int result; +static bool force = false; #ifdef HAVE_MINGW static struct WSAData wsa_state; @@ -62,10 +64,19 @@ static struct WSAData wsa_state; static struct option const long_options[] = { {"config", required_argument, NULL, 'c'}, + {"debug", optional_argument, NULL, 0}, + {"no-detach", no_argument, NULL, 0}, + {"mlock", no_argument, NULL, 0}, {"net", required_argument, NULL, 'n'}, {"help", no_argument, NULL, 1}, {"version", no_argument, NULL, 2}, {"pidfile", required_argument, NULL, 5}, + {"logfile", required_argument, NULL, 0}, + {"bypass-security", no_argument, NULL, 0}, + {"chroot", no_argument, NULL, 0}, + {"user", required_argument, NULL, 0}, + {"option", required_argument, NULL, 0}, + {"force", no_argument, NULL, 6}, {NULL, 0, NULL, 0} }; @@ -98,7 +109,7 @@ static void usage(bool status) { " [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 Start tincd.\n" + " start [tincd options] Start tincd.\n" " stop Stop tincd.\n" " restart Restart tincd.\n" " reload Partially reload configuration of running tincd.\n" @@ -112,6 +123,7 @@ static void usage(bool status) { " subnets - all known subnets in the VPN\n" " connections - all meta connections with ourself\n" " graph - graph of the VPN in dotty format\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" @@ -121,6 +133,9 @@ static void usage(bool status) { #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 [--force] Import host configuration file(s) from standard input\n" "\n"); printf("Report bugs to tinc@tinc-vpn.org.\n"); } @@ -130,7 +145,7 @@ static bool parse_options(int argc, char **argv) { int r; int option_index = 0; - while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) { + while((r = getopt_long(argc, argv, "c:n:Dd::Lo:RU:", long_options, &option_index)) != EOF) { switch (r) { case 0: /* long option */ break; @@ -155,6 +170,10 @@ static bool parse_options(int argc, char **argv) { pidfilename = xstrdup(optarg); break; + case 6: + force = true; + break; + case '?': usage(true); return false; @@ -655,6 +674,9 @@ static bool connect_tincd() { static int cmd_start(int argc, char *argv[]) { int i, j; char *c; + + argc += optind; + argv -= optind; char *slash = strrchr(argv[0], '/'); #ifdef HAVE_MINGW @@ -723,6 +745,9 @@ static int cmd_dump(int argc, char *argv[]) { return 1; } + if(!connect_tincd()) + return 1; + bool do_graph = false; if(!strcasecmp(argv[1], "nodes")) @@ -743,9 +768,6 @@ static int cmd_dump(int argc, char *argv[]) { return 1; } - if(!connect_tincd()) - return 1; - if(do_graph) printf("digraph {\n"); @@ -1275,6 +1297,18 @@ static int cmd_version(int argc, char *argv[]) { return 0; } +static int cmd_info(int argc, char *argv[]) { + if(argc != 2) { + fprintf(stderr, "Invalid number of arguments.\n"); + return 1; + } + + if(!connect_tincd()) + return 1; + + return info(fd, argv[1]); +} + static const char *conffiles[] = { "tinc.conf", "tinc-up", @@ -1338,6 +1372,131 @@ static int cmd_edit(int argc, char *argv[]) { return 0; } +static int export(const char *name, FILE *out) { + char *filename; + xasprintf(&filename, "%s/%s", hosts_dir, name); + FILE *in = fopen(filename, "r"); + if(!in) { + fprintf(stderr, "Could not open configuration file %s: %s\n", filename, strerror(errno)); + return 1; + } + + fprintf(out, "Name = %s\n", name); + char buf[4096]; + while(fgets(buf, sizeof buf, in)) + fputs(buf, out); + + if(ferror(in)) { + fprintf(stderr, "Error while reading configuration file %s: %s\n", filename, strerror(errno)); + return 1; + } + + fclose(in); + return 0; +} + +static int cmd_export(int argc, char *argv[]) { + char *name = get_my_name(); + if(!name) + return 1; + + return export(name, stdout); +} + +static int cmd_export_all(int argc, char *argv[]) { + DIR *dir = opendir(hosts_dir); + if(!dir) { + fprintf(stderr, "Could not open host configuration directory %s: %s\n", hosts_dir, strerror(errno)); + return 1; + } + + bool first = true; + int result = 0; + struct dirent *ent; + + while((ent = readdir(dir))) { + if(!check_id(ent->d_name)) + continue; + + if(first) + first = false; + else + printf("#---------------------------------------------------------------#\n"); + + result |= export(ent->d_name, stdout); + } + + closedir(dir); + return result; +} + +static int cmd_import(int argc, char *argv[]) { + FILE *in = stdin; + FILE *out = NULL; + + char buf[4096]; + char name[4096]; + char *filename; + int count = 0; + bool firstline = true; + + while(fgets(buf, sizeof buf, in)) { + if(sscanf(buf, "Name = %s", name) == 1) { + if(!check_id(name)) { + fprintf(stderr, "Invalid Name in input!\n"); + return 1; + } + + if(out) + fclose(out); + + free(filename); + xasprintf(&filename, "%s/%s", hosts_dir, name); + + if(!force && !access(filename, F_OK)) { + fprintf(stderr, "Host configuration file %s already exists, skipping.\n", filename); + out = NULL; + continue; + } + + out = fopen(filename, "w"); + if(!out) { + fprintf(stderr, "Error creating configuration file %s: %s\n", filename, strerror(errno)); + return 1; + } + + count++; + firstline = false; + continue; + } else if(firstline) { + fprintf(stderr, "Junk at the beginning of the input, ignoring.\n"); + firstline = false; + } + + + if(!strcmp(buf, "#---------------------------------------------------------------#\n")) + continue; + + if(out) { + if(fputs(buf, out) < 0) { + fprintf(stderr, "Error writing to host configuration file %s: %s\n", filename, strerror(errno)); + return 1; + } + } + } + + if(out) + fclose(out); + + if(count) { + fprintf(stderr, "Imported %d host configuration files.\n", count); + return 0; + } else { + fprintf(stderr, "No host configuration files imported.\n"); + return 1; + } +} + static const struct { const char *command; int (*function)(int argc, char *argv[]); @@ -1363,7 +1522,11 @@ static const struct { {"generate-ecdsa-keys", cmd_generate_ecdsa_keys}, {"help", cmd_help}, {"version", cmd_version}, + {"info", cmd_info}, {"edit", cmd_edit}, + {"export", cmd_export}, + {"export-all", cmd_export_all}, + {"import", cmd_import}, {NULL, NULL}, };