]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/convert-outgoingfw
4 require '/var/ipfire/general-functions.pl';
10 my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/";
11 my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/";
12 my $outgoingrules = "${General::swroot}/outgoing/rules";
13 my $outfwsettings = "${General::swroot}/outgoing/settings";
14 my $host = "Converted ";
15 my $confighosts = "${General::swroot}/fwhosts/customhosts";
16 my $confignets = "${General::swroot}/fwhosts/customnetworks";
17 my $configgroups = "${General::swroot}/fwhosts/customgroups";
18 my $ovpnsettings = "${General::swroot}/ovpn/settings";
19 my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig";
20 my $ccdconfig = "${General::swroot}/ovpn/ccd.conf";
21 my $fwdfwconfig = "${General::swroot}/forward/config";
22 my $outfwconfig = "${General::swroot}/forward/outgoing";
23 my $fwdfwsettings = "${General::swroot}/forward/settings";
24 my @ipgroups = qx(ls
$ipgrouppath);
25 my @macgroups = qx(ls
$macgrouppath);
37 &General
::readhash
($outfwsettings,\
%outsettings);
42 system("/usr/local/bin/forwardfwctrl");
45 if(! -d
"/var/log/converters"){ mkdir("/var/log/converters");}
46 if( -f
"/var/log/converters/groups-convert.log"){rmtree
("var/log/converters");}
47 open (LOG
, ">/var/log/converters/groups-convert.log") or die $!;
49 foreach my $group (@ipgroups){
51 print LOG
"\nProcessing IP-GROUP: $group...\n";
52 open (DATEI
, "<$ipgrouppath/$group");
54 foreach my $ip (@zeilen){
57 print LOG
"Check IP $ip from Group $group ";
58 my $val=&check_ip
($ip);
60 push(@hostarray,$val.",ip");
64 print LOG
"-> IP \"$ip\" from group $group not converted (invalid IP) \n";
68 &new_hostgrp
($group,'ip');
75 foreach my $group (@macgroups){
77 print LOG
"\nProcessing MAC-GROUP: $group...\n";
78 open (DATEI
, "<$macgrouppath/$group");
80 foreach my $mac (@zeilen){
83 print LOG
"Checking MAC $mac from group $group ";
85 if(&General
::validmac
($mac)){
89 push(@hostarray,$val.",mac");
93 print LOG
"-> Mac $mac from group $group not converted (invalid MAC)\n";
97 &new_hostgrp
($group,'mac');
107 #ip with subnet in decimal
108 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d
{1,2})$/){
109 $adr=int($1).".".int($2).".".int($3).".".int($4);
110 my $b = &General
::iporsubtodec
($5);
112 }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
113 $adr=int($1).".".int($2).".".int($3).".".int($4);
114 if(&General
::validip
($adr)){
115 $a=$adr."/255.255.255.255";
118 if(&General
::validipandmask
($adr)){
119 $a=&General
::iporsubtodec
($adr);
125 &General
::readhasharray
($confighosts,\
%hosts);
126 &General
::readhasharray
($confignets,\
%nets);
127 &General
::readhasharray
($configgroups,\
%groups);
130 my $name; #"converted"
132 my $name3; #custom host/custom net
133 foreach my $adr (@hostarray){
135 my ($ip,$type) = split(",",$adr);
136 my ($ippart,$subnet) = split("/",$ip);
137 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
139 print LOG
"Processing SINGLE HOST $ippart/$subnet from group $grp\n";
140 if(!&check_host
($ip)){
141 my $key = &General
::findhasharraykey
(\
%hosts);
143 $name2=$name.$ippart;
144 $name3="Custom Host";
145 $hosts{$key}[0] = $name2;
146 $hosts{$key}[1] = $type;
147 $hosts{$key}[2] = $ip;
148 $hosts{$key}[3] = '';
150 print LOG
"->Host (IP) $ip added to custom hosts\n"
152 print LOG
"->Host (IP) $ip already exists in custom hosts\n";
154 $name2=$name.$ippart;
155 $name3="Custom Host";
157 }elsif($byte4 < '255'){
158 print LOG
"Processing NETWORK $ippart/$subnet from Group $grp\n";
159 if(!&check_net
($ippart,$subnet)){
160 my $netkey = &General
::findhasharraykey
(\
%nets);
162 $name2=$name.$ippart;
163 $name3="Custom Network";
164 $nets{$netkey}[0] = $name2;
165 $nets{$netkey}[1] = $ippart;
166 $nets{$netkey}[2] = $subnet;
167 $nets{$netkey}[3] = '';
168 $nets{$netkey}[4] = 1;
169 print LOG
"->Network $ippart/$subnet added to custom networks\n";
171 print LOG
"Network $ippart already exists in custom networks\n";
173 $name2=$name.$ippart;
174 $name3="Custom Network";
177 if($name2 && !&check_grp
($grp,$name2)){
178 my $grpkey = &General
::findhasharraykey
(\
%groups);
179 $groups{$grpkey}[0] = $grp;
180 $groups{$grpkey}[1] = '';
181 $groups{$grpkey}[2] = $name2;
182 $groups{$grpkey}[3] = $name3;
183 $groups{$grpkey}[4] = 0;
184 print LOG
"->$name2 added to group $grp\n";
186 }elsif($run eq 'mac'){
188 my ($mac,$type) = split(",",$adr);
189 print LOG
"Processing HOST (MAC) $mac\n";
190 if(!&check_host
($mac)){
191 my $key = &General
::findhasharraykey
(\
%hosts);
194 $name3="Custom Host";
195 $hosts{$key}[0] = $name2;
196 $hosts{$key}[1] = $type;
197 $hosts{$key}[2] = $mac;
198 $hosts{$key}[3] = '';
200 print LOG
"->Host (MAC) $mac added to custom hosts\n";
202 print LOG
"->Host (MAC) $mac already exists in custom hosts \n";
205 $name3="Custom Host";
207 if($name2 && !&check_grp
($grp,$name2)){
208 my $grpkey = &General
::findhasharraykey
(\
%groups);
209 $groups{$grpkey}[0] = $grp;
210 $groups{$grpkey}[1] = '';
211 $groups{$grpkey}[2] = $name2;
212 $groups{$grpkey}[3] = $name3;
213 $groups{$grpkey}[4] = 0;
214 print LOG
"->$name2 added to group $grp\n";
219 &General
::writehasharray
($confighosts,\
%hosts);
220 &General
::writehasharray
($configgroups,\
%groups);
221 &General
::writehasharray
($confignets,\
%nets);
227 foreach my $key (sort keys %hosts)
229 if($hosts{$key}[2] eq $ip)
240 foreach my $key (sort keys %nets)
242 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
253 foreach my $key (sort keys %groups)
255 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
264 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);
265 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
266 if ($outsettings{'POLICY'} eq 'MODE1'){
267 $fwdfwsettings{'POLICY'}='MODE1';
270 }elsif($outsettings{'POLICY'} eq 'MODE2'){
271 $fwdsettings{'POLICY'}='MODE2';
277 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
280 if( -f
"/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
281 open (LOG
, ">/var/log/converters/outgoingfw-convert.log") or die $!;
282 open (DATEI
, "<$outgoingrules");
284 foreach my $rule (@lines)
288 print LOG
"processing: $rule\n";
290 @configline = split( /\;/, $rule );
292 if($configline[0] eq $type){
293 #some variables we can use from old config
294 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
295 if($configline[3] eq 'all' && $configline[8] ne ''){
298 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
301 push(@prot,$configline[3]);
303 if($configline[4] ne ''){
304 $configline[4] =~ s/,/;/g;
305 $remark = $configline[4];
307 if($configline[9] eq 'Active'){ $log='ON';}else{$log='';}
308 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'){
309 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
317 $time_mon=$configline[10];
318 $time_tue=$configline[11];
319 $time_wed=$configline[12];
320 $time_thu=$configline[13];
321 $time_fri=$configline[14];
322 $time_sat=$configline[15];
323 $time_sun=$configline[16];
324 $time_from=$configline[17];
325 $time_to=$configline[18];
326 ############################################################
328 if ($configline[2] eq 'green') {
331 }elsif ($configline[2] eq 'orange') {
334 }elsif ($configline[2] eq 'red') {
337 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
338 $fwdsettings{'POLICY1'}=$outsettings{'POLICY'};
339 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
340 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
341 }elsif ($configline[2] eq 'blue') {
344 }elsif ($configline[2] eq 'ipsec') {
345 print LOG
"-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
347 }elsif ($configline[2] eq 'ovpn') {
349 $grp1='cust_grp_src';
351 }elsif ($configline[2] eq 'ip') {
352 my $z=&check_ip
($configline[5]);
354 my ($ipa,$subn) = split("/",$z);
355 $subn=&General
::iporsubtocidr
($subn);
357 $source="$ipa/$subn";
359 print LOG
"-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
362 }elsif ($configline[2] eq 'mac') {
363 if(&General
::validmac
($configline[6])){
365 $source=$configline[6];
367 print LOG
"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
370 }elsif ($configline[2] eq 'all') {
374 foreach my $key (sort keys %groups){
375 if($groups{$key}[0] eq $configline[2]){
376 $grp1='cust_grp_src';
377 $source=$configline[2];
380 if ($grp1 eq '' || $source eq ''){
381 print LOG
"-> Rule not converted, no valid source recognised\n";
384 ############################################################
386 if($configline[7] ne ''){
387 my $address=&check_ip
($configline[7]);
389 my ($dip,$dsub) = split("/",$address);
390 $dsub=&General
::iporsubtocidr
($dsub);
392 $target="$dip/$dsub";
394 my $getwebsiteip=&get_ip_from_domain
($configline[7]);
397 $target=$getwebsiteip;
398 $remark.=" $configline[7]";
400 print LOG
"-> Rule not converted, invalid domain \"$configline[7]\"\n";
408 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
410 my @parts=split(",",$configline[8]);
413 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
414 if(&General
::validport
($_)){
419 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
423 my ($a1,$a2) = split(/\:/,$_);
424 if (&General
::validport
($a1) && &General
::validport
($a2) && $a1 < $a2){
426 push (@values,"$a1:$a2");
429 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
434 $port=join("|",@values);
439 print LOG
"-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
441 &General
::readhasharray
($fwdfwconfig,\
%fwconfig);
442 &General
::readhasharray
($outfwconfig,\
%fwconfigout);
445 foreach my $protocol (@prot){
446 if ($source eq 'IPFire'){
451 $protocol=uc($protocol);
452 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";
453 #Put rules into system....
454 ###########################
455 #check for double rules
456 foreach my $key (sort keys %fwconfig){
457 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"
458 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]"){
464 #increase groupcounter
466 if($grp1 eq 'cust_grp_src'){
467 foreach my $key (sort keys %groups){
468 if($groups{$key}[0] eq $source){
474 &General
::writehasharray
($configgroups,\
%groups);
477 if ($chain eq 'FORWARDFW'){
478 my $key = &General
::findhasharraykey
(\
%fwconfig);
479 $fwconfig{$key}[0] = $action;
480 $fwconfig{$key}[1] = $chain;
481 $fwconfig{$key}[2] = $active;
482 $fwconfig{$key}[3] = $grp1;
483 $fwconfig{$key}[4] = $source;
484 $fwconfig{$key}[5] = $grp2;
485 $fwconfig{$key}[6] = $target;
486 $fwconfig{$key}[11] = $useport;
487 $fwconfig{$key}[12] = $protocol;
488 $fwconfig{$key}[14] = $grp3;
489 $fwconfig{$key}[15] = $port;
490 $fwconfig{$key}[16] = $remark;
491 $fwconfig{$key}[17] = $log;
492 $fwconfig{$key}[18] = $time;
493 $fwconfig{$key}[19] = $time_mon;
494 $fwconfig{$key}[20] = $time_tue;
495 $fwconfig{$key}[21] = $time_wed;
496 $fwconfig{$key}[22] = $time_thu;
497 $fwconfig{$key}[23] = $time_fri;
498 $fwconfig{$key}[24] = $time_sat;
499 $fwconfig{$key}[25] = $time_sun;
500 $fwconfig{$key}[26] = $time_from;
501 $fwconfig{$key}[27] = $time_to;
503 my $key = &General
::findhasharraykey
(\
%fwconfigout);
504 $fwconfigout{$key}[0] = $action;
505 $fwconfigout{$key}[1] = $chain;
506 $fwconfigout{$key}[2] = $active;
507 $fwconfigout{$key}[3] = $grp1;
508 $fwconfigout{$key}[4] = $source;
509 $fwconfigout{$key}[5] = $grp2;
510 $fwconfigout{$key}[6] = $target;
511 $fwconfigout{$key}[11] = $useport;
512 $fwconfigout{$key}[12] = $protocol;
513 $fwconfigout{$key}[14] = $grp3;
514 $fwconfigout{$key}[15] = $port;
515 $fwconfigout{$key}[16] = $remark;
516 $fwconfigout{$key}[17] = $log;
517 $fwconfigout{$key}[18] = $time;
518 $fwconfigout{$key}[19] = $time_mon;
519 $fwconfigout{$key}[20] = $time_tue;
520 $fwconfigout{$key}[21] = $time_wed;
521 $fwconfigout{$key}[22] = $time_thu;
522 $fwconfigout{$key}[23] = $time_fri;
523 $fwconfigout{$key}[24] = $time_sat;
524 $fwconfigout{$key}[25] = $time_sun;
525 $fwconfigout{$key}[26] = $time_from;
526 $fwconfigout{$key}[27] = $time_to;
528 &General
::writehasharray
($fwdfwconfig,\
%fwconfig);
529 &General
::writehasharray
($outfwconfig,\
%fwconfigout);
537 sub get_ip_from_domain
542 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
544 $resolvedip=inet_ntoa
($addrs[0]);
551 &General
::readhasharray
($confighosts,\
%hosts);
552 &General
::readhasharray
($confignets,\
%nets);
553 &General
::readhasharray
($configgroups,\
%groups);
554 &General
::readhasharray
($ovpnconfig,\
%configovpn);
555 &General
::readhasharray
($ccdconfig,\
%ccdconf);
556 &General
::readhash
($ovpnsettings,\
%settingsovpn);
559 if($settingsovpn{'DOVPN_SUBNET'}){
560 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
561 push (@ovpnnets,"$net,$subnet,dynamic");
563 foreach my $key (sort keys %ccdconf){
564 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
565 $subnet=&General
::iporsubtodec
($subnet);
566 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
568 foreach my $key (sort keys %configovpn){
569 if ($configovpn{$key}[3] eq 'net'){
570 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
571 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
574 #add ovpn nets to customnetworks/groups
575 foreach my $line (@ovpnnets){
576 my ($net,$subnet,$name) = split(",",$line);
577 if (!&check_net
($net,$subnet)){
578 my $netkey = &General
::findhasharraykey
(\
%nets);
579 $name2=$name."(ovpn)".$net;
580 $name3="Custom Network";
581 $nets{$netkey}[0] = $name2;
582 $nets{$netkey}[1] = $net;
583 $nets{$netkey}[2] = $subnet;
584 $nets{$netkey}[3] = 1;
586 print LOG
"-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
589 my $grpkey = &General
::findhasharraykey
(\
%groups);
590 $groups{$grpkey}[0] = "ovpn";
591 $groups{$grpkey}[1] = '';
592 $groups{$grpkey}[2] = $name2;
593 $groups{$grpkey}[3] = "Custom Network";
594 $groups{$grpkey}[4] = 0;
599 &General
::writehasharray
($confighosts,\
%hosts);
600 &General
::writehasharray
($configgroups,\
%groups);
601 &General
::writehasharray
($confignets,\
%nets);
605 copy
("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
606 chmod oct('0777'), '/var/ipfire/forward/p2protocols';