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