X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ftincctl.c;h=a8930fe1d9d8d678f457f4afa85d7fa133616b2e;hb=9b9230a0a79c670b86f54fadd2807b864ff9d91f;hp=7b44881f3cf842c1a8295e6f2430c59741c2820d;hpb=cc3c69c892b0dad9a6ece0a0f4ccd429a22fcbff;p=tinc diff --git a/src/tincctl.c b/src/tincctl.c index 7b44881f..a8930fe1 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -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; } @@ -834,16 +850,10 @@ static int cmd_stop(int argc, char *argv[]) { } 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) { - fprintf(stderr, "Could not stop tinc daemon.\n"); - return 1; - } - // Wait for tincd to close the connection... - fd_set r; - FD_ZERO(&r); - FD_SET(fd, &r); - select(fd + 1, &r, NULL, NULL, NULL); + while(recvline(fd, line, sizeof line)) { + // Wait for tincd to close the connection... + } #else if(!remove_service()) return 1; @@ -1154,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 } @@ -1205,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; } @@ -1234,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; } @@ -1315,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--; @@ -1408,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; } @@ -1439,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; @@ -1698,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)); } @@ -1707,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); } @@ -1716,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); } @@ -1837,13 +1850,15 @@ 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; int result = export(name, stdout); if(!tty) fclose(stdout); + + free(name); return result; } @@ -1965,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}, @@ -1980,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}, @@ -2007,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++; } @@ -2034,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++; } @@ -2122,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);