]>
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 ###############################################################################
29 require Guardian
::Config
;
30 require Guardian
::Parser
;
31 require Guardian
::Socket
;
36 # Array to store the monitored logfiles.
37 my @monitored_files = (
38 "/var/log/snort/alert",
41 # Get and store the given command line arguments in a hash.
44 &GetOptions
(\
%cmdargs,
51 # Show help / version information.
52 if (defined($cmdargs{"help"})) {
53 print "Guardian $version \n";
54 print "Usage: guardian <optional arguments>\n";
55 print " -c, --config\t\tspecifiy a configuration file other than the default (/etc/guardian/guardian.conf)\n";
56 print " -f, --foreground\trun in the foreground (doesn't fork, any output goes to STDOUT)\n";
57 print " -h, --help\t\tshows this help\n";
58 print " -v, --version\t\tdisplay programm version and exit.\n";
60 } elsif (defined($cmdargs{"version"})) {
61 print "Guardian $version \n";
65 # Read-in the configuration file and store the settings.
66 # Push the may be given config file argument.
67 my %mainsettings = &Guardian
::Config
::UseConfig
($cmdargs{"config"});
69 # Create the main queue. It is used to store and process all events which are
70 # reported and enqueued by the worker threads.
71 my $queue :shared
= new Thread
::Queue
or die "Could not create new, empty queue. $!\n";;
73 # Call Init function to initzialize guardian.
76 # Infinite main loop, which processes all queued events.
78 # Get the amount of elements in our queue.
79 # "undef" will be returned if it is empty.
80 my $current_events = $queue->pending();
82 # If there is at least one element enqued
83 if($current_events > 0) {
84 # Grab the data of the top enqueued event.
85 my $event = $queue->peek();
87 print "Got event: $event\n";
89 # Drop processed event from queue.
94 # Temporary workaround to reduce the load of the main process.
101 ## This function contains code which has to be executed while guardian
105 # Setup IPC mechanism via Socket in an own thread.
106 threads
->create(\
&Socket
);
108 # Loop through the array of which files should be monitored and
109 # create a worker thread for each single one.
110 foreach my $monitored_file (@monitored_files) {
111 # Check if the file exists and is readable.
112 if (-r
"$monitored_file") {
113 # Create worker thread for the file.
114 threads
->create(\
&Worker
,$monitored_file);
122 ## This function is responsible for monitoring modifications of the given logfile,
123 ## read them and pass them to the message parser.
125 ## To get file modifications the inotify subsystem of the linux kernel is used.
127 ## In order to prevent from permanently read and keep files opened, or dealing
128 ## with huge logfiles, at initialization time of the worker process, the file will
129 ## be opened once and the cursor position of the end of file (EOF) get stored. When
130 ## reading any newly added lines from the file, we directly can jump to the last
131 ## known position and get these lines. Afterwards, we store the current curser position
132 ## again, so we can do it in this way over and over again.
134 ## All read lines get stored in an array, which will be passed to the Parser.
136 ## If any response (action) from the parser get recieved, it will be put into the
137 ## shared event queue.
142 # Get the fileposition.
143 my $fileposition = &Init_fileposition
("$file");
145 # Create inotify watcher.
146 my $watcher = new Linux
::Inotify2
or die "Could not use inotify. $!\n";
148 # Monitor the specified file.
149 $watcher->watch("$file", IN_MODIFY
) or die "Could not monitor $file. $!\n";
151 # Get all notifications.
152 while ($watcher->read) {
156 open (FILE
, $file) or die "Could not open $file. $!\n";
158 # Seek to the last known position.
159 seek (FILE
, $fileposition, 0);
161 # Get the log message.
162 while (my $line = <FILE
>) {
163 # Remove any newlines.
166 # Add all lines to the message array.
167 push (@message, $line);
170 # Update fileposition.
171 $fileposition = tell(FILE
);
176 # Send filename and message to the parser,
177 # which will return if an action has to be performed.
178 my @action = &Guardian
::Parser
::Parser
("$file", @message);
180 # Send the action to the main process and put it into
186 # Put the required action into the queue.
187 $queue->enqueue(@action);
195 ## This function uses the Socket module to create and listen to an UNIX socket.
196 ## It automatically accepts all incomming connections and pass the recieved
197 ## data messages to the the Message_Parser function which is also part of the
200 ## If a valid command has been sent through the socket, the corresponding event
201 ## will be enqueued into the shared event queue.
204 # Create the Server socket by calling the responsible function.
205 my $server = &Guardian
::Socket
::Server
();
207 # Accept incomming connections from the socket.
208 while (my $connection = $server->accept()) {
209 # Autoflush the socket after the data
211 $connection->autoflush(1);
213 # Gather all data from the connection.
214 while (my $message = <$connection>) {
215 # Remove any newlines.
218 # Send the recieved data message to the
220 my $action = &Guardian
::Socket
::Message_Parser
($message);
222 # If the parser returns to perform an action,
223 # add it to the main event queue.
228 # Enqueue the returned action.
229 $queue->enqueue($action);
236 ## Function for fileposition initialization.
238 ## This function is used to get the cursor position of the end of file (EOF) of
241 ## In order to prevent from permanently read and keep files opened, or dealing
242 ## with huge logfiles, at initialization time of the worker processes, the file will
243 ## be opened once and the cursor position of the end of file (EOF) get stored.
245 sub Init_fileposition
($) {
249 open(FILE
, $file) or die "Could not open $file. $!\n";
251 # Just seek to the end of the file (EOF).
254 # Get and store the position.
255 my $position = tell(FILE
),
257 # Close the file again.
260 # Return the position.