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