]>
git.ipfire.org Git - people/stevee/guardian.git/blob - guardian
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2015 IPFire Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
28 use Time
::HiRes qw
[ time sleep ];
30 require Guardian
::Config
;
31 require Guardian
::Parser
;
32 require Guardian
::Socket
;
39 # Array to store the monitored logfiles.
40 my @monitored_files = (
41 "/var/log/snort/alert",
44 # Get and store the given command line arguments in a hash.
47 &GetOptions
(\
%cmdargs,
54 # Show help / version information.
55 if (defined($cmdargs{"help"})) {
56 print "Guardian $version \n";
57 print "Usage: guardian <optional arguments>\n";
58 print " -c, --config\t\tspecifiy a configuration file other than the default (/etc/guardian/guardian.conf)\n";
59 print " -f, --foreground\trun in the foreground (doesn't fork, any output goes to STDOUT)\n";
60 print " -h, --help\t\tshows this help\n";
61 print " -v, --version\t\tdisplay programm version and exit.\n";
63 } elsif (defined($cmdargs{"version"})) {
64 print "Guardian $version \n";
68 # Read-in the configuration file and store the settings.
69 # Push the may be given config file argument.
70 my %mainsettings = &Guardian
::Config
::UseConfig
($cmdargs{"config"});
72 # Create the main queue. It is used to store and process all events which are
73 # reported and enqueued by the worker threads.
74 my $queue :shared
= new Thread
::Queue
or die "Could not create new, empty queue. $!\n";;
76 # Array to store all currently running worker objects.
77 # (Does not include the socket thread)
80 # Call Init function to initzialize guardian.
83 # Infinite main loop, which processes all queued events.
85 # Get the amount of elements in our queue.
86 # "undef" will be returned if it is empty.
87 my $current_events = $queue->pending();
89 # If there is at least one element enqued
90 if($current_events > 0) {
91 # Grab the data of the top enqueued event.
92 my $event = $queue->peek();
94 print "Got event: $event\n";
96 # Drop processed event from queue.
100 # Sleep 10ms to reduce the load of the main process.
107 ## This function contains code which has to be executed while guardian
111 # Setup signal handler.
114 # Setup IPC mechanism via Socket in an own thread.
115 threads
->create(\
&Socket
);
117 # Start worker threads.
124 ## This function is responsible for monitoring modifications of the given logfile,
125 ## read them and pass them to the message parser.
127 ## To get file modifications the inotify subsystem of the linux kernel is used.
129 ## In order to prevent from permanently read and keep files opened, or dealing
130 ## with huge logfiles, at initialization time of the worker process, the file will
131 ## be opened once and the cursor position of the end of file (EOF) get stored. When
132 ## reading any newly added lines from the file, we directly can jump to the last
133 ## known position and get these lines. Afterwards, we store the current curser position
134 ## again, so we can do it in this way over and over again.
136 ## All read lines get stored in an array, which will be passed to the Parser.
138 ## If any response (action) from the parser get recieved, it will be put into the
139 ## shared event queue.
144 # Signal handler to kill worker.
145 $SIG{'KILL'} = sub { threads
->exit(); };
147 # Get the fileposition.
148 my $fileposition = &Init_fileposition
("$file");
150 # Create inotify watcher.
151 my $watcher = new Linux
::Inotify2
or die "Could not use inotify. $!\n";
153 # Monitor the specified file.
154 $watcher->watch("$file", IN_MODIFY
) or die "Could not monitor $file. $!\n";
156 # Switch watcher into non-blocking mode.
157 $watcher->blocking(0);
161 # Check for any events and perform them, if there
163 if ($watcher->read) {
167 open (FILE
, $file) or die "Could not open $file. $!\n";
169 # Seek to the last known position.
170 seek (FILE
, $fileposition, 0);
172 # Get the log message.
173 while (my $line = <FILE
>) {
174 # Remove any newlines.
177 # Add all lines to the message array.
178 push (@message, $line);
181 # Update fileposition.
182 $fileposition = tell(FILE
);
187 # Send filename and message to the parser,
188 # which will return if an action has to be performed.
189 my @action = &Guardian
::Parser
::Parser
("$file", @message);
191 # Send the action to the main process and put it into
197 # Put the required action into the queue.
198 $queue->enqueue(@action);
201 # Sleep for 10ms until the next round of the loop will start.
210 ## This function uses the Socket module to create and listen to an UNIX socket.
211 ## It automatically accepts all incomming connections and pass the recieved
212 ## data messages to the the Message_Parser function which is also part of the
215 ## If a valid command has been sent through the socket, the corresponding event
216 ## will be enqueued into the shared event queue.
219 # Create the Server socket by calling the responsible function.
220 my $server = &Guardian
::Socket
::Server
();
222 # Accept incomming connections from the socket.
223 while (my $connection = $server->accept()) {
224 # Autoflush the socket after the data
226 $connection->autoflush(1);
228 # Gather all data from the connection.
229 while (my $message = <$connection>) {
230 # Remove any newlines.
233 # Send the recieved data message to the
235 my $action = &Guardian
::Socket
::Message_Parser
($message);
237 # If the parser returns to perform an action,
238 # add it to the main event queue.
243 # Enqueue the returned action.
244 $queue->enqueue($action);
251 ## Function for fileposition initialization.
253 ## This function is used to get the cursor position of the end of file (EOF) of
256 ## In order to prevent from permanently read and keep files opened, or dealing
257 ## with huge logfiles, at initialization time of the worker processes, the file will
258 ## be opened once and the cursor position of the end of file (EOF) get stored.
260 sub Init_fileposition
($) {
264 open(FILE
, $file) or die "Could not open $file. $!\n";
266 # Just seek to the end of the file (EOF).
269 # Get and store the position.
270 my $position = tell(FILE
),
272 # Close the file again.
275 # Return the position.
280 ## Function for capturing process signals.
282 ## This function captures any sent process signals and will call various
283 ## actions, basend on the captured signal.
286 $SIG{INT
} = \
&Shutdown
;
287 $SIG{TERM
} = \
&Shutdown
;
288 $SIG{QUIT
} = \
&Shutdown
;
292 ## Function to start the workers (threads) for all monitored files.
294 ## This function will loop through the array of monitored files and will
295 ## spawn an own thread based worker for each file. Every created worker will
296 ## be added to the array of running workers.
298 sub StartWorkers
() {
299 # Loop through the array of which files should be monitored and
300 # create a worker thread for each single one.
301 foreach my $monitored_file (@monitored_files) {
302 # Check if the file exists and is readable.
303 if (-r
"$monitored_file") {
304 # Create worker thread for the file.
305 push @running_workers, threads
->create(\
&Worker
,$monitored_file);
311 ## Function to stop all running workers.
313 ## This function is used to stop all currently running workers and will be
314 ## called when reloading or shutting down guardian.
317 # Loop through all running workers.
318 foreach my $worker (@running_workers) {
319 # Send the worker the "KILL" signal and detach the
320 # thread so perl can do an automatically clean-up.
321 $worker->kill('KILL')->detach();
326 ## Shutdown function.
328 ## This function is used to do a clean shutdown of guardian. It will be called
329 ## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals.
335 # Remove socket file on exit.
336 &Guardian
::Socket
::RemoveSocketFile
();