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