]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/convert-outgoingfw
4 require '/var/ipfire/general-functions.pl';
7 my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/";
8 my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/";
9 my $outgoingrules = "${General::swroot}/outgoing/rules";
10 my $outfwsettings = "${General::swroot}/outgoing/settings";
11 my $host = "Converted ";
12 my $confighosts = "${General::swroot}/fwhosts/customhosts";
13 my $confignets = "${General::swroot}/fwhosts/customnetworks";
14 my $configgroups = "${General::swroot}/fwhosts/customgroups";
15 my $ovpnsettings = "${General::swroot}/ovpn/settings";
16 my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig";
17 my $ccdconfig = "${General::swroot}/ovpn/ccd.conf";
18 my $fwdfwconfig = "${General::swroot}/forward/config";
19 my $fwdfwsettings = "${General::swroot}/forward/settings";
20 my @ipgroups = qx(ls
$ipgrouppath);
21 my @macgroups = qx(ls
$macgrouppath);
32 &General
::readhash
($outfwsettings,\
%outsettings);
39 if(! -d
"/var/log/converters"){ mkdir("/var/log/converters");}
40 if( -f
"/var/log/converters/groups-convert.log"){rmtree
("var/log/converters");}
41 open (LOG
, ">/var/log/converters/groups-convert.log") or die $!;
43 foreach my $group (@ipgroups){
45 open (DATEI
, "<$ipgrouppath/$group");
47 foreach my $ip (@zeilen){
50 my $val=&check_ip
($ip);
52 push(@hostarray,$val.",ip");
55 print LOG
"-> IP \"$ip\" from group $group not converted (invalid IP) \n";
59 &new_hostgrp
($group,'ip');
66 foreach my $group (@macgroups){
68 open (DATEI
, "<$macgrouppath/$group");
70 foreach my $mac (@zeilen){
74 if(&General
::validmac
($mac)){
78 push(@hostarray,$val.",mac");
81 print LOG
"-> Mac $mac from group $group not converted (invalid MAC)\n";
85 &new_hostgrp
($group,'mac');
94 #ip with subnet in decimal
95 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d
{1,2})$/){
96 $adr=int($1).".".int($2).".".int($3).".".int($4);
97 my $b = &General
::iporsubtodec
($5);
100 if($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
101 $adr=int($1).".".int($2).".".int($3).".".int($4);
102 if(&General
::validip
($adr)){
103 $a=$adr."/255.255.255.255";
106 if(&General
::validipandmask
($adr)){
107 $a=&General
::iporsubtodec
($adr);
113 &General
::readhasharray
($confighosts,\
%hosts);
114 &General
::readhasharray
($confignets,\
%nets);
115 &General
::readhasharray
($configgroups,\
%groups);
118 my $name; #"converted"
120 my $name3; #custom host/custom net
121 foreach my $adr (@hostarray){
123 my ($ip,$type) = split(",",$adr);
124 my ($ippart,$subnet) = split("/",$ip);
125 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
127 if(!&check_host
($ip)){
128 my $key = &General
::findhasharraykey
(\
%hosts);
130 $name2=$name.$ippart;
131 $name3="Custom Host";
132 $hosts{$key}[0] = $name2;
133 $hosts{$key}[1] = $type;
134 $hosts{$key}[2] = $ip;
137 print LOG
"Host (IP) $ip already exists\n";
139 }elsif($byte4 < '255'){
140 if(!&check_net
($ippart,$subnet)){
141 my $netkey = &General
::findhasharraykey
(\
%nets);
143 $name2=$name.$ippart;
144 $name3="Custom Network";
145 $nets{$netkey}[0] = $name2;
146 $nets{$netkey}[1] = $ippart;
147 $nets{$netkey}[2] = $subnet;
148 $nets{$netkey}[3] = 1;
150 print LOG
"Network $ippart already exists\n";
154 my $grpkey = &General
::findhasharraykey
(\
%groups);
155 $groups{$grpkey}[0] = $grp;
156 $groups{$grpkey}[1] = '';
157 $groups{$grpkey}[2] = $name2;
158 $groups{$grpkey}[3] = $name3;
159 $groups{$grpkey}[4] = 0;
161 }elsif($run eq 'mac'){
163 my ($mac,$type) = split(",",$adr);
164 if(!&check_host
($mac)){
165 my $key = &General
::findhasharraykey
(\
%hosts);
168 $name3="Custom Host";
169 $hosts{$key}[0] = $name2;
170 $hosts{$key}[1] = $type;
171 $hosts{$key}[2] = $mac;
174 print LOG
"Host (MAC) $mac already exists\n";
177 my $grpkey = &General
::findhasharraykey
(\
%groups);
178 $groups{$grpkey}[0] = $grp;
179 $groups{$grpkey}[1] = '';
180 $groups{$grpkey}[2] = $name2;
181 $groups{$grpkey}[3] = $name3;
182 $groups{$grpkey}[4] = 0;
187 &General
::writehasharray
($confighosts,\
%hosts);
188 &General
::writehasharray
($configgroups,\
%groups);
189 &General
::writehasharray
($confignets,\
%nets);
195 foreach my $key (sort keys %hosts)
197 if($hosts{$key}[2] eq $ip)
208 foreach my $key (sort keys %nets)
210 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
221 foreach my $key (sort keys %groups)
223 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
232 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
233 if($fwdsettings{'POLICY'} ne $outsettings{'POLICY'}){
234 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
235 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
238 if( -f
"/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
239 open (LOG
, ">/var/log/converters/outgoingfw-convert.log") or die $!;
241 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);
242 if ($outsettings{'POLICY'} eq 'MODE1'){
245 }elsif($outsettings{'POLICY'} eq 'MODE2'){
251 open (DATEI
, "<$outgoingrules");
253 foreach my $rule (@lines)
257 print LOG
"processing: $rule\n";
259 @configline = split( /\;/, $rule );
261 if($configline[0] eq $type){
262 #some variables we can use from old config
263 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
264 if($configline[3] eq 'all'){
270 push(@prot,$configline[3]);
274 if($configline[4] ne ''){ $remark=$configline[4];}else{$remark='';}
275 if($configline[9] eq 'aktiv'){ $log='ON';}else{$log='';}
276 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'){
277 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
285 $time_mon=$configline[10];
286 $time_tue=$configline[11];
287 $time_wed=$configline[12];
288 $time_thu=$configline[13];
289 $time_fri=$configline[14];
290 $time_sat=$configline[15];
291 $time_sun=$configline[16];
292 $time_from=$configline[17];
293 $time_to=$configline[18];
294 ############################################################
296 if ($configline[2] eq 'green') {
299 }elsif ($configline[2] eq 'orange') {
302 }elsif ($configline[2] eq 'red') {
303 print LOG
"-> Rule not converted, is an outgoing rule.Please put it manually in /etc/sysconfig/firewall.local \n";
305 }elsif ($configline[2] eq 'blue') {
308 }elsif ($configline[2] eq 'ipsec') {
309 print LOG
"-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
311 }elsif ($configline[2] eq 'ovpn') {
313 $grp1='cust_grp_src';
315 }elsif ($configline[2] eq 'ip') {
316 my $z=&check_ip
($configline[5]);
321 print LOG
"-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
324 }elsif ($configline[2] eq 'mac') {
325 if(&General
::validmac
($configline[6])){
327 $source=$configline[6];
329 print LOG
"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
332 }elsif ($configline[2] eq 'all') {
336 #&General::readhasharray($configgroups,\%groups);
337 foreach my $key (sort keys %groups){
338 if($groups{$key}[0] eq $configline[2]){
339 $grp1='cust_grp_src';
340 $source=$configline[2];
343 if ($grp1 eq '' || $source eq ''){
344 print LOG
"-> Rule not converted, no valid source recognised\n";
347 ############################################################
349 if($configline[7] ne ''){
350 my $address=&check_ip
($configline[7]);
355 my $getwebsiteip=&get_ip_from_domain
($configline[7]);
358 $target=$getwebsiteip;
360 print LOG
"-> Rule not converted, invalid domain \"$configline[7]\"\n";
368 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
370 my @parts=split(",",$configline[8]);
372 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
373 if(&General
::validport
($_)){
378 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
382 my ($a1,$a2) = split(/\:/,$_);
383 if (&General
::validport
($a1) && &General
::validport
($a2) && $a1 < $a2){
385 push (@values,"$a1:$a2");
388 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
393 $port=join("|",@values);
398 print LOG
"-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
400 &General
::readhasharray
($fwdfwconfig,\
%fwconfig);
402 foreach my $protocol (@prot){
403 $protocol=uc($protocol);
404 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";
405 #Put rules into system....
406 ###########################
408 #check for double rules
409 foreach my $key (sort keys %fwconfig){
410 if("$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"
411 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]"){
417 #increase groupcounter
419 if($grp1 eq 'cust_grp_src'){
420 foreach my $key (sort keys %groups){
421 if($groups{$key}[0] eq $source){
427 &General
::writehasharray
($configgroups,\
%groups);
430 my $key = &General
::findhasharraykey
(\
%fwconfig);
431 $fwconfig{$key}[0] = $action;
432 $fwconfig{$key}[1] = "FORWARDFW";
433 $fwconfig{$key}[2] = $active;
434 $fwconfig{$key}[3] = $grp1;
435 $fwconfig{$key}[4] = $source;
436 $fwconfig{$key}[5] = $grp2;
437 $fwconfig{$key}[6] = $target;
438 $fwconfig{$key}[11] = $useport;
439 $fwconfig{$key}[12] = $protocol;
440 $fwconfig{$key}[14] = $grp3;
441 $fwconfig{$key}[15] = $port;
442 $fwconfig{$key}[16] = $remark;
443 $fwconfig{$key}[17] = $log;
444 $fwconfig{$key}[18] = $time;
445 $fwconfig{$key}[19] = $time_mon;
446 $fwconfig{$key}[20] = $time_tue;
447 $fwconfig{$key}[21] = $time_wed;
448 $fwconfig{$key}[22] = $time_thu;
449 $fwconfig{$key}[23] = $time_fri;
450 $fwconfig{$key}[24] = $time_sat;
451 $fwconfig{$key}[25] = $time_sun;
452 $fwconfig{$key}[26] = $time_from;
453 $fwconfig{$key}[27] = $time_to;
456 &General
::writehasharray
($fwdfwconfig,\
%fwconfig);
457 system("/usr/local/bin/forwardfwctrl");
463 sub get_ip_from_domain
468 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
470 $resolvedip=inet_ntoa
($addrs[0]);
477 &General
::readhasharray
($confighosts,\
%hosts);
478 &General
::readhasharray
($confignets,\
%nets);
479 &General
::readhasharray
($configgroups,\
%groups);
480 &General
::readhasharray
($ovpnconfig,\
%configovpn);
481 &General
::readhasharray
($ccdconfig,\
%ccdconf);
482 &General
::readhash
($ovpnsettings,\
%settingsovpn);
485 if($settingsovpn{'DOVPN_SUBNET'}){
486 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
487 push (@ovpnnets,"$net,$subnet,dynamic");
489 foreach my $key (sort keys %ccdconf){
490 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
491 $subnet=&General
::iporsubtodec
($subnet);
492 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
494 foreach my $key (sort keys %configovpn){
495 if ($configovpn{$key}[3] eq 'net'){
496 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
497 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
500 #add ovpn nets to customnetworks/groups
501 foreach my $line (@ovpnnets){
502 my ($net,$subnet,$name) = split(",",$line);
503 if (!&check_net
($net,$subnet)){
504 my $netkey = &General
::findhasharraykey
(\
%nets);
505 $name2=$name."(ovpn)".$net;
506 $name3="Custom Network";
507 $nets{$netkey}[0] = $name2;
508 $nets{$netkey}[1] = $net;
509 $nets{$netkey}[2] = $subnet;
510 $nets{$netkey}[3] = 1;
512 print LOG
"-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
515 my $grpkey = &General
::findhasharraykey
(\
%groups);
516 $groups{$grpkey}[0] = "ovpn";
517 $groups{$grpkey}[1] = '';
518 $groups{$grpkey}[2] = $name2;
519 $groups{$grpkey}[3] = "Custom Network";
520 $groups{$grpkey}[4] = 0;
525 &General
::writehasharray
($confighosts,\
%hosts);
526 &General
::writehasharray
($configgroups,\
%groups);
527 &General
::writehasharray
($confignets,\
%nets);