]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/forwardfw/rules.pl
152558b643b0958c14ae9aecf3955ffd548a265d
[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 no warnings 'uninitialized';
29
30 # enable only the following on debugging purpose
31 #use warnings;
32 #use CGI::Carp 'fatalsToBrowser';
33
34 my %fwdfwsettings=();
35 my %defaultNetworks=();
36 my %configfwdfw=();
37 my %color=();
38 my %icmptypes=();
39 my %ovpnSettings=();
40 my %customgrp=();
41 our %sourcehash=();
42 our %targethash=();
43 my @timeframe=();
44 my %configinputfw=();
45 my %aliases=();
46 my @DPROT=();
47 my @p2ps=();
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 $p2pfile = "${General::swroot}/forward/p2protocols";
55 my $configgrp = "${General::swroot}/fwhosts/customgroups";
56 my $errormessage='';
57 my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT);
58 my $CHAIN="FORWARDFW";
59
60
61 &General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings);
62 &General::readhasharray($configfwdfw, \%configfwdfw);
63 &General::readhasharray($configinput, \%configinputfw);
64 &General::readhasharray($configgrp, \%customgrp);
65 &General::get_aliases(\%aliases);
66
67 ################################
68 # DEBUG/TEST #
69 ################################
70 my $MODE=1; # 0 - normal operation
71 # 1 - print configline and rules to console
72 #
73 ################################
74 my $param=shift;
75
76 if($param eq 'flush'){
77 if ($MODE eq '1'){
78 print " Flushing chains...\n";
79 }
80 &flush;
81 }else{
82 if ($MODE eq '1'){
83 print " Flushing chains...\n";
84 }
85 &flush;
86 if ($MODE eq '1'){
87 print " Preparing rules...\n";
88 }
89 &preparerules;
90 if($MODE eq '0'){
91 if ($fwdfwsettings{'POLICY'} eq 'MODE1'){
92 system ("/usr/sbin/firewall-forward-policy");
93 }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){
94 system ("/usr/sbin/firewall-forward-policy");
95 }elsif($fwdfwsettings{'POLICY'} eq 'MODE0' || $fwdfwsettings{'POLICY'} eq 'MODE2'){
96 system ("/usr/sbin/firewall-forward-policy");
97 system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT");
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 &p2pblock;
111 }
112 if (! -z "${General::swroot}/forward/input"){
113 &buildrules(\%configinputfw);
114 }
115 }
116 sub buildrules
117 {
118 my $hash=shift;
119 my $STAG;
120 foreach my $key (sort {$a <=> $b} keys %$hash){
121 $STAG='';
122 if($$hash{$key}[2] eq 'ON'){
123 #get source ip's
124 if ($$hash{$key}[3] eq 'cust_grp_src'){
125 foreach my $grp (sort {$a <=> $b} keys %customgrp){
126 if($customgrp{$grp}[0] eq $$hash{$key}[4]){
127 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src");
128 }
129 }
130 }else{
131 &get_address($$hash{$key}[3],$$hash{$key}[4],"src");
132 }
133 #get target ip's
134 if ($$hash{$key}[5] eq 'cust_grp_tgt'){
135 foreach my $grp (sort {$a <=> $b} keys %customgrp){
136 if($customgrp{$grp}[0] eq $$hash{$key}[6]){
137 &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt");
138 }
139 }
140 }elsif($$hash{$key}[5] eq 'ipfire'){
141
142 if($$hash{$key}[6] eq 'Default IP'){
143 open(FILE, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.';
144 $targethash{$key}[0]= <FILE>;
145 close(FILE);
146 }else{
147 foreach my $alias (sort keys %aliases){
148 if ($$hash{$key}[6] eq $alias){
149 $targethash{$key}[0]=$aliases{$alias}{'IPT'};
150 }
151 }
152 }
153 }else{
154 &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt");
155 }
156 ##get source prot and port
157 $SRC_TGT='SRC';
158 $SPROT = &get_prot($hash,$key);
159 $SPORT = &get_port($hash,$key);
160 $SRC_TGT='';
161
162 ##get target prot and port
163 $DPROT=&get_prot($hash,$key);
164
165 if ($DPROT eq ''){$DPROT=' ';}
166 @DPROT=split(",",$DPROT);
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 if ($MODE eq '1'){
183 print "NR:$key ";
184 foreach my $i (0 .. $#{$$hash{$key}}){
185 print "$i: $$hash{$key}[$i] ";
186 }
187 print "\n";
188 print"##################################\n";
189 #print rules to console
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(substr($sourcehash{$a}[0], 3, 3) ne 'mac'){ $STAG="-s";}
199 if ($$hash{$key}[17] eq 'ON'){
200 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n";
201 }
202 print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n";
203 }
204 }
205 }
206 }
207 print"\n";
208 }
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(substr($sourcehash{$a}[0], 3, 3) ne 'mac'){ $STAG="-s";}
219 if ($$hash{$key}[17] eq 'ON'){
220 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG");
221 }
222 system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]");
223 }
224 }
225 }
226 }
227 print"\n";
228 }
229 }
230 }
231 %sourcehash=();
232 %targethash=();
233 undef $TIME;
234 undef $TIMEFROM;
235 undef $TIMETILL;
236 }
237 }
238 sub p2pblock
239 {
240 my $P2PSTRING;
241 my $DO;
242 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
243 @p2ps = <FILE>;
244 close FILE;
245 my $CMD = "-m ipp2p";
246 foreach my $p2pentry (sort @p2ps) {
247 my @p2pline = split( /\;/, $p2pentry );
248 if ( $fwdfwsettings{'POLICY'} eq 'MODE2' ) {
249 $DO = "DROP";
250 if ("$p2pline[2]" eq "off") {
251 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
252 }
253 } else {
254 $DO = "RETURN";
255 if ("$p2pline[2]" eq "on") {
256 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
257 }
258 }
259 }
260 if ($MODE eq 1){
261 if($P2PSTRING){
262 print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n";
263 }
264 }else{
265 if($P2PSTRING){
266 system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO");
267 }
268 }
269 }
270
271 sub get_address
272 {
273 my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey
274 my $base2=shift;
275 my $type=shift; #src or tgt
276 my $hash;
277 if ($type eq 'src'){
278 $hash=\%sourcehash;
279 }else{
280 $hash=\%targethash;
281 }
282 my $key = &General::findhasharraykey($hash);
283 if($base eq 'src_addr' || $base eq 'tgt_addr' ){
284 if (&General::validmac($base2)){
285 $$hash{$key}[0] = "-m mac --mac-source $base2";
286 }else{
287 $$hash{$key}[0] = $base2;
288 }
289 }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){
290 $$hash{$key}[0]=&fwlib::get_std_net_ip($base2);
291 }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){
292 $$hash{$key}[0]=&fwlib::get_net_ip($base2);
293 }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){
294 $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type);
295 }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){
296 $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1);
297 }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){
298 $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33);
299 }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){
300 $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,27);
301 }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){
302 $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11);
303 }
304 }
305 sub get_prot
306 {
307 my $hash=shift;
308 my $key=shift;
309 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
310 if ($$hash{$key}[10] ne ''){
311 return"$$hash{$key}[8]";
312 }elsif($$hash{$key}[9] ne ''){
313 return"$$hash{$key}[8]";
314 }else{
315 return "$$hash{$key}[8]";
316 }
317 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
318 if ($$hash{$key}[14] eq 'TGT_PORT'){
319 if ($$hash{$key}[15] ne ''){
320 return "$$hash{$key}[12]";
321 }elsif($$hash{$key}[13] ne ''){
322 return "$$hash{$key}[12]";
323 }else{
324 return "$$hash{$key}[12]";
325 }
326 }elsif($$hash{$key}[14] eq 'cust_srv'){
327 return &fwlib::get_srv_prot($$hash{$key}[15]);
328
329 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
330 return &fwlib::get_srvgrp_prot($$hash{$key}[15]);
331 }
332 }
333 }
334 sub get_port
335 {
336 my $hash=shift;
337 my $key=shift;
338 my $prot=shift;
339 if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){
340 if ($$hash{$key}[10] ne ''){
341 return "--sport $$hash{$key}[10] ";
342 }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){
343 return "--icmp-type $$hash{$key}[9] ";
344 }elsif($$hash{$key}[9] eq 'All ICMP-Types'){
345 return;
346 }
347 }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){
348
349 if($$hash{$key}[14] eq 'TGT_PORT'){
350 if ($$hash{$key}[15] ne ''){
351 return "--dport $$hash{$key}[15] ";
352 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){
353 return "--icmp-type $$hash{$key}[13] ";
354 }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){
355 return;
356 }
357 }elsif($$hash{$key}[14] eq 'cust_srv'){
358 if ($prot ne 'ICMP'){
359 return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot);
360 }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){
361 return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot);
362 }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){
363 return;
364 }
365 }elsif($$hash{$key}[14] eq 'cust_srvgrp'){
366 if ($prot ne 'ICMP'){
367 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
368 }
369 elsif($prot eq 'ICMP'){
370 return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot);
371 }
372 }
373 }
374 }