]>
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");
125 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
126 system ("/usr/sbin/firewall-policy");
132 system ("iptables -F FORWARDFW");
133 system ("iptables -F INPUTFW");
134 system ("iptables -F OUTGOINGFW");
138 if (! -z
"${General::swroot}/forward/dmz"){
139 &buildrules
(\
%configdmzfw);
141 if (! -z
"${General::swroot}/forward/config"){
142 &buildrules
(\
%configfwdfw);
144 if (! -z
"${General::swroot}/forward/input"){
145 &buildrules
(\
%configinputfw);
147 if (! -z
"${General::swroot}/forward/outgoing"){
148 &buildrules
(\
%configoutgoingfw);
155 foreach my $key (sort {$a <=> $b} keys %$hash){
157 if($$hash{$key}[2] eq 'ON'){
159 if ($$hash{$key}[3] eq 'cust_grp_src'){
160 foreach my $grp (sort {$a <=> $b} keys %customgrp){
161 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
162 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
166 &get_address
($$hash{$key}[3],$$hash{$key}[4],"src");
169 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
170 foreach my $grp (sort {$a <=> $b} keys %customgrp){
171 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
172 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
175 }elsif($$hash{$key}[5] eq 'ipfire'){
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
>;
181 foreach my $alias (sort keys %aliases){
182 if ($$hash{$key}[6] eq $alias){
183 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
188 &get_address
($$hash{$key}[5],$$hash{$key}[6],"tgt");
190 ##get source prot and port
192 $SPROT = &get_prot
($hash,$key);
193 $SPORT = &get_port
($hash,$key);
196 ##get target prot and port
197 $DPROT=&get_prot
($hash,$key);
199 if ($DPROT eq ''){$DPROT=' ';}
200 @DPROT=split(",",$DPROT);
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";
218 foreach my $i (0 .. $#{$$hash{$key}}){
219 print "$i: $$hash{$key}[$i] ";
222 print"##################################\n";
223 #print rules to console
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){
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'){
231 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
232 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
233 if ($$hash{$key}[17] eq 'ON'){
234 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
236 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
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){
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'){
251 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
252 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
253 if ($$hash{$key}[17] eq 'ON'){
254 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
256 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
275 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
278 my $CMD = "-m ipp2p";
279 foreach my $p2pentry (sort @p2ps) {
280 my @p2pline = split( /\;/, $p2pentry );
281 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
283 if ("$p2pline[2]" eq "on") {
284 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
288 if ("$p2pline[2]" eq "off") {
289 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
295 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
299 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
306 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
308 my $type=shift; #src or tgt
315 my $key = &General
::findhasharraykey
($hash);
316 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
317 if (&General
::validmac
($base2)){
318 $$hash{$key}[0] = "-m mac --mac-source $base2";
320 $$hash{$key}[0] = $base2;
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);
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]";
348 return "$$hash{$key}[8]";
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]";
357 return "$$hash{$key}[12]";
359 }elsif($$hash{$key}[14] eq 'cust_srv'){
360 return &fwlib
::get_srv_prot
($$hash{$key}[15]);
362 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
363 return &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
372 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
373 if ($$hash{$key}[10] ne ''){
374 $$hash{$key}[10] =~ s/\|/,/g;
375 if(index($$hash{$key}[10],",") > 0){
376 return "-m multiport --sport $$hash{$key}[10] ";
378 return "--sport $$hash{$key}[10] ";
380 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
381 return "--icmp-type $$hash{$key}[9] ";
382 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
385 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
387 if($$hash{$key}[14] eq 'TGT_PORT'){
388 if ($$hash{$key}[15] ne ''){
389 $$hash{$key}[15] =~ s/\|/,/g;
390 if(index($$hash{$key}[15],",") > 0){
391 return "-m multiport --dport $$hash{$key}[15] ";
393 return "--dport $$hash{$key}[15] ";
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'){
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'){
408 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
409 if ($prot ne 'ICMP'){
410 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);
412 elsif($prot eq 'ICMP'){
413 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);