]> git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Base.pm
Validate IP addresses before passing to the firewall module.
[people/stevee/guardian.git] / modules / Base.pm
1 package Guardian::Base;
2 use strict;
3 use warnings;
4
5 use Exporter qw(import);
6
7 our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
8
9 use Net::IP;
10
11 #
12 ## Function to generate a hash of monitored files and their file positions.
13 #
14 ## This function is responsible for creating the hash of which files should be
15 ## monitored by guardian. In order to do this, all options from the given hash of
16 ## main settings will be parsed and all files to monitor and their configured parsers
17 ## get extracted, validated and stored into a temporary hash.
18 #
19 ## Next step will be to cleanup files which have been monitored in the past but have been
20 ## requested for beeing unmonitored for now. To do this, a check if the the file name is
21 ## part of the existing hash of monitored files and if true to transfer the data into the
22 ## new temporary hash which get returned by the function.
23 #
24 sub GenerateMonitoredFiles (\%\%) {
25 # Dereference the given hash-refs and store
26 # them into a new temporary hashes.
27 my %mainsettings = %{ $_[0] };
28 my %current_monitored_files = %{ $_[1] };
29
30 # Private hash for storing the new monitored files.
31 my %new_monitored_files = ();
32
33 # Loop through the temporary hash which contains the main settings.
34 # Search for files which should be monitored and extract the requested
35 # parser. Compare if the file already was a part of the hash which contains
36 # the monitored files and add them to the private new hash of monitored
37 # files which will be returned.
38 foreach my $config_option (keys %mainsettings) {
39 # Skip option if it does not look like "Monitor_XYZ".
40 next unless($config_option =~ m/^Monitor_/);
41
42 # Splitt monitor instruction into 2 parts, to grab the
43 # requested parser module.
44 my ($start, $parser) = split (/_/, $config_option);
45
46 # Convert parser name into lower case format.
47 # Internally the parser module name is completely handled
48 # in this way. This also prevents from any problems related
49 # how the parser name has been spelled in the config file.
50 $parser = lc($parser);
51
52 # Check if the configured parser is available and valid.
53 next unless(&Guardian::Parser::IsSupportedParser($parser));
54
55 # Get the configured file for this option.
56 my $file = $mainsettings{$config_option};
57
58 # Skip the file, if it does not exist or is not read-able.
59 next unless(-r "$file");
60
61 # Check if the file not yet has been added to the hash
62 # of monitored files.
63 unless(exists($current_monitored_files{$file})) {
64 # Add the file, init and store the fileposition.
65 $new_monitored_files{$file} = $parser;
66 } else {
67 # Copy file and parser information to the new hash.
68 $new_monitored_files{$file} = $current_monitored_files{$file};
69 }
70 }
71
72 # Return the new_monitored_files hash.
73 return %new_monitored_files;
74 }
75
76 #
77 ## The FilePositions function.
78 #
79 ## This function is responsible for creating and/or updating the hash which
80 ## stores the current cursor position of the end of file (EOF) of all
81 ## monitored files.
82 #
83 ## The function requires the hash of currently monitored files and the old hash
84 ## of the current file positions in order to work properly.
85 #
86 sub FilePositions (\%\%) {
87 # Dereference the given hash-refs and store
88 # them into a new temporary hashes.
89 my %monitored_files = %{ $_[0] };
90 my %current_file_positions = %{ $_[1] };
91
92 # Private hash for storing the new monitored files.
93 my %new_file_positions = ();
94
95 # Loop through the hash of monitored files.
96 # Compare if the file allready has been a part of the hash
97 # which contains the file positions and transfer the stored
98 # cursor position into the temporary hash which will be returned.
99 #
100 # Otherwise, call the responsible function to obtain the current
101 # end of file (EOF) and store it.
102 foreach my $file (keys %monitored_files) {
103 # Check if the filename is allready part of the hash
104 # of file positions.
105 if (exists($current_file_positions{$file})) {
106 # Copy file position into temporary hash.
107 $new_file_positions{$file} = $current_file_positions{$file};
108 } else {
109 # Call function to obtain the file position.
110 my $position = &_initFileposition($file);
111
112 # Add filename and position to the temporary hash.
113 $new_file_positions{$file} = $position;
114 }
115 }
116
117 # Return the new_file_positions hash.
118 return %new_file_positions;
119 }
120
121 #
122 ## Wrapper function for IP address and network validation.
123 #
124 ## This wrapper function uses the external Net::IP perl module to
125 ## check if a given input is a valid IPv4/IPv6 address or network.
126 #
127 sub IsValidAddressOrNetwork ($) {
128 my $address = shift;
129
130 # Check if the address is a valid IPv4/IPv6 address or network.
131 # Return "undef" False if the address is not valid.
132 my $ip = new Net::IP ($address) || return undef;
133
134 # If we got here, the address is valid. Return True.
135 return 1;
136 }
137
138 #
139 ## Function for fileposition initialization.
140 #
141 ## This function is used to get the cursor position of the end of file (EOF) of
142 ## a specified file.
143 #
144 ## In order to prevent from permanently read and keep files opened, or dealing
145 ## with huge logfiles, at initialization time of the worker processes, the file will
146 ## be opened once and the cursor position of the end of file (EOF) get stored.
147 #
148 sub _initFileposition ($) {
149 my $file = $_[0];
150
151 # Open the file.
152 open(FILE, $file) or die "Could not open $file. $!";
153
154 # Just seek to the end of the file (EOF).
155 seek(FILE, 0, 2);
156
157 # Get and store the position.
158 my $position = tell(FILE),
159
160 # Close the file again.
161 close(FILE);
162
163 # Return the position.
164 return $position;
165 }
166
167 1;