]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - html/cgi-bin/firewall.cgi
Firewall: extend DNAT dropdown with auto,BLUE,ORANGE,GREEN
[people/teissler/ipfire-2.x.git] / html / cgi-bin / firewall.cgi
old mode 100755 (executable)
new mode 100644 (file)
index b9f7d36..582950b
 
 use strict;
 use Sort::Naturally;
+use utf8;
+use feature 'unicode_strings';
+
 no warnings 'uninitialized';
+
 # enable only the following on debugging purpose
 #use warnings;
 #use CGI::Carp 'fatalsToBrowser';
@@ -29,7 +33,7 @@ no warnings 'uninitialized';
 require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
-require "${General::swroot}/firewall/bin/firewall-lib.pl";
+require "/usr/lib/firewall/firewall-lib.pl";
 
 unless (-d "${General::swroot}/firewall")                      { system("mkdir ${General::swroot}/firewall"); }
 unless (-e "${General::swroot}/firewall/settings")     { system("touch ${General::swroot}/firewall/settings"); }
@@ -136,14 +140,17 @@ print<<END;
                \$("#protocol").change(update_protocol);
                update_protocol();
 
-               // When nat not used, hide it
-               if (! \$("#USE_NAT").attr("checked")) {
+               // Show/Hide elements when NAT checkbox is checked.
+               if (\$("#USE_NAT").attr("checked")) {
+                       \$("#actions").hide();
+               } else {
                        \$(".NAT").hide();
                }
 
                // Show NAT area when "use nat" checkbox is clicked
                \$("#USE_NAT").change(function() {
                        \$(".NAT").toggle();
+                       \$("#actions").toggle();
                });
 
                // Time constraints
@@ -158,11 +165,6 @@ print<<END;
                // dropdown menu changes.
                \$("select").change(function() {
                        var id = \$(this).attr("name");
-
-                       // When using SNAT or DNAT, check "USE NAT" Checkbox
-                       if (id === 'snat' || id === 'dnat') {
-                               \$('#USE_NAT').prop('checked', true);
-                       }
                        \$('#' + id).prop("checked", true);
                });
        });
@@ -196,6 +198,7 @@ if ($fwdfwsettings{'ACTION'} eq 'saverule')
        $errormessage=&checksource;
        if(!$errormessage){&checktarget;}
        if(!$errormessage){&checkrule;}
