render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
edecca
From da064c59cfc1f35f4638a9e05447c21fea88cb14 Mon Sep 17 00:00:00 2001
edecca
Message-Id: <da064c59cfc1f35f4638a9e05447c21fea88cb14@dist-git>
edecca
From: Laine Stump <laine@laine.org>
edecca
Date: Fri, 1 Feb 2019 20:29:28 -0500
edecca
Subject: [PATCH] util: move all firewalld-specific stuff into its own files
edecca
MIME-Version: 1.0
edecca
Content-Type: text/plain; charset=UTF-8
edecca
Content-Transfer-Encoding: 8bit
edecca
edecca
In preparation for adding several other firewalld-specific functions,
edecca
separate the code that's unique to firewalld from the more-generic
edecca
"firewall" file.
edecca
edecca
Signed-off-by: Laine Stump <laine@laine.org>
edecca
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
edecca
(cherry picked from commit d8393b56e21708c219acc9bcd24a9c22ead4a3b4)
edecca
edecca
 Conflicts:
edecca
   src/util/virerror.c - ; added to end of MACRO in context upstream.
edecca
edecca
https://bugzilla.redhat.com/1650320
edecca
edecca
Signed-off-by: Laine Stump <laine@laine.org>
edecca
Reviewed-by: Ján Tomko <jtomko@redhat.com>
edecca
---
edecca
 include/libvirt/virterror.h |   1 +
edecca
 src/libvirt_private.syms    |   5 ++
edecca
 src/util/Makefile.inc.am    |   3 +
edecca
 src/util/virerror.c         |   3 +-
edecca
 src/util/virfirewall.c      |  86 +-------------------
edecca
 src/util/virfirewalld.c     | 151 ++++++++++++++++++++++++++++++++++++
edecca
 src/util/virfirewalld.h     |  33 ++++++++
edecca
 src/util/virfirewalldpriv.h |  30 +++++++
edecca
 src/util/virfirewallpriv.h  |   2 -
edecca
 tests/virfirewalltest.c     |   2 +
edecca
 10 files changed, 231 insertions(+), 85 deletions(-)
edecca
 create mode 100644 src/util/virfirewalld.c
edecca
 create mode 100644 src/util/virfirewalld.h
edecca
 create mode 100644 src/util/virfirewalldpriv.h
edecca
edecca
diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h
edecca
index 57aadb8d16..30197adc3b 100644
edecca
--- a/include/libvirt/virterror.h
edecca
+++ b/include/libvirt/virterror.h
edecca
@@ -133,6 +133,7 @@ typedef enum {
edecca
     VIR_FROM_PERF = 65,         /* Error from perf */
edecca
     VIR_FROM_LIBSSH = 66,       /* Error from libssh connection transport */
edecca
     VIR_FROM_RESCTRL = 67,      /* Error from resource control */
edecca
+    VIR_FROM_FIREWALLD = 68,    /* Error from firewalld */
edecca
 
edecca
 # ifdef VIR_ENUM_SENTINELS
edecca
     VIR_ERR_DOMAIN_LAST
edecca
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
edecca
index 86846f3b08..57948d8049 100644
edecca
--- a/src/libvirt_private.syms
edecca
+++ b/src/libvirt_private.syms
edecca
@@ -1903,6 +1903,11 @@ virFirewallStartRollback;
edecca
 virFirewallStartTransaction;
edecca
 
edecca
 
edecca
+# util/virfirewalld.h
edecca
+virFirewallDApplyRule;
edecca
+virFirewallDIsRegistered;
edecca
+
edecca
+
edecca
 # util/virfirmware.h
edecca
 virFirmwareFreeList;
edecca
 virFirmwareParse;
edecca
diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am
edecca
index a22265606c..2cef465208 100644
edecca
--- a/src/util/Makefile.inc.am
edecca
+++ b/src/util/Makefile.inc.am
edecca
@@ -57,6 +57,9 @@ UTIL_SOURCES = \
edecca
 	util/virfirewall.c \
edecca
 	util/virfirewall.h \
edecca
 	util/virfirewallpriv.h \
edecca
+	util/virfirewalld.c \
edecca
+	util/virfirewalld.h \
edecca
+	util/virfirewalldpriv.h \
edecca
 	util/virfirmware.c \
edecca
 	util/virfirmware.h \
edecca
 	util/virgettext.c \
edecca
diff --git a/src/util/virerror.c b/src/util/virerror.c
edecca
index f198f27957..1d46fcdc82 100644
edecca
--- a/src/util/virerror.c
edecca
+++ b/src/util/virerror.c
edecca
@@ -140,7 +140,8 @@ VIR_ENUM_IMPL(virErrorDomain, VIR_ERR_DOMAIN_LAST,
edecca
               "Perf", /* 65 */
edecca
               "Libssh transport layer",
edecca
               "Resource control",
edecca
-    )
edecca
+              "FirewallD",
edecca
+              );
edecca
 
