Firewall: modified DNAT and SNAT rulecreation
[people/teissler/ipfire-2.x.git] / config / firewall / 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 "/usr/lib/firewall/firewall-lib.pl";
49
50 my $configfwdfw = "${General::swroot}/firewall/config";
51 my $configinput = "${General::swroot}/firewall/input";
52 my $configoutgoing = "${General::swroot}/firewall/outgoing";
53 my $p2pfile = "${General::swroot}/firewall/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 --wait -A';
64 my $dnat ='';
65 my $snat ='';
66
67 &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings);
68 &General::readhash("$netsettings", \%defaultNetworks);
69 &General::readhasharray($configfwdfw, \%configfwdfw);
70 &General::readhasharray($configinput, \%configinputfw);
71 &General::readhasharray($configoutgoing, \%configoutgoingfw);
72 &General::readhasharray($configgrp, \%customgrp);
73 &General::get_aliases(\%aliases);
74
75 #check if we have an internetconnection
76 open (CONN,"/var/ipfire/red/iface");
77 my $con = <CONN>;
78 close(CONN);
79 if (-f "/var/ipfire/red/active"){
80 $conexists='on';
81 }
82 open (CONN1,"/var/ipfire/red/local-ipaddress");
83 my $redip = <CONN1>;
84 close(CONN1);
85 #################
86 # DEBUG/TEST #
87 #################
88 my $MODE=0; # 0 - normal operation
89 # 1 - print configline and rules to console
90 #
91 #################
92 my $param=shift;
93
94 if($param eq 'flush'){
95 if ($MODE eq '1'){
96 print " Flushing chains...\n";
97 }
98 &flush;
99 }else{
100 if ($MODE eq '1'){
101 print " Flushing chains...\n";
102 }
103 &flush;
104 if ($MODE eq '1'){
105 print " Preparing rules...\n";
106 }
107 &preparerules;
108 if($MODE eq '0'){
109 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
110 &p2pblock;
111 system ("/usr/sbin/firewall-policy");
112 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
113 &p2pblock;
114 system ("/usr/sbin/firewall-policy");
115 system ("/etc/sysconfig/firewall.local reload");
116 }
117 }
118 }
119 sub flush
120 {
121 system ("iptables --wait -F FORWARDFW");
122 system ("iptables --wait -F INPUTFW");
123 system ("iptables --wait -F OUTGOINGFW");
124 system ("iptables --wait -t nat -F NAT_DESTINATION");
125 system ("iptables --wait -t nat -F NAT_SOURCE");
126 }
127 sub preparerules
128 {
129 if (! -z "${General::swroot}/firewall/config"){
130 &buildrules(\%configfwdfw);
131 }
132 if (! -z "${General::swroot}/firewall/input"){
133 &buildrules(\%configinputfw);
134 }
135 if (! -z "${General::swroot}/firewall/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 my $icmptype;
150 foreach my $key (sort {$a <=> $b} keys %$hash){
151 next if (($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1') && $conexists eq 'off' );
152 $command="iptables --wait -A";
153 if ($$hash{$key}[28] eq 'ON'){
154 $command='iptables --wait -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 $SPORT = &get_port($hash,$key);
217 $SRC_TGT='';
218
219 ##get target prot and port
220 $DPROT=&get_prot($hash,$key);
221
222 if ($DPROT eq ''){$DPROT=' ';}
223 @DPROT=split(",",$DPROT);
224
225 #get time if defined
226 if($$hash{$key}[18] eq 'ON'){
227 my ($time1,$time2,$daylight);
228 my $daylight=$$hash{$key}[28];
229 $time1=&get_time($$hash{$key}[26],$daylight);
230 $time2=&get_time($$hash{$key}[27],$daylight);
231 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
232 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
233 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
234 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
235 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
236 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
237 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
238 $TIME=join(",",@timeframe);
239
240 $TIMEFROM="--timestart $time1 ";
241 $TIMETILL="--timestop $time2 ";
242 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
243 }
244 if ($MODE eq '1'){
245 print "NR:$key ";
246 foreach my $i (0 .. $#{$$hash{$key}}){
247 print "$i: $$hash{$key}[$i] ";
248 }
249 print "\n";
250 print"##################################\n";
251 #print rules to console
252 foreach my $DPROT (@DPROT){
253 $DPORT = &get_port($hash,$key,$DPROT);
254 if ($DPROT ne 'TCP' && $DPROT ne 'UDP' && $DPROT ne 'ICMP' ){
255 $DPORT='';
256 }
257 $PROT=$DPROT;
258 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
259 foreach my $a (sort keys %sourcehash){
260 foreach my $b (sort keys %targethash){
261 next if ($targethash{$b}[0] eq 'none');
262 $STAG='';
263 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'){
264 if($DPROT ne ''){
265 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
266 #Process ICMP RULE
267 if(substr($DPORT, 2, 4) eq 'icmp'){
268 my @icmprule= split(",",substr($DPORT, 12,));
269 foreach (@icmprule){
270 $icmptype="--icmp-type ";
271 if ($_ eq "BLANK") {
272 $icmptype="";
273 $_="";
274 }
275 if ($$hash{$key}[17] eq 'ON'){
276 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j LOG\n";
277 }
278 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j $$hash{$key}[0]\n";
279 }
280 #PROCESS DNAT RULE (Portforward)
281 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
282 $natchain='NAT_DESTINATION';
283 if ($$hash{$key}[17] eq 'ON'){
284 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
285 }
286 my ($ip,$sub) =split("/",$targethash{$b}[0]);
287 #Process NAT with servicegroup used
288 if ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[14] eq 'cust_srvgrp'){
289 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip $DPORT\n";
290 $fwaccessdport=$DPORT;
291 }else{
292 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip$DPORT\n";
293 $DPORT =~ s/\-/:/g;
294 if ($DPORT){
295 $fwaccessdport="--dport ".substr($DPORT,1,);
296 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
297 if ($$hash{$key}[30]=~m/|/i){
298 $$hash{$key}[30] =~ s/\|/,/g;
299 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
300 }else{
301 $fwaccessdport="--dport $$hash{$key}[30]";
302 }
303 }
304 }
305 print "iptables --wait -A FORWARDFW $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
306 next;
307 #PROCESS SNAT RULE
308 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
309 $natchain='NAT_SOURCE';
310 if ($$hash{$key}[17] eq 'ON' ){
311 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT' \n";
312 }
313 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to-source $natip\n";
314 }
315 #PROCESS EVERY OTHER RULE (If NOT ICMP, else the rule would be applied double)
316 if ($PROT ne '-p ICMP'){
317 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
318 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
319 }
320 print "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
321 }
322 #PROCESS Prot ICMP and type = All ICMP-Types
323 if ($PROT eq '-p ICMP' && $$hash{$key}[9] eq 'All ICMP-Types'){
324 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
325 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
326 }
327 print "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
328 }
329 }
330 }
331 }
332 }
333 print"\n";
334 }
335 }elsif($MODE eq '0'){
336 foreach my $DPROT (@DPROT){
337 $DPORT = &get_port($hash,$key,$DPROT);
338 $PROT=$DPROT;
339 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
340 if ($DPROT ne 'TCP' && $DPROT ne'UDP' && $DPROT ne 'ICMP' ){
341 $DPORT='';
342 }
343 foreach my $a (sort keys %sourcehash){
344 foreach my $b (sort keys %targethash){
345 next if ($targethash{$b}[0] eq 'none');
346 $STAG='';
347 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'){
348 if($DPROT ne ''){
349 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
350 #Process ICMP RULE
351 if(substr($DPORT, 2, 4) eq 'icmp'){
352 my @icmprule= split(",",substr($DPORT, 12,));
353 foreach (@icmprule){
354 $icmptype="--icmp-type ";
355 if ($_ eq "BLANK") {
356 $icmptype="";
357 $_="";
358 }
359 if ($$hash{$key}[17] eq 'ON'){
360 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j LOG");
361 }
362 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j $$hash{$key}[0]");
363 }
364 #PROCESS DNAT RULE (Portforward)
365 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
366 $natchain='NAT_DESTINATION';
367 if ($$hash{$key}[17] eq 'ON'){
368 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
369 }
370 my ($ip,$sub) =split("/",$targethash{$b}[0]);
371 #Process NAT with servicegroup used
372 if ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[14] eq 'cust_srvgrp'){
373 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip $DPORT\n";
374 $fwaccessdport=$DPORT;
375 }else{
376 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip$DPORT\n";
377 $DPORT =~ s/\-/:/g;
378 if ($DPORT){
379 $fwaccessdport="--dport ".substr($DPORT,1,);
380 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
381 if ($$hash{$key}[30]=~m/|/i){
382 $$hash{$key}[30] =~ s/\|/,/g;
383 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
384 }else{
385 $fwaccessdport="--dport $$hash{$key}[30]";
386 }
387 }
388 }
389 system "iptables --wait -A FORWARDFW $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
390 next;
391 #PROCESS SNAT RULE
392 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
393 $natchain='NAT_SOURCE';
394 if ($$hash{$key}[17] eq 'ON' ){
395 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT' \n";
396 }
397 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to-source $natip\n";
398 }
399 #PROCESS EVERY OTHER RULE (If NOT ICMP, else the rule would be applied double)
400 if ($PROT ne '-p ICMP'){
401 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
402 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
403 }
404 system "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
405 }
406 #PROCESS Prot ICMP and type = All ICMP-Types
407 if ($PROT eq '-p ICMP' && $$hash{$key}[9] eq 'All ICMP-Types'){
408 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
409 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
410 }
411 system "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
412 }
413 }
414 }
415 }
416 }
417 }
418 }
419 }
420 %sourcehash=();
421 %targethash=();
422 undef $TIME;
423 undef $TIMEFROM;
424 undef $TIMETILL;
425 undef $fireport;
426 }
427 }
428 sub get_nat_ip
429 {
430 my $val=shift;
431 my $type=shift;
432 my $result;
433 if($val eq 'RED' || $val eq 'GREEN' || $val eq 'ORANGE' || $val eq 'BLUE'){
434 $result=$defaultNetworks{$val.'_ADDRESS'};
435 }elsif($val eq 'ALL'){
436 $result='-i '.$con;
437 }elsif($val eq 'Default IP' && $type eq 'dnat'){
438 $result='-d '.$redip;
439 }elsif($val eq 'Default IP' && $type eq 'snat'){
440 $result=$redip;
441 }else{
442 foreach my $al (sort keys %aliases){
443 if($val eq $al && $type eq 'dnat'){
444 $result='-d '.$aliases{$al}{'IPT'};
445 }elsif($val eq $al && $type eq 'snat'){
446 $result=$aliases{$al}{'IPT'};
447 }
448 }
449 }
450 return $result;
451 }
452 sub get_time
453 {
454 my $val=shift;
455 my $val1=shift;
456 my $time;
457 my $minutes;
458 my $ruletime;
459 $minutes = &utcmin($val);
460 $ruletime = $minutes + &time_get_utc($val);
461 if ($ruletime < 0){$ruletime +=1440;}
462 if ($ruletime > 1440){$ruletime -=1440;}
463 $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60;
464 return $time;
465 }
466 sub time_get_utc
467 {
468 # Calculates the UTCtime from a given time
469 my $val=shift;
470 my @localtime=localtime(time);
471 my @gmtime=gmtime(time);
472 my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60);
473 return $diff;
474 }
475 sub utcmin
476 {
477 my $ruletime=shift;
478 my ($hrs,$min) = split(":",$ruletime);
479 my $newtime = $hrs*60+$min;
480 return $newtime;
481 }
482 sub p2pblock
483 {
484 my $P2PSTRING;
485 my $DO;
486 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
487 @p2ps = <FILE>;
488 close FILE;
489 my $CMD = "-m ipp2p";
490 foreach my $p2pentry (sort @p2ps) {
491 my @p2pline = split( /\;/, $p2pentry );
492 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
493 $DO = "ACCEPT";
494 if ("$p2pline[2]" eq "on") {
495 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
496 }
497 }else {
498 $DO = "RETURN";
499 if ("$p2pline[2]" eq "off") {
500 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
501 }
502 }
503 }
504 if ($MODE eq 1){
505 if($P2PSTRING){
506 print"/sbin/iptables --wait -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
507 }
508 }else{
509 if($P2PSTRING){
510 system("/sbin/iptables --wait -A FORWARDFW $CMD $P2PSTRING -j $DO");
511 }
512 }
513 }
514 sub get_address
515 {
516 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
517 my $base2=shift;
518 my $type=shift; #src or tgt
519 my $hash;
520 if ($type eq 'src'){
521 $hash=\%sourcehash;
522 }else{
523 $hash=\%targethash;
524 }
525 my $key = &General::findhasharraykey($hash);
526 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
527 if (&General::validmac($base2)){
528 $$hash{$key}[0] = "-m mac --mac-source $base2";
529 }else{
530 $$hash{$key}[0] = $base2;
531 }
532 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
533 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2,$con);
534 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
535 $$hash{$key}[0]=&fwlib::get_net_ip($base2);
536 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
537 $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type);
538 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
539 $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1);
540 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
541 $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33);
542 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
543 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,11);
544 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
545 $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11);
546 }elsif($base eq 'ipfire_src' ){
547 if($base2 eq 'GREEN'){
548 $$hash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'};
549 }
550 if($base2 eq 'BLUE'){
551 $$hash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'};
552 }
553 if($base2 eq 'ORANGE'){
554 $$hash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'};
555 }
556 if($base2 eq 'ALL'){
557 $$hash{$key}[0]='0.0.0.0/0';
558 }
559 if($base2 eq 'RED' || $base2 eq 'RED1'){
560 open(FILE, "/var/ipfire/red/local-ipaddress")or die "Couldn't open local-ipaddress";
561 $$hash{$key}[0]= <FILE>;
562 close(FILE);
563 }else{
564 foreach my $alias (sort keys %aliases){
565 if ($base2 eq $alias){
566 $$hash{$key}[0]=$aliases{$alias}{'IPT'};
567 }
568 }
569 }
570 }
571 }
572 sub get_prot
573 {
574 my $hash=shift;
575 my $key=shift;
576 #check AH,GRE,ESP or ICMP
577 if ($$hash{$key}[7] ne 'ON' && $$hash{$key}[11] ne 'ON'){
578 return "$$hash{$key}[8]";
579 }
580 if ($$hash{$key}[7] eq 'ON' || $$hash{$key}[11] eq 'ON'){
581 #check if servicegroup or service
582 if($$hash{$key}[14] eq 'cust_srv'){
583 return &fwlib::get_srv_prot($$hash{$key}[15]);
584 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
585 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
586 }elsif (($$hash{$key}[10] ne '' || $$hash{$key}[15] ne '') && $$hash{$key}[8] eq ''){ #when ports are used and prot set to "all"
587 return "TCP,UDP";
588 }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"
589 return "$$hash{$key}[8]";
590 }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"
591 return "$$hash{$key}[8]";
592 }else{
593 return "$$hash{$key}[8]";
594 }
595 }
596 #DNAT
597 if ($SRC_TGT eq '' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[11] eq '' && $$hash{$key}[12] ne ''){
598 return "$$hash{$key}[8]";
599 }
600 }
601 sub get_port
602 {
603 my $hash=shift;
604 my $key=shift;
605 my $prot=shift;
606 #Get manual defined Ports from SOURCE
607 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
608 if ($$hash{$key}[10] ne ''){
609 $$hash{$key}[10] =~ s/\|/,/g;
610 if(index($$hash{$key}[10],",") > 0){
611 return "-m multiport --sport $$hash{$key}[10] ";
612 }else{
613 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') ){
614 return "--sport $$hash{$key}[10] ";
615 }else{
616 return ":$$hash{$key}[10]";
617 }
618 }
619 }
620 #Get manual ports from TARGET
621 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
622 if($$hash{$key}[14] eq 'TGT_PORT'){
623 if ($$hash{$key}[15] ne ''){
624 $$hash{$key}[15] =~ s/\|/,/g;
625 if(index($$hash{$key}[15],",") > 0){
626 return "-m multiport --dport $$hash{$key}[15] ";
627 }else{
628 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ){
629 return "--dport $$hash{$key}[15] ";
630 }else{
631 $$hash{$key}[15] =~ s/\:/-/g;
632 return ":$$hash{$key}[15]";
633 }
634 }
635 }
636 #Get ports defined in custom Service (firewall-groups)
637 }elsif($$hash{$key}[14] eq 'cust_srv'){
638 if ($prot ne 'ICMP'){
639 if($$hash{$key}[31] eq 'dnat' && $$hash{$key}[28] eq 'ON'){
640 my $ports =&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
641 $ports =~ s/\:/-/g;
642 return ":".$ports
643 }else{
644 return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
645 }
646 }elsif($prot eq 'ICMP' && $$hash{$key}[11] eq 'ON'){ #When PROT is ICMP and "use targetport is checked, this is an icmp-service
647 return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot);
648 }
649 #Get ports from services which are used in custom servicegroups (firewall-groups)
650 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
651 if ($prot ne 'ICMP'){
652 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
653 }
654 elsif($prot eq 'ICMP'){
655 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
656 }
657 }
658 }
659 #CHECK ICMP
660 if ($$hash{$key}[7] ne 'ON' && $$hash{$key}[11] ne 'ON' && $SRC_TGT eq ''){
661 if($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
662 return "--icmp-type $$hash{$key}[9] ";
663 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
664 return;
665 }
666 }
667 }