+
        #check if manual ip (source) is orange network
        if ($fwdfwsettings{'grp1'} eq 'src_addr'){
                my ($sip,$scidr) = split("/",$fwdfwsettings{$fwdfwsettings{'grp1'}});
@@ -222,16 +225,17 @@ if ($fwdfwsettings{'ACTION'} eq 'saverule')
                                if (   "$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'LOG'},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'},$fwdfwsettings{'USE_NAT'},$fwdfwsettings{$fwdfwsettings{'nat'}},$fwdfwsettings{'dnatport'},$fwdfwsettings{'nat'}"
                                        eq "$configinputfw{$key}[0],$configinputfw{$key}[2],$configinputfw{$key}[3],$configinputfw{$key}[4],$configinputfw{$key}[5],$configinputfw{$key}[6],$configinputfw{$key}[7],$configinputfw{$key}[8],$configinputfw{$key}[9],$configinputfw{$key}[10],$configinputfw{$key}[11],$configinputfw{$key}[12],$configinputfw{$key}[13],$configinputfw{$key}[14],$configinputfw{$key}[15],$configinputfw{$key}[17],$configinputfw{$key}[18],$configinputfw{$key}[19],$configinputfw{$key}[20],$configinputfw{$key}[21],$configinputfw{$key}[22],$configinputfw{$key}[23],$configinputfw{$key}[24],$configinputfw{$key}[25],$configinputfw{$key}[26],$configinputfw{$key}[27],$configinputfw{$key}[28],$configinputfw{$key}[29],$configinputfw{$key}[30],$configinputfw{$key}[31]"){
                                                $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
-                                               if ($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on'){
-                                                       $errormessage='';
-                                               }elsif($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
+                                               if($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
                                                        $errormessage=$Lang::tr{'fwdfw err remark'}."<br>";
                                                }
+                                               if($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && &validremark($fwdfwsettings{'ruleremark'})){
+                                                       $errormessage='';
+                                               }
                                                if ($fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'}){
                                                        $fwdfwsettings{'nosave'} = 'on';
                                                }
-                               }       
-                       }       
+                               }
+                       }
                }
                #check Rulepos on new Rule
                if($fwdfwsettings{'rulepos'} > 0 && !$fwdfwsettings{'oldrulenumber'}){
@@ -265,11 +269,12 @@ if ($fwdfwsettings{'ACTION'} eq 'saverule')
                                if (   "$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'LOG'},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'},$fwdfwsettings{'USE_NAT'},$fwdfwsettings{$fwdfwsettings{'nat'}},$fwdfwsettings{'dnatport'},$fwdfwsettings{'nat'}"
                                        eq "$configoutgoingfw{$key}[0],$configoutgoingfw{$key}[2],$configoutgoingfw{$key}[3],$configoutgoingfw{$key}[4],$configoutgoingfw{$key}[5],$configoutgoingfw{$key}[6],$configoutgoingfw{$key}[7],$configoutgoingfw{$key}[8],$configoutgoingfw{$key}[9],$configoutgoingfw{$key}[10],$configoutgoingfw{$key}[11],$configoutgoingfw{$key}[12],$configoutgoingfw{$key}[13],$configoutgoingfw{$key}[14],$configoutgoingfw{$key}[15],$configoutgoingfw{$key}[17],$configoutgoingfw{$key}[18],$configoutgoingfw{$key}[19],$configoutgoingfw{$key}[20],$configoutgoingfw{$key}[21],$configoutgoingfw{$key}[22],$configoutgoingfw{$key}[23],$configoutgoingfw{$key}[24],$configoutgoingfw{$key}[25],$configoutgoingfw{$key}[26],$configoutgoingfw{$key}[27],$configoutgoingfw{$key}[28],$configoutgoingfw{$key}[29],$configoutgoingfw{$key}[30],$configoutgoingfw{$key}[31]"){
                                                $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
-                                               if ($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on'){
-                                                       $errormessage='';
-                                               }elsif($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
+                                               if($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
                                                        $errormessage=$Lang::tr{'fwdfw err remark'}."<br>";
                                                }
+                                               if($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && &validremark($fwdfwsettings{'ruleremark'})){
+                                                       $errormessage='';
+                                               }
                                                if ($fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'}){
                                                        $fwdfwsettings{'nosave'} = 'on';
                                                }
@@ -278,7 +283,6 @@ if ($fwdfwsettings{'ACTION'} eq 'saverule')
                }
                #check Rulepos on new Rule
                if($fwdfwsettings{'rulepos'} > 0 && !$fwdfwsettings{'oldrulenumber'}){
-                       print"CHECK OUTGOING DOPPELTE REGEL<br>";
                        $fwdfwsettings{'oldrulenumber'}=$maxkey;
                        foreach my $key (sort keys %configoutgoingfw){
                                if (   "$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'LOG'},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'},$fwdfwsettings{'USE_NAT'},$fwdfwsettings{$fwdfwsettings{'nat'}},$fwdfwsettings{'dnatport'},$fwdfwsettings{'nat'}"
@@ -311,17 +315,18 @@ if ($fwdfwsettings{'ACTION'} eq 'saverule')
                                if (   "$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'},$fwdfwsettings{'USE_NAT'},$fwdfwsettings{$fwdfwsettings{'nat'}},$fwdfwsettings{'dnatport'},$fwdfwsettings{'nat'}"
                                        eq "$configfwdfw{$key}[0],$configfwdfw{$key}[2],$configfwdfw{$key}[3],$configfwdfw{$key}[4],$configfwdfw{$key}[5],$configfwdfw{$key}[6],$configfwdfw{$key}[7],$configfwdfw{$key}[8],$configfwdfw{$key}[9],$configfwdfw{$key}[10],$configfwdfw{$key}[11],$configfwdfw{$key}[12],$configfwdfw{$key}[13],$configfwdfw{$key}[14],$configfwdfw{$key}[15],$configfwdfw{$key}[18],$configfwdfw{$key}[19],$configfwdfw{$key}[20],$configfwdfw{$key}[21],$configfwdfw{$key}[22],$configfwdfw{$key}[23],$configfwdfw{$key}[24],$configfwdfw{$key}[25],$configfwdfw{$key}[26],$configfwdfw{$key}[27],$configfwdfw{$key}[28],$configfwdfw{$key}[29],$configfwdfw{$key}[30],$configfwdfw{$key}[31]"){
                                                $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
-                                               if ($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' ){
-                                                       $errormessage='';
-                                               }elsif($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
+                                               if($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && !&validremark($fwdfwsettings{'ruleremark'})){
                                                        $errormessage=$Lang::tr{'fwdfw err remark'}."<br>";
                                                }
+                                               if($fwdfwsettings{'oldruleremark'} ne $fwdfwsettings{'ruleremark'} && $fwdfwsettings{'updatefwrule'} eq 'on' && $fwdfwsettings{'ruleremark'} ne '' && &validremark($fwdfwsettings{'ruleremark'})){
+                                                       $errormessage='';
+                                               }
                                                if ($fwdfwsettings{'oldruleremark'} eq $fwdfwsettings{'ruleremark'}){
                                                        $fwdfwsettings{'nosave'} = 'on';
                                                }
-                               }               
+                               }
                        }
-               }       
+               }
                #check Rulepos on new Rule
                if($fwdfwsettings{'rulepos'} > 0 && !$fwdfwsettings{'oldrulenumber'}){
                        $fwdfwsettings{'oldrulenumber'}=$maxkey;
@@ -329,7 +334,7 @@ if ($fwdfwsettings{'ACTION'} eq 'saverule')
                                if (   "$fwdfwsettings{'RULE_ACTION'},$fwdfwsettings{'ACTIVE'},$fwdfwsettings{'grp1'},$fwdfwsettings{$fwdfwsettings{'grp1'}},$fwdfwsettings{'grp2'},$fwdfwsettings{$fwdfwsettings{'grp2'}},$fwdfwsettings{'USE_SRC_PORT'},$fwdfwsettings{'PROT'},$fwdfwsettings{'ICMP_TYPES'},$fwdfwsettings{'SRC_PORT'},$fwdfwsettings{'USESRV'},$fwdfwsettings{'TGT_PROT'},$fwdfwsettings{'ICMP_TGT'},$fwdfwsettings{'grp3'},$fwdfwsettings{$fwdfwsettings{'grp3'}},$fwdfwsettings{'TIME'},$fwdfwsettings{'TIME_MON'},$fwdfwsettings{'TIME_TUE'},$fwdfwsettings{'TIME_WED'},$fwdfwsettings{'TIME_THU'},$fwdfwsettings{'TIME_FRI'},$fwdfwsettings{'TIME_SAT'},$fwdfwsettings{'TIME_SUN'},$fwdfwsettings{'TIME_FROM'},$fwdfwsettings{'TIME_TO'},$fwdfwsettings{'USE_NAT'},$fwdfwsettings{$fwdfwsettings{'nat'}},$fwdfwsettings{'dnatport'},$fwdfwsettings{'nat'}"
                                        eq "$configfwdfw{$key}[0],$configfwdfw{$key}[2],$configfwdfw{$key}[3],$configfwdfw{$key}[4],$configfwdfw{$key}[5],$configfwdfw{$key}[6],$configfwdfw{$key}[7],$configfwdfw{$key}[8],$configfwdfw{$key}[9],$configfwdfw{$key}[10],$configfwdfw{$key}[11],$configfwdfw{$key}[12],$configfwdfw{$key}[13],$configfwdfw{$key}[14],$configfwdfw{$key}[15],$configfwdfw{$key}[18],$configfwdfw{$key}[19],$configfwdfw{$key}[20],$configfwdfw{$key}[21],$configfwdfw{$key}[22],$configfwdfw{$key}[23],$configfwdfw{$key}[24],$configfwdfw{$key}[25],$configfwdfw{$key}[26],$configfwdfw{$key}[27],$configfwdfw{$key}[28],$configfwdfw{$key}[29],$configfwdfw{$key}[30],$configfwdfw{$key}[31]"){
                                                $errormessage.=$Lang::tr{'fwdfw err ruleexists'};
-                               }               
+                               }
                        }
                }
                #check if we just close a rule
