]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/forwardfw/rules.pl
Forward Firewall: fixed 12 Bugs from forum.
[people/teissler/ipfire-2.x.git] / config / forwardfw / rules.pl
CommitLineData
2a81ab0d
AM
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
28use strict;
29no warnings 'uninitialized';
30
31# enable only the following on debugging purpose
32#use warnings;
33#use CGI::Carp 'fatalsToBrowser';
34
35my %fwdfwsettings=();
36my %defaultNetworks=();
37my %configfwdfw=();
38my %color=();
39my %icmptypes=();
40my %ovpnSettings=();
41my %customgrp=();
42our %sourcehash=();
43our %targethash=();
44my @timeframe=();
45my %configinputfw=();
46my %aliases=();
47my @DPROT=();
48require '/var/ipfire/general-functions.pl';
49require "${General::swroot}/lang.pl";
50require "${General::swroot}/forward/bin/firewall-lib.pl";
51
52my $configfwdfw = "${General::swroot}/forward/config";
53my $configinput = "${General::swroot}/forward/input";
54my $configgrp = "${General::swroot}/fwhosts/customgroups";
55my $errormessage='';
56my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
57my $CHAIN="FORWARDFW";
58
59
60&General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings);
61&General::readhasharray($configfwdfw, \%configfwdfw);
62&General::readhasharray($configinput, \%configinputfw);
63&General::readhasharray($configgrp, \%customgrp);
64&General::get_aliases(\%aliases);
65
66################################
67# DEBUG/TEST #
68################################
69my $MODE=0; # 0 - normal operation
70 # 1 - print configline and rules to console
71 #
72################################
73my $param=shift;
74
75if($param eq 'flush'){
76 if ($MODE eq '1'){
77 print " Flushing chains...\n";
78 }
79 &flush;
80}else{
81 if ($MODE eq '1'){
82 print " Flushing chains...\n";
83 }
84 &flush;
85 if ($MODE eq '1'){
86 print " Preparing rules...\n";
87 }
88 &preparerules;
89 if($MODE eq '0'){
90 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
62fc8511 91 system ("/usr/sbin/firewall-forward-policy");
2a81ab0d 92 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
62fc8511 93 system ("/usr/sbin/firewall-forward-policy");
fd10a52c 94 }elsif($fwdfwsettings{'POLICY'} eq 'MODE0' || $fwdfwsettings{'POLICY'} eq 'MODE2'){
62fc8511 95 system ("/usr/sbin/firewall-forward-policy");
fd10a52c 96 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
2a81ab0d
AM
97 }
98 }
99}
100
101sub flush
102{
103 system ("iptables -F FORWARDFW");
104 system ("iptables -F INPUTFW");
105}
106sub preparerules
107{
108 if (! -z "${General::swroot}/forward/config"){
109 &buildrules(\%configfwdfw);
110 }
111 if (! -z "${General::swroot}/forward/input"){
112 &buildrules(\%configinputfw);
113 }
114}
115sub buildrules
116{
117 my $hash=shift;
118 foreach my $key (sort keys %$hash){
119 if($$hash{$key}[2] eq 'ON'){
120 #get source ip's
121 if ($$hash{$key}[3] eq 'cust_grp_src'){
122 foreach my $grp (sort keys %customgrp){
123 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
124 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
125 }
126 }
127 }else{
128 &get_address($$hash{$key}[3],$$hash{$key}[4],"src");
129 }
130 #get target ip's
131 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
132 foreach my $grp (sort keys %customgrp){
133 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
134 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
135 }
136 }
137 }elsif($$hash{$key}[5] eq 'ipfire'){
14f7cb87 138
2a81ab0d
AM
139 if($$hash{$key}[6] eq 'Default IP'){
140 open(FILE, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.';
141 $targethash{$key}[0]= <FILE>;
142 close(FILE);
143 }else{
144 foreach my $alias (sort keys %aliases){
145 if ($$hash{$key}[6] eq $alias){
146 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
147 }
148 }
149 }
150 }else{
151 &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt");
152 }
2a81ab0d
AM
153 ##get source prot and port
154 $SRC_TGT='SRC';
155 $SPROT = &get_prot($hash,$key);
156 $SPORT = &get_port($hash,$key);
157 $SRC_TGT='';
14f7cb87 158
2a81ab0d
AM
159 ##get target prot and port
160 $DPROT=&get_prot($hash,$key);
14f7cb87 161
2a81ab0d
AM
162 if ($DPROT eq ''){$DPROT=' ';}
163 @DPROT=split(",",$DPROT);
14f7cb87 164
62fc8511 165
2a81ab0d
AM
166 #get time if defined
167 if($$hash{$key}[18] eq 'ON'){
168 if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");}
169 if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");}
170 if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");}
171 if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");}
172 if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");}
173 if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");}
174 if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");}
175 $TIME=join(",",@timeframe);
176 $TIMEFROM="--timestart $$hash{$key}[26] ";
177 $TIMETILL="--timestop $$hash{$key}[27] ";
178 $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL";
179 }
62fc8511 180
2a81ab0d
AM
181 if ($MODE eq '1'){
182 print "NR:$key ";
183 foreach my $i (0 .. $#{$$hash{$key}}){
184 print "$i: $$hash{$key}[$i] ";
185 }
186 print "\n";
187 print"##################################\n";
188 #print rules to console
62fc8511 189
2a81ab0d
AM
190 foreach my $DPROT (@DPROT){
191 $DPORT = &get_port($hash,$key,$DPROT);
192 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
193 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
194 foreach my $a (sort keys %sourcehash){
195 foreach my $b (sort keys %targethash){
196 if ($sourcehash{$a}[0] ne $targethash{$b}[0] && $targethash{$b}[0] ne 'none'){
197 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
198 if ($$hash{$key}[17] eq 'ON'){
199 print "iptables -A $$hash{$key}[1] $PROT -s $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
200 }
201 print "iptables -A $$hash{$key}[1] $PROT -s $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
202 }
203 }
204 }
205 }
206 print"\n";
207 }
62fc8511 208
2a81ab0d
AM
209 }elsif($MODE eq '0'){
210 foreach my $DPROT (@DPROT){
211 $DPORT = &get_port($hash,$key,$DPROT);
212 if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;}
213 $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' ');
214 foreach my $a (sort keys %sourcehash){
215 foreach my $b (sort keys %targethash){
216 if ($sourcehash{$a}[0] ne $targethash{$b}[0] && $targethash{$b}[0] ne 'none'){
217 if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){
218 if ($$hash{$key}[17] eq 'ON'){
219 system ("iptables -A $$hash{$key}[1] $PROT -s $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
220 }
221 system ("iptables -A $$hash{$key}[1] $PROT -s $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
222 }
223 }
224 }
225 }
226 print"\n";
227 }
228 }
229 }
230 %sourcehash=();
231 %targethash=();
232 undef $TIME;
233 undef $TIMEFROM;
234 undef $TIMETILL;
235 }
236}
237sub get_address
238{
239 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
240 my $base2=shift;
241 my $type=shift; #src or tgt
242 my $hash;
243 if ($type eq 'src'){
244 $hash=\%sourcehash;
245 }else{
246 $hash=\%targethash;
247 }
248 my $key = &General::findhasharraykey($hash);
249 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
14f7cb87 250 $$hash{$key}[0] = $base2;
2a81ab0d
AM
251 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
252 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2);
253 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
254 $$hash{$key}[0]=&fwlib::get_net_ip($base2);
255 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
256 $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type);
257 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
258 $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1);
259 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
260 $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33);
261 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
262 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,27);
263 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
264 $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11);
265 }
266}
267sub get_prot
268{
269 my $hash=shift;
270 my $key=shift;
271 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
272 if ($$hash{$key}[10] ne ''){
273 return"$$hash{$key}[8]";
274 }elsif($$hash{$key}[9] ne ''){
275 return"$$hash{$key}[8]";
276 }else{
277 return "$$hash{$key}[8]";
278 }
279 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
280 if ($$hash{$key}[14] eq 'TGT_PORT'){
281 if ($$hash{$key}[15] ne ''){
282 return "$$hash{$key}[12]";
283 }elsif($$hash{$key}[13] ne ''){
284 return "$$hash{$key}[12]";
285 }else{
286 return "$$hash{$key}[12]";
287 }
288 }elsif($$hash{$key}[14] eq 'cust_srv'){
289 return &fwlib::get_srv_prot($$hash{$key}[15]);
290
291 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
292 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
293 }
294 }
295}
296sub get_port
297{
298 my $hash=shift;
299 my $key=shift;
300 my $prot=shift;
301 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
302 if ($$hash{$key}[10] ne ''){
303 return "--sport $$hash{$key}[10] ";
62fc8511 304 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
2a81ab0d 305 return "--icmp-type $$hash{$key}[9] ";
62fc8511
AM
306 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
307 return;
2a81ab0d
AM
308 }
309 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
2a81ab0d
AM
310 if($$hash{$key}[14] eq 'TGT_PORT'){
311 if ($$hash{$key}[15] ne ''){
312 return "--dport $$hash{$key}[15] ";
313 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){
314 return "--icmp-type $$hash{$key}[13] ";
315 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){
316 return;
317 }
318 }elsif($$hash{$key}[14] eq 'cust_srv'){
319 if ($prot ne 'ICMP'){
320 return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
321 }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){
322 return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot);
323 }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){
324 return;
325 }
326 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
327 if ($prot ne 'ICMP'){
328 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
329 }
330 elsif($prot eq 'ICMP'){
331 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
332 }
2a81ab0d
AM
333 }
334 }
335}