/* * Copyright (C) 2013 Red Hat, Inc. * Authors: * Thomas Woerner * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "firewalld.h" #include "functions.h" #include "dbus.h" #include "fwlog.h" #include "fw.h" #include #include static const gchar introspection_xml[] = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; static GDBusConnection *connection; static guint name_id; /* ************************************************************************* */ gchar * dbus_check_zone(GDBusMethodInvocation *invocation, const gchar *zone) { gchar *use_zone = fw_check_zone(zone); if ((size_t) use_zone == -1) g_dbus_method_invocation_return_dbus_error(invocation, DBUS_INTERFACE, "INVALID_ZONE"); return use_zone; } /* ************************************************************************* */ static void dbus_handle_method_call(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { /* g_print(" --> dbus_handle_method_call(%s, %s, %s, %s)\n", sender, object_path, interface_name, method_name); */ if (g_strcmp0(interface_name, DBUS_INTERFACE) == 0) { if (g_strcmp0(method_name, "reload") == 0) { fwlog_debug1("reload"); } else if (g_strcmp0(method_name, "getDefaultZone") == 0) { fwlog_debug1("getDefaultZone()"); g_dbus_method_invocation_return_value( invocation, g_variant_new("(s)", fw_get_default_zone())); } else if (g_strcmp0(method_name, "setDefaultZone") == 0) { gchar *use_zone, *zone; g_variant_get(parameters, "(&s)", &zone); fwlog_debug1("setDefaultZone(%s)", zone); if (zone == NULL || strlen(zone) < 1) { g_dbus_method_invocation_return_dbus_error(invocation, DBUS_INTERFACE, "INVALID_ZONE"); return; } use_zone = dbus_check_zone(invocation, zone); if ((size_t) use_zone == -1) return; if (fw_set_default_zone(use_zone)) { GError *error = NULL; g_dbus_connection_emit_signal(connection, NULL, object_path, interface_name, "DefaultZoneChanged", g_variant_new("(s)", use_zone), &error); g_assert_no_error(error); fwlog_debug1("DefaultZoneChanged('%s')", use_zone); g_dbus_method_invocation_return_value(invocation, NULL); } else { g_dbus_method_invocation_return_dbus_error(invocation, DBUS_INTERFACE, "ZONE_ALREADY_SET"); } } } else if (g_strcmp0(interface_name, DBUS_INTERFACE_ZONE) == 0) { if (g_strcmp0(method_name, "getZones") == 0) { GList *zones; GVariantBuilder builder; int i; fwlog_debug1("getZones()"); zones = fw_get_zones(); g_variant_builder_init(&builder, G_VARIANT_TYPE("as")); for (i=0; i handle_get_property(%s, %s, %s, %s)\n", sender, object_path, interface_name, property_name); */ if (g_strcmp0(interface_name, DBUS_INTERFACE) == 0) { if (g_strcmp0(property_name, "state") == 0) ret = g_variant_new_string("RUNNING"); else if (g_strcmp0(property_name, "version") == 0) ret = g_variant_new_string(VERSION); } else if (g_strcmp0(interface_name, DBUS_INTERFACE_ZONE) == 0) { } return ret; } /* ************************************************************************* */ static gboolean dbus_handle_set_property(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *property_name, GVariant *value, GError **error, gpointer user_data) { /* g_print("--> handle_set_property(%s, %s, %s, %s)\n", sender, object_path, interface_name, property_name); */ if (g_strcmp0(interface_name, DBUS_INTERFACE) == 0) { } else if (g_strcmp0(interface_name, DBUS_INTERFACE_ZONE) == 0) { } return *error == NULL; } /* ************************************************************************* */ static const GDBusInterfaceVTable interface_vtable = { dbus_handle_method_call, dbus_handle_get_property, dbus_handle_set_property }; /* ************************************************************************* */ static void dbus_on_bus_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) { GDBusNodeInfo *introspection_info; GDBusInterfaceInfo *interface_info; guint reg_id; /* introspection */ introspection_info = g_dbus_node_info_new_for_xml(introspection_xml, NULL); g_assert(introspection_info != NULL); /* register interfaces */ interface_info = g_dbus_node_info_lookup_interface(introspection_info, DBUS_INTERFACE); g_assert(interface_info != NULL); reg_id = g_dbus_connection_register_object(connection, DBUS_PATH, interface_info, &interface_vtable, NULL, NULL, NULL); g_assert(reg_id > 0); interface_info = g_dbus_node_info_lookup_interface(introspection_info, DBUS_INTERFACE_ZONE); g_assert(interface_info != NULL); reg_id = g_dbus_connection_register_object(connection, DBUS_PATH, interface_info, &interface_vtable, NULL, NULL, NULL); g_assert(reg_id > 0); /* initialize some variables */ fwlog_debug1("start()"); if (! fw_start()) { fwlog_error("Failed to start."); exit(EXIT_FAILURE); } } /* ************************************************************************* */ static void dbus_on_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) { fwlog_debug10("on_name_acquired(%s)", name); } /* ************************************************************************* */ static void dbus_on_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data) { fwlog_debug10("on_name_lost(%s)", name); } /* ************************************************************************* */ gboolean dbus_own() { GError *error=NULL; /* dbus connection */ error = NULL; connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); if (connection == NULL) { fwlog_error("Failed to connect to system bus: %s", error->message); g_error_free(error); return FALSE; } else { gchar *conn_name; g_object_get(connection, "unique-name", &conn_name, NULL); fwlog_debug2("Connection: '%s'\n", conn_name); g_free(conn_name); } /* do not terminate if remote peer closed connection */ g_dbus_connection_set_exit_on_close(connection, FALSE); /* own name */ name_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, DBUS_INTERFACE, G_BUS_NAME_OWNER_FLAGS_NONE, dbus_on_bus_acquired, dbus_on_name_acquired, dbus_on_name_lost, NULL, NULL); return TRUE; } /* ************************************************************************* */ gboolean dbus_unown() { g_bus_unown_name(name_id); return TRUE; }