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