]>
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 | 49 | my %fwdfwsettings=(); |
aa5f4b65 | 50 | my %fwoptions = (); |
2a81ab0d | 51 | my %defaultNetworks=(); |
8f4f4634 | 52 | my %configfwdfw=();; |
2a81ab0d | 53 | my %customgrp=(); |
2a81ab0d | 54 | my %configinputfw=(); |
5d7faa45 | 55 | my %configoutgoingfw=(); |
a6edca5a | 56 | my %confignatfw=(); |
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); |
aa5f4b65 | 67 | &General::readhash("${General::swroot}/optionsfw/settings", \%fwoptions); |
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 | 72 | &General::readhasharray($configgrp, \%customgrp); |
2a81ab0d | 73 | |
3bb4bb3f MT |
74 | my @log_limit_options = &make_log_limit_options(); |
75 | ||
aa5f4b65 MT |
76 | my $POLICY_INPUT_ALLOWED = 0; |
77 | my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2"); | |
78 | my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2"); | |
79 | ||
80 | my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"}; | |
81 | my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"}; | |
82 | my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"}; | |
83 | ||
8531b94a MT |
84 | # MAIN |
85 | &main(); | |
86 | ||
87 | sub main { | |
88 | # Flush all chains. | |
89 | &flush(); | |
90 | ||
91 | # Reload firewall rules. | |
92 | &preparerules(); | |
93 | ||
94 | # Load P2P block rules. | |
95 | &p2pblock(); | |
96 | ||
97 | # Reload firewall policy. | |
98 | run("/usr/sbin/firewall-policy"); | |
2a81ab0d | 99 | } |
97ab0569 | 100 | |
68d1eb10 MT |
101 | sub run { |
102 | # Executes or prints the given shell command. | |
103 | my $command = shift; | |
104 | ||
105 | if ($DEBUG) { | |
106 | print "$command\n"; | |
107 | } else { | |
108 | system "$command"; | |
6e87f0aa MT |
109 | |
110 | if ($?) { | |
111 | print_error("ERROR: $command"); | |
112 | } | |
68d1eb10 MT |
113 | } |
114 | } | |
115 | ||
6178953b MT |
116 | sub print_error { |
117 | my $message = shift; | |
118 | ||
119 | print STDERR "$message\n"; | |
120 | } | |
121 | ||
8f4f4634 MT |
122 | sub print_rule { |
123 | my $hash = shift; | |
124 | ||
125 | print "\nRULE:"; | |
126 | ||
127 | my $i = 0; | |
128 | foreach (@$hash) { | |
129 | printf(" %2d: %s", $i++, $_); | |
130 | } | |
131 | print "\n"; | |
132 | } | |
133 | ||
97ab0569 | 134 | sub flush { |
d98aa95a MT |
135 | run("$IPTABLES -F $CHAIN_INPUT"); |
136 | run("$IPTABLES -F $CHAIN_FORWARD"); | |
137 | run("$IPTABLES -F $CHAIN_OUTPUT"); | |
138 | run("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE"); | |
139 | run("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION"); | |
6e87f0aa | 140 | run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX"); |
86a921ee | 141 | } |
97ab0569 MT |
142 | |
143 | sub preparerules { | |
6d8eb5de | 144 | if (! -z "${General::swroot}/firewall/input"){ |
2a81ab0d AM |
145 | &buildrules(\%configinputfw); |
146 | } | |
6d8eb5de | 147 | if (! -z "${General::swroot}/firewall/outgoing"){ |
5d7faa45 AM |
148 | &buildrules(\%configoutgoingfw); |
149 | } | |
aa5f4b65 MT |
150 | if (! -z "${General::swroot}/firewall/config"){ |
151 | &buildrules(\%configfwdfw); | |
152 | } | |
2a81ab0d | 153 | } |
97ab0569 MT |
154 | |
155 | sub buildrules { | |
8f4f4634 MT |
156 | my $hash = shift; |
157 | ||
aa5f4b65 MT |
158 | # Search for targets that need to be specially handled when adding |
159 | # forwarding rules. Additional rules will automatically get inserted | |
160 | # into the INPUT/OUTPUT chains for these targets. | |
161 | my @special_input_targets = (); | |
162 | if (!$POLICY_FORWARD_ALLOWED) { | |
163 | push(@special_input_targets, "ACCEPT"); | |
164 | } | |
165 | ||
166 | if ($POLICY_INPUT_ACTION eq "DROP") { | |
167 | push(@special_input_targets, "REJECT"); | |
168 | } elsif ($POLICY_INPUT_ACTION eq "REJECT") { | |
169 | push(@special_input_targets, "DROP"); | |
170 | } | |
171 | ||
172 | my @special_output_targets = (); | |
173 | if ($POLICY_OUTPUT_ALLOWED) { | |
174 | push(@special_output_targets, ("DROP", "REJECT")); | |
175 | } else { | |
176 | push(@special_output_targets, "ACCEPT"); | |
177 | ||
178 | if ($POLICY_OUTPUT_ACTION eq "DROP") { | |
179 | push(@special_output_targets, "REJECT"); | |
180 | } elsif ($POLICY_OUTPUT_ACTION eq "REJECT") { | |
181 | push(@special_output_targets, "DROP"); | |
182 | } | |
183 | } | |
184 | ||
8f4f4634 MT |
185 | foreach my $key (sort {$a <=> $b} keys %$hash) { |
186 | # Skip disabled rules. | |
187 | next unless ($$hash{$key}[2] eq 'ON'); | |
188 | ||
189 | if ($DEBUG) { | |
190 | print_rule($$hash{$key}); | |
191 | } | |
192 | ||
193 | # Check if the target is valid. | |
194 | my $target = $$hash{$key}[0]; | |
195 | if (!$target ~~ @VALID_TARGETS) { | |
196 | print_error("Invalid target '$target' for rule $key"); | |
197 | next; | |
198 | } | |
199 | ||
200 | # Check if the chain is valid. | |
201 | my $chain = $$hash{$key}[1]; | |
202 | if (!$chain ~~ @VALID_CHAINS) { | |
203 | print_error("Invalid chain '$chain' in rule $key"); | |
204 | next; | |
205 | } | |
206 | ||
207 | # Collect all sources. | |
4e54e3c6 | 208 | my @sources = &fwlib::get_addresses($hash, $key, "src"); |
8f4f4634 MT |
209 | |
210 | # Collect all destinations. | |
4e54e3c6 | 211 | my @destinations = &fwlib::get_addresses($hash, $key, "tgt"); |
6178953b | 212 | |
c0ce9206 MT |
213 | # True if the destination is the firewall itself. |
214 | my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire"); | |
215 | ||
6178953b | 216 | # Check if logging should be enabled. |
8f4f4634 | 217 | my $LOG = ($$hash{$key}[17] eq 'ON'); |
6178953b | 218 | |
8f4f4634 MT |
219 | # Check if NAT is enabled and initialize variables, that we use for that. |
220 | my $NAT = ($$hash{$key}[28] eq 'ON'); | |
6178953b | 221 | my $NAT_MODE; |
8f4f4634 MT |
222 | if ($NAT) { |
223 | $NAT_MODE = uc($$hash{$key}[31]); | |
224 | } | |
6178953b | 225 | |
8f4f4634 MT |
226 | # Set up time constraints. |
227 | my @time_options = (); | |
228 | if ($$hash{$key}[18] eq 'ON') { | |
229 | push(@time_options, ("-m", "time")); | |
6178953b | 230 | |
8f4f4634 MT |
231 | # Select all days of the week this match is active. |
232 | my @weekdays = (); | |
233 | if ($$hash{$key}[19] ne '') { | |
234 | push (@weekdays, "Mon"); | |
235 | } | |
236 | if ($$hash{$key}[20] ne '') { | |
237 | push (@weekdays, "Tue"); | |
238 | } | |
239 | if ($$hash{$key}[21] ne '') { | |
240 | push (@weekdays, "Wed"); | |
241 | } | |
242 | if ($$hash{$key}[22] ne '') { | |
243 | push (@weekdays, "Thu"); | |
244 | } | |
245 | if ($$hash{$key}[23] ne '') { | |
246 | push (@weekdays, "Fri"); | |
247 | } | |
248 | if ($$hash{$key}[24] ne '') { | |
249 | push (@weekdays, "Sat"); | |
250 | } | |
251 | if ($$hash{$key}[25] ne '') { | |
252 | push (@weekdays, "Sun"); | |
253 | } | |
254 | if (@weekdays) { | |
255 | push(@time_options, ("--weekdays", join(",", @weekdays))); | |
256 | } | |
6178953b | 257 | |
8f4f4634 MT |
258 | # Convert start time. |
259 | my $time_start = &format_time($$hash{$key}[26]); | |
260 | if ($time_start) { | |
261 | push(@time_options, ("--timestart", $time_start)); | |
a6edca5a | 262 | } |
6178953b | 263 | |
8f4f4634 MT |
264 | # Convert end time. |
265 | my $time_stop = &format_time($$hash{$key}[27]); | |
266 | if ($time_stop) { | |
267 | push(@time_options, ("--timestop", $time_stop)); | |
268 | } | |
a6edca5a | 269 | } |
6178953b | 270 | |
8f4f4634 MT |
271 | # Check which protocols are used in this rule and so that we can |
272 | # later group rules by protocols. | |
273 | my @protocols = &get_protocols($hash, $key); | |
274 | if (!@protocols) { | |
275 | print_error("Invalid protocol configuration for rule $key"); | |
276 | next; | |
277 | } | |
278 | ||
279 | foreach my $protocol (@protocols) { | |
280 | # Check if the given protocol is supported. | |
281 | if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) { | |
282 | print_error("Protocol $protocol is not supported (rule $key)"); | |
283 | next; | |
2a81ab0d | 284 | } |
8f4f4634 MT |
285 | |
286 | # Prepare protocol options (like ICMP types, ports, etc...). | |
d7a14d01 | 287 | my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0); |
8f4f4634 MT |
288 | |
289 | # Check if this protocol knows ports. | |
290 | my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS); | |
291 | ||
02574191 MT |
292 | foreach my $src (@sources) { |
293 | # Skip invalid source. | |
294 | next unless ($src); | |
8f4f4634 | 295 | |
02574191 MT |
296 | # Sanitize source. |
297 | my $source = $src; | |
298 | if ($source ~~ @ANY_ADDRESSES) { | |
299 | $source = ""; | |
300 | } | |
301 | ||
302 | foreach my $dst (@destinations) { | |
303 | # Skip invalid rules. | |
304 | next if (!$dst || ($dst eq "none")); | |
c2a1af75 MT |
305 | |
306 | # Sanitize destination. | |
02574191 | 307 | my $destination = $dst; |
c2a1af75 MT |
308 | if ($destination ~~ @ANY_ADDRESSES) { |
309 | $destination = ""; | |
310 | } | |
311 | ||
8f4f4634 MT |
312 | # Array with iptables arguments. |
313 | my @options = (); | |
314 | ||
315 | # Append protocol. | |
316 | if ($protocol ne "all") { | |
8f4f4634 | 317 | push(@options, @protocol_options); |
2a81ab0d | 318 | } |
8f4f4634 | 319 | |
6e87f0aa MT |
320 | # Prepare source options. |
321 | my @source_options = (); | |
8f4f4634 | 322 | if ($source =~ /mac/) { |
6e87f0aa | 323 | push(@source_options, $source); |
c2a1af75 | 324 | } elsif ($source) { |
6e87f0aa | 325 | push(@source_options, ("-s", $source)); |
2a81ab0d | 326 | } |
14f7cb87 | 327 | |
6e87f0aa | 328 | # Prepare destination options. |
c2a1af75 MT |
329 | my @destination_options = (); |
330 | if ($destination) { | |
331 | push(@destination_options, ("-d", $destination)); | |
332 | } | |
14f7cb87 | 333 | |
8f4f4634 MT |
334 | # Add time constraint options. |
335 | push(@options, @time_options); | |
14f7cb87 | 336 | |
aa5f4b65 | 337 | my $firewall_is_in_source_subnet = 1; |
e9b5ba41 | 338 | if ($source) { |
da7a2208 | 339 | $firewall_is_in_source_subnet = &firewall_is_in_subnet($source); |
e9b5ba41 MT |
340 | } |
341 | ||
aa5f4b65 MT |
342 | my $firewall_is_in_destination_subnet = 1; |
343 | if ($destination) { | |
344 | $firewall_is_in_destination_subnet = &firewall_is_in_subnet($destination); | |
345 | } | |
346 | ||
8f4f4634 MT |
347 | # Process NAT rules. |
348 | if ($NAT) { | |
4e54e3c6 | 349 | my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source); |
b05ec50a | 350 | |
8f4f4634 MT |
351 | # Skip NAT rules if the NAT address is unknown |
352 | # (i.e. no internet connection has been established, yet). | |
353 | next unless ($nat_address); | |
b05ec50a | 354 | |
8f4f4634 MT |
355 | # Destination NAT |
356 | if ($NAT_MODE eq "DNAT") { | |
6e87f0aa | 357 | # Make port-forwardings useable from the internal networks. |
4e54e3c6 | 358 | my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1); |
5cf8c8c1 MT |
359 | unless ($nat_address ~~ @internal_addresses) { |
360 | &add_dnat_mangle_rules($nat_address, @options); | |
361 | } | |
b05ec50a | 362 | |
d7a14d01 MT |
363 | my @nat_options = (); |
364 | if ($protocol ne "all") { | |
365 | my @nat_protocol_options = &get_protocol_options($hash, $key, $protocol, 1); | |
366 | push(@nat_options, @nat_protocol_options); | |
367 | } | |
6e87f0aa | 368 | push(@nat_options, @source_options); |
8f4f4634 | 369 | push(@nat_options, ("-d", $nat_address)); |
51cf3f8b | 370 | push(@nat_options, @time_options); |
6e87f0aa | 371 | |
c0ce9206 | 372 | my $dnat_port; |
8f4f4634 | 373 | if ($protocol_has_ports) { |
c0ce9206 MT |
374 | $dnat_port = &get_dnat_target_port($hash, $key); |
375 | } | |
376 | ||
377 | my @nat_action_options = (); | |
b05ec50a | 378 | |
c0ce9206 MT |
379 | # Use iptables REDIRECT |
380 | my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports && $dnat_port); | |
381 | if ($use_redirect) { | |
382 | push(@nat_action_options, ("-j", "REDIRECT", "--to-ports", $dnat_port)); | |
383 | ||
384 | # Use iptables DNAT | |
385 | } else { | |
f98bb538 MT |
386 | if ($destination_is_firewall && !$destination) { |
387 | $destination = &fwlib::get_external_address(); | |
388 | } | |
389 | next unless ($destination); | |
390 | ||
c0ce9206 MT |
391 | my ($dnat_address, $dnat_mask) = split("/", $destination); |
392 | @destination_options = ("-d", $dnat_address); | |
393 | ||
394 | if ($protocol_has_ports) { | |
395 | my $dnat_port = &get_dnat_target_port($hash, $key); | |
396 | ||
397 | if ($dnat_port) { | |
398 | $dnat_address .= ":$dnat_port"; | |
399 | } | |
86a921ee | 400 | } |
c0ce9206 MT |
401 | |
402 | push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address)); | |
2a81ab0d | 403 | } |
8f4f4634 MT |
404 | |
405 | if ($LOG) { | |
3bb4bb3f | 406 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '"); |
8f4f4634 | 407 | } |
c0ce9206 | 408 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options"); |
8f4f4634 MT |
409 | |
410 | # Source NAT | |
411 | } elsif ($NAT_MODE eq "SNAT") { | |
6e87f0aa MT |
412 | my @nat_options = @options; |
413 | ||
414 | push(@nat_options, @source_options); | |
415 | push(@nat_options, @destination_options); | |
416 | ||
8f4f4634 | 417 | if ($LOG) { |
3bb4bb3f | 418 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '"); |
8f4f4634 | 419 | } |
6e87f0aa | 420 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address"); |
2a81ab0d AM |
421 | } |
422 | } | |
8f4f4634 | 423 | |
6e87f0aa MT |
424 | push(@options, @source_options); |
425 | push(@options, @destination_options); | |
426 | ||
8f4f4634 MT |
427 | # Insert firewall rule. |
428 | if ($LOG && !$NAT) { | |
0bda23f5 | 429 | run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '"); |
8f4f4634 MT |
430 | } |
431 | run("$IPTABLES -A $chain @options -j $target"); | |
aa5f4b65 MT |
432 | |
433 | # Handle forwarding rules and add corresponding rules for firewall access. | |
434 | if ($chain eq $CHAIN_FORWARD) { | |
435 | # If the firewall is part of the destination subnet and access to the destination network | |
436 | # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access | |
437 | # for the firewall, too. | |
438 | if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) { | |
439 | if ($LOG && !$NAT) { | |
440 | run("$IPTABLES -A $CHAIN_INPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '"); | |
441 | } | |
442 | run("$IPTABLES -A $CHAIN_INPUT @options -j $target"); | |
443 | } | |
444 | ||
445 | # Likewise. | |
446 | if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) { | |
447 | if ($LOG && !$NAT) { | |
448 | run("$IPTABLES -A $CHAIN_OUTPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '"); | |
449 | } | |
450 | run("$IPTABLES -A $CHAIN_OUTPUT @options -j $target"); | |
451 | } | |
452 | } | |
2a81ab0d AM |
453 | } |
454 | } | |
455 | } | |
2a81ab0d AM |
456 | } |
457 | } | |
97ab0569 | 458 | |
b05ec50a MT |
459 | # Formats the given timestamp into the iptables format which is "hh:mm" UTC. |
460 | sub format_time { | |
461 | my $val = shift; | |
462 | ||
463 | # Convert the given time into minutes. | |
464 | my $minutes = &time_convert_to_minutes($val); | |
465 | ||
466 | # Move the timestamp into UTC. | |
467 | $minutes += &time_utc_offset(); | |
468 | ||
469 | # Make sure $minutes is between 00:00 and 23:59. | |
470 | if ($minutes < 0) { | |
471 | $minutes += 1440; | |
472 | } | |
473 | ||
474 | if ($minutes > 1440) { | |
475 | $minutes -= 1440; | |
476 | } | |
477 | ||
478 | # Format as hh:mm. | |
479 | return sprintf("%02d:%02d", $minutes / 60, $minutes % 60); | |
472136c9 | 480 | } |
97ab0569 | 481 | |
b05ec50a MT |
482 | # Calculates the offsets in minutes from the local timezone to UTC. |
483 | sub time_utc_offset { | |
484 | my @localtime = localtime(time); | |
485 | my @gmtime = gmtime(time); | |
486 | ||
487 | return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60); | |
472136c9 | 488 | } |
97ab0569 | 489 | |
b05ec50a MT |
490 | # Takes a timestamp like "14:00" and converts it into minutes since midnight. |
491 | sub time_convert_to_minutes { | |
492 | my ($hrs, $min) = split(":", shift); | |
493 | ||
494 | return ($hrs * 60) + $min; | |
472136c9 | 495 | } |
97ab0569 MT |
496 | |
497 | sub p2pblock { | |
766c2f60 MT |
498 | my $search_action; |
499 | my $target; | |
500 | ||
501 | if ($fwdfwsettings{"POLICY"} eq "MODE1") { | |
502 | $search_action = "on"; | |
503 | $target = "ACCEPT"; | |
504 | } else { | |
505 | $search_action = "off"; | |
506 | $target = "DROP"; | |
507 | } | |
508 | ||
509 | open(FILE, "<$p2pfile") or die "Unable to read $p2pfile"; | |
510 | my @protocols = (); | |
511 | foreach my $p2pentry (<FILE>) { | |
512 | my @p2pline = split(/\;/, $p2pentry); | |
513 | next unless ($p2pline[2] eq $search_action); | |
514 | ||
515 | push(@protocols, "--$p2pline[1]"); | |
36196d0d | 516 | } |
766c2f60 | 517 | close(FILE); |
68d1eb10 | 518 | |
766c2f60 MT |
519 | if (@protocols) { |
520 | run("$IPTABLES -A FORWARDFW -m ipp2p @protocols -j $target"); | |
36196d0d AM |
521 | } |
522 | } | |
97ab0569 | 523 | |
8f4f4634 MT |
524 | sub get_protocols { |
525 | my $hash = shift; | |
526 | my $key = shift; | |
527 | ||
528 | my $uses_source_ports = ($$hash{$key}[7] eq "ON"); | |
529 | my $uses_services = ($$hash{$key}[11] eq "ON"); | |
530 | ||
531 | my @protocols = (); | |
532 | ||
533 | # Rules which don't have source ports or services (like ICMP, ESP, ...). | |
534 | if (!$uses_source_ports && !$uses_services) { | |
535 | push(@protocols, $$hash{$key}[8]); | |
536 | ||
537 | # Rules which either use ports or services. | |
538 | } elsif ($uses_source_ports || $uses_services) { | |
539 | # Check if service group or service | |
540 | if ($$hash{$key}[14] eq 'cust_srv') { | |
541 | push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15])); | |
542 | ||
543 | } elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
544 | my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]); | |
545 | push(@protocols, split(",", $protos)); | |
546 | ||
547 | } else { | |
548 | # Fetch the protocol for this rule. | |
549 | my $protocol = lc($$hash{$key}[8]); | |
550 | ||
551 | # Fetch source and destination ports for this rule. | |
552 | my $source_ports = $$hash{$key}[10]; | |
553 | my $destination_ports = $$hash{$key}[15]; | |
554 | ||
555 | # Check if ports are set for protocols which do not support ports. | |
556 | if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) { | |
557 | print_error("$protocol does not support ports"); | |
558 | return (); | |
559 | } | |
560 | ||
561 | push(@protocols, $protocol); | |
2a81ab0d AM |
562 | } |
563 | } | |
8f4f4634 MT |
564 | |
565 | # Remove all empty elements | |
566 | @protocols = map { $_ ? $_ : () } @protocols; | |
567 | ||
568 | # If no protocol has been defined, we assume "all". | |
569 | if (!@protocols) { | |
570 | push(@protocols, "all"); | |
98cee89f | 571 | } |
8f4f4634 MT |
572 | |
573 | # Make all protocol names lowercase. | |
574 | @protocols = map { lc } @protocols; | |
575 | ||
576 | return @protocols; | |
2a81ab0d | 577 | } |
97ab0569 | 578 | |
8f4f4634 MT |
579 | sub get_protocol_options { |
580 | my $hash = shift; | |
581 | my $key = shift; | |
582 | my $protocol = shift; | |
d7a14d01 | 583 | my $nat_options_wanted = shift; |
8f4f4634 MT |
584 | my @options = (); |
585 | ||
d7a14d01 MT |
586 | # Nothing to do if no protocol is specified. |
587 | if ($protocol eq "all") { | |
588 | return @options; | |
589 | } else { | |
590 | push(@options, ("-p", $protocol)); | |
591 | } | |
592 | ||
fcc68a42 MT |
593 | if ($protocol ~~ @PROTOCOLS_WITH_PORTS) { |
594 | # Process source ports. | |
595 | my $use_src_ports = ($$hash{$key}[7] eq "ON"); | |
596 | my $src_ports = $$hash{$key}[10]; | |
8f4f4634 | 597 | |
fcc68a42 MT |
598 | if ($use_src_ports && $src_ports) { |
599 | push(@options, &format_ports($src_ports, "src")); | |
600 | } | |
8f4f4634 | 601 | |
fcc68a42 MT |
602 | # Process destination ports. |
603 | my $use_dst_ports = ($$hash{$key}[11] eq "ON"); | |
604 | my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat")); | |
8f4f4634 | 605 | |
fcc68a42 MT |
606 | if ($use_dst_ports) { |
607 | my $dst_ports_mode = $$hash{$key}[14]; | |
608 | my $dst_ports = $$hash{$key}[15]; | |
8f4f4634 | 609 | |
fcc68a42 MT |
610 | if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) { |
611 | if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) { | |
612 | $dst_ports = $$hash{$key}[30]; | |
613 | } | |
8f4f4634 | 614 | push(@options, &format_ports($dst_ports, "dst")); |
8f4f4634 | 615 | |
fcc68a42 MT |
616 | } elsif ($dst_ports_mode eq "cust_srv") { |
617 | if ($protocol eq "ICMP") { | |
618 | push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP"))); | |
619 | } else { | |
620 | $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol)); | |
621 | push(@options, &format_ports($dst_ports, "dst")); | |
622 | } | |
623 | ||
624 | } elsif ($dst_ports_mode eq "cust_srvgrp") { | |
625 | push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol))); | |
626 | } | |
2a81ab0d AM |
627 | } |
628 | } | |
8f4f4634 MT |
629 | |
630 | # Check if a single ICMP type is selected. | |
fcc68a42 | 631 | if ($protocol eq "icmp") { |
8f4f4634 MT |
632 | my $icmp_type = $$hash{$key}[9]; |
633 | ||
634 | if (($icmp_type ne "All ICMP-Types") && $icmp_type) { | |
635 | push(@options, ("--icmp-type", $icmp_type)); | |
a4c7bf6b AM |
636 | } |
637 | } | |
8f4f4634 MT |
638 | |
639 | return @options; | |
640 | } | |
641 | ||
642 | sub format_ports { | |
643 | my $ports = shift; | |
644 | my $type = shift; | |
645 | ||
646 | my $arg; | |
647 | if ($type eq "src") { | |
648 | $arg = "--sport"; | |
649 | } elsif ($type eq "dst") { | |
650 | $arg = "--dport"; | |
651 | } | |
652 | ||
653 | my @options = (); | |
654 | ||
655 | if ($ports =~ /\|/) { | |
656 | $ports =~ s/\|/,/g; | |
657 | push(@options, ("-m", "multiport")); | |
658 | } | |
659 | ||
1c3044d7 MT |
660 | if ($ports) { |
661 | push(@options, ($arg, $ports)); | |
662 | } | |
8f4f4634 MT |
663 | |
664 | return @options; | |
665 | } | |
666 | ||
667 | sub get_dnat_target_port { | |
668 | my $hash = shift; | |
669 | my $key = shift; | |
670 | ||
671 | if ($$hash{$key}[14] eq "TGT_PORT") { | |
1c3044d7 MT |
672 | my $port = $$hash{$key}[15]; |
673 | my $external_port = $$hash{$key}[30]; | |
674 | ||
675 | if ($external_port && ($port ne $external_port)) { | |
676 | return $$hash{$key}[15]; | |
677 | } | |
8f4f4634 | 678 | } |
2a81ab0d | 679 | } |
6e87f0aa MT |
680 | |
681 | sub add_dnat_mangle_rules { | |
682 | my $nat_address = shift; | |
683 | my @options = @_; | |
684 | ||
685 | my $mark = 0; | |
686 | foreach my $zone ("GREEN", "BLUE", "ORANGE") { | |
687 | $mark++; | |
688 | ||
689 | # Skip rule if not all required information exists. | |
690 | next unless (exists $defaultNetworks{$zone . "_NETADDRESS"}); | |
691 | next unless (exists $defaultNetworks{$zone . "_NETMASK"}); | |
692 | ||
693 | my @mangle_options = @options; | |
694 | ||
695 | my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"}; | |
696 | $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"}; | |
697 | ||
698 | push(@mangle_options, ("-s", $netaddress, "-d", $nat_address)); | |
699 | push(@mangle_options, ("-j", "MARK", "--set-mark", $mark)); | |
700 | ||
701 | run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options"); | |
702 | } | |
703 | } | |
3bb4bb3f MT |
704 | |
705 | sub make_log_limit_options { | |
706 | my @options = ("-m", "limit"); | |
707 | ||
708 | # Maybe we should get this from the configuration. | |
709 | my $limit = 10; | |
710 | ||
711 | # We limit log messages to $limit messages per minute. | |
712 | push(@options, ("--limit", "$limit/min")); | |
713 | ||
714 | # And we allow bursts of 2x $limit. | |
715 | push(@options, ("--limit-burst", $limit * 2)); | |
716 | ||
717 | return @options; | |
718 | } | |
e9b5ba41 | 719 | |
da7a2208 MT |
720 | sub firewall_is_in_subnet { |
721 | my $subnet = shift; | |
5cf8c8c1 | 722 | |
e9b5ba41 MT |
723 | # ORANGE is missing here, because nothing may ever access |
724 | # the firewall from this network. | |
4e54e3c6 | 725 | my $address = &fwlib::get_internal_firewall_ip_address($subnet, 0); |
e9b5ba41 | 726 | |
da7a2208 MT |
727 | if ($address) { |
728 | return 1; | |
e9b5ba41 | 729 | } |
da7a2208 MT |
730 | |
731 | return 0; | |
e9b5ba41 | 732 | } |