]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/firewall/convert-outgoingfw
d2672cbe0bdc3fed67888481c3aebe910665ad05
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
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 ###############################################################################
22 # This script converts old groups and firewallrules #
23 # to the new one. This is a 3-step process. #
24 # STEP1: convert groups ->LOG /var/log/converters #
25 # STEP2: convert rules ->LOG /var/log/converters #
26 # STEP3: convert P2P rules #
28 ###############################################################################
30 require '/var/ipfire/general-functions.pl';
31 require "${General::swroot}/lang.pl";
37 my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/";
38 my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/";
39 my $outgoingrules = "${General::swroot}/outgoing/rules";
40 my $outfwsettings = "${General::swroot}/outgoing/settings";
41 my $host = "Converted ";
42 my $confighosts = "${General::swroot}/fwhosts/customhosts";
43 my $confignets = "${General::swroot}/fwhosts/customnetworks";
44 my $configgroups = "${General::swroot}/fwhosts/customgroups";
45 my $ovpnsettings = "${General::swroot}/ovpn/settings";
46 my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig";
47 my $ccdconfig = "${General::swroot}/ovpn/ccd.conf";
48 my $fwdfwconfig = "${General::swroot}/firewall/config";
49 my $outfwconfig = "${General::swroot}/firewall/outgoing";
50 my $fwdfwsettings = "${General::swroot}/firewall/settings";
51 my @ipgroups = qx(ls
$ipgrouppath);
52 my @macgroups = qx(ls
$macgrouppath);
65 my %ovpnSettings = ();
66 my @active= ('Aktiv', 'aktiv', 'Active', 'Activo', 'Actif', 'Actief', 'Aktywne', 'Активен', 'Aktif');
67 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnSettings);
68 &General
::readhash
($outfwsettings,\
%outsettings);
69 &General
::readhash
("${General::swroot}/ethernet/settings", \
%ownnet);
71 #ONLY RUN if /var/ipfire/outgoing exists
72 if ( -d
"/var/ipfire/outgoing"){
77 system("/usr/local/bin/firewallctrl");
80 if(! -d
"/var/log/converters"){ mkdir("/var/log/converters");}
81 if( -f
"/var/log/converters/groups-convert.log"){rmtree
("var/log/converters");}
82 open (LOG
, ">/var/log/converters/groups-convert.log") or die $!;
84 foreach my $group (@ipgroups){
87 print LOG
"\n$now Processing IP-GROUP: $group...\n";
88 open (DATEI
, "<$ipgrouppath/$group");
90 foreach my $ip (@zeilen){
93 print LOG
"$now Check IP $ip from Group $group ";
94 my $val=&check_ip
($ip);
96 push(@hostarray,$val.",ip");
97 print LOG
"$now -> OK\n";
100 print LOG
"$now -> IP \"$ip\" from group $group not converted (invalid IP) \n";
104 &new_hostgrp
($group,'ip');
110 #MAC Group processing
111 foreach my $group (@macgroups){
113 print LOG
"\nProcessing MAC-GROUP: $group...\n";
114 open (DATEI
, "<$macgrouppath/$group");
115 my @zeilen = <DATEI
>;
116 foreach my $mac (@zeilen){
119 print LOG
"$now Checking MAC $mac from group $group ";
121 if(&General
::validmac
($mac)){
125 push(@hostarray,$val.",mac");
126 print LOG
"$now -> OK\n";
129 print LOG
"$now -> Mac $mac from group $group not converted (invalid MAC)\n";
133 &new_hostgrp
($group,'mac');
143 #ip with subnet in decimal
144 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d
{1,2})$/){
145 $adr=int($1).".".int($2).".".int($3).".".int($4);
146 my $b = &General
::iporsubtodec
($5);
148 }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
149 $adr=int($1).".".int($2).".".int($3).".".int($4);
150 if(&General
::validip
($adr)){
151 $a=$adr."/255.255.255.255";
154 if(&General
::validipandmask
($adr)){
155 $a=&General
::iporsubtodec
($adr);
161 &General
::readhasharray
($confighosts,\
%hosts);
162 &General
::readhasharray
($confignets,\
%nets);
163 &General
::readhasharray
($configgroups,\
%groups);
166 my $name; #"converted"
168 my $name3; #custom host/custom net
170 foreach my $adr (@hostarray){
172 my ($ip,$type) = split(",",$adr);
173 my ($ippart,$subnet) = split("/",$ip);
174 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
176 print LOG
"Processing SINGLE HOST $ippart/$subnet from group $grp\n";
177 if(!&check_host
($ip)){
178 my $key = &General
::findhasharraykey
(\
%hosts);
180 $name2=$name.$ippart;
181 $name3="Custom Host";
182 $hosts{$key}[0] = $name2;
183 $hosts{$key}[1] = $type;
184 $hosts{$key}[2] = $ip;
185 $hosts{$key}[3] = '';
186 print LOG
"->Host (IP) $ip added to custom hosts\n"
188 print LOG
"->Host (IP) $ip already exists in custom hosts\n";
190 $name2=$name.$ippart;
192 $name2=$name.$ippart;
193 $name3="Custom Host";
195 }elsif($byte4 < '255'){
196 print LOG
"Processing NETWORK $ippart/$subnet from Group $grp\n";
197 if(!&check_net
($ippart,$subnet)){
198 #Check if this network is one one of IPFire internal networks
199 if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &General
::IpInSubnet
($ippart,$ownnet{'GREEN_NETADDRESS'},$ownnet{'GREEN_NETMASK'}))
202 $name3='Standard Network';
203 }elsif (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &General
::IpInSubnet
($ippart,$ownnet{'ORANGE_NETADDRESS'},$ownnet{'ORANGE_NETMASK'}))
206 $name3='Standard Network';
207 }elsif (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &General
::IpInSubnet
($ippart,$ownnet{'BLUE_NETADDRESS'},$ownnet{'BLUE_NETMASK'}))
210 $name3='Standard Network';
211 }elsif ($ippart eq '0.0.0.0')
214 $name3='Standard Network';
215 }elsif(defined($ovpnSettings{'DOVPN_SUBNET'}) && "$ippart/".&General
::iporsubtodec
($subnet) eq $ovpnSettings{'DOVPN_SUBNET'})
217 $name2='OpenVPN-Dyn';
218 $name3='Standard Network';
220 my $netkey = &General
::findhasharraykey
(\
%nets);
222 $name2=$name.$ippart;
223 $name3="Custom Network";
224 $nets{$netkey}[0] = $name2;
225 $nets{$netkey}[1] = $ippart;
226 $nets{$netkey}[2] = $subnet;
227 $nets{$netkey}[3] = '';
228 print LOG
"->Network $ippart/$subnet added to custom networks\n";
231 print LOG
"Network $ippart already exists in custom networks\n";
233 $name2=$name.$ippart;
235 $name2=$name.$ippart;
236 $name3="Custom Network";
239 if($name2 && !&check_grp
($grp,$name2)){
240 my $grpkey = &General
::findhasharraykey
(\
%groups);
241 $groups{$grpkey}[0] = $grp;
242 $groups{$grpkey}[1] = '';
243 $groups{$grpkey}[2] = $name2;
244 $groups{$grpkey}[3] = $name3;
245 print LOG
"->$name2 added to group $grp\n";
247 }elsif($run eq 'mac'){
249 my ($mac,$type) = split(",",$adr);
250 print LOG
"Processing HOST (MAC) $mac\n";
251 if(!&check_host
($mac)){
252 my $key = &General
::findhasharraykey
(\
%hosts);
257 $name3="Custom Host";
258 $hosts{$key}[0] = $name2;
259 $hosts{$key}[1] = $type;
260 $hosts{$key}[2] = $mac;
261 print LOG
"->Host (MAC) $mac added to custom hosts\n";
265 print LOG
"->Host (MAC) $mac already exists in custom hosts \n";
268 $name3="Custom Host";
270 if($name2 && !&check_grp
($grp,$name2)){
271 my $grpkey = &General
::findhasharraykey
(\
%groups);
272 $groups{$grpkey}[0] = $grp;
273 $groups{$grpkey}[1] = '';
274 $groups{$grpkey}[2] = $name2;
275 $groups{$grpkey}[3] = $name3;
276 print LOG
"->$name2 added to group $grp\n";
281 &General
::writehasharray
($confighosts,\
%hosts);
282 &General
::writehasharray
($configgroups,\
%groups);
283 &General
::writehasharray
($confignets,\
%nets);
289 foreach my $key (sort keys %hosts)
291 if($hosts{$key}[2] eq $ip)
302 foreach my $key (sort keys %nets)
304 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
315 foreach my $key (sort keys %groups)
317 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
326 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);
328 if( -f
"/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
329 open (LOG
, ">/var/log/converters/outgoingfw-convert.log") or die $!;
331 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
332 if ($outsettings{'POLICY'} eq 'MODE1'){
333 $fwdsettings{'POLICY'}='MODE1';
334 $fwdsettings{'POLICY1'}='MODE2';
338 $fwdsettings{'POLICY'}='MODE2';
339 $fwdsettings{'POLICY1'}='MODE2';
343 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
344 open (DATEI
, "<$outgoingrules");
346 foreach my $rule (@lines)
348 &General
::readhasharray
($fwdfwconfig,\
%fwconfig);
349 &General
::readhasharray
($outfwconfig,\
%fwconfigout);
353 print LOG
"$now processing: $rule\n";
355 @configline = split( /\;/, $rule );
357 if($configline[0] eq $type){
358 #some variables we can use from old config
359 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
360 if($configline[3] eq 'all' && $configline[8] ne ''){
363 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
366 push(@prot,$configline[3]);
368 if($configline[4] ne ''){
369 $configline[4] =~ s/,/;/g;
370 $remark = $configline[4];
372 #find all "active" tags in all language files and check them against the old config
375 $logging='1' if ($_ eq $configline[9]);
377 if($logging eq '1' ){ $log='ON';}else{$log='';}
378 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'){
379 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
387 $time_mon=$configline[10];
388 $time_tue=$configline[11];
389 $time_wed=$configline[12];
390 $time_thu=$configline[13];
391 $time_fri=$configline[14];
392 $time_sat=$configline[15];
393 $time_sun=$configline[16];
394 $time_from=$configline[17];
395 $time_to=$configline[18];
396 ############################################################
398 if ($configline[2] eq 'green') {
401 }elsif ($configline[2] eq 'orange') {
404 }elsif ($configline[2] eq 'red') {
407 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
408 $fwdsettings{'POLICY1'}=$outsettings{'POLICY'};
409 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
410 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
411 }elsif ($configline[2] eq 'blue') {
414 }elsif ($configline[2] eq 'ipsec') {
415 print LOG
"$now -> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
417 }elsif ($configline[2] eq 'ovpn') {
418 print LOG
"$now ->Creating networks/groups for OpenVPN...\n";
420 $grp1='cust_grp_src';
422 }elsif ($configline[2] eq 'ip') {
423 my $z=&check_ip
($configline[5]);
425 my ($ipa,$subn) = split("/",$z);
426 $subn=&General
::iporsubtocidr
($subn);
428 $source="$ipa/$subn";
430 print LOG
"$now -> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
433 }elsif ($configline[2] eq 'mac') {
434 if(&General
::validmac
($configline[6])){
436 $source=$configline[6];
438 print LOG
"$now -> Rule not converted, invalid MAC \"$configline[6]\" \n";
441 }elsif ($configline[2] eq 'all') {
445 foreach my $key (sort keys %groups){
446 if($groups{$key}[0] eq $configline[2]){
447 $grp1='cust_grp_src';
448 $source=$configline[2];
451 if ($grp1 eq '' || $source eq ''){
452 print LOG
"$now -> Rule not converted, no valid source recognised\n";
455 ############################################################
457 if($configline[7] ne '' && $configline[7] ne '0.0.0.0'){
458 my $address=&check_ip
($configline[7]);
460 my ($dip,$dsub) = split("/",$address);
461 $dsub=&General
::iporsubtocidr
($dsub);
463 $target="$dip/$dsub";
465 my $getwebsiteip=&get_ip_from_domain
($configline[7]);
468 $target=$getwebsiteip;
469 $remark.=" $configline[7]";
471 print LOG
"$now -> Rule not converted, invalid domain \"$configline[7]\"\n";
479 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
481 my @parts=split(",",$configline[8]);
484 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
485 if(&General
::validport
($_)){
490 print LOG
"$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n";
494 my ($a1,$a2) = split(/\:/,$_);
495 if (&General
::validport
($a1) && &General
::validport
($a2) && $a1 < $a2){
497 push (@values,"$a1:$a2");
500 print LOG
"$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n";
505 $port=join("|",@values);
510 print LOG
"-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
514 foreach my $protocol (@prot){
516 if ($source eq 'IPFire'){
521 $protocol=uc($protocol);
522 print LOG
"$now -> Converted: $action,$chain,$active,$grp1,$source,$grp2,$target,,$protocol,,,$useport,,,$grp3,$port,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to\n";
523 #Put rules into system....
524 ###########################
525 #check for double rules
526 foreach my $key (sort keys %fwconfig){
527 if("$action,$chain,$active,$grp1,$source,$grp2,$target,$protocol,$useport,$grp3,$port,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to"
528 eq "$fwconfig{$key}[0],$fwconfig{$key}[1],$fwconfig{$key}[2],$fwconfig{$key}[3],$fwconfig{$key}[4],$fwconfig{$key}[5],$fwconfig{$key}[6],$fwconfig{$key}[8],$fwconfig{$key}[11],$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]"){
534 if ($chain eq 'FORWARDFW'){
535 my $key = &General
::findhasharraykey
(\
%fwconfig);
536 $fwconfig{$key}[0] = $action;
537 $fwconfig{$key}[1] = $chain;
538 $fwconfig{$key}[2] = $active;
539 $fwconfig{$key}[3] = $grp1;
540 $fwconfig{$key}[4] = $source;
541 $fwconfig{$key}[5] = $grp2;
542 $fwconfig{$key}[6] = $target;
543 $fwconfig{$key}[8] = $protocol;
544 $fwconfig{$key}[11] = $useport;
545 $fwconfig{$key}[14] = $grp3;
546 $fwconfig{$key}[15] = $port;
547 $fwconfig{$key}[16] = $remark;
548 $fwconfig{$key}[17] = $log;
549 $fwconfig{$key}[18] = $time;
550 $fwconfig{$key}[19] = $time_mon;
551 $fwconfig{$key}[20] = $time_tue;
552 $fwconfig{$key}[21] = $time_wed;
553 $fwconfig{$key}[22] = $time_thu;
554 $fwconfig{$key}[23] = $time_fri;
555 $fwconfig{$key}[24] = $time_sat;
556 $fwconfig{$key}[25] = $time_sun;
557 $fwconfig{$key}[26] = $time_from;
558 $fwconfig{$key}[27] = $time_to;
559 $fwconfig{$key}[28] = '';
560 $fwconfig{$key}[29] = 'ALL';
561 $fwconfig{$key}[30] = '';
562 $fwconfig{$key}[31] = 'dnat';
563 &General
::writehasharray
($fwdfwconfig,\
%fwconfig);
565 my $key = &General
::findhasharraykey
(\
%fwconfigout);
566 $fwconfigout{$key}[0] = $action;
567 $fwconfigout{$key}[1] = $chain;
568 $fwconfigout{$key}[2] = $active;
569 $fwconfigout{$key}[3] = $grp1;
570 $fwconfigout{$key}[4] = $source;
571 $fwconfigout{$key}[5] = $grp2;
572 $fwconfigout{$key}[6] = $target;
573 $fwconfigout{$key}[8] = $protocol;
574 $fwconfigout{$key}[11] = $useport;
575 $fwconfigout{$key}[14] = $grp3;
576 $fwconfigout{$key}[15] = $port;
577 $fwconfigout{$key}[16] = $remark;
578 $fwconfigout{$key}[17] = $log;
579 $fwconfigout{$key}[18] = $time;
580 $fwconfigout{$key}[19] = $time_mon;
581 $fwconfigout{$key}[20] = $time_tue;
582 $fwconfigout{$key}[21] = $time_wed;
583 $fwconfigout{$key}[22] = $time_thu;
584 $fwconfigout{$key}[23] = $time_fri;
585 $fwconfigout{$key}[24] = $time_sat;
586 $fwconfigout{$key}[25] = $time_sun;
587 $fwconfigout{$key}[26] = $time_from;
588 $fwconfigout{$key}[27] = $time_to;
589 $fwconfigout{$key}[28] = '';
590 $fwconfigout{$key}[29] = 'ALL';
591 $fwconfigout{$key}[30] = '';
592 $fwconfigout{$key}[31] = 'dnat';
593 &General
::writehasharray
($outfwconfig,\
%fwconfigout);
602 sub get_ip_from_domain
607 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
609 $resolvedip=inet_ntoa
($addrs[0]);
617 &General
::readhasharray
($confighosts,\
%hosts);
618 &General
::readhasharray
($confignets,\
%nets);
619 &General
::readhasharray
($configgroups,\
%groups);
620 &General
::readhasharray
($ovpnconfig,\
%configovpn);
621 &General
::readhasharray
($ccdconfig,\
%ccdconf);
622 &General
::readhash
($ovpnsettings,\
%settingsovpn);
625 if($settingsovpn{'DOVPN_SUBNET'}){
626 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
627 push (@ovpnnets,"$net,$subnet,dynamic");
628 print LOG
"$now ->found dynamic OpenVPN net\n";
630 foreach my $key (sort keys %ccdconf){
631 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
632 $subnet=&General
::iporsubtodec
($subnet);
633 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
634 print LOG
"$now ->found OpenVPN static net $net/$subnet\n";
636 foreach my $key (sort keys %configovpn){
637 if ($configovpn{$key}[3] eq 'net'){
638 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
639 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
640 print LOG
"$now ->found OpenVPN $net/$subnet $configovpn{$key}[2]\n";
643 #add ovpn nets to customnetworks/groups
644 foreach my $line (@ovpnnets){
646 my ($net,$subnet,$name) = split(",",$line);
647 if (!&check_net
($net,$subnet)){
648 my $netkey = &General
::findhasharraykey
(\
%nets);
649 $name2=$name."(ovpn)".$net;
650 $name3="Custom Network";
651 $nets{$netkey}[0] = $name2;
652 $nets{$netkey}[1] = $net;
653 $nets{$netkey}[2] = $subnet;
654 $nets{$netkey}[3] = '';
655 print LOG
"$now ->added $name2 $net/$subnet to customnetworks\n";
657 print LOG
"-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
660 my $grpkey = &General
::findhasharraykey
(\
%groups);
661 $groups{$grpkey}[0] = "ovpn";
662 $groups{$grpkey}[1] = '';
663 $groups{$grpkey}[2] = $name2;
664 $groups{$grpkey}[3] = "Custom Network";
665 print LOG
"$now ->added $name2 to customgroup ovpn\n";
670 &General
::writehasharray
($confighosts,\
%hosts);
671 &General
::writehasharray
($configgroups,\
%groups);
672 &General
::writehasharray
($confignets,\
%nets);
673 print LOG
"$now ->finished OVPN\n";
677 copy
("/var/ipfire/outgoing/p2protocols","/var/ipfire/firewall/p2protocols");
678 chmod oct('0777'), '/var/ipfire/firewall/p2protocols';