]> git.meshlink.io Git - meshlink/blobdiff - src/conf.c
Fix potential double fclose().
[meshlink] / src / conf.c
index 5db2c016e8c5bfcfcde876207961605a0600c2ad..3003a6f4205b17e040d58e3e7ad12e8438804746 100644 (file)
@@ -80,18 +80,21 @@ static bool sync_path(const char *pathname) {
 
        if(fd < 0) {
                logger(NULL, MESHLINK_ERROR, "Failed to open %s: %s\n", pathname, strerror(errno));
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
        if(fsync(fd)) {
                logger(NULL, MESHLINK_ERROR, "Failed to sync %s: %s\n", pathname, strerror(errno));
                close(fd);
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
        if(close(fd)) {
                logger(NULL, MESHLINK_ERROR, "Failed to close %s: %s\n", pathname, strerror(errno));
                close(fd);
+               meshlink_errno = MESHLINK_ESTORAGE;
                return false;
        }
 
@@ -206,6 +209,10 @@ static bool copytree(const char *src_dir_name, const void *src_key, const char *
                return false;
        }
 
+       char src_filename[PATH_MAX];
+       char dst_filename[PATH_MAX];
+       struct dirent *ent;
+
        DIR *src_dir = opendir(src_dir_name);
 
        if(!src_dir) {
@@ -213,29 +220,23 @@ static bool copytree(const char *src_dir_name, const void *src_key, const char *
                return false;
        }
 
-       struct dirent *ent;
+       // Delete if already exists and create a new destination directory
+       deltree(dst_dir_name);
+
+       if(mkdir(dst_dir_name, 0700)) {
+               logger(NULL, MESHLINK_ERROR, "Could not create directory %s\n", dst_filename);
+               return false;
+       }
 
        while((ent = readdir(src_dir))) {
                if(ent->d_name[0] == '.') {
                        continue;
                }
 
-               char src_filename[PATH_MAX];
-               char dst_filename[PATH_MAX];
-
                snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", dst_dir_name, ent->d_name);
                snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", src_dir_name, ent->d_name);
 
                if(ent->d_type == DT_DIR) {
-
-                       // Delete if already exists and create a new destination directory
-                       deltree(dst_filename);
-
-                       if(mkdir(dst_filename, 0700)) {
-                               logger(NULL, MESHLINK_ERROR, "Could create directory %s\n", dst_filename);
-                               return false;
-                       }
-
                        if(!copytree(src_filename, src_key, dst_filename, dst_key)) {
                                logger(NULL, MESHLINK_ERROR, "Copying %s to %s failed\n", src_filename, dst_filename);
                                return false;
@@ -244,7 +245,6 @@ static bool copytree(const char *src_dir_name, const void *src_key, const char *
                        if(!sync_path(dst_filename)) {
                                return false;
                        }
-
                } else if(ent->d_type == DT_REG) {
                        struct stat st;
                        config_t config;
@@ -318,15 +318,6 @@ bool config_copy(meshlink_handle_t *mesh, const char *src_dir_name, const void *
        snprintf(dst_filename, sizeof(dst_filename), "%s" SLASH "%s", mesh->confbase, dst_dir_name);
        snprintf(src_filename, sizeof(src_filename), "%s" SLASH "%s", mesh->confbase, src_dir_name);
 
-       if(main_config_exists(mesh, dst_dir_name)) {
-               deltree(dst_dir_name);
-       }
-
-       if(mkdir(dst_filename, 0700)) {
-               logger(NULL, MESHLINK_ERROR, "Could create directory %s\n", dst_filename);
-               return false;
-       }
-
        return copytree(src_filename, src_key, dst_filename, dst_key);
 }
 
@@ -355,6 +346,31 @@ bool config_rename(meshlink_handle_t *mesh, const char *old_conf_subdir, const c
        return rename(old_path, new_path) == 0;
 }
 
+bool config_sync(meshlink_handle_t *mesh, const char *conf_subdir) {
+       if(!mesh->confbase) {
+               return true;
+       }
+
+       if(!conf_subdir) {
+               return false;
+       }
+
+       char path[PATH_MAX];
+       snprintf(path, sizeof(path), "%s" SLASH "%s" SLASH "hosts", mesh->confbase, conf_subdir);
+
+       if(!sync_path(path)) {
+               return false;
+       }
+
+       snprintf(path, sizeof(path), "%s" SLASH "%s", mesh->confbase, conf_subdir);
+
+       if(!sync_path(path)) {
+               return false;
+       }
+
+       return true;
+}
+
 bool meshlink_confbase_exists(meshlink_handle_t *mesh) {
        if(!mesh->confbase) {
                return false;
@@ -465,7 +481,6 @@ bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const
        if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
                logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
                meshlink_errno = MESHLINK_ESTORAGE;
-               fclose(f);
                return false;
        }
 
@@ -474,7 +489,6 @@ bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config, const
        if(fread(buf, len, 1, f) != 1) {
                logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
                meshlink_errno = MESHLINK_ESTORAGE;
-               fclose(f);
                return false;
        }
 
@@ -583,10 +597,7 @@ bool config_read(meshlink_handle_t *mesh, const char *conf_subdir, const char *n
                return false;
        }
 
-       if(fclose(f)) {
-               logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
-               return false;
-       }
+       fclose(f);
 
        return true;
 }
@@ -631,23 +642,36 @@ bool config_write(meshlink_handle_t *mesh, const char *conf_subdir, const char *
        }
 
        char path[PATH_MAX];
+       char tmp_path[PATH_MAX + 4];
        make_host_path(mesh, conf_subdir, name, path, sizeof(path));
+       snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
 
-       FILE *f = fopen(path, "w");
+       FILE *f = fopen(tmp_path, "w");
 
        if(!f) {
-               logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
+               logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
                return false;
        }
 
        if(!config_write_file(mesh, f, config, key)) {
-               logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
+               logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
+               fclose(f);
+               return false;
+       }
+
+       if(fsync(fileno(f))) {
+               logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
                fclose(f);
                return false;
        }
 
        if(fclose(f)) {
-               logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
+               logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
+               return false;
+       }
+
+       if(rename(tmp_path, path)) {
+               logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
                return false;
        }
 
@@ -676,10 +700,7 @@ bool main_config_read(meshlink_handle_t *mesh, const char *conf_subdir, config_t
                return false;
        }
 
-       if(fclose(f)) {
-               logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
-               return false;
-       }
+       fclose(f);
 
        return true;
 }
@@ -691,23 +712,37 @@ bool main_config_write(meshlink_handle_t *mesh, const char *conf_subdir, const c
        }
 
        char path[PATH_MAX];
+       char tmp_path[PATH_MAX + 4];
        make_main_path(mesh, conf_subdir, path, sizeof(path));
+       snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
 
-       FILE *f = fopen(path, "w");
+       FILE *f = fopen(tmp_path, "w");
 
        if(!f) {
-               logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
+               logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", tmp_path, strerror(errno));
                return false;
        }
 
        if(!config_write_file(mesh, f, config, key)) {
-               logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
+               logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", tmp_path, strerror(errno));
+               fclose(f);
+               return false;
+       }
+
+       if(fsync(fileno(f))) {
+               logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", tmp_path, strerror(errno));
+               fclose(f);
+               return false;
+       }
+
+       if(rename(tmp_path, path)) {
+               logger(mesh, MESHLINK_ERROR, "Failed to rename `%s' to `%s': %s", tmp_path, path, strerror(errno));
                fclose(f);
                return false;
        }
 
        if(fclose(f)) {
-               logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
+               logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", tmp_path, strerror(errno));
                return false;
        }
 
@@ -753,7 +788,7 @@ bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const cha
                return false;
        }
 
-       if(time(NULL) > st.st_mtime + mesh->invitation_timeout) {
+       if(mesh->loop.now.tv_sec > st.st_mtime + mesh->invitation_timeout) {
                logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
                fclose(f);
                unlink(used_path);
@@ -767,10 +802,7 @@ bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const cha
                return false;
        }
 
-       if(fclose(f)) {
-               logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
-               return false;
-       }
+       fclose(f);
 
        unlink(used_path);
        return true;
@@ -798,6 +830,12 @@ bool invitation_write(meshlink_handle_t *mesh, const char *conf_subdir, const ch
                return false;
        }
 
+       if(fsync(fileno(f))) {
+               logger(mesh, MESHLINK_ERROR, "Failed to sync `%s': %s", path, strerror(errno));
+               fclose(f);
+               return false;
+       }
+
        if(fclose(f)) {
                logger(mesh, MESHLINK_ERROR, "Failed to close `%s': %s", path, strerror(errno));
                return false;