]>
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);
110 if($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
111 $adr=int($1).".".int($2).".".int($3).".".int($4);
112 if(&General
::validip
($adr)){
113 $a=$adr."/255.255.255.255";
116 if(&General
::validipandmask
($adr)){
117 $a=&General
::iporsubtodec
($adr);
123 &General
::readhasharray
($confighosts,\
%hosts);
124 &General
::readhasharray
($confignets,\
%nets);
125 &General
::readhasharray
($configgroups,\
%groups);
128 my $name; #"converted"
130 my $name3; #custom host/custom net
131 foreach my $adr (@hostarray){
133 my ($ip,$type) = split(",",$adr);
134 my ($ippart,$subnet) = split("/",$ip);
135 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
137 print LOG
"Processing SINGLE HOST $ippart/$subnet from group $grp\n";
138 if(!&check_host
($ip)){
139 my $key = &General
::findhasharraykey
(\
%hosts);
141 $name2=$name.$ippart;
142 $name3="Custom Host";
143 $hosts{$key}[0] = $name2;
144 $hosts{$key}[1] = $type;
145 $hosts{$key}[2] = $ip;
147 print LOG
"->Host (IP) $ip added to custom hosts\n"
149 print LOG
"->Host (IP) $ip already exists\n";
151 }elsif($byte4 < '255'){
152 print LOG
"Processing NETWORK $ippart/$subnet from Group $grp\n";
153 if(!&check_net
($ippart,$subnet)){
154 my $netkey = &General
::findhasharraykey
(\
%nets);
156 $name2=$name.$ippart;
157 $name3="Custom Network";
158 $nets{$netkey}[0] = $name2;
159 $nets{$netkey}[1] = $ippart;
160 $nets{$netkey}[2] = $subnet;
161 $nets{$netkey}[3] = 1;
162 print LOG
"->Network $ippart/$subnet added to custom networks\n";
164 print LOG
"Network $ippart already exists\n";
168 my $grpkey = &General
::findhasharraykey
(\
%groups);
169 $groups{$grpkey}[0] = $grp;
170 $groups{$grpkey}[1] = '';
171 $groups{$grpkey}[2] = $name2;
172 $groups{$grpkey}[3] = $name3;
173 $groups{$grpkey}[4] = 0;
174 print LOG
"->$name2 added to group $grp\n";
176 }elsif($run eq 'mac'){
178 my ($mac,$type) = split(",",$adr);
179 print LOG
"Processing HOST (MAC) $mac\n";
180 if(!&check_host
($mac)){
181 my $key = &General
::findhasharraykey
(\
%hosts);
184 $name3="Custom Host";
185 $hosts{$key}[0] = $name2;
186 $hosts{$key}[1] = $type;
187 $hosts{$key}[2] = $mac;
189 print LOG
"->Host (MAC) $mac added to custom hosts\n";
191 print LOG
"->Host (MAC) $mac already exists\n";
194 my $grpkey = &General
::findhasharraykey
(\
%groups);
195 $groups{$grpkey}[0] = $grp;
196 $groups{$grpkey}[1] = '';
197 $groups{$grpkey}[2] = $name2;
198 $groups{$grpkey}[3] = $name3;
199 $groups{$grpkey}[4] = 0;
200 print LOG
"->$name2 added to group $grp\n";
205 &General
::writehasharray
($confighosts,\
%hosts);
206 &General
::writehasharray
($configgroups,\
%groups);
207 &General
::writehasharray
($confignets,\
%nets);
213 foreach my $key (sort keys %hosts)
215 if($hosts{$key}[2] eq $ip)
226 foreach my $key (sort keys %nets)
228 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
239 foreach my $key (sort keys %groups)
241 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
250 &General
::readhash
($fwdfwsettings,\
%fwdsettings);
251 if($fwdsettings{'POLICY'} ne $outsettings{'POLICY'}){
252 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
253 &General
::writehash
($fwdfwsettings,\
%fwdsettings);
256 if( -f
"/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
257 open (LOG
, ">/var/log/converters/outgoingfw-convert.log") or die $!;
259 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);
260 if ($outsettings{'POLICY'} eq 'MODE1'){
263 }elsif($outsettings{'POLICY'} eq 'MODE2'){
269 open (DATEI
, "<$outgoingrules");
271 foreach my $rule (@lines)
275 print LOG
"processing: $rule\n";
277 @configline = split( /\;/, $rule );
279 if($configline[0] eq $type){
280 #some variables we can use from old config
281 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
282 if($configline[3] eq 'all' && $configline[8] ne ''){
285 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
288 push(@prot,$configline[3]);
291 if($configline[4] ne ''){
292 $configline[4] =~ s/,/;/g;
293 $remark = $configline[4];
295 if($configline[9] eq 'Active'){ $log='ON';}else{$log='';}
296 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'){
297 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
305 $time_mon=$configline[10];
306 $time_tue=$configline[11];
307 $time_wed=$configline[12];
308 $time_thu=$configline[13];
309 $time_fri=$configline[14];
310 $time_sat=$configline[15];
311 $time_sun=$configline[16];
312 $time_from=$configline[17];
313 $time_to=$configline[18];
314 ############################################################
316 if ($configline[2] eq 'green') {
319 }elsif ($configline[2] eq 'orange') {
322 }elsif ($configline[2] eq 'red') {
323 print LOG
"-> Rule not converted, is an outgoing rule.Please put it manually in /etc/sysconfig/firewall.local \n";
325 }elsif ($configline[2] eq 'blue') {
328 }elsif ($configline[2] eq 'ipsec') {
329 print LOG
"-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
331 }elsif ($configline[2] eq 'ovpn') {
333 $grp1='cust_grp_src';
335 }elsif ($configline[2] eq 'ip') {
336 my $z=&check_ip
($configline[5]);
338 my ($ipa,$subn) = split("/",$z);
339 $subn=&General
::iporsubtocidr
($subn);
341 $source="$ipa/$subn";
343 print LOG
"-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
346 }elsif ($configline[2] eq 'mac') {
347 if(&General
::validmac
($configline[6])){
349 $source=$configline[6];
351 print LOG
"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
354 }elsif ($configline[2] eq 'all') {
358 #&General::readhasharray($configgroups,\%groups);
359 foreach my $key (sort keys %groups){
360 if($groups{$key}[0] eq $configline[2]){
361 $grp1='cust_grp_src';
362 $source=$configline[2];
365 if ($grp1 eq '' || $source eq ''){
366 print LOG
"-> Rule not converted, no valid source recognised\n";
369 ############################################################
371 if($configline[7] ne ''){
372 my $address=&check_ip
($configline[7]);
374 my ($dip,$dsub) = split("/",$address);
375 $dsub=&General
::iporsubtocidr
($dsub);
377 $target="$dip/$dsub";
379 my $getwebsiteip=&get_ip_from_domain
($configline[7]);
382 $target=$getwebsiteip;
383 $remark.=" $configline[7]";
385 print LOG
"-> Rule not converted, invalid domain \"$configline[7]\"\n";
393 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
395 my @parts=split(",",$configline[8]);
397 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
398 if(&General
::validport
($_)){
403 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
407 my ($a1,$a2) = split(/\:/,$_);
408 if (&General
::validport
($a1) && &General
::validport
($a2) && $a1 < $a2){
410 push (@values,"$a1:$a2");
413 print LOG
"-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
418 $port=join("|",@values);
423 print LOG
"-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
425 &General
::readhasharray
($fwdfwconfig,\
%fwconfig);
427 foreach my $protocol (@prot){
428 $protocol=uc($protocol);
429 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";
430 #Put rules into system....
431 ###########################
433 #check for double rules
434 foreach my $key (sort keys %fwconfig){
435 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"
436 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]"){
442 #increase groupcounter
444 if($grp1 eq 'cust_grp_src'){
445 foreach my $key (sort keys %groups){
446 if($groups{$key}[0] eq $source){
452 &General
::writehasharray
($configgroups,\
%groups);
455 my $key = &General
::findhasharraykey
(\
%fwconfig);
456 $fwconfig{$key}[0] = $action;
457 $fwconfig{$key}[1] = "FORWARDFW";
458 $fwconfig{$key}[2] = $active;
459 $fwconfig{$key}[3] = $grp1;
460 $fwconfig{$key}[4] = $source;
461 $fwconfig{$key}[5] = $grp2;
462 $fwconfig{$key}[6] = $target;
463 $fwconfig{$key}[11] = $useport;
464 $fwconfig{$key}[12] = $protocol;
465 $fwconfig{$key}[14] = $grp3;
466 $fwconfig{$key}[15] = $port;
467 $fwconfig{$key}[16] = $remark;
468 $fwconfig{$key}[17] = $log;
469 $fwconfig{$key}[18] = $time;
470 $fwconfig{$key}[19] = $time_mon;
471 $fwconfig{$key}[20] = $time_tue;
472 $fwconfig{$key}[21] = $time_wed;
473 $fwconfig{$key}[22] = $time_thu;
474 $fwconfig{$key}[23] = $time_fri;
475 $fwconfig{$key}[24] = $time_sat;
476 $fwconfig{$key}[25] = $time_sun;
477 $fwconfig{$key}[26] = $time_from;
478 $fwconfig{$key}[27] = $time_to;
481 &General
::writehasharray
($fwdfwconfig,\
%fwconfig);
487 sub get_ip_from_domain
492 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
494 $resolvedip=inet_ntoa
($addrs[0]);
501 &General
::readhasharray
($confighosts,\
%hosts);
502 &General
::readhasharray
($confignets,\
%nets);
503 &General
::readhasharray
($configgroups,\
%groups);
504 &General
::readhasharray
($ovpnconfig,\
%configovpn);
505 &General
::readhasharray
($ccdconfig,\
%ccdconf);
506 &General
::readhash
($ovpnsettings,\
%settingsovpn);
509 if($settingsovpn{'DOVPN_SUBNET'}){
510 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
511 push (@ovpnnets,"$net,$subnet,dynamic");
513 foreach my $key (sort keys %ccdconf){
514 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
515 $subnet=&General
::iporsubtodec
($subnet);
516 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
518 foreach my $key (sort keys %configovpn){
519 if ($configovpn{$key}[3] eq 'net'){
520 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
521 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
524 #add ovpn nets to customnetworks/groups
525 foreach my $line (@ovpnnets){
526 my ($net,$subnet,$name) = split(",",$line);
527 if (!&check_net
($net,$subnet)){
528 my $netkey = &General
::findhasharraykey
(\
%nets);
529 $name2=$name."(ovpn)".$net;
530 $name3="Custom Network";
531 $nets{$netkey}[0] = $name2;
532 $nets{$netkey}[1] = $net;
533 $nets{$netkey}[2] = $subnet;
534 $nets{$netkey}[3] = 1;
536 print LOG
"-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
539 my $grpkey = &General
::findhasharraykey
(\
%groups);
540 $groups{$grpkey}[0] = "ovpn";
541 $groups{$grpkey}[1] = '';
542 $groups{$grpkey}[2] = $name2;
543 $groups{$grpkey}[3] = "Custom Network";
544 $groups{$grpkey}[4] = 0;
549 &General
::writehasharray
($confighosts,\
%hosts);
550 &General
::writehasharray
($configgroups,\
%groups);
551 &General
::writehasharray
($confignets,\
%nets);
555 copy
("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
556 chmod oct(0777), '/var/ipfire/forward/p2protocols';