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