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