Fix the "network" command in tinc shell.
[tinc] / src / tincctl.c
index 1eb9818..4231cce 100644 (file)
@@ -330,7 +330,7 @@ static void disable_old_keys(const char *filename, const char *what) {
 
 static FILE *ask_and_open(const char *filename, const char *what, const char *mode, bool ask, mode_t perms) {
        FILE *r;
-       char *directory;
+       char directory[PATH_MAX] = ".";
        char buf[PATH_MAX];
        char buf2[PATH_MAX];
 
@@ -358,7 +358,7 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo
        if(filename[0] != '/') {
 #endif
                /* The directory is a relative path or a filename. */
-               directory = get_current_dir_name();
+               getcwd(directory, sizeof directory);
                snprintf(buf2, sizeof buf2, "%s" SLASH "%s", directory, filename);
                filename = buf2;
        }
@@ -2284,26 +2284,29 @@ static int cmd_exchange_all(int argc, char *argv[]) {
 }
 
 static int switch_network(char *name) {
+       if(strcmp(name, ".")) {
+               if(!check_netname(name, false)) {
+                       fprintf(stderr, "Invalid character in netname!\n");
+                       return 1;
+               }
+
+               if(!check_netname(name, true))
+                       fprintf(stderr, "Warning: unsafe character in netname!\n");
+       }
+
        if(fd >= 0) {
                close(fd);
                fd = -1;
        }
 
-       free(confbase);
-       confbase = NULL;
-       free(pidfilename);
-       pidfilename = NULL;
-       free(logfilename);
-       logfilename = NULL;
-       free(unixsocketname);
-       unixsocketname = NULL;
+       free_names();
+       netname = strcmp(name, ".") ? xstrdup(name) : NULL;
+       make_names(false);
+
        free(tinc_conf);
        free(hosts_dir);
        free(prompt);
 
-       free(netname);
-       netname = strcmp(name, ".") ? xstrdup(name) : NULL;
-
         xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
         xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
        xasprintf(&prompt, "%s> ", identname);