diff --git a/include/vuurmuur.h b/include/vuurmuur.h index 59b7cab..751dd98 100644 --- a/include/vuurmuur.h +++ b/include/vuurmuur.h @@ -1589,6 +1589,7 @@ void vrmr_log_record_parse_prefix(struct vrmr_log_record *log_record, char *pref io.c */ FILE *vuurmuur_fopen(const int, const struct vrmr_config *, const char *path, const char *mode); +DIR *vuurmuur_tryopendir(const int debuglvl, const struct vrmr_config *cnf, const char *name); DIR *vuurmuur_opendir(const int, const struct vrmr_config *, const char *); int vrmr_stat_ok(const int, const struct vrmr_config *, const char *, char, char, char); int vrmr_check_pidfile(char *pidfile_location, char *service, pid_t *thepid); diff --git a/lib/io.c b/lib/io.c index 72bb74a..bfaa670 100644 --- a/lib/io.c +++ b/lib/io.c @@ -59,6 +59,17 @@ vuurmuur_fopen(const int debuglvl, const struct vrmr_config *cnf, const char *pa return(fp); } +/* open dir if it exist, don't print errors if it doesn't */ +DIR * +vuurmuur_tryopendir(const int debuglvl, const struct vrmr_config *cnf, const char *name) +{ + if (!(vrmr_stat_ok(debuglvl, cnf, name, VRMR_STATOK_WANT_DIR, + VRMR_STATOK_VERBOSE, VRMR_STATOK_ALLOW_NOTFOUND))) + return(NULL); + + DIR *dir_p = opendir(name); + return(dir_p); +} DIR * vuurmuur_opendir(const int debuglvl, const struct vrmr_config *cnf, const char *name) diff --git a/lib/textdir/textdir_list.c b/lib/textdir/textdir_list.c index 617a55a..53ff9ee 100644 --- a/lib/textdir/textdir_list.c +++ b/lib/textdir/textdir_list.c @@ -433,7 +433,7 @@ char vrmr_debug(__FUNC__, "opening host dir: %s.", hostdir_location); /* this is allowed to fail, if it does, is will be NULL, and will be detected above */ - tb->host_p = vuurmuur_opendir(debuglvl, tb->cfg, hostdir_location); + tb->host_p = vuurmuur_tryopendir(debuglvl, tb->cfg, hostdir_location); /* open the groupdir */ snprintf(groupdir_location, sizeof(groupdir_location), "%s/%s/groups", netdir_location, dir_entry_p->d_name); @@ -441,7 +441,7 @@ char vrmr_debug(__FUNC__, "opening group dir: %s.", groupdir_location); /* this is allowed to fail, if it does, is will be NULL, and will be detected above */ - tb->group_p = vuurmuur_opendir(debuglvl, tb->cfg, groupdir_location); + tb->group_p = vuurmuur_tryopendir(debuglvl, tb->cfg, groupdir_location); snprintf(cur_zonename, sizeof(cur_zonename), "%s.%s", tb->cur_network, tb->cur_zone); @@ -505,7 +505,7 @@ char vrmr_debug(__FUNC__, "opening: %s.", netdir_location); /* this is allowed to fail, if it does, is will be NULL, and will be detected above */ - tb->network_p = vuurmuur_opendir(debuglvl, tb->cfg, netdir_location); + tb->network_p = vuurmuur_tryopendir(debuglvl, tb->cfg, netdir_location); // lets check against regex if(vrmr_validate_zonename(debuglvl, dir_entry_p->d_name, 1, NULL, NULL, NULL, tb->zonename_reg, VRMR_QUIET) == 0) diff --git a/lib/textdir/textdir_plugin.c b/lib/textdir/textdir_plugin.c index 9ac9ccc..02f680b 100644 --- a/lib/textdir/textdir_plugin.c +++ b/lib/textdir/textdir_plugin.c @@ -502,6 +502,19 @@ init_textdir(int debuglvl, void *backend, int type) return(0); } +static int create_dir_if_missing(const char *dir_location) +{ + errno = 0; + if (mkdir(dir_location, 0700) < 0) { + if (errno == EEXIST) + return 0; + + vrmr_error(-1, "Error", "Creating directory %s failed: %s.", + dir_location, strerror(errno)); + return -1; + } + return 0; +} /* add item to the backend @@ -548,7 +561,8 @@ add_textdir(const int debuglvl, void *backend, char *name, int type) } /* create the dirs for zones and networks */ - if(type == VRMR_TYPE_ZONE || type == VRMR_TYPE_NETWORK) + if (type == VRMR_TYPE_ZONE || type == VRMR_TYPE_NETWORK || + type == VRMR_TYPE_HOST || type == VRMR_TYPE_GROUP) { /* split up the name */ if(vrmr_validate_zonename(debuglvl, name, 0, zonename, networkname, hostname, tb->zonename_reg, VRMR_VERBOSE) != 0) @@ -560,64 +574,92 @@ add_textdir(const int debuglvl, void *backend, char *name, int type) return(-1); } - if(type == VRMR_TYPE_ZONE) - { - /* zone dir */ - snprintf(dir_location, sizeof(dir_location), "%s/zones/%s", tb->textdirlocation, zonename); - if(mkdir(dir_location, 0700) < 0) - { - vrmr_error(-1, "Error", "Creating directory %s failed: %s.", dir_location, strerror(errno)); - - free(file_location); - file_location = NULL; - return(-1); - } - - /* network dir */ - snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks", tb->textdirlocation, zonename); - if(mkdir(dir_location, 0700) < 0) - { - vrmr_error(-1, "Error", "Creating directory %s failed: %s.", dir_location, strerror(errno)); + switch (type) { + case VRMR_TYPE_ZONE: + /* zones dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones", + tb->textdirlocation); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } + /* zone dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s", + tb->textdirlocation, zonename); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } - free(file_location); - file_location = NULL; - return(-1); - } - } - else if(type == VRMR_TYPE_NETWORK) - { - /* network dir */ - snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s", tb->textdirlocation, zonename, networkname); - if(mkdir(dir_location, 0700) < 0) - { - vrmr_error(-1, "Error", "Creating directory %s failed: %s.", dir_location, strerror(errno)); + /* network dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks", + tb->textdirlocation, zonename); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } + break; + case VRMR_TYPE_NETWORK: + /* networks dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks", + tb->textdirlocation, zonename); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } - free(file_location); - file_location = NULL; - return(-1); - } + /* network dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s", + tb->textdirlocation, zonename, networkname); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } - /* host dir */ - snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s/hosts", tb->textdirlocation, zonename, networkname); - if(mkdir(dir_location, 0700) < 0) - { - vrmr_error(-1, "Error", "Creating directory %s failed: %s.", dir_location, strerror(errno)); + /* host dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s/hosts", + tb->textdirlocation, zonename, networkname); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } - free(file_location); - file_location = NULL; - return(-1); - } + /* group dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s/groups", + tb->textdirlocation, zonename, networkname); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } + break; - /* group dir */ - snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s/groups", tb->textdirlocation, zonename, networkname); - if(mkdir(dir_location, 0700) < 0) - { - vrmr_error(-1, "Error", "Creating directory %s failed: %s.", dir_location, strerror(errno)); + case VRMR_TYPE_HOST: + case VRMR_TYPE_GROUP: + /* host dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s/hosts", + tb->textdirlocation, zonename, networkname); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } - free(file_location); - file_location = NULL; - return(-1); - } + /* group dir */ + snprintf(dir_location, sizeof(dir_location), "%s/zones/%s/networks/%s/groups", + tb->textdirlocation, zonename, networkname); + if (create_dir_if_missing(dir_location) < 0) { + free(file_location); + file_location = NULL; + return(-1); + } + break; } }