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