]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | use strict; | |
23 | use threads; | |
24 | use threads::shared; | |
45bf2a03 | 25 | use Getopt::Long; |
88d9af2c SS |
26 | use Thread::Queue; |
27 | use Linux::Inotify2; | |
01147a7e | 28 | use Time::HiRes qw[ time sleep ]; |
88d9af2c | 29 | |
45bf2a03 | 30 | require Guardian::Config; |
88d9af2c | 31 | require Guardian::Parser; |
6abac3d4 | 32 | require Guardian::Socket; |
88d9af2c | 33 | |
5c694317 SS |
34 | use warnings; |
35 | ||
45bf2a03 SS |
36 | # Define version. |
37 | my $version ="2.0"; | |
38 | ||
88d9af2c SS |
39 | # Array to store the monitored logfiles. |
40 | my @monitored_files = ( | |
41 | "/var/log/snort/alert", | |
42 | ); | |
43 | ||
45bf2a03 SS |
44 | # Get and store the given command line arguments in a hash. |
45 | my %cmdargs = (); | |
46 | ||
47 | &GetOptions (\%cmdargs, | |
48 | 'foreground|f', | |
49 | 'config|c=s', | |
50 | 'help|h', | |
51 | 'version|v', | |
52 | ); | |
53 | ||
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"; | |
62 | exit; | |
63 | } elsif (defined($cmdargs{"version"})) { | |
64 | print "Guardian $version \n"; | |
65 | exit; | |
66 | } | |
67 | ||
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"}); | |
71 | ||
88d9af2c SS |
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";; | |
75 | ||
76 | # Call Init function to initzialize guardian. | |
77 | &Init(); | |
78 | ||
79 | # Infinite main loop, which processes all queued events. | |
80 | while(1) { | |
81 | # Get the amount of elements in our queue. | |
82 | # "undef" will be returned if it is empty. | |
83 | my $current_events = $queue->pending(); | |
84 | ||
85 | # If there is at least one element enqued | |
86 | if($current_events > 0) { | |
87 | # Grab the data of the top enqueued event. | |
88 | my $event = $queue->peek(); | |
89 | ||
90 | print "Got event: $event\n"; | |
91 | ||
92 | # Drop processed event from queue. | |
93 | $queue->dequeue(); | |
94 | } | |
95 | ||
01147a7e SS |
96 | # Sleep 10ms to reduce the load of the main process. |
97 | sleep(0.01); | |
88d9af2c SS |
98 | } |
99 | ||
100 | # | |
101 | ## Init function. | |
102 | # | |
103 | ## This function contains code which has to be executed while guardian | |
104 | ## is starting. | |
105 | # | |
106 | sub Init () { | |
915d9f45 SS |
107 | # Setup signal handler. |
108 | &SignalHandler(); | |
109 | ||
6abac3d4 SS |
110 | # Setup IPC mechanism via Socket in an own thread. |
111 | threads->create(\&Socket); | |
112 | ||
88d9af2c SS |
113 | # Loop through the array of which files should be monitored and |
114 | # create a worker thread for each single one. | |
115 | foreach my $monitored_file (@monitored_files) { | |
116 | # Check if the file exists and is readable. | |
117 | if (-r "$monitored_file") { | |
118 | # Create worker thread for the file. | |
119 | threads->create(\&Worker,$monitored_file); | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | # | |
125 | ## Worker function. | |
126 | # | |
127 | ## This function is responsible for monitoring modifications of the given logfile, | |
128 | ## read them and pass them to the message parser. | |
129 | # | |
130 | ## To get file modifications the inotify subsystem of the linux kernel is used. | |
131 | # | |
132 | ## In order to prevent from permanently read and keep files opened, or dealing | |
133 | ## with huge logfiles, at initialization time of the worker process, the file will | |
134 | ## be opened once and the cursor position of the end of file (EOF) get stored. When | |
135 | ## reading any newly added lines from the file, we directly can jump to the last | |
136 | ## known position and get these lines. Afterwards, we store the current curser position | |
137 | ## again, so we can do it in this way over and over again. | |
138 | # | |
139 | ## All read lines get stored in an array, which will be passed to the Parser. | |
140 | # | |
141 | ## If any response (action) from the parser get recieved, it will be put into the | |
142 | ## shared event queue. | |
143 | # | |
144 | sub Worker ($) { | |
145 | my $file = @_[0]; | |
146 | ||
147 | # Get the fileposition. | |
148 | my $fileposition = &Init_fileposition("$file"); | |
149 | ||
150 | # Create inotify watcher. | |
151 | my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!\n"; | |
152 | ||
153 | # Monitor the specified file. | |
154 | $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!\n"; | |
155 | ||
156 | # Get all notifications. | |
157 | while ($watcher->read) { | |
158 | my @message = (); | |
159 | ||
160 | # Open the file. | |
161 | open (FILE, $file) or die "Could not open $file. $!\n"; | |
162 | ||
163 | # Seek to the last known position. | |
164 | seek (FILE, $fileposition, 0); | |
165 | ||
166 | # Get the log message. | |
167 | while (my $line = <FILE>) { | |
168 | # Remove any newlines. | |
169 | chomp $line; | |
170 | ||
171 | # Add all lines to the message array. | |
172 | push (@message, $line); | |
173 | } | |
174 | ||
175 | # Update fileposition. | |
176 | $fileposition = tell(FILE); | |
177 | ||
178 | # Close file. | |
179 | close(FILE); | |
180 | ||
181 | # Send filename and message to the parser, | |
182 | # which will return if an action has to be performed. | |
183 | my @action = &Guardian::Parser::Parser("$file", @message); | |
184 | ||
185 | # Send the action to the main process and put it into | |
186 | # the queue. | |
187 | if (@action) { | |
188 | # Lock the queue. | |
189 | lock($queue); | |
190 | ||
191 | # Put the required action into the queue. | |
192 | $queue->enqueue(@action); | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
6abac3d4 SS |
197 | # |
198 | ## Socket function. | |
199 | # | |
200 | ## This function uses the Socket module to create and listen to an UNIX socket. | |
201 | ## It automatically accepts all incomming connections and pass the recieved | |
202 | ## data messages to the the Message_Parser function which is also part of the | |
203 | ## socket module. | |
204 | # | |
205 | ## If a valid command has been sent through the socket, the corresponding event | |
206 | ## will be enqueued into the shared event queue. | |
207 | # | |
208 | sub Socket () { | |
209 | # Create the Server socket by calling the responsible function. | |
210 | my $server = &Guardian::Socket::Server(); | |
211 | ||
212 | # Accept incomming connections from the socket. | |
213 | while (my $connection = $server->accept()) { | |
214 | # Autoflush the socket after the data | |
215 | # has been recieved. | |
216 | $connection->autoflush(1); | |
217 | ||
218 | # Gather all data from the connection. | |
219 | while (my $message = <$connection>) { | |
220 | # Remove any newlines. | |
221 | chomp($message); | |
222 | ||
223 | # Send the recieved data message to the | |
224 | # socket parser. | |
225 | my $action = &Guardian::Socket::Message_Parser($message); | |
226 | ||
227 | # If the parser returns to perform an action, | |
228 | # add it to the main event queue. | |
229 | if ($action) { | |
230 | # Lock the queue. | |
231 | lock($queue); | |
232 | ||
233 | # Enqueue the returned action. | |
234 | $queue->enqueue($action); | |
235 | } | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
88d9af2c SS |
240 | # |
241 | ## Function for fileposition initialization. | |
242 | # | |
243 | ## This function is used to get the cursor position of the end of file (EOF) of | |
244 | ## a specified file. | |
245 | # | |
246 | ## In order to prevent from permanently read and keep files opened, or dealing | |
247 | ## with huge logfiles, at initialization time of the worker processes, the file will | |
248 | ## be opened once and the cursor position of the end of file (EOF) get stored. | |
249 | # | |
250 | sub Init_fileposition ($) { | |
251 | my $file = $_[0]; | |
252 | ||
253 | # Open the file. | |
254 | open(FILE, $file) or die "Could not open $file. $!\n"; | |
255 | ||
256 | # Just seek to the end of the file (EOF). | |
257 | seek(FILE, 0, 2); | |
258 | ||
259 | # Get and store the position. | |
260 | my $position = tell(FILE), | |
261 | ||
262 | # Close the file again. | |
263 | close(FILE); | |
264 | ||
265 | # Return the position. | |
266 | return $position; | |
267 | } | |
915d9f45 SS |
268 | |
269 | # | |
270 | ## Function for capturing process signals. | |
271 | # | |
272 | ## This function captures any sent process signals and will call various | |
273 | ## actions, basend on the captured signal. | |
274 | # | |
275 | sub SignalHandler { | |
276 | $SIG{INT} = \&Shutdown; | |
277 | $SIG{TERM} = \&Shutdown; | |
278 | $SIG{QUIT} = \&Shutdown; | |
279 | } | |
280 | ||
281 | # | |
282 | ## Shutdown function. | |
283 | # | |
284 | ## This function is used to do a clean shutdown of guardian. It will be called | |
285 | ## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals. | |
286 | # | |
287 | sub Shutdown () { | |
288 | # Remove socket file on exit. | |
289 | &Guardian::Socket::RemoveSocketFile(); | |
290 | ||
291 | # Exit guardian. | |
292 | exit; | |
293 | } |