]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/rules.pl
Forward Firewall: edited GPL-header
[people/teissler/ipfire-2.x.git] / config / forwardfw / rules.pl
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
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 use strict;
23 use Time::Local;
24 no warnings 'uninitialized';
25
26 # enable only the following on debugging purpose
27 #use warnings;
28 #use CGI::Carp 'fatalsToBrowser';
29
30 my %fwdfwsettings=();
31 my %defaultNetworks=();
32 my %configfwdfw=();
33 my %color=();
34 my %icmptypes=();
35 my %ovpnSettings=();
36 my %customgrp=();
37 our %sourcehash=();
38 our %targethash=();
39 my @timeframe=();
40 my %configinputfw=();
41 my %configoutgoingfw=();
42 my %confignatfw=();
43 my %aliases=();
44 my @DPROT=();
45 my @p2ps=();
46 require '/var/ipfire/general-functions.pl';
47 require "${General::swroot}/lang.pl";
48 require "${General::swroot}/forward/bin/firewall-lib.pl";
49
50 my $configfwdfw = "${General::swroot}/forward/config";
51 my $configinput = "${General::swroot}/forward/input";
52 my $configoutgoing = "${General::swroot}/forward/outgoing";
53 my $p2pfile = "${General::swroot}/forward/p2protocols";
54 my $configgrp = "${General::swroot}/fwhosts/customgroups";
55 my $netsettings = "${General::swroot}/ethernet/settings";
56 my $errormessage='';
57 my $orange;
58 my $green;
59 my $blue;
60 my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
61 my $CHAIN="FORWARDFW";
62 my $conexists='off';
63 my $command = 'iptables -A';
64 my $dnat='';
65 my $snat='';
66 &General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings);
67 &General::readhash("$netsettings", \%defaultNetworks);
68 &General::readhasharray($configfwdfw, \%configfwdfw);
69 &General::readhasharray($configinput, \%configinputfw);
70 &General::readhasharray($configoutgoing, \%configoutgoingfw);
71 &General::readhasharray($configgrp, \%customgrp);
72 &General::get_aliases(\%aliases);
73
74 #check if we have an internetconnection
75 open (CONN,"/var/ipfire/red/iface");
76 my $con = <CONN>;
77 close(CONN);
78 if (-f "/var/ipfire/red/active"){
79 $conexists='on';
80 }
81 open (CONN1,"/var/ipfire/red/local-ipaddress");
82 my $redip = <CONN1>;
83 close(CONN1);
84 ################################
85 # DEBUG/TEST #
86 ################################
87 my $MODE=0; # 0 - normal operation
88 # 1 - print configline and rules to console
89 #
90 ################################
91 my $param=shift;
92
93 if($param eq 'flush'){
94 if ($MODE eq '1'){
95 print " Flushing chains...\n";
96 }
97 &flush;
98 }else{
99 if ($MODE eq '1'){
100 print " Flushing chains...\n";
101 }
102 &flush;
103 if ($MODE eq '1'){
104 print " Preparing rules...\n";
105 }
106 &preparerules;
107 if($MODE eq '0'){
108 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
109 &p2pblock;
110 system ("/usr/sbin/firewall-policy");
111 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
112 &p2pblock;
113 system ("iptables -A $CHAIN -m conntrack --ctstate NEW -j ACCEPT");
114 system ("/usr/sbin/firewall-policy");
115 system ("/etc/sysconfig/firewall.local reload");
116 }
117 }
118 }
119 sub flush
120 {
121 system ("iptables -F FORWARDFW");
122 system ("iptables -F INPUTFW");
123 system ("iptables -F OUTGOINGFW");
124 system ("iptables -t nat -F NAT_DESTINATION");
125 system ("iptables -t nat -F NAT_SOURCE");
126 }
127 sub preparerules
128 {
129 if (! -z "${General::swroot}/forward/config"){
130 &buildrules(\%configfwdfw);
131 }
132 if (! -z "${General::swroot}/forward/input"){
133 &buildrules(\%configinputfw);
134 }
135 if (! -z "${General::swroot}/forward/outgoing"){
136 &buildrules(\%configoutgoingfw);
137 }
138 }
139 sub buildrules
140 {
141 my $hash=shift;
142 my $STAG;
143 my $natip;
144 my $snatport;
145 my $fireport;
146 my $nat;
147 my $fwaccessdport;
148 my $natchain;
149 foreach my $key (sort {$a <=> $b} keys %$hash){
150 next if (($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1') && $conexists eq 'off' );
151 if ($$hash{$key}[28] eq 'ON'){
152 $command='iptables -t nat -A';
153 $natip=&get_nat_ip($$hash{$key}[29],$$hash{$key}[31]);
154 if($$hash{$key}[31] eq 'dnat'){
155 $nat='DNAT';
156 if ($$hash{$key}[30] =~ /\|/){
157 $$hash{$key}[30]=~ tr/|/,/;
158 $fireport='-m multiport --dport '.$$hash{$key}[30];
159 }else{
160 $fireport='--dport '.$$hash{$key}[30] if ($$hash{$key}[30]>0);
161 }
162 }else{
163 $nat='SNAT';
164 }
165 }
166 $STAG='';
167 if($$hash{$key}[2] eq 'ON'){
168 #get source ip's
169 if ($$hash{$key}[3] eq 'cust_grp_src'){
170 foreach my $grp (sort {$a <=> $b} keys %customgrp){
171 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
172 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
173 }
174 }
175 }else{
176 &get_address($$hash{$key}[3],$$hash{$key}[4],"src");
177 }
178 #get target ip's
179 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
180 foreach my $grp (sort {$a <=> $b} keys %customgrp){
181 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
182 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
183 }
184 }
185 }elsif($$hash{$key}[5] eq 'ipfire' ){
186 if($$hash{$key}[6] eq 'GREEN'){
187 $targethash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'};
188 }
189 if($$hash{$key}[6] eq 'BLUE'){
190 $targethash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'};
191 }
192 if($$hash{$key}[6] eq 'ORANGE'){
193 $targethash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'};
194 }
195 if($$hash{$key}[6] eq 'ALL'){
196 $targethash{$key}[0]='0.0.0.0/0';
197 }
198 if($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1'){
199 open(FILE, "/var/ipfire/red/local-ipaddress")or die "Couldn't open local-ipaddress";
200 $targethash{$key}[0]= <FILE>;
201 close(FILE);
202 }else{
203 foreach my $alias (sort keys %aliases){
204 if ($$hash{$key}[6] eq $alias){
205 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
206 }
207 }
208 }
209 }else{
210 &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt");
211 }
212 ##get source prot and port
213 $SRC_TGT='SRC';
214 $SPROT = &get_prot($hash,$key);
215 $SPORT = &get_port($hash,$key);
216 $SRC_TGT='';
217
218 ##get target prot and port
219 $DPROT=&get_prot($hash,$key);
220
221 if ($DPROT eq ''){$DPROT=' ';}
222 @DPROT=split(",",$DPROT);
223
224 #get time if defined
225 if($$hash{$key}[18] eq 'ON'){
226 my ($time1,$time2,$daylight);
227 my $daylight=$$hash{$key}[28];
228 $time1=&get_time($$hash{$key}[26],$daylight);
229 $time2=&get_time($$hash{$key}[27],$daylight);
230 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
231 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
232 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
233 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
234 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
235 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
236 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
237 $TIME=join(",",@timeframe);
238
239 $TIMEFROM="--timestart $time1 ";
240 $TIMETILL="--timestop $time2 ";
241 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
242 }
243 if ($MODE eq '1'){
244 print "NR:$key ";
245 foreach my $i (0 .. $#{$$hash{$key}}){
246 print "$i: $$hash{$key}[$i] ";
247 }
248 print "\n";
249 print"##################################\n";
250 #print rules to console
251 foreach my $DPROT (@DPROT){
252 $DPORT = &get_port($hash,$key,$DPROT);
253 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
254 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
255 foreach my $a (sort keys %sourcehash){
256 foreach my $b (sort keys %targethash){
257 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'){
258 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
259 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
260 if(substr($DPORT, 2, 4) eq 'icmp'){
261 my @icmprule= split(",",substr($DPORT, 12,));
262 foreach (@icmprule){
263 if ($$hash{$key}[17] eq 'ON'){
264 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j LOG\n";
265 }
266 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]\n";
267 }
268 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
269 $natchain='NAT_DESTINATION';
270 if ($$hash{$key}[17] eq 'ON'){
271 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
272 }
273 my ($ip,$sub) =split("/",$targethash{$b}[0]);
274 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n";
275 $DPORT =~ s/\-/:/g;
276 if ($DPORT){
277 $fwaccessdport="--dport ".substr($DPORT,1,);
278 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
279 if ($$hash{$key}[30]=~m/|/i){
280 $$hash{$key}[30] =~ s/\|/,/g;
281 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
282 }else{
283 $fwaccessdport="--dport $$hash{$key}[30]";
284 }
285 }
286 print "iptables -A FORWARDFW $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
287 next;
288 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
289 $natchain='NAT_SOURCE';
290 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip\n";
291 }
292 if ($$hash{$key}[17] eq 'ON'){
293 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
294 }
295 if ($PROT ne '-p ICMP'){
296 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
297 }
298 }
299 }
300 }
301 }
302 print"\n";
303 }
304 }elsif($MODE eq '0'){
305 foreach my $DPROT (@DPROT){
306 $DPORT = &get_port($hash,$key,$DPROT);
307 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
308 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
309 foreach my $a (sort keys %sourcehash){
310 foreach my $b (sort keys %targethash){
311 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'){
312 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
313 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
314 #Process ICMP RULE
315 if(substr($DPORT, 2, 4) eq 'icmp'){
316 my @icmprule= split(",",substr($DPORT, 12,));
317 foreach (@icmprule){
318 if ($$hash{$key}[17] eq 'ON'){
319 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] -- icmp-type $_ $TIME -j LOG");
320 }
321 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]");
322 }
323 #PROCESS DNAT RULE (Portforward)
324 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
325 $natchain='NAT_DESTINATION';
326 if ($$hash{$key}[17] eq 'ON'){
327 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
328 }
329 my ($ip,$sub) =split("/",$targethash{$b}[0]);
330 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n";
331 $DPORT =~ s/\-/:/g;
332 if ($DPORT){
333 $fwaccessdport="--dport ".substr($DPORT,1,);
334 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
335 if ($$hash{$key}[30]=~m/|/i){
336 $$hash{$key}[30] =~ s/\|/,/g;
337 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
338 }else{
339 $fwaccessdport="--dport $$hash{$key}[30]";
340 }
341 }
342 system "iptables -A FORWARDFW $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
343 next;
344 #PROCESS SNAT RULE
345 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
346 $natchain='NAT_SOURCE';
347 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip\n";
348 }
349 if ($$hash{$key}[17] eq 'ON'){
350 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
351 }
352 #PROCESS EVERY OTHER RULE (If NOT ICMP, else the rule would be applied double)
353 if ($PROT ne '-p ICMP'){
354 system "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
355 }
356 }
357 }
358 }
359 }
360 }
361 }
362 }
363 %sourcehash=();
364 %targethash=();
365 undef $TIME;
366 undef $TIMEFROM;
367 undef $TIMETILL;
368 undef $fireport;
369 }
370 }
371 sub get_nat_ip
372 {
373 my $val=shift;
374 my $type=shift;
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;
380 }elsif($val eq 'Default IP' && $type eq 'dnat'){
381 $result='-d '.$redip;
382 }elsif($val eq 'Default IP' && $type eq 'snat'){
383 $result=$redip;
384 }else{
385 foreach my $al (sort keys %aliases){
386 if($val eq $al && $type eq 'dnat'){
387 $result='-d '.$aliases{$al}{'IPT'};
388 }elsif($val eq $al && $type eq 'snat'){
389 $result=$aliases{$al}{'IPT'};
390 }
391 }
392 }
393 return $result;
394 }
395 sub 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 }
409 sub 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 }
418 sub utcmin
419 {
420 my $ruletime=shift;
421 my ($hrs,$min) = split(":",$ruletime);
422 my $newtime = $hrs*60+$min;
423 return $newtime;
424 }
425 sub 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 );
435 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
436 $DO = "ACCEPT";
437 if ("$p2pline[2]" eq "on") {
438 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
439 }
440 }else {
441 $DO = "RETURN";
442 if ("$p2pline[2]" eq "off") {
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 }
457 sub 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' ){
470 if (&General::validmac($base2)){
471 $$hash{$key}[0] = "-m mac --mac-source $base2";
472 }else{
473 $$hash{$key}[0] = $base2;
474 }
475 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
476 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2,$con);
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'){
486 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,11);
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);
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 }
513 }
514 }
515 sub 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 }
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 }
547 }
548 sub 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 ''){
555 $$hash{$key}[10] =~ s/\|/,/g;
556 if(index($$hash{$key}[10],",") > 0){
557 return "-m multiport --sport $$hash{$key}[10] ";
558 }else{
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 }
564 }
565 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
566 return "--icmp-type $$hash{$key}[9] ";
567 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
568 return;
569 }
570 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
571 if($$hash{$key}[14] eq 'TGT_PORT'){
572 if ($$hash{$key}[15] ne ''){
573 $$hash{$key}[15] =~ s/\|/,/g;
574 if(index($$hash{$key}[15],",") > 0){
575 return "-m multiport --dport $$hash{$key}[15] ";
576 }else{
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{
580 $$hash{$key}[15] =~ s/\:/-/g;
581 return ":$$hash{$key}[15]";
582 }
583 }
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'){
591 if($$hash{$key}[31] eq 'dnat' && $$hash{$key}[28] eq 'ON'){
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 }
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 }
608 }
609 }
610 }