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