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