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