@@ -507,8 +512,8 @@ sub checksource
                        return $errormessage;
                }
        }elsif($fwdfwsettings{'src_addr'} eq $fwdfwsettings{$fwdfwsettings{'grp1'}} && $fwdfwsettings{'src_addr'} eq ''){
-               $errormessage.=$Lang::tr{'fwdfw err nosrcip'};
-               return $errormessage;
+               $fwdfwsettings{'grp1'}='std_net_src';
+               $fwdfwsettings{$fwdfwsettings{'grp1'}} = 'ALL';
        }
 
        #check empty fields
@@ -608,8 +613,21 @@ sub checktarget
                        return $errormessage;
                }
        }elsif($fwdfwsettings{'tgt_addr'} eq $fwdfwsettings{$fwdfwsettings{'grp2'}} && $fwdfwsettings{'tgt_addr'} eq ''){
-               $errormessage.=$Lang::tr{'fwdfw err notgtip'};
-               return $errormessage;
+               $fwdfwsettings{'grp2'}='std_net_tgt';
+               $fwdfwsettings{$fwdfwsettings{'grp2'}} = 'ALL';
+       }
+       #check for mac in targetgroup
+       if ($fwdfwsettings{'grp2'} eq 'cust_grp_tgt'){
+               &General::readhasharray("$configgrp", \%customgrp);
+               &General::readhasharray("$confighost", \%customhost);
+               foreach my $grpkey (sort keys %customgrp){
+                       foreach my $hostkey (sort keys %customhost){
+                               if ($customgrp{$grpkey}[2] eq $customhost{$hostkey}[0] && $customhost{$hostkey}[1] eq 'mac'){
+                                       $hint=$Lang::tr{'fwdfw hint mac'};
+                                       return $hint;
+                               }
+                       }
+               }
        }
        #check empty fields
        if ($fwdfwsettings{$fwdfwsettings{'grp2'}} eq ''){ $errormessage.=$Lang::tr{'fwdfw err notgt'}."<br>";}
@@ -664,6 +682,8 @@ sub checktarget
                                                        if (&General::validport($_)){
                                                                push (@values,$_);
                                                        }else{
+                                                               $errormessage=$Lang::tr{'fwdfw err tgt_port'};
+                                                               return $errormessage;
                                                        }
                                                }
                                        }
