]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/convert-outgoingfw
Forward Firewall: minor improvements, if an outgoingrule has a given port and prot...
[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 $fwdfwsettings = "${General::swroot}/forward/settings";
23 my @ipgroups = qx(ls $ipgrouppath);
24 my @macgroups = qx(ls $macgrouppath);
25 my @hostarray=();
26 my %outsettings=();
27 my %hosts=();
28 my %nets=();
29 my %groups=();
30 my %settingsovpn=();
31 my %configovpn=();
32 my %ccdconf=();
33 my %fwconf=();
34 my %fwdsettings=();
35 &General::readhash($outfwsettings,\%outsettings);
36
37 &process_groups;
38 &process_rules;
39 &process_p2p;
40 system("/usr/local/bin/forwardfwctrl");
41 sub process_groups
42 {
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 $!;
46 #IP Group processing
47 foreach my $group (@ipgroups){
48 chomp $group;
49 print LOG "\nProcessing IP-GROUP: $group...\n";
50 open (DATEI, "<$ipgrouppath/$group");
51 my @zeilen = <DATEI>;
52 foreach my $ip (@zeilen){
53 chomp($ip);
54 $ip =~ s/\s//gi;
55 print LOG "Check IP $ip from Group $group ";
56 my $val=&check_ip($ip);
57 if($val){
58 push(@hostarray,$val.",ip");
59 print LOG "-> OK\n";
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;
75 print LOG "\nProcessing MAC-GROUP: $group...\n";
76 open (DATEI, "<$macgrouppath/$group");
77 my @zeilen = <DATEI>;
78 foreach my $mac (@zeilen){
79 chomp($mac);
80 $mac =~ s/\s//gi;
81 print LOG "Checking MAC $mac from group $group ";
82 #MAC checking
83 if(&General::validmac($mac)){
84 $val=$mac;
85 }
86 if($val){
87 push(@hostarray,$val.",mac");
88 print LOG "-> OK\n";
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 }
100 sub 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 }
121 sub 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);
136 if($byte4 eq '255'){
137 print LOG "Processing SINGLE HOST $ippart/$subnet from group $grp\n";
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;
147 print LOG "->Host (IP) $ip added to custom hosts\n"
148 }else{
149 print LOG "->Host (IP) $ip already exists\n";
150 }
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);
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;
162 print LOG "->Network $ippart/$subnet added to custom networks\n";
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;
174 print LOG "->$name2 added to group $grp\n";
175 }
176 }elsif($run eq 'mac'){
177 #MACRUN
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);
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;
189 print LOG "->Host (MAC) $mac added to custom hosts\n";
190 }else{
191 print LOG "->Host (MAC) $mac already exists\n";
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;
200 print LOG "->$name2 added to group $grp\n";
201 }
202 }
203 }
204 @hostarray=();
205 &General::writehasharray($confighosts,\%hosts);
206 &General::writehasharray($configgroups,\%groups);
207 &General::writehasharray($confignets,\%nets);
208
209 }
210 sub 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 }
222 sub 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 }
235 sub 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 }
248 sub process_rules
249 {
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
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 $!;
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);
274 $port='';
275 print LOG "processing: $rule\n";
276 my @configline=();
277 @configline = split( /\;/, $rule );
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' && $configline[8] ne ''){
283 push(@prot,"TCP");
284 push(@prot,"UDP");
285 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
286 push(@prot,"");
287 }else{
288 push(@prot,$configline[3]);
289 }
290
291 if($configline[4] ne ''){
292 $configline[4] =~ s/,/;/g;
293 $remark = $configline[4];
294 }else{$remark = '';}
295 if($configline[9] eq 'aktiv'){ $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'){
298 $time='';
299 }else{
300 $time='ON';
301 }
302 }else{
303 $time='ON';
304 }
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 ############################################################
315 #sourcepart
316 if ($configline[2] eq 'green') {
317 $grp1='std_net_src';
318 $source='GREEN';
319 }elsif ($configline[2] eq 'orange') {
320 $grp1='std_net_src';
321 $source='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";
324 next;
325 }elsif ($configline[2] eq 'blue') {
326 $grp1='std_net_src';
327 $source='BLUE';
328 }elsif ($configline[2] eq 'ipsec') {
329 print LOG "-> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
330 next;
331 }elsif ($configline[2] eq 'ovpn') {
332 &build_ovpn_grp;
333 $grp1='cust_grp_src';
334 $source='ovpn'
335 }elsif ($configline[2] eq 'ip') {
336 my $z=&check_ip($configline[5]);
337 if($z){
338 $grp1='src_addr';
339 $source=$z;
340 }else{
341 print LOG "-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
342 next;
343 }
344 }elsif ($configline[2] eq 'mac') {
345 if(&General::validmac($configline[6])){
346 $grp1='src_addr';
347 $source=$configline[6];
348 }else{
349 print LOG"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
350 next;
351 }
352 }elsif ($configline[2] eq 'all') {
353 $grp1='std_net_src';
354 $source='ALL';
355 }else{
356 #&General::readhasharray($configgroups,\%groups);
357 foreach my $key (sort keys %groups){
358 if($groups{$key}[0] eq $configline[2]){
359 $grp1='cust_grp_src';
360 $source=$configline[2];
361 }
362 }
363 if ($grp1 eq '' || $source eq ''){
364 print LOG "-> Rule not converted, no valid source recognised\n";
365 }
366 }
367 ############################################################
368 #destinationpart
369 if($configline[7] ne ''){
370 my $address=&check_ip($configline[7]);
371 if($address){
372 $grp2='tgt_addr';
373 $target=$address;
374 }elsif(!$address){
375 my $getwebsiteip=&get_ip_from_domain($configline[7]);
376 if ($getwebsiteip){
377 $grp2='tgt_addr';
378 $target=$getwebsiteip;
379 $remark.=" $configline[7]";
380 }else{
381 print LOG "-> Rule not converted, invalid domain \"$configline[7]\"\n";
382 next;
383 }
384 }
385 }else{
386 $grp2='std_net_tgt';
387 $target='ALL';
388 }
389 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
390 my @values=();
391 my @parts=split(",",$configline[8]);
392 foreach (@parts){
393 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
394 if(&General::validport($_)){
395 $useport='ON';
396 push (@values,$_);
397 $grp3='TGT_PORT';
398 }else{
399 print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
400 next;
401 }
402 }else{
403 my ($a1,$a2) = split(/\:/,$_);
404 if (&General::validport($a1) && &General::validport($a2) && $a1 < $a2){
405 $useport='ON';
406 push (@values,"$a1:$a2");
407 $grp3='TGT_PORT';
408 }else{
409 print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
410 next;
411 }
412 }
413 }
414 $port=join("|",@values);
415 @values=();
416 @parts=();
417 }
418 }else{
419 print LOG "-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
420 }
421 &General::readhasharray($fwdfwconfig,\%fwconfig);
422 my $check;
423 foreach my $protocol (@prot){
424 $protocol=uc($protocol);
425 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";
426 #Put rules into system....
427 ###########################
428
429 #check for double rules
430 foreach my $key (sort keys %fwconfig){
431 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"
432 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]"){
433 $check='on';
434 next;
435 }
436 }
437 if($check ne 'on'){
438 #increase groupcounter
439 my $check1;
440 if($grp1 eq 'cust_grp_src'){
441 foreach my $key (sort keys %groups){
442 if($groups{$key}[0] eq $source){
443 $groups{$key}[4]++;
444 $check1='on';
445 }
446 }
447 if($check1 eq 'on'){
448 &General::writehasharray($configgroups,\%groups);
449 }
450 }
451 my $key = &General::findhasharraykey(\%fwconfig);
452 $fwconfig{$key}[0] = $action;
453 $fwconfig{$key}[1] = "FORWARDFW";
454 $fwconfig{$key}[2] = $active;
455 $fwconfig{$key}[3] = $grp1;
456 $fwconfig{$key}[4] = $source;
457 $fwconfig{$key}[5] = $grp2;
458 $fwconfig{$key}[6] = $target;
459 $fwconfig{$key}[11] = $useport;
460 $fwconfig{$key}[12] = $protocol;
461 $fwconfig{$key}[14] = $grp3;
462 $fwconfig{$key}[15] = $port;
463 $fwconfig{$key}[16] = $remark;
464 $fwconfig{$key}[17] = $log;
465 $fwconfig{$key}[18] = $time;
466 $fwconfig{$key}[19] = $time_mon;
467 $fwconfig{$key}[20] = $time_tue;
468 $fwconfig{$key}[21] = $time_wed;
469 $fwconfig{$key}[22] = $time_thu;
470 $fwconfig{$key}[23] = $time_fri;
471 $fwconfig{$key}[24] = $time_sat;
472 $fwconfig{$key}[25] = $time_sun;
473 $fwconfig{$key}[26] = $time_from;
474 $fwconfig{$key}[27] = $time_to;
475 }
476 }
477 &General::writehasharray($fwdfwconfig,\%fwconfig);
478 @prot=();
479 }
480 close(LOG);
481 @lines=();
482 }
483 sub get_ip_from_domain
484 {
485 $web=shift;
486 my $resolvedip;
487 my $checked;
488 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
489 if(@addrs){
490 $resolvedip=inet_ntoa($addrs[0]);
491 return $resolvedip;
492 }
493 return;
494 }
495 sub build_ovpn_grp
496 {
497 &General::readhasharray($confighosts,\%hosts);
498 &General::readhasharray($confignets,\%nets);
499 &General::readhasharray($configgroups,\%groups);
500 &General::readhasharray($ovpnconfig,\%configovpn);
501 &General::readhasharray($ccdconfig,\%ccdconf);
502 &General::readhash($ovpnsettings,\%settingsovpn);
503 #get ovpn nets
504 my @ovpnnets=();
505 if($settingsovpn{'DOVPN_SUBNET'}){
506 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
507 push (@ovpnnets,"$net,$subnet,dynamic");
508 }
509 foreach my $key (sort keys %ccdconf){
510 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
511 $subnet=&General::iporsubtodec($subnet);
512 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
513 }
514 foreach my $key (sort keys %configovpn){
515 if ($configovpn{$key}[3] eq 'net'){
516 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
517 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
518 }
519 }
520 #add ovpn nets to customnetworks/groups
521 foreach my $line (@ovpnnets){
522 my ($net,$subnet,$name) = split(",",$line);
523 if (!&check_net($net,$subnet)){
524 my $netkey = &General::findhasharraykey(\%nets);
525 $name2=$name."(ovpn)".$net;
526 $name3="Custom Network";
527 $nets{$netkey}[0] = $name2;
528 $nets{$netkey}[1] = $net;
529 $nets{$netkey}[2] = $subnet;
530 $nets{$netkey}[3] = 1;
531 }else{
532 print LOG "-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
533 }
534 if($name2){
535 my $grpkey = &General::findhasharraykey(\%groups);
536 $groups{$grpkey}[0] = "ovpn";
537 $groups{$grpkey}[1] = '';
538 $groups{$grpkey}[2] = $name2;
539 $groups{$grpkey}[3] = "Custom Network";
540 $groups{$grpkey}[4] = 0;
541 }
542 $name2='';
543 }
544 @ovpnnets=();
545 &General::writehasharray($confighosts,\%hosts);
546 &General::writehasharray($configgroups,\%groups);
547 &General::writehasharray($confignets,\%nets);
548 }
549 sub process_p2p
550 {
551 copy("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
552 chmod oct(0777), '/var/ipfire/forward/p2protocols';
553 }