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