Skip to content

Commit

Permalink
backend: allow for missing dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
victorjulien committed Nov 19, 2017
1 parent 7d6811a commit 9228760
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 56 deletions.
1 change: 1 addition & 0 deletions include/vuurmuur.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions lib/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lib/textdir/textdir_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@ 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);
if(debuglvl >= HIGH)
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);

Expand Down Expand Up @@ -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)
Expand Down
148 changes: 95 additions & 53 deletions lib/textdir/textdir_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 9228760

Please sign in to comment.