X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ftincctl.c;h=4f6660a9272ced8729b17330b897e9a0390102c4;hb=953f5b4231bbbb8269bb0c55b96a1c8c4bb34a59;hp=12e5ead920b5a3209030adcf855b4bb09b3989c8;hpb=22ae0c3549628739ca7c40e48ce1a276469ded92;p=tinc diff --git a/src/tincctl.c b/src/tincctl.c index 12e5ead9..4f6660a9 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -355,6 +355,8 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo char buf[PATH_MAX]; char buf2[PATH_MAX]; +ask_filename: + /* Check stdin and stdout */ if(ask && tty) { /* Ask for a file and/or directory name. */ @@ -385,7 +387,17 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo #endif /* The directory is a relative path or a filename. */ getcwd(directory, sizeof(directory)); - snprintf(buf2, sizeof(buf2), "%s" SLASH "%s", directory, filename); + + if(snprintf(buf2, sizeof(buf2), "%s" SLASH "%s", directory, filename) >= sizeof(buf2)) { + fprintf(stderr, "Filename too long: %s" SLASH "%s\n", directory, filename); + + if(ask && tty) { + goto ask_filename; + } else { + return NULL; + } + } + filename = buf2; } @@ -825,6 +837,8 @@ bool connect_tincd(bool verbose) { strncpy(sa.sun_path, unixsocketname, sizeof(sa.sun_path)); + sa.sun_path[sizeof(sa.sun_path) - 1] = 0; + fd = socket(AF_UNIX, SOCK_STREAM, 0); if(fd < 0) { @@ -902,6 +916,8 @@ bool connect_tincd(bool verbose) { setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&one, sizeof(one)); #endif + sendline(fd, "%d ^%s %d", ID, controlcookie, TINC_CTL_VERSION_CURRENT); + char data[4096]; int version; @@ -915,8 +931,6 @@ bool connect_tincd(bool verbose) { return false; } - sendline(fd, "%d ^%s %d", ID, controlcookie, TINC_CTL_VERSION_CURRENT); - if(!recvline(fd, line, sizeof(line)) || sscanf(line, "%d %d %d", &code, &version, &pid) != 3 || code != 4 || version != TINC_CTL_VERSION_CURRENT) { if(verbose) { fprintf(stderr, "Could not fully establish control socket connection\n"); @@ -1161,7 +1175,12 @@ static int dump_invitations(void) { } char fname[PATH_MAX]; - snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ent->d_name); + + if(snprintf(fname, sizeof(fname), "%s" SLASH "%s", dname, ent->d_name) >= sizeof(fname)) { + fprintf(stderr, "Filename too long: %s" SLASH "%s\n", dname, ent->d_name); + continue; + } + FILE *f = fopen(fname, "r"); if(!f) { @@ -1930,7 +1949,11 @@ static int cmd_config(int argc, char *argv[]) { FILE *tf = NULL; if(action >= -1) { - snprintf(tmpfile, sizeof(tmpfile), "%s.config.tmp", filename); + if(snprintf(tmpfile, sizeof(tmpfile), "%s.config.tmp", filename) >= sizeof(tmpfile)) { + fprintf(stderr, "Filename too long: %s.config.tmp\n", filename); + return 1; + } + tf = fopen(tmpfile, "w"); if(!tf) { @@ -2128,7 +2151,7 @@ static bool try_bind(int port) { return success; } -int check_port(char *name) { +int check_port(const char *name) { if(try_bind(655)) { return 655; } @@ -2537,7 +2560,10 @@ static int cmd_import(int argc, char *argv[]) { fclose(out); } - snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, name); + if(snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, name) >= sizeof(filename)) { + fprintf(stderr, "Filename too long: %s" SLASH "%s\n", hosts_dir, name); + return 1; + } if(!force && !access(filename, F_OK)) { fprintf(stderr, "Host configuration file %s already exists, skipping.\n", filename); @@ -3149,10 +3175,7 @@ static int cmd_shell(int argc, char *argv[]) { free(line); rl_basic_word_break_characters = "\t\n "; line = readline(prompt); - - if(line) { - copy = xstrdup(line); - } + copy = line ? xstrdup(line) : NULL; } else { line = fgets(buf, sizeof(buf), stdin); } @@ -3198,6 +3221,9 @@ static int cmd_shell(int argc, char *argv[]) { } if(!strcasecmp(nargv[argc], "exit") || !strcasecmp(nargv[argc], "quit")) { +#ifdef HAVE_READLINE + free(copy); +#endif free(nargv); return result; } @@ -3226,6 +3252,9 @@ static int cmd_shell(int argc, char *argv[]) { } } +#ifdef HAVE_READLINE + free(copy); +#endif free(nargv); if(tty) { @@ -3264,7 +3293,7 @@ int main(int argc, char *argv[]) { static struct WSAData wsa_state; if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) { - fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError())); + fprintf(stderr, "System call `%s' failed: %s\n", "WSAStartup", winerror(GetLastError())); return false; }