- include: directive in config file accepts wildcards. Patch from

Paul Wouters.  Suggested use: include: "/etc/unbound.d/conf.d/*"


git-svn-id: file:///svn/unbound/trunk@2765 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2012-09-27 08:52:37 +00:00
parent 1ade6965b2
commit 9c4bbfd37d
5 changed files with 345 additions and 180 deletions

View file

@ -1,3 +1,7 @@
27 September 2012: Wouter
- include: directive in config file accepts wildcards. Patch from
Paul Wouters. Suggested use: include: "/etc/unbound.d/conf.d/*"
21 September 2012: Wouter
- chdir to / after chroot call (suggested by Camiel Dobbelaar).

View file

@ -71,7 +71,7 @@ is followed by its containing attributes, or a value.
.P
Files can be included using the
.B include:
directive. It can appear anywhere, and takes a single filename as an argument.
directive. It can appear anywhere, and accepts wildcards as an argument.
Processing continues as if the text from the included file was copied into
the config file at that point. If also using chroot, using full path names
for the included files works, relative pathnames for the included names work

View file

@ -53,6 +53,10 @@
#include "util/regional.h"
#include "util/fptr_wlist.h"
#include "util/data/dname.h"
#ifdef HAVE_GLOB_H
# include <glob.h>
#endif
/** global config during parsing */
struct config_parser_state* cfg_parser = 0;
/** lex in file */
@ -687,8 +691,69 @@ config_read(struct config_file* cfg, const char* filename, const char* chroot)
{
FILE *in;
char *fname = (char*)filename;
#ifdef HAVE_GLOB
glob_t g;
size_t i;
int r, flags;
#endif
if(!fname)
return 1;
/* check for wildcards */
#ifdef HAVE_GLOB
if(!(!strchr(fname, '*') && !strchr(fname, '?') && !strchr(fname, '[') &&
!strchr(fname, '{') && !strchr(fname, '~'))) {
verbose(VERB_QUERY, "wildcard found, processing %s", fname);
flags = 0
#ifdef GLOB_ERR
| GLOB_ERR
#endif
#ifdef GLOB_NOSORT
| GLOB_NOSORT
#endif
#ifdef GLOB_BRACE
| GLOB_BRACE
#endif
#ifdef GLOB_TILDE
| GLOB_TILDE
#endif
;
memset(&g, 0, sizeof(g));
r = glob(fname, flags, NULL, &g);
if(r) {
/* some error */
globfree(&g);
if(r == GLOB_NOMATCH) {
verbose(VERB_QUERY, "include: "
"no matches for %s", fname);
return 1;
} else if(r == GLOB_NOSPACE) {
log_err("include: %s: "
"fnametern out of memory", fname);
} else if(r == GLOB_ABORTED) {
log_err("wildcard include: %s: expansion "
"aborted (%s)", fname, strerror(errno));
} else {
log_err("wildcard include: %s: expansion "
"failed (%s)", fname, strerror(errno));
}
/* ignore globs that yield no files */
return 1;
}
/* process files found, if any */
for(i=0; i<(size_t)g.gl_pathc; i++) {
if(!config_read(cfg, g.gl_pathv[i], chroot)) {
log_err("error reading wildcard "
"include: %s", g.gl_pathv[i]);
globfree(&g);
return 0;
}
}
globfree(&g);
return 1;
}
#endif /* HAVE_GLOB */
in = fopen(fname, "r");
if(!in) {
log_err("Could not open %s: %s", fname, strerror(errno));

File diff suppressed because it is too large Load diff

View file

@ -11,6 +11,9 @@
#include <ctype.h>
#include <string.h>
#include <strings.h>
#ifdef HAVE_GLOB_H
# include <glob.h>
#endif
#include "util/config_file.h"
#include "util/configparser.h"
@ -43,6 +46,7 @@ static int config_include_stack_ptr = 0;
static int inc_prev = 0;
static int num_args = 0;
static void config_start_include(const char* filename)
{
FILE *input;
@ -74,6 +78,50 @@ static void config_start_include(const char* filename)
++config_include_stack_ptr;
}
static void config_start_include_glob(const char* filename)
{
/* check for wildcards */
#ifdef HAVE_GLOB
glob_t g;
size_t i;
int r, flags;
if(!(!strchr(filename, '*') && !strchr(filename, '?') && !strchr(filename, '[') &&
!strchr(filename, '{') && !strchr(filename, '~'))) {
flags = 0
#ifdef GLOB_ERR
| GLOB_ERR
#endif
#ifdef GLOB_NOSORT
| GLOB_NOSORT
#endif
#ifdef GLOB_BRACE
| GLOB_BRACE
#endif
#ifdef GLOB_TILDE
| GLOB_TILDE
#endif
;
memset(&g, 0, sizeof(g));
r = glob(filename, flags, NULL, &g);
if(r) {
/* some error */
globfree(&g);
config_start_include(filename); /* let original deal with it */
return;
}
/* process files found, if any */
for(i=0; i<(size_t)g.gl_pathc; i++) {
config_start_include(g.gl_pathv[i]);
}
globfree(&g);
return;
}
#endif /* HAVE_GLOB */
config_start_include(filename);
}
static void config_end_include(void)
{
--config_include_stack_ptr;
@ -299,7 +347,7 @@ rrset-roundrobin{COLON} { YDVAR(1, VAR_RRSET_ROUNDROBIN) }
<include>\" { LEXOUT(("IQS ")); BEGIN(include_quoted); }
<include>{UNQUOTEDLETTER}* {
LEXOUT(("Iunquotedstr(%s) ", yytext));
config_start_include(yytext);
config_start_include_glob(yytext);
BEGIN(inc_prev);
}
<include_quoted><<EOF>> {
@ -312,7 +360,7 @@ rrset-roundrobin{COLON} { YDVAR(1, VAR_RRSET_ROUNDROBIN) }
<include_quoted>\" {
LEXOUT(("IQE "));
yytext[yyleng - 1] = '\0';
config_start_include(yytext);
config_start_include_glob(yytext);
BEGIN(inc_prev);
}
<INITIAL,val><<EOF>> {