Add the ability to query configuration variables to tincctl.
[tinc] / src / tincctl.c
index 24359ab..64340c7 100644 (file)
@@ -47,6 +47,7 @@ static bool show_version = false;
 static char *name = NULL;
 static char *identname = NULL;                         /* program name for syslog */
 static char *pidfilename = NULL;                       /* pid file location */
+static char *confdir = NULL;
 static char controlcookie[1024];
 char *netname = NULL;
 char *confbase = NULL;
@@ -110,6 +111,7 @@ static void usage(bool status) {
                                "Valid commands are:\n"
                                "  init [name]                Create initial configuration files.\n"
                                "  config                     Change configuration:\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"
@@ -192,7 +194,7 @@ static bool parse_options(int argc, char **argv) {
 
         /* netname "." is special: a "top-level name" */
 
-        if(!strcmp(netname, ".")) {
+        if(netname && !strcmp(netname, ".")) {
                 free(netname);
                 netname = NULL;
         }
@@ -234,7 +236,7 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo
 #endif
                /* The directory is a relative path or a filename. */
                directory = get_current_dir_name();
-               snprintf(buf2, sizeof buf2, "%s/%s", directory, filename);
+               snprintf(buf2, sizeof buf2, "%s" SLASH "%s", directory, filename);
                filename = buf2;
        }
 
@@ -269,7 +271,7 @@ static bool ecdsa_keygen() {
        } else
                fprintf(stderr, "Done.\n");
 
-       xasprintf(&filename, "%s/ecdsa_key.priv", confbase);
+       xasprintf(&filename, "%s" SLASH "ecdsa_key.priv", confbase);
        f = ask_and_open(filename, "private ECDSA key", "a");
 
        if(!f)
@@ -289,9 +291,9 @@ static bool ecdsa_keygen() {
        free(filename);
 
        if(name)
-               xasprintf(&filename, "%s/hosts/%s", confbase, name);
+               xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
        else
-               xasprintf(&filename, "%s/ecdsa_key.pub", confbase);
+               xasprintf(&filename, "%s" SLASH "ecdsa_key.pub", confbase);
 
        f = ask_and_open(filename, "public ECDSA key", "a");
 
@@ -328,7 +330,7 @@ static bool rsa_keygen(int bits) {
        } else
                fprintf(stderr, "Done.\n");
 
-       xasprintf(&filename, "%s/rsa_key.priv", confbase);
+       xasprintf(&filename, "%s" SLASH "rsa_key.priv", confbase);
        f = ask_and_open(filename, "private RSA key", "a");
 
        if(!f)
@@ -348,9 +350,9 @@ static bool rsa_keygen(int bits) {
        free(filename);
 
        if(name)
-               xasprintf(&filename, "%s/hosts/%s", confbase, name);
+               xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
        else
-               xasprintf(&filename, "%s/rsa_key.pub", confbase);
+               xasprintf(&filename, "%s" SLASH "rsa_key.pub", confbase);
 
        f = ask_and_open(filename, "public RSA key", "a");
 
@@ -388,38 +390,40 @@ static void make_names(void) {
                if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
                        if(!confbase) {
                                if(netname)
-                                       xasprintf(&confbase, "%s/%s", installdir, netname);
+                                       xasprintf(&confbase, "%s" SLASH "%s", installdir, netname);
                                else
                                        xasprintf(&confbase, "%s", installdir);
                        }
                }
                if(!pidfilename)
-                       xasprintf(&pidfilename, "%s/pid", confbase);
+                       xasprintf(&pidfilename, "%s" SLASH "pid", confbase);
                RegCloseKey(key);
        }
 
        if(!*installdir) {
 #endif
+       confdir = xstrdup(CONFDIR);
 
        if(!pidfilename)
-               xasprintf(&pidfilename, "%s/run/%s.pid", LOCALSTATEDIR, identname);
+               xasprintf(&pidfilename, "%s" SLASH "run" SLASH "%s.pid", LOCALSTATEDIR, identname);
 
        if(netname) {
                if(!confbase)
-                       xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
+                       xasprintf(&confbase, CONFDIR SLASH "tinc" SLASH "%s", netname);
                else
                        fprintf(stderr, "Both netname and configuration directory given, using the latter...\n");
        } else {
                if(!confbase)
-                       xasprintf(&confbase, CONFDIR "/tinc");
+                       xasprintf(&confbase, CONFDIR SLASH "tinc");
        }
 
 #ifdef HAVE_MINGW
-       }
+       } else
+               confdir = xstrdup(installdir);
 #endif
 
-       xasprintf(&tinc_conf, "%s/tinc.conf", confbase);
-       xasprintf(&hosts_dir, "%s/hosts", confbase);
+       xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
+       xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
 }
 
 static char buffer[4096];
@@ -711,8 +715,16 @@ static int cmd_start(int argc, char *argv[]) {
 
 static int cmd_stop(int argc, char *argv[]) {
 #ifndef HAVE_MINGW
-       if(!connect_tincd())
+       if(!connect_tincd()) {
+               if(pid) {
+                       if(kill(pid, SIGTERM)) 
+                               return 1;
+                       fprintf(stderr, "Sent TERM signal to process with PID %u.\n", pid);
+                       return 0;
+               }
+
                return 1;
+       }
 
        sendline(fd, "%d %d", CONTROL, REQ_STOP);
        if(!recvline(fd, line, sizeof line) || sscanf(line, "%d %d %d", &code, &req, &result) != 3 || code != CONTROL || req != REQ_STOP || result) {
@@ -727,7 +739,8 @@ static int cmd_stop(int argc, char *argv[]) {
 }
 
 static int cmd_restart(int argc, char *argv[]) {
-       return cmd_stop(argc, argv) ?: cmd_start(argc, argv);
+       cmd_stop(argc, argv);
+       return cmd_start(argc, argv);
 }
 
 static int cmd_reload(int argc, char *argv[]) {
@@ -930,7 +943,7 @@ static int cmd_log(int argc, char *argv[]) {
 }
 
 static int cmd_pid(int argc, char *argv[]) {
-       if(!connect_tincd())
+       if(!connect_tincd() && !pid)
                return 1;
 
        printf("%d\n", pid);
@@ -1044,8 +1057,10 @@ static int cmd_config(int argc, char *argv[]) {
                return 1;
        }
 
-       int action = 0;
-       if(!strcasecmp(argv[1], "add")) {
+       int action = -2;
+       if(!strcasecmp(argv[1], "get")) {
+               argv++, argc--;
+       } else if(!strcasecmp(argv[1], "add")) {
                argv++, argc--, action = 1;
        } else if(!strcasecmp(argv[1], "del")) {
                argv++, argc--, action = -1;
@@ -1097,6 +1112,9 @@ static int cmd_config(int argc, char *argv[]) {
                return 1;
        }
 
+       if(action < -1 && *value)
+               action = 0;
+
        /* Some simple checks. */
        bool found = false;
 
@@ -1145,8 +1163,8 @@ static int cmd_config(int argc, char *argv[]) {
                return 1;
        }
 
-       if(!found && action >= 0) {
-               if(force) {
+       if(!found) {
+               if(force || action < 0) {
                        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);
@@ -1157,7 +1175,7 @@ static int cmd_config(int argc, char *argv[]) {
        // Open the right configuration file.
        char *filename;
        if(node)
-               xasprintf(&filename, "%s/%s", hosts_dir, node);
+               xasprintf(&filename, "%s" SLASH "%s", hosts_dir, node);
        else
                filename = tinc_conf;
 
@@ -1178,19 +1196,24 @@ static int cmd_config(int argc, char *argv[]) {
                }
        }
 
-       char *tmpfile;
-       xasprintf(&tmpfile, "%s.config.tmp", filename);
-       FILE *tf = fopen(tmpfile, "w");
-       if(!tf) {
-               fprintf(stderr, "Could not open temporary file %s: %s\n", tmpfile, strerror(errno));
-               return 1;
+       char *tmpfile = NULL;
+       FILE *tf = NULL;
+
+       if(action >= -1) {
+               xasprintf(&tmpfile, "%s.config.tmp", filename);
+               tf = fopen(tmpfile, "w");
+               if(!tf) {
+                       fprintf(stderr, "Could not open temporary file %s: %s\n", tmpfile, strerror(errno));
+                       return 1;
+               }
        }
 
-       // Copy the file, making modifications on the fly.
+       // Copy the file, making modifications on the fly, unless we are just getting a value.
        char buf1[4096];
        char buf2[4096];
        bool set = false;
        bool removed = false;
+       found = false;
 
        while(fgets(buf1, sizeof buf1, f)) {
                buf1[sizeof buf1 - 1] = 0;
@@ -1212,8 +1235,12 @@ static int cmd_config(int argc, char *argv[]) {
 
                // Did it match?
                if(!strcasecmp(buf2, variable)) {
+                       // Get
+                       if(action < -1) {
+                               found = true;
+                               printf("%s\n", bvalue);
                        // Del
-                       if(action < 0) {
+                       } else if(action == -1) {
                                if(!*value || !strcasecmp(bvalue, value)) {
                                        removed = true;
                                        continue;
@@ -1233,18 +1260,20 @@ static int cmd_config(int argc, char *argv[]) {
                        }
                }
 
-               // Copy original line...
-               if(fputs(buf1, tf) < 0) {
-                       fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno));
-                       return 1;
-               }
-
-               // Add newline if it is missing...
-               if(*buf1 && buf1[strlen(buf1) - 1] != '\n') {
-                       if(fputc('\n', tf) < 0) {
+               if(action >= -1) {
+                       // Copy original line...
+                       if(fputs(buf1, tf) < 0) {
                                fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno));
                                return 1;
                        }
+
+                       // Add newline if it is missing...
+                       if(*buf1 && buf1[strlen(buf1) - 1] != '\n') {
+                               if(fputc('\n', tf) < 0) {
+                                       fprintf(stderr, "Error writing to temporary file %s: %s\n", tmpfile, strerror(errno));
+                                       return 1;
+                               }
+                       }
                }
        }
 
@@ -1267,6 +1296,12 @@ static int cmd_config(int argc, char *argv[]) {
                }
        }
 
+       if(action < -1) {
+               if(!found)
+                       fprintf(stderr, "No matching configuration variables found.\n");
+               return 0;
+       }
+
        // Make sure we wrote everything...
        if(fclose(tf)) {
                fprintf(stderr, "Error closing temporary file %s: %s\n", tmpfile, strerror(errno));
@@ -1348,7 +1383,7 @@ static int cmd_init(int argc, char *argv[]) {
                return 1;
        }
 
-       if(mkdir(CONFDIR, 0755) && errno != EEXIST) {
+       if(mkdir(confdir, 0755) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", CONFDIR, strerror(errno));
                return 1;
        }
@@ -1358,8 +1393,6 @@ static int cmd_init(int argc, char *argv[]) {
                return 1;
        }
 
-       char *hosts_dir = NULL;
-       xasprintf(&hosts_dir, "%s/hosts", confbase);
        if(mkdir(hosts_dir, 0755) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
                return 1;
@@ -1376,9 +1409,24 @@ static int cmd_init(int argc, char *argv[]) {
 
        fclose(stdin);
        if(!rsa_keygen(2048) || !ecdsa_keygen())
-               return false;
+               return 1;
 
-       return true;
+#ifndef HAVE_MINGW
+       char *filename;
+       xasprintf(&filename, "%s" SLASH "tinc-up", confbase);
+       if(access(filename, F_OK)) {
+               FILE *f = fopen(filename, "w");
+               if(!f) {
+                       fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
+                       return 1;
+               }
+               fchmod(fileno(f), 0755);
+               fprintf(f, "#!/bin/sh\n\necho 'Unconfigured tinc-up script, please edit!'\n\n#ifconfig $INTERFACE <your vpn IP address> netmask <netmask of whole VPN>\n");
+               fclose(f);
+       }
+#endif
+
+       return 0;
 
 }
 
@@ -1435,10 +1483,10 @@ static int cmd_edit(int argc, char *argv[]) {
 
        char *filename = NULL;
 
-       if(strncmp(argv[1], "hosts/", 6)) {
+       if(strncmp(argv[1], "hosts" SLASH, 6)) {
                for(int i = 0; conffiles[i]; i++) {
                        if(!strcmp(argv[1], conffiles[i])) {
-                               xasprintf(&filename, "%s/%s", confbase, argv[1]);
+                               xasprintf(&filename, "%s" SLASH "%s", confbase, argv[1]);
                                break;
                        }
                }
@@ -1447,7 +1495,7 @@ static int cmd_edit(int argc, char *argv[]) {
        }
 
        if(!filename) {
-               xasprintf(&filename, "%s/%s", hosts_dir, argv[1]);
+               xasprintf(&filename, "%s" SLASH "%s", hosts_dir, argv[1]);
                char *dash = strchr(argv[1], '-');
                if(dash) {
                        *dash++ = 0;
@@ -1458,14 +1506,12 @@ static int cmd_edit(int argc, char *argv[]) {
                }
        }
 
+       char *command;
 #ifndef HAVE_MINGW
-       char *editor = getenv("VISUAL") ?: getenv("EDITOR") ?: "vi";
+       xasprintf(&command, "\"%s\" \"%s\"", getenv("VISUAL") ?: getenv("EDITOR") ?: "vi", filename);
 #else
-       char *editor = "edit";
+       xasprintf(&command, "edit \"%s\"", filename);
 #endif
-
-       char *command;
-       xasprintf(&command, "\"%s\" \"%s\"", editor, filename);
        int result = system(command);
        if(result)
                return result;
@@ -1481,7 +1527,7 @@ static int cmd_edit(int argc, char *argv[]) {
 
 static int export(const char *name, FILE *out) {
        char *filename;
-       xasprintf(&filename, "%s/%s", hosts_dir, name);
+       xasprintf(&filename, "%s" SLASH "%s", hosts_dir, name);
        FILE *in = fopen(filename, "r");
        if(!in) {
                fprintf(stderr, "Could not open configuration file %s: %s\n", filename, strerror(errno));
@@ -1560,7 +1606,7 @@ static int cmd_import(int argc, char *argv[]) {
                                fclose(out);
 
                        free(filename);
-                       xasprintf(&filename, "%s/%s", hosts_dir, name);
+                       xasprintf(&filename, "%s" SLASH "%s", hosts_dir, name);
 
                        if(!force && !access(filename, F_OK)) {
                                fprintf(stderr, "Host configuration file %s already exists, skipping.\n", filename);