]>
Commit | Line | Data |
---|---|---|
6178953b | 1 | #!/usr/bin/perl -w |
2a81ab0d AM |
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; |
2a81ab0d | 23 | |
97ab0569 MT |
24 | require '/var/ipfire/general-functions.pl'; |
25 | require "${General::swroot}/lang.pl"; | |
26 | require "/usr/lib/firewall/firewall-lib.pl"; | |
2a81ab0d | 27 | |
68d1eb10 MT |
28 | # Set to one to enable debugging mode. |
29 | my $DEBUG = 0; | |
30 | ||
1f9e7b53 MT |
31 | my $IPTABLES = "iptables --wait"; |
32 | ||
6178953b | 33 | # iptables chains |
8f4f4634 MT |
34 | my $CHAIN_INPUT = "INPUTFW"; |
35 | my $CHAIN_FORWARD = "FORWARDFW"; | |
d98aa95a | 36 | my $CHAIN_OUTPUT = "OUTGOINGFW"; |
8f4f4634 | 37 | my $CHAIN = $CHAIN_FORWARD; |
6178953b MT |
38 | my $CHAIN_NAT_SOURCE = "NAT_SOURCE"; |
39 | my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION"; | |
6e87f0aa | 40 | my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION"; |
8f4f4634 | 41 | my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT); |
c2a1af75 | 42 | my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0"); |
8f4f4634 MT |
43 | |
44 | my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip"); | |
45 | my @PROTOCOLS_WITH_PORTS = ("tcp", "udp"); | |
46 | ||
47 | my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT"); | |
6178953b | 48 | |
2a81ab0d AM |
49 | my %fwdfwsettings=(); |
50 | my %defaultNetworks=(); | |
8f4f4634 | 51 | my %configfwdfw=();; |
2a81ab0d | 52 | my %customgrp=(); |
2a81ab0d | 53 | my %configinputfw=(); |
5d7faa45 | 54 | my %configoutgoingfw=(); |
a6edca5a | 55 | my %confignatfw=(); |
2a81ab0d | 56 | my %aliases=(); |
36196d0d | 57 | my @p2ps=(); |
2a81ab0d | 58 | |
6d8eb5de AM |
59 | my $configfwdfw = "${General::swroot}/firewall/config"; |
60 | my $configinput = "${General::swroot}/firewall/input"; | |
61 | my $configoutgoing = "${General::swroot}/firewall/outgoing"; | |
62 | my $p2pfile = "${General::swroot}/firewall/p2protocols"; | |
2a81ab0d | 63 | my $configgrp = "${General::swroot}/fwhosts/customgroups"; |
210ee67b | 64 | my $netsettings = "${General::swroot}/ethernet/settings"; |
86a921ee | 65 | |
6d8eb5de | 66 | &General::readhash("${General::swroot}/firewall/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 | ||
3bb4bb3f MT |
74 | my @log_limit_options = &make_log_limit_options(); |
75 | ||
8531b94a MT |
76 | # MAIN |
77 | &main(); | |
78 | ||
79 | sub main { | |
80 | # Flush all chains. | |
81 | &flush(); | |
82 | ||
83 | # Reload firewall rules. | |
84 | &preparerules(); | |
85 | ||
86 | # Load P2P block rules. | |
87 | &p2pblock(); | |
88 | ||
89 | # Reload firewall policy. | |
90 | run("/usr/sbin/firewall-policy"); | |
2a81ab0d | 91 | } |
97ab0569 | 92 | |
68d1eb10 MT |
93 | sub run { |
94 | # Executes or prints the given shell command. | |
95 | my $command = shift; | |
96 | ||
97 | if ($DEBUG) { | |
98 | print "$command\n"; | |
99 | } else { | |
100 | system "$command"; | |
6e87f0aa MT |
101 | |
102 | if ($?) { | |
103 | print_error("ERROR: $command"); | |
104 | } | |
68d1eb10 MT |
105 | } |
106 | } | |
107 | ||
6178953b MT |
108 | sub print_error { |
109 | my $message = shift; | |
110 | ||
111 | print STDERR "$message\n"; | |
112 | } | |
113 | ||
8f4f4634 MT |
114 | sub print_rule { |
115 | my $hash = shift; | |
116 | ||
117 | print "\nRULE:"; | |
118 | ||
119 | my $i = 0; | |
120 | foreach (@$hash) { | |
121 | printf(" %2d: %s", $i++, $_); | |
122 | } | |
123 | print "\n"; | |
124 | } | |
125 | ||
97ab0569 | 126 | sub flush { |
d98aa95a MT |
127 | run("$IPTABLES -F $CHAIN_INPUT"); |
128 | run("$IPTABLES -F $CHAIN_FORWARD"); | |
129 | run("$IPTABLES -F $CHAIN_OUTPUT"); | |
130 | run("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE"); | |
131 | run("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION"); | |
6e87f0aa | 132 | run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX"); |
86a921ee | 133 | } |
97ab0569 MT |
134 | |
135 | sub preparerules { | |
6d8eb5de | 136 | if (! -z "${General::swroot}/firewall/config"){ |
2a81ab0d AM |
137 | &buildrules(\%configfwdfw); |
138 | } | |
6d8eb5de | 139 | if (! -z "${General::swroot}/firewall/input"){ |
2a81ab0d AM |
140 | &buildrules(\%configinputfw); |
141 | } | |
6d8eb5de | 142 | if (! -z "${General::swroot}/firewall/outgoing"){ |
5d7faa45 AM |
143 | &buildrules(\%configoutgoingfw); |
144 | } | |
2a81ab0d | 145 | } |
97ab0569 MT |
146 | |
147 | sub buildrules { | |
8f4f4634 MT |
148 | my $hash = shift; |
149 | ||
150 | foreach my $key (sort {$a <=> $b} keys %$hash) { | |
151 | # Skip disabled rules. | |
152 | next unless ($$hash{$key}[2] eq 'ON'); | |
153 | ||
154 | if ($DEBUG) { | |
155 | print_rule($$hash{$key}); | |
156 | } | |
157 | ||
158 | # Check if the target is valid. | |
159 | my $target = $$hash{$key}[0]; | |
160 | if (!$target ~~ @VALID_TARGETS) { | |
161 | print_error("Invalid target '$target' for rule $key"); | |
162 | next; | |
163 | } | |
164 | ||
165 | # Check if the chain is valid. | |
166 | my $chain = $$hash{$key}[1]; | |
167 | if (!$chain ~~ @VALID_CHAINS) { | |
168 | print_error("Invalid chain '$chain' in rule $key"); | |
169 | next; | |
170 | } | |
171 | ||
172 | # Collect all sources. | |
173 | my @sources = &get_addresses($hash, $key, "src"); | |
174 | ||
175 | # Collect all destinations. | |
176 | my @destinations = &get_addresses($hash, $key, "tgt"); | |
6178953b | 177 | |
6178953b | 178 | # Check if logging should be enabled. |
8f4f4634 | 179 | my $LOG = ($$hash{$key}[17] eq 'ON'); |
6178953b | 180 | |
8f4f4634 MT |
181 | # Check if NAT is enabled and initialize variables, that we use for that. |
182 | my $NAT = ($$hash{$key}[28] eq 'ON'); | |
6178953b | 183 | my $NAT_MODE; |
8f4f4634 MT |
184 | if ($NAT) { |
185 | $NAT_MODE = uc($$hash{$key}[31]); | |
186 | } | |
6178953b | 187 | |
8f4f4634 MT |
188 | # Set up time constraints. |
189 | my @time_options = (); | |
190 | if ($$hash{$key}[18] eq 'ON') { | |
191 | push(@time_options, ("-m", "time")); | |
6178953b | 192 | |
8f4f4634 MT |
193 | # Select all days of the week this match is active. |
194 | my @weekdays = (); | |
195 | if ($$hash{$key}[19] ne '') { | |
196 | push (@weekdays, "Mon"); | |
197 | } | |
198 | if ($$hash{$key}[20] ne '') { | |
199 | push (@weekdays, "Tue"); | |
200 | } | |
201 | if ($$hash{$key}[21] ne '') { | |
202 | push (@weekdays, "Wed"); | |
203 | } | |
204 | if ($$hash{$key}[22] ne '') { | |
205 | push (@weekdays, "Thu"); | |
206 | } | |
207 | if ($$hash{$key}[23] ne '') { | |
208 | push (@weekdays, "Fri"); | |
209 | } | |
210 | if ($$hash{$key}[24] ne '') { | |
211 | push (@weekdays, "Sat"); | |
212 | } | |
213 | if ($$hash{$key}[25] ne '') { | |
214 | push (@weekdays, "Sun"); | |
215 | } | |
216 | if (@weekdays) { | |
217 | push(@time_options, ("--weekdays", join(",", @weekdays))); | |
218 | } | |
6178953b | 219 | |
8f4f4634 MT |
220 | # Convert start time. |
221 | my $time_start = &format_time($$hash{$key}[26]); | |
222 | if ($time_start) { | |
223 | push(@time_options, ("--timestart", $time_start)); | |
a6edca5a | 224 | } |
6178953b | 225 | |
8f4f4634 MT |
226 | # Convert end time. |
227 | my $time_stop = &format_time($$hash{$key}[27]); | |
228 | if ($time_stop) { | |
229 | push(@time_options, ("--timestop", $time_stop)); | |
230 | } | |
a6edca5a | 231 | } |
6178953b | 232 | |
8f4f4634 MT |
233 | # Check which protocols are used in this rule and so that we can |
234 | # later group rules by protocols. | |
235 | my @protocols = &get_protocols($hash, $key); | |
236 | if (!@protocols) { | |
237 | print_error("Invalid protocol configuration for rule $key"); | |
238 | next; | |
239 | } | |
240 | ||
241 | foreach my $protocol (@protocols) { | |
242 | # Check if the given protocol is supported. | |
243 | if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) { | |
244 | print_error("Protocol $protocol is not supported (rule $key)"); | |
245 | next; | |
2a81ab0d | 246 | } |
8f4f4634 MT |
247 | |
248 | # Prepare protocol options (like ICMP types, ports, etc...). | |
249 | my @protocol_options = &get_protocol_options($hash, $key, $protocol); | |
250 | ||
251 | # Check if this protocol knows ports. | |
252 | my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS); | |
253 | ||
254 | foreach my $source (@sources) { | |
255 | foreach my $destination (@destinations) { | |
256 | # Skip invalid rules. | |
257 | next if (!$source || !$destination || ($destination eq "none")); | |
258 | ||
c2a1af75 MT |
259 | # Sanitize source. |
260 | if ($source ~~ @ANY_ADDRESSES) { | |
261 | $source = ""; | |
262 | } | |
263 | ||
264 | # Sanitize destination. | |
265 | if ($destination ~~ @ANY_ADDRESSES) { | |
266 | $destination = ""; | |
267 | } | |
268 | ||
8f4f4634 MT |
269 | # Array with iptables arguments. |
270 | my @options = (); | |
271 | ||
272 | # Append protocol. | |
273 | if ($protocol ne "all") { | |
274 | push(@options, ("-p", $protocol)); | |
275 | push(@options, @protocol_options); | |
2a81ab0d | 276 | } |
8f4f4634 | 277 | |
6e87f0aa MT |
278 | # Prepare source options. |
279 | my @source_options = (); | |
8f4f4634 | 280 | if ($source =~ /mac/) { |
6e87f0aa | 281 | push(@source_options, $source); |
c2a1af75 | 282 | } elsif ($source) { |
6e87f0aa | 283 | push(@source_options, ("-s", $source)); |
2a81ab0d | 284 | } |
14f7cb87 | 285 | |
6e87f0aa | 286 | # Prepare destination options. |
c2a1af75 MT |
287 | my @destination_options = (); |
288 | if ($destination) { | |
289 | push(@destination_options, ("-d", $destination)); | |
290 | } | |
14f7cb87 | 291 | |
8f4f4634 MT |
292 | # Add time constraint options. |
293 | push(@options, @time_options); | |
14f7cb87 | 294 | |
e9b5ba41 MT |
295 | my $firewall_is_in_source_subnet = 0; |
296 | if ($source) { | |
da7a2208 | 297 | $firewall_is_in_source_subnet = &firewall_is_in_subnet($source); |
e9b5ba41 MT |
298 | } |
299 | ||
8f4f4634 MT |
300 | # Process NAT rules. |
301 | if ($NAT) { | |
5cf8c8c1 | 302 | my $nat_address = &get_nat_address($$hash{$key}[29], $source); |
b05ec50a | 303 | |
8f4f4634 MT |
304 | # Skip NAT rules if the NAT address is unknown |
305 | # (i.e. no internet connection has been established, yet). | |
306 | next unless ($nat_address); | |
b05ec50a | 307 | |
8f4f4634 MT |
308 | # Destination NAT |
309 | if ($NAT_MODE eq "DNAT") { | |
6e87f0aa | 310 | # Make port-forwardings useable from the internal networks. |
da7a2208 | 311 | my @internal_addresses = &get_internal_firewall_ip_addresses(1); |
5cf8c8c1 MT |
312 | unless ($nat_address ~~ @internal_addresses) { |
313 | &add_dnat_mangle_rules($nat_address, @options); | |
314 | } | |
b05ec50a | 315 | |
8f4f4634 | 316 | my @nat_options = @options; |
6e87f0aa | 317 | push(@nat_options, @source_options); |
8f4f4634 | 318 | push(@nat_options, ("-d", $nat_address)); |
6e87f0aa MT |
319 | |
320 | my ($dnat_address, $dnat_mask) = split("/", $destination); | |
321 | @destination_options = ("-d", $dnat_address); | |
b05ec50a | 322 | |
8f4f4634 MT |
323 | if ($protocol_has_ports) { |
324 | my $dnat_port = &get_dnat_target_port($hash, $key); | |
b05ec50a | 325 | |
8f4f4634 MT |
326 | if ($dnat_port) { |
327 | $dnat_address .= ":$dnat_port"; | |
86a921ee | 328 | } |
2a81ab0d | 329 | } |
8f4f4634 MT |
330 | |
331 | if ($LOG) { | |
3bb4bb3f | 332 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '"); |
8f4f4634 MT |
333 | } |
334 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j DNAT --to-destination $dnat_address"); | |
335 | ||
336 | # Source NAT | |
337 | } elsif ($NAT_MODE eq "SNAT") { | |
6e87f0aa MT |
338 | my @nat_options = @options; |
339 | ||
340 | push(@nat_options, @source_options); | |
341 | push(@nat_options, @destination_options); | |
342 | ||
8f4f4634 | 343 | if ($LOG) { |
3bb4bb3f | 344 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '"); |
8f4f4634 | 345 | } |
6e87f0aa | 346 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address"); |
2a81ab0d AM |
347 | } |
348 | } | |
8f4f4634 | 349 | |
6e87f0aa | 350 | push(@options, @source_options); |
e9b5ba41 MT |
351 | |
352 | if ($firewall_is_in_source_subnet && ($fwdfwsettings{"POLICY"} eq "MODE1") && ($chain eq $CHAIN_FORWARD)) { | |
353 | if ($LOG && !$NAT) { | |
354 | run("$IPTABLES -A $CHAIN_INPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '"); | |
355 | } | |
356 | run("$IPTABLES -A $CHAIN_INPUT @options -j $target"); | |
357 | } | |
358 | ||
6e87f0aa MT |
359 | push(@options, @destination_options); |
360 | ||
8f4f4634 MT |
361 | # Insert firewall rule. |
362 | if ($LOG && !$NAT) { | |
0bda23f5 | 363 | run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '"); |
8f4f4634 MT |
364 | } |
365 | run("$IPTABLES -A $chain @options -j $target"); | |
2a81ab0d AM |
366 | } |
367 | } | |
368 | } | |
2a81ab0d AM |
369 | } |
370 | } | |
97ab0569 | 371 | |
8f4f4634 MT |
372 | sub get_external_interface() { |
373 | open(IFACE, "/var/ipfire/red/iface") or return ""; | |
374 | my $iface = <IFACE>; | |
375 | close(IFACE); | |
376 | ||
377 | return $iface; | |
378 | } | |
379 | ||
380 | sub get_external_address() { | |
381 | open(ADDR, "/var/ipfire/red/local-ipaddress") or return ""; | |
382 | my $address = <ADDR>; | |
383 | close(ADDR); | |
384 | ||
385 | return $address; | |
386 | } | |
387 | ||
388 | sub get_alias { | |
389 | my $id = shift; | |
390 | ||
391 | foreach my $alias (sort keys %aliases) { | |
392 | if ($id eq $alias) { | |
393 | return $aliases{$alias}; | |
a6edca5a AM |
394 | } |
395 | } | |
8f4f4634 MT |
396 | } |
397 | ||
398 | sub get_nat_address { | |
399 | my $zone = shift; | |
5cf8c8c1 | 400 | my $source = shift; |
8f4f4634 MT |
401 | |
402 | # Any static address of any zone. | |
b0d9fad3 | 403 | if ($zone eq "AUTO") { |
5cf8c8c1 | 404 | if ($source) { |
da7a2208 | 405 | my $firewall_ip = &get_internal_firewall_ip_address($source, 1); |
b0d9fad3 MT |
406 | if ($firewall_ip) { |
407 | return $firewall_ip; | |
408 | } | |
5cf8c8c1 | 409 | |
b0d9fad3 | 410 | $firewall_ip = &get_matching_firewall_address($source, 1); |
5cf8c8c1 MT |
411 | if ($firewall_ip) { |
412 | return $firewall_ip; | |
413 | } | |
414 | } | |
415 | ||
8f4f4634 MT |
416 | return &get_external_address(); |
417 | ||
b0d9fad3 MT |
418 | } elsif ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") { |
419 | return $defaultNetworks{$zone . "_ADDRESS"}; | |
420 | ||
421 | } elsif ($zone eq "Default IP") { | |
422 | return &get_external_address(); | |
423 | ||
8f4f4634 MT |
424 | } else { |
425 | return &get_alias($zone); | |
426 | } | |
427 | ||
428 | print_error("Could not find NAT address"); | |
a6edca5a | 429 | } |
97ab0569 | 430 | |
b05ec50a MT |
431 | # Formats the given timestamp into the iptables format which is "hh:mm" UTC. |
432 | sub format_time { | |
433 | my $val = shift; | |
434 | ||
435 | # Convert the given time into minutes. | |
436 | my $minutes = &time_convert_to_minutes($val); | |
437 | ||
438 | # Move the timestamp into UTC. | |
439 | $minutes += &time_utc_offset(); | |
440 | ||
441 | # Make sure $minutes is between 00:00 and 23:59. | |
442 | if ($minutes < 0) { | |
443 | $minutes += 1440; | |
444 | } | |
445 | ||
446 | if ($minutes > 1440) { | |
447 | $minutes -= 1440; | |
448 | } | |
449 | ||
450 | # Format as hh:mm. | |
451 | return sprintf("%02d:%02d", $minutes / 60, $minutes % 60); | |
472136c9 | 452 | } |
97ab0569 | 453 | |
b05ec50a MT |
454 | # Calculates the offsets in minutes from the local timezone to UTC. |
455 | sub time_utc_offset { | |
456 | my @localtime = localtime(time); | |
457 | my @gmtime = gmtime(time); | |
458 | ||
459 | return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60); | |
472136c9 | 460 | } |
97ab0569 | 461 | |
b05ec50a MT |
462 | # Takes a timestamp like "14:00" and converts it into minutes since midnight. |
463 | sub time_convert_to_minutes { | |
464 | my ($hrs, $min) = split(":", shift); | |
465 | ||
466 | return ($hrs * 60) + $min; | |
472136c9 | 467 | } |
97ab0569 MT |
468 | |
469 | sub p2pblock { | |
6178953b | 470 | my $P2PSTRING = ""; |
36196d0d AM |
471 | my $DO; |
472 | open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile"; | |
473 | @p2ps = <FILE>; | |
474 | close FILE; | |
475 | my $CMD = "-m ipp2p"; | |
476 | foreach my $p2pentry (sort @p2ps) { | |
477 | my @p2pline = split( /\;/, $p2pentry ); | |
8d1beadc AM |
478 | if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) { |
479 | $DO = "ACCEPT"; | |
5238a871 | 480 | if ("$p2pline[2]" eq "on") { |
36196d0d AM |
481 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; |
482 | } | |
8d1beadc | 483 | }else { |
36196d0d | 484 | $DO = "RETURN"; |
5238a871 | 485 | if ("$p2pline[2]" eq "off") { |
36196d0d AM |
486 | $P2PSTRING = "$P2PSTRING --$p2pline[1]"; |
487 | } | |
488 | } | |
489 | } | |
68d1eb10 MT |
490 | |
491 | if($P2PSTRING) { | |
1f9e7b53 | 492 | run("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO"); |
36196d0d AM |
493 | } |
494 | } | |
97ab0569 | 495 | |
8f4f4634 MT |
496 | sub get_addresses { |
497 | my $hash = shift; | |
498 | my $key = shift; | |
499 | my $type = shift; | |
500 | ||
501 | my @addresses = (); | |
502 | my $addr_type; | |
503 | my $value; | |
504 | my $group_name; | |
505 | ||
506 | if ($type eq "src") { | |
507 | $addr_type = $$hash{$key}[3]; | |
508 | $value = $$hash{$key}[4]; | |
509 | ||
510 | } elsif ($type eq "tgt") { | |
511 | $addr_type = $$hash{$key}[5]; | |
512 | $value = $$hash{$key}[6]; | |
513 | } | |
514 | ||
515 | if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) { | |
516 | foreach my $grp (sort {$a <=> $b} keys %customgrp) { | |
517 | if ($customgrp{$grp}[0] eq $value) { | |
518 | my @address = &get_address($customgrp{$grp}[3], $customgrp{$grp}[2], $type); | |
519 | ||
520 | if (@address) { | |
521 | push(@addresses, @address); | |
522 | } | |
523 | } | |
524 | } | |
525 | } else { | |
526 | my @address = &get_address($addr_type, $value, $type); | |
527 | ||
528 | if (@address) { | |
529 | push(@addresses, @address); | |
530 | } | |
2a81ab0d | 531 | } |
8f4f4634 MT |
532 | |
533 | return @addresses; | |
534 | } | |
535 | ||
536 | sub get_address { | |
537 | my $key = shift; | |
538 | my $value = shift; | |
539 | my $type = shift; | |
540 | ||
541 | my @ret = (); | |
542 | ||
543 | # If the user manually typed an address, we just check if it is a MAC | |
544 | # address. Otherwise, we assume that it is an IP address. | |
545 | if ($key ~~ ["src_addr", "tgt_addr"]) { | |
546 | if (&General::validmac($value)) { | |
547 | push(@ret, "-m mac --mac-source $value"); | |
548 | } else { | |
549 | push(@ret, $value); | |
550 | } | |
551 | ||
552 | # If a default network interface (GREEN, BLUE, etc.) is selected, we | |
553 | # try to get the corresponding address of the network. | |
554 | } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) { | |
555 | my $external_interface = &get_external_interface(); | |
556 | ||
557 | my $network_address = &fwlib::get_std_net_ip($value, $external_interface); | |
558 | if ($network_address) { | |
559 | push(@ret, $network_address); | |
b5269091 | 560 | } |
8f4f4634 MT |
561 | |
562 | # Custom networks. | |
563 | } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) { | |
564 | my $network_address = &fwlib::get_net_ip($value); | |
565 | if ($network_address) { | |
566 | push(@ret, $network_address); | |
a0fb1099 | 567 | } |
8f4f4634 MT |
568 | |
569 | # Custom hosts. | |
570 | } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) { | |
571 | my $host_address = &fwlib::get_host_ip($value, $type); | |
572 | if ($host_address) { | |
573 | push(@ret, $host_address); | |
a0fb1099 | 574 | } |
8f4f4634 MT |
575 | |
576 | # OpenVPN networks. | |
577 | } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) { | |
578 | my $network_address = &fwlib::get_ovpn_net_ip($value, 1); | |
579 | if ($network_address) { | |
580 | push(@ret, $network_address); | |
a0fb1099 | 581 | } |
8f4f4634 MT |
582 | |
583 | # OpenVPN hosts. | |
584 | } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) { | |
585 | my $host_address = &fwlib::get_ovpn_host_ip($value, 33); | |
586 | if ($host_address) { | |
587 | push(@ret, $host_address); | |
a0fb1099 | 588 | } |
8f4f4634 MT |
589 | |
590 | # OpenVPN N2N. | |
591 | } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) { | |
592 | my $network_address = &fwlib::get_ovpn_n2n_ip($value, 11); | |
593 | if ($network_address) { | |
594 | push(@ret, $network_address); | |
595 | } | |
596 | ||
597 | # IPsec networks. | |
598 | } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) { | |
599 | my $network_address = &fwlib::get_ipsec_net_ip($value, 11); | |
600 | if ($network_address) { | |
601 | push(@ret, $network_address); | |
602 | } | |
603 | ||
604 | # The firewall's own IP addresses. | |
605 | } elsif ($key ~~ ["ipfire", "ipfire_src"]) { | |
606 | # ALL | |
607 | if ($value eq "ALL") { | |
608 | push(@ret, "0/0"); | |
609 | ||
610 | # GREEN | |
611 | } elsif ($value eq "GREEN") { | |
612 | push(@ret, $defaultNetworks{"GREEN_ADDRESS"}); | |
613 | ||
614 | # BLUE | |
615 | } elsif ($value eq "BLUE") { | |
616 | push(@ret, $defaultNetworks{"BLUE_ADDRESS"}); | |
617 | ||
618 | # ORANGE | |
619 | } elsif ($value eq "ORANGE") { | |
620 | push(@ret, $defaultNetworks{"ORANGE_ADDRESS"}); | |
621 | ||
622 | # RED | |
623 | } elsif ($value ~~ ["RED", "RED1"]) { | |
624 | my $address = &get_external_address(); | |
625 | if ($address) { | |
626 | push(@ret, $address); | |
627 | } | |
628 | ||
629 | # Aliases | |
630 | } else { | |
631 | my %alias = &get_alias($value); | |
632 | if (%alias) { | |
633 | push(@ret, $alias{"IPT"}); | |
a0fb1099 AM |
634 | } |
635 | } | |
8f4f4634 MT |
636 | |
637 | # If nothing was selected, we assume "any". | |
638 | } else { | |
639 | push(@ret, "0/0"); | |
2a81ab0d | 640 | } |
8f4f4634 MT |
641 | |
642 | return @ret; | |
2a81ab0d | 643 | } |
97ab0569 | 644 | |
8f4f4634 MT |
645 | sub get_protocols { |
646 | my $hash = shift; | |
647 | my $key = shift; | |
648 | ||
649 | my $uses_source_ports = ($$hash{$key}[7] eq "ON"); | |
650 | my $uses_services = ($$hash{$key}[11] eq "ON"); | |
651 | ||
652 | my @protocols = (); | |
653 | ||
654 | # Rules which don't have source ports or services (like ICMP, ESP, ...). | |
655 | if (!$uses_source_ports && !$uses_services) { | |
656 | push(@protocols, $$hash{$key}[8]); | |
657 | ||
658 | # Rules which either use ports or services. | |
659 | } elsif ($uses_source_ports || $uses_services) { | |
660 | # Check if service group or service | |
661 | if ($$hash{$key}[14] eq 'cust_srv') { | |
662 | push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15])); | |
663 | ||
664 | } elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
665 | my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]); | |
666 | push(@protocols, split(",", $protos)); | |
667 | ||
668 | } else { | |
669 | # Fetch the protocol for this rule. | |
670 | my $protocol = lc($$hash{$key}[8]); | |
671 | ||
672 | # Fetch source and destination ports for this rule. | |
673 | my $source_ports = $$hash{$key}[10]; | |
674 | my $destination_ports = $$hash{$key}[15]; | |
675 | ||
676 | # Check if ports are set for protocols which do not support ports. | |
677 | if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) { | |
678 | print_error("$protocol does not support ports"); | |
679 | return (); | |
680 | } | |
681 | ||
682 | push(@protocols, $protocol); | |
2a81ab0d AM |
683 | } |
684 | } | |
8f4f4634 MT |
685 | |
686 | # Remove all empty elements | |
687 | @protocols = map { $_ ? $_ : () } @protocols; | |
688 | ||
689 | # If no protocol has been defined, we assume "all". | |
690 | if (!@protocols) { | |
691 | push(@protocols, "all"); | |
98cee89f | 692 | } |
8f4f4634 MT |
693 | |
694 | # Make all protocol names lowercase. | |
695 | @protocols = map { lc } @protocols; | |
696 | ||
697 | return @protocols; | |
2a81ab0d | 698 | } |
97ab0569 | 699 | |
8f4f4634 MT |
700 | sub get_protocol_options { |
701 | my $hash = shift; | |
702 | my $key = shift; | |
703 | my $protocol = shift; | |
704 | my @options = (); | |
705 | ||
706 | # Process source ports. | |
707 | my $use_src_ports = ($$hash{$key}[7] eq "ON"); | |
708 | my $src_ports = $$hash{$key}[10]; | |
709 | ||
710 | if ($use_src_ports && $src_ports) { | |
711 | push(@options, &format_ports($src_ports, "src")); | |
712 | } | |
713 | ||
714 | # Process destination ports. | |
715 | my $use_dst_ports = ($$hash{$key}[11] eq "ON"); | |
716 | my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat")); | |
717 | ||
718 | if ($use_dst_ports) { | |
719 | my $dst_ports_mode = $$hash{$key}[14]; | |
720 | my $dst_ports = $$hash{$key}[15]; | |
8f4f4634 MT |
721 | |
722 | if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) { | |
1c3044d7 MT |
723 | if ($use_dnat && $$hash{$key}[30]) { |
724 | $dst_ports = $$hash{$key}[30]; | |
725 | } | |
8f4f4634 MT |
726 | push(@options, &format_ports($dst_ports, "dst")); |
727 | ||
728 | } elsif ($dst_ports_mode eq "cust_srv") { | |
729 | if ($protocol eq "ICMP") { | |
730 | push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP"))); | |
731 | } else { | |
732 | $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol)); | |
733 | push(@options, &format_ports($dst_ports, "dst")); | |
2a81ab0d | 734 | } |
8f4f4634 MT |
735 | |
736 | } elsif ($dst_ports_mode eq "cust_srvgrp") { | |
737 | push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol))); | |
2a81ab0d AM |
738 | } |
739 | } | |
8f4f4634 MT |
740 | |
741 | # Check if a single ICMP type is selected. | |
742 | if (!$use_src_ports && !$use_dst_ports && $protocol eq "icmp") { | |
743 | my $icmp_type = $$hash{$key}[9]; | |
744 | ||
745 | if (($icmp_type ne "All ICMP-Types") && $icmp_type) { | |
746 | push(@options, ("--icmp-type", $icmp_type)); | |
a4c7bf6b AM |
747 | } |
748 | } | |
8f4f4634 MT |
749 | |
750 | return @options; | |
751 | } | |
752 | ||
753 | sub format_ports { | |
754 | my $ports = shift; | |
755 | my $type = shift; | |
756 | ||
757 | my $arg; | |
758 | if ($type eq "src") { | |
759 | $arg = "--sport"; | |
760 | } elsif ($type eq "dst") { | |
761 | $arg = "--dport"; | |
762 | } | |
763 | ||
764 | my @options = (); | |
765 | ||
766 | if ($ports =~ /\|/) { | |
767 | $ports =~ s/\|/,/g; | |
768 | push(@options, ("-m", "multiport")); | |
769 | } | |
770 | ||
1c3044d7 MT |
771 | if ($ports) { |
772 | push(@options, ($arg, $ports)); | |
773 | } | |
8f4f4634 MT |
774 | |
775 | return @options; | |
776 | } | |
777 | ||
778 | sub get_dnat_target_port { | |
779 | my $hash = shift; | |
780 | my $key = shift; | |
781 | ||
782 | if ($$hash{$key}[14] eq "TGT_PORT") { | |
1c3044d7 MT |
783 | my $port = $$hash{$key}[15]; |
784 | my $external_port = $$hash{$key}[30]; | |
785 | ||
786 | if ($external_port && ($port ne $external_port)) { | |
787 | return $$hash{$key}[15]; | |
788 | } | |
8f4f4634 | 789 | } |
2a81ab0d | 790 | } |
6e87f0aa MT |
791 | |
792 | sub add_dnat_mangle_rules { | |
793 | my $nat_address = shift; | |
794 | my @options = @_; | |
795 | ||
796 | my $mark = 0; | |
797 | foreach my $zone ("GREEN", "BLUE", "ORANGE") { | |
798 | $mark++; | |
799 | ||
800 | # Skip rule if not all required information exists. | |
801 | next unless (exists $defaultNetworks{$zone . "_NETADDRESS"}); | |
802 | next unless (exists $defaultNetworks{$zone . "_NETMASK"}); | |
803 | ||
804 | my @mangle_options = @options; | |
805 | ||
806 | my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"}; | |
807 | $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"}; | |
808 | ||
809 | push(@mangle_options, ("-s", $netaddress, "-d", $nat_address)); | |
810 | push(@mangle_options, ("-j", "MARK", "--set-mark", $mark)); | |
811 | ||
812 | run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options"); | |
813 | } | |
814 | } | |
3bb4bb3f MT |
815 | |
816 | sub make_log_limit_options { | |
817 | my @options = ("-m", "limit"); | |
818 | ||
819 | # Maybe we should get this from the configuration. | |
820 | my $limit = 10; | |
821 | ||
822 | # We limit log messages to $limit messages per minute. | |
823 | push(@options, ("--limit", "$limit/min")); | |
824 | ||
825 | # And we allow bursts of 2x $limit. | |
826 | push(@options, ("--limit-burst", $limit * 2)); | |
827 | ||
828 | return @options; | |
829 | } | |
e9b5ba41 | 830 | |
5cf8c8c1 | 831 | sub get_internal_firewall_ip_addresses { |
da7a2208 MT |
832 | my $use_orange = shift; |
833 | ||
834 | my @zones = ("GREEN", "BLUE"); | |
835 | if ($use_orange) { | |
836 | push(@zones, "ORANGE"); | |
837 | } | |
5cf8c8c1 | 838 | |
da7a2208 MT |
839 | my @addresses = (); |
840 | for my $zone (@zones) { | |
5cf8c8c1 MT |
841 | next unless (exists $defaultNetworks{$zone . "_ADDRESS"}); |
842 | ||
843 | my $zone_address = $defaultNetworks{$zone . "_ADDRESS"}; | |
844 | push(@addresses, $zone_address); | |
845 | } | |
846 | ||
847 | return @addresses; | |
848 | } | |
849 | ||
da7a2208 | 850 | sub get_internal_firewall_ip_address { |
e9b5ba41 | 851 | my $subnet = shift; |
5cf8c8c1 | 852 | my $use_orange = shift; |
e9b5ba41 MT |
853 | |
854 | my ($net_address, $net_mask) = split("/", $subnet); | |
b0d9fad3 MT |
855 | if ((!$net_mask) || ($net_mask ~~ ["32", "255.255.255.255"])) { |
856 | return 0; | |
e9b5ba41 MT |
857 | } |
858 | ||
da7a2208 MT |
859 | my @addresses = &get_internal_firewall_ip_addresses($use_orange); |
860 | foreach my $zone_address (@addresses) { | |
861 | if (&General::IpInSubnet($zone_address, $net_address, $net_mask)) { | |
862 | return $zone_address; | |
863 | } | |
5cf8c8c1 | 864 | } |
b0d9fad3 MT |
865 | |
866 | return 0; | |
da7a2208 MT |
867 | } |
868 | ||
869 | sub firewall_is_in_subnet { | |
870 | my $subnet = shift; | |
5cf8c8c1 | 871 | |
e9b5ba41 MT |
872 | # ORANGE is missing here, because nothing may ever access |
873 | # the firewall from this network. | |
da7a2208 | 874 | my $address = &get_internal_firewall_ip_address($subnet, 0); |
e9b5ba41 | 875 | |
da7a2208 MT |
876 | if ($address) { |
877 | return 1; | |
e9b5ba41 | 878 | } |
da7a2208 MT |
879 | |
880 | return 0; | |
e9b5ba41 | 881 | } |
b0d9fad3 MT |
882 | |
883 | sub get_matching_firewall_address { | |
884 | my $addr = shift; | |
885 | my $use_orange = shift; | |
886 | ||
887 | my ($address, $netmask) = split("/", $addr); | |
888 | ||
889 | my @zones = ("GREEN", "BLUE"); | |
890 | if ($use_orange) { | |
891 | push(@zones, "ORANGE"); | |
892 | } | |
893 | ||
894 | foreach my $zone (@zones) { | |
895 | next unless (exists $defaultNetworks{$zone . "_ADDRESS"}); | |
896 | ||
897 | my $zone_subnet = $defaultNetworks{$zone . "_NETADDRESS"}; | |
898 | my $zone_mask = $defaultNetworks{$zone . "_NETMASK"}; | |
899 | ||
900 | if (&General::IpInSubnet($address, $zone_subnet, $zone_mask)) { | |
901 | return $defaultNetworks{$zone . "_ADDRESS"}; | |
902 | } | |
903 | } | |
904 | ||
905 | return 0; | |
906 | } |