Use conditional compilation for cryptographic functions.
[tinc] / src / tincctl.c
index 45bf6ee..a8930fe 100644 (file)
@@ -112,11 +112,10 @@ static void usage(bool status) {
                                "\n"
                                "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"
+                               "  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                    Restart tincd.\n"
@@ -347,13 +346,13 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo
   them in.
 */
 static bool ecdsa_keygen(bool ask) {
-       ecdsa_t key;
+       ecdsa_t *key;
        FILE *f;
        char *pubname, *privname;
 
        fprintf(stderr, "Generating ECDSA keypair:\n");
 
-       if(!ecdsa_generate(&key)) {
+       if(!(key = ecdsa_generate())) {
                fprintf(stderr, "Error during key generation!\n");
                return false;
        } else
@@ -371,7 +370,12 @@ static bool ecdsa_keygen(bool ask) {
        fchmod(fileno(f), 0600);
 #endif
 
-       ecdsa_write_pem_private_key(&key, f);
+       if(!ecdsa_write_pem_private_key(key, f)) {
+               fprintf(stderr, "Error writing private key!\n");
+               ecdsa_free(key);
+               fclose(f);
+               return false;
+       }
 
        fclose(f);
 
@@ -386,11 +390,12 @@ static bool ecdsa_keygen(bool ask) {
        if(!f)
                return false;
 
-       char *pubkey = ecdsa_get_base64_public_key(&key);
+       char *pubkey = ecdsa_get_base64_public_key(key);
        fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
        free(pubkey);
 
        fclose(f);
+       ecdsa_free(key);
 
        return true;
 }
@@ -400,13 +405,13 @@ static bool ecdsa_keygen(bool ask) {
   them in.
 */
 static bool rsa_keygen(int bits, bool ask) {
-       rsa_t key;
+       rsa_t *key;
        FILE *f;
        char *pubname, *privname;
 
        fprintf(stderr, "Generating %d bits keys:\n", bits);
 
-       if(!rsa_generate(&key, bits, 0x10001)) {
+       if(!(key = rsa_generate(bits, 0x10001))) {
                fprintf(stderr, "Error during key generation!\n");
                return false;
        } else
@@ -424,7 +429,12 @@ static bool rsa_keygen(int bits, bool ask) {
        fchmod(fileno(f), 0600);
 #endif
 
-       rsa_write_pem_private_key(&key, f);
+       if(!rsa_write_pem_private_key(key, f)) {
+               fprintf(stderr, "Error writing private key!\n");
+               fclose(f);
+               rsa_free(key);
+               return false;
+       }
 
        fclose(f);
 
@@ -439,9 +449,15 @@ static bool rsa_keygen(int bits, bool ask) {
        if(!f)
                return false;
 
-       rsa_write_pem_public_key(&key, f);
+       if(!rsa_write_pem_public_key(key, f)) {
+               fprintf(stderr, "Error writing public key!\n");
+               fclose(f);
+               rsa_free(key);
+               return false;
+       }
 
        fclose(f);
+       rsa_free(key);
 
        return true;
 }
@@ -1148,7 +1164,7 @@ static int cmd_top(int argc, char *argv[]) {
        top(fd);
        return 0;
 #else
-       fprintf(stderr, "This version of tincctl was compiled without support for the curses library.\n");
+       fprintf(stderr, "This version of tinc was compiled without support for the curses library.\n");
        return 1;
 #endif
 }
@@ -1199,10 +1215,11 @@ static int rstrip(char *value) {
        return len;
 }
 
-static char *get_my_name() {
+static char *get_my_name(bool verbose) {
        FILE *f = fopen(tinc_conf, "r");
        if(!f) {
-               fprintf(stderr, "Could not open %s: %s\n", tinc_conf, strerror(errno));
+               if(verbose)
+                       fprintf(stderr, "Could not open %s: %s\n", tinc_conf, strerror(errno));
                return NULL;
        }
 
@@ -1228,7 +1245,8 @@ static char *get_my_name() {
        }
 
        fclose(f);
-       fprintf(stderr, "Could not find Name in %s.\n", tinc_conf);
+       if(verbose)
+               fprintf(stderr, "Could not find Name in %s.\n", tinc_conf);
        return NULL;
 }
 
@@ -1309,6 +1327,9 @@ static int cmd_config(int argc, char *argv[]) {
                return 1;
        }
 
+       if(strcasecmp(argv[0], "config"))
+               argv--, argc++;
+
        int action = -2;
        if(!strcasecmp(argv[1], "get")) {
                argv++, argc--;
@@ -1402,7 +1423,7 @@ static int cmd_config(int argc, char *argv[]) {
                /* Should this go into our own host config file? */
 
                if(!node && !(variables[i].type & VAR_SERVER)) {
-                       node = get_my_name();
+                       node = get_my_name(true);
                        if(!node)
                                return 1;
                }
@@ -1433,19 +1454,8 @@ static int cmd_config(int argc, char *argv[]) {
 
        FILE *f = fopen(filename, "r");
        if(!f) {
-               if(action < 0 || errno != ENOENT) {
-                       fprintf(stderr, "Could not open configuration file %s: %s\n", filename, strerror(errno));
-                       return 1;
-               }
-
-               // If it doesn't exist, create it.
-               f = fopen(filename, "a+");
-               if(!f) {
-                       fprintf(stderr, "Could not create configuration file %s: %s\n", filename, strerror(errno));
-                       return 1;
-               } else {
-                       fprintf(stderr, "Created configuration file %s.\n", filename);
-               }
+               fprintf(stderr, "Could not open configuration file %s: %s\n", filename, strerror(errno));
+               return 1;
        }
 
        char *tmpfile = NULL;
@@ -1692,6 +1702,9 @@ static int cmd_generate_keys(int argc, char *argv[]) {
                return 1;
        }
 
+       if(!name)
+               name = get_my_name(false);
+
        return !(rsa_keygen(argc > 1 ? atoi(argv[1]) : 2048, true) && ecdsa_keygen(true));
 }
 
@@ -1701,6 +1714,9 @@ static int cmd_generate_rsa_keys(int argc, char *argv[]) {
                return 1;
        }
 
+       if(!name)
+               name = get_my_name(false);
+
        return !rsa_keygen(argc > 1 ? atoi(argv[1]) : 2048, true);
 }
 
@@ -1710,6 +1726,9 @@ static int cmd_generate_ecdsa_keys(int argc, char *argv[]) {
                return 1;
        }
 
+       if(!name)
+               name = get_my_name(false);
+
        return !ecdsa_keygen(true);
 }
 
@@ -1831,7 +1850,7 @@ static int cmd_export(int argc, char *argv[]) {
                return 1;
        }
 
-       char *name = get_my_name();
+       char *name = get_my_name(true);
        if(!name)
                return 1;
 
@@ -1961,6 +1980,7 @@ static int cmd_exchange_all(int argc, char *argv[]) {
 static const struct {
        const char *command;
        int (*function)(int argc, char *argv[]);
+       bool hidden;
 } commands[] = {
        {"start", cmd_start},
        {"stop", cmd_stop},
@@ -1976,7 +1996,11 @@ static const struct {
        {"pcap", cmd_pcap},
        {"log", cmd_log},
        {"pid", cmd_pid},
-       {"config", cmd_config},
+       {"config", cmd_config, true},
+       {"add", cmd_config},
+       {"del", cmd_config},
+       {"get", cmd_config},
+       {"set", cmd_config},
        {"init", cmd_init},
        {"generate-keys", cmd_generate_keys},
        {"generate-rsa-keys", cmd_generate_rsa_keys},
@@ -2003,7 +2027,7 @@ static char *complete_command(const char *text, int state) {
                i++;
 
        while(commands[i].command) {
-               if(!strncasecmp(commands[i].command, text, strlen(text)))
+               if(!commands[i].hidden && !strncasecmp(commands[i].command, text, strlen(text)))
                        return xstrdup(commands[i].command);
                i++;
        }
@@ -2030,42 +2054,24 @@ static char *complete_dump(const char *text, int state) {
 }
 
 static char *complete_config(const char *text, int state) {
-       const char *sub[] = {"get", "set", "add", "del"};
        static int i;
-       if(!state) {
+
+       if(!state)
                i = 0;
-               if(!strchr(rl_line_buffer + 7, ' '))
-                       i = -4;
-               else {
-                       bool found = false;
-                       for(int i = 0; i < 4; i++) {
-                               if(!strncasecmp(rl_line_buffer + 7, sub[i], strlen(sub[i])) && rl_line_buffer[7 + strlen(sub[i])] == ' ') {
-                                       found = true;
-                                       break;
-                               }
-                       }
-                       if(!found)
-                               return NULL;
-               }
-       } else {
+       else
                i++;
-       }
 
-       while(i < 0 || variables[i].name) {
-               if(i < 0 && !strncasecmp(sub[i + 4], text, strlen(text)))
-                       return xstrdup(sub[i + 4]);
-               if(i >= 0) {
-                       char *dot = strchr(text, '.');
-                       if(dot) {
-                               if((variables[i].type & VAR_HOST) && !strncasecmp(variables[i].name, dot + 1, strlen(dot + 1))) {
-                                       char *match;
-                                       xasprintf(&match, "%.*s.%s", dot - text, text, variables[i].name);
-                                       return match;
-                               }
-                       } else {
-                               if(!strncasecmp(variables[i].name, text, strlen(text)))
-                                       return xstrdup(variables[i].name);
+       while(variables[i].name) {
+               char *dot = strchr(text, '.');
+               if(dot) {
+                       if((variables[i].type & VAR_HOST) && !strncasecmp(variables[i].name, dot + 1, strlen(dot + 1))) {
+                               char *match;
+                               xasprintf(&match, "%.*s.%s", dot - text, text, variables[i].name);
+                               return match;
                        }
+               } else {
+                       if(!strncasecmp(variables[i].name, text, strlen(text)))
+                               return xstrdup(variables[i].name);
                }
                i++;
        }
@@ -2118,7 +2124,13 @@ static char **completion (const char *text, int start, int end) {
                matches = rl_completion_matches(text, complete_command);
        else if(!strncasecmp(rl_line_buffer, "dump ", 5))
                matches = rl_completion_matches(text, complete_dump);
-       else if(!strncasecmp(rl_line_buffer, "config ", 7))
+       else if(!strncasecmp(rl_line_buffer, "add ", 4))
+               matches = rl_completion_matches(text, complete_config);
+       else if(!strncasecmp(rl_line_buffer, "del ", 4))
+               matches = rl_completion_matches(text, complete_config);
+       else if(!strncasecmp(rl_line_buffer, "get ", 4))
+               matches = rl_completion_matches(text, complete_config);
+       else if(!strncasecmp(rl_line_buffer, "set ", 4))
                matches = rl_completion_matches(text, complete_config);
        else if(!strncasecmp(rl_line_buffer, "info ", 5))
                matches = rl_completion_matches(text, complete_info);