]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/forwardfw/convert-outgoingfw
Forward Firewall: extended converter
[people/teissler/ipfire-2.x.git] / config / forwardfw / convert-outgoingfw
CommitLineData
27f4a6b1
AM
1#!/usr/bin/perl
2
3
4require '/var/ipfire/general-functions.pl';
5a9fd5db 5
27f4a6b1 6use Socket;
8f0b047b 7use File::Path;
5a9fd5db
AM
8use File::Copy;
9
27f4a6b1
AM
10my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/";
11my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/";
12my $outgoingrules = "${General::swroot}/outgoing/rules";
13my $outfwsettings = "${General::swroot}/outgoing/settings";
14my $host = "Converted ";
15my $confighosts = "${General::swroot}/fwhosts/customhosts";
16my $confignets = "${General::swroot}/fwhosts/customnetworks";
17my $configgroups = "${General::swroot}/fwhosts/customgroups";
18my $ovpnsettings = "${General::swroot}/ovpn/settings";
19my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig";
20my $ccdconfig = "${General::swroot}/ovpn/ccd.conf";
21my $fwdfwconfig = "${General::swroot}/forward/config";
22my $fwdfwsettings = "${General::swroot}/forward/settings";
23my @ipgroups = qx(ls $ipgrouppath);
24my @macgroups = qx(ls $macgrouppath);
25my @hostarray=();
26my %outsettings=();
27my %hosts=();
28my %nets=();
29my %groups=();
30my %settingsovpn=();
31my %configovpn=();
32my %ccdconf=();
33my %fwconf=();
34my %fwdsettings=();
35&General::readhash($outfwsettings,\%outsettings);
36
27f4a6b1
AM
37&process_groups;
38&process_rules;
5a9fd5db 39&process_p2p;
27f4a6b1 40
27f4a6b1
AM
41sub process_groups
42{
2833f567 43 if(! -d "/var/log/converters"){ mkdir("/var/log/converters");}
8f0b047b 44 if( -f "/var/log/converters/groups-convert.log"){rmtree("var/log/converters");}
2833f567 45 open (LOG, ">/var/log/converters/groups-convert.log") or die $!;
27f4a6b1
AM
46 #IP Group processing
47 foreach my $group (@ipgroups){
48 chomp $group;
5a9fd5db 49 print LOG "\nProcessing IP-GROUP: $group...\n";
27f4a6b1
AM
50 open (DATEI, "<$ipgrouppath/$group");
51 my @zeilen = <DATEI>;
52 foreach my $ip (@zeilen){
53 chomp($ip);
54 $ip =~ s/\s//gi;
5a9fd5db 55 print LOG "Check IP $ip from Group $group ";
27f4a6b1
AM
56 my $val=&check_ip($ip);
57 if($val){
58 push(@hostarray,$val.",ip");
5a9fd5db 59 print LOG "-> OK\n";
27f4a6b1
AM
60 }
61 else{
62 print LOG "-> IP \"$ip\" from group $group not converted (invalid IP) \n";
63 }
64 $val='';
65 }
66 &new_hostgrp($group,'ip');
67 @hostarray=();
68 }
69 $group='';
70 @zeilen=();
71 @hostarray=();
72 #MAC Group processing
73 foreach my $group (@macgroups){
74 chomp $group;
5a9fd5db 75 print LOG "\nProcessing MAC-GROUP: $group...\n";
27f4a6b1
AM
76 open (DATEI, "<$macgrouppath/$group");
77 my @zeilen = <DATEI>;
78 foreach my $mac (@zeilen){
79 chomp($mac);
80 $mac =~ s/\s//gi;
5a9fd5db 81 print LOG "Checking MAC $mac from group $group ";
27f4a6b1
AM
82 #MAC checking
83 if(&General::validmac($mac)){
84 $val=$mac;
85 }
86 if($val){
87 push(@hostarray,$val.",mac");
5a9fd5db 88 print LOG "-> OK\n";
27f4a6b1
AM
89 }
90 else{
91 print LOG "-> Mac $mac from group $group not converted (invalid MAC)\n";
92 }
93 $val='';
94 }
95 &new_hostgrp($group,'mac');
96 @hostarray=();
97 }
98 close (LOG);
99}
100sub check_ip
101{
102 my $adr=shift;
103 my $a;
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);
108 $a=$adr."/".$b;
109 }
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";
114 }
115 }
116 if(&General::validipandmask($adr)){
117 $a=&General::iporsubtodec($adr);
118 }
119 return $a;
120}
121sub new_hostgrp
122{
123 &General::readhasharray($confighosts,\%hosts);
124 &General::readhasharray($confignets,\%nets);
125 &General::readhasharray($configgroups,\%groups);
126 my $grp=shift;
127 my $run=shift;
128 my $name; #"converted"
129 my $name2;
130 my $name3; #custom host/custom net
131 foreach my $adr (@hostarray){
132 if($run eq 'ip'){
133 my ($ip,$type) = split(",",$adr);
134 my ($ippart,$subnet) = split("/",$ip);
135 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
5a9fd5db
AM
136 if($byte4 eq '255'){
137 print LOG "Processing SINGLE HOST $ippart/$subnet from group $grp\n";
27f4a6b1
AM
138 if(!&check_host($ip)){
139 my $key = &General::findhasharraykey(\%hosts);
140 $name="host ";
141 $name2=$name.$ippart;
142 $name3="Custom Host";
143 $hosts{$key}[0] = $name2;
144 $hosts{$key}[1] = $type;
145 $hosts{$key}[2] = $ip;
146 $hosts{$key}[3] = 1;
5a9fd5db 147 print LOG "->Host (IP) $ip added to custom hosts\n"
27f4a6b1 148 }else{
5a9fd5db 149 print LOG "->Host (IP) $ip already exists\n";
27f4a6b1
AM
150 }
151 }elsif($byte4 < '255'){
5a9fd5db 152 print LOG "Processing NETWORK $ippart/$subnet from Group $grp\n";
27f4a6b1
AM
153 if(!&check_net($ippart,$subnet)){
154 my $netkey = &General::findhasharraykey(\%nets);
155 $name="net ";
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;
5a9fd5db 162 print LOG "->Network $ippart/$subnet added to custom networks\n";
27f4a6b1
AM
163 }else{
164 print LOG "Network $ippart already exists\n";
165 }
166 }
167 if($name2){
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;
5a9fd5db 174 print LOG "->$name2 added to group $grp\n";
27f4a6b1
AM
175 }
176 }elsif($run eq 'mac'){
177 #MACRUN
178 my ($mac,$type) = split(",",$adr);
5a9fd5db 179 print LOG "Processing HOST (MAC) $mac\n";
27f4a6b1
AM
180 if(!&check_host($mac)){
181 my $key = &General::findhasharraykey(\%hosts);
182 $name="host ";
183 $name2=$name.$mac;
184 $name3="Custom Host";
185 $hosts{$key}[0] = $name2;
186 $hosts{$key}[1] = $type;
187 $hosts{$key}[2] = $mac;
188 $hosts{$key}[3] = 1;
5a9fd5db 189 print LOG "->Host (MAC) $mac added to custom hosts\n";
27f4a6b1 190 }else{
5a9fd5db 191 print LOG "->Host (MAC) $mac already exists\n";
27f4a6b1
AM
192 }
193 if($name2){
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;
5a9fd5db 200 print LOG "->$name2 added to group $grp\n";
27f4a6b1
AM
201 }
202 }
203 }
5a9fd5db 204 @hostarray=();
27f4a6b1
AM
205 &General::writehasharray($confighosts,\%hosts);
206 &General::writehasharray($configgroups,\%groups);
207 &General::writehasharray($confignets,\%nets);
99e698d0 208
27f4a6b1
AM
209}
210sub check_host
211{
212 my $ip=shift;
213 foreach my $key (sort keys %hosts)
214 {
215 if($hosts{$key}[2] eq $ip)
216 {
217 return 1;
218 }
219 }
220 return 0;
221}
222sub check_net
223{
224 my $ip=shift;
225 my $sub=shift;
226 foreach my $key (sort keys %nets)
227 {
228 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
229 {
230 return 1;
231 }
232 }
233 return 0;
234}
235sub check_grp
236{
237 my $grp=shift;
238 my $value=shift;
239 foreach my $key (sort keys %groups)
240 {
241 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
242 {
243 return 1;
244 }
245 }
246 return 0;
247}
248sub process_rules
249{
27f4a6b1
AM
250 &General::readhash($fwdfwsettings,\%fwdsettings);
251 if($fwdsettings{'POLICY'} ne $outsettings{'POLICY'}){
252 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
253 &General::writehash($fwdfwsettings,\%fwdsettings);
254 }
255 #open LOG
2833f567
AM
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 $!;
27f4a6b1
AM
258
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'){
261 $type='ALLOW';
262 $action='ACCEPT';
263 }elsif($outsettings{'POLICY'} eq 'MODE2'){
264 $type='DENY';
265 $action='DROP';
266 }else{
267 return;
268 }
269 open (DATEI, "<$outgoingrules");
270 my @lines = <DATEI>;
271 foreach my $rule (@lines)
272 {
273 chomp($rule);
99e698d0 274 $port='';
27f4a6b1 275 print LOG "processing: $rule\n";
99e698d0
AM
276 my @configline=();
277 @configline = split( /\;/, $rule );
27f4a6b1
AM
278 my @prot=();
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'){
5a9fd5db 283 push(@prot,"");
27f4a6b1
AM
284 $useport='ON';
285 $grp3='TGT_PORT';
27f4a6b1
AM
286 }else{
287 push(@prot,$configline[3]);
288 $useport='ON';
289 $grp3='TGT_PORT';
290 }
291 if($configline[4] ne ''){ $remark=$configline[4];}else{$remark='';}
292 if($configline[9] eq 'aktiv'){ $log='ON';}else{$log='';}
27f4a6b1
AM
293 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'){
294 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
295 $time='';
296 }else{
297 $time='ON';
298 }
299 }else{
300 $time='ON';
301 }
302 $time_mon=$configline[10];
303 $time_tue=$configline[11];
304 $time_wed=$configline[12];
305 $time_thu=$configline[13];
306 $time_fri=$configline[14];
307 $time_sat=$configline[15];
308 $time_sun=$configline[16];
309 $time_from=$configline[17];
310 $time_to=$configline[18];
311 ############################################################
312 #sourcepart
313 if ($configline[2] eq 'green') {
314 $grp1='std_net_src';
315 $source='GREEN';
316 }elsif ($configline[2] eq 'orange') {
317 $grp1='std_net_src';
318 $source='ORANGE';
319 }elsif ($configline[2] eq 'red') {
320 print LOG "-> Rule not converted, is an outgoing rule.Please put it manually in /etc/sysconfig/firewall.local \n";
321 next;
322 }elsif ($configline[2] eq 'blue') {
323 $grp1='std_net_src';
324 $source='BLUE';
325 }elsif ($configline[2] eq 'ipsec') {
326 print LOG "-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
327 next;
328 }elsif ($configline[2] eq 'ovpn') {
99e698d0
AM
329 &build_ovpn_grp;
330 $grp1='cust_grp_src';
331 $source='ovpn'
27f4a6b1
AM
332 }elsif ($configline[2] eq 'ip') {
333 my $z=&check_ip($configline[5]);
334 if($z){
335 $grp1='src_addr';
336 $source=$z;
337 }else{
338 print LOG "-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
339 next;
340 }
341 }elsif ($configline[2] eq 'mac') {
342 if(&General::validmac($configline[6])){
343 $grp1='src_addr';
344 $source=$configline[6];
345 }else{
346 print LOG"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
347 next;
348 }
349 }elsif ($configline[2] eq 'all') {
350 $grp1='std_net_src';
351 $source='ALL';
352 }else{
353 #&General::readhasharray($configgroups,\%groups);
354 foreach my $key (sort keys %groups){
355 if($groups{$key}[0] eq $configline[2]){
356 $grp1='cust_grp_src';
357 $source=$configline[2];
358 }
359 }
360 if ($grp1 eq '' || $source eq ''){
361 print LOG "-> Rule not converted, no valid source recognised\n";
362 }
363 }
364 ############################################################
365 #destinationpart
366 if($configline[7] ne ''){
367 my $address=&check_ip($configline[7]);
368 if($address){
369 $grp2='tgt_addr';
370 $target=$address;
371 }elsif(!$address){
372 my $getwebsiteip=&get_ip_from_domain($configline[7]);
373 if ($getwebsiteip){
374 $grp2='tgt_addr';
375 $target=$getwebsiteip;
5a9fd5db 376 $remark.=" $configline[7]";
27f4a6b1
AM
377 }else{
378 print LOG "-> Rule not converted, invalid domain \"$configline[7]\"\n";
379 next;
380 }
381 }
382 }else{
383 $grp2='std_net_tgt';
384 $target='ALL';
385 }
87946296 386 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
8f0b047b
AM
387 my @values=();
388 my @parts=split(",",$configline[8]);
389 foreach (@parts){
8f0b047b
AM
390 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
391 if(&General::validport($_)){
392 $useport='ON';
8f0b047b
AM
393 push (@values,$_);
394 $grp3='TGT_PORT';
395 }else{
396 print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
397 next;
398 }
27f4a6b1 399 }else{
8b3dd791
AM
400 my ($a1,$a2) = split(/\:/,$_);
401 if (&General::validport($a1) && &General::validport($a2) && $a1 < $a2){
8f0b047b 402 $useport='ON';
8f0b047b
AM
403 push (@values,"$a1:$a2");
404 $grp3='TGT_PORT';
8b3dd791 405 }else{
8f0b047b
AM
406 print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
407 next;
8b3dd791 408 }
8f0b047b 409 }
27f4a6b1 410 }
8b3dd791
AM
411 $port=join("|",@values);
412 @values=();
99e698d0 413 @parts=();
27f4a6b1
AM
414 }
415 }else{
416 print LOG "-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
417 }
418 &General::readhasharray($fwdfwconfig,\%fwconfig);
419 my $check;
420 foreach my $protocol (@prot){
421 $protocol=uc($protocol);
27f4a6b1
AM
422 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";
423 #Put rules into system....
424 ###########################
425
426 #check for double rules
427 foreach my $key (sort keys %fwconfig){
428 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"
429 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]"){
430 $check='on';
431 next;
432 }
433 }
434 if($check ne 'on'){
435 #increase groupcounter
436 my $check1;
437 if($grp1 eq 'cust_grp_src'){
438 foreach my $key (sort keys %groups){
439 if($groups{$key}[0] eq $source){
440 $groups{$key}[4]++;
441 $check1='on';
442 }
443 }
444 if($check1 eq 'on'){
27f4a6b1
AM
445 &General::writehasharray($configgroups,\%groups);
446 }
447 }
27f4a6b1
AM
448 my $key = &General::findhasharraykey(\%fwconfig);
449 $fwconfig{$key}[0] = $action;
450 $fwconfig{$key}[1] = "FORWARDFW";
451 $fwconfig{$key}[2] = $active;
452 $fwconfig{$key}[3] = $grp1;
453 $fwconfig{$key}[4] = $source;
454 $fwconfig{$key}[5] = $grp2;
455 $fwconfig{$key}[6] = $target;
456 $fwconfig{$key}[11] = $useport;
457 $fwconfig{$key}[12] = $protocol;
458 $fwconfig{$key}[14] = $grp3;
459 $fwconfig{$key}[15] = $port;
460 $fwconfig{$key}[16] = $remark;
461 $fwconfig{$key}[17] = $log;
462 $fwconfig{$key}[18] = $time;
463 $fwconfig{$key}[19] = $time_mon;
464 $fwconfig{$key}[20] = $time_tue;
465 $fwconfig{$key}[21] = $time_wed;
466 $fwconfig{$key}[22] = $time_thu;
467 $fwconfig{$key}[23] = $time_fri;
468 $fwconfig{$key}[24] = $time_sat;
469 $fwconfig{$key}[25] = $time_sun;
470 $fwconfig{$key}[26] = $time_from;
471 $fwconfig{$key}[27] = $time_to;
472 }
473 }
474 &General::writehasharray($fwdfwconfig,\%fwconfig);
475 system("/usr/local/bin/forwardfwctrl");
476 @prot=();
477 }
478 close(LOG);
479 @lines=();
480}
481sub get_ip_from_domain
482{
483 $web=shift;
484 my $resolvedip;
485 my $checked;
486 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
487 if(@addrs){
488 $resolvedip=inet_ntoa($addrs[0]);
489 return $resolvedip;
490 }
491 return;
492}
493sub build_ovpn_grp
494{
495 &General::readhasharray($confighosts,\%hosts);
496 &General::readhasharray($confignets,\%nets);
497 &General::readhasharray($configgroups,\%groups);
498 &General::readhasharray($ovpnconfig,\%configovpn);
499 &General::readhasharray($ccdconfig,\%ccdconf);
500 &General::readhash($ovpnsettings,\%settingsovpn);
501 #get ovpn nets
502 my @ovpnnets=();
503 if($settingsovpn{'DOVPN_SUBNET'}){
504 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
505 push (@ovpnnets,"$net,$subnet,dynamic");
506 }
507 foreach my $key (sort keys %ccdconf){
508 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
509 $subnet=&General::iporsubtodec($subnet);
510 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
511 }
512 foreach my $key (sort keys %configovpn){
513 if ($configovpn{$key}[3] eq 'net'){
514 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
515 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
516 }
517 }
518 #add ovpn nets to customnetworks/groups
519 foreach my $line (@ovpnnets){
520 my ($net,$subnet,$name) = split(",",$line);
521 if (!&check_net($net,$subnet)){
522 my $netkey = &General::findhasharraykey(\%nets);
523 $name2=$name."(ovpn)".$net;
524 $name3="Custom Network";
525 $nets{$netkey}[0] = $name2;
526 $nets{$netkey}[1] = $net;
527 $nets{$netkey}[2] = $subnet;
528 $nets{$netkey}[3] = 1;
529 }else{
530 print LOG "-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
531 }
532 if($name2){
533 my $grpkey = &General::findhasharraykey(\%groups);
534 $groups{$grpkey}[0] = "ovpn";
535 $groups{$grpkey}[1] = '';
536 $groups{$grpkey}[2] = $name2;
537 $groups{$grpkey}[3] = "Custom Network";
538 $groups{$grpkey}[4] = 0;
539 }
540 $name2='';
541 }
542 @ovpnnets=();
543 &General::writehasharray($confighosts,\%hosts);
544 &General::writehasharray($configgroups,\%groups);
545 &General::writehasharray($confignets,\%nets);
546}
5a9fd5db
AM
547sub process_p2p
548{
549 copy("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
550 chmod oct(0777), '/var/ipfire/forward/p2protocols';
551}