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