@@ -729,17 +749,32 @@ sub checkrule
 {
        #check valid port for NAT
        if($fwdfwsettings{'USE_NAT'} eq 'ON'){
+               #RULE_ACTION must be ACCEPT if we use NAT
+               $fwdfwsettings{'RULE_ACTION'} = 'ACCEPT';
+
+               #if no dnat or snat selected errormessage
+               if ($fwdfwsettings{'nat'} eq ''){
+                       $errormessage=$Lang::tr{'fwdfw dnat nochoice'};
+                       return;
+               }
+
+               #if using snat, the external port has to be empty
+               if ($fwdfwsettings{'nat'} eq 'snat' && $fwdfwsettings{'dnatport'} ne ''){
+                       $errormessage=$Lang::tr{'fwdfw dnat extport'};
+                       return;
+               }
                #if no dest port is given in nat area, take target host port
                if($fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'grp3'} eq 'TGT_PORT' && $fwdfwsettings{'dnatport'} eq ''){$fwdfwsettings{'dnatport'}=$fwdfwsettings{'TGT_PORT'};}
                if($fwdfwsettings{'TGT_PORT'} eq '' && $fwdfwsettings{'dnatport'} ne '' && ($fwdfwsettings{'PROT'} eq 'TCP' || $fwdfwsettings{'PROT'} eq 'UDP')){
                        $errormessage=$Lang::tr{'fwdfw dnat porterr2'};
+                       return;
                }
                #check if port given in nat area is a single valid port or portrange
                if($fwdfwsettings{'nat'} eq 'dnat' && $fwdfwsettings{'TGT_PORT'} ne '' && !&check_natport($fwdfwsettings{'dnatport'})){
                        $errormessage=$Lang::tr{'fwdfw target'}.": ".$Lang::tr{'fwdfw dnat porterr'}."<br>";
                }elsif($fwdfwsettings{'USESRV'} eq 'ON' && $fwdfwsettings{'grp3'} eq 'cust_srv'){
                        my $custsrvport;
-                       #get servcie Protocol and Port
+                       #get service Protocol and Port
                        foreach my $key (sort keys %customservice){
                                if($fwdfwsettings{$fwdfwsettings{'grp3'}} eq $customservice{$key}[0]){
                                        if ($customservice{$key}[2] ne 'TCP' && $customservice{$key}[2] ne 'UDP'){
@@ -973,7 +1008,6 @@ sub error
                print "<class name='base'>$errormessage\n";
                print "&nbsp;</class>\n";
                &Header::closebox();
-               print"<hr>";
        }
 }
 sub fillselect
@@ -1017,7 +1051,7 @@ sub gen_dd_block
 print<<END;
                <table width='100%' border='0'>
                <tr><td width='50%' valign='top'>
-               <table width='100%' border='0'>
+               <table width='95%' border='0'>
                <tr><td width='1%'><input type='radio' name='$grp' id='std_net_$srctgt' value='std_net_$srctgt' $checked{$grp}{'std_net_'.$srctgt}></td><td>$Lang::tr{'fwhost stdnet'}</td><td align='right'><select name='std_net_$srctgt' style='width:200px;'>
 END
        foreach my $network (sort keys %defaultNetworks)
@@ -1053,7 +1087,7 @@ END
        if (! -z $configgrp || $optionsfw{'SHOWDROPDOWN'} eq 'on'){
                print"<tr><td valign='top'><input type='radio' name='$grp' id='cust_grp_$srctgt' value='cust_grp_$srctgt' $checked{$grp}{'cust_grp_'.$srctgt}></td><td >$Lang::tr{'fwhost cust grp'}</td><td align='right'><select name='cust_grp_$srctgt' style='width:200px;'>";
                foreach my $key (sort { ncmp($customgrp{$a}[0],$customgrp{$b}[0]) } keys %customgrp) {
-                       if($helper ne $customgrp{$key}[0]){
+                       if($helper ne $customgrp{$key}[0] && $customgrp{$key}[2] ne 'none'){
                                print"<option ";
                                print "selected='selected' " if ($fwdfwsettings{$fwdfwsettings{$grp}} eq $customgrp{$key}[0]);
                                print ">$customgrp{$key}[0]</option>";
@@ -1063,7 +1097,7 @@ END
                print"</select></td>";
        }
        #End left table. start right table (vpn)
-       print"</tr></table></td><td valign='top'><table width='100%' border='0'><tr>";
+       print"</tr></table></td><td valign='top'><table width='95%' border='0' align='right'><tr>";
        # CCD networks
        if( ! -z $configccdnet || $optionsfw{'SHOWDROPDOWN'} eq 'on'){
                print"<td width='1%'><input type='radio' name='$grp' id='ovpn_net_$srctgt' value='ovpn_net_$srctgt'  $checked{$grp}{'ovpn_net_'.$srctgt}></td><td nowrap='nowrap' width='16%'>$Lang::tr{'fwhost ccdnet'}</td><td nowrap='nowrap' width='1%' align='right'><select name='ovpn_net_$srctgt' style='width:200px;'>";
@@ -1266,6 +1300,12 @@ sub getcolor
        my $val=shift;
        my $hash=shift;
        if($optionsfw{'SHOWCOLORS'} eq 'on'){
+               # Don't colourise MAC addresses
+               if (&General::validmac($val)) {
+                       $tdcolor = "";
+                       return;
+               }
+
                #custom Hosts
                if ($nettype eq 'cust_host_src' || $nettype eq 'cust_host_tgt'){
                        foreach my $key (sort keys %$hash){
@@ -1354,6 +1394,7 @@ sub getcolor
                        #Check if IP is part of a IPsec N2N network
                        foreach my $key (sort keys %ipsecconf){
                                my ($a,$b) = split("/",$ipsecconf{$key}[11]);
+                               $b=&General::iporsubtodec($b);
                                if (&General::IpInSubnet($c,$a,$b)){
                                        $tdcolor="style='background-color: $Header::colourvpn;color:white;'";
                                        return;
@@ -1388,24 +1429,7 @@ sub hint
                print "<class name='base'>$hint\n";
                print "&nbsp;</class>\n";
                &Header::closebox();
-               print"<hr>";
-       }
-}
-sub inc_counter
-{
-       my $config=shift;
-       my %hash=%{(shift)};
-       my $val=shift;
-       my $pos;
-
-       &General::readhasharray($config, \%hash);
-       foreach my $key (sort { uc($hash{$a}[0]) cmp uc($hash{$b}[0]) }  keys %hash){
-               if($hash{$key}[0] eq $val){
-                       $pos=$#{$hash{$key}};
-                       $hash{$key}[$pos] = $hash{$key}[$pos]+1;
-               }
        }
-       &General::writehasharray($config, \%hash);
 }
 sub newrule
 {
@@ -1431,6 +1455,11 @@ sub newrule
        open (CONN1,"/var/ipfire/red/local-ipaddress");
        my $redip = <CONN1>;
        close(CONN1);
+       if (! $fwdfwsettings{'RULE_ACTION'} && $fwdfwsettings{'POLICY'} eq 'MODE2'){
+               $fwdfwsettings{'RULE_ACTION'}='DROP';
+       }elsif(! $fwdfwsettings{'RULE_ACTION'} && $fwdfwsettings{'POLICY'} eq 'MODE1'){
+               $fwdfwsettings{'RULE_ACTION'}='ACCEPT';
+       }
        $checked{'grp1'}{$fwdfwsettings{'grp1'}}                                = 'CHECKED';
        $checked{'grp2'}{$fwdfwsettings{'grp2'}}                                = 'CHECKED';
        $checked{'grp3'}{$fwdfwsettings{'grp3'}}                                = 'CHECKED';
@@ -1447,7 +1476,6 @@ sub newrule
        $checked{'TIME_SAT'}{$fwdfwsettings{'TIME_SAT'}}                = 'CHECKED';
        $checked{'TIME_SUN'}{$fwdfwsettings{'TIME_SUN'}}                = 'CHECKED';
        $checked{'USE_NAT'}{$fwdfwsettings{'USE_NAT'}}                  = 'CHECKED';
-       $checked{'nat'}{$fwdfwsettings{'nat'}}          = 'CHECKED';
        $selected{'TIME_FROM'}{$fwdfwsettings{'TIME_FROM'}}             = 'selected';
        $selected{'TIME_TO'}{$fwdfwsettings{'TIME_TO'}}                 = 'selected';
        $selected{'ipfire'}{$fwdfwsettings{$fwdfwsettings{'grp2'}}} ='selected';
@@ -1507,13 +1535,11 @@ sub newrule
                                $checked{'TIME_SAT'}{$fwdfwsettings{'TIME_SAT'}}                = 'CHECKED';
                                $checked{'TIME_SUN'}{$fwdfwsettings{'TIME_SUN'}}                = 'CHECKED';
                                $checked{'USE_NAT'}{$fwdfwsettings{'USE_NAT'}}                  = 'CHECKED';
-                               $checked{'nat'}{$fwdfwsettings{'nat'}}                                  = 'CHECKED';
+                               $checked{'nat'}{$fwdfwsettings{'nat'}}                                  = 'CHECKED';
                                $selected{'TIME_FROM'}{$fwdfwsettings{'TIME_FROM'}}             = 'selected';
                                $selected{'TIME_TO'}{$fwdfwsettings{'TIME_TO'}}                 = 'selected';
                                $selected{'ipfire'}{$fwdfwsettings{$fwdfwsettings{'grp2'}}} ='selected';
                                $selected{'ipfire_src'}{$fwdfwsettings{$fwdfwsettings{'grp1'}}} ='selected';
-                               $selected{'dnat'}{$fwdfwsettings{$fwdfwsettings{'nat'}}} ='selected';
-                               $selected{'snat'}{$fwdfwsettings{$fwdfwsettings{'nat'}}} ='selected';
                        }
                }
                $fwdfwsettings{'oldgrp1a'}=$fwdfwsettings{'grp1'};
@@ -1535,7 +1561,9 @@ sub newrule
                }
        }else{
                $fwdfwsettings{'ACTIVE'}='ON';
+               $fwdfwsettings{'nat'} = 'dnat';
                $checked{'ACTIVE'}{$fwdfwsettings{'ACTIVE'}} = 'CHECKED';
+               $checked{'nat'}{$fwdfwsettings{'nat'}} = 'CHECKED';
                $fwdfwsettings{'oldgrp1a'}=$fwdfwsettings{'grp1'};
                $fwdfwsettings{'oldgrp1b'}=$fwdfwsettings{$fwdfwsettings{'grp1'}};
                $fwdfwsettings{'oldgrp2a'}=$fwdfwsettings{'grp2'};
@@ -1553,8 +1581,11 @@ sub newrule
                        }
                }       
        }
-       &Header::openbox('100%', 'left', $Lang::tr{'fwdfw addrule'});
-       &Header::closebox();
+       # Split manual source and target address and delete the subnet
+       my ($sip,$scidr) = split("/",$fwdfwsettings{$fwdfwsettings{'grp1'}});
+       if ($scidr eq '32'){$fwdfwsettings{$fwdfwsettings{'grp1'}}=$sip;}
+       my ($dip,$dcidr) = split("/",$fwdfwsettings{$fwdfwsettings{'grp2'}});
+       if ($dcidr eq '32'){$fwdfwsettings{$fwdfwsettings{'grp2'}}=$dip;}
        &Header::openbox('100%', 'left', $Lang::tr{'fwdfw source'});
        #------SOURCE-------------------------------------------------------
        print "<form method='post'>";
@@ -1576,10 +1607,10 @@ END
                }
                print<<END;
                </select></td></tr>
-               <tr><td colspan='8'><hr style='border:dotted #BFBFBF; border-width:1px 0 0 0 ; ' /></td></tr></table>
+               <tr><td><br></td></tr>
+               </table>
 END
                &gen_dd_block('src','grp1');
-               print"<hr>";
                &Header::closebox();
 
                #---SNAT / DNAT ------------------------------------------------
@@ -1595,31 +1626,34 @@ END
                                                <td width='5%'></td>
                                                <td width='40%'>
                                                        <label>
-                                                               <input type='radio' name='nat' id='use_dnat' value='dnat' $checked{'nat'}{'dnat'}>
+                                                               <input type='radio' name='nat'  value='dnat' $checked{'nat'}{'dnat'}>
                                                                $Lang::tr{'fwdfw dnat'}
                                                        </label>
                                                </td>
 END
 
-               if (%aliases) {
-                       print <<END;
+       print <<END;
                                                <td width='25%' align='right'>$Lang::tr{'dnat address'}:</td>
                                                <td width='30%'>
                                                        <select name='dnat' style='width: 100%;'>
-                                                               <option value='Default IP' $selected{'dnat'}{'Default IP'}>$Lang::tr{'default ip'} ($netsettings{'RED_ADDRESS'})</option>
+                                                               <option value='AUTO' $selected{'dnat'}{'AUTO'}>- $Lang::tr{'automatic'} -</option>
+                                                               <option value='Default IP' $selected{'dnat'}{'Default IP'}>$Lang::tr{'red1'} ($netsettings{'RED_ADDRESS'})</option>
 END
+               if (%aliases) {
                        foreach my $alias (sort keys %aliases) {
                                print "<option value='$alias' $selected{'dnat'}{$alias}>$alias ($aliases{$alias}{'IPT'})</option>";
                        }
-
-                       print "</select>";
-               } else {
-                       print <<END;
-                                               <td colspan="2" width='55%'>
-                                                       <input type='hidden' name='dnat' value='Default IP'>
-                                               </td>
-END
                }
+               #DNAT Dropdown
+               foreach my $network (sort keys %defaultNetworks)
+               {
+                       if ($defaultNetworks{$network}{'NAME'} eq 'BLUE'||$defaultNetworks{$network}{'NAME'} eq 'GREEN' ||$defaultNetworks{$network}{'NAME'} eq 'ORANGE'){
+                               print "<option value='$defaultNetworks{$network}{'NAME'}'";
+                               print " selected='selected'" if ($fwdfwsettings{$fwdfwsettings{'nat'}} eq $defaultNetworks{$network}{'NAME'});
+                               print ">$network ($defaultNetworks{$network}{'NET'})</option>";
+                       }
+               }
+               print "</select>";
                print "</tr>";
 
                #SNAT
@@ -1628,7 +1662,7 @@ END
                                                <td width='5%'></td>
                                                <td width='40%'>
                                                        <label>
-                                                               <input type='radio' name='nat' id='use_snat' value='snat' $checked{'nat'}{'snat'}>
+                                                               <input type='radio' name='nat'  value='snat' $checked{'nat'}{'snat'}>
                                                                $Lang::tr{'fwdfw snat'}
                                                        </label>
                                                </td>
@@ -1640,19 +1674,14 @@ END
                foreach my $alias (sort keys %aliases) {
                        print "<option value='$alias' $selected{'snat'}{$alias}>$alias ($aliases{$alias}{'IPT'})</option>";
                }
-
-               # XXX this is composed in a very ugly fashion
+               # SNAT Dropdown
                foreach my $network (sort keys %defaultNetworks) {
-                       next if($defaultNetworks{$network}{'NAME'} eq "IPFire");
-                       next if($defaultNetworks{$network}{'NAME'} eq "ALL");
-                       next if($defaultNetworks{$network}{'NAME'} =~ /OpenVPN/i);
-                       next if($defaultNetworks{$network}{'NAME'} =~ /IPsec/i);
-
-                       print "<option value='$defaultNetworks{$network}{'NAME'}'";
-                       print " selected='selected'" if ($fwdfwsettings{$fwdfwsettings{'nat'}} eq $defaultNetworks{$network}{'NAME'});
-                       print ">$network ($defaultNetworks{$network}{'NET'})</option>";
+                       if ($defaultNetworks{$network}{'NAME'} eq 'BLUE'||$defaultNetworks{$network}{'NAME'} eq 'GREEN' ||$defaultNetworks{$network}{'NAME'} eq 'ORANGE'){
+                               print "<option value='$defaultNetworks{$network}{'NAME'}'";
+                               print " selected='selected'" if ($fwdfwsettings{$fwdfwsettings{'nat'}} eq $defaultNetworks{$network}{'NAME'});
+                               print ">$network ($defaultNetworks{$network}{'NET'})</option>";
+                       }
                }
-
                print <<END;
                                                        </select>
                                                </td>
@@ -1682,10 +1711,9 @@ END
                }
                print<<END;
                </select></td></tr>
-               <tr><td colspan='7'><hr style='border:dotted #BFBFBF; border-width:1px 0 0 0 ; ' /></td></tr></table>
+               <tr><td><br></td></tr></table>
 END
                &gen_dd_block('tgt','grp2');
-               print"<hr>";
                &Header::closebox;
                #---PROTOCOL------------------------------------------------------
                $fwdfwsettings{'SRC_PORT'} =~ s/\|/,/g;
@@ -1819,7 +1847,7 @@ END
                &General::readhasharray("$configsrvgrp", \%customservicegrp);
                my $helper;
                foreach my $key (sort { ncmp($customservicegrp{$a}[0],$customservicegrp{$b}[0]) } keys %customservicegrp){
-                       if ($helper ne $customservicegrp{$key}[0]){
+                       if ($helper ne $customservicegrp{$key}[0] && $customservicegrp{$key}[2] ne 'none'){
                                print"<option ";
                                print"selected='selected'" if ($fwdfwsettings{$fwdfwsettings{'grp3'}} eq $customservicegrp{$key}[0]);
                                print">$customservicegrp{$key}[0]</option>";
@@ -1838,25 +1866,10 @@ END
 END
 
                &Header::closebox;
-
-               $checked{"RULE_ACTION"} = ();
-               foreach ("ACCEPT", "DROP", "REJECT") {
-                       $checked{"RULE_ACTION"}{$_} = "";
-               }
-
-               if($fwdfwsettings{'updatefwrule'} eq 'on') {
-                       $checked{"RULE_ACTION"}{$fwdfwsettings{'RULE_ACTION'}} = "checked";
-               } elsif ($fwdfwsettings{'POLICY'} eq 'MODE1') {
-                       $checked{"RULE_ACTION"}{"ACCEPT"} = "checked";
-               } elsif ($fwdfwsettings{'POLICY'} eq 'MODE2') {
-                       $checked{"RULE_ACTION"}{"DROP"} = "checked";
-               }
-
+               $checked{"RULE_ACTION"}{$fwdfwsettings{'RULE_ACTION'}}  = 'CHECKED';
                print <<END;
-                       <hr><br>
-
                        <center>
-                               <table width="80%" border="0">
+                               <table width="80%" class='tbl' id='actions'>
                                        <tr>
                                                <td width="33%" align="center" bgcolor="$color{'color17'}">
                                                        &nbsp;<br>&nbsp;
@@ -1893,7 +1906,6 @@ END
 
                        <br>
 END
-
                #---Activate/logging/remark-------------------------------------
                &Header::openbox('100%', 'left', $Lang::tr{'fwdfw additional'});
                print<<END;
@@ -2001,7 +2013,7 @@ END
                                </td>
                        </tr>
                </table>
-               <br><hr>
+               <br>
 END
 
                #---ACTION------------------------------------------------------
@@ -2131,6 +2143,9 @@ sub saverule
                        &changerule($configfwdfw);
                        #print"6";
                }
+               $fwdfwsettings{'ruleremark'}=~ s/,/;/g;
+               utf8::decode($fwdfwsettings{'ruleremark'});
+               $fwdfwsettings{'ruleremark'}=&Header::escape($fwdfwsettings{'ruleremark'});
                if ($fwdfwsettings{'updatefwrule'} ne 'on'){
                        my $key = &General::findhasharraykey ($hash);
                        $$hash{$key}[0]  = $fwdfwsettings{'RULE_ACTION'};
@@ -2266,22 +2281,19 @@ sub saverule
 sub validremark
 {
        # Checks a hostname against RFC1035
-        my $remark = $_[0];
-
-       # Each part should be at least two characters in length
-       # but no more than 63 characters
-       if (length ($remark) < 1 || length ($remark) > 255) {
-               return 0;}
-       # Only valid characters are a-z, A-Z, 0-9 and -
-       if ($remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/\s]*$/) {
-               return 0;}
-       # First character can only be a letter or a digit
-       if (substr ($remark, 0, 1) !~ /^[a-zäöüA-ZÖÄÜ0-9(]*$/) {
-               return 0;}
-       # Last character can only be a letter or a digit
-       if (substr ($remark, -1, 1) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/) {
-               return 0;}
-       return 1;
+       my $remark = $_[0];
+
+       # Try to decode $remark into UTF-8. If this doesn't work,
+       # we assume that the string it not sane.
+       if (!utf8::decode($remark)) {
+               return 0;
+       }
+
+       # Check if the string only contains of printable characters.
+       if ($remark =~ /^[[:print:]]*$/) {
+               return 1;
+       }
+       return 0;
 }
 sub viewtablerule
 {
@@ -2306,9 +2318,11 @@ sub viewtablenew
        &General::readhasharray("$config", $hash);
        &General::readhasharray("$configccdnet", \%ccdnet);
        &General::readhasharray("$configccdhost", \%ccdhost);
+       &General::readhasharray("$configgrp", \%customgrp);
+       &General::readhasharray("$configsrvgrp", \%customservicegrp);
 
        &Header::openbox('100%', 'left', $title);
-       print "<table width='100%' cellspacing='0' border='0'>";
+       print "<table width='100%' cellspacing='0' class='tbl'>";
 
        if (! -z $config) {
                my $count=0;
@@ -2359,26 +2373,18 @@ END
                                if($$hash{$key}[3] eq  'ipsec_net_src'){
                                        if(&fwlib::get_ipsec_net_ip($host,11) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }elsif($$hash{$key}[3] eq  'ovpn_net_src'){
                                        if(&fwlib::get_ovpn_net_ip($host,1) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }elsif($$hash{$key}[3] eq  'ovpn_n2n_src'){
                                        if(&fwlib::get_ovpn_n2n_ip($host,27) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }elsif($$hash{$key}[3] eq  'ovpn_host_src'){
                                        if(&fwlib::get_ovpn_host_ip($host,33) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }
                        }
@@ -2386,29 +2392,32 @@ END
                                if($$hash{$key}[5] eq 'ipsec_net_tgt'){
                                        if(&fwlib::get_ipsec_net_ip($host,11) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }elsif($$hash{$key}[5] eq 'ovpn_net_tgt'){
                                        if(&fwlib::get_ovpn_net_ip($host,1) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }elsif($$hash{$key}[5] eq 'ovpn_n2n_tgt'){
                                        if(&fwlib::get_ovpn_n2n_ip($host,27) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }elsif($$hash{$key}[5] eq 'ovpn_host_tgt'){
                                        if(&fwlib::get_ovpn_host_ip($host,33) eq ''){
                                                $coloryellow='on';
-                                               &disable_rule($key);
-                                               $$hash{$key}[2]='';
                                        }
                                }
                        }
+                       #check if networkgroups or servicegroups are empty
+                       foreach my $netgroup (sort keys %customgrp){
+                               if(($$hash{$key}[4] eq $customgrp{$netgroup}[0] || $$hash{$key}[6] eq $customgrp{$netgroup}[0]) && $customgrp{$netgroup}[2] eq 'none'){
+                                       $coloryellow='on';
+                               }
+                       }
+                       foreach my $srvgroup (sort keys %customservicegrp){
+                               if($$hash{$key}[15] eq $customservicegrp{$srvgroup}[0] && $customservicegrp{$srvgroup}[2] eq 'none'){
+                                       $coloryellow='on';
+                               }
+                       }
                        $$hash{'ACTIVE'}=$$hash{$key}[2];
                        $count++;
                        if($coloryellow eq 'on'){
@@ -2698,7 +2707,7 @@ END
                                <td colspan='13'>&nbsp;</td>
                        </tr>
                        <tr>
-                               <td colspan='13'>
+                               <td colspan='13' style="padding-left:0px;padding-right:0px">
                                        <table width="100%" border='1' rules="cols" cellspacing='0'>
 END
 
@@ -2781,14 +2790,16 @@ END
                                                <font color="$Header::colourorange">$Lang::tr{'orange'}</font>
                                                ($Lang::tr{'fwdfw pol block'})
                                        </td>
+END
+                       }
+
+                       print <<END;
                                        <td align='center'>
                                                <font color="$Header::colourgreen">$Lang::tr{'green'}</font>
                                                ($Lang::tr{'fwdfw pol block'})
                                        </td>
+                               </tr>
 END
-                       }
-
-                       print"</tr>";
                }
 
                print <<END;