]>
Commit | Line | Data |
---|---|---|
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 | ||
2a81ab0d AM |
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=(); | |
36196d0d | 47 | my @p2ps=(); |
2a81ab0d AM |
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"; | |
36196d0d | 54 | my $p2pfile = "${General::swroot}/forward/p2protocols"; |
2a81ab0d | 55 | my $configgrp = "${General::swroot}/fwhosts/customgroups"; |
210ee67b | 56 | my $netsettings = "${General::swroot}/ethernet/settings"; |
2a81ab0d | 57 | my $errormessage=''; |
210ee67b AM |
58 | my $orange; |
59 | my $green; | |
2a81ab0d AM |
60 | my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT); |
61 | my $CHAIN="FORWARDFW"; | |
62 | ||
63 | ||
64 | &General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings); | |
210ee67b | 65 | &General::readhash("$netsettings", \%defaultNetworks); |
2a81ab0d AM |
66 | &General::readhasharray($configfwdfw, \%configfwdfw); |
67 | &General::readhasharray($configinput, \%configinputfw); | |
68 | &General::readhasharray($configgrp, \%customgrp); | |
69 | &General::get_aliases(\%aliases); | |
70 | ||
71 | ################################ | |
72 | # DEBUG/TEST # | |
73 | ################################ | |
54cb7ff0 | 74 | my $MODE=1; # 0 - normal operation |
2a81ab0d AM |
75 | # 1 - print configline and rules to console |
76 | # | |
77 | ################################ | |
78 | my $param=shift; | |
79 | ||
80 | if($param eq 'flush'){ | |
81 | if ($MODE eq '1'){ | |
82 | print " Flushing chains...\n"; | |
83 | } | |
84 | &flush; | |
85 | }else{ | |
86 | if ($MODE eq '1'){ | |
87 | print " Flushing chains...\n"; | |
88 | } | |
89 | &flush; | |
90 | if ($MODE eq '1'){ | |
91 | print " Preparing rules...\n"; | |
92 | } | |
93 | &preparerules; | |
94 | if($MODE eq '0'){ | |
95 | if ($fwdfwsettings{'POLICY'} eq 'MODE1'){ | |
af49e367 | 96 | &p2pblock; |
62fc8511 | 97 | system ("/usr/sbin/firewall-forward-policy"); |
2a81ab0d | 98 | }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){ |
210ee67b AM |
99 | if ($defaultNetworks{'ORANGE_DEV'}){ |
100 | $defaultNetworks{'ORANGE_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'ORANGE_NETMASK'}); | |
101 | $defaultNetworks{'GREEN_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'GREEN_NETMASK'}); | |
102 | $orange="$defaultNetworks{'ORANGE_ADDRESS'}/$defaultNetworks{'ORANGE_NETMASK'}"; | |
103 | $green="$defaultNetworks{'GREEN_ADDRESS'}/$defaultNetworks{'GREEN_NETMASK'}"; | |
104 | #set default rules for DMZ | |
105 | system ("iptables -A $CHAIN -s $orange -d $green -j RETURN"); | |
106 | &p2pblock; | |
107 | } | |
fd10a52c | 108 | system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT"); |
210ee67b | 109 | system ("/usr/sbin/firewall-forward-policy"); |
2a81ab0d AM |
110 | } |
111 | } | |
112 | } | |
2a81ab0d AM |
113 | sub flush |
114 | { | |
115 | system ("iptables -F FORWARDFW"); | |
116 | system ("iptables -F INPUTFW"); | |
117 | } | |
118 | sub preparerules | |
119 | { | |
120 | if (! -z "${General::swroot}/forward/config"){ | |
121 | &buildrules(\%configfwdfw); | |
122 | } | |
123 | if (! -z "${General::swroot}/forward/input"){ | |
124 | &buildrules(\%configinputfw); | |
125 | } | |
126 | } | |
127 | sub buildrules | |
128 | { | |
129 | my $hash=shift; | |
b5269091 | 130 | my $STAG; |
992394d5 | 131 | foreach my $key (sort {$a <=> $b} keys %$hash){ |
b5269091 | 132 | $STAG=''; |
2a81ab0d AM |
133 | if($$hash{$key}[2] eq 'ON'){ |
134 | #get source ip's | |
135 | if ($$hash{$key}[3] eq 'cust_grp_src'){ | |
992394d5 | 136 | foreach my $grp (sort {$a <=> $b} keys %customgrp){ |
2a81ab0d AM |
137 | if($customgrp{$grp}[0] eq $$hash{$key}[4]){ |
138 | &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src"); | |
139 | } | |
140 | } | |
141 | }else{ | |
142 | &get_address($$hash{$key}[3],$$hash{$key}[4],"src"); | |
143 | } | |
144 | #get target ip's | |
145 | if ($$hash{$key}[5] eq 'cust_grp_tgt'){ | |
992394d5 | 146 | foreach my $grp (sort {$a <=> $b} keys %customgrp){ |
2a81ab0d AM |
147 | if($customgrp{$grp}[0] eq $$hash{$key}[6]){ |
148 | &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt"); | |
149 | } | |
150 | } | |
151 | }elsif($$hash{$key}[5] eq 'ipfire'){ | |
14f7cb87 | 152 | |
2a81ab0d AM |
153 | if($$hash{$key}[6] eq 'Default IP'){ |
154 | open(FILE, "/var/ipfire/red/local-ipaddress") or die 'Unable to open config file.'; | |
155 | $targethash{$key}[0]= <FILE>; | |
156 | close(FILE); | |
157 | }else{ | |
158 | foreach my $alias (sort keys %aliases){ | |
159 | if ($$hash{$key}[6] eq $alias){ | |
160 | $targethash{$key}[0]=$aliases{$alias}{'IPT'}; | |
161 | } | |
162 | } | |
163 | } | |
164 | }else{ | |
165 | &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt"); | |
166 | } | |
2a81ab0d AM |
167 | ##get source prot and port |
168 | $SRC_TGT='SRC'; | |
169 | $SPROT = &get_prot($hash,$key); | |
170 | $SPORT = &get_port($hash,$key); | |
171 | $SRC_TGT=''; | |
14f7cb87 | 172 | |
2a81ab0d AM |
173 | ##get target prot and port |
174 | $DPROT=&get_prot($hash,$key); | |
14f7cb87 | 175 | |
2a81ab0d AM |
176 | if ($DPROT eq ''){$DPROT=' ';} |
177 | @DPROT=split(",",$DPROT); | |
14f7cb87 | 178 | |
2a81ab0d AM |
179 | #get time if defined |
180 | if($$hash{$key}[18] eq 'ON'){ | |
181 | if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");} | |
182 | if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");} | |
183 | if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");} | |
184 | if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");} | |
185 | if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");} | |
186 | if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");} | |
187 | if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");} | |
188 | $TIME=join(",",@timeframe); | |
189 | $TIMEFROM="--timestart $$hash{$key}[26] "; | |
190 | $TIMETILL="--timestop $$hash{$key}[27] "; | |
191 | $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL"; | |
192 | } | |
2a81ab0d AM |
193 | if ($MODE eq '1'){ |
194 | print "NR:$key "; | |
195 | foreach my $i (0 .. $#{$$hash{$key}}){ | |
196 | print "$i: $$hash{$key}[$i] "; | |
197 | } | |
198 | print "\n"; | |
199 | print"##################################\n"; | |
200 | #print rules to console | |
2a81ab0d AM |
201 | foreach my $DPROT (@DPROT){ |
202 | $DPORT = &get_port($hash,$key,$DPROT); | |
203 | if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;} | |
204 | $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' '); | |
205 | foreach my $a (sort keys %sourcehash){ | |
206 | foreach my $b (sort keys %targethash){ | |
d7dc9718 | 207 | 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'){ |
2a81ab0d | 208 | if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){ |
54cb7ff0 | 209 | if(substr($sourcehash{$a}[0], 3, 3) ne 'mac'){ $STAG="-s";} |
2a81ab0d | 210 | if ($$hash{$key}[17] eq 'ON'){ |
b5269091 | 211 | print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n"; |
2a81ab0d | 212 | } |
b5269091 | 213 | print "iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n"; |
2a81ab0d AM |
214 | } |
215 | } | |
216 | } | |
217 | } | |
218 | print"\n"; | |
219 | } | |
2a81ab0d AM |
220 | }elsif($MODE eq '0'){ |
221 | foreach my $DPROT (@DPROT){ | |
222 | $DPORT = &get_port($hash,$key,$DPROT); | |
223 | if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;} | |
224 | $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' '); | |
225 | foreach my $a (sort keys %sourcehash){ | |
226 | foreach my $b (sort keys %targethash){ | |
d7dc9718 | 227 | 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'){ |
2a81ab0d | 228 | if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){ |
54cb7ff0 | 229 | if(substr($sourcehash{$a}[0], 3, 3) ne 'mac'){ $STAG="-s";} |
2a81ab0d | 230 | if ($$hash{$key}[17] eq 'ON'){ |
b5269091 | 231 | system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG"); |
2a81ab0d | 232 | } |
b5269091 | 233 | system ("iptables -A $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]"); |
2a81ab0d AM |
234 | } |
235 | } | |
236 | } | |
237 | } | |
2a81ab0d AM |
238 | } |
239 | } | |
240 | } | |
241 | %sourcehash=(); | |
242 | %targethash=(); | |
243 | undef $TIME; | |
244 | undef $TIMEFROM; | |
245 | undef $TIMETILL; | |
246 | } | |
247 | } | |
36196d0d AM |
248 | sub p2pblock |
249 | { | |
250 | my $P2PSTRING; | |
251 | my $DO; | |
252 | open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile"; | |
253 | @p2ps = <FILE>; | |
254 | close FILE; | |
255 | my $CMD = "-m ipp2p"; | |
256 | foreach my $p2pentry (sort @p2ps) { | |
257 | my @p2pline = split( /\;/, $p2pentry ); | |
8d1beadc AM |
258 | if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) { |
259 | $DO = "ACCEPT"; | |
5238a871 | 260 | if ("$p2pline[2]" eq "on") { |
36196d0d AM |
261 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; |
262 | } | |
8d1beadc | 263 | }else { |
36196d0d | 264 | $DO = "RETURN"; |
5238a871 | 265 | if ("$p2pline[2]" eq "off") { |
36196d0d AM |
266 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; |
267 | } | |
268 | } | |
269 | } | |
270 | if ($MODE eq 1){ | |
271 | if($P2PSTRING){ | |
272 | print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n"; | |
273 | } | |
274 | }else{ | |
275 | if($P2PSTRING){ | |
276 | system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO"); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
2a81ab0d AM |
281 | sub get_address |
282 | { | |
283 | my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey | |
284 | my $base2=shift; | |
285 | my $type=shift; #src or tgt | |
286 | my $hash; | |
287 | if ($type eq 'src'){ | |
288 | $hash=\%sourcehash; | |
289 | }else{ | |
290 | $hash=\%targethash; | |
291 | } | |
292 | my $key = &General::findhasharraykey($hash); | |
293 | if($base eq 'src_addr' || $base eq 'tgt_addr' ){ | |
b5269091 AM |
294 | if (&General::validmac($base2)){ |
295 | $$hash{$key}[0] = "-m mac --mac-source $base2"; | |
296 | }else{ | |
297 | $$hash{$key}[0] = $base2; | |
298 | } | |
2a81ab0d AM |
299 | }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){ |
300 | $$hash{$key}[0]=&fwlib::get_std_net_ip($base2); | |
301 | }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){ | |
302 | $$hash{$key}[0]=&fwlib::get_net_ip($base2); | |
303 | }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){ | |
304 | $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type); | |
305 | }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){ | |
306 | $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1); | |
307 | }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){ | |
308 | $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33); | |
309 | }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){ | |
310 | $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,27); | |
311 | }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){ | |
312 | $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11); | |
313 | } | |
314 | } | |
315 | sub get_prot | |
316 | { | |
317 | my $hash=shift; | |
318 | my $key=shift; | |
319 | if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){ | |
320 | if ($$hash{$key}[10] ne ''){ | |
321 | return"$$hash{$key}[8]"; | |
322 | }elsif($$hash{$key}[9] ne ''){ | |
323 | return"$$hash{$key}[8]"; | |
324 | }else{ | |
325 | return "$$hash{$key}[8]"; | |
326 | } | |
327 | }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){ | |
328 | if ($$hash{$key}[14] eq 'TGT_PORT'){ | |
329 | if ($$hash{$key}[15] ne ''){ | |
330 | return "$$hash{$key}[12]"; | |
331 | }elsif($$hash{$key}[13] ne ''){ | |
332 | return "$$hash{$key}[12]"; | |
333 | }else{ | |
334 | return "$$hash{$key}[12]"; | |
335 | } | |
336 | }elsif($$hash{$key}[14] eq 'cust_srv'){ | |
337 | return &fwlib::get_srv_prot($$hash{$key}[15]); | |
338 | ||
339 | }elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
340 | return &fwlib::get_srvgrp_prot($$hash{$key}[15]); | |
341 | } | |
342 | } | |
343 | } | |
344 | sub get_port | |
345 | { | |
346 | my $hash=shift; | |
347 | my $key=shift; | |
348 | my $prot=shift; | |
349 | if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){ | |
350 | if ($$hash{$key}[10] ne ''){ | |
8f0b047b | 351 | $$hash{$key}[10] =~ s/\|/,/g; |
93a5f4a5 AM |
352 | if(index($$hash{$key}[10],",") > 0){ |
353 | return "-m multiport --sport $$hash{$key}[10] "; | |
354 | }else{ | |
355 | return "--sport $$hash{$key}[10] "; | |
356 | } | |
62fc8511 | 357 | }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){ |
2a81ab0d | 358 | return "--icmp-type $$hash{$key}[9] "; |
62fc8511 AM |
359 | }elsif($$hash{$key}[9] eq 'All ICMP-Types'){ |
360 | return; | |
2a81ab0d AM |
361 | } |
362 | }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){ | |
b5269091 | 363 | |
2a81ab0d AM |
364 | if($$hash{$key}[14] eq 'TGT_PORT'){ |
365 | if ($$hash{$key}[15] ne ''){ | |
8f0b047b | 366 | $$hash{$key}[15] =~ s/\|/,/g; |
93a5f4a5 AM |
367 | if(index($$hash{$key}[15],",") > 0){ |
368 | return "-m multiport --dport $$hash{$key}[15] "; | |
369 | }else{ | |
370 | return "--dport $$hash{$key}[15] "; | |
371 | } | |
2a81ab0d AM |
372 | }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){ |
373 | return "--icmp-type $$hash{$key}[13] "; | |
374 | }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){ | |
375 | return; | |
376 | } | |
377 | }elsif($$hash{$key}[14] eq 'cust_srv'){ | |
378 | if ($prot ne 'ICMP'){ | |
379 | return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot); | |
380 | }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){ | |
381 | return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot); | |
382 | }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){ | |
383 | return; | |
384 | } | |
385 | }elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
386 | if ($prot ne 'ICMP'){ | |
387 | return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot); | |
388 | } | |
389 | elsif($prot eq 'ICMP'){ | |
390 | return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot); | |
391 | } | |
2a81ab0d AM |
392 | } |
393 | } | |
394 | } |