Introduce SortAddressHash function.
[people/stevee/guardian.git] / modules / Events.pm
CommitLineData
df6ec59f
SS
1package Guardian::Events;
2use strict;
3use warnings;
4
5use Exporter qw(import);
6
7c8f0696 7our @EXPORT_OK = qw(Init CheckAction GenerateIgnoreList Update);
df6ec59f
SS
8
9# Hash which stores all supported commands from the queue.
10my %commands = (
11 'count' => \&Counter,
12 'block' => \&CallBlock,
13 'unblock' => \&CallUnblock,
14 'flush' => \&CallFlush,
7121d8cd 15 'reload' => \&main::Reload,
ab7c10eb 16 'reload-ignore-list' => \&main::ReloadIgnoreList,
648ca493 17 'logrotate' => \&main::Logrotate,
df6ec59f
SS
18);
19
20# Hash to store addresses and their current count.
21my %counthash = ();
22
23# Hash to store all currentyl blocked addresses and a timestamp
24# when the block for this address can be released.
25my %blockhash = ();
26
66e1ad0a 27# Hash to store user-defined IP addresses and/or subnets which should be
0d218038 28# ignored in case any events should be repored for them.
66e1ad0a
SS
29my %ignorehash = ();
30
0d218038
SS
31# Array to store localhost related IP addresses.
32# They are always white-listed to prevent guardian from blocking
33# any local traffic.
34my @localhost_addresses = ("127.0.0.1", "::1");
35
df6ec59f
SS
36# This object will contain the reference to the logger object after calling Init.
37my $logger;
38
39#
40## The "Init" (Block) function.
41#
42## This function is responsible to initialize Block as a class based object.
43## It has to be called once before any blocking event can be processed.
44#
45## The following arguments must be passed, during initialization:
46## "BlockCount" and "BlockTime" which both have to be natural numbers.
47#
48sub Init (%) {
49 my ( $class, %args ) = @_;
50 my $self = \%args;
51
52 # Fail, if some critical arguments are missing.
53 unless ((exists($self->{BlockCount})) && (exists($self->{BlockTime}))) {
54 die "Could not initialize Block: Too less arguments are given.\n";
55 }
56
57 # Use bless to make "$self" to an object of class "$class".
58 bless($self, $class);
59
60 # Assign logger object.
61 $logger = $self->{Logger};
62
63 # Log used firewall engine.
64 $logger->Log("debug", "Using firewall engine: $self->{FirewallEngine}");
65
66 # Try to load the specified firewall engine or die.
67 my $module_name = "Guardian::" . $self->{FirewallEngine};
68 eval "use $module_name; 1" or die "Could not load a module for firewall engine: $self->{FirewallEngine}!";
69
66e1ad0a
SS
70 # Check if an IgnoreFile has been configured.
71 if (exists($self->{IgnoreFile})) {
72 # Call function to handle the ignore mechanism.
73 &GenerateIgnoreList($self->{IgnoreFile});
0d218038
SS
74 } else {
75 # Whitelist local addresses.
76 %ignorehash = &_whitelist_localhost();
66e1ad0a
SS
77 }
78
df6ec59f
SS
79 # Return the class object.
80 return $self;
81}
82
10442560
SS
83#
84## The "Update" Block settings function.
85#
86## This object based function is called to update various class settings.
87#
88sub Update (\%) {
89 my $self = shift;
90
91 # Dereference the given hash-ref and store
92 # the values into a new temporary hash.
93 my %settings = %{ $_[0] };
94
95 # Skip settings update if some essential settings are missing.
96 unless ((exists($settings{BlockCount})) && (exists($settings{BlockTime}))) {
97 $logger->Log("err", "Values for BlockCount or BlockTime are missing, keeping previously configured settings.");
98
99 # Return unmodified class object.
100 return $self;
101 }
102
103 # Change settings.
104 $self->{BlockCount} = $settings{BlockCount};
105 $self->{BlockTime} = $settings{BlockTime};
106
107 # Return modified class object.
108 return $self;
109}
110
df6ec59f
SS
111#
112## The main "CheckAction" function.
113#
114## This function is used to handle each recived event from the main event queue of guardian.
115#
116## It will check if the given command is valid and will pass it to the responsible function.
117#
118sub CheckAction ($$) {
119 my $self = shift;
120 my @event = split(/ /, $_[0], 4);
121 my ($command, $address, $module, $message) = @event;
122
123 # Check if we got an invalid command.
124 unless(exists($commands{$command})) {
125 $logger->Log("err", "The CheckAction function has been called with an unsupported command ($command)!");
126 return;
127 }
128
e1ff25ce
SS
129 # Check if the given event contains an address.
130 if ($address) {
131 # Convert and validate the given address.
132 my $bin_address = &Guardian::Base::IPOrNet2Int($address);
133
134 # Abort if the given address could not be converted because it is not valid.
135 unless ($bin_address) {
136 $logger->Log("err", "Invalid IP address: $address");
137 return;
138 }
139
140 # Check if address should be ignored.
141 if(&_IsOnIgnoreList($bin_address)) {
142 # Log message.
143 $logger->Log("info", "Ignoring event for $address, because it is part of the ignore list.");
144 return;
145 }
66e1ad0a
SS
146 }
147
df6ec59f
SS
148 # Call required handler.
149 my $error = $commands{$command}->($self, $address, $module, $message);
150
151 # If we got any return code, something went wrong and should be logged.
152 if ($error) {
153 $logger->Log("err", "Got error: $error");
154 return;
155 }
156}
157
158#
159## The main "Counter" function.
160#
161## This function is used to handle each count message + address, which has been sent by the main event
162## loop of guardian.
163#
164## It stores the address and the current count into the counthash and increase the count each time when
165## the same address should be counted again. When the current count reaches the configured BlockCount,
166## the responsible function will be called to block the address.
167#
168sub Counter ($@) {
169 my $self = shift;
170 my ($address, $module, $message) = @_;
171
172 # Log event.
173 $logger->Log("debug", "$module reported $message for address: $address");
174
175 # Increase existing count or start counting for new source addresses.
176 if (exists($counthash{$address})) {
177 # Skip already blocked addresses.
178 if (exists($blockhash{$address})) {
179 return undef;
180 }
181
182 # Increase count of the existing entry.
183 $counthash{$address} = $counthash{$address} + 1;
184
185 # Log altered count of the address.
186 $logger->Log("debug", "Source $address now has count $counthash{$address}/$self->{BlockCount}...");
187 } else {
188 # Log that counting for the address has been started.
189 $logger->Log("debug", "Start counting for $address...");
190
191 # Set count to "1".
192 $counthash{$address} = 1;
193 }
194
195 # Check if the address has reached the configured count limit.
196 if ($counthash{$address} >= $self->{BlockCount}) {
197 # Write out log message.
198 $logger->Log("info", "Blocking $address for $self->{BlockTime} seconds...");
199
200 # Call block subroutine to block the address.
201 my $error = &CallBlock($self, $address, $module, $message);
202
203 # Return the message if an error occurs.
204 return $error;
205 }
206
207 # Everything worked well, return nothing.
208 return undef;
209}
210
211#
212## The RemoveBlocks function.
213#
214## This function periodly will be called and is responsible for releasing the block if the Blocktime
215## on an address has expired.
216#
217## To do this, the code will loop through all entries of the blockhash and check
218## if the estimiated BlockTime of each address has reached and so the block can be released.
219#
220sub RemoveBlocks () {
221 my $self = shift;
222
223 # Get the current time.
224 my $time = time();
225
226 # Loop through the blockhash.
227 foreach my $address (keys %blockhash) {
228 # Check if the blocktime for the address has expired.
229 if ($blockhash{$address} <= $time) {
230 # Call unblock subroutine.
231 my $error = &CallUnblock($self, $address, "BlockTime", "has expired for $address");
232
233 # Log error messages if returned.
234 if ($error) {
235 $logger->Log("err", "$error");
236 }
237 }
238 }
239
240 # Everything okay, return nothing.
241 return undef;
242}
243
244#
245## The CallBlock function.
246#
247## This function is called, if the BlockCount for an address is reached or a direct
248## request for blocking an address has been recieved.
249#
250sub CallBlock ($@) {
251 my $self = shift;
252 my ($address, $module, $message) = @_;
253
254 # Log the call for blocking an address.
255 $logger->Log("info", "$module - $message");
256
257 # Check if an entry for this address exists
258 # in the blockhash. If not, the address has
259 # not been blocked yet, call the responisible
260 # function to do this now.
261 unless (exists($blockhash{$address})) {
e9c558fe
SS
262 # Obtain the configured FirewallAction.
263 my $action = $self->{FirewallAction};
df6ec59f
SS
264
265 # Block the given address.
266 my $error = &DoBlock($address, $action);
267
268 # If we got back an error message something went wrong.
269 if ($error) {
f842727a
SS
270 # Exit function and return the used FirewallEngine and the error message.
271 return "$self->{FirewallEngine} - $error";
df6ec59f
SS
272 } else {
273 # Address has been successfully blocked, print a log message.
274 $logger->Log("debug", "Address $address successfully has been blocked...");
275 }
276 }
277
278 # Generate time when the block will expire.
279 my $expire = time() + $self->{BlockTime};
280
281 # Store the blocked address and the expire time
282 # in the blockhash.
283 $blockhash{$address} = $expire;
284
285 # Return nothing "undef" if everything is okay.
286 return undef;
287}
288
289#
290## CallUnblock function.
291#
292## This function is responsible for unblocking and deleting a given
293## address from the blockhash.
294#
295sub CallUnblock ($) {
296 my $self = shift;
297 my ($address, $module, $message) = @_;
298
299 # Log the call for unblocking an address.
300 $logger->Log("info", "$module - $message");
301
302 # Return an error if no entry for the given address
303 # is present in the blockhash.
304 unless (exists($blockhash{$address})) {
305 return "Address $address was not blocked!";
306 }
307
308 # Unblock the address.
309 my $error = &DoUnblock($address);
310
311 # If an error message is returned, something went wrong.
312 if ($error) {
313 # Exit function and return the error message.
314 return $error;
315 } else {
316 # Address successfully has been unblocked.
317 $logger->Log("debug", "Address $address successfully has been unblocked...");
318 }
319
320 # Drop address from blockhash.
321 delete ($blockhash{$address});
322
e2377f63
SS
323 # Drop address from counthash if the address has been unblocked
324 # by the user. This happens when the called module is "Socket".
325 if ($module eq "Socket") {
326 delete ($counthash{$address});
327 }
328
df6ec59f
SS
329 # Everything worked well, return nothing.
330 return undef;
331}
332
7c96d150
SS
333#
334## CallFlush function.
335#
336## This function is responsible for calling the used firewall
337## engine to do a flush of the used firewall chain. This will
338## clean the entire firewall chain.
339#
340sub CallFlush ($) {
341 my $self = shift;
342
343 # Log the call for flushing.
344 $logger->Log("info", "Flush has been called...");
345
346 # Call flush.
347 my $error = &DoFlush();
348
349 # If an error message is returned, something went wrong.
350 if ($error) {
351 # Exit function and return the error message.
352 return $error;
353 } else {
354 # Flush successfully has been performed.
355 $logger->Log("debug", "Flush successfully has been performed...");
356 }
357
358 # Flush blockhash.
359 %blockhash = ();
360
361 # Everything worked well, return nothing.
362 return undef;
363}
364
66e1ad0a
SS
365#
366## GenerateIgnoreList function.
367#
368## This function is responsible for generating/updating the
369## IgnoreHash which contains all ignored IP addresses and
370## networks.
371#
372sub GenerateIgnoreList($) {
373 my $file = shift;
900cad7f
SS
374 my @include_files;
375
376 # Reset current ignore hash and add
377 # localhost related IP addresses.
378 %ignorehash = &_whitelist_localhost();
66e1ad0a
SS
379
380 # Check if the given IgnoreFile could be opened.
381 unless(-e $file) {
382 $logger->Log("err", "The configured IgnoreFile \($file\) could not be opened. Skipped!");
383 return;
384 }
385
900cad7f 386 # Open the given IgnoreFile.
66e1ad0a
SS
387 open (IGNORE, $file);
388
389 # Read-in the file line by line.
390 while (<IGNORE>) {
391 # Skip comments.
392 next if (/\#/);
393
394 # Skip blank lines.
395 next if (/^\s*$/);
396
397 # Remove any newlines.
398 chomp;
399
900cad7f
SS
400 # Check for an include instruction.
401 if ($_ =~ /^Include_File = (.*)/) {
402 my $include_file = $1;
66e1ad0a 403
900cad7f
SS
404 # Check if the parsed include file exists and is read-able.
405 if (-e $include_file) {
406 # Add file to the array of files wich will be included.
407 push(@include_files, $include_file);
408
409 # Write out log message.
410 $logger->Log("debug", "Addresses from $include_file will be included...");
411 } else {
412 # Log missing file.
413 $logger->Log("err", "$include_file will not be included. File does not exist!");
414 }
415 } else {
416 # Check if the line contains a valid single address or network and
417 # convert it into binary format. Store the result/start and
418 # end values in a temporary array.
419 my @values = &Guardian::Base::IPOrNet2Int($_);
420
421 # If the function returned any values, the line contained a valid
422 # single address or network which successfully has been converted into
423 # binary format.
424 if (@values) {
425 # Assign the array as value to the ignorehash.
426 $ignorehash{$_} = [@values];
427 } else {
428 # Log invalid entry.
429 $logger->Log("err", "IgnoreFile contains an invalid address/network: $_");
430
431 # Skip line.
432 next;
433 }
66e1ad0a
SS
434 }
435 }
436
437 # Close filehandle for the IgnoreFile.
438 close (IGNORE);
5cd378cd 439
900cad7f
SS
440 # Check if any files should be included.
441 if (@include_files) {
442 # Loop through the array of files which should be included.
443 foreach my $file (@include_files) {
444 # Open the file.
445 open(INCLUDE, $file);
446
447 # Read-in file line by line.
448 while(<INCLUDE>) {
449 # Skip any comments.
450 next if (/\#/);
451
452 # Skip any blank lines.
453 next if (/^\s*$/);
454
455 # Chomp any newlines.
456 chomp;
457
458 # Check if the line contains a valid single address or network and
459 # convert it into binary format. Store the result/start and
460 # end values in a temporary array.
461 my @values = &Guardian::Base::IPOrNet2Int($_);
462
463 # If the function returned any values, the line contained a valid
464 # single address or network which successfully has been converted into
465 # binary format.
466 if (@values) {
467 # Assign the array as value to the ignorehash.
468 $ignorehash{$_} = [@values];
469 } else {
470 # Log invalid entry.
471 $logger->Log("err", "$file contains an invalid address/network: $_");
472
473 # Skip line.
474 next;
475 }
476 }
477
478 # Close filehandle.
479 close(INCLUDE);
480 }
481 }
482
5cd378cd
SS
483 # Get amount of current elements in hash.
484 my $amount = scalar(keys(%ignorehash));
485
486 # Write out log message.
487 $logger->Log("debug", "Ignore list currently contains $amount entries.");
66e1ad0a
SS
488}
489
490#
491## Private function to check if an address is part of the Ignore Hash.
492#
7c8f0696
SS
493## This function checks if a passed IP address in binary format (!),
494## directly or as part of an ignored network is stored in the ignore hash.
66e1ad0a
SS
495#
496sub _IsOnIgnoreList ($) {
497 my $bin_address = shift;
498
499 # Loop through the ignore hash and grab the stored values.
500 foreach my $key ( keys %ignorehash ) {
501 # Dereference values array.
502 my @values = @{$ignorehash{$key}};
503
504 # Obtain amount of items for the current value array.
505 my $items = scalar @values;
506
507 # Whether the amount equals one, the given binary address just
508 # needs to be compared against a single address.
509 if ($items eq "1") {
510 my ($ignored_address) = @values;
511
512 # Simple check if the stored and the given binary address
513 # are the same.
514 if ($bin_address eq $ignored_address) {
515 # The given address is part of the ignore list.
516 $logger->Log("debug", "Address $key found on the ignore list.");
517
518 # Return "1" (True).
519 return 1;
520 }
521 }
522
523 # If the amount equals two, for passed binary address needs to
524 # be checked if it is part of the ignored network range.
525 elsif ($items eq "2") {
526 my ($first_address, $last_address) = @values;
527
528 # Check if the passed binary address is bigger than
529 # the first address and smaler than the last address
530 # (between) the stored network range.
531 if (($bin_address >= $first_address) && ($bin_address <= $last_address)) {
532 # The address is part of an ignored network.
533 $logger->Log("debug", "Address is found inside the ignored network $key.");
534
535 # Return "1" (True).
536 return 1;
537 }
538
539 # If the amount is not eighter one or two, the current entry of the ignorehash seems
540 # to be corrupted. Skip and log it.
541 } else {
542 # Write log message about this corruped item in the ignore hash.
543 $logger->Log("err", "Invalid item in the Ignore Hash: $key - @values");
544
545 # Skip this element of the ignore hash.
546 next;
547 }
548 }
549
550 # If we got here, the given address is not part of the ignore hash.
551 # Return nothing (False).
552 return;
553}
554
0d218038
SS
555#
556## The _whitelist_localhost function.
557#
558## This tiny private function simple generates and returns a hash which contains
559## the clear and binary converted addresses for all array-stored
560## (@localhost_addresses) in an ignorelist compatible format.
561#
562sub _whitelist_localhost () {
563 my %temphash;
564
565 # Loop through the array of localhost related addresses.
566 foreach my $address (@localhost_addresses) {
567 # Validate and convert the addresss into binary format.
568 my @values = &Guardian::Base::IPOrNet2Int($address);
569
570 # Check if any values are returned.
571 if (@values) {
572 # Store the converted binary values in the temporary hash.
573 $temphash{$address} = [@values];
574 }
575 }
576
577 # Return the temporary hash.
578 return %temphash;
579}
580
df6ec59f 5811;