]>
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 ###############################################################################
29 no warnings
'uninitialized';
31 # enable only the following on debugging purpose
33 #use CGI::Carp 'fatalsToBrowser';
36 my %defaultNetworks=();
46 my %configoutgoingfw=();
51 require '/var/ipfire/general-functions.pl';
52 require "${General::swroot}/lang.pl";
53 require "${General::swroot}/forward/bin/firewall-lib.pl";
55 my $configdmz = "${General::swroot}/forward/dmz";
56 my $configfwdfw = "${General::swroot}/forward/config";
57 my $configinput = "${General::swroot}/forward/input";
58 my $configoutgoing = "${General::swroot}/forward/outgoing";
59 my $p2pfile = "${General::swroot}/forward/p2protocols";
60 my $configgrp = "${General::swroot}/fwhosts/customgroups";
61 my $netsettings = "${General::swroot}/ethernet/settings";
66 my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
67 my $CHAIN="FORWARDFW";
70 &General
::readhash
("${General::swroot}/forward/settings", \
%fwdfwsettings);
71 &General
::readhash
("$netsettings", \
%defaultNetworks);
72 &General
::readhasharray
($configdmz, \
%configdmzfw);
73 &General
::readhasharray
($configfwdfw, \
%configfwdfw);
74 &General
::readhasharray
($configinput, \
%configinputfw);
75 &General
::readhasharray
($configoutgoing, \
%configoutgoingfw);
76 &General
::readhasharray
($configgrp, \
%customgrp);
77 &General
::get_aliases
(\
%aliases);
79 #check if we have an internetconnection
80 open (CONN
,"/var/ipfire/red/iface");
83 if (-f
"/var/ipfire/red/active"){
87 ################################
89 ################################
90 my $MODE=1; # 0 - normal operation
91 # 1 - print configline and rules to console
93 ################################
96 if($param eq 'flush'){
98 print " Flushing chains...\n";
103 print " Flushing chains...\n";
107 print " Preparing rules...\n";
111 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
113 system ("/usr/sbin/firewall-policy");
114 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
115 $defaultNetworks{'GREEN_NETMASK'}=&General
::iporsubtocidr
($defaultNetworks{'GREEN_NETMASK'});
116 $green="$defaultNetworks{'GREEN_ADDRESS'}/$defaultNetworks{'GREEN_NETMASK'}";
117 if ($defaultNetworks{'BLUE_DEV'}){
118 $defaultNetworks{'BLUE_NETMASK'}=&General
::iporsubtocidr
($defaultNetworks{'BLUE_NETMASK'});
119 $blue="$defaultNetworks{'BLUE_ADDRESS'}/$defaultNetworks{'BLUE_NETMASK'}";
120 #set default rules for BLUE
121 system ("iptables -A $CHAIN -s $blue -d $green -j RETURN");
123 if ($defaultNetworks{'ORANGE_DEV'}){
124 $defaultNetworks{'ORANGE_NETMASK'}=&General
::iporsubtocidr
($defaultNetworks{'ORANGE_NETMASK'});
125 $orange="$defaultNetworks{'ORANGE_ADDRESS'}/$defaultNetworks{'ORANGE_NETMASK'}";
126 #set default rules for DMZ
127 system ("iptables -A $CHAIN -s $orange -d $green -j RETURN");
128 if ($defaultNetworks{'BLUE_DEV'}){
129 system ("iptables -A $CHAIN -s $orange -d $blue -j RETURN");
133 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
134 system ("/usr/sbin/firewall-policy");
140 system ("iptables -F FORWARDFW");
141 system ("iptables -F INPUTFW");
142 system ("iptables -F OUTGOINGFW");
146 if (! -z
"${General::swroot}/forward/dmz"){
147 &buildrules
(\
%configdmzfw);
149 if (! -z
"${General::swroot}/forward/config"){
150 &buildrules
(\
%configfwdfw);
152 if (! -z
"${General::swroot}/forward/input"){
153 &buildrules
(\
%configinputfw);
155 if (! -z
"${General::swroot}/forward/outgoing"){
156 &buildrules
(\
%configoutgoingfw);
163 foreach my $key (sort {$a <=> $b} keys %$hash){
164 next if ($$hash{$key}[6] eq 'RED' && $conexists eq 'off' );
166 if($$hash{$key}[2] eq 'ON'){
168 if ($$hash{$key}[3] eq 'cust_grp_src'){
169 foreach my $grp (sort {$a <=> $b} keys %customgrp){
170 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
171 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
175 &get_address
($$hash{$key}[3],$$hash{$key}[4],"src");
178 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
179 foreach my $grp (sort {$a <=> $b} keys %customgrp){
180 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
181 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
184 }elsif($$hash{$key}[5] eq 'ipfire'){
185 if($$hash{$key}[6] eq 'Default IP'){
186 open(FILE
, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.';
187 $targethash{$key}[0]= <FILE
>;
190 foreach my $alias (sort keys %aliases){
191 if ($$hash{$key}[6] eq $alias){
192 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
197 &get_address
($$hash{$key}[5],$$hash{$key}[6],"tgt");
199 ##get source prot and port
201 $SPROT = &get_prot
($hash,$key);
202 $SPORT = &get_port
($hash,$key);
205 ##get target prot and port
206 $DPROT=&get_prot
($hash,$key);
208 if ($DPROT eq ''){$DPROT=' ';}
209 @DPROT=split(",",$DPROT);
212 if($$hash{$key}[18] eq 'ON'){
213 my ($time1,$time2,$daylight);
214 my $daylight=$$hash{$key}[28];
215 $time1=&get_time
($$hash{$key}[26],$daylight);
216 $time2=&get_time
($$hash{$key}[27],$daylight);
217 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
218 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
219 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
220 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
221 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
222 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
223 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
224 $TIME=join(",",@timeframe);
226 $TIMEFROM="--timestart $time1 ";
227 $TIMETILL="--timestop $time2 ";
228 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
232 foreach my $i (0 .. $#{$$hash{$key}}){
233 print "$i: $$hash{$key}[$i] ";
236 print"##################################\n";
237 #print rules to console
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){
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'){
245 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
246 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
247 if(substr($DPORT, 2, 4) eq 'icmp'){
248 my @icmprule= split(",",substr($DPORT, 12,));
250 if ($$hash{$key}[17] eq 'ON'){
251 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j LOG\n";
253 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]\n";
256 if ($$hash{$key}[17] eq 'ON'){
257 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
259 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
267 }elsif($MODE eq '0'){
268 foreach my $DPROT (@DPROT){
269 $DPORT = &get_port
($hash,$key,$DPROT);
270 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
271 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
272 foreach my $a (sort keys %sourcehash){
273 foreach my $b (sort keys %targethash){
274 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'){
275 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
276 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
277 if(substr($DPORT, 2, 4) eq 'icmp'){
278 my @icmprule= split(",",substr($DPORT, 12,));
280 if ($$hash{$key}[17] eq 'ON'){
281 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] -- icmp-type $_ $TIME -j LOG");
283 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]");
286 if ($$hash{$key}[17] eq 'ON'){
287 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
289 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
312 $minutes = &utcmin
($val);
313 $ruletime = $minutes + &time_get_utc
($val);
314 if ($ruletime < 0){$ruletime +=1440;}
315 if ($ruletime > 1440){$ruletime -=1440;}
316 $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60;
321 # Calculates the UTCtime from a given time
323 my @localtime=localtime(time);
324 my @gmtime=gmtime(time);
325 my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60);
331 my ($hrs,$min) = split(":",$ruletime);
332 my $newtime = $hrs*60+$min;
339 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
342 my $CMD = "-m ipp2p";
343 foreach my $p2pentry (sort @p2ps) {
344 my @p2pline = split( /\;/, $p2pentry );
345 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
347 if ("$p2pline[2]" eq "on") {
348 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
352 if ("$p2pline[2]" eq "off") {
353 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
359 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
363 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
370 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
372 my $type=shift; #src or tgt
379 my $key = &General
::findhasharraykey
($hash);
380 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
381 if (&General
::validmac
($base2)){
382 $$hash{$key}[0] = "-m mac --mac-source $base2";
384 $$hash{$key}[0] = $base2;
386 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
387 $$hash{$key}[0]=&fwlib
::get_std_net_ip
($base2,$con);
388 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
389 $$hash{$key}[0]=&fwlib
::get_net_ip
($base2);
390 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
391 $$hash{$key}[0]=&fwlib
::get_host_ip
($base2,$type);
392 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
393 $$hash{$key}[0]=&fwlib
::get_ovpn_net_ip
($base2,1);
394 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
395 $$hash{$key}[0]=&fwlib
::get_ovpn_host_ip
($base2,33);
396 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
397 $$hash{$key}[0]=&fwlib
::get_ovpn_n2n_ip
($base2,27);
398 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
399 $$hash{$key}[0]=&fwlib
::get_ipsec_net_ip
($base2,11);
406 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
407 if ($$hash{$key}[10] ne ''){
408 return"$$hash{$key}[8]";
409 }elsif($$hash{$key}[9] ne ''){
410 return"$$hash{$key}[8]";
412 return "$$hash{$key}[8]";
414 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
415 if ($$hash{$key}[14] eq 'TGT_PORT'){
416 if ($$hash{$key}[15] ne ''){
417 return "$$hash{$key}[12]";
418 }elsif($$hash{$key}[13] ne ''){
419 return "$$hash{$key}[12]";
421 return "$$hash{$key}[12]";
423 }elsif($$hash{$key}[14] eq 'cust_srv'){
424 return &fwlib
::get_srv_prot
($$hash{$key}[15]);
426 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
427 return &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
436 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
437 if ($$hash{$key}[10] ne ''){
438 $$hash{$key}[10] =~ s/\|/,/g;
439 if(index($$hash{$key}[10],",") > 0){
440 return "-m multiport --sport $$hash{$key}[10] ";
442 return "--sport $$hash{$key}[10] ";
444 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
445 return "--icmp-type $$hash{$key}[9] ";
446 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
449 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
451 if($$hash{$key}[14] eq 'TGT_PORT'){
452 if ($$hash{$key}[15] ne ''){
453 $$hash{$key}[15] =~ s/\|/,/g;
454 if(index($$hash{$key}[15],",") > 0){
455 return "-m multiport --dport $$hash{$key}[15] ";
457 return "--dport $$hash{$key}[15] ";
459 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){
460 return "--icmp-type $$hash{$key}[13] ";
461 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){
464 }elsif($$hash{$key}[14] eq 'cust_srv'){
465 if ($prot ne 'ICMP'){
466 return "--dport ".&fwlib
::get_srv_port
($$hash{$key}[15],1,$prot);
467 }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){
468 return "--icmp-type ".&fwlib
::get_srv_port
($$hash{$key}[15],3,$prot);
469 }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){
472 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
473 if ($prot ne 'ICMP'){
474 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);
476 elsif($prot eq 'ICMP'){
477 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);