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