scanner: honor absolute and relative paths via include file
Pablo Neira Ayuso
maintainerPATCH 1/3 scanner: honor absolute and relative paths via include file
If the path refers to an absolute or relative path, do not check for the default include paths, eg. /etc/nftables/.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/scanner.l | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
src/scanner.l +17 -7
@@ -611,6 +611,13 @@ err: return -1; } +static bool search_in_include_path(const char *filename) +{ + return (strncmp(filename, "./", strlen("./") != 0) && + strncmp(filename, "../", strlen("../") != 0) && + filename[0] != '/'); +} + int scanner_include_file(void *scanner, const char *filename, const struct location *loc) {
@@ -622,13 +629,16 @@ int scanner_include_file(void *scanner, const char *filename, FILE *f; f = NULL; - for (i = 0; i < INCLUDE_PATHS_MAX; i++) { - if (include_paths[i] == NULL) - break; - snprintf(buf, sizeof(buf), "%s/%s", include_paths[i], filename); - f = fopen(buf, "r"); - if (f != NULL) - break; + if (search_in_include_path(filename)) { + for (i = 0; i < INCLUDE_PATHS_MAX; i++) { + if (include_paths[i] == NULL) + break; + snprintf(buf, sizeof(buf), "%s/%s", + include_paths[i], filename); + f = fopen(buf, "r"); + if (f != NULL) + break; + } } if (f == NULL) { f = fopen(filename, "r");
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/3 scanner: don't fall back on current directory if include is not found
This resolves an ambiguity if the same file name is used both under sysconfdir and the current working directory.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/scanner.l | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
src/scanner.l +6 -7
@@ -639,16 +639,15 @@ int scanner_include_file(void *scanner, const char *filename, if (f != NULL) break; } - } - if (f == NULL) { + } else { f = fopen(filename, "r"); - if (f == NULL) { - erec = error(loc, "Could not open file \"%s\": %s\n", - filename, strerror(errno)); - goto err; - } name = filename; } + if (f == NULL) { + erec = error(loc, "Could not open file \"%s\": %s\n", + filename, strerror(errno)); + goto err; + } erec = scanner_push_file(scanner, name, f, loc); if (erec != NULL)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/3 scanner: don't break line on include error message
In reply to Pablo Neira AyusoFor consistency with other error messages in this codebase, don't add a line break.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/scanner.l | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/scanner.l +1 -1
@@ -644,7 +644,7 @@ int scanner_include_file(void *scanner, const char *filename, name = filename; } if (f == NULL) { - erec = error(loc, "Could not open file \"%s\": %s\n", + erec = error(loc, "Could not open file \"%s\": %s", filename, strerror(errno)); goto err; }
Signature · 1 line
2.1.4