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