]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/convert-outgoingfw
c9077a8713dbbc4f2f403436269e93eb89d205ce
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
21 # Author: Alexander Marx (amarx@ipfire.org) #
22 ###############################################################################
24 # This script converts old groups and firewallrules #
25 # to the new one. This is a 3-step process. #
26 # STEP1: convert groups ->LOG /var/log/converters #
27 # STEP2: convert rules ->LOG /var/log/converters #
28 # STEP3: convert P2P rules #
30 ###############################################################################
32 require '/var/ipfire/general-functions.pl';
38 my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/";
39 my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/";
40 my $outgoingrules = "${General::swroot}/outgoing/rules";
41 my $outfwsettings = "${General::swroot}/outgoing/settings";
42 my $host = "Converted ";
43 my $confighosts = "${General::swroot}/fwhosts/customhosts";
44 my $confignets = "${General::swroot}/fwhosts/customnetworks";
45 my $configgroups = "${General::swroot}/fwhosts/customgroups";
46 my $ovpnsettings = "${General::swroot}/ovpn/settings";
47 my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig";
48 my $ccdconfig = "${General::swroot}/ovpn/ccd.conf";
49 my $fwdfwconfig = "${General::swroot}/forward/config";
50 my $outfwconfig = "${General::swroot}/forward/outgoing";
51 my $fwdfwsettings = "${General::swroot}/forward/settings";
52 my @ipgroups = qx(ls
$ipgrouppath);
53 my @macgroups = qx(ls
$macgrouppath);
66 my %ovpnSettings = ();
67 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnSettings);
68 &General
::readhash
($outfwsettings,\
%outsettings);
69 &General
::readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
70 #ONLY RUN if /var/ipfire/outgoing exists
71 if ( -d
"/var/ipfire/outgoing"){
76 system("/usr/local/bin/forwardfwctrl");
79 if(! -d
"/var/log/converters"){ mkdir("/var/log/converters");}
80 if( -f
"/var/log/converters/groups-convert.log"){rmtree
("var/log/converters");}
81 open (LOG
, ">/var/log/converters/groups-convert.log") or die $!;
83 foreach my $group (@ipgroups){
86 print LOG
"\n$now Processing IP-GROUP: $group...\n";
87 open (DATEI
, "<$ipgrouppath/$group");
89 foreach my $ip (@zeilen){
92 print LOG
"$now Check IP $ip from Group $group ";
93 my $val=&check_ip
($ip);
95 push(@hostarray,$val.",ip");
96 print LOG
"$now -> OK\n";
99 print LOG
"$now -> IP \"$ip\" from group $group not converted (invalid IP) \n";
103 &new_hostgrp
($group,'ip');
109 #MAC Group processing
110 foreach my $group (@macgroups){
112 print LOG
"\nProcessing MAC-GROUP: $group...\n";
113 open (DATEI
, "<$macgrouppath/$group");
114 my @zeilen = <DATEI
>;
115 foreach my $mac (@zeilen){
118 print LOG
"$now Checking MAC $mac from group $group ";
120 if(&General
::validmac
($mac)){
124 push(@hostarray,$val.",mac");
125 print LOG
"$now -> OK\n";
128 print LOG
"$now -> Mac $mac from group $group not converted (invalid MAC)\n";
132 &new_hostgrp
($group,'mac');
142 #ip with subnet in decimal
143 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d
{1,2})$/){
144 $adr=int($1).".".int($2).".".int($3).".".int($4);
145 my $b = &General
::iporsubtodec
($5);
147 }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
148 $adr=int($1).".".int($2).".".int($3).".".int($4);
149 if(&General
::validip
($adr)){
150 $a=$adr."/255.255.255.255";
153 if(&General
::validipandmask
($adr)){
154 $a=&General
::iporsubtodec
($adr);
160 &General
::readhasharray
($confighosts,\
%hosts);
161 &General
::readhasharray
($confignets,\
%nets);
162 &General
::readhasharray
($configgroups,\
%groups);
165 my $name; #"converted"
167 my $name3; #custom host/custom net
168 foreach my $adr (@hostarray){
170 my ($ip,$type) = split(",",$adr);
171 my ($ippart,$subnet) = split("/",$ip);
172 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
174 print LOG
"Processing SINGLE HOST $ippart/$subnet from group $grp\n";
175 if(!&check_host
($ip)){
176 my $key = &General
::findhasharraykey
(\
%hosts);
178 $name2=$name.$ippart;
179 $name3="Custom Host";
180 $hosts{$key}[0] = $name2;
181 $hosts{$key}[1] = $type;
182 $hosts{$key}[2] = $ip;
183 $hosts{$key}[3] = '';
185 print LOG
"->Host (IP) $ip added to custom hosts\n"
187 print LOG
"->Host (IP) $ip already exists in custom hosts\n";
189 $name2=$name.$ippart;
190 foreach my $key (sort keys %hosts){
191 if($hosts{$key}[0] eq $name2){
196 $name2=$name.$ippart;
197 $name3="Custom Host";
199 }elsif($byte4 < '255'){
200 print LOG
"Processing NETWORK $ippart/$subnet from Group $grp\n";
201 if(!&check_net
($ippart,$subnet)){
202 #Check if this network is one one of IPFire internal networks
203 if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &General
::IpInSubnet
($ippart,$ownnet{'GREEN_NETADDRESS'},$ownnet{'GREEN_NETMASK'}))
206 $name3='Standard Network';
207 }elsif (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &General
::IpInSubnet
($ippart,$ownnet{'ORANGE_NETADDRESS'},$ownnet{'ORANGE_NETMASK'}))
210 $name3='Standard Network';
211 }elsif (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &General
::IpInSubnet
($ippart,$ownnet{'BLUE_NETADDRESS'},$ownnet{'BLUE_NETMASK'}))
214 $name3='Standard Network';
215 }elsif ($ippart eq '0.0.0.0')
218 $name3='Standard Network';
219 }elsif(defined($ovpnSettings{'DOVPN_SUBNET'}) && "$ippart/".&General
::iporsubtodec
($subnet) eq $ovpnSettings{'DOVPN_SUBNET'})
221 $name2='OpenVPN-Dyn';
222 $name3='Standard Network';
224 my $netkey = &General
::findhasharraykey
(\
%nets);
226 $name2=$name.$ippart;
227 $name3="Custom Network";
228 $nets{$netkey}[0] = $name2;
229 $nets{$netkey}[1] = $ippart;
230 $nets{$netkey}[2] = $subnet;
231 $nets{$netkey}[3] = '';
232 $nets{$netkey}[4] = 1;
233 print LOG
"->Network $ippart/$subnet added to custom networks\n";
236 print LOG
"Network $ippart already exists in custom networks\n";
238 $name2=$name.$ippart;
239 foreach my $key (sort keys %nets){
240 if($nets{$key}[0] eq $name2){
245 $name2=$name.$ippart;
246 $name3="Custom Network";
249 if($name2 && !&check_grp
($grp,$name2)){
250 my $grpkey = &General
::findhasharraykey
(\
%groups);
251 $groups{$grpkey}[0] = $grp;
252 $groups{$grpkey}[1] = '';
253 $groups{$grpkey}[2] = $name2;
254 $groups{$grpkey}[3] = $name3;
255 $groups{$grpkey}[4] = 0;
256 print LOG
"->$name2 added to group $grp\n";
258 }elsif($run eq 'mac'){
260 my ($mac,$type) = split(",",$adr);
261 print LOG
"Processing HOST (MAC) $mac\n";
262 if(!&check_host
($mac)){
263 my $key = &General
::findhasharraykey
(\
%hosts);
266 $name3="Custom Host";
267 $hosts{$key}[0] = $name2;
268 $hosts{$key}[1] = $type;
269 $hosts{$key}[2] = $mac;
270 $hosts{$key}[3] = '';
272 print LOG
"->Host (MAC) $mac added to custom hosts\n";
274 print LOG
"->Host (MAC) $mac already exists in custom hosts \n";
277 foreach my $key (sort keys %hosts){
278 if($hosts{$key}[0] eq $name2){
284 $name3="Custom Host";
286 if($name2 && !&check_grp
($grp,$name2)){
287 my $grpkey = &General
::findhasharraykey
(\
%groups);
288 $groups{$grpkey}[0] = $grp;
289 $groups{$grpkey}[1] = '';
290 $groups{$grpkey}[2] = $name2;
291 $groups{$grpkey}[3] = $name3;
292 $groups{$grpkey}[4] = 0;
293 print LOG
"->$name2 added to group $grp\n";
298 &General
::writehasharray
($confighosts,\
%hosts);
299 &General
::writehasharray
($configgroups,\
%groups);
300 &General
::writehasharray
($confignets,\
%nets);
306 foreach my $key (sort keys %hosts)
308 if($hosts{$key}[2] eq $ip)
319 foreach my $key (sort keys %nets)
321 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
332 foreach my $key (sort keys %groups)
334 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
343 my ($type,$action,$active,$grp1,$source,$grp2,$useport,$port,$prot,$grp3,$target,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to);
345 if( -f
"/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
346 open (LOG
, ">/var/log/converters/outgoingfw-convert.log") or die $!;
348 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
349 if ($outsettings{'POLICY'} eq 'MODE1'){
350 $fwdsettings{'POLICY'}='MODE1';
351 $fwdsettings{'POLICY1'}='MODE2';
355 $fwdsettings{'POLICY'}='MODE2';
356 $fwdsettings{'POLICY1'}='MODE2';
360 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
361 open (DATEI
, "<$outgoingrules");
363 foreach my $rule (@lines)
368 print LOG
"$now processing: $rule\n";
370 @configline = split( /\;/, $rule );
372 if($configline[0] eq $type){
373 #some variables we can use from old config
374 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
375 if($configline[3] eq 'all' && $configline[8] ne ''){
378 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
381 push(@prot,$configline[3]);
383 if($configline[4] ne ''){
384 $configline[4] =~ s/,/;/g;
385 $remark = $configline[4];
387 if($configline[9] eq 'Active'){ $log='ON';}else{$log='';}
388 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'){
389 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
397 $time_mon=$configline[10];
398 $time_tue=$configline[11];
399 $time_wed=$configline[12];
400 $time_thu=$configline[13];
401 $time_fri=$configline[14];
402 $time_sat=$configline[15];
403 $time_sun=$configline[16];
404 $time_from=$configline[17];
405 $time_to=$configline[18];
406 ############################################################
408 if ($configline[2] eq 'green') {
411 }elsif ($configline[2] eq 'orange') {
414 }elsif ($configline[2] eq 'red') {
417 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
418 $fwdsettings{'POLICY1'}=$outsettings{'POLICY'};
419 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
420 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
421 }elsif ($configline[2] eq 'blue') {
424 }elsif ($configline[2] eq 'ipsec') {
425 print LOG
"$now -> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
427 }elsif ($configline[2] eq 'ovpn') {
428 print LOG
"$now ->Creating networks/groups for OpenVPN...\n";
430 $grp1='cust_grp_src';
432 }elsif ($configline[2] eq 'ip') {
433 my $z=&check_ip
($configline[5]);
435 my ($ipa,$subn) = split("/",$z);
436 $subn=&General
::iporsubtocidr
($subn);
438 $source="$ipa/$subn";
440 print LOG
"$now -> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
443 }elsif ($configline[2] eq 'mac') {
444 if(&General
::validmac
($configline[6])){
446 $source=$configline[6];
448 print LOG
"$now -> Rule not converted, invalid MAC \"$configline[6]\" \n";
451 }elsif ($configline[2] eq 'all') {
455 foreach my $key (sort keys %groups){
456 if($groups{$key}[0] eq $configline[2]){
457 $grp1='cust_grp_src';
458 $source=$configline[2];
461 if ($grp1 eq '' || $source eq ''){
462 print LOG
"$now -> Rule not converted, no valid source recognised\n";
465 ############################################################
467 if($configline[7] ne ''){
468 my $address=&check_ip
($configline[7]);
470 my ($dip,$dsub) = split("/",$address);
471 $dsub=&General
::iporsubtocidr
($dsub);
473 $target="$dip/$dsub";
475 my $getwebsiteip=&get_ip_from_domain
($configline[7]);
478 $target=$getwebsiteip;
479 $remark.=" $configline[7]";
481 print LOG
"$now -> Rule not converted, invalid domain \"$configline[7]\"\n";
489 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
491 my @parts=split(",",$configline[8]);
494 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
495 if(&General
::validport
($_)){
500 print LOG
"$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n";
504 my ($a1,$a2) = split(/\:/,$_);
505 if (&General
::validport
($a1) && &General
::validport
($a2) && $a1 < $a2){
507 push (@values,"$a1:$a2");
510 print LOG
"$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n";
515 $port=join("|",@values);
520 print LOG
"-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
522 &General
::readhasharray
($fwdfwconfig,\
%fwconfig);
523 &General
::readhasharray
($outfwconfig,\
%fwconfigout);
526 foreach my $protocol (@prot){
528 if ($source eq 'IPFire'){
533 $protocol=uc($protocol);
534 print LOG
"$now -> Converted: $action,$chain,$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";
535 #Put rules into system....
536 ###########################
537 #check for double rules
538 foreach my $key (sort keys %fwconfig){
539 if("$action,$chain,$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"
540 eq "$fwconfig{$key}[0],$fwconfig{$key}[1],$fwconfig{$key}[2],$fwconfig{$key}[3],$fwconfig{$key}[4],$fwconfig{$key}[5],$fwconfig{$key}[6],,,,,$fwconfig{$key}[11],$fwconfig{$key}[12],,$fwconfig{$key}[14],$fwconfig{$key}[15],$fwconfig{$key}[16],$fwconfig{$key}[17],$fwconfig{$key}[18],$fwconfig{$key}[19],$fwconfig{$key}[20],$fwconfig{$key}[21],$fwconfig{$key}[22],$fwconfig{$key}[23],$fwconfig{$key}[24],$fwconfig{$key}[25],$fwconfig{$key}[26],$fwconfig{$key}[27]"){
546 #increase groupcounter
548 if($grp1 eq 'cust_grp_src'){
549 foreach my $key (sort keys %groups){
550 if($groups{$key}[0] eq $source){
556 &General
::writehasharray
($configgroups,\
%groups);
559 if ($chain eq 'FORWARDFW'){
560 my $key = &General
::findhasharraykey
(\
%fwconfig);
561 $fwconfig{$key}[0] = $action;
562 $fwconfig{$key}[1] = $chain;
563 $fwconfig{$key}[2] = $active;
564 $fwconfig{$key}[3] = $grp1;
565 $fwconfig{$key}[4] = $source;
566 $fwconfig{$key}[5] = $grp2;
567 $fwconfig{$key}[6] = $target;
568 $fwconfig{$key}[11] = $useport;
569 $fwconfig{$key}[12] = $protocol;
570 $fwconfig{$key}[14] = $grp3;
571 $fwconfig{$key}[15] = $port;
572 $fwconfig{$key}[16] = $remark;
573 $fwconfig{$key}[17] = $log;
574 $fwconfig{$key}[18] = $time;
575 $fwconfig{$key}[19] = $time_mon;
576 $fwconfig{$key}[20] = $time_tue;
577 $fwconfig{$key}[21] = $time_wed;
578 $fwconfig{$key}[22] = $time_thu;
579 $fwconfig{$key}[23] = $time_fri;
580 $fwconfig{$key}[24] = $time_sat;
581 $fwconfig{$key}[25] = $time_sun;
582 $fwconfig{$key}[26] = $time_from;
583 $fwconfig{$key}[27] = $time_to;
584 $fwconfig{$key}[28] = '';
585 $fwconfig{$key}[29] = 'ALL';
586 $fwconfig{$key}[30] = '';
587 $fwconfig{$key}[31] = 'dnat';
589 my $key = &General
::findhasharraykey
(\
%fwconfigout);
590 $fwconfigout{$key}[0] = $action;
591 $fwconfigout{$key}[1] = $chain;
592 $fwconfigout{$key}[2] = $active;
593 $fwconfigout{$key}[3] = $grp1;
594 $fwconfigout{$key}[4] = $source;
595 $fwconfigout{$key}[5] = $grp2;
596 $fwconfigout{$key}[6] = $target;
597 $fwconfigout{$key}[11] = $useport;
598 $fwconfigout{$key}[12] = $protocol;
599 $fwconfigout{$key}[14] = $grp3;
600 $fwconfigout{$key}[15] = $port;
601 $fwconfigout{$key}[16] = $remark;
602 $fwconfigout{$key}[17] = $log;
603 $fwconfigout{$key}[18] = $time;
604 $fwconfigout{$key}[19] = $time_mon;
605 $fwconfigout{$key}[20] = $time_tue;
606 $fwconfigout{$key}[21] = $time_wed;
607 $fwconfigout{$key}[22] = $time_thu;
608 $fwconfigout{$key}[23] = $time_fri;
609 $fwconfigout{$key}[24] = $time_sat;
610 $fwconfigout{$key}[25] = $time_sun;
611 $fwconfigout{$key}[26] = $time_from;
612 $fwconfigout{$key}[27] = $time_to;
613 $fwconfigout{$key}[28] = '';
614 $fwconfigout{$key}[29] = 'ALL';
615 $fwconfigout{$key}[30] = '';
616 $fwconfigout{$key}[31] = 'dnat';
618 &General
::writehasharray
($fwdfwconfig,\
%fwconfig);
619 &General
::writehasharray
($outfwconfig,\
%fwconfigout);
627 sub get_ip_from_domain
632 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
634 $resolvedip=inet_ntoa
($addrs[0]);
642 &General
::readhasharray
($confighosts,\
%hosts);
643 &General
::readhasharray
($confignets,\
%nets);
644 &General
::readhasharray
($configgroups,\
%groups);
645 &General
::readhasharray
($ovpnconfig,\
%configovpn);
646 &General
::readhasharray
($ccdconfig,\
%ccdconf);
647 &General
::readhash
($ovpnsettings,\
%settingsovpn);
650 if($settingsovpn{'DOVPN_SUBNET'}){
651 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
652 push (@ovpnnets,"$net,$subnet,dynamic");
653 print LOG
"$now ->found dynamic OpenVPN net\n";
655 foreach my $key (sort keys %ccdconf){
656 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
657 $subnet=&General
::iporsubtodec
($subnet);
658 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
659 print LOG
"$now ->found OpenVPN static net $net/$subnet\n";
661 foreach my $key (sort keys %configovpn){
662 if ($configovpn{$key}[3] eq 'net'){
663 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
664 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
665 print LOG
"$now ->found OpenVPN $net/$subnet $configovpn{$key}[2]\n";
668 #add ovpn nets to customnetworks/groups
669 foreach my $line (@ovpnnets){
671 my ($net,$subnet,$name) = split(",",$line);
672 if (!&check_net
($net,$subnet)){
673 my $netkey = &General
::findhasharraykey
(\
%nets);
674 $name2=$name."(ovpn)".$net;
675 $name3="Custom Network";
676 $nets{$netkey}[0] = $name2;
677 $nets{$netkey}[1] = $net;
678 $nets{$netkey}[2] = $subnet;
679 $nets{$netkey}[3] = '';
680 $nets{$netkey}[4] = 1;
681 print LOG
"$now ->added $name2 $net/$subnet to customnetworks\n";
683 print LOG
"-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
686 my $grpkey = &General
::findhasharraykey
(\
%groups);
687 $groups{$grpkey}[0] = "ovpn";
688 $groups{$grpkey}[1] = '';
689 $groups{$grpkey}[2] = $name2;
690 $groups{$grpkey}[3] = "Custom Network";
691 $groups{$grpkey}[4] = 0;
692 print LOG
"$now ->added $name2 to customgroup ovpn\n";
697 &General
::writehasharray
($confighosts,\
%hosts);
698 &General
::writehasharray
($configgroups,\
%groups);
699 &General
::writehasharray
($confignets,\
%nets);
700 print LOG
"$now ->finished OVPN\n";
704 copy
("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
705 chmod oct('0777'), '/var/ipfire/forward/p2protocols';