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