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