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