From 2d2b1e48eb5f10f64bdc54c3f33eae6f341c215f Mon Sep 17 00:00:00 2001 From: aland Date: Mon, 22 Oct 2007 13:05:04 +0000 Subject: [PATCH] Allow "local" filenames. e.g. $INCLUDE foo.conf is relative to the file that started all of this off. This change should remove much of the "$INCLUDE ${confdir}/foo", and make it "$INCLUDE foo", which is more robust in light of "-d radius_dir" not entirely working... --- src/main/conffile.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/conffile.c b/src/main/conffile.c index fe17489..64ceb18 100644 --- a/src/main/conffile.c +++ b/src/main/conffile.c @@ -1080,6 +1080,29 @@ static int condition_looks_ok(const char **ptr) } +static const char *cf_local_file(CONF_SECTION *cs, const char *local, + char *buffer, size_t bufsize) +{ + size_t dirsize; + const char *p; + CONF_SECTION *parentcs = cf_top_section(cs); + + p = strrchr(parentcs->item.filename, '/'); + if (!p) return local; + + dirsize = (p - parentcs->item.filename); + + if ((dirsize + strlen(local)) >= bufsize) { + return NULL; + } + + memcpy(buffer, parentcs->item.filename, dirsize); + strlcpy(buffer + dirsize, local, bufsize - dirsize); + + return buffer; +} + + /* * Read a part of the config file. */ @@ -1190,6 +1213,17 @@ static int cf_section_read(const char *filename, int *lineno, FILE *fp, value = cf_expand_variables(filename, lineno, this, buf, buf2); if (!value) return -1; + if (value[0] != '/') { + value = cf_local_file(current, value, buf3, + sizeof(buf3)); + if (!value) { + radlog(L_ERR, "%s[%d]: Directories too deep.", + filename, *lineno); + return -1; + } + } + + #ifdef HAVE_DIRENT_H /* * $INCLUDE foo/ @@ -1516,6 +1550,8 @@ int cf_file_include(const char *filename, CONF_SECTION *cs) return -1; } + if (!cs->item.filename) cs->item.filename = filename; + /* * Read the section. It's OK to have EOF without a * matching close brace. -- 2.1.4