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