]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/firewall/rules.pl
Firewall: Fix errormessages on rulecreation when red has no IP
[people/teissler/ipfire-2.x.git] / config / firewall / 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";
73372ed4 48require "/usr/lib/firewall/firewall-lib.pl";
2a81ab0d 49
6d8eb5de
AM
50my $configfwdfw = "${General::swroot}/firewall/config";
51my $configinput = "${General::swroot}/firewall/input";
52my $configoutgoing = "${General::swroot}/firewall/outgoing";
53my $p2pfile = "${General::swroot}/firewall/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';
a211fee3 63my $command = 'iptables --wait -A';
86a921ee
AM
64my $dnat ='';
65my $snat ='';
66
6d8eb5de 67&General::readhash("${General::swroot}/firewall/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
14bcb9a2
AM
89 # 1 - print configline and rules to console
90 #
86a921ee 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;
5d7faa45 114 system ("/usr/sbin/firewall-policy");
674f4e9d 115 system ("/etc/sysconfig/firewall.local reload");
2a81ab0d
AM
116 }
117 }
118}
2a81ab0d
AM
119sub flush
120{
a211fee3
MT
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");
86a921ee 126}
2a81ab0d
AM
127sub preparerules
128{
6d8eb5de 129 if (! -z "${General::swroot}/firewall/config"){
2a81ab0d
AM
130 &buildrules(\%configfwdfw);
131 }
6d8eb5de 132 if (! -z "${General::swroot}/firewall/input"){
2a81ab0d
AM
133 &buildrules(\%configinputfw);
134 }
6d8eb5de 135 if (! -z "${General::swroot}/firewall/outgoing"){
5d7faa45
AM
136 &buildrules(\%configoutgoingfw);
137 }
2a81ab0d
AM
138}
139sub buildrules
140{
141 my $hash=shift;
b5269091 142 my $STAG;
a6edca5a
AM
143 my $natip;
144 my $snatport;
145 my $fireport;
bc912c6e 146 my $nat;
98cee89f 147 my $fwaccessdport;
c12392c0 148 my $natchain;
2aeb4b25 149 my $icmptype;
992394d5 150 foreach my $key (sort {$a <=> $b} keys %$hash){
ff4770c7 151 next if (($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1') && $conexists eq 'off' );
a211fee3 152 $command="iptables --wait -A";
a6edca5a 153 if ($$hash{$key}[28] eq 'ON'){
a211fee3 154 $command='iptables --wait -t nat -A';
08e1c65d 155 $natip=&get_nat_ip($$hash{$key}[29],$$hash{$key}[31]);
a6edca5a 156 if($$hash{$key}[31] eq 'dnat'){
bc912c6e 157 $nat='DNAT';
98cee89f
AM
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 }
a6edca5a 164 }else{
bc912c6e 165 $nat='SNAT';
a6edca5a
AM
166 }
167 }
b5269091 168 $STAG='';
2a81ab0d
AM
169 if($$hash{$key}[2] eq 'ON'){
170 #get source ip's
171 if ($$hash{$key}[3] eq 'cust_grp_src'){
992394d5 172 foreach my $grp (sort {$a <=> $b} keys %customgrp){
2a81ab0d
AM
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'){
992394d5 182 foreach my $grp (sort {$a <=> $b} keys %customgrp){
2a81ab0d
AM
183 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
184 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
185 }
186 }
a0fb1099 187 }elsif($$hash{$key}[5] eq 'ipfire' ){
05d4f131
AM
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 }
8762442c
AM
197 if($$hash{$key}[6] eq 'ALL'){
198 $targethash{$key}[0]='0.0.0.0/0';
199 }
690b0bd7 200 if($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1'){
ff4770c7 201 open(FILE, "/var/ipfire/red/local-ipaddress")or die "Couldn't open local-ipaddress";
2a81ab0d
AM
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 }
2a81ab0d
AM
214 ##get source prot and port
215 $SRC_TGT='SRC';
2a81ab0d
AM
216 $SPORT = &get_port($hash,$key);
217 $SRC_TGT='';
14f7cb87 218
2a81ab0d
AM
219 ##get target prot and port
220 $DPROT=&get_prot($hash,$key);
14f7cb87 221
a4c7bf6b 222 if ($DPROT eq ''){$DPROT=' ';}
2a81ab0d 223 @DPROT=split(",",$DPROT);
14f7cb87 224
2a81ab0d
AM
225 #get time if defined
226 if($$hash{$key}[18] eq 'ON'){
472136c9
AM
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);
2a81ab0d
AM
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);
86a921ee 239
472136c9
AM
240 $TIMEFROM="--timestart $time1 ";
241 $TIMETILL="--timestop $time2 ";
a0f267b9 242 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
2a81ab0d 243 }
86a921ee 244 if ($MODE eq '1'){
2a81ab0d
AM
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
2a81ab0d
AM
252 foreach my $DPROT (@DPROT){
253 $DPORT = &get_port($hash,$key,$DPROT);
82b837cf
AM
254 if ($DPROT ne 'TCP' && $DPROT ne 'UDP' && $DPROT ne 'ICMP' ){
255 $DPORT='';
256 }
a4c7bf6b 257 $PROT=$DPROT;
3af3ecd3 258 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
2a81ab0d
AM
259 foreach my $a (sort keys %sourcehash){
260 foreach my $b (sort keys %targethash){
f942937c
AM
261 if(! $sourcehash{$a}[0] || ! $targethash{$b}[0] || ($natip eq '-d ' && $$hash{$key}[28] eq 'ON') || (!$natip && $$hash{$key}[28] eq 'ON')){
262 #Skip rules when no RED IP is set (DHCP,DSL)
263 next;
264 }
d334d7cb
AM
265 next if ($targethash{$b}[0] eq 'none');
266 $STAG='';
d7dc9718 267 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 268 if($DPROT ne ''){
5d7faa45 269 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
cdb3536b 270 #Process ICMP RULE
8cb1afc8
AM
271 if(substr($DPORT, 2, 4) eq 'icmp'){
272 my @icmprule= split(",",substr($DPORT, 12,));
273 foreach (@icmprule){
2aeb4b25
AM
274 $icmptype="--icmp-type ";
275 if ($_ eq "BLANK") {
276 $icmptype="";
277 $_="";
278 }
8cb1afc8 279 if ($$hash{$key}[17] eq 'ON'){
2aeb4b25 280 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j LOG\n";
8cb1afc8 281 }
2aeb4b25 282 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j $$hash{$key}[0]\n";
8cb1afc8 283 }
cdb3536b 284 #PROCESS DNAT RULE (Portforward)
28640b73 285 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
c12392c0 286 $natchain='NAT_DESTINATION';
28640b73 287 if ($$hash{$key}[17] eq 'ON'){
cdb3536b 288 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
28640b73 289 }
28640b73 290 my ($ip,$sub) =split("/",$targethash{$b}[0]);
f0befbc3
AM
291 #Process NAT with servicegroup used
292 if ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[14] eq 'cust_srvgrp'){
525204e0 293 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip $DPORT\n";
f0befbc3
AM
294 $fwaccessdport=$DPORT;
295 }else{
525204e0 296 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip$DPORT\n";
f0befbc3
AM
297 $DPORT =~ s/\-/:/g;
298 if ($DPORT){
299 $fwaccessdport="--dport ".substr($DPORT,1,);
300 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
301 if ($$hash{$key}[30]=~m/|/i){
302 $$hash{$key}[30] =~ s/\|/,/g;
303 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
304 }else{
305 $fwaccessdport="--dport $$hash{$key}[30]";
306 }
98cee89f
AM
307 }
308 }
97bf45e5 309 print "iptables --wait -A FORWARDFW $PROT $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
c12392c0 310 next;
cdb3536b 311 #PROCESS SNAT RULE
08e1c65d 312 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
c12392c0 313 $natchain='NAT_SOURCE';
cdb3536b
AM
314 if ($$hash{$key}[17] eq 'ON' ){
315 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT' \n";
316 }
525204e0 317 print "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to-source $natip\n";
2a81ab0d 318 }
cdb3536b 319 #PROCESS EVERY OTHER RULE (If NOT ICMP, else the rule would be applied double)
93c2de1c 320 if ($PROT ne '-p ICMP'){
cdb3536b
AM
321 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
322 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
323 }
a211fee3 324 print "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
93c2de1c 325 }
cdb3536b 326 #PROCESS Prot ICMP and type = All ICMP-Types
86a921ee 327 if ($PROT eq '-p ICMP' && $$hash{$key}[9] eq 'All ICMP-Types'){
cdb3536b
AM
328 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
329 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
330 }
a211fee3 331 print "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
86a921ee
AM
332 }
333 }
2a81ab0d
AM
334 }
335 }
336 }
337 print"\n";
338 }
2a81ab0d
AM
339 }elsif($MODE eq '0'){
340 foreach my $DPROT (@DPROT){
341 $DPORT = &get_port($hash,$key,$DPROT);
a4c7bf6b 342 $PROT=$DPROT;
3af3ecd3 343 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
82b837cf
AM
344 if ($DPROT ne 'TCP' && $DPROT ne'UDP' && $DPROT ne 'ICMP' ){
345 $DPORT='';
346 }
2a81ab0d
AM
347 foreach my $a (sort keys %sourcehash){
348 foreach my $b (sort keys %targethash){
f942937c
AM
349 if(! $sourcehash{$a}[0] || ! $targethash{$b}[0] || $natip eq '-d ' || !$natip){
350 #Skip rules when no RED IP is set (DHCP,DSL)
351 next;
352 }
d334d7cb
AM
353 next if ($targethash{$b}[0] eq 'none');
354 $STAG='';
d7dc9718 355 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 356 if($DPROT ne ''){
5d7faa45 357 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
93c2de1c 358 #Process ICMP RULE
8cb1afc8
AM
359 if(substr($DPORT, 2, 4) eq 'icmp'){
360 my @icmprule= split(",",substr($DPORT, 12,));
361 foreach (@icmprule){
2aeb4b25
AM
362 $icmptype="--icmp-type ";
363 if ($_ eq "BLANK") {
364 $icmptype="";
365 $_="";
366 }
8cb1afc8 367 if ($$hash{$key}[17] eq 'ON'){
2aeb4b25 368 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j LOG");
8cb1afc8 369 }
2aeb4b25 370 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $icmptype $_ $TIME -j $$hash{$key}[0]");
a6edca5a 371 }
93c2de1c 372 #PROCESS DNAT RULE (Portforward)
a6edca5a 373 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
c12392c0 374 $natchain='NAT_DESTINATION';
a6edca5a 375 if ($$hash{$key}[17] eq 'ON'){
cdb3536b 376 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
8cb1afc8 377 }
a6edca5a 378 my ($ip,$sub) =split("/",$targethash{$b}[0]);
f0befbc3
AM
379 #Process NAT with servicegroup used
380 if ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[14] eq 'cust_srvgrp'){
525204e0 381 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip $DPORT\n";
f0befbc3
AM
382 $fwaccessdport=$DPORT;
383 }else{
525204e0 384 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to-destination $ip$DPORT\n";
f0befbc3
AM
385 $DPORT =~ s/\-/:/g;
386 if ($DPORT){
387 $fwaccessdport="--dport ".substr($DPORT,1,);
388 }elsif(! $DPORT && $$hash{$key}[30] ne ''){
389 if ($$hash{$key}[30]=~m/|/i){
390 $$hash{$key}[30] =~ s/\|/,/g;
391 $fwaccessdport="-m multiport --dport $$hash{$key}[30]";
392 }else{
393 $fwaccessdport="--dport $$hash{$key}[30]";
394 }
98cee89f
AM
395 }
396 }
97bf45e5 397 system "iptables --wait -A FORWARDFW $PROT $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n";
c12392c0 398 next;
93c2de1c 399 #PROCESS SNAT RULE
a6edca5a 400 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
c12392c0 401 $natchain='NAT_SOURCE';
cdb3536b
AM
402 if ($$hash{$key}[17] eq 'ON' ){
403 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT' \n";
404 }
525204e0 405 system "$command $natchain $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to-source $natip\n";
c12392c0 406 }
93c2de1c
AM
407 #PROCESS EVERY OTHER RULE (If NOT ICMP, else the rule would be applied double)
408 if ($PROT ne '-p ICMP'){
cdb3536b
AM
409 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
410 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
411 }
a211fee3 412 system "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
93c2de1c 413 }
86a921ee
AM
414 #PROCESS Prot ICMP and type = All ICMP-Types
415 if ($PROT eq '-p ICMP' && $$hash{$key}[9] eq 'All ICMP-Types'){
cdb3536b
AM
416 if ($$hash{$key}[17] eq 'ON' && $$hash{$key}[28] ne 'ON'){
417 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
418 }
a211fee3 419 system "iptables --wait -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
86a921ee
AM
420 }
421 }
2a81ab0d
AM
422 }
423 }
424 }
2a81ab0d
AM
425 }
426 }
427 }
428 %sourcehash=();
429 %targethash=();
430 undef $TIME;
431 undef $TIMEFROM;
432 undef $TIMETILL;
a6edca5a 433 undef $fireport;
2a81ab0d
AM
434 }
435}
a6edca5a
AM
436sub get_nat_ip
437{
438 my $val=shift;
08e1c65d 439 my $type=shift;
a6edca5a
AM
440 my $result;
441 if($val eq 'RED' || $val eq 'GREEN' || $val eq 'ORANGE' || $val eq 'BLUE'){
442 $result=$defaultNetworks{$val.'_ADDRESS'};
443 }elsif($val eq 'ALL'){
444 $result='-i '.$con;
08e1c65d 445 }elsif($val eq 'Default IP' && $type eq 'dnat'){
a6edca5a 446 $result='-d '.$redip;
08e1c65d
AM
447 }elsif($val eq 'Default IP' && $type eq 'snat'){
448 $result=$redip;
a6edca5a
AM
449 }else{
450 foreach my $al (sort keys %aliases){
08e1c65d 451 if($val eq $al && $type eq 'dnat'){
a6edca5a 452 $result='-d '.$aliases{$al}{'IPT'};
08e1c65d
AM
453 }elsif($val eq $al && $type eq 'snat'){
454 $result=$aliases{$al}{'IPT'};
a6edca5a
AM
455 }
456 }
457 }
458 return $result;
459}
472136c9
AM
460sub get_time
461{
462 my $val=shift;
463 my $val1=shift;
464 my $time;
465 my $minutes;
466 my $ruletime;
467 $minutes = &utcmin($val);
468 $ruletime = $minutes + &time_get_utc($val);
469 if ($ruletime < 0){$ruletime +=1440;}
470 if ($ruletime > 1440){$ruletime -=1440;}
471 $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60;
472 return $time;
473}
474sub time_get_utc
475{
476 # Calculates the UTCtime from a given time
477 my $val=shift;
478 my @localtime=localtime(time);
479 my @gmtime=gmtime(time);
480 my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60);
481 return $diff;
482}
483sub utcmin
484{
485 my $ruletime=shift;
486 my ($hrs,$min) = split(":",$ruletime);
487 my $newtime = $hrs*60+$min;
488 return $newtime;
489}
36196d0d
AM
490sub p2pblock
491{
492 my $P2PSTRING;
493 my $DO;
494 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
495 @p2ps = <FILE>;
496 close FILE;
497 my $CMD = "-m ipp2p";
498 foreach my $p2pentry (sort @p2ps) {
499 my @p2pline = split( /\;/, $p2pentry );
8d1beadc
AM
500 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
501 $DO = "ACCEPT";
5238a871 502 if ("$p2pline[2]" eq "on") {
36196d0d
AM
503 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
504 }
8d1beadc 505 }else {
36196d0d 506 $DO = "RETURN";
5238a871 507 if ("$p2pline[2]" eq "off") {
36196d0d
AM
508 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
509 }
510 }
511 }
512 if ($MODE eq 1){
513 if($P2PSTRING){
a211fee3 514 print"/sbin/iptables --wait -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
36196d0d
AM
515 }
516 }else{
517 if($P2PSTRING){
a211fee3 518 system("/sbin/iptables --wait -A FORWARDFW $CMD $P2PSTRING -j $DO");
36196d0d
AM
519 }
520 }
521}
2a81ab0d
AM
522sub get_address
523{
524 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
525 my $base2=shift;
526 my $type=shift; #src or tgt
527 my $hash;
528 if ($type eq 'src'){
86a921ee 529 $hash=\%sourcehash;
2a81ab0d
AM
530 }else{
531 $hash=\%targethash;
532 }
533 my $key = &General::findhasharraykey($hash);
534 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
b5269091
AM
535 if (&General::validmac($base2)){
536 $$hash{$key}[0] = "-m mac --mac-source $base2";
537 }else{
538 $$hash{$key}[0] = $base2;
539 }
2a81ab0d 540 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
ddcec9d3 541 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2,$con);
2a81ab0d
AM
542 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
543 $$hash{$key}[0]=&fwlib::get_net_ip($base2);
544 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
545 $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type);
546 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
547 $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1);
548 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
549 $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33);
550 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
6fab5bca 551 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,11);
2a81ab0d
AM
552 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
553 $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11);
a0fb1099
AM
554 }elsif($base eq 'ipfire_src' ){
555 if($base2 eq 'GREEN'){
556 $$hash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'};
557 }
558 if($base2 eq 'BLUE'){
559 $$hash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'};
560 }
561 if($base2 eq 'ORANGE'){
562 $$hash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'};
563 }
564 if($base2 eq 'ALL'){
565 $$hash{$key}[0]='0.0.0.0/0';
566 }
567 if($base2 eq 'RED' || $base2 eq 'RED1'){
f942937c 568 open(FILE, "/var/ipfire/red/local-ipaddress");
a0fb1099
AM
569 $$hash{$key}[0]= <FILE>;
570 close(FILE);
571 }else{
572 foreach my $alias (sort keys %aliases){
573 if ($base2 eq $alias){
574 $$hash{$key}[0]=$aliases{$alias}{'IPT'};
575 }
576 }
577 }
2a81ab0d
AM
578 }
579}
580sub get_prot
581{
582 my $hash=shift;
583 my $key=shift;
a4c7bf6b
AM
584 #check AH,GRE,ESP or ICMP
585 if ($$hash{$key}[7] ne 'ON' && $$hash{$key}[11] ne 'ON'){
586 return "$$hash{$key}[8]";
587 }
588 if ($$hash{$key}[7] eq 'ON' || $$hash{$key}[11] eq 'ON'){
589 #check if servicegroup or service
590 if($$hash{$key}[14] eq 'cust_srv'){
2a81ab0d 591 return &fwlib::get_srv_prot($$hash{$key}[15]);
2a81ab0d
AM
592 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
593 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
a4c7bf6b
AM
594 }elsif (($$hash{$key}[10] ne '' || $$hash{$key}[15] ne '') && $$hash{$key}[8] eq ''){ #when ports are used and prot set to "all"
595 return "TCP,UDP";
596 }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"
597 return "$$hash{$key}[8]";
598 }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"
599 return "$$hash{$key}[8]";
600 }else{
601 return "$$hash{$key}[8]";
2a81ab0d
AM
602 }
603 }
98cee89f
AM
604 #DNAT
605 if ($SRC_TGT eq '' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[11] eq '' && $$hash{$key}[12] ne ''){
fadcfb73 606 return "$$hash{$key}[8]";
98cee89f 607 }
2a81ab0d
AM
608}
609sub get_port
610{
611 my $hash=shift;
612 my $key=shift;
613 my $prot=shift;
14bcb9a2 614 #Get manual defined Ports from SOURCE
2a81ab0d
AM
615 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
616 if ($$hash{$key}[10] ne ''){
8f0b047b 617 $$hash{$key}[10] =~ s/\|/,/g;
93a5f4a5
AM
618 if(index($$hash{$key}[10],",") > 0){
619 return "-m multiport --sport $$hash{$key}[10] ";
620 }else{
a6edca5a
AM
621 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') ){
622 return "--sport $$hash{$key}[10] ";
623 }else{
624 return ":$$hash{$key}[10]";
625 }
93a5f4a5 626 }
2a81ab0d 627 }
14bcb9a2 628 #Get manual ports from TARGET
2a81ab0d 629 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
2a81ab0d
AM
630 if($$hash{$key}[14] eq 'TGT_PORT'){
631 if ($$hash{$key}[15] ne ''){
8f0b047b 632 $$hash{$key}[15] =~ s/\|/,/g;
93a5f4a5
AM
633 if(index($$hash{$key}[15],",") > 0){
634 return "-m multiport --dport $$hash{$key}[15] ";
635 }else{
a6edca5a
AM
636 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ){
637 return "--dport $$hash{$key}[15] ";
638 }else{
829697d0 639 $$hash{$key}[15] =~ s/\:/-/g;
653a71b9 640 return ":$$hash{$key}[15]";
a6edca5a 641 }
93a5f4a5 642 }
2a81ab0d 643 }
14bcb9a2 644 #Get ports defined in custom Service (firewall-groups)
2a81ab0d
AM
645 }elsif($$hash{$key}[14] eq 'cust_srv'){
646 if ($prot ne 'ICMP'){
653a71b9 647 if($$hash{$key}[31] eq 'dnat' && $$hash{$key}[28] eq 'ON'){
14bcb9a2
AM
648 my $ports =&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
649 $ports =~ s/\:/-/g;
650 return ":".$ports
6be32fe5
AM
651 }else{
652 return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
653 }
e6e9a811
AM
654 }elsif($prot eq 'ICMP' && $$hash{$key}[11] eq 'ON'){ #When PROT is ICMP and "use targetport is checked, this is an icmp-service
655 return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot);
2a81ab0d 656 }
14bcb9a2 657 #Get ports from services which are used in custom servicegroups (firewall-groups)
2a81ab0d
AM
658 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
659 if ($prot ne 'ICMP'){
660 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
661 }
662 elsif($prot eq 'ICMP'){
663 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
664 }
2a81ab0d
AM
665 }
666 }
a4c7bf6b
AM
667 #CHECK ICMP
668 if ($$hash{$key}[7] ne 'ON' && $$hash{$key}[11] ne 'ON' && $SRC_TGT eq ''){
669 if($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
670 return "--icmp-type $$hash{$key}[9] ";
671 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
672 return;
673 }
674 }
2a81ab0d 675}