]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/rules.pl
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
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. #
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. #
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/>. #
20 ###############################################################################
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 ###############################################################################
28 no warnings
'uninitialized';
30 # enable only the following on debugging purpose
32 #use CGI::Carp 'fatalsToBrowser';
35 my %defaultNetworks=();
45 my %configoutgoingfw=();
50 require '/var/ipfire/general-functions.pl';
51 require "${General::swroot}/lang.pl";
52 require "${General::swroot}/forward/bin/firewall-lib.pl";
54 my $configdmz = "${General::swroot}/forward/dmz";
55 my $configfwdfw = "${General::swroot}/forward/config";
56 my $configinput = "${General::swroot}/forward/input";
57 my $configoutgoing = "${General::swroot}/forward/outgoing";
58 my $p2pfile = "${General::swroot}/forward/p2protocols";
59 my $configgrp = "${General::swroot}/fwhosts/customgroups";
60 my $netsettings = "${General::swroot}/ethernet/settings";
65 my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
66 my $CHAIN="FORWARDFW";
69 &General
::readhash
("${General::swroot}/forward/settings", \
%fwdfwsettings);
70 &General
::readhash
("$netsettings", \
%defaultNetworks);
71 &General
::readhasharray
($configdmz, \
%configdmzfw);
72 &General
::readhasharray
($configfwdfw, \
%configfwdfw);
73 &General
::readhasharray
($configinput, \
%configinputfw);
74 &General
::readhasharray
($configoutgoing, \
%configoutgoingfw);
75 &General
::readhasharray
($configgrp, \
%customgrp);
76 &General
::get_aliases
(\
%aliases);
78 ################################
80 ################################
81 my $MODE=1; # 0 - normal operation
82 # 1 - print configline and rules to console
84 ################################
87 if($param eq 'flush'){
89 print " Flushing chains...\n";
94 print " Flushing chains...\n";
98 print " Preparing rules...\n";
102 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
104 system ("/usr/sbin/firewall-policy");
105 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
106 $defaultNetworks{'GREEN_NETMASK'}=&General
::iporsubtocidr
($defaultNetworks{'GREEN_NETMASK'});
107 $green="$defaultNetworks{'GREEN_ADDRESS'}/$defaultNetworks{'GREEN_NETMASK'}";
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");
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");
124 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
125 system ("/usr/sbin/firewall-policy");
131 system ("iptables -F FORWARDFW");
132 system ("iptables -F INPUTFW");
133 system ("iptables -F OUTGOINGFW");
137 if (! -z
"${General::swroot}/forward/dmz"){
138 &buildrules
(\
%configdmzfw);
140 if (! -z
"${General::swroot}/forward/config"){
141 &buildrules
(\
%configfwdfw);
143 if (! -z
"${General::swroot}/forward/input"){
144 &buildrules
(\
%configinputfw);
146 if (! -z
"${General::swroot}/forward/outgoing"){
147 &buildrules
(\
%configoutgoingfw);
154 foreach my $key (sort {$a <=> $b} keys %$hash){
156 if($$hash{$key}[2] eq 'ON'){
158 if ($$hash{$key}[3] eq 'cust_grp_src'){
159 foreach my $grp (sort {$a <=> $b} keys %customgrp){
160 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
161 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
165 &get_address
($$hash{$key}[3],$$hash{$key}[4],"src");
168 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
169 foreach my $grp (sort {$a <=> $b} keys %customgrp){
170 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
171 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
174 }elsif($$hash{$key}[5] eq 'ipfire'){
175 if($$hash{$key}[6] eq 'Default IP'){
176 open(FILE
, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.';
177 $targethash{$key}[0]= <FILE
>;
180 foreach my $alias (sort keys %aliases){
181 if ($$hash{$key}[6] eq $alias){
182 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
187 &get_address
($$hash{$key}[5],$$hash{$key}[6],"tgt");
189 ##get source prot and port
191 $SPROT = &get_prot
($hash,$key);
192 $SPORT = &get_port
($hash,$key);
195 ##get target prot and port
196 $DPROT=&get_prot
($hash,$key);
198 if ($DPROT eq ''){$DPROT=' ';}
199 @DPROT=split(",",$DPROT);
202 if($$hash{$key}[18] eq 'ON'){
203 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
204 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
205 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
206 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
207 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
208 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
209 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
210 $TIME=join(",",@timeframe);
211 $TIMEFROM="--timestart $$hash{$key}[26] ";
212 $TIMETILL="--timestop $$hash{$key}[27] ";
213 $TIME="-m time --kerneltz --weekdays $TIME $TIMEFROM $TIMETILL";
217 foreach my $i (0 .. $#{$$hash{$key}}){
218 print "$i: $$hash{$key}[$i] ";
221 print"##################################\n";
222 #print rules to console
223 foreach my $DPROT (@DPROT){
224 $DPORT = &get_port
($hash,$key,$DPROT);
225 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
226 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
227 foreach my $a (sort keys %sourcehash){
228 foreach my $b (sort keys %targethash){
229 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'){
230 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
231 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
232 if(substr($DPORT, 2, 4) eq 'icmp'){
233 my @icmprule= split(",",substr($DPORT, 12,));
235 if ($$hash{$key}[17] eq 'ON'){
236 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j LOG\n";
238 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]\n";
241 if ($$hash{$key}[17] eq 'ON'){
242 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
244 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
252 }elsif($MODE eq '0'){
253 foreach my $DPROT (@DPROT){
254 $DPORT = &get_port
($hash,$key,$DPROT);
255 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
256 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
257 foreach my $a (sort keys %sourcehash){
258 foreach my $b (sort keys %targethash){
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'){
260 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
261 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
262 if(substr($DPORT, 2, 4) eq 'icmp'){
263 my @icmprule= split(",",substr($DPORT, 12,));
265 if ($$hash{$key}[17] eq 'ON'){
266 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] -- icmp-type $_ $TIME -j LOG");
268 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]");
271 if ($$hash{$key}[17] eq 'ON'){
272 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
274 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
294 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
297 my $CMD = "-m ipp2p";
298 foreach my $p2pentry (sort @p2ps) {
299 my @p2pline = split( /\;/, $p2pentry );
300 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
302 if ("$p2pline[2]" eq "on") {
303 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
307 if ("$p2pline[2]" eq "off") {
308 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
314 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
318 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
325 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
327 my $type=shift; #src or tgt
334 my $key = &General
::findhasharraykey
($hash);
335 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
336 if (&General
::validmac
($base2)){
337 $$hash{$key}[0] = "-m mac --mac-source $base2";
339 $$hash{$key}[0] = $base2;
341 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
342 $$hash{$key}[0]=&fwlib
::get_std_net_ip
($base2);
343 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
344 $$hash{$key}[0]=&fwlib
::get_net_ip
($base2);
345 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
346 $$hash{$key}[0]=&fwlib
::get_host_ip
($base2,$type);
347 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
348 $$hash{$key}[0]=&fwlib
::get_ovpn_net_ip
($base2,1);
349 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
350 $$hash{$key}[0]=&fwlib
::get_ovpn_host_ip
($base2,33);
351 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
352 $$hash{$key}[0]=&fwlib
::get_ovpn_n2n_ip
($base2,27);
353 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
354 $$hash{$key}[0]=&fwlib
::get_ipsec_net_ip
($base2,11);
361 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
362 if ($$hash{$key}[10] ne ''){
363 return"$$hash{$key}[8]";
364 }elsif($$hash{$key}[9] ne ''){
365 return"$$hash{$key}[8]";
367 return "$$hash{$key}[8]";
369 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
370 if ($$hash{$key}[14] eq 'TGT_PORT'){
371 if ($$hash{$key}[15] ne ''){
372 return "$$hash{$key}[12]";
373 }elsif($$hash{$key}[13] ne ''){
374 return "$$hash{$key}[12]";
376 return "$$hash{$key}[12]";
378 }elsif($$hash{$key}[14] eq 'cust_srv'){
379 return &fwlib
::get_srv_prot
($$hash{$key}[15]);
381 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
382 return &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
391 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
392 if ($$hash{$key}[10] ne ''){
393 $$hash{$key}[10] =~ s/\|/,/g;
394 if(index($$hash{$key}[10],",") > 0){
395 return "-m multiport --sport $$hash{$key}[10] ";
397 return "--sport $$hash{$key}[10] ";
399 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
400 return "--icmp-type $$hash{$key}[9] ";
401 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
404 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
406 if($$hash{$key}[14] eq 'TGT_PORT'){
407 if ($$hash{$key}[15] ne ''){
408 $$hash{$key}[15] =~ s/\|/,/g;
409 if(index($$hash{$key}[15],",") > 0){
410 return "-m multiport --dport $$hash{$key}[15] ";
412 return "--dport $$hash{$key}[15] ";
414 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){
415 return "--icmp-type $$hash{$key}[13] ";
416 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){
419 }elsif($$hash{$key}[14] eq 'cust_srv'){
420 if ($prot ne 'ICMP'){
421 return "--dport ".&fwlib
::get_srv_port
($$hash{$key}[15],1,$prot);
422 }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){
423 return "--icmp-type ".&fwlib
::get_srv_port
($$hash{$key}[15],3,$prot);
424 }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){
427 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
428 if ($prot ne 'ICMP'){
429 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);
431 elsif($prot eq 'ICMP'){
432 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);