Blame SOURCES/0002-firewall-core-io-functions-add-check_config.patch

e3f863
From 2342548148763cca0579da98ed0a682d22beb49d Mon Sep 17 00:00:00 2001
e3f863
From: Eric Garver <e@erig.me>
e3f863
Date: Fri, 1 Jun 2018 09:37:34 -0400
e3f863
Subject: [PATCH 2/5] firewall/core/io/functions: add check_config()
e3f863
e3f863
This is a utility function to run checks on all the configuration files.
e3f863
e3f863
(cherry picked from commit 4164148b88f1882eabde4eeb4cc9a45506aff0fa)
e3f863
---
e3f863
 po/POTFILES.in                    |  1 +
e3f863
 src/Makefile.am                   |  1 +
e3f863
 src/firewall/core/io/functions.py | 84 +++++++++++++++++++++++++++++++++++++++
e3f863
 3 files changed, 86 insertions(+)
e3f863
 create mode 100644 src/firewall/core/io/functions.py
e3f863
e3f863
diff --git a/po/POTFILES.in b/po/POTFILES.in
e3f863
index 12cdbf2c6929..2332f8acc4eb 100644
e3f863
--- a/po/POTFILES.in
e3f863
+++ b/po/POTFILES.in
e3f863
@@ -70,6 +70,7 @@ src/firewall/core/prog.py
e3f863
 src/firewall/core/watcher.py
e3f863
 src/firewall/core/io/__init__.py
e3f863
 src/firewall/core/io/firewalld_conf.py
e3f863
+src/firewall/core/io/functions.py
e3f863
 src/firewall/core/io/icmptype.py
e3f863
 src/firewall/core/io/io_object.py
e3f863
 src/firewall/core/io/service.py
e3f863
diff --git a/src/Makefile.am b/src/Makefile.am
e3f863
index b249c2e5fd46..b44ae0c1eca4 100644
e3f863
--- a/src/Makefile.am
e3f863
+++ b/src/Makefile.am
e3f863
@@ -34,6 +34,7 @@ nobase_dist_python_DATA = \
e3f863
 	firewall/core/__init__.py \
e3f863
 	firewall/core/io/direct.py \
e3f863
 	firewall/core/io/firewalld_conf.py \
e3f863
+	firewall/core/io/functions.py \
e3f863
 	firewall/core/io/helper.py \
e3f863
 	firewall/core/io/icmptype.py \
e3f863
 	firewall/core/io/ifcfg.py \
