]> git.ipfire.org Git - people/stevee/guardian.git/blame - guardian.in
Parser: Adjust HTTP parser to be compatible with newer log format.
[people/stevee/guardian.git] / guardian.in
CommitLineData
88d9af2c
SS
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
ab7c10eb 5# Copyright (C) 2015-2016 IPFire Development Team #
88d9af2c
SS
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;
45bf2a03 25use Getopt::Long;
88d9af2c
SS
26use Thread::Queue;
27use Linux::Inotify2;
01147a7e 28use Time::HiRes qw[ time sleep ];
88d9af2c 29
3111df62 30require Guardian::Base;
45bf2a03 31require Guardian::Config;
ccf4f8e1 32require Guardian::Daemon;
e8528a98 33require Guardian::Events;
c95dfa5b 34require Guardian::Logger;
88d9af2c 35require Guardian::Parser;
6abac3d4 36require Guardian::Socket;
88d9af2c 37
5c694317
SS
38use warnings;
39
a2f46a64 40# Disable warnings of unjoined threads when stopping guardian.
e02ed027
SS
41no warnings 'threads';
42
45bf2a03 43# Define version.
72ab6e2a 44my $version ="@PACKAGE_VERSION@";
45bf2a03 45
45bf2a03
SS
46# Get and store the given command line arguments in a hash.
47my %cmdargs = ();
48
49&GetOptions (\%cmdargs,
50 'foreground|f',
51 'config|c=s',
52 'help|h',
53 'version|v',
54);
55
56# Show help / version information.
57if (defined($cmdargs{"help"})) {
58 print "Guardian $version \n";
59 print "Usage: guardian <optional arguments>\n";
60 print " -c, --config\t\tspecifiy a configuration file other than the default (/etc/guardian/guardian.conf)\n";
a2f46a64 61 print " -f, --foreground\turn in the foreground (doesn't fork, any output goes to STDOUT)\n";
45bf2a03
SS
62 print " -h, --help\t\tshows this help\n";
63 print " -v, --version\t\tdisplay programm version and exit.\n";
64 exit;
65} elsif (defined($cmdargs{"version"})) {
66 print "Guardian $version \n";
67 exit;
68}
69
624876cf
SS
70# Check if another instance of guardian is allready running.
71if (&Guardian::Daemon::IsRunning()) {
a2f46a64 72 die "Another instance of Guardian is already running...\n";
624876cf
SS
73}
74
45bf2a03
SS
75# Read-in the configuration file and store the settings.
76# Push the may be given config file argument.
77my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
78
c95dfa5b 79# Initialize Logger.
aab61123 80my $logger = Guardian::Logger->Init(%mainsettings);
c95dfa5b
SS
81$logger->Log("debug", "Logger successfully initialized...");
82
fc555263
SS
83# Add the logger object to the mainsettings for passing
84# it to the modules.
85$mainsettings{Logger} = $logger;
86
0815025a
SS
87# Redirect perls "die" messages to the logger before exiting.
88$SIG{__DIE__} = sub { $logger->Log("err", "@_"); };
89
df8eb305
SS
90# Initialize the parser module.
91my $parser = Guardian::Parser->Init(%mainsettings);
92
e8528a98
SS
93# Initialize the event handler.
94my $events = Guardian::Events->Init(%mainsettings);
95
cfe5a220
SS
96# Hash to store the currently monitored files and their configured
97# parsers.
98my %monitored_files = ();
99
100# Shared hash between the main process and all threads. It will store the
3111df62 101# monitored files and their current file position.
cfe5a220 102my %file_positions :shared = ();
3111df62 103
88d9af2c
SS
104# Create the main queue. It is used to store and process all events which are
105# reported and enqueued by the worker threads.
c0a59a63 106my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!";;
88d9af2c 107
bac2d500 108# Hash to store all currently running worker objects and their corresponding files.
9c74e9bb 109# (Does not include the socket thread)
bac2d500
SS
110my %running_workers;
111
112# Variable to store if the workers should pause or compute.
113my $workers_pause :shared = 0;
9c74e9bb 114
ccf4f8e1
SS
115# Check if guardian should be daemonized or keep in the foreground.
116unless (defined($cmdargs{"foreground"})) {
117 # Fork into background.
118 &Guardian::Daemon::Daemonize();
119} else {
120 # Write PID (process-id).
121 &Guardian::Daemon::WritePID();
122}
123
88d9af2c
SS
124# Call Init function to initzialize guardian.
125&Init();
126
db81766d
SS
127# Log successfully started process.
128$logger->Log("info", "Guardian $version successfully started...");
129
88d9af2c
SS
130# Infinite main loop, which processes all queued events.
131while(1) {
132 # Get the amount of elements in our queue.
133 # "undef" will be returned if it is empty.
134 my $current_events = $queue->pending();
135
136 # If there is at least one element enqued
137 if($current_events > 0) {
138 # Grab the data of the top enqueued event.
139 my $event = $queue->peek();
140
c95dfa5b
SS
141 # Log processed event.
142 $logger->Log("debug", "QUEUE - Processed event: $event");
88d9af2c 143
e8528a98
SS
144 # Send event data to the events parser to determine
145 # if any action is required.
146 $events->CheckAction($event);
147
88d9af2c
SS
148 # Drop processed event from queue.
149 $queue->dequeue();
150 }
151
e8528a98
SS
152 # Call RemoveBlocks routine from the Events module to check
153 # if items from the block list can be dropped.
154 $events->RemoveBlocks();
155
396aba99
SS
156 # Sleep 50ms to reduce the load of the main process.
157 sleep(0.05);
88d9af2c
SS
158}
159
160#
161## Init function.
162#
163## This function contains code which has to be executed while guardian
164## is starting.
165#
166sub Init () {
915d9f45
SS
167 # Setup signal handler.
168 &SignalHandler();
169
6abac3d4
SS
170 # Setup IPC mechanism via Socket in an own thread.
171 threads->create(\&Socket);
172
3111df62
SS
173 # Generate hash of monitored files.
174 %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
175
9c74e9bb
SS
176 # Start worker threads.
177 &StartWorkers();
88d9af2c
SS
178}
179
180#
181## Worker function.
182#
183## This function is responsible for monitoring modifications of the given logfile,
184## read them and pass them to the message parser.
185#
186## To get file modifications the inotify subsystem of the linux kernel is used.
187#
188## In order to prevent from permanently read and keep files opened, or dealing
189## with huge logfiles, at initialization time of the worker process, the file will
190## be opened once and the cursor position of the end of file (EOF) get stored. When
191## reading any newly added lines from the file, we directly can jump to the last
a2f46a64 192## known position and get these lines. Afterwards, we store the current cursor position
88d9af2c
SS
193## again, so we can do it in this way over and over again.
194#
195## All read lines get stored in an array, which will be passed to the Parser.
196#
a2f46a64 197## If any response (action) from the parser is received, it will be put into the
88d9af2c
SS
198## shared event queue.
199#
200sub Worker ($) {
9c74e9bb
SS
201 my $file = $_[0];
202
a2f46a64 203 # Obtain the parser name which should be used to parse any
cfe5a220 204 # messages of this file.
df8eb305 205 my $use_parser = $monitored_files{$file};
cfe5a220 206
9c74e9bb
SS
207 # Signal handler to kill worker.
208 $SIG{'KILL'} = sub { threads->exit(); };
88d9af2c 209
88d9af2c 210 # Create inotify watcher.
c0a59a63 211 my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!";
88d9af2c
SS
212
213 # Monitor the specified file.
c0a59a63 214 $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!";
88d9af2c 215
9c74e9bb
SS
216 # Switch watcher into non-blocking mode.
217 $watcher->blocking(0);
88d9af2c 218
c95dfa5b
SS
219 # Log successfully spawned worker.
220 $logger->Log("debug", "Spawned worker thread for: $file");
221
9c74e9bb
SS
222 # Infinite loop.
223 while(1) {
a2f46a64 224 # Check if the workers should pause or perform their desired work.
bac2d500
SS
225 if ($workers_pause) {
226 # Wait 1 second until the next check.
227 sleep(1);
228 } else {
229 # Check for any events and perform them, if there
230 # is a least one.
231 if ($watcher->read) {
232 my @message = ();
88d9af2c 233
bac2d500
SS
234 # Obtain fileposition from hash.
235 my $fileposition = $file_positions{$file};
88d9af2c 236
bac2d500
SS
237 # Open the file.
238 open (FILE, $file) or die "Could not open $file. $!";
88d9af2c 239
bac2d500
SS
240 # Seek to the last known position.
241 seek (FILE, $fileposition, 0);
88d9af2c 242
bac2d500
SS
243 # Get the log message.
244 while (my $line = <FILE>) {
245 # Remove any newlines.
246 chomp $line;
3111df62 247
bac2d500
SS
248 # Add all lines to the message array.
249 push (@message, $line);
250 }
88d9af2c 251
bac2d500
SS
252 {
253 # Lock shared hash.
254 lock(%file_positions);
88d9af2c 255
bac2d500
SS
256 # Update fileposition.
257 $file_positions{$file} = tell(FILE);
258 }
9c74e9bb 259
bac2d500
SS
260 # Close file.
261 close(FILE);
262
263 # Send filename and message to the parser,
264 # which will return if any actions have to be performed.
df8eb305 265 my @actions = $parser->Parser("$use_parser", @message);
bac2d500
SS
266
267 # Send the action to the main process and put it into
268 # the queue.
269 if (@actions) {
270 # Lock the queue.
271 lock($queue);
272
273 # Loop through the actions array, and perform
274 # every single action.
275 foreach my $action (@actions) {
276 # Prevent from enqueuing empty actions.
277 if (defined($action)) {
278 # Put the required action into the queue.
279 $queue->enqueue($action);
280 }
43fdb161
SS
281 }
282 }
bac2d500 283 } else {
396aba99
SS
284 # Sleep for 100ms until the next round of the loop will start.
285 sleep(0.1);
9c74e9bb 286 }
88d9af2c
SS
287 }
288 }
289}
290
6abac3d4
SS
291#
292## Socket function.
293#
294## This function uses the Socket module to create and listen to an UNIX socket.
a2f46a64 295## It automatically accepts all incoming connections and pass the received
6abac3d4
SS
296## data messages to the the Message_Parser function which is also part of the
297## socket module.
298#
299## If a valid command has been sent through the socket, the corresponding event
300## will be enqueued into the shared event queue.
301#
302sub Socket () {
303 # Create the Server socket by calling the responsible function.
6bd7c588 304 my $server = &Guardian::Socket::Server($mainsettings{SocketOwner});
6abac3d4 305
c95dfa5b
SS
306 # Log successfull creation of socket.
307 $logger->Log("debug", "Listening to Socket...");
308
a2f46a64 309 # Accept incoming connections from the socket.
6abac3d4
SS
310 while (my $connection = $server->accept()) {
311 # Autoflush the socket after the data
a2f46a64 312 # has been received.
6abac3d4
SS
313 $connection->autoflush(1);
314
315 # Gather all data from the connection.
316 while (my $message = <$connection>) {
317 # Remove any newlines.
318 chomp($message);
319
a2f46a64 320 # Log received socket command.
c95dfa5b
SS
321 $logger->Log("debug", "Socket - Recieved message: $message");
322
a2f46a64 323 # Send the received data message to the
6abac3d4
SS
324 # socket parser.
325 my $action = &Guardian::Socket::Message_Parser($message);
326
327 # If the parser returns to perform an action,
328 # add it to the main event queue.
329 if ($action) {
330 # Lock the queue.
331 lock($queue);
332
333 # Enqueue the returned action.
334 $queue->enqueue($action);
335 }
336 }
337 }
338}
339
915d9f45
SS
340#
341## Function for capturing process signals.
342#
343## This function captures any sent process signals and will call various
a2f46a64 344## actions, based on the captured signal.
915d9f45
SS
345#
346sub SignalHandler {
347 $SIG{INT} = \&Shutdown;
348 $SIG{TERM} = \&Shutdown;
349 $SIG{QUIT} = \&Shutdown;
ba734c53 350 $SIG{HUP} = \&Reload;
ab7c10eb 351 $SIG{USR1} = \&ReloadIgnoreList;
915d9f45
SS
352}
353
9c74e9bb
SS
354#
355## Function to start the workers (threads) for all monitored files.
356#
3111df62 357## This function will loop through the hash of monitored files and will
9c74e9bb
SS
358## spawn an own thread based worker for each file. Every created worker will
359## be added to the array of running workers.
360#
361sub StartWorkers () {
cfe5a220
SS
362 # Init/Update hash which contains the cursor position of EOF.
363 %file_positions = &Guardian::Base::FilePositions(\%monitored_files, \%file_positions);
364
3111df62
SS
365 # Loop through the hash which contains the monitored files and start
366 # a worker thread for each single one.
367 foreach my $file (keys %monitored_files) {
a2f46a64 368 # Check if an worker is already running for this file.
bac2d500
SS
369 # If not, start the worker.
370 unless (exists($running_workers{$file})) {
371 $logger->Log("debug", "Starting worker thread for $file");
372
373 # Create worker thread for the file.
374 $running_workers{$file} = threads->create(\&Worker,$file);
375 }
9c74e9bb
SS
376 }
377}
378
379#
380## Function to stop all running workers.
381#
382## This function is used to stop all currently running workers and will be
383## called when reloading or shutting down guardian.
384#
385sub StopWorkers () {
386 # Loop through all running workers.
bac2d500
SS
387 foreach my $worker (keys %running_workers) {
388 # Determine if the worker should be stopped.
a2f46a64 389 # This happens if the file should not be longer monitored.
bac2d500
SS
390 unless(exists($monitored_files{$worker})) {
391 $logger->Log("debug", "Stopping worker thread for $worker");
392
393 # Send a "KILL" signal to the worker.
394 $running_workers{$worker}->kill('KILL');
395
396 # Remove worker from hash of running workers.
397 delete($running_workers{$worker});
398 }
9c74e9bb 399 }
bac2d500
SS
400
401 # Get amount of currently running worker threads.
402 if (! keys(%running_workers)) {
403 $logger->Log("debug", "All workers have been stopped...");
404 }
405
406 # Return nothing.
407 return;
408}
409
410#
411## Function to pause all running workers.
412#
413## This function is used to pause all currently running workers.
414#
415sub PauseWorkers() {
416 # Set workers_pause variable to "1".
a2f46a64 417 # All workers will sleep until the variable has been set to "0".
bac2d500
SS
418 $workers_pause = 1;
419
420 # Log paused workers.
421 $logger->Log("debug", "All workers have been paused...");
422
423 # Return nothing.
424 return;
425}
426
427#
428## Function to continue all running workers.
429#
430## This function is used to continue all paused workers.
431#
432sub ResumeWorkers() {
433 # Set workers_suspend variable to "0" - they will continue their work
434 # again.
435 $workers_pause = 0;
436
437 # Log continued workers.
438 $logger->Log("debug", "All workers are working again...");
439
440 # Return nothing.
441 return;
9c74e9bb
SS
442}
443
ba734c53
SS
444#
445## Reload function.
446#
a2f46a64 447## This function will get called if the signal handler receives a "SIGHUP" signal,
ba734c53
SS
448## or the reload command will be sent via socket connection. It is responsible for
449## reloading all configure options and stopping/starting the worker threads.
450#
451sub Reload () {
c95dfa5b
SS
452 # Log reload.
453 $logger->Log("info", "Reload configuration...");
454
bac2d500
SS
455 # Pause all running workers.
456 &PauseWorkers();
ba734c53
SS
457
458 # Re-read configuration file.
459 %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
460
fc555263 461 # Update Logger settings.
aab61123 462 $logger = Guardian::Logger->Init(%mainsettings);
fc555263
SS
463
464 # Update logger object in mainsettings hash.
465 $mainsettings{Logger} = $logger;
466
df8eb305
SS
467 # Update Parser handler.
468 $parser->Update(\%mainsettings);
469
10442560
SS
470 # Update Event handler.
471 $events->Update(\%mainsettings);
472
ab7c10eb
SS
473 # Update ignore list.
474 &ReloadIgnoreList();
57dc4265 475
ba734c53
SS
476 # Re-generate hash of monitored files.
477 %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
478
bac2d500
SS
479 # Stop workers if they are not needed anymore.
480 &StopWorkers();
481
482 # Start new worker threads if required.
ba734c53 483 &StartWorkers();
bac2d500
SS
484
485 # Resume workers.
486 &ResumeWorkers();
487
488 # Return nothing.
489 return;
ba734c53
SS
490}
491
ab7c10eb
SS
492#
493## ReloadIgnoreList function.
494#
a2f46a64 495## This function will be called if the signal handler receives a "SIGUSR1" signal,
ab7c10eb
SS
496## or the reload-ignore-list command will be sent via the socket connection. It just
497## performs a simple check if an ignore file has been configured and calls the responsible
498## function on the events module.
499#
500sub ReloadIgnoreList () {
501 # Update ignore list, if an ignorefile has been specified.
502 if (exists($mainsettings{IgnoreFile})) {
503 # Log reload of the ignore list.
504 $logger->Log("info", "Reloading ignore list...");
505
506 # Call responsible function from the events module.
507 &Guardian::Events::GenerateIgnoreList($mainsettings{IgnoreFile});
508 }
509}
510
648ca493
SS
511#
512## Logrotate function.
513#
514## This function only get called when the logrotate command will be sent via
515## the socket connection. It is responsible for validating and altering the current
a2f46a64 516## cursor positions of the monitored files and stopping/starting the worker threads.
648ca493
SS
517#
518sub Logrotate () {
519 # Stop all running workers.
bac2d500 520 &PauseWorkers();
648ca493
SS
521
522 {
523 # Lock shared hash.
524 lock(%file_positions);
525
526 # Loop through the hash which contains the current
527 # file positions.
528 foreach my $file (keys(%file_positions)) {
529 # Obtain stored value from hash.
530 my $stored_position = $file_positions{$file};
531
532 # Call function to get the current position.
533 my $current_position = &Guardian::Base::GetFileposition($file);
534
535 # Compare if the current position still matches
536 # the stored one.
537 if ($current_position ne $stored_position) {
538 # Update to the new position, because
539 # they has changed during file rotation.
540 $file_positions{$file} = $current_position;
541
542 # Log notice about rotated file.
543 $logger->Log("debug", "$file have been rotated - Using new file position.");
544 }
545 }
546
547 # After this bracket, the lock of the hash will be released.
548 }
549
550 # Restart all worker threads.
bac2d500
SS
551 &ResumeWorkers();
552
553 # Return nothing.
554 return;
648ca493
SS
555}
556
915d9f45
SS
557#
558## Shutdown function.
559#
560## This function is used to do a clean shutdown of guardian. It will be called
a2f46a64 561## by the signal handler when receiving INT (2), QUIT (3) and TERM (15) signals.
915d9f45
SS
562#
563sub Shutdown () {
c95dfa5b
SS
564 # Log shutdown.
565 $logger->Log("info", "Shutting down...");
566
bac2d500
SS
567 # Reset hash of monitored files.
568 %monitored_files = ();
569
9c74e9bb
SS
570 # Stop all workers.
571 &StopWorkers();
572
24f500ae
SS
573 # Unblock all blocked hosts.
574 &Guardian::Events::CallFlush();
575
915d9f45
SS
576 # Remove socket file on exit.
577 &Guardian::Socket::RemoveSocketFile();
578
0eb86493
SS
579 # Remove pid file on exit.
580 &Guardian::Daemon::RemovePIDFile();
581
a9ef502c
SS
582 # Sleep for one second to give perl some
583 # time to proper clean up everything before
584 # exiting.
585 sleep(1);
586
c95dfa5b
SS
587 # Log good bye message.
588 $logger->Log("debug", "Good Bye!");
589
915d9f45
SS
590 # Exit guardian.
591 exit;
592}