Submitted by:            DJ Lucas (dj_AT_linuxfromscratch_DOT_org)
Date:                    2019-10-04
Initial Package Version: 0.7.1
Upstream Status:         Submitted:
                         https://github.com/firewalld/firewalld/pull/518
Origin:                  Self
Description:             Fixes use with kernel builtins

diff -Naurp firewalld-0.7.2-orig/src/firewall/functions.py firewalld-0.7.2/src/firewall/functions.py
--- firewalld-0.7.2-orig/src/firewall/functions.py	2019-06-11 09:43:12.000000000 -0500
+++ firewalld-0.7.2/src/firewall/functions.py	2019-09-25 22:34:34.282784821 -0500
@@ -348,12 +348,27 @@ def enable_ip_forwarding(ipv):
 
 def get_modinfos(path_templates, prefix):
     kver = os.uname()[2]
+    builtinmods = []
     modules = []
-    for path in (t % kver for t in path_templates):
-        if os.path.isdir(path):
-            for filename in sorted(os.listdir(path)):
+    modulesdir = os.path.join ("/lib/modules/", kver)
+    builtins = os.path.join(modulesdir, "modules.builtin")
+    if os.path.exists(builtins):
+        modlist = open(builtins, "r")
+        for t in path_templates:
+            m = os.path.join (t, prefix)
+            for l in modlist:
+                if re.search(m, l):
+                    builtinmods.append(l.split(".")[0])
+        modlist.close()
+
+    # Get modules
+    for path in (path_templates):
+        p = os.path.join(modulesdir, t)
+        if os.path.isdir(p):
+            for filename in sorted(os.listdir(p)):
                 if filename.startswith(prefix):
                     modules.append(filename.split(".")[0])
+
     if modules:
         # Ignore status as it is not 0 if even one module had problems
         (status, ret) = runProg(COMMANDS["modinfo"], modules)
@@ -368,9 +383,34 @@ def get_modinfos(path_templates, prefix)
         if "filename" in entry:
             yield entry
 
+    if builtinmods:
+        bimodinfo = os.path.join(modulesdir, "modules.builtin.modinfo")
+        ret = ""
+        for mod in builtinmods:
+            ret = ret + "\n" + "filename:\t" + mod
+            modx = os.path.basename(mod)
+            bmods = open(bimodinfo, 'r')
+            for line in bmods.read().split('\0'):
+                if re.search(modx, line):
+                    out = line.split('=')
+                    val = out[1]
+                    keys = out[0].split('.')
+                    key = keys[1]
+                    ret = ret + "\n" + key + ":\t" + val
+            bmods.close()
+        entry = {}
+        for m in re.finditer(r"^(\w+):[ \t]*(\S.*?)[ \t]*$", ret, re.MULTILINE):
+            key, value = m.groups()
+            if key == "filename" and "filename" in entry:
+                yield entry
+                entry = {}
+            entry.setdefault(key, [ ]).append(value)
+        if "filename" in entry:
+            yield entry
+
 def get_nf_conntrack_helpers():
     helpers = { }
-    for modinfo in get_modinfos(["/lib/modules/%s/kernel/net/netfilter/"], "nf_conntrack_"):
+    for modinfo in get_modinfos(["kernel/net/netfilter/"], "nf_conntrack_"):
         filename = modinfo['filename'][0].split("/")[-1]
         name = filename.split(".")[0]
         # If module name matches "nf_conntrack_proto_*"
@@ -393,9 +433,9 @@ def get_nf_conntrack_helpers():
 
 def get_nf_nat_helpers():
     helpers = { }
-    for modinfo in get_modinfos(["/lib/modules/%s/kernel/net/netfilter/",
-                                 "/lib/modules/%s/kernel/net/ipv4/netfilter/",
-                                 "/lib/modules/%s/kernel/net/ipv6/netfilter/"], "nf_nat_"):
+    for modinfo in get_modinfos(["kernel/net/netfilter/",
+                                 "kernel/net/ipv4/netfilter/",
+                                 "kernel/net/ipv6/netfilter/"], "nf_nat_"):
         filename = modinfo['filename'][0].split("/")[-1]
         name = filename.split(".")[0]
         helper = name
