]>
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=();
52 require '/var/ipfire/general-functions.pl';
53 require "${General::swroot}/lang.pl";
54 require "${General::swroot}/forward/bin/firewall-lib.pl";
56 my $configdmz = "${General::swroot}/forward/dmz";
57 my $configfwdfw = "${General::swroot}/forward/config";
58 my $configinput = "${General::swroot}/forward/input";
59 my $configoutgoing = "${General::swroot}/forward/outgoing";
60 my $confignat = "${General::swroot}/forward/nat";
61 my $p2pfile = "${General::swroot}/forward/p2protocols";
62 my $configgrp = "${General::swroot}/fwhosts/customgroups";
63 my $netsettings = "${General::swroot}/ethernet/settings";
68 my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
69 my $CHAIN="FORWARDFW";
71 my $command = 'iptables -A';
74 &General
::readhash
("${General::swroot}/forward/settings", \
%fwdfwsettings);
75 &General
::readhash
("$netsettings", \
%defaultNetworks);
76 &General
::readhasharray
($configdmz, \
%configdmzfw);
77 &General
::readhasharray
($configfwdfw, \
%configfwdfw);
78 &General
::readhasharray
($configinput, \
%configinputfw);
79 &General
::readhasharray
($configoutgoing, \
%configoutgoingfw);
80 &General
::readhasharray
($confignat, \
%confignatfw);
81 &General
::readhasharray
($configgrp, \
%customgrp);
82 &General
::get_aliases
(\
%aliases);
84 #check if we have an internetconnection
85 open (CONN
,"/var/ipfire/red/iface");
88 if (-f
"/var/ipfire/red/active"){
91 open (CONN1
,"/var/ipfire/red/local-ipaddress");
94 ################################
96 ################################
97 my $MODE=1; # 0 - normal operation
98 # 1 - print configline and rules to console
100 ################################
103 if($param eq 'flush'){
105 print " Flushing chains...\n";
110 print " Flushing chains...\n";
114 print " Preparing rules...\n";
118 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
120 system ("/usr/sbin/firewall-policy");
121 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
122 $defaultNetworks{'GREEN_NETMASK'}=&General
::iporsubtocidr
($defaultNetworks{'GREEN_NETMASK'});
123 $green="$defaultNetworks{'GREEN_ADDRESS'}/$defaultNetworks{'GREEN_NETMASK'}";
124 if ($defaultNetworks{'BLUE_DEV'}){
125 $defaultNetworks{'BLUE_NETMASK'}=&General
::iporsubtocidr
($defaultNetworks{'BLUE_NETMASK'});
126 $blue="$defaultNetworks{'BLUE_ADDRESS'}/$defaultNetworks{'BLUE_NETMASK'}";
127 #set default rules for BLUE
128 system ("iptables -A $CHAIN -s $blue -d $green -j RETURN");
130 if ($defaultNetworks{'ORANGE_DEV'}){
131 $defaultNetworks{'ORANGE_NETMASK'}=&General
::iporsubtocidr
($defaultNetworks{'ORANGE_NETMASK'});
132 $orange="$defaultNetworks{'ORANGE_ADDRESS'}/$defaultNetworks{'ORANGE_NETMASK'}";
133 #set default rules for DMZ
134 system ("iptables -A $CHAIN -s $orange -d $green -j RETURN");
135 if ($defaultNetworks{'BLUE_DEV'}){
136 system ("iptables -A $CHAIN -s $orange -d $blue -j RETURN");
140 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
141 system ("/usr/sbin/firewall-policy");
147 system ("iptables -F FORWARDFW");
148 system ("iptables -F INPUTFW");
149 system ("iptables -F OUTGOINGFW");
150 system ("iptables -F PORTFWACCESS");
151 system ("iptables -t nat -F NAT_DESTINATION");
152 system ("iptables -t nat -F NAT_SOURCE");
156 if (! -z
"${General::swroot}/forward/dmz"){
157 &buildrules
(\
%configdmzfw);
159 if (! -z
"${General::swroot}/forward/config"){
160 &buildrules
(\
%configfwdfw);
162 if (! -z
"${General::swroot}/forward/input"){
163 &buildrules
(\
%configinputfw);
165 if (! -z
"${General::swroot}/forward/outgoing"){
166 &buildrules
(\
%configoutgoingfw);
168 if (! -z
"${General::swroot}/forward/nat"){
169 &buildrules
(\
%confignatfw);
179 foreach my $key (sort {$a <=> $b} keys %$hash){
180 next if ($$hash{$key}[6] eq 'RED' && $conexists eq 'off' );
181 if ($$hash{$key}[28] eq 'ON'){
182 $command='iptables -t nat -A';
183 $natip=&get_nat_ip
($$hash{$key}[29]);
184 if($$hash{$key}[31] eq 'dnat'){
185 $$hash{$key}[0]='DNAT';
186 $fireport='--dport '.$$hash{$key}[30] if ($$hash{$key}[30]>0);
188 $$hash{$key}[0]='SNAT';
192 if($$hash{$key}[2] eq 'ON'){
194 if ($$hash{$key}[3] eq 'cust_grp_src'){
195 foreach my $grp (sort {$a <=> $b} keys %customgrp){
196 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
197 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
201 &get_address
($$hash{$key}[3],$$hash{$key}[4],"src");
204 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
205 foreach my $grp (sort {$a <=> $b} keys %customgrp){
206 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
207 &get_address
($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
210 }elsif($$hash{$key}[5] eq 'ipfire'){
211 if($$hash{$key}[6] eq 'Default IP'){
212 open(FILE
, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.';
213 $targethash{$key}[0]= <FILE
>;
216 foreach my $alias (sort keys %aliases){
217 if ($$hash{$key}[6] eq $alias){
218 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
223 &get_address
($$hash{$key}[5],$$hash{$key}[6],"tgt");
225 ##get source prot and port
227 $SPROT = &get_prot
($hash,$key);
228 $SPORT = &get_port
($hash,$key);
231 ##get target prot and port
232 $DPROT=&get_prot
($hash,$key);
234 if ($DPROT eq ''){$DPROT=' ';}
235 @DPROT=split(",",$DPROT);
238 if($$hash{$key}[18] eq 'ON'){
239 my ($time1,$time2,$daylight);
240 my $daylight=$$hash{$key}[28];
241 $time1=&get_time
($$hash{$key}[26],$daylight);
242 $time2=&get_time
($$hash{$key}[27],$daylight);
243 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
244 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
245 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
246 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
247 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
248 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
249 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
250 $TIME=join(",",@timeframe);
252 $TIMEFROM="--timestart $time1 ";
253 $TIMETILL="--timestop $time2 ";
254 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
258 foreach my $i (0 .. $#{$$hash{$key}}){
259 print "$i: $$hash{$key}[$i] ";
262 print"##################################\n";
263 #print rules to console
264 foreach my $DPROT (@DPROT){
265 $DPORT = &get_port
($hash,$key,$DPROT);
266 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
267 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
268 foreach my $a (sort keys %sourcehash){
269 foreach my $b (sort keys %targethash){
270 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'){
271 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
272 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
273 if(substr($DPORT, 2, 4) eq 'icmp'){
274 my @icmprule= split(",",substr($DPORT, 12,));
276 if ($$hash{$key}[17] eq 'ON'){
277 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j LOG\n";
279 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]\n";
281 }elsif($$hash{$key}[28] ne 'ON'){
282 if ($$hash{$key}[17] eq 'ON'){
283 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
285 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
286 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
287 if ($$hash{$key}[17] eq 'ON'){
288 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
290 my $fwaccessdport="--dport ".substr($DPORT,1,) if ($DPORT);
291 my ($ip,$sub) =split("/",$targethash{$b}[0]);
292 print "iptables -A PORTFWACCESS $PROT -i $con -d $ip $fwaccessdport $TIME -j ACCEPT\n";
293 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $$hash{$key}[0] --to $ip$DPORT\n";
294 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[32] eq 'snat'){
295 print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0] --to $natip$fireport\n";
303 }elsif($MODE eq '0'){
304 foreach my $DPROT (@DPROT){
305 $DPORT = &get_port
($hash,$key,$DPROT);
306 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
307 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
308 foreach my $a (sort keys %sourcehash){
309 foreach my $b (sort keys %targethash){
310 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'){
311 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
312 if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";}
313 if(substr($DPORT, 2, 4) eq 'icmp'){
314 my @icmprule= split(",",substr($DPORT, 12,));
316 if ($$hash{$key}[17] eq 'ON'){
317 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] -- icmp-type $_ $TIME -j LOG");
319 system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]");
321 }elsif($$hash{$key}[28] ne 'ON'){
322 if ($$hash{$key}[17] eq 'ON'){
323 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
325 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
326 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){
327 if ($$hash{$key}[17] eq 'ON'){
328 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT' \n";
330 my $fwaccessdport="--dport ".substr($DPORT,1,) if ($DPORT);
331 my ($ip,$sub) =split("/",$targethash{$b}[0]);
332 system "iptables -A PORTFWACCESS $PROT -i $con -d $ip $fwaccessdport $TIME -j ACCEPT\n";
333 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $$hash{$key}[0] --to $ip$DPORT\n";
334 }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){
335 if ($$hash{$key}[17] eq 'ON'){
336 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT '\n";
338 system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0] --to $natip$fireport\n";
359 if($val eq 'RED' || $val eq 'GREEN' || $val eq 'ORANGE' || $val eq 'BLUE'){
360 $result=$defaultNetworks{$val.'_ADDRESS'};
361 }elsif($val eq 'ALL'){
363 }elsif($val eq 'Default IP'){
364 $result='-d '.$redip;
366 foreach my $al (sort keys %aliases){
368 $result='-d '.$aliases{$al}{'IPT'};
381 $minutes = &utcmin
($val);
382 $ruletime = $minutes + &time_get_utc
($val);
383 if ($ruletime < 0){$ruletime +=1440;}
384 if ($ruletime > 1440){$ruletime -=1440;}
385 $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60;
390 # Calculates the UTCtime from a given time
392 my @localtime=localtime(time);
393 my @gmtime=gmtime(time);
394 my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60);
400 my ($hrs,$min) = split(":",$ruletime);
401 my $newtime = $hrs*60+$min;
408 open( FILE
, "< $p2pfile" ) or die "Unable to read $p2pfile";
411 my $CMD = "-m ipp2p";
412 foreach my $p2pentry (sort @p2ps) {
413 my @p2pline = split( /\;/, $p2pentry );
414 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
416 if ("$p2pline[2]" eq "on") {
417 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
421 if ("$p2pline[2]" eq "off") {
422 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
428 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
432 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
438 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
440 my $type=shift; #src or tgt
447 my $key = &General
::findhasharraykey
($hash);
448 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
449 if (&General
::validmac
($base2)){
450 $$hash{$key}[0] = "-m mac --mac-source $base2";
452 $$hash{$key}[0] = $base2;
454 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
455 $$hash{$key}[0]=&fwlib
::get_std_net_ip
($base2,$con);
456 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
457 $$hash{$key}[0]=&fwlib
::get_net_ip
($base2);
458 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
459 $$hash{$key}[0]=&fwlib
::get_host_ip
($base2,$type);
460 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
461 $$hash{$key}[0]=&fwlib
::get_ovpn_net_ip
($base2,1);
462 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
463 $$hash{$key}[0]=&fwlib
::get_ovpn_host_ip
($base2,33);
464 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
465 $$hash{$key}[0]=&fwlib
::get_ovpn_n2n_ip
($base2,27);
466 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
467 $$hash{$key}[0]=&fwlib
::get_ipsec_net_ip
($base2,11);
474 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
475 if ($$hash{$key}[10] ne ''){
476 return"$$hash{$key}[8]";
477 }elsif($$hash{$key}[9] ne ''){
478 return"$$hash{$key}[8]";
480 return "$$hash{$key}[8]";
482 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
483 if ($$hash{$key}[14] eq 'TGT_PORT'){
484 if ($$hash{$key}[15] ne ''){
485 return "$$hash{$key}[12]";
486 }elsif($$hash{$key}[13] ne ''){
487 return "$$hash{$key}[12]";
489 return "$$hash{$key}[12]";
491 }elsif($$hash{$key}[14] eq 'cust_srv'){
492 return &fwlib
::get_srv_prot
($$hash{$key}[15]);
494 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
495 return &fwlib
::get_srvgrp_prot
($$hash{$key}[15]);
504 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
505 if ($$hash{$key}[10] ne ''){
506 $$hash{$key}[10] =~ s/\|/,/g;
507 if(index($$hash{$key}[10],",") > 0){
508 return "-m multiport --sport $$hash{$key}[10] ";
510 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ||($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat') ){
511 return "--sport $$hash{$key}[10] ";
513 return ":$$hash{$key}[10]";
516 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
517 return "--icmp-type $$hash{$key}[9] ";
518 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
521 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
523 if($$hash{$key}[14] eq 'TGT_PORT'){
524 if ($$hash{$key}[15] ne ''){
525 $$hash{$key}[15] =~ s/\|/,/g;
526 if(index($$hash{$key}[15],",") > 0){
527 return "-m multiport --dport $$hash{$key}[15] ";
529 if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ){
530 return "--dport $$hash{$key}[15] ";
532 return ":$$hash{$key}[15]";
535 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){
536 return "--icmp-type $$hash{$key}[13] ";
537 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){
540 }elsif($$hash{$key}[14] eq 'cust_srv'){
541 if ($prot ne 'ICMP'){
542 if($$hash{$key}[31] eq 'dnat'){
543 return ":".&fwlib
::get_srv_port
($$hash{$key}[15],1,$prot);
545 return "--dport ".&fwlib
::get_srv_port
($$hash{$key}[15],1,$prot);
547 }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){
548 return "--icmp-type ".&fwlib
::get_srv_port
($$hash{$key}[15],3,$prot);
549 }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){
552 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
553 if ($prot ne 'ICMP'){
554 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);
556 elsif($prot eq 'ICMP'){
557 return &fwlib
::get_srvgrp_port
($$hash{$key}[15],$prot);