Use socket module to provide an IPC mechanism.
[people/stevee/guardian.git] / guardian
CommitLineData
88d9af2c
SS
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2015 IPFire Development Team #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22use strict;
23use threads;
24use threads::shared;
25use Thread::Queue;
26use Linux::Inotify2;
27
28require Guardian::Parser;
6abac3d4 29require Guardian::Socket;
88d9af2c
SS
30
31# Array to store the monitored logfiles.
32my @monitored_files = (
33 "/var/log/snort/alert",
34);
35
36# Create the main queue. It is used to store and process all events which are
37# reported and enqueued by the worker threads.
38my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!\n";;
39
40# Call Init function to initzialize guardian.
41&Init();
42
43# Infinite main loop, which processes all queued events.
44while(1) {
45 # Get the amount of elements in our queue.
46 # "undef" will be returned if it is empty.
47 my $current_events = $queue->pending();
48
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();
53
54 print "Got event: $event\n";
55
56 # Drop processed event from queue.
57 $queue->dequeue();
58 }
59
60 # XXX
61 # Temporary workaround to reduce the load of the main process.
62 sleep(1);
63}
64
65#
66## Init function.
67#
68## This function contains code which has to be executed while guardian
69## is starting.
70#
71sub Init () {
6abac3d4
SS
72 # Setup IPC mechanism via Socket in an own thread.
73 threads->create(\&Socket);
74
88d9af2c
SS
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);
82 }
83 }
84}
85
86#
87## Worker function.
88#
89## This function is responsible for monitoring modifications of the given logfile,
90## read them and pass them to the message parser.
91#
92## To get file modifications the inotify subsystem of the linux kernel is used.
93#
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.
100#
101## All read lines get stored in an array, which will be passed to the Parser.
102#
103## If any response (action) from the parser get recieved, it will be put into the
104## shared event queue.
105#
106sub Worker ($) {
107 my $file = @_[0];
108
109 # Get the fileposition.
110 my $fileposition = &Init_fileposition("$file");
111
112 # Create inotify watcher.
113 my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!\n";
114
115 # Monitor the specified file.
116 $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!\n";
117
118 # Get all notifications.
119 while ($watcher->read) {
120 my @message = ();
121
122 # Open the file.
123 open (FILE, $file) or die "Could not open $file. $!\n";
124
125 # Seek to the last known position.
126 seek (FILE, $fileposition, 0);
127
128 # Get the log message.
129 while (my $line = <FILE>) {
130 # Remove any newlines.
131 chomp $line;
132
133 # Add all lines to the message array.
134 push (@message, $line);
135 }
136
137 # Update fileposition.
138 $fileposition = tell(FILE);
139
140 # Close file.
141 close(FILE);
142
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);
146
147 # Send the action to the main process and put it into
148 # the queue.
149 if (@action) {
150 # Lock the queue.
151 lock($queue);
152
153 # Put the required action into the queue.
154 $queue->enqueue(@action);
155 }
156 }
157}
158
6abac3d4
SS
159#
160## Socket function.
161#
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
165## socket module.
166#
167## If a valid command has been sent through the socket, the corresponding event
168## will be enqueued into the shared event queue.
169#
170sub Socket () {
171 # Create the Server socket by calling the responsible function.
172 my $server = &Guardian::Socket::Server();
173
174 # Accept incomming connections from the socket.
175 while (my $connection = $server->accept()) {
176 # Autoflush the socket after the data
177 # has been recieved.
178 $connection->autoflush(1);
179
180 # Gather all data from the connection.
181 while (my $message = <$connection>) {
182 # Remove any newlines.
183 chomp($message);
184
185 # Send the recieved data message to the
186 # socket parser.
187 my $action = &Guardian::Socket::Message_Parser($message);
188
189 # If the parser returns to perform an action,
190 # add it to the main event queue.
191 if ($action) {
192 # Lock the queue.
193 lock($queue);
194
195 # Enqueue the returned action.
196 $queue->enqueue($action);
197 }
198 }
199 }
200}
201
88d9af2c
SS
202#
203## Function for fileposition initialization.
204#
205## This function is used to get the cursor position of the end of file (EOF) of
206## a specified file.
207#
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.
211#
212sub Init_fileposition ($) {
213 my $file = $_[0];
214
215 # Open the file.
216 open(FILE, $file) or die "Could not open $file. $!\n";
217
218 # Just seek to the end of the file (EOF).
219 seek(FILE, 0, 2);
220
221 # Get and store the position.
222 my $position = tell(FILE),
223
224 # Close the file again.
225 close(FILE);
226
227 # Return the position.
228 return $position;
229}