Skip to contentSkip to conversation

nft include path updates

PATCH New Pablo Neira Ayuso 1 participant 3 messages Jun 15, 2024 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover nft include path updates

Hi,

This patchset updates include path logic of nftables:

Patch #1 adds -f/--filename base directory as implicit include path,
so users do not need to add a redundant -I/--includepath such as:

  # nft -I /path/to/files -f /path/to/files/ruleset.nft

Patch #2 searches for default include path last so users have a way
to override the default include path either via -I/--includepath or the implicit include path added by Patch #1

For instance, assuming you have:

  # cat /path/to/files/ruleset.nft
    include "file1.nft"
    include "file2.nft"
  # ls /path/to/files/
    file1.nft file2.nft

then, make a copy of the ruleset:

  # mkdir update
  # cp -r /path/to/files/* update
  # vim update/file1.nft
  ...
      file edit goes here
  ...
  # nft -f copy/ruleset.nft

Comments welcome, thanks.

Pablo Neira Ayuso (2):
libnftables: add base directory of -f/--filename to include path
libnftables: search for default include path last

 doc/nft.txt       |  2 ++
 src/libnftables.c | 19 +++++++++++++-
 src/scanner.l     | 63 ++++++++++++++++++++++++++++++-----------------
 3 files changed, 61 insertions(+), 23 deletions(-)

--
2.30.2

author maintainer
#1 lore raw

PATCH 1/2 libnftables: add base directory of -f/--filename to include path

This patch adds an include path relative to the current (the including) file's directory.

Users of -f/--filename have to explicitly specify -I with a redundant path to find included files in the main file, eg.

 # nft -I /path/to/files -f /path/to/files/ruleset.nft

Assuming:

 # cat /path/to/files/ruleset.nft
 include "file1.nft"
 include "file2.nft"
 include "file3.nft"
---
 doc/nft.txt       |  2 ++
 src/libnftables.c | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)
doc/nft.txt +2 -0
index e4eb982e75af..3f4593a29831 100644
@@ -43,6 +43,8 @@ understanding of their meaning. You can get information about options by running
 *-f*::
 *--file 'filename'*::
 	Read input from 'filename'. If 'filename' is -, read from stdin.
+	The directory path to this file is inserted at the beginning the list of
+	directories to be searched for included files (see *-I/--includepath*).
 
 *-D*::
 *--define 'name=value'*::
src/libnftables.c +18 -0
index 0dee1bacb0db..40e37bdf8c06 100644
@@ -17,6 +17,7 @@
 #include <cmd.h>
 #include <errno.h>
 #include <sys/stat.h>
+#include <libgen.h>
 
 static int nft_netlink(struct nft_ctx *nft,
 		       struct list_head *cmds, struct list_head *msgs)
@@ -786,6 +787,19 @@ static int nft_run_optimized_file(struct nft_ctx *nft, const char *filename)
 	return __nft_run_cmd_from_filename(nft, filename);
 }
 
+static int nft_ctx_add_basedir_include_path(struct nft_ctx *nft,
+					    const char *filename)
+{
+	const char *basedir = dirname(xstrdup(filename));
+	int ret;
+
+	ret = nft_ctx_add_include_path(nft, basedir);
+
+	free_const(basedir);
+
+	return ret;
+}
+
 EXPORT_SYMBOL(nft_run_cmd_from_filename);
 int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
 {
@@ -798,6 +812,10 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
 	    !nft_output_json(&nft->output))
 		nft->stdin_buf = stdin_to_buffer();
 
+	if (!nft->stdin_buf &&
+	    nft_ctx_add_basedir_include_path(nft, filename) < 0)
+		return -1;
+
 	if (nft->optimize_flags) {
 		ret = nft_run_optimized_file(nft, filename);
 		free_const(nft->stdin_buf);
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/2 libnftables: search for default include path last

In reply to Pablo Neira Ayuso

The default include path is searched for files before include paths specified via -I/--include.

Search for default include path after user-specified include paths to allow users for test nftables configurations spanning multiple files without overwriting the globally installed ones.

See:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

---
 src/libnftables.c |  1 -
 src/scanner.l     | 63 ++++++++++++++++++++++++++++++-----------------
 2 files changed, 41 insertions(+), 23 deletions(-)
src/libnftables.c +0 -1
index 40e37bdf8c06..af4734c05004 100644
@@ -202,7 +202,6 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 	nft_init(ctx);
 
 	ctx->state = xzalloc(sizeof(struct parser_state));
-	nft_ctx_add_include_path(ctx, DEFAULT_INCLUDE_PATH);
 	ctx->parser_max_errors	= 10;
 	cache_init(&ctx->cache.table_cache);
 	ctx->top_scope = scope_alloc();
src/scanner.l +41 -22

80 lines not on this page · raw message

Signature · 1 line
2.30.2