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