Skip to contentSkip to conversation

scanner: honor absolute and relative paths via include file

PATCH New Pablo Neira Ayuso 1 participant 3 messages Aug 9, 2016 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 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/.

---
 src/scanner.l | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
src/scanner.l +17 -7
index 88669d0..6f1a551 100644
@@ -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
author maintainer
#1 lore raw

PATCH 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.

---
 src/scanner.l | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
src/scanner.l +6 -7
index 6f1a551..cb2ea32 100644
@@ -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
author maintainer
#2 lore raw

PATCH 3/3 scanner: don't break line on include error message

In reply to Pablo Neira Ayuso

For consistency with other error messages in this codebase, don't add a line break.

---
 src/scanner.l | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/scanner.l +1 -1
index cb2ea32..613c3c9 100644
@@ -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