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