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