edecca
 
edecca
 /*
edecca
diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
edecca
index 10c370a2ae..4d084f6cbf 100644
edecca
--- a/src/util/virfirewall.c
edecca
+++ b/src/util/virfirewall.c
edecca
@@ -29,12 +29,12 @@
edecca
 
edecca
 #include "viralloc.h"
edecca
 #include "virfirewallpriv.h"
edecca
+#include "virfirewalld.h"
edecca
 #include "virerror.h"
edecca
 #include "virutil.h"
edecca
 #include "virstring.h"
edecca
 #include "vircommand.h"
edecca
 #include "virlog.h"
edecca
-#include "virdbus.h"
edecca
 #include "virfile.h"
edecca
 #include "virthread.h"
edecca
 
edecca
@@ -51,11 +51,6 @@ VIR_ENUM_IMPL(virFirewallLayerCommand, VIR_FIREWALL_LAYER_LAST,
edecca
               IPTABLES_PATH,
edecca
               IP6TABLES_PATH);
edecca
 
edecca
-VIR_ENUM_DECL(virFirewallLayerFirewallD)
edecca
-VIR_ENUM_IMPL(virFirewallLayerFirewallD, VIR_FIREWALL_LAYER_LAST,
edecca
-              "eb", "ipv4", "ipv6")
edecca
-
edecca
-
edecca
 struct _virFirewallRule {
edecca
     virFirewallLayer layer;
edecca
 
edecca
@@ -158,7 +153,7 @@ virFirewallValidateBackend(virFirewallBackend backend)
edecca
     VIR_DEBUG("Validating backend %d", backend);
edecca
     if (backend == VIR_FIREWALL_BACKEND_AUTOMATIC ||
edecca
         backend == VIR_FIREWALL_BACKEND_FIREWALLD) {
edecca
-        int rv = virDBusIsServiceRegistered(VIR_FIREWALL_FIREWALLD_SERVICE);
edecca
+        int rv = virFirewallDIsRegistered();
edecca
 
edecca
         VIR_DEBUG("Firewalld is registered ? %d", rv);
edecca
         if (rv < 0) {
edecca
@@ -726,81 +721,8 @@ virFirewallApplyRuleFirewallD(virFirewallRulePtr rule,
edecca
                               bool ignoreErrors,
edecca
                               char **output)
edecca
 {
edecca
-    const char *ipv = virFirewallLayerFirewallDTypeToString(rule->layer);
edecca
-    DBusConnection *sysbus = virDBusGetSystemBus();
edecca
-    DBusMessage *reply = NULL;
edecca
-    virError error;
edecca
-    int ret = -1;
edecca
-
edecca
-    if (!sysbus)
edecca
-        return -1;
edecca
-
edecca
-    memset(&error, 0, sizeof(error));
edecca
-
edecca
-    if (!ipv) {
edecca
-        virReportError(VIR_ERR_INTERNAL_ERROR,
edecca
-                       _("Unknown firewall layer %d"),
edecca
-                       rule->layer);
edecca
-        goto cleanup;
edecca
-    }
edecca
-
edecca
-    if (virDBusCallMethod(sysbus,
edecca
-                          &reply,
edecca
-                          &error,
edecca
-                          VIR_FIREWALL_FIREWALLD_SERVICE,
edecca
-                          "/org/fedoraproject/FirewallD1",
edecca
-                          "org.fedoraproject.FirewallD1.direct",
edecca
-                          "passthrough",
edecca
-                          "sa&s",
edecca
-                          ipv,
edecca
-                          (int)rule->argsLen,
edecca
-                          rule->args) < 0)
edecca
-        goto cleanup;
edecca
-
edecca
-    if (error.level == VIR_ERR_ERROR) {
edecca
-        /*
edecca
-         * As of firewalld-0.3.9.3-1.fc20.noarch the name and
edecca
-         * message fields in the error look like
edecca
-         *
edecca
-         *    name="org.freedesktop.DBus.Python.dbus.exceptions.DBusException"
edecca
-         * message="COMMAND_FAILED: '/sbin/iptables --table filter --delete
edecca
-         *          INPUT --in-interface virbr0 --protocol udp --destination-port 53
edecca
-         *          --jump ACCEPT' failed: iptables: Bad rule (does a matching rule
edecca
-         *          exist in that chain?)."
edecca
-         *
edecca
-         * We'd like to only ignore DBus errors precisely related to the failure
edecca
-         * of iptables/ebtables commands. A well designed DBus interface would
edecca
-         * return specific named exceptions not the top level generic python dbus
edecca
-         * exception name. With this current scheme our only option is todo a
edecca
-         * sub-string match for 'COMMAND_FAILED' on the message. eg like
edecca
-         *
edecca
-         * if (ignoreErrors &&
edecca
-         *     STREQ(error.name,
edecca
-         *           "org.freedesktop.DBus.Python.dbus.exceptions.DBusException") &&
edecca
-         *     STRPREFIX(error.message, "COMMAND_FAILED"))
edecca
-         *    ...
edecca
-         *
edecca
-         * But this risks our error detecting code being broken if firewalld changes
edecca
-         * ever alter the message string, so we're avoiding doing that.
edecca
-         */
edecca
-        if (ignoreErrors) {
edecca
-            VIR_DEBUG("Ignoring error '%s': '%s'",
edecca
-                      error.str1, error.message);
edecca
-        } else {
edecca
-            virReportErrorObject(&error);
edecca
-            goto cleanup;
edecca
-        }
edecca
-    } else {
edecca
-        if (virDBusMessageRead(reply, "s", output) < 0)
edecca
-            goto cleanup;
edecca
-    }
edecca
-
edecca
-    ret = 0;
edecca
-
edecca
- cleanup:
edecca
-    virResetError(&error);
edecca
-    virDBusMessageUnref(reply);
edecca
-    return ret;
edecca
+    /* wrapper necessary because virFirewallRule is a private struct */
edecca
+    return virFirewallDApplyRule(rule->layer, rule->args, rule->argsLen, ignoreErrors, output);
edecca
 }
