#!/usr/bin/perl

use strict;
use CWeb::GetVars;
use CWeb::TemplProc;
use CWeb::Tools;
use Config::Sh;
use Config::Security;
use Config::List;

my $gv = new CWeb::GetVars;
my $get = $gv->{FORM};
my $p = new CWeb::TemplProc;
my $conf_sec = new Config::Security;
my $fw = new Config::Sh("/etc/firewall/firewall.conf");
$fw->read();
my $sl = new Config::List("/etc/firewall/allow.list",
			  ["name","proto","sport","dport"]);
$sl->read();

# ------------------------- command: ok button-------------------------

if ( $get->{ 'cmd' } eq 'ok' ) {

	$fw->{VARS}->{FW_ENABLE} = $get->{'fw_enabled'} eq "on" ? "yes" : "no";
	$fw->{VARS}->{FW_ALLOW_ALL} = $get->{'fw_selective'} eq "off" ? "yes" : "no";
	$fw->write($fw->{VARS});
	restart_firewall();
}

# ------------------------ command: add / edit / delete services ------------

if ( $get->{ 'cmd' } eq 'add_service' | $get->{ 'cmd' } eq 'edit_service' | $get->{ 'cmd' } eq 'del_service') {

    $fw->{VARS}->{FW_ENABLE} = $get->{'fw_enabled'} eq "on" ? "yes" : "no";
    $fw->{VARS}->{FW_ALLOW_ALL} = $get->{'fw_selective'} eq "off" ? "yes" : "no";
    $fw->write($fw->{VARS});
    restart_firewall();

    if ( $get->{ 'cmd' } eq 'add_service' | $get->{ 'cmd' } eq 'edit_service' ) {
		my $tmpvars = {};
		my $ndx;
	
		if ($get->{sv_edit}) {
		    	$ndx = $get->{sv_edit};
		    	$tmpvars = $sl->{VARS}->{$ndx};
		} 
		else {
		    	$ndx = $sl->{VARS}->{SIZE} + 1;
		    	$tmpvars = {
	    			INDEX => $ndx,
					name  => "",
					proto => "tcpudp",
					sport => "",
					dport => ""
			};
		}
		
		$p->process_template("templates/security_firewall_edit.html",$tmpvars);
		exit(1); 	
    }
    
    elsif ( $get->{ 'cmd' } eq 'del_service' ) {
	
		if (my $ndx = $get->{sv_edit}) { 	# row selected
	    
	    	if (defined($sl->{VARS}->{$ndx})) {
		
				delete($sl->{VARS}->{$ndx});
				$sl->{VARS}->{SIZE} -= 1;
				my $i = 1;
		
				foreach (sort {$a <=> $b} (keys(%{$sl->{VARS}}))) {
		    
		    		if (($_ > 0) && ($_ <= $sl->{VARS}->{SIZE}+1)) {
						my $old = $sl->{VARS}->{$_};
						$sl->{VARS}->{$i++} = {
					    	   	INDEX => $i,
					       		name  => $old->{name},
					       		proto => $old->{proto},
					       		sport => $old->{sport},
					       		dport => $old->{dport}
						};
		    		}
				}
		
				delete($sl->{VARS}->{$sl->{VARS}->{SIZE}+1});
				$sl->write($sl->{VARS});
				restart_firewall();
			}
		}
	}
} 

# --------------------------------- command: save service -------------------------

if ( $get->{ 'cmd' } eq 'save_service' ) {

#	open (TMP, ">> /tmp/2");
#	print TMP "$get->{action}\n";

    # ------------------------- Add/Modify Service ------------------------

   my $ndx = $get->{index};
   $sl->{VARS}->{SIZE} = $ndx if $ndx > $sl->{VARS}->{SIZE}; # was an "add"
   $sl->{VARS}->{$ndx} = { 
   			INDEX => $ndx,
		    name  => $get->{name},
		    proto => $get->{proto},
		    sport => $get->{sport},
		    dport => $get->{dport}
    };

    $sl->write($sl->{VARS});
    restart_firewall();
}

my $sec_val = $conf_sec->values_get();
$sec_val->{FW_SERVICES} = $sl->{VARS};
$sec_val->{FW_SETTINGS} = $fw->{VARS};
$p->process_template( "templates/security_firewall.html", $sec_val );

sub restart_firewall {
    `/etc/init.d/simple-firewall restart > /dev/null 2>&1` ;
    #system("/etc/init.d/firewall","restart");
}
