]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - config/forwardfw/convert-outgoingfw
Forward Firewall: added Policymode for OUTGOING to converterscript
[people/teissler/ipfire-2.x.git] / config / forwardfw / convert-outgoingfw
old mode 100644 (file)
new mode 100755 (executable)
index de6a67c..3c11f92
@@ -2,7 +2,10 @@
 
 
 require '/var/ipfire/general-functions.pl';
+
 use Socket;
+use File::Path;
+use File::Copy;
 
 my $ipgrouppath        = "${General::swroot}/outgoing/groups/ipgroups/";
 my $macgrouppath       = "${General::swroot}/outgoing/groups/macgroups/";
@@ -33,23 +36,27 @@ my %fwdsettings=();
 
 &process_groups;
 &process_rules;
-
+&process_p2p;
+system("/usr/local/bin/forwardfwctrl");
 sub process_groups
 {
        if(! -d "/var/log/converters"){ mkdir("/var/log/converters");}
-       if( -f "/var/log/converters/groups-convert.log"){unlink ("/var/log/converters/groups-convert.log");}
+       if( -f "/var/log/converters/groups-convert.log"){rmtree("var/log/converters");}
        open (LOG, ">/var/log/converters/groups-convert.log") or die $!;
        #IP Group processing
        foreach my $group (@ipgroups){
                chomp $group;
+               print LOG "\nProcessing IP-GROUP: $group...\n";
                open (DATEI, "<$ipgrouppath/$group");
                my @zeilen = <DATEI>;
                foreach my $ip (@zeilen){
                        chomp($ip);
                        $ip =~ s/\s//gi;
+                       print LOG "Check IP $ip from Group $group ";
                        my $val=&check_ip($ip);
                        if($val){
                                push(@hostarray,$val.",ip");
+                               print LOG "-> OK\n";
                        }
                        else{
                                print LOG "-> IP \"$ip\" from group $group not converted (invalid IP) \n";
@@ -65,17 +72,20 @@ sub process_groups
        #MAC Group processing
        foreach my $group (@macgroups){
                chomp $group;
+               print LOG "\nProcessing MAC-GROUP: $group...\n";
                open (DATEI, "<$macgrouppath/$group");
                my @zeilen = <DATEI>;
                foreach my $mac (@zeilen){
                        chomp($mac);
                        $mac =~ s/\s//gi;
+                       print LOG "Checking MAC $mac from group $group ";
                        #MAC checking
                        if(&General::validmac($mac)){
                                $val=$mac;
                        }
                        if($val){
                                push(@hostarray,$val.",mac");
+                               print LOG "-> OK\n";
                        }
                        else{
                                print LOG "-> Mac $mac from group $group not converted (invalid MAC)\n";
@@ -96,8 +106,7 @@ sub check_ip
                $adr=int($1).".".int($2).".".int($3).".".int($4);
                my $b = &General::iporsubtodec($5);
                $a=$adr."/".$b;
-       }
-       if($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
+       }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
                $adr=int($1).".".int($2).".".int($3).".".int($4);
                if(&General::validip($adr)){
                        $a=$adr."/255.255.255.255";
@@ -123,7 +132,8 @@ sub new_hostgrp
                        my ($ip,$type)                  = split(",",$adr);
                        my ($ippart,$subnet)    = split("/",$ip);
                        my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
-                       if($byte4 eq '255'){ 
+                       if($byte4 eq '255'){
+                               print LOG "Processing SINGLE HOST $ippart/$subnet from group $grp\n"; 
                                if(!&check_host($ip)){
                                        my $key         = &General::findhasharraykey(\%hosts);
                                        $name="host ";
@@ -133,10 +143,12 @@ sub new_hostgrp
                                        $hosts{$key}[1] = $type;
                                        $hosts{$key}[2] = $ip;
                                        $hosts{$key}[3] = 1;
+                                       print LOG "->Host (IP) $ip added to custom hosts\n"
                                }else{
-                                       print LOG "Host (IP) $ip already exists\n";
+                                       print LOG "->Host (IP) $ip already exists\n";
                                }
                        }elsif($byte4 < '255'){
+                               print LOG "Processing NETWORK $ippart/$subnet from Group $grp\n";
                                if(!&check_net($ippart,$subnet)){
                                        my $netkey      =  &General::findhasharraykey(\%nets);
                                        $name="net ";
@@ -146,6 +158,7 @@ sub new_hostgrp
                                        $nets{$netkey}[1] = $ippart;
                                        $nets{$netkey}[2] = $subnet;
                                        $nets{$netkey}[3] = 1;
+                                       print LOG "->Network $ippart/$subnet added to custom networks\n";
                                }else{
                                        print LOG "Network $ippart already exists\n";
                                }
@@ -157,10 +170,12 @@ sub new_hostgrp
                                $groups{$grpkey}[2]     = $name2;
                                $groups{$grpkey}[3]     = $name3;
                                $groups{$grpkey}[4]     = 0;
+                               print LOG "->$name2 added to group $grp\n";
                        }
                }elsif($run eq 'mac'){
                        #MACRUN
                        my ($mac,$type)                         = split(",",$adr);
+                       print LOG "Processing HOST (MAC) $mac\n";
                        if(!&check_host($mac)){
                                my $key         = &General::findhasharraykey(\%hosts);
                                $name="host ";
@@ -170,8 +185,9 @@ sub new_hostgrp
                                $hosts{$key}[1] = $type;
                                $hosts{$key}[2] = $mac;
                                $hosts{$key}[3] = 1;
+                               print LOG "->Host (MAC) $mac added to custom hosts\n";
                        }else{
-                               print LOG "Host (MAC) $mac already exists\n";
+                               print LOG "->Host (MAC) $mac already exists\n";
                        }
                        if($name2){
                                my $grpkey      = &General::findhasharraykey(\%groups);
@@ -180,12 +196,15 @@ sub new_hostgrp
                                $groups{$grpkey}[2]     = $name2;
                                $groups{$grpkey}[3]     = $name3;
                                $groups{$grpkey}[4]     = 0;
+                               print LOG "->$name2 added to group $grp\n";
                        }
                }
        }
+       @hostarray=();
        &General::writehasharray($confighosts,\%hosts);
        &General::writehasharray($configgroups,\%groups);
        &General::writehasharray($confignets,\%nets);
+
 }
 sub check_host
 {
@@ -227,10 +246,10 @@ sub check_grp
 }
 sub process_rules
 {
-       print "Outgoing ist im Mode $outsettings{'POLICY'}\n";
        &General::readhash($fwdfwsettings,\%fwdsettings);
        if($fwdsettings{'POLICY'} ne $outsettings{'POLICY'}){
                $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
+               $fwdsettings{'POLICY1'}='MODE2';
                &General::writehash($fwdfwsettings,\%fwdsettings);
        }
        #open LOG
@@ -252,27 +271,28 @@ sub process_rules
        foreach my $rule (@lines)
        {
                chomp($rule);
+               $port='';
                print LOG "processing: $rule\n";
-               print "$rule\n";
-               my @configline = split( /\;/, $rule );
+               my @configline=();
+               @configline = split( /\;/, $rule );
                my @prot=();
                if($configline[0] eq $type){
                        #some variables we can use from old config
                        if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
-                       if($configline[3] eq 'all'){ 
-                               push(@prot,"tcp");
-                               push(@prot,"udp");
-                               $useport='ON';
-                               $grp3='TGT_PORT';
-                               
+                       if($configline[3] eq 'all' && $configline[8] ne ''){ 
+                               push(@prot,"TCP");
+                               push(@prot,"UDP");
+                       }elsif($configline[3] eq 'all' && $configline[8] eq ''){
+                               push(@prot,"");
                        }else{
                                push(@prot,$configline[3]);
-                               $useport='ON';
-                               $grp3='TGT_PORT';
                        }
-                       if($configline[4] ne ''){ $remark=$configline[4];}else{$remark='';}
-                       if($configline[9] eq 'aktiv'){ $log='ON';}else{$log='';}
-                       
+
+                       if($configline[4] ne ''){ 
+                               $configline[4] =~ s/,/;/g;
+                               $remark = $configline[4];
+                       }else{$remark = '';}
+                       if($configline[9] eq 'Active'){ $log='ON';}else{$log='';}
                        if($configline[10] eq 'on' && $configline[11] eq 'on' && $configline[12] eq 'on' && $configline[13] eq 'on' && $configline[14] eq 'on' && $configline[15] eq 'on' && $configline[16] eq 'on'){
                                if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
                                        $time='';
@@ -309,12 +329,16 @@ sub process_rules
                                print LOG "-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
                                next;
                        }elsif ($configline[2] eq 'ovpn') {
-                               &build_ovpn_grp;                                
+                               &build_ovpn_grp;                
+                               $grp1='cust_grp_src';
+                               $source='ovpn'          
                        }elsif ($configline[2] eq 'ip') {
                                my $z=&check_ip($configline[5]);
                                if($z){
+                                       my ($ipa,$subn) = split("/",$z);
+                                       $subn=&General::iporsubtocidr($subn);
                                        $grp1='src_addr';
-                                       $source=$z;
+                                       $source="$ipa/$subn";
                                }else{
                                        print LOG "-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
                                        next;
@@ -347,13 +371,16 @@ sub process_rules
                        if($configline[7] ne ''){
                                my $address=&check_ip($configline[7]);
                                 if($address){
+                                        my ($dip,$dsub) = split("/",$address);
+                                        $dsub=&General::iporsubtocidr($dsub);
                                         $grp2='tgt_addr';
-                                        $target=$address;
+                                        $target="$dip/$dsub";
                                 }elsif(!$address){
                                        my $getwebsiteip=&get_ip_from_domain($configline[7]);
                                        if ($getwebsiteip){
                                                $grp2='tgt_addr';
                                                $target=$getwebsiteip;  
+                                               $remark.=" $configline[7]";
                                        }else{
                                                print LOG "-> Rule not converted, invalid domain \"$configline[7]\"\n";
                                                next;
@@ -363,27 +390,34 @@ sub process_rules
                                $grp2='std_net_tgt';
                                $target='ALL';
                        }
-                       if($configline[8] ne ''){
-                               if (!($configline[8] =~ /^(\d+)\:(\d+)$/)) {
-                                       if(&General::validport($configline[8])){
-                                               $useport='ON';  
-                                               $port=$configline[8];
-                                               $grp3='TGT_PORT';
-                                       }else{
-                                               print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
-                                               next;
-                                       }
-                                }else{
-                                        my ($a1,$a2) = split(/\:/,$configline[8]);
-                                        if (&General::validport($a1) && &General::validport($a2) && $a1 < $a2){
-                                               $useport='ON';  
-                                               $port=$configline[8];
-                                               $grp3='TGT_PORT';
+                       if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
+                               my @values=();
+                               my @parts=split(",",$configline[8]);
+                               foreach (@parts){
+                                       if (!($_ =~ /^(\d+)\:(\d+)$/)) {
+                                               if(&General::validport($_)){
+                                                       $useport='ON';  
+                                                       push (@values,$_);
+                                                       $grp3='TGT_PORT';
+                                               }else{
+                                                       print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
+                                                       next;
+                                               }
                                         }else{
-                                               print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n"; 
-                                               next;
-                                        } 
+                                               my ($a1,$a2) = split(/\:/,$_);
+                                               if (&General::validport($a1) && &General::validport($a2) && $a1 < $a2){
+                                                       $useport='ON';  
+                                                       push (@values,"$a1:$a2");
+                                                       $grp3='TGT_PORT';
+                                               }else{
+                                                       print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n"; 
+                                                       next;
+                                               } 
+                                        }
                                 }
+                               $port=join("|",@values);
+                               @values=();
+                               @parts=();
                        }
                }else{
                        print LOG "-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
@@ -392,7 +426,6 @@ sub process_rules
                my $check;
                foreach my $protocol (@prot){
                        $protocol=uc($protocol);
-                       print"0:$action 1:FORWARDFW 2:$active 3:$grp1 4:$source 5:$grp2 6:$target 7:src_srv(leer) 8:srcprot(leer) 9:icmp(leer) 10:srcport(leer) 11:$useport 12:$protocol 13:icmp(leer) 14:$grp3 15:$port 16:$remark 17:$log 18:$time 19:$time_mon 20:$time_tue 21:$time_wed 22:$time_thu 23:$time_fri 24:$time_sat 25:$time_sun 26:$time_from 27:$time_to\n\n";
                        print LOG "-> Converted: $action,FORWARDFW,$active,$grp1,$source,$grp2,$target,,,,,$useport,$protocol,,$grp3,$port,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to\n";
                        #Put rules into system....
                        ###########################
@@ -416,11 +449,9 @@ sub process_rules
                                                }
                                        }
                                        if($check1 eq 'on'){
-                                               print" schreibe in gruppe\n";
                                                &General::writehasharray($configgroups,\%groups);
                                        }
                                }
-                               print"SCHREIBE REGEL!\n";
                                my $key = &General::findhasharraykey(\%fwconfig);
                                $fwconfig{$key}[0]      = $action;
                                $fwconfig{$key}[1]      = "FORWARDFW";
@@ -448,7 +479,6 @@ sub process_rules
                        }
                }
                &General::writehasharray($fwdfwconfig,\%fwconfig);
-               system("/usr/local/bin/forwardfwctrl");
                @prot=();
        }
        close(LOG);
@@ -520,3 +550,8 @@ sub build_ovpn_grp
        &General::writehasharray($configgroups,\%groups);
        &General::writehasharray($confignets,\%nets);
 }
+sub process_p2p
+{
+       copy("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
+       chmod oct('0777'), '/var/ipfire/forward/p2protocols';
+}