]>
Commit | Line | Data |
---|---|---|
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 | use Time::Local; | |
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 %configoutgoingfw=(); | |
47 | my %confignatfw=(); | |
48 | my %aliases=(); | |
49 | my @DPROT=(); | |
50 | my @p2ps=(); | |
51 | require '/var/ipfire/general-functions.pl'; | |
52 | require "${General::swroot}/lang.pl"; | |
53 | require "${General::swroot}/forward/bin/firewall-lib.pl"; | |
54 | ||
55 | my $configfwdfw = "${General::swroot}/forward/config"; | |
56 | my $configinput = "${General::swroot}/forward/input"; | |
57 | my $configoutgoing = "${General::swroot}/forward/outgoing"; | |
58 | my $confignat = "${General::swroot}/forward/nat"; | |
59 | my $p2pfile = "${General::swroot}/forward/p2protocols"; | |
60 | my $configgrp = "${General::swroot}/fwhosts/customgroups"; | |
61 | my $netsettings = "${General::swroot}/ethernet/settings"; | |
62 | my $errormessage=''; | |
63 | my $orange; | |
64 | my $green; | |
65 | my $blue; | |
66 | my ($TYPE,$PROT,$SPROT,$DPROT,$SPORT,$DPORT,$TIME,$TIMEFROM,$TIMETILL,$SRC_TGT); | |
67 | my $CHAIN="FORWARDFW"; | |
68 | my $conexists='off'; | |
69 | my $command = 'iptables -A'; | |
70 | my $dnat=''; | |
71 | my $snat=''; | |
72 | &General::readhash("${General::swroot}/forward/settings", \%fwdfwsettings); | |
73 | &General::readhash("$netsettings", \%defaultNetworks); | |
74 | &General::readhasharray($configfwdfw, \%configfwdfw); | |
75 | &General::readhasharray($configinput, \%configinputfw); | |
76 | &General::readhasharray($configoutgoing, \%configoutgoingfw); | |
77 | &General::readhasharray($confignat, \%confignatfw); | |
78 | &General::readhasharray($configgrp, \%customgrp); | |
79 | &General::get_aliases(\%aliases); | |
80 | ||
81 | #check if we have an internetconnection | |
82 | open (CONN,"/var/ipfire/red/iface"); | |
83 | my $con = <CONN>; | |
84 | close(CONN); | |
85 | if (-f "/var/ipfire/red/active"){ | |
86 | $conexists='on'; | |
87 | } | |
88 | open (CONN1,"/var/ipfire/red/local-ipaddress"); | |
89 | my $redip = <CONN1>; | |
90 | close(CONN1); | |
91 | ################################ | |
92 | # DEBUG/TEST # | |
93 | ################################ | |
94 | my $MODE=1; # 0 - normal operation | |
95 | # 1 - print configline and rules to console | |
96 | # | |
97 | ################################ | |
98 | my $param=shift; | |
99 | ||
100 | if($param eq 'flush'){ | |
101 | if ($MODE eq '1'){ | |
102 | print " Flushing chains...\n"; | |
103 | } | |
104 | &flush; | |
105 | }else{ | |
106 | if ($MODE eq '1'){ | |
107 | print " Flushing chains...\n"; | |
108 | } | |
109 | &flush; | |
110 | if ($MODE eq '1'){ | |
111 | print " Preparing rules...\n"; | |
112 | } | |
113 | &preparerules; | |
114 | if($MODE eq '0'){ | |
115 | if ($fwdfwsettings{'POLICY'} eq 'MODE1'){ | |
116 | &p2pblock; | |
117 | system ("/usr/sbin/firewall-policy"); | |
118 | }elsif($fwdfwsettings{'POLICY'} eq 'MODE2'){ | |
119 | $defaultNetworks{'GREEN_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'GREEN_NETMASK'}); | |
120 | $green="$defaultNetworks{'GREEN_ADDRESS'}/$defaultNetworks{'GREEN_NETMASK'}"; | |
121 | if ($defaultNetworks{'BLUE_DEV'}){ | |
122 | $defaultNetworks{'BLUE_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'BLUE_NETMASK'}); | |
123 | $blue="$defaultNetworks{'BLUE_ADDRESS'}/$defaultNetworks{'BLUE_NETMASK'}"; | |
124 | #set default rules for BLUE | |
125 | system ("iptables -A $CHAIN -s $blue -d $green -j RETURN"); | |
126 | } | |
127 | if ($defaultNetworks{'ORANGE_DEV'}){ | |
128 | $defaultNetworks{'ORANGE_NETMASK'}=&General::iporsubtocidr($defaultNetworks{'ORANGE_NETMASK'}); | |
129 | $orange="$defaultNetworks{'ORANGE_ADDRESS'}/$defaultNetworks{'ORANGE_NETMASK'}"; | |
130 | #set default rules for DMZ | |
131 | system ("iptables -A $CHAIN -s $orange -d $green -j RETURN"); | |
132 | if ($defaultNetworks{'BLUE_DEV'}){ | |
133 | system ("iptables -A $CHAIN -s $orange -d $blue -j RETURN"); | |
134 | } | |
135 | } | |
136 | &p2pblock; | |
137 | system ("iptables -A $CHAIN -m state --state NEW -j ACCEPT"); | |
138 | system ("/usr/sbin/firewall-policy"); | |
139 | system ("/etc/sysconfig/firewall.local reload"); | |
140 | } | |
141 | } | |
142 | } | |
143 | sub flush | |
144 | { | |
145 | system ("iptables -F FORWARDFW"); | |
146 | system ("iptables -F INPUTFW"); | |
147 | system ("iptables -F OUTGOINGFW"); | |
148 | system ("iptables -F PORTFWACCESS"); | |
149 | system ("iptables -t nat -F NAT_DESTINATION"); | |
150 | system ("iptables -t nat -F NAT_SOURCE"); | |
151 | } | |
152 | sub preparerules | |
153 | { | |
154 | if (! -z "${General::swroot}/forward/config"){ | |
155 | &buildrules(\%configfwdfw); | |
156 | } | |
157 | if (! -z "${General::swroot}/forward/input"){ | |
158 | &buildrules(\%configinputfw); | |
159 | } | |
160 | if (! -z "${General::swroot}/forward/outgoing"){ | |
161 | &buildrules(\%configoutgoingfw); | |
162 | } | |
163 | if (! -z "${General::swroot}/forward/nat"){ | |
164 | &buildrules(\%confignatfw); | |
165 | } | |
166 | } | |
167 | sub buildrules | |
168 | { | |
169 | my $hash=shift; | |
170 | my $STAG; | |
171 | my $natip; | |
172 | my $snatport; | |
173 | my $fireport; | |
174 | my $nat; | |
175 | my $fwaccessdport; | |
176 | foreach my $key (sort {$a <=> $b} keys %$hash){ | |
177 | next if (($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1') && $conexists eq 'off' ); | |
178 | if ($$hash{$key}[28] eq 'ON'){ | |
179 | $command='iptables -t nat -A'; | |
180 | $natip=&get_nat_ip($$hash{$key}[29],$$hash{$key}[31]); | |
181 | if($$hash{$key}[31] eq 'dnat'){ | |
182 | $nat='DNAT'; | |
183 | if ($$hash{$key}[30] =~ /\|/){ | |
184 | $$hash{$key}[30]=~ tr/|/,/; | |
185 | $fireport='-m multiport --dport '.$$hash{$key}[30]; | |
186 | }else{ | |
187 | $fireport='--dport '.$$hash{$key}[30] if ($$hash{$key}[30]>0); | |
188 | } | |
189 | }else{ | |
190 | $nat='SNAT'; | |
191 | } | |
192 | } | |
193 | $STAG=''; | |
194 | if($$hash{$key}[2] eq 'ON'){ | |
195 | #get source ip's | |
196 | if ($$hash{$key}[3] eq 'cust_grp_src'){ | |
197 | foreach my $grp (sort {$a <=> $b} keys %customgrp){ | |
198 | if($customgrp{$grp}[0] eq $$hash{$key}[4]){ | |
199 | &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"src"); | |
200 | } | |
201 | } | |
202 | }else{ | |
203 | &get_address($$hash{$key}[3],$$hash{$key}[4],"src"); | |
204 | } | |
205 | #get target ip's | |
206 | if ($$hash{$key}[5] eq 'cust_grp_tgt'){ | |
207 | foreach my $grp (sort {$a <=> $b} keys %customgrp){ | |
208 | if($customgrp{$grp}[0] eq $$hash{$key}[6]){ | |
209 | &get_address($customgrp{$grp}[3],$customgrp{$grp}[2],"tgt"); | |
210 | } | |
211 | } | |
212 | }elsif($$hash{$key}[5] eq 'ipfire'){ | |
213 | if($$hash{$key}[6] eq 'GREEN'){ | |
214 | $targethash{$key}[0]=$defaultNetworks{'GREEN_ADDRESS'}; | |
215 | } | |
216 | if($$hash{$key}[6] eq 'BLUE'){ | |
217 | $targethash{$key}[0]=$defaultNetworks{'BLUE_ADDRESS'}; | |
218 | } | |
219 | if($$hash{$key}[6] eq 'ORANGE'){ | |
220 | $targethash{$key}[0]=$defaultNetworks{'ORANGE_ADDRESS'}; | |
221 | } | |
222 | if($$hash{$key}[6] eq 'ALL'){ | |
223 | $targethash{$key}[0]='0.0.0.0/0'; | |
224 | } | |
225 | if($$hash{$key}[6] eq 'RED' || $$hash{$key}[6] eq 'RED1'){ | |
226 | open(FILE, "/var/ipfire/red/local-ipaddress")or die "Couldn't open local-ipaddress"; | |
227 | $targethash{$key}[0]= <FILE>; | |
228 | close(FILE); | |
229 | }else{ | |
230 | foreach my $alias (sort keys %aliases){ | |
231 | if ($$hash{$key}[6] eq $alias){ | |
232 | $targethash{$key}[0]=$aliases{$alias}{'IPT'}; | |
233 | } | |
234 | } | |
235 | } | |
236 | }else{ | |
237 | &get_address($$hash{$key}[5],$$hash{$key}[6],"tgt"); | |
238 | } | |
239 | ##get source prot and port | |
240 | $SRC_TGT='SRC'; | |
241 | $SPROT = &get_prot($hash,$key); | |
242 | $SPORT = &get_port($hash,$key); | |
243 | $SRC_TGT=''; | |
244 | ||
245 | ##get target prot and port | |
246 | $DPROT=&get_prot($hash,$key); | |
247 | ||
248 | if ($DPROT eq ''){$DPROT=' ';} | |
249 | @DPROT=split(",",$DPROT); | |
250 | ||
251 | #get time if defined | |
252 | if($$hash{$key}[18] eq 'ON'){ | |
253 | my ($time1,$time2,$daylight); | |
254 | my $daylight=$$hash{$key}[28]; | |
255 | $time1=&get_time($$hash{$key}[26],$daylight); | |
256 | $time2=&get_time($$hash{$key}[27],$daylight); | |
257 | if($$hash{$key}[19] ne ''){push (@timeframe,"Mon");} | |
258 | if($$hash{$key}[20] ne ''){push (@timeframe,"Tue");} | |
259 | if($$hash{$key}[21] ne ''){push (@timeframe,"Wed");} | |
260 | if($$hash{$key}[22] ne ''){push (@timeframe,"Thu");} | |
261 | if($$hash{$key}[23] ne ''){push (@timeframe,"Fri");} | |
262 | if($$hash{$key}[24] ne ''){push (@timeframe,"Sat");} | |
263 | if($$hash{$key}[25] ne ''){push (@timeframe,"Sun");} | |
264 | $TIME=join(",",@timeframe); | |
265 | ||
266 | $TIMEFROM="--timestart $time1 "; | |
267 | $TIMETILL="--timestop $time2 "; | |
268 | $TIME="-m time --weekdays $TIME $TIMEFROM $TIMETILL"; | |
269 | } | |
270 | if ($MODE eq '1'){ | |
271 | print "NR:$key "; | |
272 | foreach my $i (0 .. $#{$$hash{$key}}){ | |
273 | print "$i: $$hash{$key}[$i] "; | |
274 | } | |
275 | print "\n"; | |
276 | print"##################################\n"; | |
277 | #print rules to console | |
278 | foreach my $DPROT (@DPROT){ | |
279 | $DPORT = &get_port($hash,$key,$DPROT); | |
280 | if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;} | |
281 | $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' '); | |
282 | foreach my $a (sort keys %sourcehash){ | |
283 | foreach my $b (sort keys %targethash){ | |
284 | 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'){ | |
285 | if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){ | |
286 | if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";} | |
287 | if(substr($DPORT, 2, 4) eq 'icmp'){ | |
288 | my @icmprule= split(",",substr($DPORT, 12,)); | |
289 | foreach (@icmprule){ | |
290 | if ($$hash{$key}[17] eq 'ON'){ | |
291 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j LOG\n"; | |
292 | } | |
293 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]\n"; | |
294 | } | |
295 | }elsif($$hash{$key}[28] ne 'ON'){ | |
296 | if ($$hash{$key}[17] eq 'ON'){ | |
297 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n"; | |
298 | } | |
299 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n"; | |
300 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){ | |
301 | if ($$hash{$key}[17] eq 'ON'){ | |
302 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $fireport $TIME -j LOG --log-prefix 'DNAT' \n"; | |
303 | } | |
304 | my ($ip,$sub) =split("/",$targethash{$b}[0]); | |
305 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n"; | |
306 | $DPORT =~ s/\-/:/g; | |
307 | if ($DPORT){ | |
308 | $fwaccessdport="--dport ".substr($DPORT,1,); | |
309 | }elsif(! $DPORT && $$hash{$key}[30] ne ''){ | |
310 | if ($$hash{$key}[30]=~m/|/i){ | |
311 | $$hash{$key}[30] =~ s/\|/,/g; | |
312 | $fwaccessdport="-m multiport --dport $$hash{$key}[30]"; | |
313 | }else{ | |
314 | $fwaccessdport="--dport $$hash{$key}[30]"; | |
315 | } | |
316 | } | |
317 | print "iptables -A PORTFWACCESS $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n"; | |
318 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){ | |
319 | print "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip\n"; | |
320 | } | |
321 | } | |
322 | } | |
323 | } | |
324 | } | |
325 | print"\n"; | |
326 | } | |
327 | }elsif($MODE eq '0'){ | |
328 | foreach my $DPROT (@DPROT){ | |
329 | $DPORT = &get_port($hash,$key,$DPROT); | |
330 | if ($SPROT ne ''){$PROT=$SPROT;}else{$PROT=$DPROT;} | |
331 | $PROT="-p $PROT" if ($PROT ne '' && $PROT ne ' '); | |
332 | foreach my $a (sort keys %sourcehash){ | |
333 | foreach my $b (sort keys %targethash){ | |
334 | 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'){ | |
335 | if($SPROT eq '' || $SPROT eq $DPROT || $DPROT eq ' '){ | |
336 | if(substr($sourcehash{$a}[0], 3, 3) ne 'mac' && $sourcehash{$a}[0] ne ''){ $STAG="-s";} | |
337 | if(substr($DPORT, 2, 4) eq 'icmp'){ | |
338 | my @icmprule= split(",",substr($DPORT, 12,)); | |
339 | foreach (@icmprule){ | |
340 | if ($$hash{$key}[17] eq 'ON'){ | |
341 | system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] -- icmp-type $_ $TIME -j LOG"); | |
342 | } | |
343 | system ("$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] --icmp-type $_ $TIME -j $$hash{$key}[0]"); | |
344 | } | |
345 | }elsif($$hash{$key}[28] ne 'ON'){ | |
346 | if ($$hash{$key}[17] eq 'ON'){ | |
347 | system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG\n"; | |
348 | } | |
349 | system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $$hash{$key}[0]\n"; | |
350 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat'){ | |
351 | if ($$hash{$key}[17] eq 'ON'){ | |
352 | system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j LOG --log-prefix 'DNAT' \n"; | |
353 | } | |
354 | my ($ip,$sub) =split("/",$targethash{$b}[0]); | |
355 | system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT $natip $fireport $TIME -j $nat --to $ip$DPORT\n"; | |
356 | $DPORT =~ s/\-/:/g; | |
357 | if ($DPORT){ | |
358 | $fwaccessdport="--dport ".substr($DPORT,1,); | |
359 | }elsif(! $DPORT && $$hash{$key}[30] ne ''){ | |
360 | if ($$hash{$key}[30]=~m/|/i){ | |
361 | $$hash{$key}[30] =~ s/\|/,/g; | |
362 | $fwaccessdport="-m multiport --dport $$hash{$key}[30]"; | |
363 | }else{ | |
364 | $fwaccessdport="--dport $$hash{$key}[30]"; | |
365 | } | |
366 | } | |
367 | system "iptables -A PORTFWACCESS $PROT -i $con $STAG $sourcehash{$a}[0] -d $ip $fwaccessdport $TIME -j $$hash{$key}[0]\n"; | |
368 | }elsif($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat'){ | |
369 | if ($$hash{$key}[17] eq 'ON'){ | |
370 | system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j LOG --log-prefix 'SNAT '\n"; | |
371 | } | |
372 | system "$command $$hash{$key}[1] $PROT $STAG $sourcehash{$a}[0] $SPORT -d $targethash{$b}[0] $DPORT $TIME -j $nat --to $natip$fireport\n"; | |
373 | } | |
374 | } | |
375 | } | |
376 | } | |
377 | } | |
378 | } | |
379 | } | |
380 | } | |
381 | %sourcehash=(); | |
382 | %targethash=(); | |
383 | undef $TIME; | |
384 | undef $TIMEFROM; | |
385 | undef $TIMETILL; | |
386 | undef $fireport; | |
387 | } | |
388 | } | |
389 | sub get_nat_ip | |
390 | { | |
391 | my $val=shift; | |
392 | my $type=shift; | |
393 | my $result; | |
394 | if($val eq 'RED' || $val eq 'GREEN' || $val eq 'ORANGE' || $val eq 'BLUE'){ | |
395 | $result=$defaultNetworks{$val.'_ADDRESS'}; | |
396 | }elsif($val eq 'ALL'){ | |
397 | $result='-i '.$con; | |
398 | }elsif($val eq 'Default IP' && $type eq 'dnat'){ | |
399 | $result='-d '.$redip; | |
400 | }elsif($val eq 'Default IP' && $type eq 'snat'){ | |
401 | $result=$redip; | |
402 | }else{ | |
403 | foreach my $al (sort keys %aliases){ | |
404 | if($val eq $al && $type eq 'dnat'){ | |
405 | $result='-d '.$aliases{$al}{'IPT'}; | |
406 | }elsif($val eq $al && $type eq 'snat'){ | |
407 | $result=$aliases{$al}{'IPT'}; | |
408 | } | |
409 | } | |
410 | } | |
411 | return $result; | |
412 | } | |
413 | sub get_time | |
414 | { | |
415 | my $val=shift; | |
416 | my $val1=shift; | |
417 | my $time; | |
418 | my $minutes; | |
419 | my $ruletime; | |
420 | $minutes = &utcmin($val); | |
421 | $ruletime = $minutes + &time_get_utc($val); | |
422 | if ($ruletime < 0){$ruletime +=1440;} | |
423 | if ($ruletime > 1440){$ruletime -=1440;} | |
424 | $time=sprintf "%02d:%02d", $ruletime / 60, $ruletime % 60; | |
425 | return $time; | |
426 | } | |
427 | sub time_get_utc | |
428 | { | |
429 | # Calculates the UTCtime from a given time | |
430 | my $val=shift; | |
431 | my @localtime=localtime(time); | |
432 | my @gmtime=gmtime(time); | |
433 | my $diff = ($gmtime[2]*60+$gmtime[1]%60)-($localtime[2]*60+$localtime[1]%60); | |
434 | return $diff; | |
435 | } | |
436 | sub utcmin | |
437 | { | |
438 | my $ruletime=shift; | |
439 | my ($hrs,$min) = split(":",$ruletime); | |
440 | my $newtime = $hrs*60+$min; | |
441 | return $newtime; | |
442 | } | |
443 | sub p2pblock | |
444 | { | |
445 | my $P2PSTRING; | |
446 | my $DO; | |
447 | open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile"; | |
448 | @p2ps = <FILE>; | |
449 | close FILE; | |
450 | my $CMD = "-m ipp2p"; | |
451 | foreach my $p2pentry (sort @p2ps) { | |
452 | my @p2pline = split( /\;/, $p2pentry ); | |
453 | if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) { | |
454 | $DO = "ACCEPT"; | |
455 | if ("$p2pline[2]" eq "on") { | |
456 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; | |
457 | } | |
458 | }else { | |
459 | $DO = "RETURN"; | |
460 | if ("$p2pline[2]" eq "off") { | |
461 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; | |
462 | } | |
463 | } | |
464 | } | |
465 | if ($MODE eq 1){ | |
466 | if($P2PSTRING){ | |
467 | print"/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO\n"; | |
468 | } | |
469 | }else{ | |
470 | if($P2PSTRING){ | |
471 | system("/sbin/iptables -A FORWARDFW $CMD $P2PSTRING -j $DO"); | |
472 | } | |
473 | } | |
474 | } | |
475 | sub get_address | |
476 | { | |
477 | my $base=shift; #source of checking ($configfwdfw{$key}[x] or groupkey | |
478 | my $base2=shift; | |
479 | my $type=shift; #src or tgt | |
480 | my $hash; | |
481 | if ($type eq 'src'){ | |
482 | $hash=\%sourcehash; | |
483 | }else{ | |
484 | $hash=\%targethash; | |
485 | } | |
486 | my $key = &General::findhasharraykey($hash); | |
487 | if($base eq 'src_addr' || $base eq 'tgt_addr' ){ | |
488 | if (&General::validmac($base2)){ | |
489 | $$hash{$key}[0] = "-m mac --mac-source $base2"; | |
490 | }else{ | |
491 | $$hash{$key}[0] = $base2; | |
492 | } | |
493 | }elsif($base eq 'std_net_src' || $base eq 'std_net_tgt' || $base eq 'Standard Network'){ | |
494 | $$hash{$key}[0]=&fwlib::get_std_net_ip($base2,$con); | |
495 | }elsif($base eq 'cust_net_src' || $base eq 'cust_net_tgt' || $base eq 'Custom Network'){ | |
496 | $$hash{$key}[0]=&fwlib::get_net_ip($base2); | |
497 | }elsif($base eq 'cust_host_src' || $base eq 'cust_host_tgt' || $base eq 'Custom Host'){ | |
498 | $$hash{$key}[0]=&fwlib::get_host_ip($base2,$type); | |
499 | }elsif($base eq 'ovpn_net_src' || $base eq 'ovpn_net_tgt' || $base eq 'OpenVPN static network'){ | |
500 | $$hash{$key}[0]=&fwlib::get_ovpn_net_ip($base2,1); | |
501 | }elsif($base eq 'ovpn_host_src' ||$base eq 'ovpn_host_tgt' || $base eq 'OpenVPN static host'){ | |
502 | $$hash{$key}[0]=&fwlib::get_ovpn_host_ip($base2,33); | |
503 | }elsif($base eq 'ovpn_n2n_src' ||$base eq 'ovpn_n2n_tgt' || $base eq 'OpenVPN N-2-N'){ | |
504 | $$hash{$key}[0]=&fwlib::get_ovpn_n2n_ip($base2,11); | |
505 | }elsif($base eq 'ipsec_net_src' || $base eq 'ipsec_net_tgt' || $base eq 'IpSec Network'){ | |
506 | $$hash{$key}[0]=&fwlib::get_ipsec_net_ip($base2,11); | |
507 | } | |
508 | } | |
509 | sub get_prot | |
510 | { | |
511 | my $hash=shift; | |
512 | my $key=shift; | |
513 | if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){ | |
514 | if ($$hash{$key}[10] ne ''){ | |
515 | return"$$hash{$key}[8]"; | |
516 | }elsif($$hash{$key}[9] ne ''){ | |
517 | return"$$hash{$key}[8]"; | |
518 | }else{ | |
519 | return "$$hash{$key}[8]"; | |
520 | } | |
521 | }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){ | |
522 | if ($$hash{$key}[14] eq 'TGT_PORT'){ | |
523 | if ($$hash{$key}[15] ne ''){ | |
524 | return "$$hash{$key}[12]"; | |
525 | }elsif($$hash{$key}[13] ne ''){ | |
526 | return "$$hash{$key}[12]"; | |
527 | }else{ | |
528 | return "$$hash{$key}[12]"; | |
529 | } | |
530 | }elsif($$hash{$key}[14] eq 'cust_srv'){ | |
531 | return &fwlib::get_srv_prot($$hash{$key}[15]); | |
532 | ||
533 | }elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
534 | return &fwlib::get_srvgrp_prot($$hash{$key}[15]); | |
535 | } | |
536 | } | |
537 | #DNAT | |
538 | if ($SRC_TGT eq '' && $$hash{$key}[31] eq 'dnat' && $$hash{$key}[11] eq '' && $$hash{$key}[12] ne ''){ | |
539 | return "$$hash{$key}[12]"; | |
540 | } | |
541 | } | |
542 | sub get_port | |
543 | { | |
544 | my $hash=shift; | |
545 | my $key=shift; | |
546 | my $prot=shift; | |
547 | if ($$hash{$key}[7] eq 'ON' && $SRC_TGT eq 'SRC'){ | |
548 | if ($$hash{$key}[10] ne ''){ | |
549 | $$hash{$key}[10] =~ s/\|/,/g; | |
550 | if(index($$hash{$key}[10],",") > 0){ | |
551 | return "-m multiport --sport $$hash{$key}[10] "; | |
552 | }else{ | |
553 | if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ||($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'dnat') ){ | |
554 | return "--sport $$hash{$key}[10] "; | |
555 | }else{ | |
556 | return ":$$hash{$key}[10]"; | |
557 | } | |
558 | } | |
559 | }elsif($$hash{$key}[9] ne '' && $$hash{$key}[9] ne 'All ICMP-Types'){ | |
560 | return "--icmp-type $$hash{$key}[9] "; | |
561 | }elsif($$hash{$key}[9] eq 'All ICMP-Types'){ | |
562 | return; | |
563 | } | |
564 | }elsif($$hash{$key}[11] eq 'ON' && $SRC_TGT eq ''){ | |
565 | if($$hash{$key}[14] eq 'TGT_PORT'){ | |
566 | if ($$hash{$key}[15] ne ''){ | |
567 | $$hash{$key}[15] =~ s/\|/,/g; | |
568 | if(index($$hash{$key}[15],",") > 0){ | |
569 | return "-m multiport --dport $$hash{$key}[15] "; | |
570 | }else{ | |
571 | if($$hash{$key}[28] ne 'ON' || ($$hash{$key}[28] eq 'ON' && $$hash{$key}[31] eq 'snat') ){ | |
572 | return "--dport $$hash{$key}[15] "; | |
573 | }else{ | |
574 | $$hash{$key}[15] =~ s/\:/-/g; | |
575 | return ":$$hash{$key}[15]"; | |
576 | } | |
577 | } | |
578 | }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] ne 'All ICMP-Types'){ | |
579 | return "--icmp-type $$hash{$key}[13] "; | |
580 | }elsif($$hash{$key}[13] ne '' && $$hash{$key}[13] eq 'All ICMP-Types'){ | |
581 | return; | |
582 | } | |
583 | }elsif($$hash{$key}[14] eq 'cust_srv'){ | |
584 | if ($prot ne 'ICMP'){ | |
585 | if($$hash{$key}[31] eq 'dnat'){ | |
586 | return ":".&fwlib::get_srv_port($$hash{$key}[15],1,$prot); | |
587 | }else{ | |
588 | return "--dport ".&fwlib::get_srv_port($$hash{$key}[15],1,$prot); | |
589 | } | |
590 | }elsif($prot eq 'ICMP' && $$hash{$key}[15] ne 'All ICMP-Types'){ | |
591 | return "--icmp-type ".&fwlib::get_srv_port($$hash{$key}[15],3,$prot); | |
592 | }elsif($prot eq 'ICMP' && $$hash{$key}[15] eq 'All ICMP-Types'){ | |
593 | return; | |
594 | } | |
595 | }elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
596 | if ($prot ne 'ICMP'){ | |
597 | return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot); | |
598 | } | |
599 | elsif($prot eq 'ICMP'){ | |
600 | return &fwlib::get_srvgrp_port($$hash{$key}[15],$prot); | |
601 | } | |
602 | } | |
603 | } | |
604 | } |