Extract filesystem-related functions into fs.c
[tinc] / src / invitation.c
index 5db1e98..e068ae8 100644 (file)
@@ -36,6 +36,7 @@
 #include "xalloc.h"
 #include "random.h"
 #include "pidfile.h"
+#include "fs.h"
 
 #include "ed25519/sha512.h"
 
@@ -333,6 +334,11 @@ int cmd_invite(int argc, char *argv[]) {
                return 1;
        }
 
+       if(argc > 2) {
+               fprintf(stderr, "Too many arguments!\n");
+               return 1;
+       }
+
        // Check validity of the new node's name
        if(!check_id(argv[1])) {
                fprintf(stderr, "Invalid name for node.\n");
@@ -379,13 +385,12 @@ int cmd_invite(int argc, char *argv[]) {
                }
        }
 
-       snprintf(filename, sizeof(filename), "%s" SLASH "invitations", confbase);
-
-       if(mkdir(filename, 0700) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno));
-               return 1;
+       if(!makedirs(DIR_INVITATIONS)) {
+               return false;
        }
 
+       snprintf(filename, sizeof(filename), "%s" SLASH "invitations", confbase);
+
        // Count the number of valid invitations, clean up old ones
        DIR *dir = opendir(filename);
 
@@ -673,7 +678,7 @@ static char *get_value(const char *data, const char *var) {
 }
 
 static char *grep(const char *data, const char *var) {
-       static char value[1024];
+       char value[1024];
 
        const char *p = data;
        size_t varlen = strlen(var);
@@ -713,7 +718,7 @@ static char *grep(const char *data, const char *var) {
 
        memcpy(value, p, e - p);
        value[e - p] = 0;
-       return value;
+       return xstrdup(value);
 }
 
 static bool finalize_join(void) {
@@ -730,10 +735,10 @@ static bool finalize_join(void) {
        }
 
        if(!netname) {
-               const char *net = grep(data, "NetName");
+               char *net = grep(data, "NetName");
 
                if(net) {
-                       netname = xstrdup(net);
+                       netname = net;
 
                        if(!check_netname(netname, true)) {
                                fprintf(stderr, "Unsafe NetName found in invitation!\n");
@@ -774,13 +779,7 @@ make_names:
                goto make_names;
        }
 
-       if(mkdir(confbase, 0777) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
-               return false;
-       }
-
-       if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
+       if(!makedirs(DIR_HOSTS | DIR_CONFBASE | DIR_CACHE)) {
                return false;
        }
 
@@ -1208,14 +1207,8 @@ int cmd_join(int argc, char *argv[]) {
        }
 
        // Make sure confbase exists and is accessible.
-       if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
-               return 1;
-       }
-
-       if(mkdir(confbase, 0777) && errno != EEXIST) {
-               fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
-               return 1;
+       if(!makedirs(DIR_CONFDIR | DIR_CONFBASE)) {
+               return false;
        }
 
        if(access(confbase, R_OK | W_OK | X_OK)) {
@@ -1243,7 +1236,7 @@ int cmd_join(int argc, char *argv[]) {
 
                if(!fgets(line, sizeof(line), stdin)) {
                        fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
-                       return false;
+                       return 1;
                }
 
                invitation = line;