]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/convert-outgoingfw
2 ########################################################################
3 # Script: convert-outgoingfw
5 # Author: Alexander Marx (amarx@ipfire.org)
6 ########################################################################
8 # This script converts old groups and firewallrules
9 # to the new one. This is a 3-step process.
10 # STEP1: convert groups ->LOG /var/log/converters
11 # STEP2: convert rules ->LOG /var/log/converters
12 # STEP3: convert P2P rules
14 ########################################################################
16 require '/var/ipfire/general-functions.pl';
22 my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/";
23 my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/";
24 my $outgoingrules = "${General::swroot}/outgoing/rules";
25 my $outfwsettings = "${General::swroot}/outgoing/settings";
26 my $host = "Converted ";
27 my $confighosts = "${General::swroot}/fwhosts/customhosts";
28 my $confignets = "${General::swroot}/fwhosts/customnetworks";
29 my $configgroups = "${General::swroot}/fwhosts/customgroups";
30 my $ovpnsettings = "${General::swroot}/ovpn/settings";
31 my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig";
32 my $ccdconfig = "${General::swroot}/ovpn/ccd.conf";
33 my $fwdfwconfig = "${General::swroot}/forward/config";
34 my $outfwconfig = "${General::swroot}/forward/outgoing";
35 my $fwdfwsettings = "${General::swroot}/forward/settings";
36 my @ipgroups = qx(ls
$ipgrouppath);
37 my @macgroups = qx(ls
$macgrouppath);
49 &General
::readhash
($outfwsettings,\
%outsettings);
50 #ONLY RUN if /var/ipfire/outgoing exists
51 if ( -d
"/var/ipfire/outgoing"){
56 system("/usr/local/bin/forwardfwctrl");
59 if(! -d
"/var/log/converters"){ mkdir("/var/log/converters");}
60 if( -f
"/var/log/converters/groups-convert.log"){rmtree
("var/log/converters");}
61 open (LOG
, ">/var/log/converters/groups-convert.log") or die $!;
63 foreach my $group (@ipgroups){
65 print LOG
"\nProcessing IP-GROUP: $group...\n";
66 open (DATEI
, "<$ipgrouppath/$group");
68 foreach my $ip (@zeilen){
71 print LOG
"Check IP $ip from Group $group ";
72 my $val=&check_ip
($ip);
74 push(@hostarray,$val.",ip");
78 print LOG
"-> IP \"$ip\" from group $group not converted (invalid IP) \n";
82 &new_hostgrp
($group,'ip');
89 foreach my $group (@macgroups){
91 print LOG
"\nProcessing MAC-GROUP: $group...\n";
92 open (DATEI
, "<$macgrouppath/$group");
94 foreach my $mac (@zeilen){
97 print LOG
"Checking MAC $mac from group $group ";
99 if(&General
::validmac
($mac)){
103 push(@hostarray,$val.",mac");
107 print LOG
"-> Mac $mac from group $group not converted (invalid MAC)\n";
111 &new_hostgrp
($group,'mac');
121 #ip with subnet in decimal
122 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d
{1,2})$/){
123 $adr=int($1).".".int($2).".".int($3).".".int($4);
124 my $b = &General
::iporsubtodec
($5);
126 }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
127 $adr=int($1).".".int($2).".".int($3).".".int($4);
128 if(&General
::validip
($adr)){
129 $a=$adr."/255.255.255.255";
132 if(&General
::validipandmask
($adr)){
133 $a=&General
::iporsubtodec
($adr);
139 &General
::readhasharray
($confighosts,\
%hosts);
140 &General
::readhasharray
($confignets,\
%nets);
141 &General
::readhasharray
($configgroups,\
%groups);
144 my $name; #"converted"
146 my $name3; #custom host/custom net
147 foreach my $adr (@hostarray){
149 my ($ip,$type) = split(",",$adr);
150 my ($ippart,$subnet) = split("/",$ip);
151 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
153 print LOG
"Processing SINGLE HOST $ippart/$subnet from group $grp\n";
154 if(!&check_host
($ip)){
155 my $key = &General
::findhasharraykey
(\
%hosts);
157 $name2=$name.$ippart;
158 $name3="Custom Host";
159 $hosts{$key}[0] = $name2;
160 $hosts{$key}[1] = $type;
161 $hosts{$key}[2] = $ip;
162 $hosts{$key}[3] = '';
164 print LOG
"->Host (IP) $ip added to custom hosts\n"
166 print LOG
"->Host (IP) $ip already exists in custom hosts\n";
168 $name2=$name.$ippart;
169 foreach my $key (sort keys %hosts){
170 if($hosts{$key}[0] eq $name2){
175 $name2=$name.$ippart;
176 $name3="Custom Host";
178 }elsif($byte4 < '255'){
179 print LOG
"Processing NETWORK $ippart/$subnet from Group $grp\n";
180 if(!&check_net
($ippart,$subnet)){
181 my $netkey = &General
::findhasharraykey
(\
%nets);
183 $name2=$name.$ippart;
184 $name3="Custom Network";
185 $nets{$netkey}[0] = $name2;
186 $nets{$netkey}[1] = $ippart;
187 $nets{$netkey}[2] = $subnet;
188 $nets{$netkey}[3] = '';
189 $nets{$netkey}[4] = 1;
190 print LOG
"->Network $ippart/$subnet added to custom networks\n";
192 print LOG
"Network $ippart already exists in custom networks\n";
194 $name2=$name.$ippart;
195 foreach my $key (sort keys %nets){
196 if($nets{$key}[0] eq $name2){
201 $name2=$name.$ippart;
202 $name3="Custom Network";
205 if($name2 && !&check_grp
($grp,$name2)){
206 my $grpkey = &General
::findhasharraykey
(\
%groups);
207 $groups{$grpkey}[0] = $grp;
208 $groups{$grpkey}[1] = '';
209 $groups{$grpkey}[2] = $name2;
210 $groups{$grpkey}[3] = $name3;
211 $groups{$grpkey}[4] = 0;
212 print LOG
"->$name2 added to group $grp\n";
214 }elsif($run eq 'mac'){
216 my ($mac,$type) = split(",",$adr);
217 print LOG
"Processing HOST (MAC) $mac\n";
218 if(!&check_host
($mac)){
219 my $key = &General
::findhasharraykey
(\
%hosts);
222 $name3="Custom Host";
223 $hosts{$key}[0] = $name2;
224 $hosts{$key}[1] = $type;
225 $hosts{$key}[2] = $mac;
226 $hosts{$key}[3] = '';
228 print LOG
"->Host (MAC) $mac added to custom hosts\n";
230 print LOG
"->Host (MAC) $mac already exists in custom hosts \n";
233 foreach my $key (sort keys %hosts){
234 if($hosts{$key}[0] eq $name2){
240 $name3="Custom Host";
242 if($name2 && !&check_grp
($grp,$name2)){
243 my $grpkey = &General
::findhasharraykey
(\
%groups);
244 $groups{$grpkey}[0] = $grp;
245 $groups{$grpkey}[1] = '';
246 $groups{$grpkey}[2] = $name2;
247 $groups{$grpkey}[3] = $name3;
248 $groups{$grpkey}[4] = 0;
249 print LOG
"->$name2 added to group $grp\n";
254 &General
::writehasharray
($confighosts,\
%hosts);
255 &General
::writehasharray
($configgroups,\
%groups);
256 &General
::writehasharray
($confignets,\
%nets);
262 foreach my $key (sort keys %hosts)
264 if($hosts{$key}[2] eq $ip)
275 foreach my $key (sort keys %nets)
277 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
288 foreach my $key (sort keys %groups)
290 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
299 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);
300 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
301 if ($outsettings{'POLICY'} eq 'MODE1'){
302 $fwdfwsettings{'POLICY'}='MODE1';
305 }elsif($outsettings{'POLICY'} eq 'MODE2'){
306 $fwdsettings{'POLICY'}='MODE2';
312 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
315 if( -f
"/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
316 open (LOG
, ">/var/log/converters/outgoingfw-convert.log") or die $!;
317 open (DATEI
, "<$outgoingrules");
319 foreach my $rule (@lines)
323 print LOG
"processing: $rule\n";
325 @configline = split( /\;/, $rule );
327 if($configline[0] eq $type){
328 #some variables we can use from old config
329 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
330 if($configline[3] eq 'all' && $configline[8] ne ''){
333 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
336 push(@prot,$configline[3]);
338 if($configline[4] ne ''){
339 $configline[4] =~ s/,/;/g;
340 $remark = $configline[4];
342 if($configline[9] eq 'Active'){ $log='ON';}else{$log='';}
343 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'){
344 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
352 $time_mon=$configline[10];
353 $time_tue=$configline[11];
354 $time_wed=$configline[12];
355 $time_thu=$configline[13];
356 $time_fri=$configline[14];
357 $time_sat=$configline[15];
358 $time_sun=$configline[16];
359 $time_from=$configline[17];
360 $time_to=$configline[18];
361 ############################################################
363 if ($configline[2] eq 'green') {
366 }elsif ($configline[2] eq 'orange') {
369 }elsif ($configline[2] eq 'red') {
372 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
373 $fwdsettings{'POLICY1'}=$outsettings{'POLICY'};
374 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
375 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
376 }elsif ($configline[2] eq 'blue') {
379 }elsif ($configline[2] eq 'ipsec') {
380 print LOG
"-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
382 }elsif ($configline[2] eq 'ovpn') {
384 $grp1='cust_grp_src';
386 }elsif ($configline[2] eq 'ip') {
387 my $z=&check_ip
($configline[5]);
389 my ($ipa,$subn) = split("/",$z);
390 $subn=&General
::iporsubtocidr
($subn);
392 $source="$ipa/$subn";
394 print LOG
"-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
397 }elsif ($configline[2] eq 'mac') {
398 if(&General
::validmac
($configline[6])){
400 $source=$configline[6];
402 print LOG
"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
405 }elsif ($configline[2] eq 'all') {
409 foreach my $key (sort keys %groups){
410 if($groups{$key}[0] eq $configline[2]){
411 $grp1='cust_grp_src';
412 $source=$configline[2];
415 if ($grp1 eq '' || $source eq ''){
416 print LOG
"-> Rule not converted, no valid source recognised\n";
419 ############################################################
421 if($configline[7] ne ''){
422 my $address=&check_ip
($configline[7]);
424 my ($dip,$dsub) = split("/",$address);
425 $dsub=&General
::iporsubtocidr
($dsub);
427 $target="$dip/$dsub";
429 my $getwebsiteip=&get_ip_from_domain
($configline[7]);
432 $target=$getwebsiteip;
433 $remark.=" $configline[7]";
435 print LOG
"-> Rule not converted, invalid domain \"$configline[7]\"\n";
443 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
445 my @parts=split(",",$configline[8]);
448 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
449 if(&General
::validport
($_)){
454 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
458 my ($a1,$a2) = split(/\:/,$_);
459 if (&General
::validport
($a1) && &General
::validport
($a2) && $a1 < $a2){
461 push (@values,"$a1:$a2");
464 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
469 $port=join("|",@values);
474 print LOG
"-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
476 &General
::readhasharray
($fwdfwconfig,\
%fwconfig);
477 &General
::readhasharray
($outfwconfig,\
%fwconfigout);
480 foreach my $protocol (@prot){
481 if ($source eq 'IPFire'){
486 $protocol=uc($protocol);
487 print LOG
"-> 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";
488 #Put rules into system....
489 ###########################
490 #check for double rules
491 foreach my $key (sort keys %fwconfig){
492 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"
493 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]"){
499 #increase groupcounter
501 if($grp1 eq 'cust_grp_src'){
502 foreach my $key (sort keys %groups){
503 if($groups{$key}[0] eq $source){
509 &General
::writehasharray
($configgroups,\
%groups);
512 if ($chain eq 'FORWARDFW'){
513 my $key = &General
::findhasharraykey
(\
%fwconfig);
514 $fwconfig{$key}[0] = $action;
515 $fwconfig{$key}[1] = $chain;
516 $fwconfig{$key}[2] = $active;
517 $fwconfig{$key}[3] = $grp1;
518 $fwconfig{$key}[4] = $source;
519 $fwconfig{$key}[5] = $grp2;
520 $fwconfig{$key}[6] = $target;
521 $fwconfig{$key}[11] = $useport;
522 $fwconfig{$key}[12] = $protocol;
523 $fwconfig{$key}[14] = $grp3;
524 $fwconfig{$key}[15] = $port;
525 $fwconfig{$key}[16] = $remark;
526 $fwconfig{$key}[17] = $log;
527 $fwconfig{$key}[18] = $time;
528 $fwconfig{$key}[19] = $time_mon;
529 $fwconfig{$key}[20] = $time_tue;
530 $fwconfig{$key}[21] = $time_wed;
531 $fwconfig{$key}[22] = $time_thu;
532 $fwconfig{$key}[23] = $time_fri;
533 $fwconfig{$key}[24] = $time_sat;
534 $fwconfig{$key}[25] = $time_sun;
535 $fwconfig{$key}[26] = $time_from;
536 $fwconfig{$key}[27] = $time_to;
538 my $key = &General
::findhasharraykey
(\
%fwconfigout);
539 $fwconfigout{$key}[0] = $action;
540 $fwconfigout{$key}[1] = $chain;
541 $fwconfigout{$key}[2] = $active;
542 $fwconfigout{$key}[3] = $grp1;
543 $fwconfigout{$key}[4] = $source;
544 $fwconfigout{$key}[5] = $grp2;
545 $fwconfigout{$key}[6] = $target;
546 $fwconfigout{$key}[11] = $useport;
547 $fwconfigout{$key}[12] = $protocol;
548 $fwconfigout{$key}[14] = $grp3;
549 $fwconfigout{$key}[15] = $port;
550 $fwconfigout{$key}[16] = $remark;
551 $fwconfigout{$key}[17] = $log;
552 $fwconfigout{$key}[18] = $time;
553 $fwconfigout{$key}[19] = $time_mon;
554 $fwconfigout{$key}[20] = $time_tue;
555 $fwconfigout{$key}[21] = $time_wed;
556 $fwconfigout{$key}[22] = $time_thu;
557 $fwconfigout{$key}[23] = $time_fri;
558 $fwconfigout{$key}[24] = $time_sat;
559 $fwconfigout{$key}[25] = $time_sun;
560 $fwconfigout{$key}[26] = $time_from;
561 $fwconfigout{$key}[27] = $time_to;
563 &General
::writehasharray
($fwdfwconfig,\
%fwconfig);
564 &General
::writehasharray
($outfwconfig,\
%fwconfigout);
572 sub get_ip_from_domain
577 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
579 $resolvedip=inet_ntoa
($addrs[0]);
586 &General
::readhasharray
($confighosts,\
%hosts);
587 &General
::readhasharray
($confignets,\
%nets);
588 &General
::readhasharray
($configgroups,\
%groups);
589 &General
::readhasharray
($ovpnconfig,\
%configovpn);
590 &General
::readhasharray
($ccdconfig,\
%ccdconf);
591 &General
::readhash
($ovpnsettings,\
%settingsovpn);
594 if($settingsovpn{'DOVPN_SUBNET'}){
595 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
596 push (@ovpnnets,"$net,$subnet,dynamic");
598 foreach my $key (sort keys %ccdconf){
599 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
600 $subnet=&General
::iporsubtodec
($subnet);
601 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
603 foreach my $key (sort keys %configovpn){
604 if ($configovpn{$key}[3] eq 'net'){
605 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
606 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
609 #add ovpn nets to customnetworks/groups
610 foreach my $line (@ovpnnets){
611 my ($net,$subnet,$name) = split(",",$line);
612 if (!&check_net
($net,$subnet)){
613 my $netkey = &General
::findhasharraykey
(\
%nets);
614 $name2=$name."(ovpn)".$net;
615 $name3="Custom Network";
616 $nets{$netkey}[0] = $name2;
617 $nets{$netkey}[1] = $net;
618 $nets{$netkey}[2] = $subnet;
619 $nets{$netkey}[3] = 1;
621 print LOG
"-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
624 my $grpkey = &General
::findhasharraykey
(\
%groups);
625 $groups{$grpkey}[0] = "ovpn";
626 $groups{$grpkey}[1] = '';
627 $groups{$grpkey}[2] = $name2;
628 $groups{$grpkey}[3] = "Custom Network";
629 $groups{$grpkey}[4] = 0;
634 &General
::writehasharray
($confighosts,\
%hosts);
635 &General
::writehasharray
($configgroups,\
%groups);
636 &General
::writehasharray
($confignets,\
%nets);
640 copy
("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
641 chmod oct('0777'), '/var/ipfire/forward/p2protocols';