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