#!/usr/bin/perl
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2013 IPFire Development Team #
# #
# 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 3 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 . #
# #
###############################################################################
use strict;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
#workaround to suppress a warning when a variable is used only once
my @dummy = ( ${Header::colouryellow} );
undef (@dummy);
my %cgiparams=();
my %checked=();
my %selected=();
my $errormessage = '';
my $filename = "${General::swroot}/dnsforward/config";
my $changed = 'no';
my %color = ();
my %mainsettings = ();
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
&Header::showhttpheaders();
$cgiparams{'ENABLED'} = 'off';
$cgiparams{'ACTION'} = '';
$cgiparams{'ZONE'} = '';
$cgiparams{'FORWARD_SERVER'} = '';
$cgiparams{'REMARK'} ='';
&Header::getcgihash(\%cgiparams);
open(FILE, $filename) or die 'Unable to open config file.';
my @current = ;
close(FILE);
###
# Add / Edit entries.
#
if ($cgiparams{'ACTION'} eq $Lang::tr{'add'})
{
# Check if the entered domainname is valid.
unless (&General::validdomainname($cgiparams{'ZONE'})) {
$errormessage = $Lang::tr{'invalid domain name'};
}
# Check if the settings for the forward server are valid.
unless(&General::validip($cgiparams{'FORWARD_SERVER'})) {
$errormessage = $Lang::tr{'invalid ip'};
}
# Go further if there was no error.
if ( ! $errormessage)
{
# Check if a remark has been entered.
$cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
# Check if we want to edit an existing or add a new entry.
if($cgiparams{'EDITING'} eq 'no') {
open(FILE,">>$filename") or die 'Unable to open config file.';
flock FILE, 2;
print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVER'},$cgiparams{'REMARK'}\n";
} else {
open(FILE, ">$filename") or die 'Unable to open config file.';
flock FILE, 2;
my $id = 0;
foreach my $line (@current)
{
$id++;
if ($cgiparams{'EDITING'} eq $id) {
print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVER'},$cgiparams{'REMARK'}\n";
} else { print FILE "$line"; }
}
}
close(FILE);
undef %cgiparams;
$changed = 'yes';
} else {
# stay on edit mode if an error occur
if ($cgiparams{'EDITING'} ne 'no')
{
$cgiparams{'ACTION'} = $Lang::tr{'edit'};
$cgiparams{'ID'} = $cgiparams{'EDITING'};
}
}
# Restart dnsmasq.
system('/usr/local/bin/dnsmasqctrl restart >/dev/null');
}
###
# Remove existing entries.
#
if ($cgiparams{'ACTION'} eq $Lang::tr{'remove'})
{
my $id = 0;
open(FILE, ">$filename") or die 'Unable to open config file.';
flock FILE, 2;
foreach my $line (@current)
{
$id++;
unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
}
close(FILE);
# Restart dnsmasq.
system('/usr/local/bin/dnsmasqctrl restart >/dev/null');
}
###
# Toggle Enable/Disable for entries.
#
if ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'})
{
open(FILE, ">$filename") or die 'Unable to open config file.';
flock FILE, 2;
my $id = 0;
foreach my $line (@current)
{
$id++;
unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
else
{
chomp($line);
my @temp = split(/\,/,$line);
print FILE "$cgiparams{'ENABLE'},$temp[1],$temp[2],$temp[3]\n";
}
}
close(FILE);
# Restart dnsmasq.
system('/usr/local/bin/dnsmasqctrl restart >/dev/null');
}
###
# Read items for edit mode.
#
if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'})
{
my $id = 0;
foreach my $line (@current)
{
$id++;
if ($cgiparams{'ID'} eq $id)
{
chomp($line);
my @temp = split(/\,/,$line);
$cgiparams{'ENABLED'} = $temp[0];
$cgiparams{'ZONE'} = $temp[1];
$cgiparams{'FORWARD_SERVER'} = $temp[2];
$cgiparams{'REMARK'} = $temp[3];
}
}
}
$checked{'ENABLED'}{'off'} = '';
$checked{'ENABLED'}{'on'} = '';
$checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
&Header::openpage($Lang::tr{'dnsforward configuration'}, 1, '');
&Header::openbigbox('100%', 'left', '', $errormessage);
###
# Error messages layout.
#
if ($errormessage) {
&Header::openbox('100%', 'left', $Lang::tr{'error messages'});
print "$errormessage\n";
print " \n";
&Header::closebox();
}
print "\n";
###
# Existing rules.
#
&Header::openbox('100%', 'left', $Lang::tr{'dnsforward entries'});
print <
$Lang::tr{'dnsforward zone'}
$Lang::tr{'dnsforward forward_server'}
$Lang::tr{'remark'}
$Lang::tr{'action'}
END
;
# If something has happened re-read config
if($cgiparams{'ACTION'} ne '' or $changed ne 'no')
{
open(FILE, $filename) or die 'Unable to open config file.';
@current = ;
close(FILE);
}
###
# Re-read entries and highlight selected item for editing.
#
my $id = 0;
my $col="";
foreach my $line (@current)
{
$id++;
chomp($line);
my @temp = split(/\,/,$line);
my $toggle = '';
my $gif = '';
my $gdesc = '';
my $toggle = '';
if($cgiparams{'ACTION'} eq $Lang::tr{'edit'} && $cgiparams{'ID'} eq $id) {
print "
";
$col="bgcolor='$color{'color20'}'"; }
if ($temp[0] eq 'on') { $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};}
else { $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'}; }
###
# Display edit page.
#
print <$temp[1]
$temp[2]
$temp[3]
END
;
}
print "\n";
###
# Print the legend at the bottom if there are any configured entries.
#
# Check if the file size is zero - no existing entries.
if ( ! -z "$filename") {
print <
$Lang::tr{'legend'}:
$Lang::tr{'click to disable'}
$Lang::tr{'click to enable'}
$Lang::tr{'edit'}
$Lang::tr{'remove'}
END
;
}
&Header::closebox();
&Header::closebigbox();
&Header::closepage();