Merge branch 'next-geoip' into core-90-geoip

This commit is contained in:
Stefan Schantl
2015-04-15 17:10:49 +02:00
28 changed files with 1669 additions and 24 deletions

View File

@@ -33,6 +33,7 @@ no warnings 'uninitialized';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
require "${General::swroot}/geoip-functions.pl";
require "/usr/lib/firewall/firewall-lib.pl";
unless (-d "${General::swroot}/firewall") { system("mkdir ${General::swroot}/firewall"); }
@@ -47,6 +48,7 @@ my %defaultNetworks=();
my %netsettings=();
my %customhost=();
my %customgrp=();
my %customgeoipgrp=();
my %customnetworks=();
my %customservice=();
my %customservicegrp=();
@@ -74,6 +76,7 @@ my $color;
my $confignet = "${General::swroot}/fwhosts/customnetworks";
my $confighost = "${General::swroot}/fwhosts/customhosts";
my $configgrp = "${General::swroot}/fwhosts/customgroups";
my $configgeoipgrp = "${General::swroot}/fwhosts/customgeoipgrp";
my $configsrv = "${General::swroot}/fwhosts/customservices";
my $configsrvgrp = "${General::swroot}/fwhosts/customservicegrp";
my $configccdnet = "${General::swroot}/ovpn/ccd.conf";
@@ -1060,6 +1063,54 @@ END
}
print"</select></td>";
}
# geoip locations / groups.
my @geoip_locations = &fwlib::get_geoip_locations();
print "<tr>\n";
print "<td valign='top'><input type='radio' name='$grp' id='cust_geoip_$srctgt' value='cust_geoip_$srctgt' $checked{$grp}{'cust_geoip_'.$srctgt}></td>\n";
print "<td>$Lang::tr{'geoip'}</td>\n";
print "<td align='right'><select name='cust_geoip_$srctgt' style='width:200px;'>\n";
# Add GeoIP groups to dropdown.
if (!-z $configgeoipgrp) {
print "<optgroup label='$Lang::tr{'fwhost cust geoipgroup'}'>\n";
foreach my $key (sort { ncmp($customgeoipgrp{$a}[0],$customgeoipgrp{$b}[0]) } keys %customgeoipgrp) {
my $selected;
# Generate stored value for select detection.
my $stored = join(':', "group",$customgeoipgrp{$key}[0]);
# Only show a group once and group with elements.
if($helper ne $customgeoipgrp{$key}[0] && $customgeoipgrp{$key}[2] ne 'none') {
# Mark current entry as selected.
if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $stored) {
$selected = "selected='selected'";
}
print"<option $selected value='group:$customgeoipgrp{$key}[0]'>$customgeoipgrp{$key}[0]</option>\n";
}
$helper=$customgeoipgrp{$key}[0];
}
print "</optgroup>\n";
}
# Add locations.
print "<optgroup label='$Lang::tr{'fwhost cust geoiplocation'}'>\n";
foreach my $location (@geoip_locations) {
# Get country name.
my $country_name = &GeoIP::get_full_country_name($location);
# Mark current entry as selected.
my $selected;
if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $location) {
$selected = "selected='selected'";
}
print "<option $selected value='$location'>$location - $country_name</option>\n";
}
print "</optgroup>\n";
# Close GeoIP dropdown.
print "</select></td>\n";
#End left table. start right table (vpn)
print"</tr></table></td><td valign='top'><table width='95%' border='0' align='right'><tr>";
# CCD networks
@@ -1397,6 +1448,7 @@ sub newrule
&General::readhasharray("$confighost", \%customhost);
&General::readhasharray("$configccdhost", \%ccdhost);
&General::readhasharray("$configgrp", \%customgrp);
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp);
&General::readhasharray("$configipsec", \%ipsecconf);
&General::get_aliases(\%aliases);
my %checked=();
@@ -2525,6 +2577,13 @@ END
}else{
print $$hash{$key}[4];
}
}elsif ($$hash{$key}[3] eq 'cust_geoip_src') {
my ($split1,$split2) = split(":", $$hash{$key}[4]);
if ($split2) {
print "$split2\n";
}else{
print "$Lang::tr{'geoip'}: $$hash{$key}[4]\n";
}
}elsif ($$hash{$key}[4] eq 'RED1'){
print "$ipfireiface $Lang::tr{'fwdfw red'}";
}elsif ($$hash{$key}[4] eq 'ALL'){
@@ -2601,6 +2660,13 @@ END
}else{
print $$hash{$key}[6];
}
}elsif ($$hash{$key}[5] eq 'cust_geoip_tgt') {
my ($split1,$split2) = split(":", $$hash{$key}[6]);
if ($split2) {
print "$split2\n";
}else{
print "$Lang::tr{'geoip'}: $$hash{$key}[6]\n";
}
}elsif ($$hash{$key}[5] eq 'tgt_addr'){
my ($split1,$split2) = split("/",$$hash{$key}[6]);
if ($split2 eq '32'){
@@ -2618,7 +2684,6 @@ END
#RULE ACTIVE
if($$hash{$key}[2] eq 'ON'){
$gif="/images/on.gif"
}else{
$gif="/images/off.gif"
}

View File

@@ -27,6 +27,8 @@ use Sort::Naturally;
use CGI::Carp 'fatalsToBrowser';
no warnings 'uninitialized';
require '/var/ipfire/general-functions.pl';
require "/var/ipfire/geoip-functions.pl";
require "/usr/lib/firewall/firewall-lib.pl";
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
@@ -36,6 +38,7 @@ my %customhost=();
my %customgrp=();
my %customservice=();
my %customservicegrp=();
my %customgeoipgrp=();
my %ccdnet=();
my %ccdhost=();
my %ipsecconf=();
@@ -62,6 +65,7 @@ my $configccdhost = "${General::swroot}/ovpn/ovpnconfig";
my $configipsec = "${General::swroot}/vpn/config";
my $configsrv = "${General::swroot}/fwhosts/customservices";
my $configsrvgrp = "${General::swroot}/fwhosts/customservicegrp";
my $configgeoipgrp = "${General::swroot}/fwhosts/customgeoipgrp";
my $fwconfigfwd = "${General::swroot}/firewall/config";
my $fwconfiginp = "${General::swroot}/firewall/input";
my $fwconfigout = "${General::swroot}/firewall/outgoing";
@@ -73,6 +77,7 @@ unless (-e $confighost) { system("touch $confighost"); }
unless (-e $configgrp) { system("touch $configgrp"); }
unless (-e $configsrv) { system("touch $configsrv"); }
unless (-e $configsrvgrp) { system("touch $configsrvgrp"); }
unless (-e $configgeoipgrp) { system("touch $configgeoipgrp"); }
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
@@ -671,6 +676,87 @@ if ($fwhostsettings{'ACTION'} eq 'savegrp')
&addgrp;
&viewtablegrp;
}
if ($fwhostsettings{'ACTION'} eq 'savegeoipgrp')
{
my $grp=$fwhostsettings{'grp_name'};
my $rem=$fwhostsettings{'remark'};
my $count;
my $type;
my @target;
my @newgrp;
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp);
&General::readhasharray("$fwconfigfwd", \%fwfwd);
&General::readhasharray("$fwconfiginp", \%fwinp);
&General::readhasharray("$fwconfigout", \%fwout);
# Check for existing group name.
if (!&checkgroup($grp) && $fwhostsettings{'update'} ne 'on'){
$errormessage = $Lang::tr{'fwhost err grpexist'};
}
# Check remark.
if ($rem ne '' && !&validremark($rem) && $fwhostsettings{'update'} ne 'on'){
$errormessage = $Lang::tr{'fwhost err remark'};
}
if ($fwhostsettings{'update'} eq 'on'){
@target=$fwhostsettings{'COUNTRY_CODE'};
$type='GeoIP Group';
#check if host/net exists in grp
my $test="$grp,$fwhostsettings{'oldremark'},@target";
foreach my $key (keys %customgeoipgrp) {
my $test1="$customgeoipgrp{$key}[0],$customgeoipgrp{$key}[1],$customgeoipgrp{$key}[2]";
if ($test1 eq $test){
$errormessage=$Lang::tr{'fwhost err isingrp'};
$fwhostsettings{'update'} = 'on';
}
}
}
if (!$errormessage){
#on first save, we have an empty @target, so fill it with nothing
my $targetvalues=@target;
if ($targetvalues == '0'){
@target="none";
}
#on update, we have to delete the dummy entry
foreach my $key (keys %customgeoipgrp){
if ($customgeoipgrp{$key}[0] eq $grp && $customgeoipgrp{$key}[2] eq "none"){
delete $customgeoipgrp{$key};
last;
}
}
&General::writehasharray("$configgeoipgrp", \%customgeoipgrp);
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp);
#create array with new lines
foreach my $line (@target){
push (@newgrp,"$grp,$rem,$line");
}
#append new entries
my $key = &General::findhasharraykey (\%customgeoipgrp);
foreach my $line (@newgrp){
foreach my $i (0 .. 3) { $customgeoipgrp{$key}[$i] = "";}
my ($a,$b,$c,$d) = split (",",$line);
$customgeoipgrp{$key}[0] = $a;
$customgeoipgrp{$key}[1] = $b;
$customgeoipgrp{$key}[2] = $c;
$customgeoipgrp{$key}[3] = $type;
}
&General::writehasharray("$configgeoipgrp", \%customgeoipgrp);
#update counter in Host/Net
$fwhostsettings{'update'}='on';
}
#check if ruleupdate is needed
my $geoipgrpcount=0;
$geoipgrpcount=&getgeoipcount($grp);
if($geoipgrpcount > 0 )
{
&General::firewall_config_changed();
}
&addgeoipgrp;
&viewtablegeoipgrp;
}
if ($fwhostsettings{'ACTION'} eq 'saveservice')
{
my $ICMP;
@@ -798,6 +884,12 @@ if ($fwhostsettings{'ACTION'} eq 'editgrp')
&addgrp;
&viewtablegrp;
}
if ($fwhostsettings{'ACTION'} eq 'editgeoipgrp')
{
$fwhostsettings{'update'}='on';
&addgeoipgrp;
&viewtablegeoipgrp;
}
if ($fwhostsettings{'ACTION'} eq 'editservice')
{
$fwhostsettings{'updatesrv'}='on';
@@ -830,6 +922,12 @@ if ($fwhostsettings{'ACTION'} eq 'resetgrp')
$fwhostsettings{'remark'} ="";
&showmenu;
}
if ($fwhostsettings{'ACTION'} eq 'resetgeoipgrp')
{
$fwhostsettings{'grp_name'} ="";
$fwhostsettings{'remark'} ="";
&showmenu;
}
# delete
if ($fwhostsettings{'ACTION'} eq 'delnet')
{
@@ -887,6 +985,37 @@ if ($fwhostsettings{'ACTION'} eq 'deletegrphost')
&addgrp;
&viewtablegrp;
}
if ($fwhostsettings{'ACTION'} eq 'deletegeoipgrpentry')
{
my $grpremark;
my $grpname;
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp);
foreach my $key (keys %customgeoipgrp){
if($customgeoipgrp{$key}[0].",".$customgeoipgrp{$key}[1].",".$customgeoipgrp{$key}[2].",".$customgeoipgrp{$key}[3] eq $fwhostsettings{'delentry'}){
$grpname=$customgeoipgrp{$key}[0];
$grpremark=$customgeoipgrp{$key}[1];
#check if we delete the last entry, then generate dummy
if ($fwhostsettings{'last'} eq 'on'){
$customgeoipgrp{$key}[1] = '';
$customgeoipgrp{$key}[2] = 'none';
$customgeoipgrp{$key}[3] = '';
$fwhostsettings{'last'}='';
last;
}else{
delete $customgeoipgrp{$key};
}
}
}
&General::writehasharray("$configgeoipgrp", \%customgeoipgrp);
&General::firewall_config_changed();
if ($fwhostsettings{'update'} eq 'on'){
$fwhostsettings{'remark'}= $grpremark;
$fwhostsettings{'grp_name'}=$grpname;
}
&addgeoipgrp;
&viewtablegeoipgrp;
}
if ($fwhostsettings{'ACTION'} eq 'delgrp')
{
&General::readhasharray("$configgrp", \%customgrp);
@@ -903,6 +1032,22 @@ if ($fwhostsettings{'ACTION'} eq 'delgrp')
&addgrp;
&viewtablegrp;
}
if ($fwhostsettings{'ACTION'} eq 'delgeoipgrp')
{
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp);
&decrease($fwhostsettings{'grp_name'});
foreach my $key (sort keys %customgeoipgrp)
{
if($customgeoipgrp{$key}[0] eq $fwhostsettings{'grp_name'})
{
delete $customgeoipgrp{$key};
}
}
&General::writehasharray("$configgeoipgrp", \%customgeoipgrp);
$fwhostsettings{'grp_name'}='';
&addgeoipgrp;
&viewtablegeoipgrp;
}
if ($fwhostsettings{'ACTION'} eq 'delservice')
{
&General::readhasharray("$configsrv", \%customservice);
@@ -977,6 +1122,11 @@ if ($fwhostsettings{'ACTION'} eq $Lang::tr{'fwhost newgrp'})
&addgrp;
&viewtablegrp;
}
if ($fwhostsettings{'ACTION'} eq $Lang::tr{'fwhost newgeoipgrp'})
{
&addgeoipgrp;
&viewtablegeoipgrp;
}
if ($fwhostsettings{'ACTION'} eq $Lang::tr{'fwhost newservice'})
{
&addservice;
@@ -1011,6 +1161,31 @@ if ($fwhostsettings{'ACTION'} eq 'changegrpremark')
&addgrp;
&viewtablegrp;
}
if ($fwhostsettings{'ACTION'} eq 'changegeoipgrpremark')
{
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp);
if ($fwhostsettings{'oldrem'} ne $fwhostsettings{'newrem'} && (&validremark($fwhostsettings{'newrem'}) || $fwhostsettings{'newrem'} eq '')){
foreach my $key (sort keys %customgeoipgrp)
{
if($customgeoipgrp{$key}[0] eq $fwhostsettings{'grp'} && $customgeoipgrp{$key}[1] eq $fwhostsettings{'oldrem'})
{
$customgeoipgrp{$key}[1]='';
$customgeoipgrp{$key}[1]=$fwhostsettings{'newrem'};
}
}
&General::writehasharray("$configgeoipgrp", \%customgeoipgrp);
$fwhostsettings{'update'}='on';
$fwhostsettings{'remark'}=$fwhostsettings{'newrem'};
}else{
$errormessage=$Lang::tr{'fwhost err remark'};
$fwhostsettings{'remark'}=$fwhostsettings{'oldrem'};
$fwhostsettings{'grp_name'}=$fwhostsettings{'grp'};
$fwhostsettings{'update'} = 'on';
}
$fwhostsettings{'grp_name'}=$fwhostsettings{'grp'};
&addgeoipgrp;
&viewtablegeoipgrp;
}
if ($fwhostsettings{'ACTION'} eq 'changesrvgrpremark')
{
&General::readhasharray("$configsrvgrp", \%customservicegrp );
@@ -1085,6 +1260,29 @@ if ($fwhostsettings{'ACTION'} eq 'changegrpname')
&addgrp;
&viewtablegrp;
}
if ($fwhostsettings{'ACTION'} eq 'changegeoipgrpname')
{
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp );
if ($fwhostsettings{'oldgrpname'} ne $fwhostsettings{'grp'}){
#Check new groupname
if (!&validhostname($fwhostsettings{'grp'})){
$errormessage.=$Lang::tr{'fwhost err name'}."<br>";
}
if (!$errormessage){
# Rename group.
foreach my $key (keys %customgeoipgrp) {
if($customgeoipgrp{$key}[0] eq $fwhostsettings{'oldgrpname'}){
$customgeoipgrp{$key}[0]=$fwhostsettings{'grp'};
}
}
&General::writehasharray("$configgeoipgrp", \%customgeoipgrp );
#change name in FW Rules
&changenameinfw($fwhostsettings{'oldgrpname'},$fwhostsettings{'grp'},6);
}
}
&addgeoipgrp;
&viewtablegeoipgrp;
}
### VIEW ###
if($fwhostsettings{'ACTION'} eq '')
{
@@ -1096,7 +1294,7 @@ sub showmenu {
print "$Lang::tr{'fwhost welcome'}";
print<<END;
<br><br><table border='0' width='100%'>
<tr><td><form method='post'><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newnet'}' ><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newhost'}' ><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newgrp'}' ></form></td>
<tr><td><form method='post'><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newnet'}' ><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newhost'}' ><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newgrp'}' ><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newgeoipgrp'}' ></form></td>
<td align='right'><form method='post'><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newservice'}' ><input type='submit' name='ACTION' value='$Lang::tr{'fwhost newservicegrp'}' ></form></td></tr>
<tr><td colspan='6'></td></tr></table>
END
@@ -1381,6 +1579,113 @@ END
print"<tr><td style='text-align:right;'><input type='submit' value='$Lang::tr{'add'}' style='min-width:100px;' /><input type='hidden' name='oldremark' value='$fwhostsettings{'oldremark'}'><input type='hidden' name='update' value=\"$fwhostsettings{'update'}\"><input type='hidden' name='ACTION' value='savegrp' ></form><form method='post' style='display:inline'><input type='submit' value='$Lang::tr{'fwhost back'}' style='min-width:100px;'><input type='hidden' name='ACTION' value='resetgrp'></form></td></table>";
&Header::closebox();
}
sub addgeoipgrp
{
&hint;
&error;
&showmenu;
&Header::openbox('100%', 'left', $Lang::tr{'fwhost addgeoipgrp'});
my %checked=();
my $show='';
$checked{'check1'}{'off'} = '';
$checked{'check1'}{'on'} = '';
$checked{'grp2'}{$fwhostsettings{'grp2'}} = 'CHECKED';
$fwhostsettings{'oldremark'}=$fwhostsettings{'remark'};
$fwhostsettings{'oldgrpname'}=$fwhostsettings{'grp_name'};
my $grp=$fwhostsettings{'grp_name'};
my $rem=$fwhostsettings{'remark'};
if ($fwhostsettings{'update'} eq ''){
print<<END;
<table width='100%' border='0'>
<tr>
<td style='width:15%;'>$Lang::tr{'fwhost addgrpname'}</td>
<td><form method='post'><input type='TEXT' name='grp_name' value='$fwhostsettings{'grp_name'}' size='30'></td>
</tr>
<tr>
<td>$Lang::tr{'remark'}:</td>
<td ><input type='TEXT' name='remark' value='$fwhostsettings{'remark'}' style='width: 99%;'></td>
</tr>
<tr>
<td colspan='2'><br></td>
</tr>
</table>
END
} else {
print<<END;
<table width='100%' border='0'>
<form method='post'><tr>
<td style='width:15%;'>$Lang::tr{'fwhost addgrpname'}</td>
<td style='width:30%;'><input type='TEXT' name='grp' value='$fwhostsettings{'grp_name'}' size='30'></td>
<td>
<input type='submit' value='$Lang::tr{'fwhost change'}'>
<input type='hidden' name='oldgrpname' value='$fwhostsettings{'oldgrpname'}'>
<input type='hidden' name='ACTION' value='changegeoipgrpname'>
</td>
<td></td>
</tr></form>
<tr><form method='post' style='display:inline'>
<td>$Lang::tr{'remark'}:</td>
<td colspan='2' style='width:98%;'>
<input type='TEXT' name='newrem' value='$fwhostsettings{'remark'}' style='width:98%;'>
</td>
<td align='right'>
<input type='submit' value='$Lang::tr{'fwhost change'}'>
<input type='hidden' name='grp' value='$fwhostsettings{'grp_name'}'>
<input type='hidden' name='oldrem' value='$fwhostsettings{'oldremark'}'>
<input type='hidden' name='ACTION' value='changegeoipgrpremark'>
</td>
</tr></form>
</table>
<br><br>
END
}
if ($fwhostsettings{'update'} eq 'on') {
my @geoip_locations = &fwlib::get_geoip_locations();
print<<END;
<form method='post'>
<input type='hidden' name='remark' value='$rem'>
<input type='hidden' name='grp_name' value='$grp'>
<table width='100%' border='0'>
<tr>
<td style='text-align:left;'>
<select name='COUNTRY_CODE' style='width:16em;'>";
END
foreach my $location (@geoip_locations) {
# Get full country name.
my $fullname = &GeoIP::get_full_country_name($location);
print"<option value='$location'>$location - $fullname</option>\n";
}
print <<END;
</select>
</td>
</tr>
</table>
<br><br>
END
}
print <<END;
<table width='100%'>
<tr><td style='text-align:right;'>
<input type='submit' value='$Lang::tr{'add'}' style='min-width:100px;' />
<input type='hidden' name='oldremark' value='$fwhostsettings{'oldremark'}'>
<input type='hidden' name='update' value=\"$fwhostsettings{'update'}\">
<input type='hidden' name='ACTION' value='savegeoipgrp' >
</form>
<form method='post' style='display:inline'>
<input type='submit' value='$Lang::tr{'fwhost back'}' style='min-width:100px;'>
<input type='hidden' name='ACTION' value='resetgeoipgrp'>
</form>
</td></tr></table>
END
&Header::closebox();
}
sub addservice
{
&error;
@@ -1838,6 +2143,195 @@ sub viewtablegrp
&Header::closebox();
}
}
sub viewtablegeoipgrp
{
# If our filesize is "zero" there is nothing to read-in.
if (-z "$configgeoipgrp") {
return;
}
&Header::openbox('100%', 'left', $Lang::tr{'fwhost cust geoipgrp'});
&General::readhasharray("$configgeoipgrp", \%customgeoipgrp);
&General::readhasharray("$fwconfigfwd", \%fwfwd);
&General::readhasharray("$fwconfiginp", \%fwinp);
&General::readhasharray("$fwconfigout", \%fwout);
my @grp=();
my $helper='';
my $count=1;
my $country_code;
my $grpname;
my $remark;
my $number;
my $delflag;
my @counter;
my %hash;
# If there are no groups we are finished here.
if (!keys %customgeoipgrp) {
print "<center><b>$Lang::tr{'fwhost err emptytable'}</b>";
return;
}
# Put all groups in a hash.
foreach my $key (sort { ncmp($customgeoipgrp{$a}[0],$customgeoipgrp{$b}[0]) }
sort { ncmp($customgeoipgrp{$a}[2],$customgeoipgrp{$b}[2]) } keys %customgeoipgrp) {
push (@counter,$customgeoipgrp{$key}[0]);
}
# Increase current used key.
foreach my $key1 (@counter) {
$hash{$key1}++ ;
}
# Sort hash.
foreach my $key (sort { ncmp($customgeoipgrp{$a}[0],$customgeoipgrp{$b}[0]) }
sort { ncmp($customgeoipgrp{$a}[2],$customgeoipgrp{$b}[2]) } keys %customgeoipgrp) {
$count++;
if ($helper ne $customgeoipgrp{$key}[0]) {
$delflag='0';
foreach my $key1 (sort { ncmp($customgeoipgrp{$a}[0],$customgeoipgrp{$b}[0]) }
sort { ncmp($customgeoipgrp{$a}[2],$customgeoipgrp{$b}[2]) } keys %customgeoipgrp) {
if ($customgeoipgrp{$key}[0] eq $customgeoipgrp{$key1}[0])
{
$delflag++;
}
if($delflag > 1){
last;
}
}
$number=1;
# Groupname.
$grpname=$customgeoipgrp{$key}[0];
# Group remark.
$remark="$customgeoipgrp{$key}[1]";
# Country code.
$country_code="$customgeoipgrp{$key}[2]";
if ($count gt 1){
print"</table>";
$count=1;
}
# Display groups header.
print "<br><b><u>$grpname</u></b>&nbsp; &nbsp;\n";
print "<b>$Lang::tr{'remark'}:</b>&nbsp $remark &nbsp\n" if ($remark ne '');
# Get group count.
my $geoipgrpcount=&getgeoipcount($grpname);
print "<b>$Lang::tr{'used'}:</b> $geoipgrpcount x";
# Only display delete icon, if the group is not used by a firewall rule.
if($geoipgrpcount == '0') {
print"<form method='post' style='display:inline'>\n";
print"<input type='image' src='/images/delete.gif' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' align='right' />\n";
print"<input type='hidden' name='grp_name' value='$grpname' >\n";
print"<input type='hidden' name='ACTION' value='delgeoipgrp'>\n";
print"</form>";
}
# Icon for group editing.
print <<END;
<form method='post' style='display:inline'>
<input type='image' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' align='right'/>
<input type='hidden' name='grp_name' value='$grpname' >
<input type='hidden' name='remark' value='$remark' >
<input type='hidden' name='ACTION' value='editgeoipgrp'>
</form>
<table width='100%' cellspacing='0' class='tbl'>
END
# Display headlines if the group contains any entries.
if ($country_code ne "none") {
print <<END;
<tr>
<td width='10%' align='center'>
<b>$Lang::tr{'flag'}</b>
</td>
<td width='10%'align='center'>
<b>$Lang::tr{'countrycode'}</b>
</td>
<td width='70%'align='left'>
<b>$Lang::tr{'country'}</b>
</td>
<td width='10%' align='right'></td>
</tr>
END
}
}
# Check if our group contains any entries.
if ($country_code eq "none") {
print "<tr><td>$Lang::tr{'fwhost err emptytable'}</td></tr>\n";
} else {
# Check if we are currently editing a group and assign column backgound colors.
my $col='';
if ( ($fwhostsettings{'ACTION'} eq 'editgeoipgrp' || $fwhostsettings{'update'} ne '')
&& $fwhostsettings{'grp_name'} eq $customgeoipgrp{$key}[0]) {
$col="bgcolor='${Header::colouryellow}'";
} elsif ($count %2 == 0){
$col="bgcolor='$color{'color20'}'";
} else {
$col="bgcolor='$color{'color22'}'";
}
# Get country flag.
my $icon = &GeoIP::get_flag_icon($customgeoipgrp{$key}[2]);
# Print column with flag icon.
my $col_content;
if ($icon) {
$col_content = "<img src='$icon' alt='$customgeoipgrp{$key}[2]' title='$customgeoipgrp{$key}[2]'>";
} else {
$col_content = "<b>N/A</b>";
}
print "<td align='center' $col>$col_content</td>\n";
# Print column with country code.
print "<td align='center' $col>$customgeoipgrp{$key}[2]</td>\n";
# Print column with full country name.
my $country_name = &GeoIP::get_full_country_name($customgeoipgrp{$key}[2]);
print "<td align='left' $col>$country_name</td>\n";
# Generate from for removing entries from a group.
print "<td align='right' width='1%' $col><form method='post'>\n";
if ($delflag > 0){
print"<input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}'/>\n";
# Check if this group only has a single entry.
foreach my $key2 (keys %hash) {
if ($hash{$key2}<2 && $key2 eq $customgeoipgrp{$key}[0]){
print "<input type='hidden' name='last' value='on'>" ;
}
}
}
print "<input type='hidden' name='ACTION' value='deletegeoipgrpentry'>\n";
print "<input type='hidden' name='update' value='$fwhostsettings{'update'}'>\n";
print "<input type='hidden' name='delentry' value='$grpname,$remark,$customgeoipgrp{$key}[2],$customgeoipgrp{$key}[3]'>\n";
print "</form>\n";
print "</td>\n";
print "</tr>\n";
}
$helper=$customgeoipgrp{$key}[0];
$number++;
}
print"</table>\n";
&Header::closebox();
}
sub viewtableservice
{
@@ -2196,6 +2690,44 @@ sub gethostcount
}
return $srvcounter;
}
sub getgeoipcount
{
my $groupname=shift;
my $counter=0;
# GeoIP groups are stored as "group:groupname" in the
# firewall settings files.
my $searchstring = join(':', "group",$groupname);
# Count services used in firewall - forward
foreach my $key1 (keys %fwfwd) {
if($fwfwd{$key1}[4] eq $searchstring){
$counter++;
}
if($fwfwd{$key1}[6] eq $searchstring){
$counter++;
}
}
#Count services used in firewall - input
foreach my $key2 (keys %fwinp) {
if($fwinp{$key2}[4] eq $searchstring){
$counter++;
}
if($fwinp{$key2}[6] eq $searchstring){
$counter++;
}
}
#Count services used in firewall - outgoing
foreach my $key3 (keys %fwout) {
if($fwout{$key3}[4] eq $searchstring){
$counter++;
}
if($fwout{$key3}[6] eq $searchstring){
$counter++;
}
}
return $counter;
}
sub getnetcount
{
my $searchstring=shift;

View File

@@ -0,0 +1,263 @@
#!/usr/bin/perl
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2014 IPFire Developemnt Team <info@ipfire.org> #
# #
# 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 <http://www.gnu.org/licenses/>. #
# #
###############################################################################
use strict;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/geoip-functions.pl";
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
require "/usr/lib/firewall/firewall-lib.pl";
my $notice;
my $settingsfile = "${General::swroot}/firewall/geoipblock";
my %color = ();
my %mainsettings = ();
my %settings = ();
my %cgiparams = ();
# Read configuration file.
&General::readhash("$settingsfile", \%settings);
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
&Header::showhttpheaders();
#Get GUI values
&Header::getcgihash(\%cgiparams);
# Call subfunction to get all available locations.
my @locations = &fwlib::get_geoip_locations();
if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
# Check if we want to disable geoipblock.
if (exists $cgiparams{'GEOIPBLOCK_ENABLED'}) {
$settings{'GEOIPBLOCK_ENABLED'} = "on";
} else {
$settings{'GEOIPBLOCK_ENABLED'} = "off";
}
# Loop through our locations array to prevent from
# non existing countries or code.
foreach my $cn (@locations) {
# Check if blocking for this country should be enabled/disabled.
if (exists $cgiparams{$cn}) {
$settings{$cn} = "on";
} else {
$settings{$cn} = "off";
}
}
&General::writehash("$settingsfile", \%settings);
# Mark the firewall config as changed.
&General::firewall_config_changed();
# Assign reload notice. We directly can use
# the notice from p2p block.
$notice = $Lang::tr{'p2p block save notice'};
}
&Header::openpage($Lang::tr{'geoipblock configuration'}, 1, '');
# Print notice that a firewall reload is required.
if ($notice) {
&Header::openbox('100%', 'left', $Lang::tr{'notice'});
print "<font class='base'>$notice</font>";
&Header::closebox();
}
# Checkbox pre-selection.
my $checked;
if ($settings{'GEOIPBLOCK_ENABLED'} eq "on") {
$checked = "checked='checked'";
}
# Print box to enable/disable geoipblock.
print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
&Header::openbox('100%', 'center', $Lang::tr{'geoipblock'});
print <<END;
<table width='95%'>
<tr>
<td width='25%' class='base'>$Lang::tr{'geoipblock enable feature'}
<td><input type='checkbox' name='GEOIPBLOCK_ENABLED' $checked></td>
</tr>
<tr>
<td colspan='2'><br></td>
</tr>
</table>
<hr>
<table width='95%'>
<tr>
<td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
</tr>
</table>
END
&Header::closebox();
&Header::openbox('100%', 'center', $Lang::tr{'geoipblock block countries'});
### JAVA SCRIPT ###
print <<END;
<script>
// Function to allow checking all checkboxes at once.
function check_all() {
\$("#countries").find(":checkbox").prop("checked", true);
}
function uncheck_all() {
\$("#countries").find(":checkbox").prop("checked", false);
}
</script>
<table width='95%' class='tbl' id="countries">
<tr>
<td width='5%' align='center' bgcolor='$color{'color20'}'></td>
<td width='5%' align='center' bgcolor='$color{'color20'}'>
<b>$Lang::tr{'flag'}</b>
</td>
<td width='5%' align='center' bgcolor='$color{'color20'}'>
<b>$Lang::tr{'countrycode'}</b>
</td>
<td with='35%' align='left' bgcolor='$color{'color20'}'>
<b>$Lang::tr{'country'}</b>
</td>
<td width='5%' bgcolor='$color{'color20'}'>&nbsp;</td>
<td width='5%' align='center' bgcolor='$color{'color20'}'></td>
<td width='5%' align='center' bgcolor='$color{'color20'}'>
<b>$Lang::tr{'flag'}</b>
</td>
<td width='5%' align='center' bgcolor='$color{'color20'}'>
<b>$Lang::tr{'countrycode'}</b>
</td>
<td with='35%' align='left' bgcolor='$color{'color20'}'>
<b>$Lang::tr{'country'}</b>
</td>
</tr>
END
my $lines;
my $lines2;
my $col;
foreach my $location (@locations) {
# Country code in upper case. (DE)
my $ccode_uc = $location;
# County code in lower case. (de)
my $ccode_lc = lc($location);
# Full name of the country based on the country code.
my $cname = &GeoIP::get_full_country_name($ccode_lc);
# Get flag icon for of the country.
my $flag_icon = &GeoIP::get_flag_icon($ccode_uc);
my $flag;
# Check if a flag for the country is available.
if ($flag_icon) {
$flag="<img src='$flag_icon' alt='$ccode_uc' title='$ccode_uc'>";
} else {
$flag="<b>N/A</b>";
}
# Checkbox pre-selection.
my $checked;
if ($settings{$ccode_uc} eq "on") {
$checked = "checked='checked'";
}
# Colour lines.
if ($lines % 2) {
$col="bgcolor='$color{'color20'}'";
} else {
$col="bgcolor='$color{'color22'}'";
}
# Grouping elements.
my $line_start;
my $line_end;
if ($lines2 % 2) {
# Increase lines (background color by once.
$lines++;
# Add empty column in front.
$line_start="<td $col>&nbsp;</td>";
# When the line number can be diveded by "2",
# we are going to close the line.
$line_end="</tr>";
} else {
# When the line number is not divideable by "2",
# we are starting a new line.
$line_start="<tr>";
$line_end;
}
print "$line_start<td align='center' $col><input type='checkbox' name='$ccode_uc' $checked></td>\n";
print "<td align='center' $col>$flag</td>\n";
print "<td align='center' $col>$ccode_uc</td>\n";
print "<td align='left' $col>$cname</td>$line_end\n";
$lines2++;
}
print <<END;
</table>
<table width='95%'>
<tr>
<td align='right'>
<a href="javascript:check_all()">$Lang::tr{'check all'}</a> /
<a href="javascript:uncheck_all()">$Lang::tr{'uncheck all'}</a>
</td>
</tr>
<tr>
<td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
</tr>
</table>
<hr>
<table width='70%'>
<tr>
<td width='5%'><img src='/images/on.gif'></td>
<td>$Lang::tr{'geoipblock country is blocked'}</td>
<td width='5%'><img src='/images/off.gif'></td>
<td>$Lang::tr{'geoipblock country is allowed'}</td>
</tr>
</table>
END
&Header::closebox();
print"</form>\n";
&Header::closebigbox();
&Header::closepage();