]>
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 require Guardian
::Parser
;
29 require Guardian
::Socket
;
31 # Array to store the monitored logfiles.
32 my @monitored_files = (
33 "/var/log/snort/alert",
36 # Create the main queue. It is used to store and process all events which are
37 # reported and enqueued by the worker threads.
38 my $queue :shared
= new Thread
::Queue
or die "Could not create new, empty queue. $!\n";;
40 # Call Init function to initzialize guardian.
43 # Infinite main loop, which processes all queued events.
45 # Get the amount of elements in our queue.
46 # "undef" will be returned if it is empty.
47 my $current_events = $queue->pending();
49 # If there is at least one element enqued
50 if($current_events > 0) {
51 # Grab the data of the top enqueued event.
52 my $event = $queue->peek();
54 print "Got event: $event\n";
56 # Drop processed event from queue.
61 # Temporary workaround to reduce the load of the main process.
68 ## This function contains code which has to be executed while guardian
72 # Setup IPC mechanism via Socket in an own thread.
73 threads
->create(\
&Socket
);
75 # Loop through the array of which files should be monitored and
76 # create a worker thread for each single one.
77 foreach my $monitored_file (@monitored_files) {
78 # Check if the file exists and is readable.
79 if (-r
"$monitored_file") {
80 # Create worker thread for the file.
81 threads
->create(\
&Worker
,$monitored_file);
89 ## This function is responsible for monitoring modifications of the given logfile,
90 ## read them and pass them to the message parser.
92 ## To get file modifications the inotify subsystem of the linux kernel is used.
94 ## In order to prevent from permanently read and keep files opened, or dealing
95 ## with huge logfiles, at initialization time of the worker process, the file will
96 ## be opened once and the cursor position of the end of file (EOF) get stored. When
97 ## reading any newly added lines from the file, we directly can jump to the last
98 ## known position and get these lines. Afterwards, we store the current curser position
99 ## again, so we can do it in this way over and over again.
101 ## All read lines get stored in an array, which will be passed to the Parser.
103 ## If any response (action) from the parser get recieved, it will be put into the
104 ## shared event queue.
109 # Get the fileposition.
110 my $fileposition = &Init_fileposition
("$file");
112 # Create inotify watcher.
113 my $watcher = new Linux
::Inotify2
or die "Could not use inotify. $!\n";
115 # Monitor the specified file.
116 $watcher->watch("$file", IN_MODIFY
) or die "Could not monitor $file. $!\n";
118 # Get all notifications.
119 while ($watcher->read) {
123 open (FILE
, $file) or die "Could not open $file. $!\n";
125 # Seek to the last known position.
126 seek (FILE
, $fileposition, 0);
128 # Get the log message.
129 while (my $line = <FILE
>) {
130 # Remove any newlines.
133 # Add all lines to the message array.
134 push (@message, $line);
137 # Update fileposition.
138 $fileposition = tell(FILE
);
143 # Send filename and message to the parser,
144 # which will return if an action has to be performed.
145 my @action = &Guardian
::Parser
::Parser
("$file", @message);
147 # Send the action to the main process and put it into
153 # Put the required action into the queue.
154 $queue->enqueue(@action);
162 ## This function uses the Socket module to create and listen to an UNIX socket.
163 ## It automatically accepts all incomming connections and pass the recieved
164 ## data messages to the the Message_Parser function which is also part of the
167 ## If a valid command has been sent through the socket, the corresponding event
168 ## will be enqueued into the shared event queue.
171 # Create the Server socket by calling the responsible function.
172 my $server = &Guardian
::Socket
::Server
();
174 # Accept incomming connections from the socket.
175 while (my $connection = $server->accept()) {
176 # Autoflush the socket after the data
178 $connection->autoflush(1);
180 # Gather all data from the connection.
181 while (my $message = <$connection>) {
182 # Remove any newlines.
185 # Send the recieved data message to the
187 my $action = &Guardian
::Socket
::Message_Parser
($message);
189 # If the parser returns to perform an action,
190 # add it to the main event queue.
195 # Enqueue the returned action.
196 $queue->enqueue($action);
203 ## Function for fileposition initialization.
205 ## This function is used to get the cursor position of the end of file (EOF) of
208 ## In order to prevent from permanently read and keep files opened, or dealing
209 ## with huge logfiles, at initialization time of the worker processes, the file will
210 ## be opened once and the cursor position of the end of file (EOF) get stored.
212 sub Init_fileposition
($) {
216 open(FILE
, $file) or die "Could not open $file. $!\n";
218 # Just seek to the end of the file (EOF).
221 # Get and store the position.
222 my $position = tell(FILE
),
224 # Close the file again.
227 # Return the position.