]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/rules.pl
Forward Firewall: Firewall sets Internetdevice correctly now (was always red0)
[people/teissler/ipfire-2.x.git] / config / forwardfw / rules.pl
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
27 use strict;
28 use Time::Local;
29 no warnings 'uninitialized';
30
31 # enable only the following on debugging purpose
32 #use warnings;
33 #use CGI::Carp 'fatalsToBrowser';
34
35 my %fwdfwsettings=();
36 my %defaultNetworks=();
37 my %configfwdfw=();
38 my %color=();
39 my %icmptypes=();
40 my %ovpnSettings=();
41 my %customgrp=();
42 our %sourcehash=();
43 our %targethash=();
44 my @timeframe=();
45 my %configinputfw=();
46 my %configoutgoingfw=();
47 my %configdmzfw=();
48 my %aliases=();
49 my @DPROT=();
50 my @p2ps=();
51 require '/var/ipfire/general-functions.pl';
52 require "${General::swroot}/lang.pl";
53 require "${General::swroot}/forward/bin/firewall-lib.pl";
54
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";
62 my $errormessage='';
63 my $orange;
64 my $green;
65 my $blue;
66 my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
67 my $CHAIN="FORWARDFW";
68 my $conexists='off';
69
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);
78
79 #check if we have an internetconnection
80 open (CONN,"/var/ipfire/red/iface");
81 my $con = <CONN>;
82 close(CONN);
83 if (-f "/var/ipfire/red/active"){
84 $conexists='on';
85 }
86
87 ################################
88 # DEBUG/TEST #
89 ################################
90 my $MODE=1; # 0 - normal operation
91 # 1 - print configline and rules to console
92 #
93 ################################
94 my $param=shift;
95
96 if($param eq 'flush'){
97 if ($MODE eq '1'){
98 print " Flushing chains...\n";
99 }
100 &flush;
101 }else{
102 if ($MODE eq '1'){
103 print " Flushing chains...\n";
104 }
105 &flush;
106 if ($MODE eq '1'){
107 print " Preparing rules...\n";
108 }
109 &preparerules;
110 if($MODE eq '0'){
111 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
112 &p2pblock;
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");
122 }
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");
130 }
131 }
132 &p2pblock;
133 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
134 system ("/usr/sbin/firewall-policy");
135 }
136 }
137 }
138 sub flush
139 {
140 system ("iptables -F FORWARDFW");
141 system ("iptables -F INPUTFW");
142 system ("iptables -F OUTGOINGFW");
143 }
144 sub preparerules
145 {
146 if (! -z "${General::swroot}/forward/dmz"){
147 &buildrules(\%configdmzfw);
148 }
149 if (! -z "${General::swroot}/forward/config"){
150 &buildrules(\%configfwdfw);
151 }
152 if (! -z "${General::swroot}/forward/input"){
153 &buildrules(\%configinputfw);
154 }
155 if (! -z "${General::swroot}/forward/outgoing"){
156 &buildrules(\%configoutgoingfw);
157 }
158 }
159 sub buildrules
160 {
161 my $hash=shift;
162 my $STAG;
163 foreach my $key (sort {$a <=> $b} keys %$hash){
164 next if ($$hash{$key}[6] eq 'RED' && $conexists eq 'off' );
165 $STAG='';
166 if($$hash{$key}[2] eq 'ON'){
167 #get source ip's
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");
172 }
173 }
174 }else{
175 &get_address($$hash{$key}[3],$$hash{$key}[4],"src");
176 }
177 #get target ip's
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");
182 }
183 }
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>;
188 close(FILE);
189 }else{
190 foreach my $alias (sort keys %aliases){
191 if ($$hash{$key}[6] eq $alias){
192 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
193 }
194 }
195 }
196 }else{
197 &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt");
198 }
199 ##get source prot and port
200 $SRC_TGT='SRC';
201 $SPROT = &get_prot($hash,$key);
202 $SPORT = &get_port($hash,$key);
203 $SRC_TGT='';
204
205 ##get target prot and port
206 $DPROT=&get_prot($hash,$key);
207
208 if ($DPROT eq ''){$DPROT=' ';}
209 @DPROT=split(",",$DPROT);
210
211 #get time if defined
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);
225
226 $TIMEFROM="--timestart $time1 ";
227 $TIMETILL="--timestop $time2 ";
228 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
229 }
230 if ($MODE eq '1'){
231 print "NR:$key ";
232 foreach my $i (0 .. $#{$$hash{$key}}){
233 print "$i: $$hash{$key}[$i] ";
234 }
235 print "\n";
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,));
249 foreach (@icmprule){
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";
252 }
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";
254 }
255 }else{
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";
258 }
259 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
260 }
261 }
262 }
263 }
264 }
265 print"\n";
266 }
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,));
279 foreach (@icmprule){
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");
282 }
283 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]");
284 }
285 }else{
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");
288 }
289 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
290 }
291 }
292 }
293 }
294 }
295 }
296 }
297 }
298 %sourcehash=();
299 %targethash=();
300 undef $TIME;
301 undef $TIMEFROM;
302 undef $TIMETILL;
303 }
304 }
305 sub get_time
306 {
307 my $val=shift;
308 my $val1=shift;
309 my $time;
310 my $minutes;
311 my $ruletime;
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;
317 return $time;
318 }
319 sub time_get_utc
320 {
321 # Calculates the UTCtime from a given time
322 my $val=shift;
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);
326 return $diff;
327 }
328 sub utcmin
329 {
330 my $ruletime=shift;
331 my ($hrs,$min) = split(":",$ruletime);
332 my $newtime = $hrs*60+$min;
333 return $newtime;
334 }
335 sub p2pblock
336 {
337 my $P2PSTRING;
338 my $DO;
339 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
340 @p2ps = <FILE>;
341 close FILE;
342 my $CMD = "-m ipp2p";
343 foreach my $p2pentry (sort @p2ps) {
344 my @p2pline = split( /\;/, $p2pentry );
345 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
346 $DO = "ACCEPT";
347 if ("$p2pline[2]" eq "on") {
348 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
349 }
350 }else {
351 $DO = "RETURN";
352 if ("$p2pline[2]" eq "off") {
353 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
354 }
355 }
356 }
357 if ($MODE eq 1){
358 if($P2PSTRING){
359 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
360 }
361 }else{
362 if($P2PSTRING){
363 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
364 }
365 }
366 }
367
368 sub get_address
369 {
370 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
371 my $base2=shift;
372 my $type=shift; #src or tgt
373 my $hash;
374 if ($type eq 'src'){
375 $hash=\%sourcehash;
376 }else{
377 $hash=\%targethash;
378 }
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";
383 }else{
384 $$hash{$key}[0] = $base2;
385 }
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);
400 }
401 }
402 sub get_prot
403 {
404 my $hash=shift;
405 my $key=shift;
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]";
411 }else{
412 return "$$hash{$key}[8]";
413 }
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]";
420 }else{
421 return "$$hash{$key}[12]";
422 }
423 }elsif($$hash{$key}[14] eq 'cust_srv'){
424 return &fwlib::get_srv_prot($$hash{$key}[15]);
425
426 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
427 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
428 }
429 }
430 }
431 sub get_port
432 {
433 my $hash=shift;
434 my $key=shift;
435 my $prot=shift;
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] ";
441 }else{
442 return "--sport $$hash{$key}[10] ";
443 }
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'){
447 return;
448 }
449 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
450
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] ";
456 }else{
457 return "--dport $$hash{$key}[15] ";
458 }
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'){
462 return;
463 }
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'){
470 return;
471 }
472 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
473 if ($prot ne 'ICMP'){
474 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
475 }
476 elsif($prot eq 'ICMP'){
477 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
478 }
479 }
480 }
481 }