e3f863
diff --git a/src/firewall/core/io/functions.py b/src/firewall/core/io/functions.py
e3f863
new file mode 100644
e3f863
index 000000000000..7509a5390e12
e3f863
--- /dev/null
e3f863
+++ b/src/firewall/core/io/functions.py
e3f863
@@ -0,0 +1,84 @@
e3f863
+# -*- coding: utf-8 -*-
e3f863
+#
e3f863
+# Copyright (C) 2018 Red Hat, Inc.
e3f863
+#
e3f863
+# Authors:
e3f863
+# Eric Garver <egarver@redhat.com>
e3f863
+#
e3f863
+# This program is free software; you can redistribute it and/or modify
e3f863
+# it under the terms of the GNU General Public License as published by
e3f863
+# the Free Software Foundation; either version 2 of the License, or
e3f863
+# (at your option) any later version.
e3f863
+#
e3f863
+# This program is distributed in the hope that it will be useful,
e3f863
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
e3f863
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e3f863
+# GNU General Public License for more details.
e3f863
+#
e3f863
+# You should have received a copy of the GNU General Public License
e3f863
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
e3f863
+#
e3f863
+
e3f863
+import os
e3f863
+
e3f863
+from firewall import config
e3f863
+from firewall.errors import FirewallError
e3f863
+
e3f863
+from firewall.core.io.zone import zone_reader
e3f863
+from firewall.core.io.service import service_reader
e3f863
+from firewall.core.io.ipset import ipset_reader
e3f863
+from firewall.core.io.icmptype import icmptype_reader
e3f863
+from firewall.core.io.helper import helper_reader
e3f863
+from firewall.core.io.direct import Direct
e3f863
+from firewall.core.io.lockdown_whitelist import LockdownWhitelist
e3f863
+from firewall.core.io.firewalld_conf import firewalld_conf
e3f863
+
e3f863
+def check_config(fw=None):
e3f863
+    readers = {
e3f863
+        "ipset" : (ipset_reader, [config.FIREWALLD_IPSETS, config.ETC_FIREWALLD_IPSETS]),
e3f863
+        "helper" : (helper_reader, [config.FIREWALLD_HELPERS, config.ETC_FIREWALLD_HELPERS]),
e3f863
+        "icmptype" : (icmptype_reader, [config.FIREWALLD_ICMPTYPES, config.ETC_FIREWALLD_ICMPTYPES]),
e3f863
+        "service" : (service_reader, [config.FIREWALLD_SERVICES, config.ETC_FIREWALLD_SERVICES]),
e3f863
+        "zone" : (zone_reader, [config.FIREWALLD_ZONES, config.ETC_FIREWALLD_ZONES]),
e3f863
+    }
e3f863
+    for reader in readers.keys():
e3f863
+        for dir in readers[reader][1]:
e3f863
+            if not os.path.isdir(dir):
e3f863
+                continue
e3f863
+            for file in sorted(os.listdir(dir)):
e3f863
+                if file.endswith(".xml"):
e3f863
+                    try:
e3f863
+                        obj = readers[reader][0](file, dir)
e3f863
+                        if fw and reader == "zone":
e3f863
+                            obj.fw_config = fw.config
e3f863
+                        obj.check_config(obj.export_config())
e3f863
+                    except FirewallError as error:
e3f863
+                        raise FirewallError(error.code, "'%s': %s" % (file, error.msg))
e3f863
+                    except Exception as msg:
e3f863
+                        raise Exception("'%s': %s" % (file, msg))
e3f863
+    if os.path.isfile(config.FIREWALLD_DIRECT):
e3f863
+        try:
e3f863
+            obj = Direct(config.FIREWALLD_DIRECT)
e3f863
+            obj.read()
e3f863
+            obj.check_config(obj.export_config())
e3f863
+        except FirewallError as error:
e3f863
+            raise FirewallError(error.code, "'%s': %s" % (config.FIREWALLD_DIRECT, error.msg))
e3f863
+        except Exception as msg:
e3f863
+            raise Exception("'%s': %s" % (config.FIREWALLD_DIRECT, msg))
e3f863
+    if os.path.isfile(config.LOCKDOWN_WHITELIST):
e3f863
+        try:
e3f863
+            obj = LockdownWhitelist(config.LOCKDOWN_WHITELIST)
e3f863
+            obj.read()
e3f863
+            obj.check_config(obj.export_config())
e3f863
+        except FirewallError as error:
e3f863
+            raise FirewallError(error.code, "'%s': %s" % (config.LOCKDOWN_WHITELIST, error.msg))
e3f863
+        except Exception as msg:
e3f863
+            raise Exception("'%s': %s" % (config.LOCKDOWN_WHITELIST, msg))
e3f863
+    if os.path.isfile(config.FIREWALLD_CONF):
e3f863
+        try:
e3f863
+            obj = firewalld_conf(config.FIREWALLD_CONF)
e3f863
+            obj.read()
e3f863
+        except FirewallError as error:
e3f863
+            raise FirewallError(error.code, "'%s': %s" % (config.FIREWALLD_CONF, error.msg))
e3f863
+        except Exception as msg:
e3f863
+            raise Exception("'%s': %s" % (config.FIREWALLD_CONF, msg))
e3f863
-- 
e3f863
2.16.3
e3f863