]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/forwardfw/rules.pl
openvpnctrl: Cleanup flushChain functions.
[people/teissler/ipfire-2.x.git] / config / forwardfw / rules.pl
CommitLineData
2a81ab0d
AM
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2012 #
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# #
22# Hi folks! I hope this code is useful for all. I needed something to handle #
23# my VPN Connections in a comfortable way. #
24# This script builds firewallrules from the webinterface #
25###############################################################################
26
2a81ab0d 27use strict;
472136c9 28use Time::Local;
2a81ab0d
AM
29no warnings 'uninitialized';
30
31# enable only the following on debugging purpose
32#use warnings;
33#use CGI::Carp 'fatalsToBrowser';
34
35my %fwdfwsettings=();
36my %defaultNetworks=();
37my %configfwdfw=();
38my %color=();
39my %icmptypes=();
40my %ovpnSettings=();
41my %customgrp=();
42our %sourcehash=();
43our %targethash=();
44my @timeframe=();
45my %configinputfw=();
5d7faa45 46my %configoutgoingfw=();
31fef6cc 47my %configdmzfw=();
a6edca5a 48my %confignatfw=();
2a81ab0d
AM
49my %aliases=();
50my @DPROT=();
36196d0d 51my @p2ps=();
2a81ab0d
AM
52require '/var/ipfire/general-functions.pl';
53require "${General::swroot}/lang.pl";
54require "${General::swroot}/forward/bin/firewall-lib.pl";
55
31fef6cc 56my $configdmz = "${General::swroot}/forward/dmz";
2a81ab0d
AM
57my $configfwdfw = "${General::swroot}/forward/config";
58my $configinput = "${General::swroot}/forward/input";
5d7faa45 59my $configoutgoing = "${General::swroot}/forward/outgoing";
a6edca5a 60my $confignat = "${General::swroot}/forward/nat";
36196d0d 61my $p2pfile = "${General::swroot}/forward/p2protocols";
2a81ab0d 62my $configgrp = "${General::swroot}/fwhosts/customgroups";
210ee67b 63my $netsettings = "${General::swroot}/ethernet/settings";
2a81ab0d 64my $errormessage='';
210ee67b
AM
65my $orange;
66my $green;
6adcf156 67my $blue;
2a81ab0d
AM
68my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
69my $CHAIN="FORWARDFW";
ddcec9d3 70my $conexists='off';
a6edca5a
AM
71my $command = 'iptables -A';
72my $dnat='';
73my $snat='';
2a81ab0d 74&General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings);
210ee67b 75&General::readhash("$netsettings", \%defaultNetworks);
31fef6cc 76&General::readhasharray($configdmz, \%configdmzfw);
2a81ab0d
AM
77&General::readhasharray($configfwdfw, \%configfwdfw);
78&General::readhasharray($configinput, \%configinputfw);
5d7faa45 79&General::readhasharray($configoutgoing, \%configoutgoingfw);
a6edca5a 80&General::readhasharray($confignat, \%confignatfw);
2a81ab0d
AM
81&General::readhasharray($configgrp, \%customgrp);
82&General::get_aliases(\%aliases);
83
ddcec9d3
AM
84#check if we have an internetconnection
85open (CONN,"/var/ipfire/red/iface");
86my $con = <CONN>;
87close(CONN);
88if (-f "/var/ipfire/red/active"){
89 $conexists='on';
90}
a6edca5a
AM
91open (CONN1,"/var/ipfire/red/local-ipaddress");
92my $redip = <CONN1>;
93close(CONN1);
2a81ab0d
AM
94################################
95# DEBUG/TEST #
96################################
54cb7ff0 97my $MODE=1; # 0 - normal operation
2a81ab0d
AM
98 # 1 - print configline and rules to console
99 #
100################################
101my $param=shift;
102
103if($param eq 'flush'){
104 if ($MODE eq '1'){
105 print " Flushing chains...\n";
106 }
107 &flush;
108}else{
109 if ($MODE eq '1'){
110 print " Flushing chains...\n";
111 }
112 &flush;
113 if ($MODE eq '1'){
114 print " Preparing rules...\n";
115 }
116 &preparerules;
117 if($MODE eq '0'){
118 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
af49e367 119 &p2pblock;
5d7faa45 120 system ("/usr/sbin/firewall-policy");
2a81ab0d 121 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
6adcf156
AM
122 $defaultNetworks{'GREEN_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'GREEN_NETMASK'});
123 $green="$defaultNetworks{'GREEN_ADDRESS'}/$defaultNetworks{'GREEN_NETMASK'}";
6adcf156
AM
124 if ($defaultNetworks{'BLUE_DEV'}){
125 $defaultNetworks{'BLUE_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'BLUE_NETMASK'});
126 $blue="$defaultNetworks{'BLUE_ADDRESS'}/$defaultNetworks{'BLUE_NETMASK'}";
127 #set default rules for BLUE
128 system ("iptables -A $CHAIN -s $blue -d $green -j RETURN");
129 }
5b7ed8bb
AM
130 if ($defaultNetworks{'ORANGE_DEV'}){
131 $defaultNetworks{'ORANGE_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'ORANGE_NETMASK'});
132 $orange="$defaultNetworks{'ORANGE_ADDRESS'}/$defaultNetworks{'ORANGE_NETMASK'}";
133 #set default rules for DMZ
134 system ("iptables -A $CHAIN -s $orange -d $green -j RETURN");
135 if ($defaultNetworks{'BLUE_DEV'}){
136 system ("iptables -A $CHAIN -s $orange -d $blue -j RETURN");
137 }
138 }
6adcf156 139 &p2pblock;
fd10a52c 140 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
5d7faa45 141 system ("/usr/sbin/firewall-policy");
2a81ab0d
AM
142 }
143 }
144}
2a81ab0d
AM
145sub flush
146{
147 system ("iptables -F FORWARDFW");
148 system ("iptables -F INPUTFW");
5d7faa45 149 system ("iptables -F OUTGOINGFW");
28640b73
AM
150 system ("iptables -F PORTFWACCESS");
151 system ("iptables -t nat -F NAT_DESTINATION");
152 system ("iptables -t nat -F NAT_SOURCE");
2a81ab0d
AM
153}
154sub preparerules
155{
31fef6cc
AM
156 if (! -z "${General::swroot}/forward/dmz"){
157 &buildrules(\%configdmzfw);
158 }
2a81ab0d
AM
159 if (! -z "${General::swroot}/forward/config"){
160 &buildrules(\%configfwdfw);
161 }
162 if (! -z "${General::swroot}/forward/input"){
163 &buildrules(\%configinputfw);
164 }
5d7faa45
AM
165 if (! -z "${General::swroot}/forward/outgoing"){
166 &buildrules(\%configoutgoingfw);
167 }
a6edca5a
AM
168 if (! -z "${General::swroot}/forward/nat"){
169 &buildrules(\%confignatfw);
170 }
2a81ab0d
AM
171}
172sub buildrules
173{
174 my $hash=shift;
b5269091 175 my $STAG;
a6edca5a
AM
176 my $natip;
177 my $snatport;
178 my $fireport;
bc912c6e 179 my $nat;
98cee89f 180 my $fwaccessdport;
992394d5 181 foreach my $key (sort {$a <=> $b} keys %$hash){
ddcec9d3 182 next if ($$hash{$key}[6] eq 'RED' && $conexists eq 'off' );
a6edca5a
AM
183 if ($$hash{$key}[28] eq 'ON'){
184 $command='iptables -t nat -A';
08e1c65d 185 $natip=&get_nat_ip($$hash{$key}[29],$$hash{$key}[31]);
a6edca5a 186 if($$hash{$key}[31] eq 'dnat'){
bc912c6e 187 $nat='DNAT';
98cee89f
AM
188 if ($$hash{$key}[30] =~ /\|/){
189 $$hash{$key}[30]=~ tr/|/,/;
190 $fireport='-m multiport --dport '.$$hash{$key}[30];
191 }else{
192 $fireport='--dport '.$$hash{$key}[30] if ($$hash{$key}[30]>0);
193 }
a6edca5a 194 }else{
bc912c6e 195 $nat='SNAT';
a6edca5a
AM
196 }
197 }
b5269091 198 $STAG='';
2a81ab0d
AM
199 if($$hash{$key}[2] eq 'ON'){
200 #get source ip's
201 if ($$hash{$key}[3] eq 'cust_grp_src'){
992394d5 202 foreach my $grp (sort {$a <=> $b} keys %customgrp){
2a81ab0d
AM
203 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
204 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
205 }
206 }
207 }else{
208 &get_address($$hash{$key}[3],$$hash{$key}[4],"src");
209 }
210 #get target ip's
211 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
992394d5 212 foreach my $grp (sort {$a <=> $b} keys %customgrp){
2a81ab0d
AM
213 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
214 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
215 }
216 }
217 }elsif($$hash{$key}[5] eq 'ipfire'){
05d4f131
AM
218 if($$hash{$key}[6] eq 'GREEN'){
219 $targethash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'};
220 }
221 if($$hash{$key}[6] eq 'BLUE'){
222 $targethash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'};
223 }
224 if($$hash{$key}[6] eq 'ORANGE'){
225 $targethash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'};
226 }
227 if($$hash{$key}[6] eq 'RED'){
2a81ab0d
AM
228 open(FILE, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.';
229 $targethash{$key}[0]= <FILE>;
230 close(FILE);
231 }else{
232 foreach my $alias (sort keys %aliases){
233 if ($$hash{$key}[6] eq $alias){
234 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
235 }
236 }
237 }
238 }else{
239 &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt");
240 }
2a81ab0d
AM
241 ##get source prot and port
242 $SRC_TGT='SRC';
243 $SPROT = &get_prot($hash,$key);
244 $SPORT = &get_port($hash,$key);
245 $SRC_TGT='';
14f7cb87 246
2a81ab0d
AM
247 ##get target prot and port
248 $DPROT=&get_prot($hash,$key);
14f7cb87 249
2a81ab0d
AM
250 if ($DPROT eq ''){$DPROT=' ';}
251 @DPROT=split(",",$DPROT);
14f7cb87 252
2a81ab0d
AM
253 #get time if defined
254 if($$hash{$key}[18] eq 'ON'){
472136c9
AM
255 my ($time1,$time2,$daylight);
256 my $daylight=$$hash{$key}[28];
257 $time1=&get_time($$hash{$key}[26],$daylight);
258 $time2=&get_time($$hash{$key}[27],$daylight);
2a81ab0d
AM
259 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
260 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
261 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
262 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
263 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
264 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
265 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
266 $TIME=join(",",@timeframe);
472136c9
AM
267
268 $TIMEFROM="--timestart $time1 ";
269 $TIMETILL="--timestop $time2 ";
a0f267b9 270 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
2a81ab0d 271 }
2a81ab0d
AM
272 if ($MODE eq '1'){
273 print "NR:$key ";
274 foreach my $i (0 .. $#{$$hash{$key}}){
275 print "$i: $$hash{$key}[$i] ";
276 }
277 print "\n";
278 print"##################################\n";
279 #print rules to console
2a81ab0d
AM
280 foreach my $DPROT (@DPROT){
281 $DPORT = &get_port($hash,$key,$DPROT);
282 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
283 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
284 foreach my $a (sort keys %sourcehash){
285 foreach my $b (sort keys %targethash){
d7dc9718 286 if ($sourcehash{$a}[0] ne $targethash{$b}[0] && $targethash{$b}[0] ne 'none' || $sourcehash{$a}[0] eq '0.0.0.0/0.0.0.0'){
2a81ab0d 287 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
5d7faa45 288 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
8cb1afc8
AM
289 if(substr($DPORT, 2, 4) eq 'icmp'){
290 my @icmprule= split(",",substr($DPORT, 12,));
291 foreach (@icmprule){
292 if ($$hash{$key}[17] eq 'ON'){
a6edca5a 293 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j LOG\n";
8cb1afc8 294 }
a6edca5a 295 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]\n";
8cb1afc8 296 }
a6edca5a 297 }elsif($$hash{$key}[28] ne 'ON'){
8cb1afc8 298 if ($$hash{$key}[17] eq 'ON'){
a6edca5a 299 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
8cb1afc8 300 }
a6edca5a 301 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
28640b73
AM
302 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
303 if ($$hash{$key}[17] eq 'ON'){
304 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
305 }
28640b73 306 my ($ip,$sub) =split("/",$targethash{$b}[0]);
bc912c6e 307 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n";
829697d0 308 $DPORT =~ s/\-/:/g;
98cee89f
AM
309 if ($DPORT){
310 $fwaccessdport="--dport ".substr($DPORT,1,);
311 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
312 if ($$hash{$key}[30]=~m/|/i){
313 $$hash{$key}[30] =~ s/\|/,/g;
314 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
315 }else{
316 $fwaccessdport="--dport $$hash{$key}[30]";
317 }
318 }
bc912c6e 319 print "iptables -A PORTFWACCESS $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
08e1c65d
AM
320 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
321 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip\n";
2a81ab0d 322 }
2a81ab0d
AM
323 }
324 }
325 }
326 }
327 print"\n";
328 }
2a81ab0d
AM
329 }elsif($MODE eq '0'){
330 foreach my $DPROT (@DPROT){
331 $DPORT = &get_port($hash,$key,$DPROT);
332 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
333 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
334 foreach my $a (sort keys %sourcehash){
335 foreach my $b (sort keys %targethash){
d7dc9718 336 if ($sourcehash{$a}[0] ne $targethash{$b}[0] && $targethash{$b}[0] ne 'none' || $sourcehash{$a}[0] eq '0.0.0.0/0.0.0.0'){
2a81ab0d 337 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
5d7faa45 338 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
8cb1afc8
AM
339 if(substr($DPORT, 2, 4) eq 'icmp'){
340 my @icmprule= split(",",substr($DPORT, 12,));
341 foreach (@icmprule){
342 if ($$hash{$key}[17] eq 'ON'){
a6edca5a 343 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] -- icmp-type $_ $TIME -j LOG");
8cb1afc8 344 }
a6edca5a
AM
345 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]");
346 }
347 }elsif($$hash{$key}[28] ne 'ON'){
348 if ($$hash{$key}[17] eq 'ON'){
349 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
350 }
351 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
352 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
353 if ($$hash{$key}[17] eq 'ON'){
354 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
8cb1afc8 355 }
a6edca5a 356 my ($ip,$sub) =split("/",$targethash{$b}[0]);
bc912c6e 357 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n";
829697d0 358 $DPORT =~ s/\-/:/g;
98cee89f
AM
359 if ($DPORT){
360 $fwaccessdport="--dport ".substr($DPORT,1,);
361 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
362 if ($$hash{$key}[30]=~m/|/i){
363 $$hash{$key}[30] =~ s/\|/,/g;
364 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
365 }else{
366 $fwaccessdport="--dport $$hash{$key}[30]";
367 }
368 }
bc912c6e 369 system "iptables -A PORTFWACCESS $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
a6edca5a 370 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
8cb1afc8 371 if ($$hash{$key}[17] eq 'ON'){
a6edca5a 372 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT '\n";
8cb1afc8 373 }
bc912c6e 374 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip$fireport\n";
2a81ab0d 375 }
2a81ab0d
AM
376 }
377 }
378 }
379 }
2a81ab0d
AM
380 }
381 }
382 }
383 %sourcehash=();
384 %targethash=();
385 undef $TIME;
386 undef $TIMEFROM;
387 undef $TIMETILL;
a6edca5a 388 undef $fireport;
2a81ab0d
AM
389 }
390}
a6edca5a
AM
391sub get_nat_ip
392{
393 my $val=shift;
08e1c65d 394 my $type=shift;
a6edca5a
AM
395 my $result;
396 if($val eq 'RED' || $val eq 'GREEN' || $val eq 'ORANGE' || $val eq 'BLUE'){
397 $result=$defaultNetworks{$val.'_ADDRESS'};
398 }elsif($val eq 'ALL'){
399 $result='-i '.$con;
08e1c65d 400 }elsif($val eq 'Default IP' && $type eq 'dnat'){
a6edca5a 401 $result='-d '.$redip;
08e1c65d
AM
402 }elsif($val eq 'Default IP' && $type eq 'snat'){
403 $result=$redip;
a6edca5a
AM
404 }else{
405 foreach my $al (sort keys %aliases){
08e1c65d 406 if($val eq $al && $type eq 'dnat'){
a6edca5a 407 $result='-d '.$aliases{$al}{'IPT'};
08e1c65d
AM
408 }elsif($val eq $al && $type eq 'snat'){
409 $result=$aliases{$al}{'IPT'};
a6edca5a
AM
410 }
411 }
412 }
413 return $result;
414}
472136c9
AM
415sub get_time
416{
417 my $val=shift;
418 my $val1=shift;
419 my $time;
420 my $minutes;
421 my $ruletime;
422 $minutes = &utcmin($val);
423 $ruletime = $minutes + &time_get_utc($val);
424 if ($ruletime < 0){$ruletime +=1440;}
425 if ($ruletime > 1440){$ruletime -=1440;}
426 $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60;
427 return $time;
428}
429sub time_get_utc
430{
431 # Calculates the UTCtime from a given time
432 my $val=shift;
433 my @localtime=localtime(time);
434 my @gmtime=gmtime(time);
435 my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60);
436 return $diff;
437}
438sub utcmin
439{
440 my $ruletime=shift;
441 my ($hrs,$min) = split(":",$ruletime);
442 my $newtime = $hrs*60+$min;
443 return $newtime;
444}
36196d0d
AM
445sub p2pblock
446{
447 my $P2PSTRING;
448 my $DO;
449 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
450 @p2ps = <FILE>;
451 close FILE;
452 my $CMD = "-m ipp2p";
453 foreach my $p2pentry (sort @p2ps) {
454 my @p2pline = split( /\;/, $p2pentry );
8d1beadc
AM
455 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
456 $DO = "ACCEPT";
5238a871 457 if ("$p2pline[2]" eq "on") {
36196d0d
AM
458 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
459 }
8d1beadc 460 }else {
36196d0d 461 $DO = "RETURN";
5238a871 462 if ("$p2pline[2]" eq "off") {
36196d0d
AM
463 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
464 }
465 }
466 }
467 if ($MODE eq 1){
468 if($P2PSTRING){
469 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
470 }
471 }else{
472 if($P2PSTRING){
473 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
474 }
475 }
476}
2a81ab0d
AM
477sub get_address
478{
479 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
480 my $base2=shift;
481 my $type=shift; #src or tgt
482 my $hash;
483 if ($type eq 'src'){
484 $hash=\%sourcehash;
485 }else{
486 $hash=\%targethash;
487 }
488 my $key = &General::findhasharraykey($hash);
489 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
b5269091
AM
490 if (&General::validmac($base2)){
491 $$hash{$key}[0] = "-m mac --mac-source $base2";
492 }else{
493 $$hash{$key}[0] = $base2;
494 }
2a81ab0d 495 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
ddcec9d3 496 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2,$con);
2a81ab0d
AM
497 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
498 $$hash{$key}[0]=&fwlib::get_net_ip($base2);
499 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
500 $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type);
501 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
502 $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1);
503 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
504 $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33);
505 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
6fab5bca 506 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,11);
2a81ab0d
AM
507 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
508 $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11);
509 }
510}
511sub get_prot
512{
513 my $hash=shift;
514 my $key=shift;
515 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
516 if ($$hash{$key}[10] ne ''){
517 return"$$hash{$key}[8]";
518 }elsif($$hash{$key}[9] ne ''){
519 return"$$hash{$key}[8]";
520 }else{
521 return "$$hash{$key}[8]";
522 }
523 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
524 if ($$hash{$key}[14] eq 'TGT_PORT'){
525 if ($$hash{$key}[15] ne ''){
526 return "$$hash{$key}[12]";
527 }elsif($$hash{$key}[13] ne ''){
528 return "$$hash{$key}[12]";
529 }else{
530 return "$$hash{$key}[12]";
531 }
532 }elsif($$hash{$key}[14] eq 'cust_srv'){
533 return &fwlib::get_srv_prot($$hash{$key}[15]);
534
535 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
536 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
537 }
538 }
98cee89f
AM
539 #DNAT
540 if ($SRC_TGT eq '' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[11] eq '' && $$hash{$key}[12] ne ''){
541 return "$$hash{$key}[12]";
542 }
2a81ab0d
AM
543}
544sub get_port
545{
546 my $hash=shift;
547 my $key=shift;
548 my $prot=shift;
549 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
550 if ($$hash{$key}[10] ne ''){
8f0b047b 551 $$hash{$key}[10] =~ s/\|/,/g;
93a5f4a5
AM
552 if(index($$hash{$key}[10],",") > 0){
553 return "-m multiport --sport $$hash{$key}[10] ";
554 }else{
a6edca5a
AM
555 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ||($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat') ){
556 return "--sport $$hash{$key}[10] ";
557 }else{
558 return ":$$hash{$key}[10]";
559 }
93a5f4a5 560 }
62fc8511 561 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
2a81ab0d 562 return "--icmp-type $$hash{$key}[9] ";
62fc8511
AM
563 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
564 return;
2a81ab0d
AM
565 }
566 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
2a81ab0d
AM
567 if($$hash{$key}[14] eq 'TGT_PORT'){
568 if ($$hash{$key}[15] ne ''){
8f0b047b 569 $$hash{$key}[15] =~ s/\|/,/g;
93a5f4a5
AM
570 if(index($$hash{$key}[15],",") > 0){
571 return "-m multiport --dport $$hash{$key}[15] ";
572 }else{
a6edca5a
AM
573 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ){
574 return "--dport $$hash{$key}[15] ";
575 }else{
829697d0 576 $$hash{$key}[15] =~ s/\:/-/g;
a6edca5a
AM
577 return ":$$hash{$key}[15]";
578 }
93a5f4a5 579 }
2a81ab0d
AM
580 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){
581 return "--icmp-type $$hash{$key}[13] ";
582 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){
583 return;
584 }
585 }elsif($$hash{$key}[14] eq 'cust_srv'){
586 if ($prot ne 'ICMP'){
6be32fe5
AM
587 if($$hash{$key}[31] eq 'dnat'){
588 return ":".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
589 }else{
590 return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
591 }
2a81ab0d
AM
592 }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){
593 return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot);
594 }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){
595 return;
596 }
597 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
598 if ($prot ne 'ICMP'){
599 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
600 }
601 elsif($prot eq 'ICMP'){
602 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
603 }
2a81ab0d
AM
604 }
605 }
606}