]>
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 $fwdfwsettings = "${General::swroot}/forward/settings";
23 my @ipgroups = qx(ls
$ipgrouppath);
24 my @macgroups = qx(ls
$macgrouppath);
35 &General
::readhash
($outfwsettings,\
%outsettings);
40 system("/usr/local/bin/forwardfwctrl");
43 if(! -d
"/var/log/converters"){ mkdir("/var/log/converters");}
44 if( -f
"/var/log/converters/groups-convert.log"){rmtree
("var/log/converters");}
45 open (LOG
, ">/var/log/converters/groups-convert.log") or die $!;
47 foreach my $group (@ipgroups){
49 print LOG
"\nProcessing IP-GROUP: $group...\n";
50 open (DATEI
, "<$ipgrouppath/$group");
52 foreach my $ip (@zeilen){
55 print LOG
"Check IP $ip from Group $group ";
56 my $val=&check_ip
($ip);
58 push(@hostarray,$val.",ip");
62 print LOG
"-> IP \"$ip\" from group $group not converted (invalid IP) \n";
66 &new_hostgrp
($group,'ip');
73 foreach my $group (@macgroups){
75 print LOG
"\nProcessing MAC-GROUP: $group...\n";
76 open (DATEI
, "<$macgrouppath/$group");
78 foreach my $mac (@zeilen){
81 print LOG
"Checking MAC $mac from group $group ";
83 if(&General
::validmac
($mac)){
87 push(@hostarray,$val.",mac");
91 print LOG
"-> Mac $mac from group $group not converted (invalid MAC)\n";
95 &new_hostgrp
($group,'mac');
104 #ip with subnet in decimal
105 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d
{1,2})$/){
106 $adr=int($1).".".int($2).".".int($3).".".int($4);
107 my $b = &General
::iporsubtodec
($5);
109 }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
110 $adr=int($1).".".int($2).".".int($3).".".int($4);
111 if(&General
::validip
($adr)){
112 $a=$adr."/255.255.255.255";
115 if(&General
::validipandmask
($adr)){
116 $a=&General
::iporsubtodec
($adr);
122 &General
::readhasharray
($confighosts,\
%hosts);
123 &General
::readhasharray
($confignets,\
%nets);
124 &General
::readhasharray
($configgroups,\
%groups);
127 my $name; #"converted"
129 my $name3; #custom host/custom net
130 foreach my $adr (@hostarray){
132 my ($ip,$type) = split(",",$adr);
133 my ($ippart,$subnet) = split("/",$ip);
134 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
136 print LOG
"Processing SINGLE HOST $ippart/$subnet from group $grp\n";
137 if(!&check_host
($ip)){
138 my $key = &General
::findhasharraykey
(\
%hosts);
140 $name2=$name.$ippart;
141 $name3="Custom Host";
142 $hosts{$key}[0] = $name2;
143 $hosts{$key}[1] = $type;
144 $hosts{$key}[2] = $ip;
146 print LOG
"->Host (IP) $ip added to custom hosts\n"
148 print LOG
"->Host (IP) $ip already exists\n";
150 }elsif($byte4 < '255'){
151 print LOG
"Processing NETWORK $ippart/$subnet from Group $grp\n";
152 if(!&check_net
($ippart,$subnet)){
153 my $netkey = &General
::findhasharraykey
(\
%nets);
155 $name2=$name.$ippart;
156 $name3="Custom Network";
157 $nets{$netkey}[0] = $name2;
158 $nets{$netkey}[1] = $ippart;
159 $nets{$netkey}[2] = $subnet;
160 $nets{$netkey}[3] = 1;
161 print LOG
"->Network $ippart/$subnet added to custom networks\n";
163 print LOG
"Network $ippart already exists\n";
167 my $grpkey = &General
::findhasharraykey
(\
%groups);
168 $groups{$grpkey}[0] = $grp;
169 $groups{$grpkey}[1] = '';
170 $groups{$grpkey}[2] = $name2;
171 $groups{$grpkey}[3] = $name3;
172 $groups{$grpkey}[4] = 0;
173 print LOG
"->$name2 added to group $grp\n";
175 }elsif($run eq 'mac'){
177 my ($mac,$type) = split(",",$adr);
178 print LOG
"Processing HOST (MAC) $mac\n";
179 if(!&check_host
($mac)){
180 my $key = &General
::findhasharraykey
(\
%hosts);
183 $name3="Custom Host";
184 $hosts{$key}[0] = $name2;
185 $hosts{$key}[1] = $type;
186 $hosts{$key}[2] = $mac;
188 print LOG
"->Host (MAC) $mac added to custom hosts\n";
190 print LOG
"->Host (MAC) $mac already exists\n";
193 my $grpkey = &General
::findhasharraykey
(\
%groups);
194 $groups{$grpkey}[0] = $grp;
195 $groups{$grpkey}[1] = '';
196 $groups{$grpkey}[2] = $name2;
197 $groups{$grpkey}[3] = $name3;
198 $groups{$grpkey}[4] = 0;
199 print LOG
"->$name2 added to group $grp\n";
204 &General
::writehasharray
($confighosts,\
%hosts);
205 &General
::writehasharray
($configgroups,\
%groups);
206 &General
::writehasharray
($confignets,\
%nets);
212 foreach my $key (sort keys %hosts)
214 if($hosts{$key}[2] eq $ip)
225 foreach my $key (sort keys %nets)
227 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
238 foreach my $key (sort keys %groups)
240 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
249 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
250 if($fwdsettings{'POLICY'} ne $outsettings{'POLICY'}){
251 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
252 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
255 if( -f
"/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
256 open (LOG
, ">/var/log/converters/outgoingfw-convert.log") or die $!;
258 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);
259 if ($outsettings{'POLICY'} eq 'MODE1'){
262 }elsif($outsettings{'POLICY'} eq 'MODE2'){
268 open (DATEI
, "<$outgoingrules");
270 foreach my $rule (@lines)
274 print LOG
"processing: $rule\n";
276 @configline = split( /\;/, $rule );
278 if($configline[0] eq $type){
279 #some variables we can use from old config
280 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
281 if($configline[3] eq 'all' && $configline[8] ne ''){
284 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
287 push(@prot,$configline[3]);
290 if($configline[4] ne ''){
291 $configline[4] =~ s/,/;/g;
292 $remark = $configline[4];
294 if($configline[9] eq 'Active'){ $log='ON';}else{$log='';}
295 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'){
296 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
304 $time_mon=$configline[10];
305 $time_tue=$configline[11];
306 $time_wed=$configline[12];
307 $time_thu=$configline[13];
308 $time_fri=$configline[14];
309 $time_sat=$configline[15];
310 $time_sun=$configline[16];
311 $time_from=$configline[17];
312 $time_to=$configline[18];
313 ############################################################
315 if ($configline[2] eq 'green') {
318 }elsif ($configline[2] eq 'orange') {
321 }elsif ($configline[2] eq 'red') {
322 print LOG
"-> Rule not converted, is an outgoing rule.Please put it manually in /etc/sysconfig/firewall.local \n";
324 }elsif ($configline[2] eq 'blue') {
327 }elsif ($configline[2] eq 'ipsec') {
328 print LOG
"-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
330 }elsif ($configline[2] eq 'ovpn') {
332 $grp1='cust_grp_src';
334 }elsif ($configline[2] eq 'ip') {
335 my $z=&check_ip
($configline[5]);
337 my ($ipa,$subn) = split("/",$z);
338 $subn=&General
::iporsubtocidr
($subn);
340 $source="$ipa/$subn";
342 print LOG
"-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
345 }elsif ($configline[2] eq 'mac') {
346 if(&General
::validmac
($configline[6])){
348 $source=$configline[6];
350 print LOG
"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
353 }elsif ($configline[2] eq 'all') {
357 #&General::readhasharray($configgroups,\%groups);
358 foreach my $key (sort keys %groups){
359 if($groups{$key}[0] eq $configline[2]){
360 $grp1='cust_grp_src';
361 $source=$configline[2];
364 if ($grp1 eq '' || $source eq ''){
365 print LOG
"-> Rule not converted, no valid source recognised\n";
368 ############################################################
370 if($configline[7] ne ''){
371 my $address=&check_ip
($configline[7]);
373 my ($dip,$dsub) = split("/",$address);
374 $dsub=&General
::iporsubtocidr
($dsub);
376 $target="$dip/$dsub";
378 my $getwebsiteip=&get_ip_from_domain
($configline[7]);
381 $target=$getwebsiteip;
382 $remark.=" $configline[7]";
384 print LOG
"-> Rule not converted, invalid domain \"$configline[7]\"\n";
392 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
394 my @parts=split(",",$configline[8]);
396 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
397 if(&General
::validport
($_)){
402 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
406 my ($a1,$a2) = split(/\:/,$_);
407 if (&General
::validport
($a1) && &General
::validport
($a2) && $a1 < $a2){
409 push (@values,"$a1:$a2");
412 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
417 $port=join("|",@values);
422 print LOG
"-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
424 &General
::readhasharray
($fwdfwconfig,\
%fwconfig);
426 foreach my $protocol (@prot){
427 $protocol=uc($protocol);
428 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";
429 #Put rules into system....
430 ###########################
432 #check for double rules
433 foreach my $key (sort keys %fwconfig){
434 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"
435 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]"){
441 #increase groupcounter
443 if($grp1 eq 'cust_grp_src'){
444 foreach my $key (sort keys %groups){
445 if($groups{$key}[0] eq $source){
451 &General
::writehasharray
($configgroups,\
%groups);
454 my $key = &General
::findhasharraykey
(\
%fwconfig);
455 $fwconfig{$key}[0] = $action;
456 $fwconfig{$key}[1] = "FORWARDFW";
457 $fwconfig{$key}[2] = $active;
458 $fwconfig{$key}[3] = $grp1;
459 $fwconfig{$key}[4] = $source;
460 $fwconfig{$key}[5] = $grp2;
461 $fwconfig{$key}[6] = $target;
462 $fwconfig{$key}[11] = $useport;
463 $fwconfig{$key}[12] = $protocol;
464 $fwconfig{$key}[14] = $grp3;
465 $fwconfig{$key}[15] = $port;
466 $fwconfig{$key}[16] = $remark;
467 $fwconfig{$key}[17] = $log;
468 $fwconfig{$key}[18] = $time;
469 $fwconfig{$key}[19] = $time_mon;
470 $fwconfig{$key}[20] = $time_tue;
471 $fwconfig{$key}[21] = $time_wed;
472 $fwconfig{$key}[22] = $time_thu;
473 $fwconfig{$key}[23] = $time_fri;
474 $fwconfig{$key}[24] = $time_sat;
475 $fwconfig{$key}[25] = $time_sun;
476 $fwconfig{$key}[26] = $time_from;
477 $fwconfig{$key}[27] = $time_to;
480 &General
::writehasharray
($fwdfwconfig,\
%fwconfig);
486 sub get_ip_from_domain
491 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
493 $resolvedip=inet_ntoa
($addrs[0]);
500 &General
::readhasharray
($confighosts,\
%hosts);
501 &General
::readhasharray
($confignets,\
%nets);
502 &General
::readhasharray
($configgroups,\
%groups);
503 &General
::readhasharray
($ovpnconfig,\
%configovpn);
504 &General
::readhasharray
($ccdconfig,\
%ccdconf);
505 &General
::readhash
($ovpnsettings,\
%settingsovpn);
508 if($settingsovpn{'DOVPN_SUBNET'}){
509 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
510 push (@ovpnnets,"$net,$subnet,dynamic");
512 foreach my $key (sort keys %ccdconf){
513 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
514 $subnet=&General
::iporsubtodec
($subnet);
515 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
517 foreach my $key (sort keys %configovpn){
518 if ($configovpn{$key}[3] eq 'net'){
519 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
520 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
523 #add ovpn nets to customnetworks/groups
524 foreach my $line (@ovpnnets){
525 my ($net,$subnet,$name) = split(",",$line);
526 if (!&check_net
($net,$subnet)){
527 my $netkey = &General
::findhasharraykey
(\
%nets);
528 $name2=$name."(ovpn)".$net;
529 $name3="Custom Network";
530 $nets{$netkey}[0] = $name2;
531 $nets{$netkey}[1] = $net;
532 $nets{$netkey}[2] = $subnet;
533 $nets{$netkey}[3] = 1;
535 print LOG
"-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
538 my $grpkey = &General
::findhasharraykey
(\
%groups);
539 $groups{$grpkey}[0] = "ovpn";
540 $groups{$grpkey}[1] = '';
541 $groups{$grpkey}[2] = $name2;
542 $groups{$grpkey}[3] = "Custom Network";
543 $groups{$grpkey}[4] = 0;
548 &General
::writehasharray
($confighosts,\
%hosts);
549 &General
::writehasharray
($configgroups,\
%groups);
550 &General
::writehasharray
($confignets,\
%nets);
554 copy
("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
555 chmod oct('0777'), '/var/ipfire/forward/p2protocols';