Forward Firewall: Deleted MODE0 from WEB Interface and added a table for DMZ-Rules.
[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 #
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
2a81ab0d
AM
27use strict;
28no warnings 'uninitialized';
29
30# enable only the following on debugging purpose
31#use warnings;
32#use CGI::Carp 'fatalsToBrowser';
33
34my %fwdfwsettings=();
35my %defaultNetworks=();
36my %configfwdfw=();
37my %color=();
38my %icmptypes=();
39my %ovpnSettings=();
40my %customgrp=();
41our %sourcehash=();
42our %targethash=();
43my @timeframe=();
44my %configinputfw=();
45my %aliases=();
46my @DPROT=();
36196d0d 47my @p2ps=();
2a81ab0d
AM
48require '/var/ipfire/general-functions.pl';
49require "${General::swroot}/lang.pl";
50require "${General::swroot}/forward/bin/firewall-lib.pl";
51
52my $configfwdfw = "${General::swroot}/forward/config";
53my $configinput = "${General::swroot}/forward/input";
36196d0d 54my $p2pfile = "${General::swroot}/forward/p2protocols";
2a81ab0d
AM
55my $configgrp = "${General::swroot}/fwhosts/customgroups";
56my $errormessage='';
57my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
58my $CHAIN="FORWARDFW";
59
60
61&General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings);
62&General::readhasharray($configfwdfw, \%configfwdfw);
63&General::readhasharray($configinput, \%configinputfw);
64&General::readhasharray($configgrp, \%customgrp);
65&General::get_aliases(\%aliases);
66
67################################
68# DEBUG/TEST #
69################################
54cb7ff0 70my $MODE=1; # 0 - normal operation
2a81ab0d
AM
71 # 1 - print configline and rules to console
72 #
73################################
74my $param=shift;
75
76if($param eq 'flush'){
77 if ($MODE eq '1'){
78 print " Flushing chains...\n";
79 }
80 &flush;
81}else{
82 if ($MODE eq '1'){
83 print " Flushing chains...\n";
84 }
85 &flush;
86 if ($MODE eq '1'){
87 print " Preparing rules...\n";
88 }
89 &preparerules;
90 if($MODE eq '0'){
91 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
af49e367 92 &p2pblock;
62fc8511 93 system ("/usr/sbin/firewall-forward-policy");
2a81ab0d 94 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
af49e367 95 &p2pblock;
62fc8511 96 system ("/usr/sbin/firewall-forward-policy");
af49e367
AM
97 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
98 }elsif($fwdfwsettings{'POLICY'} eq 'MODE0'){
62fc8511 99 system ("/usr/sbin/firewall-forward-policy");
fd10a52c 100 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
2a81ab0d
AM
101 }
102 }
103}
2a81ab0d
AM
104sub flush
105{
106 system ("iptables -F FORWARDFW");
107 system ("iptables -F INPUTFW");
108}
109sub preparerules
110{
111 if (! -z "${General::swroot}/forward/config"){
112 &buildrules(\%configfwdfw);
113 }
114 if (! -z "${General::swroot}/forward/input"){
115 &buildrules(\%configinputfw);
116 }
117}
118sub buildrules
119{
120 my $hash=shift;
b5269091 121 my $STAG;
992394d5 122 foreach my $key (sort {$a <=> $b} keys %$hash){
b5269091 123 $STAG='';
2a81ab0d
AM
124 if($$hash{$key}[2] eq 'ON'){
125 #get source ip's
126 if ($$hash{$key}[3] eq 'cust_grp_src'){
992394d5 127 foreach my $grp (sort {$a <=> $b} keys %customgrp){
2a81ab0d
AM
128 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
129 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
130 }
131 }
132 }else{
133 &get_address($$hash{$key}[3],$$hash{$key}[4],"src");
134 }
135 #get target ip's
136 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
992394d5 137 foreach my $grp (sort {$a <=> $b} keys %customgrp){
2a81ab0d
AM
138 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
139 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
140 }
141 }
142 }elsif($$hash{$key}[5] eq 'ipfire'){
14f7cb87 143
2a81ab0d
AM
144 if($$hash{$key}[6] eq 'Default IP'){
145 open(FILE, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.';
146 $targethash{$key}[0]= <FILE>;
147 close(FILE);
148 }else{
149 foreach my $alias (sort keys %aliases){
150 if ($$hash{$key}[6] eq $alias){
151 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
152 }
153 }
154 }
155 }else{
156 &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt");
157 }
2a81ab0d
AM
158 ##get source prot and port
159 $SRC_TGT='SRC';
160 $SPROT = &get_prot($hash,$key);
161 $SPORT = &get_port($hash,$key);
162 $SRC_TGT='';
14f7cb87 163
2a81ab0d
AM
164 ##get target prot and port
165 $DPROT=&get_prot($hash,$key);
14f7cb87 166
2a81ab0d
AM
167 if ($DPROT eq ''){$DPROT=' ';}
168 @DPROT=split(",",$DPROT);
14f7cb87 169
2a81ab0d
AM
170 #get time if defined
171 if($$hash{$key}[18] eq 'ON'){
172 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
173 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
174 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
175 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
176 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
177 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
178 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
179 $TIME=join(",",@timeframe);
180 $TIMEFROM="--timestart $$hash{$key}[26] ";
181 $TIMETILL="--timestop $$hash{$key}[27] ";
182 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
183 }
2a81ab0d
AM
184 if ($MODE eq '1'){
185 print "NR:$key ";
186 foreach my $i (0 .. $#{$$hash{$key}}){
187 print "$i: $$hash{$key}[$i] ";
188 }
189 print "\n";
190 print"##################################\n";
191 #print rules to console
2a81ab0d
AM
192 foreach my $DPROT (@DPROT){
193 $DPORT = &get_port($hash,$key,$DPROT);
194 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
195 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
196 foreach my $a (sort keys %sourcehash){
197 foreach my $b (sort keys %targethash){
d7dc9718 198 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'){
2a81ab0d 199 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
54cb7ff0 200 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac'){ $STAG="-s";}
2a81ab0d 201 if ($$hash{$key}[17] eq 'ON'){
b5269091 202 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
2a81ab0d 203 }
b5269091 204 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
2a81ab0d
AM
205 }
206 }
207 }
208 }
209 print"\n";
210 }
2a81ab0d
AM
211 }elsif($MODE eq '0'){
212 foreach my $DPROT (@DPROT){
213 $DPORT = &get_port($hash,$key,$DPROT);
214 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
215 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
216 foreach my $a (sort keys %sourcehash){
217 foreach my $b (sort keys %targethash){
d7dc9718 218 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'){
2a81ab0d 219 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
54cb7ff0 220 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac'){ $STAG="-s";}
2a81ab0d 221 if ($$hash{$key}[17] eq 'ON'){
b5269091 222 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
2a81ab0d 223 }
b5269091 224 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
2a81ab0d
AM
225 }
226 }
227 }
228 }
2a81ab0d
AM
229 }
230 }
231 }
232 %sourcehash=();
233 %targethash=();
234 undef $TIME;
235 undef $TIMEFROM;
236 undef $TIMETILL;
237 }
238}
36196d0d
AM
239sub p2pblock
240{
241 my $P2PSTRING;
242 my $DO;
243 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
244 @p2ps = <FILE>;
245 close FILE;
246 my $CMD = "-m ipp2p";
247 foreach my $p2pentry (sort @p2ps) {
248 my @p2pline = split( /\;/, $p2pentry );
8d1beadc
AM
249 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
250 $DO = "ACCEPT";
5238a871 251 if ("$p2pline[2]" eq "on") {
36196d0d
AM
252 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
253 }
8d1beadc 254 }else {
36196d0d 255 $DO = "RETURN";
5238a871 256 if ("$p2pline[2]" eq "off") {
36196d0d
AM
257 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
258 }
259 }
260 }
261 if ($MODE eq 1){
262 if($P2PSTRING){
263 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
264 }
265 }else{
266 if($P2PSTRING){
267 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
268 }
269 }
270}
271
2a81ab0d
AM
272sub get_address
273{
274 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
275 my $base2=shift;
276 my $type=shift; #src or tgt
277 my $hash;
278 if ($type eq 'src'){
279 $hash=\%sourcehash;
280 }else{
281 $hash=\%targethash;
282 }
283 my $key = &General::findhasharraykey($hash);
284 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
b5269091
AM
285 if (&General::validmac($base2)){
286 $$hash{$key}[0] = "-m mac --mac-source $base2";
287 }else{
288 $$hash{$key}[0] = $base2;
289 }
2a81ab0d
AM
290 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
291 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2);
292 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
293 $$hash{$key}[0]=&fwlib::get_net_ip($base2);
294 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
295 $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type);
296 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
297 $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1);
298 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
299 $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33);
300 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
301 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,27);
302 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
303 $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11);
304 }
305}
306sub get_prot
307{
308 my $hash=shift;
309 my $key=shift;
310 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
311 if ($$hash{$key}[10] ne ''){
312 return"$$hash{$key}[8]";
313 }elsif($$hash{$key}[9] ne ''){
314 return"$$hash{$key}[8]";
315 }else{
316 return "$$hash{$key}[8]";
317 }
318 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
319 if ($$hash{$key}[14] eq 'TGT_PORT'){
320 if ($$hash{$key}[15] ne ''){
321 return "$$hash{$key}[12]";
322 }elsif($$hash{$key}[13] ne ''){
323 return "$$hash{$key}[12]";
324 }else{
325 return "$$hash{$key}[12]";
326 }
327 }elsif($$hash{$key}[14] eq 'cust_srv'){
328 return &fwlib::get_srv_prot($$hash{$key}[15]);
329
330 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
331 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
332 }
333 }
334}
335sub get_port
336{
337 my $hash=shift;
338 my $key=shift;
339 my $prot=shift;
340 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
341 if ($$hash{$key}[10] ne ''){
8f0b047b 342 $$hash{$key}[10] =~ s/\|/,/g;
93a5f4a5
AM
343 if(index($$hash{$key}[10],",") > 0){
344 return "-m multiport --sport $$hash{$key}[10] ";
345 }else{
346 return "--sport $$hash{$key}[10] ";
347 }
62fc8511 348 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
2a81ab0d 349 return "--icmp-type $$hash{$key}[9] ";
62fc8511
AM
350 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
351 return;
2a81ab0d
AM
352 }
353 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
b5269091 354
2a81ab0d
AM
355 if($$hash{$key}[14] eq 'TGT_PORT'){
356 if ($$hash{$key}[15] ne ''){
8f0b047b 357 $$hash{$key}[15] =~ s/\|/,/g;
93a5f4a5
AM
358 if(index($$hash{$key}[15],",") > 0){
359 return "-m multiport --dport $$hash{$key}[15] ";
360 }else{
361 return "--dport $$hash{$key}[15] ";
362 }
2a81ab0d
AM
363 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){
364 return "--icmp-type $$hash{$key}[13] ";
365 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){
366 return;
367 }
368 }elsif($$hash{$key}[14] eq 'cust_srv'){
369 if ($prot ne 'ICMP'){
370 return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
371 }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){
372 return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot);
373 }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){
374 return;
375 }
376 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
377 if ($prot ne 'ICMP'){
378 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
379 }
380 elsif($prot eq 'ICMP'){
381 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
382 }
2a81ab0d
AM
383 }
384 }
385}