/**
 * Copyright 2004, Novell, Inc.  All rights reserved.
 *
 * File:	modules/SuSEFirewall4Network.ycp
 * Package:	Network Configuration
 * Summary:	Module for handling interfaces in SuSEfirewall2
 * Authors:	Lukas Ocilka <locilka@suse.cz>
 *
 * $Id: SuSEFirewall4Network.ycp 21182 2005-02-02 16:37:11Z mvidner $
 *
 * Module for handling network interfaces in SuSEfirewall2 using SuSEFirewall
 * module.
 */

{
    module "SuSEFirewall4Network";
    textdomain "network";

    import "SuSEFirewall";

    /**
     * Function reads configuration of SuSEFirewall.
     *
     * @return	boolean if successful
     */
    global define boolean Read () {
	y2milestone("Reading the firewall configuration");
	return SuSEFirewall::Read();
    }

    /**
     * Function writes configuration of SuSEFirewall.
     *
     * @return	boolean if successful
     */
    global define boolean Write () {
	y2milestone("Writing the firewall configuration");
	return SuSEFirewall::Write();
    }

    /**
     * @return boolean whether enabled and started
     */
    global boolean IsOn () {
	return
	    SuSEFirewall::GetEnableService () &&
	    SuSEFirewall::GetStartService ();
    }

    /**
     * Function returns list of items for combo box with all known
     * firewall zones.
     * There's also an item for "" (no zone or fw off).
     *
     * @return	list <term> of combo box items
     */
    global define list<term> FirewallZonesComboBoxItems () {
	list <term> list_items = [];
	boolean protected_from_internal = SuSEFirewall::GetProtectFromInternalZone();
	string nozone = IsOn ()?
	    // item in combo box Firewall Zone
	    _("No Zone, All Traffic Blocked"):
	    // item in combo box Firewall Zone
	    _("Firewall Disabled");
	list_items = add (list_items, `item (`id (""), nozone));

	// Listing all known zones
	foreach (string zone_shortname, SuSEFirewall::GetKnownFirewallZones(), {
	    // Getting zone name for zone
	    // Informing user about Unprotected inetrnal zone
	    string zone_name = SuSEFirewall::GetZoneFullName(zone_shortname)
		+ ((zone_shortname == "INT" && !protected_from_internal) ?
		    // TRANSLATORS: Part of combo box item -> "Internal Zone (Unprotected)"
		    " " + _("(Unprotected)")
		    :
		    ""
		);
	    list_items = add (list_items, `item(`id(zone_shortname), zone_name));
	});

	return list_items;
    }

    /**
     * Function returns if interface is protected by firewall.
     * It means: Firewall is Running and Enabled. Interface is included
     * in any protected firewall zone (means EXT, DMZ or INT).
     *
     * @param	string interface
     * @return	boolean if it is protected
     */
    global define boolean IsProtectedByFirewall (string interface) {
	string interface_zone	= SuSEFirewall::GetZoneOfInterface(interface);

	// interface is mentioned in uprotected zone
	if (interface_zone == "INT" && !SuSEFirewall::GetProtectFromInternalZone()) {
	    y2warning("Interface '%1' is mentioned in uprotected zone '%2'", interface, "INT");
	}

	// firewall must be running and enabled, interface must be in any zone
	return (IsOn () && interface_zone!=nil);
    }

    /**
     * Function returns the firewall zone of interface, "" if no zone includes
     * the interface. Error is reported when interface is found in multiple
     * firewall zones, then the first appearance is returned.
     * If firewall is off, "" is returned.
     *
     * @param	string interface
     * @return	string zone
     */
    global define string GetZoneOfInterface (string interface) {
	if (!IsOn ())
	{
	    return "";
	}
	string zoi = SuSEFirewall::GetZoneOfInterface (interface);
	return (zoi == nil)? "": zoi;
    }

    /**
     * Functions sets protection of interface by the protect-status.<br>
     * protect==true  -> add interface into selected firewall zone, sets firewall
     *			 to be started and enabled when booting.<br>
     * protect==false -> removes interface from all firewall zones, if there
     *			 are no other interfaces protected by firewall, stops it
     *			 and removes it from boot process.
     *
     * @param	string interface
     * @param	string zone (makes sense for protect_status==true)
     * @param	boolean protect_status
     * @return	boolean if successful
     */
    global define boolean ProtectByFirewall (string interface, string zone, boolean protect_status) {
	// Adding protection
	if (protect_status == true) {
	    y2milestone("Enabling firewall because of '%1' interface", interface);
	    SuSEFirewall::AddInterfaceIntoZone(interface, zone);
	    SuSEFirewall::SetEnableService(true);
	    SuSEFirewall::SetStartService(true);
	// Removing protection
	} else {
	    // removing from all known zones
	    foreach (string remove_from_zone, SuSEFirewall::GetKnownFirewallZones(), {
		SuSEFirewall::RemoveInterfaceFromZone(interface, remove_from_zone);
	    });
	    // if there are no other interfaces in configuration, stop firewall
	    // and remove it from boot process
	    if (size(SuSEFirewall::GetFirewallInterfaces())==0) {
		y2milestone("Disabling firewall, no interfaces are protected.");
		SuSEFirewall::SetEnableService(false);
		SuSEFirewall::SetStartService(false);
	    }
	}

	return true;
    }

/* EOF */
}
