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