Add missing configuration variables.
[tinc] / src / tincctl.c
index f519e78..7eb141d 100644 (file)
@@ -111,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"
@@ -714,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) {
@@ -730,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[]) {
@@ -933,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);
@@ -1009,6 +1019,7 @@ static struct {
        {"KeyExpire", VAR_SERVER},
        {"LocalDiscovery", VAR_SERVER},
        {"MACExpire", VAR_SERVER},
+       {"MaxOutputBufferSize", VAR_SERVER},
        {"MaxTimeout", VAR_SERVER},
        {"Mode", VAR_SERVER},
        {"Name", VAR_SERVER},
@@ -1018,26 +1029,32 @@ static struct {
        {"PrivateKey", VAR_SERVER | VAR_OBSOLETE},
        {"PrivateKeyFile", VAR_SERVER},
        {"ProcessPriority", VAR_SERVER},
+       {"Proxy", VAR_SERVER},
        {"ReplayWindow", VAR_SERVER},
        {"StrictSubnets", VAR_SERVER},
        {"TunnelServer", VAR_SERVER},
        {"UDPRcvBuf", VAR_SERVER},
        {"UDPSndBuf", VAR_SERVER},
+       {"VDEGroup", VAR_SERVER},
+       {"VDEPort", VAR_SERVER},
        /* Host configuration */
        {"Address", VAR_HOST | VAR_MULTIPLE},
        {"Cipher", VAR_SERVER | VAR_HOST},
        {"ClampMSS", VAR_SERVER | VAR_HOST},
        {"Compression", VAR_SERVER | VAR_HOST},
        {"Digest", VAR_SERVER | VAR_HOST},
+       {"ECDSAPublicKey", VAR_HOST},
+       {"ECDSAPublicKeyFile", VAR_SERVER | VAR_HOST},
        {"IndirectData", VAR_SERVER | VAR_HOST},
        {"MACLength", VAR_SERVER | VAR_HOST},
        {"PMTU", VAR_SERVER | VAR_HOST},
        {"PMTUDiscovery", VAR_SERVER | VAR_HOST},
        {"Port", VAR_HOST},
-       {"PublicKey", VAR_SERVER | VAR_HOST | VAR_OBSOLETE},
+       {"PublicKey", VAR_HOST | VAR_OBSOLETE},
        {"PublicKeyFile", VAR_SERVER | VAR_HOST | VAR_OBSOLETE},
        {"Subnet", VAR_HOST | VAR_MULTIPLE},
        {"TCPOnly", VAR_SERVER | VAR_HOST},
+       {"Weight", VAR_HOST},
        {NULL, 0}
 };
 
@@ -1047,8 +1064,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;
@@ -1100,6 +1119,9 @@ static int cmd_config(int argc, char *argv[]) {
                return 1;
        }
 
+       if(action < -1 && *value)
+               action = 0;
+
        /* Some simple checks. */
        bool found = false;
 
@@ -1148,8 +1170,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);
@@ -1181,19 +1203,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;
@@ -1215,8 +1242,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;
@@ -1236,18 +1267,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;
+                               }
+                       }
                }
        }
 
@@ -1270,6 +1303,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));
@@ -1361,8 +1400,6 @@ static int cmd_init(int argc, char *argv[]) {
                return 1;
        }
 
-       char *hosts_dir = NULL;
-       xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
        if(mkdir(hosts_dir, 0755) && errno != EEXIST) {
                fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
                return 1;
@@ -1379,9 +1416,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;
 
 }
 
@@ -1461,14 +1513,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;