]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/firewall/convert-outgoingfw
convert-outgoingfw: Fix permissions of p2protocols configuration file.
[people/teissler/ipfire-2.x.git] / config / firewall / convert-outgoingfw
CommitLineData
27f4a6b1 1#!/usr/bin/perl
dc21519f
AM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5bee9a9d 5# Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
dc21519f
AM
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
dc21519f
AM
21# #
22# This script converts old groups and firewallrules #
23# to the new one. This is a 3-step process. #
24# STEP1: convert groups ->LOG /var/log/converters #
25# STEP2: convert rules ->LOG /var/log/converters #
26# STEP3: convert P2P rules #
27# #
28###############################################################################
27f4a6b1
AM
29
30require '/var/ipfire/general-functions.pl';
454d47a9 31require "${General::swroot}/lang.pl";
5a9fd5db 32
27f4a6b1 33use Socket;
8f0b047b 34use File::Path;
5a9fd5db
AM
35use File::Copy;
36
27f4a6b1
AM
37my $ipgrouppath = "${General::swroot}/outgoing/groups/ipgroups/";
38my $macgrouppath = "${General::swroot}/outgoing/groups/macgroups/";
39my $outgoingrules = "${General::swroot}/outgoing/rules";
40my $outfwsettings = "${General::swroot}/outgoing/settings";
41my $host = "Converted ";
42my $confighosts = "${General::swroot}/fwhosts/customhosts";
43my $confignets = "${General::swroot}/fwhosts/customnetworks";
44my $configgroups = "${General::swroot}/fwhosts/customgroups";
45my $ovpnsettings = "${General::swroot}/ovpn/settings";
46my $ovpnconfig = "${General::swroot}/ovpn/ovpnconfig";
47my $ccdconfig = "${General::swroot}/ovpn/ccd.conf";
6d8eb5de
AM
48my $fwdfwconfig = "${General::swroot}/firewall/config";
49my $outfwconfig = "${General::swroot}/firewall/outgoing";
50my $fwdfwsettings = "${General::swroot}/firewall/settings";
27f4a6b1
AM
51my @ipgroups = qx(ls $ipgrouppath);
52my @macgroups = qx(ls $macgrouppath);
53my @hostarray=();
54my %outsettings=();
55my %hosts=();
56my %nets=();
57my %groups=();
58my %settingsovpn=();
59my %configovpn=();
60my %ccdconf=();
6128ded8
AM
61my %fwconfig=();
62my %fwconfigout=();
27f4a6b1 63my %fwdsettings=();
7326051e
AM
64my %ownnet=();
65my %ovpnSettings = ();
02cb636c 66my @active= ('Aktiv', 'aktiv', 'Active', 'Activo', 'Actif', 'Actief', 'Aktywne', 'Активен', 'Aktif');
7326051e 67&General::readhash("${General::swroot}/ovpn/settings", \%ovpnSettings);
27f4a6b1 68&General::readhash($outfwsettings,\%outsettings);
7326051e 69&General::readhash("${General::swroot}/ethernet/settings", \%ownnet);
454d47a9 70
8343fd12
AM
71#ONLY RUN if /var/ipfire/outgoing exists
72if ( -d "/var/ipfire/outgoing"){
73 &process_groups;
74 &process_rules;
75 &process_p2p;
76}
8039a710 77system("/usr/local/bin/firewallctrl");
27f4a6b1
AM
78sub process_groups
79{
2833f567 80 if(! -d "/var/log/converters"){ mkdir("/var/log/converters");}
8f0b047b 81 if( -f "/var/log/converters/groups-convert.log"){rmtree("var/log/converters");}
2833f567 82 open (LOG, ">/var/log/converters/groups-convert.log") or die $!;
27f4a6b1
AM
83 #IP Group processing
84 foreach my $group (@ipgroups){
e09884e0 85 my $now=localtime;
27f4a6b1 86 chomp $group;
e09884e0 87 print LOG "\n$now Processing IP-GROUP: $group...\n";
27f4a6b1
AM
88 open (DATEI, "<$ipgrouppath/$group");
89 my @zeilen = <DATEI>;
90 foreach my $ip (@zeilen){
91 chomp($ip);
92 $ip =~ s/\s//gi;
e09884e0 93 print LOG "$now Check IP $ip from Group $group ";
27f4a6b1
AM
94 my $val=&check_ip($ip);
95 if($val){
96 push(@hostarray,$val.",ip");
e09884e0 97 print LOG "$now -> OK\n";
27f4a6b1
AM
98 }
99 else{
e09884e0 100 print LOG "$now -> IP \"$ip\" from group $group not converted (invalid IP) \n";
27f4a6b1
AM
101 }
102 $val='';
103 }
104 &new_hostgrp($group,'ip');
105 @hostarray=();
106 }
107 $group='';
108 @zeilen=();
109 @hostarray=();
110 #MAC Group processing
111 foreach my $group (@macgroups){
112 chomp $group;
5a9fd5db 113 print LOG "\nProcessing MAC-GROUP: $group...\n";
27f4a6b1
AM
114 open (DATEI, "<$macgrouppath/$group");
115 my @zeilen = <DATEI>;
116 foreach my $mac (@zeilen){
117 chomp($mac);
118 $mac =~ s/\s//gi;
e09884e0 119 print LOG "$now Checking MAC $mac from group $group ";
27f4a6b1
AM
120 #MAC checking
121 if(&General::validmac($mac)){
122 $val=$mac;
123 }
124 if($val){
125 push(@hostarray,$val.",mac");
e09884e0 126 print LOG "$now -> OK\n";
27f4a6b1
AM
127 }
128 else{
e09884e0 129 print LOG "$now -> Mac $mac from group $group not converted (invalid MAC)\n";
27f4a6b1
AM
130 }
131 $val='';
132 }
133 &new_hostgrp($group,'mac');
134 @hostarray=();
05612a54 135 @zeilen=();
27f4a6b1
AM
136 }
137 close (LOG);
138}
139sub check_ip
140{
141 my $adr=shift;
142 my $a;
143 #ip with subnet in decimal
144 if($adr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)\/(\d{1,2})$/){
145 $adr=int($1).".".int($2).".".int($3).".".int($4);
146 my $b = &General::iporsubtodec($5);
147 $a=$adr."/".$b;
e3afaf88 148 }elsif($adr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
27f4a6b1
AM
149 $adr=int($1).".".int($2).".".int($3).".".int($4);
150 if(&General::validip($adr)){
151 $a=$adr."/255.255.255.255";
152 }
153 }
154 if(&General::validipandmask($adr)){
155 $a=&General::iporsubtodec($adr);
156 }
157 return $a;
158}
159sub new_hostgrp
160{
161 &General::readhasharray($confighosts,\%hosts);
162 &General::readhasharray($confignets,\%nets);
163 &General::readhasharray($configgroups,\%groups);
164 my $grp=shift;
165 my $run=shift;
166 my $name; #"converted"
167 my $name2;
168 my $name3; #custom host/custom net
454d47a9 169 my $mac2;
27f4a6b1
AM
170 foreach my $adr (@hostarray){
171 if($run eq 'ip'){
172 my ($ip,$type) = split(",",$adr);
173 my ($ippart,$subnet) = split("/",$ip);
174 my ($byte1,$byte2,$byte3,$byte4) = split(/\./,$subnet);
5a9fd5db
AM
175 if($byte4 eq '255'){
176 print LOG "Processing SINGLE HOST $ippart/$subnet from group $grp\n";
27f4a6b1
AM
177 if(!&check_host($ip)){
178 my $key = &General::findhasharraykey(\%hosts);
179 $name="host ";
180 $name2=$name.$ippart;
181 $name3="Custom Host";
182 $hosts{$key}[0] = $name2;
183 $hosts{$key}[1] = $type;
184 $hosts{$key}[2] = $ip;
05612a54 185 $hosts{$key}[3] = '';
5a9fd5db 186 print LOG "->Host (IP) $ip added to custom hosts\n"
27f4a6b1 187 }else{
05612a54
AM
188 print LOG "->Host (IP) $ip already exists in custom hosts\n";
189 $name="host ";
190 $name2=$name.$ippart;
8343fd12
AM
191 $name="host ";
192 $name2=$name.$ippart;
05612a54 193 $name3="Custom Host";
27f4a6b1
AM
194 }
195 }elsif($byte4 < '255'){
5a9fd5db 196 print LOG "Processing NETWORK $ippart/$subnet from Group $grp\n";
27f4a6b1 197 if(!&check_net($ippart,$subnet)){
7326051e
AM
198 #Check if this network is one one of IPFire internal networks
199 if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &General::IpInSubnet($ippart,$ownnet{'GREEN_NETADDRESS'},$ownnet{'GREEN_NETMASK'}))
200 {
201 $name2='GREEN';
202 $name3='Standard Network';
203 }elsif (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &General::IpInSubnet($ippart,$ownnet{'ORANGE_NETADDRESS'},$ownnet{'ORANGE_NETMASK'}))
204 {
205 $name2='ORANGE';
206 $name3='Standard Network';
207 }elsif (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &General::IpInSubnet($ippart,$ownnet{'BLUE_NETADDRESS'},$ownnet{'BLUE_NETMASK'}))
208 {
209 $name2='BLUE';
210 $name3='Standard Network';
211 }elsif ($ippart eq '0.0.0.0')
212 {
213 $name2='ALL';
214 $name3='Standard Network';
215 }elsif(defined($ovpnSettings{'DOVPN_SUBNET'}) && "$ippart/".&General::iporsubtodec($subnet) eq $ovpnSettings{'DOVPN_SUBNET'})
216 {
217 $name2='OpenVPN-Dyn';
218 $name3='Standard Network';
219 }else{
220 my $netkey = &General::findhasharraykey(\%nets);
221 $name="net ";
222 $name2=$name.$ippart;
223 $name3="Custom Network";
224 $nets{$netkey}[0] = $name2;
225 $nets{$netkey}[1] = $ippart;
226 $nets{$netkey}[2] = $subnet;
227 $nets{$netkey}[3] = '';
7326051e
AM
228 print LOG "->Network $ippart/$subnet added to custom networks\n";
229 }
27f4a6b1 230 }else{
05612a54
AM
231 print LOG "Network $ippart already exists in custom networks\n";
232 $name="net ";
233 $name2=$name.$ippart;
8343fd12
AM
234 $name="net ";
235 $name2=$name.$ippart;
05612a54 236 $name3="Custom Network";
27f4a6b1
AM
237 }
238 }
f2ab6fba 239 if($name2 && !&check_grp($grp,$name2)){
27f4a6b1
AM
240 my $grpkey = &General::findhasharraykey(\%groups);
241 $groups{$grpkey}[0] = $grp;
242 $groups{$grpkey}[1] = '';
243 $groups{$grpkey}[2] = $name2;
244 $groups{$grpkey}[3] = $name3;
5a9fd5db 245 print LOG "->$name2 added to group $grp\n";
27f4a6b1
AM
246 }
247 }elsif($run eq 'mac'){
248 #MACRUN
454d47a9 249 my ($mac,$type) = split(",",$adr);
5a9fd5db 250 print LOG "Processing HOST (MAC) $mac\n";
27f4a6b1 251 if(!&check_host($mac)){
454d47a9 252 my $key = &General::findhasharraykey(\%hosts);
27f4a6b1 253 $name="host ";
454d47a9
AM
254 $mac2=$mac;
255 $mac2 =~ s/:/-/g;
256 $name2=$name.$mac2;
27f4a6b1
AM
257 $name3="Custom Host";
258 $hosts{$key}[0] = $name2;
259 $hosts{$key}[1] = $type;
260 $hosts{$key}[2] = $mac;
5a9fd5db 261 print LOG "->Host (MAC) $mac added to custom hosts\n";
27f4a6b1 262 }else{
454d47a9
AM
263 $mac2=mac;
264 $mac2 =~ s/:/-/g;
05612a54
AM
265 print LOG "->Host (MAC) $mac already exists in custom hosts \n";
266 $name="host ";
454d47a9 267 $name2=$name.$mac2;
05612a54 268 $name3="Custom Host";
27f4a6b1 269 }
f2ab6fba 270 if($name2 && !&check_grp($grp,$name2)){
27f4a6b1
AM
271 my $grpkey = &General::findhasharraykey(\%groups);
272 $groups{$grpkey}[0] = $grp;
273 $groups{$grpkey}[1] = '';
274 $groups{$grpkey}[2] = $name2;
275 $groups{$grpkey}[3] = $name3;
5a9fd5db 276 print LOG "->$name2 added to group $grp\n";
27f4a6b1
AM
277 }
278 }
279 }
5a9fd5db 280 @hostarray=();
27f4a6b1
AM
281 &General::writehasharray($confighosts,\%hosts);
282 &General::writehasharray($configgroups,\%groups);
283 &General::writehasharray($confignets,\%nets);
70d38e50 284
27f4a6b1
AM
285}
286sub check_host
287{
288 my $ip=shift;
289 foreach my $key (sort keys %hosts)
290 {
291 if($hosts{$key}[2] eq $ip)
292 {
293 return 1;
294 }
295 }
296 return 0;
297}
298sub check_net
299{
300 my $ip=shift;
301 my $sub=shift;
302 foreach my $key (sort keys %nets)
303 {
304 if($nets{$key}[1] eq $ip && $nets{$key}[2] eq $sub)
305 {
306 return 1;
307 }
308 }
309 return 0;
310}
311sub check_grp
312{
313 my $grp=shift;
314 my $value=shift;
315 foreach my $key (sort keys %groups)
316 {
317 if($groups{$key}[0] eq $grp && $groups{$key}[2] eq $value)
318 {
319 return 1;
320 }
321 }
322 return 0;
323}
324sub process_rules
325{
6128ded8 326 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);
e09884e0
AM
327 #open LOG
328 if( -f "/var/log/converters/outgoingfw-convert.log"){unlink ("/var/log/converters/outgoingfw-convert.log");}
329 open (LOG, ">/var/log/converters/outgoingfw-convert.log") or die $!;
330
27f4a6b1 331 &General::readhash($fwdfwsettings,\%fwdsettings);
12a43202 332 if ($outsettings{'POLICY'} eq 'MODE1'){
e09884e0
AM
333 $fwdsettings{'POLICY'}='MODE1';
334 $fwdsettings{'POLICY1'}='MODE2';
27f4a6b1
AM
335 $type='ALLOW';
336 $action='ACCEPT';
e09884e0 337 }else{
fccf52cf 338 $fwdsettings{'POLICY'}='MODE2';
e09884e0 339 $fwdsettings{'POLICY1'}='MODE2';
27f4a6b1
AM
340 $type='DENY';
341 $action='DROP';
27f4a6b1 342 }
fccf52cf 343 &General::writehash($fwdfwsettings,\%fwdsettings);
27f4a6b1
AM
344 open (DATEI, "<$outgoingrules");
345 my @lines = <DATEI>;
346 foreach my $rule (@lines)
347 {
454d47a9
AM
348 &General::readhasharray($fwdfwconfig,\%fwconfig);
349 &General::readhasharray($outfwconfig,\%fwconfigout);
e09884e0 350 my $now=localtime;
27f4a6b1 351 chomp($rule);
99e698d0 352 $port='';
e09884e0 353 print LOG "$now processing: $rule\n";
99e698d0
AM
354 my @configline=();
355 @configline = split( /\;/, $rule );
27f4a6b1
AM
356 my @prot=();
357 if($configline[0] eq $type){
358 #some variables we can use from old config
359 if($configline[1] eq 'on'){ $active='ON';}else{$active='';}
5238a871
AM
360 if($configline[3] eq 'all' && $configline[8] ne ''){
361 push(@prot,"TCP");
362 push(@prot,"UDP");
363 }elsif($configline[3] eq 'all' && $configline[8] eq ''){
5a9fd5db 364 push(@prot,"");
27f4a6b1
AM
365 }else{
366 push(@prot,$configline[3]);
27f4a6b1 367 }
5238a871
AM
368 if($configline[4] ne ''){
369 $configline[4] =~ s/,/;/g;
370 $remark = $configline[4];
371 }else{$remark = '';}
02cb636c
AM
372 #find all "active" tags in all language files and check them against the old config
373 my $logging='0';
374 foreach (@active){
375 $logging='1' if ($_ eq $configline[9]);
376 }
377 if($logging eq '1' ){ $log='ON';}else{$log='';}
27f4a6b1
AM
378 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'){
379 if($configline[17] eq '00:00' && $configline[18] eq '00:00'){
380 $time='';
381 }else{
382 $time='ON';
383 }
384 }else{
385 $time='ON';
386 }
387 $time_mon=$configline[10];
388 $time_tue=$configline[11];
389 $time_wed=$configline[12];
390 $time_thu=$configline[13];
391 $time_fri=$configline[14];
392 $time_sat=$configline[15];
393 $time_sun=$configline[16];
394 $time_from=$configline[17];
395 $time_to=$configline[18];
396 ############################################################
397 #sourcepart
398 if ($configline[2] eq 'green') {
399 $grp1='std_net_src';
400 $source='GREEN';
401 }elsif ($configline[2] eq 'orange') {
402 $grp1='std_net_src';
403 $source='ORANGE';
404 }elsif ($configline[2] eq 'red') {
6128ded8
AM
405 $grp1='std_net_src';
406 $source='IPFire';
407 &General::readhash($fwdfwsettings,\%fwdsettings);
408 $fwdsettings{'POLICY1'}=$outsettings{'POLICY'};
fccf52cf 409 $fwdsettings{'POLICY'}=$outsettings{'POLICY'};
6128ded8 410 &General::writehash($fwdfwsettings,\%fwdsettings);
27f4a6b1
AM
411 }elsif ($configline[2] eq 'blue') {
412 $grp1='std_net_src';
413 $source='BLUE';
414 }elsif ($configline[2] eq 'ipsec') {
e09884e0 415 print LOG "$now -> Rule not converted, ipsec+ interface is obsolet since IPFire 2.7 \n";
27f4a6b1
AM
416 next;
417 }elsif ($configline[2] eq 'ovpn') {
e09884e0 418 print LOG "$now ->Creating networks/groups for OpenVPN...\n";
99e698d0
AM
419 &build_ovpn_grp;
420 $grp1='cust_grp_src';
421 $source='ovpn'
27f4a6b1
AM
422 }elsif ($configline[2] eq 'ip') {
423 my $z=&check_ip($configline[5]);
424 if($z){
3b81fad4
AM
425 my ($ipa,$subn) = split("/",$z);
426 $subn=&General::iporsubtocidr($subn);
27f4a6b1 427 $grp1='src_addr';
3b81fad4 428 $source="$ipa/$subn";
27f4a6b1 429 }else{
e09884e0 430 print LOG "$now -> Rule not converted, missing/invalid source ip \"$configline[5]\"\n";
27f4a6b1
AM
431 next;
432 }
433 }elsif ($configline[2] eq 'mac') {
434 if(&General::validmac($configline[6])){
435 $grp1='src_addr';
436 $source=$configline[6];
437 }else{
e09884e0 438 print LOG"$now -> Rule not converted, invalid MAC \"$configline[6]\" \n";
27f4a6b1
AM
439 next;
440 }
441 }elsif ($configline[2] eq 'all') {
442 $grp1='std_net_src';
443 $source='ALL';
444 }else{
27f4a6b1
AM
445 foreach my $key (sort keys %groups){
446 if($groups{$key}[0] eq $configline[2]){
447 $grp1='cust_grp_src';
448 $source=$configline[2];
449 }
450 }
451 if ($grp1 eq '' || $source eq ''){
e09884e0 452 print LOG "$now -> Rule not converted, no valid source recognised\n";
27f4a6b1
AM
453 }
454 }
455 ############################################################
456 #destinationpart
454d47a9 457 if($configline[7] ne '' && $configline[7] ne '0.0.0.0'){
27f4a6b1
AM
458 my $address=&check_ip($configline[7]);
459 if($address){
3b81fad4
AM
460 my ($dip,$dsub) = split("/",$address);
461 $dsub=&General::iporsubtocidr($dsub);
27f4a6b1 462 $grp2='tgt_addr';
3b81fad4 463 $target="$dip/$dsub";
27f4a6b1
AM
464 }elsif(!$address){
465 my $getwebsiteip=&get_ip_from_domain($configline[7]);
466 if ($getwebsiteip){
467 $grp2='tgt_addr';
468 $target=$getwebsiteip;
5a9fd5db 469 $remark.=" $configline[7]";
27f4a6b1 470 }else{
e09884e0 471 print LOG "$now -> Rule not converted, invalid domain \"$configline[7]\"\n";
27f4a6b1
AM
472 next;
473 }
474 }
475 }else{
476 $grp2='std_net_tgt';
477 $target='ALL';
478 }
87946296 479 if($configline[8] ne '' && $configline[3] ne 'gre' && $configline[3] ne 'esp'){
8f0b047b
AM
480 my @values=();
481 my @parts=split(",",$configline[8]);
482 foreach (@parts){
fccf52cf 483 $_=~ tr/-/:/;
8f0b047b
AM
484 if (!($_ =~ /^(\d+)\:(\d+)$/)) {
485 if(&General::validport($_)){
486 $useport='ON';
8f0b047b
AM
487 push (@values,$_);
488 $grp3='TGT_PORT';
489 }else{
e09884e0 490 print LOG "$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n";
8f0b047b
AM
491 next;
492 }
27f4a6b1 493 }else{
8b3dd791
AM
494 my ($a1,$a2) = split(/\:/,$_);
495 if (&General::validport($a1) && &General::validport($a2) && $a1 < $a2){
8f0b047b 496 $useport='ON';
8f0b047b
AM
497 push (@values,"$a1:$a2");
498 $grp3='TGT_PORT';
8b3dd791 499 }else{
e09884e0 500 print LOG "$now -> Rule not converted, invalid destination Port \"$configline[8]\"\n";
8f0b047b 501 next;
8b3dd791 502 }
8f0b047b 503 }
27f4a6b1 504 }
8b3dd791
AM
505 $port=join("|",@values);
506 @values=();
99e698d0 507 @parts=();
27f4a6b1
AM
508 }
509 }else{
510 print LOG "-> Rule not converted because not for Firewall mode $outsettings{'POLICY'} (we are only converting for actual mode)\n";
511 }
27f4a6b1 512 my $check;
6128ded8 513 my $chain;
27f4a6b1 514 foreach my $protocol (@prot){
e09884e0 515 my $now=localtime;
6128ded8
AM
516 if ($source eq 'IPFire'){
517 $chain='OUTGOINGFW';
518 }else{
519 $chain='FORWARDFW';
520 }
27f4a6b1 521 $protocol=uc($protocol);
454d47a9 522 print LOG "$now -> Converted: $action,$chain,$active,$grp1,$source,$grp2,$target,,$protocol,,,$useport,,,$grp3,$port,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to\n";
27f4a6b1
AM
523 #Put rules into system....
524 ###########################
27f4a6b1
AM
525 #check for double rules
526 foreach my $key (sort keys %fwconfig){
454d47a9
AM
527 if("$action,$chain,$active,$grp1,$source,$grp2,$target,$protocol,$useport,$grp3,$port,$remark,$log,$time,$time_mon,$time_tue,$time_wed,$time_thu,$time_fri,$time_sat,$time_sun,$time_from,$time_to"
528 eq "$fwconfig{$key}[0],$fwconfig{$key}[1],$fwconfig{$key}[2],$fwconfig{$key}[3],$fwconfig{$key}[4],$fwconfig{$key}[5],$fwconfig{$key}[6],$fwconfig{$key}[8],$fwconfig{$key}[11],$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]"){
27f4a6b1
AM
529 $check='on';
530 next;
531 }
532 }
533 if($check ne 'on'){
6128ded8
AM
534 if ($chain eq 'FORWARDFW'){
535 my $key = &General::findhasharraykey(\%fwconfig);
536 $fwconfig{$key}[0] = $action;
537 $fwconfig{$key}[1] = $chain;
538 $fwconfig{$key}[2] = $active;
539 $fwconfig{$key}[3] = $grp1;
540 $fwconfig{$key}[4] = $source;
541 $fwconfig{$key}[5] = $grp2;
542 $fwconfig{$key}[6] = $target;
454d47a9 543 $fwconfig{$key}[8] = $protocol;
6128ded8 544 $fwconfig{$key}[11] = $useport;
6128ded8
AM
545 $fwconfig{$key}[14] = $grp3;
546 $fwconfig{$key}[15] = $port;
547 $fwconfig{$key}[16] = $remark;
548 $fwconfig{$key}[17] = $log;
549 $fwconfig{$key}[18] = $time;
550 $fwconfig{$key}[19] = $time_mon;
551 $fwconfig{$key}[20] = $time_tue;
552 $fwconfig{$key}[21] = $time_wed;
553 $fwconfig{$key}[22] = $time_thu;
554 $fwconfig{$key}[23] = $time_fri;
555 $fwconfig{$key}[24] = $time_sat;
556 $fwconfig{$key}[25] = $time_sun;
557 $fwconfig{$key}[26] = $time_from;
558 $fwconfig{$key}[27] = $time_to;
ac9e77e3
AM
559 $fwconfig{$key}[28] = '';
560 $fwconfig{$key}[29] = 'ALL';
561 $fwconfig{$key}[30] = '';
562 $fwconfig{$key}[31] = 'dnat';
454d47a9 563 &General::writehasharray($fwdfwconfig,\%fwconfig);
6128ded8
AM
564 }else{
565 my $key = &General::findhasharraykey(\%fwconfigout);
566 $fwconfigout{$key}[0] = $action;
567 $fwconfigout{$key}[1] = $chain;
568 $fwconfigout{$key}[2] = $active;
569 $fwconfigout{$key}[3] = $grp1;
570 $fwconfigout{$key}[4] = $source;
571 $fwconfigout{$key}[5] = $grp2;
572 $fwconfigout{$key}[6] = $target;
454d47a9 573 $fwconfigout{$key}[8] = $protocol;
6128ded8 574 $fwconfigout{$key}[11] = $useport;
6128ded8
AM
575 $fwconfigout{$key}[14] = $grp3;
576 $fwconfigout{$key}[15] = $port;
577 $fwconfigout{$key}[16] = $remark;
578 $fwconfigout{$key}[17] = $log;
579 $fwconfigout{$key}[18] = $time;
580 $fwconfigout{$key}[19] = $time_mon;
581 $fwconfigout{$key}[20] = $time_tue;
582 $fwconfigout{$key}[21] = $time_wed;
583 $fwconfigout{$key}[22] = $time_thu;
584 $fwconfigout{$key}[23] = $time_fri;
585 $fwconfigout{$key}[24] = $time_sat;
586 $fwconfigout{$key}[25] = $time_sun;
587 $fwconfigout{$key}[26] = $time_from;
588 $fwconfigout{$key}[27] = $time_to;
ac9e77e3
AM
589 $fwconfigout{$key}[28] = '';
590 $fwconfigout{$key}[29] = 'ALL';
591 $fwconfigout{$key}[30] = '';
592 $fwconfigout{$key}[31] = 'dnat';
454d47a9 593 &General::writehasharray($outfwconfig,\%fwconfigout);
6128ded8 594 }
27f4a6b1
AM
595 }
596 }
27f4a6b1
AM
597 @prot=();
598 }
599 close(LOG);
600 @lines=();
601}
602sub get_ip_from_domain
603{
604 $web=shift;
605 my $resolvedip;
606 my $checked;
607 my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($web);
608 if(@addrs){
609 $resolvedip=inet_ntoa($addrs[0]);
610 return $resolvedip;
611 }
612 return;
613}
614sub build_ovpn_grp
615{
e09884e0 616 my $now=localtime;
27f4a6b1
AM
617 &General::readhasharray($confighosts,\%hosts);
618 &General::readhasharray($confignets,\%nets);
619 &General::readhasharray($configgroups,\%groups);
620 &General::readhasharray($ovpnconfig,\%configovpn);
621 &General::readhasharray($ccdconfig,\%ccdconf);
622 &General::readhash($ovpnsettings,\%settingsovpn);
623 #get ovpn nets
624 my @ovpnnets=();
625 if($settingsovpn{'DOVPN_SUBNET'}){
626 my ($net,$subnet)=split("/",$settingsovpn{'DOVPN_SUBNET'});
627 push (@ovpnnets,"$net,$subnet,dynamic");
e09884e0 628 print LOG "$now ->found dynamic OpenVPN net\n";
27f4a6b1
AM
629 }
630 foreach my $key (sort keys %ccdconf){
631 my ($net,$subnet)=split("/",$ccdconf{$key}[1]);
632 $subnet=&General::iporsubtodec($subnet);
633 push (@ovpnnets,"$net,$subnet,$ccdconf{$key}[0]");
e09884e0 634 print LOG "$now ->found OpenVPN static net $net/$subnet\n";
27f4a6b1
AM
635 }
636 foreach my $key (sort keys %configovpn){
637 if ($configovpn{$key}[3] eq 'net'){
638 my ($net,$subnet)=split("/",$configovpn{$key}[27]);
639 push (@ovpnnets,"$net,$subnet,$configovpn{$key}[2]");
e09884e0 640 print LOG "$now ->found OpenVPN $net/$subnet $configovpn{$key}[2]\n";
27f4a6b1
AM
641 }
642 }
643 #add ovpn nets to customnetworks/groups
644 foreach my $line (@ovpnnets){
e09884e0 645 my $now=localtime;
27f4a6b1
AM
646 my ($net,$subnet,$name) = split(",",$line);
647 if (!&check_net($net,$subnet)){
648 my $netkey = &General::findhasharraykey(\%nets);
649 $name2=$name."(ovpn)".$net;
650 $name3="Custom Network";
651 $nets{$netkey}[0] = $name2;
652 $nets{$netkey}[1] = $net;
653 $nets{$netkey}[2] = $subnet;
e09884e0 654 $nets{$netkey}[3] = '';
e09884e0 655 print LOG "$now ->added $name2 $net/$subnet to customnetworks\n";
27f4a6b1
AM
656 }else{
657 print LOG "-> Custom Network with same IP already exist \"$net/$subnet\" (you can ignore this, if this run was manual from shell)\n";
658 }
659 if($name2){
660 my $grpkey = &General::findhasharraykey(\%groups);
661 $groups{$grpkey}[0] = "ovpn";
662 $groups{$grpkey}[1] = '';
663 $groups{$grpkey}[2] = $name2;
664 $groups{$grpkey}[3] = "Custom Network";
e09884e0 665 print LOG "$now ->added $name2 to customgroup ovpn\n";
27f4a6b1
AM
666 }
667 $name2='';
668 }
669 @ovpnnets=();
670 &General::writehasharray($confighosts,\%hosts);
671 &General::writehasharray($configgroups,\%groups);
672 &General::writehasharray($confignets,\%nets);
e09884e0 673 print LOG "$now ->finished OVPN\n";
27f4a6b1 674}
5a9fd5db
AM
675sub process_p2p
676{
6d8eb5de 677 copy("/var/ipfire/outgoing/p2protocols","/var/ipfire/firewall/p2protocols");
7514fe47 678 chown 99, 99, '/var/ipfire/firewall/p2protocols';
5a9fd5db 679}