]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/convert-outgoingfw
Forward Firewall: converter now creates cdir format from ip-addresses
[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 my ($ipa,$subn) = split("/",$z);
339 $subn=&General::iporsubtocidr($subn);
340 $grp1='src_addr';
341 $source="$ipa/$subn";
342 }else{
343 print LOG "-> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
344 next;
345 }
346 }elsif ($configline[2] eq 'mac') {
347 if(&General::validmac($configline[6])){
348 $grp1='src_addr';
349 $source=$configline[6];
350 }else{
351 print LOG"-> Rule not converted, invalid MAC \"$configline[6]\" \n";
352 next;
353 }
354 }elsif ($configline[2] eq 'all') {
355 $grp1='std_net_src';
356 $source='ALL';
357 }else{
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];
363 }
364 }
365 if ($grp1 eq '' || $source eq ''){
366 print LOG "-> Rule not converted, no valid source recognised\n";
367 }
368 }
369 ############################################################
370 #destinationpart
371 if($configline[7] ne ''){
372 my $address=&check_ip($configline[7]);
373 if($address){
374 my ($dip,$dsub) = split("/",$address);
375 $dsub=&General::iporsubtocidr($dsub);
376 $grp2='tgt_addr';
377 $target="$dip/$dsub";
378 }elsif(!$address){
379 my $getwebsiteip=&get_ip_from_domain($configline[7]);
380 if ($getwebsiteip){
381 $grp2='tgt_addr';
382 $target=$getwebsiteip;
383 $remark.=" $configline[7]";
384 }else{
385 print LOG "-> Rule not converted, invalid domain \"$configline[7]\"\n";
386 next;
387 }
388 }
389 }else{
390 $grp2='std_net_tgt';
391 $target='ALL';
392 }
393 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
394 my @values=();
395 my @parts=split(",",$configline[8]);
396 foreach (@parts){
397 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
398 if(&General::validport($_)){
399 $useport='ON';
400 push (@values,$_);
401 $grp3='TGT_PORT';
402 }else{
403 print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
404 next;
405 }
406 }else{
407 my ($a1,$a2) = split(/\:/,$_);
408 if (&General::validport($a1) && &General::validport($a2) && $a1 < $a2){
409 $useport='ON';
410 push (@values,"$a1:$a2");
411 $grp3='TGT_PORT';
412 }else{
413 print LOG "-> Rule not converted, invalid destination Port \"$configline[8]\"\n";
414 next;
415 }
416 }
417 }
418 $port=join("|",@values);
419 @values=();
420 @parts=();
421 }
422 }else{
423 print LOG "-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
424 }
425 &General::readhasharray($fwdfwconfig,\%fwconfig);
426 my $check;
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 ###########################
432
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]"){
437 $check='on';
438 next;
439 }
440 }
441 if($check ne 'on'){
442 #increase groupcounter
443 my $check1;
444 if($grp1 eq 'cust_grp_src'){
445 foreach my $key (sort keys %groups){
446 if($groups{$key}[0] eq $source){
447 $groups{$key}[4]++;
448 $check1='on';
449 }
450 }
451 if($check1 eq 'on'){
452 &General::writehasharray($configgroups,\%groups);
453 }
454 }
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;
479 }
480 }
481 &General::writehasharray($fwdfwconfig,\%fwconfig);
482 @prot=();
483 }
484 close(LOG);
485 @lines=();
486 }
487 sub get_ip_from_domain
488 {
489 $web=shift;
490 my $resolvedip;
491 my $checked;
492 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
493 if(@addrs){
494 $resolvedip=inet_ntoa($addrs[0]);
495 return $resolvedip;
496 }
497 return;
498 }
499 sub build_ovpn_grp
500 {
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);
507 #get ovpn nets
508 my @ovpnnets=();
509 if($settingsovpn{'DOVPN_SUBNET'}){
510 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
511 push (@ovpnnets,"$net,$subnet,dynamic");
512 }
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]");
517 }
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]");
522 }
523 }
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;
535 }else{
536 print LOG "-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
537 }
538 if($name2){
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;
545 }
546 $name2='';
547 }
548 @ovpnnets=();
549 &General::writehasharray($confighosts,\%hosts);
550 &General::writehasharray($configgroups,\%groups);
551 &General::writehasharray($confignets,\%nets);
552 }
553 sub process_p2p
554 {
555 copy("/var/ipfire/outgoing/p2protocols","/var/ipfire/forward/p2protocols");
556 chmod oct(0777), '/var/ipfire/forward/p2protocols';
557 }