]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
datatype: rt_symbol_table_init() to search for iproute2 configs
authorPhil Sutter <phil@nwl.cc>
Fri, 15 Dec 2023 20:59:44 +0000 (21:59 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 2 Jan 2024 17:29:51 +0000 (18:29 +0100)
There is an ongoing effort among various distributions to tidy up in
/etc. The idea is to reduce contents to just what the admin manually
inserted to customize the system, anything else shall move out to /usr
(or so). The various files in /etc/iproute2 fall in that category as
they are seldomly modified.

The crux is though that iproute2 project seems not quite sure yet where
the files should go. While v6.6.0 installs them into /usr/lib/iproute2,
current mast^Wmain branch uses /usr/share/iproute2. Assume this is going
to stay as /(usr/)lib does not seem right for such files.

Note that rt_symbol_table_init() is not just used for
iproute2-maintained configs but also for connlabel.conf - so retain the
old behaviour when passed an absolute path.

Signed-off-by: Phil Sutter <phil@nwl.cc>
src/datatype.c
src/meta.c
src/rt.c

index 86d55a52426946794ec6ebbb036b12e59f743fa7..9ca0516700f81d9aa7d5528992cea3276bbb608b 100644 (file)
@@ -855,19 +855,47 @@ const struct datatype inet_service_type = {
 
 #define RT_SYM_TAB_INITIAL_SIZE                16
 
+static FILE *open_iproute2_db(const char *filename, char **path)
+{
+       FILE *ret;
+
+       if (filename[0] == '/')
+               return fopen(filename, "r");
+
+       if (asprintf(path, "/etc/iproute2/%s", filename) == -1)
+               goto fail;
+
+       ret = fopen(*path, "r");
+       if (ret)
+               return ret;
+
+       free(*path);
+       if (asprintf(path, "/usr/share/iproute2/%s", filename) == -1)
+               goto fail;
+
+       ret = fopen(*path, "r");
+       if (ret)
+               return ret;
+
+       free(*path);
+fail:
+       *path = NULL;
+       return NULL;
+}
+
 struct symbol_table *rt_symbol_table_init(const char *filename)
 {
+       char buf[512], namebuf[512], *p, *path = NULL;
        struct symbolic_constant s;
        struct symbol_table *tbl;
        unsigned int size, nelems, val;
-       char buf[512], namebuf[512], *p;
        FILE *f;
 
        size = RT_SYM_TAB_INITIAL_SIZE;
        tbl = xmalloc(sizeof(*tbl) + size * sizeof(s));
        nelems = 0;
 
-       f = fopen(filename, "r");
+       f = open_iproute2_db(filename, &path);
        if (f == NULL)
                goto out;
 
@@ -882,7 +910,7 @@ struct symbol_table *rt_symbol_table_init(const char *filename)
                    sscanf(p, "%u %511s\n", &val, namebuf) != 2 &&
                    sscanf(p, "%u %511s #", &val, namebuf) != 2) {
                        fprintf(stderr, "iproute database '%s' corrupted\n",
-                               filename);
+                               path ?: filename);
                        break;
                }
 
@@ -899,6 +927,8 @@ struct symbol_table *rt_symbol_table_init(const char *filename)
 
        fclose(f);
 out:
+       if (path)
+               free(path);
        tbl->symbols[nelems] = SYMBOL_LIST_END;
        return tbl;
 }
@@ -914,7 +944,7 @@ void rt_symbol_table_free(const struct symbol_table *tbl)
 
 void mark_table_init(struct nft_ctx *ctx)
 {
-       ctx->output.tbl.mark = rt_symbol_table_init("/etc/iproute2/rt_marks");
+       ctx->output.tbl.mark = rt_symbol_table_init("rt_marks");
 }
 
 void mark_table_exit(struct nft_ctx *ctx)
index 8d0b7aae9629225511e068f3e7776f68a159a647..6f76f0033a630279cc20b3fe4f5dbd6c310f87e1 100644 (file)
@@ -325,7 +325,7 @@ const struct datatype pkttype_type = {
 
 void devgroup_table_init(struct nft_ctx *ctx)
 {
-       ctx->output.tbl.devgroup = rt_symbol_table_init("/etc/iproute2/group");
+       ctx->output.tbl.devgroup = rt_symbol_table_init("group");
 }
 
 void devgroup_table_exit(struct nft_ctx *ctx)
index f5c80559ffeef37113e044e17f0575c5916ec1d0..3ee710ddc05b59e33a965184582e5f0137d5abec 100644 (file)
--- a/src/rt.c
+++ b/src/rt.c
@@ -25,7 +25,7 @@
 
 void realm_table_rt_init(struct nft_ctx *ctx)
 {
-       ctx->output.tbl.realm = rt_symbol_table_init("/etc/iproute2/rt_realms");
+       ctx->output.tbl.realm = rt_symbol_table_init("rt_realms");
 }
 
 void realm_table_rt_exit(struct nft_ctx *ctx)