/* * 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 "fw.h" #include "functions.h" #include gchar *default_zone; GList *zones; GHashTable *active_zones; /* ************************************************************************* */ gboolean fw_start() { default_zone = g_strdup("public"); zones = g_list_append(zones, "block"); zones = g_list_append(zones, "home"); zones = g_list_append(zones, "public"); zones = g_list_append(zones, "work"); zones = g_list_append(zones, "trusted"); zones = g_list_append(zones, "drop"); active_zones = g_hash_table_new(g_str_hash, g_str_equal); return TRUE; } /* ************************************************************************* */ gboolean fw_stop() { return TRUE; } /* ************************************************************************* */ GList * fw_get_zones() { return zones; } /* ************************************************************************* */ GHashTable * fw_get_active_zones() { return active_zones; } /* ************************************************************************* */ gchar * fw_check_zone(const gchar *zone) { gchar *use_zone; if (zone == NULL || strlen(zone) < 1) use_zone = fw_get_default_zone(); else use_zone = (char *)zone; if (g_list_contains_str(zones, use_zone)) return use_zone; return (gchar *) -1; } /* ************************************************************************* */ gchar * fw_get_default_zone() { return default_zone; } /* ************************************************************************* */ gboolean fw_set_default_zone(const gchar *zone) { if (g_strcmp0(default_zone, zone) != 0) { g_free(default_zone); default_zone = g_strdup(zone); return TRUE; } else return FALSE; } /* ************************************************************************* */ gchar * fw_add_interface(const gchar *zone, const gchar *interface) { gchar *use_zone; GHashTable *hash; GList *interfaces; if (zone == NULL || strlen(zone) < 1) use_zone = fw_get_default_zone(); else use_zone = (char *)zone; hash = g_hash_table_lookup(active_zones, use_zone); if (hash == NULL) { hash = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(active_zones, (gpointer) g_strdup(use_zone), hash); } interfaces = g_hash_table_lookup(hash, "interfaces"); if (g_list_contains_str(interfaces, interface)) return NULL; interfaces = g_list_append(interfaces, (gpointer) g_strdup(interface)); g_hash_table_replace(hash, "interfaces", interfaces); return use_zone; } /* ************************************************************************* */ gchar * fw_remove_interface(const gchar *zone, const gchar *interface) { gchar *use_zone; GHashTable *hash; GList *interfaces; if (zone == NULL || strlen(zone) < 1) use_zone = fw_get_default_zone(); else use_zone = (char *)zone; hash = g_hash_table_lookup(active_zones, use_zone); if (hash != NULL) interfaces = g_hash_table_lookup(hash, "interfaces"); if (hash == NULL || interfaces == NULL || ! g_list_contains_str(interfaces, interface)) { return NULL; } interfaces = g_list_remove_str(interfaces, interface); if (g_list_length(interfaces) == 0) { GList *sources = g_hash_table_lookup(hash, "sources"); if (sources == NULL || g_list_length(sources) == 0) { g_hash_table_remove(active_zones, use_zone); if (sources != NULL) { g_hash_table_remove(hash, "sources"); g_list_free(sources); } g_hash_table_remove(hash, "interfaces"); g_list_free(interfaces); g_hash_table_unref(hash); } } else g_hash_table_replace(hash, "interfaces", interfaces); return use_zone; } /* ************************************************************************* */ gchar * fw_get_zone_of_interface(const gchar *interface) { GHashTableIter iter; gpointer key, value; GList *interfaces; g_hash_table_iter_init(&iter, active_zones); while (g_hash_table_iter_next(&iter, &key, &value)) { interfaces = g_hash_table_lookup(value, "interfaces"); if (g_list_contains_str(interfaces, interface)) return key; } return NULL; } /* ************************************************************************* */ gchar * fw_change_zone_of_interface(const gchar *zone, const gchar *interface) { gchar *use_zone, *old_zone; if (zone == NULL || strlen(zone) < 1) use_zone = fw_get_default_zone(); else use_zone = (char *)zone; old_zone = fw_get_zone_of_interface(interface); if (old_zone != NULL) { if (g_strcmp0(old_zone, use_zone) == 0) return NULL; fw_remove_interface(old_zone, interface); } return fw_add_interface(use_zone, interface); } /* ************************************************************************* */ gboolean fw_query_interface(const gchar *zone, const gchar *interface) { gchar *use_zone; GHashTable *hash; GList *interfaces; if (zone == NULL || strlen(zone) < 1) use_zone = fw_get_default_zone(); else use_zone = (char *)zone; hash = g_hash_table_lookup(active_zones, use_zone); if (hash == NULL) return FALSE; interfaces = g_hash_table_lookup(hash, "interfaces"); if (interfaces != NULL && g_list_contains_str(interfaces, interface)) return TRUE; return FALSE; } /* ************************************************************************* */ GList * fw_get_interfaces(const gchar *zone) { gchar *use_zone; GHashTable *hash; if (zone == NULL || strlen(zone) < 1) use_zone = fw_get_default_zone(); else use_zone = (char *)zone; hash = g_hash_table_lookup(active_zones, use_zone); if (hash == NULL) return NULL; return g_hash_table_lookup(hash, "interfaces"); }