edecca
 
edecca
 static int
edecca
diff --git a/src/util/virfirewalld.c b/src/util/virfirewalld.c
edecca
new file mode 100644
edecca
index 0000000000..f27ec9c124
edecca
--- /dev/null
edecca
+++ b/src/util/virfirewalld.c
edecca
@@ -0,0 +1,151 @@
edecca
+/*
edecca
+ * virfirewalld.c: support for firewalld (https://firewalld.org)
edecca
+ *
edecca
+ * Copyright (C) 2019 Red Hat, Inc.
edecca
+ *
edecca
+ * This library is free software; you can redistribute it and/or
edecca
+ * modify it under the terms of the GNU Lesser General Public
edecca
+ * License as published by the Free Software Foundation; either
edecca
+ * version 2.1 of the License, or (at your option) any later version.
edecca
+ *
edecca
+ * This library is distributed in the hope that it will be useful,
edecca
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
edecca
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
edecca
+ * Lesser General Public License for more details.
edecca
+ *
edecca
+ * You should have received a copy of the GNU Lesser General Public
edecca
+ * License along with this library.  If not, see
edecca
+ * <http://www.gnu.org/licenses/>.
edecca
+ */
edecca
+
edecca
+#include <config.h>
edecca
+
edecca
+#include <stdarg.h>
edecca
+
edecca
+#include "virfirewall.h"
edecca
+#include "virfirewalld.h"
edecca
+#define LIBVIRT_VIRFIREWALLDPRIV_H_ALLOW
edecca
+#include "virfirewalldpriv.h"
edecca
+#include "virerror.h"
edecca
+#include "virutil.h"
edecca
+#include "virlog.h"
edecca
+#include "virdbus.h"
edecca
+
edecca
+#define VIR_FROM_THIS VIR_FROM_FIREWALLD
edecca
+
edecca
+VIR_LOG_INIT("util.firewalld");
edecca
+
edecca
+/* used to convert virFirewallLayer enum values to strings
edecca
+ * understood by the firewalld.direct "passthrough" method
edecca
+ */
edecca
+VIR_ENUM_DECL(virFirewallLayerFirewallD);
edecca
+VIR_ENUM_IMPL(virFirewallLayerFirewallD, VIR_FIREWALL_LAYER_LAST,
edecca
+              "eb",
edecca
+              "ipv4",
edecca
+              "ipv6",
edecca
+              );
edecca
+
edecca
+
edecca
+/**
edecca
+ * virFirewallDIsRegistered:
edecca
+ *
edecca
+ * Returns 0 if service is registered, -1 on fatal error, or -2 if service is not registered
edecca
+ */
edecca
+int
edecca
+virFirewallDIsRegistered(void)
edecca
+{
edecca
+    return virDBusIsServiceRegistered(VIR_FIREWALL_FIREWALLD_SERVICE);
edecca
+}
edecca
+
edecca
+
edecca
+/**
edecca
+ * virFirewallDApplyRule:
edecca
+ * @layer:        which layer to apply the rule to
edecca
+ * @args:         list of args to send to this layer's passthrough command.
edecca
+ * @argsLen:      number of items in @args
edecca
+ * @ignoreErrors: true to suppress logging of errors and return success
edecca
+ *                false to log errors and return actual status
edecca
+ * @output:       output of the direct passthrough command, if it was successful
edecca
+ */
edecca
+int
edecca
+virFirewallDApplyRule(virFirewallLayer layer,
edecca
+                      char **args, size_t argsLen,
edecca
+                      bool ignoreErrors,
edecca
+                      char **output)
edecca
+{
edecca
+    const char *ipv = virFirewallLayerFirewallDTypeToString(layer);
edecca
+    DBusConnection *sysbus = virDBusGetSystemBus();
edecca
+    DBusMessage *reply = NULL;
edecca
+    virError error;
edecca
+    int ret = -1;
edecca
+
edecca
+    if (!sysbus)
edecca
+        return -1;
edecca
+
edecca
+    memset(&error, 0, sizeof(error));
edecca
+
edecca
+    if (!ipv) {
edecca
+        virReportError(VIR_ERR_INTERNAL_ERROR,
edecca
+                       _("Unknown firewall layer %d"),
edecca
+                       layer);
edecca
+        goto cleanup;
edecca
+    }
edecca
+
edecca
+    if (virDBusCallMethod(sysbus,
edecca
+                          &reply,
edecca
+                          &error,
edecca
+                          VIR_FIREWALL_FIREWALLD_SERVICE,
edecca
+                          "/org/fedoraproject/FirewallD1",
edecca
+                          "org.fedoraproject.FirewallD1.direct",
edecca
+                          "passthrough",
edecca
+                          "sa&s",
edecca
+                          ipv,
edecca
+                          (int)argsLen,
edecca
+                          args) < 0)
edecca
+        goto cleanup;
edecca
+
edecca
+    if (error.level == VIR_ERR_ERROR) {
edecca
+        /*
edecca
+         * As of firewalld-0.3.9.3-1.fc20.noarch the name and
edecca
+         * message fields in the error look like
edecca
+         *
edecca
+         *    name="org.freedesktop.DBus.Python.dbus.exceptions.DBusException"
edecca
+         * message="COMMAND_FAILED: '/sbin/iptables --table filter --delete
edecca
+         *          INPUT --in-interface virbr0 --protocol udp --destination-port 53
edecca
+         *          --jump ACCEPT' failed: iptables: Bad rule (does a matching rule
edecca
+         *          exist in that chain?)."
edecca
+         *
edecca
+         * We'd like to only ignore DBus errors precisely related to the failure
edecca
+         * of iptables/ebtables commands. A well designed DBus interface would
edecca
+         * return specific named exceptions not the top level generic python dbus
edecca
+         * exception name. With this current scheme our only option is todo a
edecca
+         * sub-string match for 'COMMAND_FAILED' on the message. eg like
edecca
+         *
edecca
+         * if (ignoreErrors &&
edecca
+         *     STREQ(error.name,
edecca
+         *           "org.freedesktop.DBus.Python.dbus.exceptions.DBusException") &&
edecca
+         *     STRPREFIX(error.message, "COMMAND_FAILED"))
edecca
+         *    ...
edecca
+         *
edecca
+         * But this risks our error detecting code being broken if firewalld changes
edecca
+         * ever alter the message string, so we're avoiding doing that.
edecca
+         */
edecca
+        if (ignoreErrors) {
edecca
+            VIR_DEBUG("Ignoring error '%s': '%s'",
edecca
+                      error.str1, error.message);
edecca
+        } else {
edecca
+            virReportErrorObject(&error);
edecca
+            goto cleanup;
edecca
+        }
edecca
+    } else {
edecca
+        if (virDBusMessageRead(reply, "s", output) < 0)
edecca
+            goto cleanup;
edecca
+    }
edecca
+
edecca
+    ret = 0;
edecca
+
edecca
+ cleanup:
edecca
+    virResetError(&error);
edecca
+    virDBusMessageUnref(reply);
edecca
+    return ret;
edecca
+}
edecca
diff --git a/src/util/virfirewalld.h b/src/util/virfirewalld.h
edecca
new file mode 100644
edecca
index 0000000000..83fe1149cc
edecca
--- /dev/null
edecca
+++ b/src/util/virfirewalld.h
edecca
@@ -0,0 +1,33 @@
edecca
+/*
edecca
+ * virfirewalld.h: support for firewalld (https://firewalld.org)
edecca
+ *
edecca
+ * Copyright (C) 2019 Red Hat, Inc.
edecca
+ *
edecca
+ * This library is free software; you can redistribute it and/or
edecca
+ * modify it under the terms of the GNU Lesser General Public
edecca
+ * License as published by the Free Software Foundation; either
edecca
+ * version 2.1 of the License, or (at your option) any later version.
edecca
+ *
edecca
+ * This library is distributed in the hope that it will be useful,
edecca
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
edecca
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
edecca
+ * Lesser General Public License for more details.
edecca
+ *
edecca
+ * You should have received a copy of the GNU Lesser General Public
edecca
+ * License along with this library.  If not, see
edecca
+ * <http://www.gnu.org/licenses/>.
edecca
+ */
edecca
+
edecca
+#ifndef LIBVIRT_VIRFIREWALLD_H
edecca
+# define LIBVIRT_VIRFIREWALLD_H
edecca
+
edecca
+# define VIR_FIREWALL_FIREWALLD_SERVICE "org.fedoraproject.FirewallD1"
edecca
+
edecca
+int virFirewallDIsRegistered(void);
edecca
+
edecca
+int virFirewallDApplyRule(virFirewallLayer layer,
edecca
+                          char **args, size_t argsLen,
edecca
+                          bool ignoreErrors,
edecca
+                          char **output);
edecca
+
edecca
+#endif /* LIBVIRT_VIRFIREWALLD_H */
edecca
diff --git a/src/util/virfirewalldpriv.h b/src/util/virfirewalldpriv.h
edecca
new file mode 100644
edecca
index 0000000000..6c03b467c9
edecca
--- /dev/null
edecca
+++ b/src/util/virfirewalldpriv.h
edecca
@@ -0,0 +1,30 @@
edecca
+/*
edecca
+ * virfirewalldpriv.h: private APIs for firewalld
edecca
+ *
edecca
+ * Copyright (C) 2019 Red Hat, Inc.
edecca
+ *
edecca
+ * This library is free software; you can redistribute it and/or
edecca
+ * modify it under the terms of the GNU Lesser General Public
edecca
+ * License as published by the Free Software Foundation; either
edecca
+ * version 2.1 of the License, or (at your option) any later version.
edecca
+ *
edecca
+ * This library is distributed in the hope that it will be useful,
edecca
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
edecca
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
edecca
+ * Lesser General Public License for more details.
edecca
+ *
edecca
+ * You should have received a copy of the GNU Lesser General Public
edecca
+ * License along with this library.  If not, see
edecca
+ * <http://www.gnu.org/licenses/>.
edecca
+ */
edecca
+
edecca
+#ifndef LIBVIRT_VIRFIREWALLDPRIV_H_ALLOW
edecca
+# error "virfirewalldpriv.h may only be included by virfirewalld.c or test suites"
edecca
+#endif /* LIBVIRT_VIRFIREWALLDPRIV_H_ALLOW */
edecca
+
edecca
+#ifndef LIBVIRT_VIRFIREWALLDPRIV_H
edecca
+# define LIBVIRT_VIRFIREWALLDPRIV_H
edecca
+
edecca
+# define VIR_FIREWALL_FIREWALLD_SERVICE "org.fedoraproject.FirewallD1"
edecca
+
edecca
+#endif /* LIBVIRT_VIRFIREWALLDPRIV_H */
edecca
diff --git a/src/util/virfirewallpriv.h b/src/util/virfirewallpriv.h
edecca
index 130aaa1b73..bd6089bcc6 100644
edecca
--- a/src/util/virfirewallpriv.h
edecca
+++ b/src/util/virfirewallpriv.h
edecca
@@ -30,8 +30,6 @@
edecca
 
edecca
 # include "virfirewall.h"
edecca
 
edecca
-# define VIR_FIREWALL_FIREWALLD_SERVICE "org.fedoraproject.FirewallD1"
edecca
-
edecca
 typedef enum {
edecca
     VIR_FIREWALL_BACKEND_AUTOMATIC,
edecca
     VIR_FIREWALL_BACKEND_DIRECT,
edecca
diff --git a/tests/virfirewalltest.c b/tests/virfirewalltest.c
edecca
index e6d68745ae..397abfcc60 100644
edecca
--- a/tests/virfirewalltest.c
edecca
+++ b/tests/virfirewalltest.c
edecca
@@ -30,6 +30,8 @@
edecca
 # include "virbuffer.h"
edecca
 # include "vircommandpriv.h"
edecca
 # include "virfirewallpriv.h"
edecca
+# define LIBVIRT_VIRFIREWALLDPRIV_H_ALLOW
edecca
+# include "virfirewalldpriv.h"
edecca
 # include "virmock.h"
edecca
 # include "virdbuspriv.h"
edecca
 
edecca
-- 
edecca
2.20.1
edecca