]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | |
3111df62 | 30 | require Guardian::Base; |
45bf2a03 | 31 | require Guardian::Config; |
ccf4f8e1 | 32 | require Guardian::Daemon; |
e8528a98 | 33 | require Guardian::Events; |
c95dfa5b | 34 | require Guardian::Logger; |
88d9af2c | 35 | require Guardian::Parser; |
6abac3d4 | 36 | require Guardian::Socket; |
88d9af2c | 37 | |
5c694317 SS |
38 | use warnings; |
39 | ||
e02ed027 SS |
40 | # Disable warnings of unjoinded threads when stopping guardian. |
41 | no warnings 'threads'; | |
42 | ||
45bf2a03 | 43 | # Define version. |
72ab6e2a | 44 | my $version ="@PACKAGE_VERSION@"; |
45bf2a03 | 45 | |
45bf2a03 SS |
46 | # Get and store the given command line arguments in a hash. |
47 | my %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. | |
57 | if (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"; | |
61 | print " -f, --foreground\trun in the foreground (doesn't fork, any output goes to STDOUT)\n"; | |
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. |
71 | if (&Guardian::Daemon::IsRunning()) { | |
72 | die "Another instance of Guardian is allready running...\n"; | |
73 | } | |
74 | ||
45bf2a03 SS |
75 | # Read-in the configuration file and store the settings. |
76 | # Push the may be given config file argument. | |
77 | my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"}); | |
78 | ||
c95dfa5b | 79 | # Initialize Logger. |
aab61123 | 80 | my $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 | ||
e8528a98 SS |
90 | # Initialize the event handler. |
91 | my $events = Guardian::Events->Init(%mainsettings); | |
92 | ||
cfe5a220 SS |
93 | # Hash to store the currently monitored files and their configured |
94 | # parsers. | |
95 | my %monitored_files = (); | |
96 | ||
97 | # Shared hash between the main process and all threads. It will store the | |
3111df62 | 98 | # monitored files and their current file position. |
cfe5a220 | 99 | my %file_positions :shared = (); |
3111df62 | 100 | |
88d9af2c SS |
101 | # Create the main queue. It is used to store and process all events which are |
102 | # reported and enqueued by the worker threads. | |
c0a59a63 | 103 | my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!";; |
88d9af2c | 104 | |
9c74e9bb SS |
105 | # Array to store all currently running worker objects. |
106 | # (Does not include the socket thread) | |
107 | my @running_workers; | |
108 | ||
ccf4f8e1 SS |
109 | # Check if guardian should be daemonized or keep in the foreground. |
110 | unless (defined($cmdargs{"foreground"})) { | |
111 | # Fork into background. | |
112 | &Guardian::Daemon::Daemonize(); | |
113 | } else { | |
114 | # Write PID (process-id). | |
115 | &Guardian::Daemon::WritePID(); | |
116 | } | |
117 | ||
88d9af2c SS |
118 | # Call Init function to initzialize guardian. |
119 | &Init(); | |
120 | ||
121 | # Infinite main loop, which processes all queued events. | |
122 | while(1) { | |
123 | # Get the amount of elements in our queue. | |
124 | # "undef" will be returned if it is empty. | |
125 | my $current_events = $queue->pending(); | |
126 | ||
127 | # If there is at least one element enqued | |
128 | if($current_events > 0) { | |
129 | # Grab the data of the top enqueued event. | |
130 | my $event = $queue->peek(); | |
131 | ||
c95dfa5b SS |
132 | # Log processed event. |
133 | $logger->Log("debug", "QUEUE - Processed event: $event"); | |
88d9af2c | 134 | |
e8528a98 SS |
135 | # Send event data to the events parser to determine |
136 | # if any action is required. | |
137 | $events->CheckAction($event); | |
138 | ||
88d9af2c SS |
139 | # Drop processed event from queue. |
140 | $queue->dequeue(); | |
141 | } | |
142 | ||
e8528a98 SS |
143 | # Call RemoveBlocks routine from the Events module to check |
144 | # if items from the block list can be dropped. | |
145 | $events->RemoveBlocks(); | |
146 | ||
01147a7e SS |
147 | # Sleep 10ms to reduce the load of the main process. |
148 | sleep(0.01); | |
88d9af2c SS |
149 | } |
150 | ||
151 | # | |
152 | ## Init function. | |
153 | # | |
154 | ## This function contains code which has to be executed while guardian | |
155 | ## is starting. | |
156 | # | |
157 | sub Init () { | |
915d9f45 SS |
158 | # Setup signal handler. |
159 | &SignalHandler(); | |
160 | ||
6abac3d4 SS |
161 | # Setup IPC mechanism via Socket in an own thread. |
162 | threads->create(\&Socket); | |
163 | ||
3111df62 SS |
164 | # Generate hash of monitored files. |
165 | %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files); | |
166 | ||
9c74e9bb SS |
167 | # Start worker threads. |
168 | &StartWorkers(); | |
88d9af2c SS |
169 | } |
170 | ||
171 | # | |
172 | ## Worker function. | |
173 | # | |
174 | ## This function is responsible for monitoring modifications of the given logfile, | |
175 | ## read them and pass them to the message parser. | |
176 | # | |
177 | ## To get file modifications the inotify subsystem of the linux kernel is used. | |
178 | # | |
179 | ## In order to prevent from permanently read and keep files opened, or dealing | |
180 | ## with huge logfiles, at initialization time of the worker process, the file will | |
181 | ## be opened once and the cursor position of the end of file (EOF) get stored. When | |
182 | ## reading any newly added lines from the file, we directly can jump to the last | |
183 | ## known position and get these lines. Afterwards, we store the current curser position | |
184 | ## again, so we can do it in this way over and over again. | |
185 | # | |
186 | ## All read lines get stored in an array, which will be passed to the Parser. | |
187 | # | |
188 | ## If any response (action) from the parser get recieved, it will be put into the | |
189 | ## shared event queue. | |
190 | # | |
191 | sub Worker ($) { | |
9c74e9bb SS |
192 | my $file = $_[0]; |
193 | ||
cfe5a220 SS |
194 | # Obtain the parser name which should be used to parser any |
195 | # messages of this file. | |
196 | my $parser = $monitored_files{$file}; | |
197 | ||
9c74e9bb SS |
198 | # Signal handler to kill worker. |
199 | $SIG{'KILL'} = sub { threads->exit(); }; | |
88d9af2c | 200 | |
88d9af2c | 201 | # Create inotify watcher. |
c0a59a63 | 202 | my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!"; |
88d9af2c SS |
203 | |
204 | # Monitor the specified file. | |
c0a59a63 | 205 | $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!"; |
88d9af2c | 206 | |
9c74e9bb SS |
207 | # Switch watcher into non-blocking mode. |
208 | $watcher->blocking(0); | |
88d9af2c | 209 | |
c95dfa5b SS |
210 | # Log successfully spawned worker. |
211 | $logger->Log("debug", "Spawned worker thread for: $file"); | |
212 | ||
9c74e9bb SS |
213 | # Infinite loop. |
214 | while(1) { | |
215 | # Check for any events and perform them, if there | |
216 | # is a least one. | |
217 | if ($watcher->read) { | |
218 | my @message = (); | |
88d9af2c | 219 | |
3111df62 | 220 | # Obtain fileposition from hash. |
cfe5a220 | 221 | my $fileposition = $file_positions{$file}; |
3111df62 | 222 | |
9c74e9bb | 223 | # Open the file. |
c0a59a63 | 224 | open (FILE, $file) or die "Could not open $file. $!"; |
88d9af2c | 225 | |
9c74e9bb SS |
226 | # Seek to the last known position. |
227 | seek (FILE, $fileposition, 0); | |
88d9af2c | 228 | |
9c74e9bb SS |
229 | # Get the log message. |
230 | while (my $line = <FILE>) { | |
231 | # Remove any newlines. | |
232 | chomp $line; | |
88d9af2c | 233 | |
9c74e9bb SS |
234 | # Add all lines to the message array. |
235 | push (@message, $line); | |
236 | } | |
88d9af2c | 237 | |
3111df62 SS |
238 | { |
239 | # Lock shared hash. | |
cfe5a220 | 240 | lock(%file_positions); |
3111df62 SS |
241 | |
242 | # Update fileposition. | |
cfe5a220 | 243 | $file_positions{$file} = tell(FILE); |
3111df62 | 244 | } |
88d9af2c | 245 | |
9c74e9bb SS |
246 | # Close file. |
247 | close(FILE); | |
88d9af2c | 248 | |
9c74e9bb | 249 | # Send filename and message to the parser, |
43fdb161 SS |
250 | # which will return if any actions have to be performed. |
251 | my @actions = &Guardian::Parser::Parser("$parser", @message); | |
88d9af2c | 252 | |
9c74e9bb SS |
253 | # Send the action to the main process and put it into |
254 | # the queue. | |
43fdb161 | 255 | if (@actions) { |
9c74e9bb SS |
256 | # Lock the queue. |
257 | lock($queue); | |
258 | ||
43fdb161 SS |
259 | # Loop through the actions array, and perform |
260 | # every single action. | |
261 | foreach my $action (@actions) { | |
262 | # Prevent from enqueuing empty actions. | |
263 | if (defined($action)) { | |
264 | # Put the required action into the queue. | |
265 | $queue->enqueue($action); | |
266 | } | |
267 | } | |
9c74e9bb SS |
268 | } |
269 | } else { | |
270 | # Sleep for 10ms until the next round of the loop will start. | |
271 | sleep(0.01); | |
88d9af2c SS |
272 | } |
273 | } | |
274 | } | |
275 | ||
6abac3d4 SS |
276 | # |
277 | ## Socket function. | |
278 | # | |
279 | ## This function uses the Socket module to create and listen to an UNIX socket. | |
280 | ## It automatically accepts all incomming connections and pass the recieved | |
281 | ## data messages to the the Message_Parser function which is also part of the | |
282 | ## socket module. | |
283 | # | |
284 | ## If a valid command has been sent through the socket, the corresponding event | |
285 | ## will be enqueued into the shared event queue. | |
286 | # | |
287 | sub Socket () { | |
288 | # Create the Server socket by calling the responsible function. | |
6bd7c588 | 289 | my $server = &Guardian::Socket::Server($mainsettings{SocketOwner}); |
6abac3d4 | 290 | |
c95dfa5b SS |
291 | # Log successfull creation of socket. |
292 | $logger->Log("debug", "Listening to Socket..."); | |
293 | ||
6abac3d4 SS |
294 | # Accept incomming connections from the socket. |
295 | while (my $connection = $server->accept()) { | |
296 | # Autoflush the socket after the data | |
297 | # has been recieved. | |
298 | $connection->autoflush(1); | |
299 | ||
300 | # Gather all data from the connection. | |
301 | while (my $message = <$connection>) { | |
302 | # Remove any newlines. | |
303 | chomp($message); | |
304 | ||
c95dfa5b SS |
305 | # Log recieved socket command. |
306 | $logger->Log("debug", "Socket - Recieved message: $message"); | |
307 | ||
6abac3d4 SS |
308 | # Send the recieved data message to the |
309 | # socket parser. | |
310 | my $action = &Guardian::Socket::Message_Parser($message); | |
311 | ||
312 | # If the parser returns to perform an action, | |
313 | # add it to the main event queue. | |
314 | if ($action) { | |
315 | # Lock the queue. | |
316 | lock($queue); | |
317 | ||
318 | # Enqueue the returned action. | |
319 | $queue->enqueue($action); | |
320 | } | |
321 | } | |
322 | } | |
323 | } | |
324 | ||
915d9f45 SS |
325 | # |
326 | ## Function for capturing process signals. | |
327 | # | |
328 | ## This function captures any sent process signals and will call various | |
329 | ## actions, basend on the captured signal. | |
330 | # | |
331 | sub SignalHandler { | |
332 | $SIG{INT} = \&Shutdown; | |
333 | $SIG{TERM} = \&Shutdown; | |
334 | $SIG{QUIT} = \&Shutdown; | |
ba734c53 | 335 | $SIG{HUP} = \&Reload; |
ab7c10eb | 336 | $SIG{USR1} = \&ReloadIgnoreList; |
915d9f45 SS |
337 | } |
338 | ||
9c74e9bb SS |
339 | # |
340 | ## Function to start the workers (threads) for all monitored files. | |
341 | # | |
3111df62 | 342 | ## This function will loop through the hash of monitored files and will |
9c74e9bb SS |
343 | ## spawn an own thread based worker for each file. Every created worker will |
344 | ## be added to the array of running workers. | |
345 | # | |
346 | sub StartWorkers () { | |
cfe5a220 SS |
347 | # Init/Update hash which contains the cursor position of EOF. |
348 | %file_positions = &Guardian::Base::FilePositions(\%monitored_files, \%file_positions); | |
349 | ||
3111df62 SS |
350 | # Loop through the hash which contains the monitored files and start |
351 | # a worker thread for each single one. | |
352 | foreach my $file (keys %monitored_files) { | |
c95dfa5b | 353 | $logger->Log("debug", "Starting worker thread for $file"); |
3111df62 SS |
354 | # Create worker thread for the file. |
355 | push @running_workers, threads->create(\&Worker,$file); | |
9c74e9bb SS |
356 | } |
357 | } | |
358 | ||
359 | # | |
360 | ## Function to stop all running workers. | |
361 | # | |
362 | ## This function is used to stop all currently running workers and will be | |
363 | ## called when reloading or shutting down guardian. | |
364 | # | |
365 | sub StopWorkers () { | |
366 | # Loop through all running workers. | |
367 | foreach my $worker (@running_workers) { | |
368 | # Send the worker the "KILL" signal and detach the | |
369 | # thread so perl can do an automatically clean-up. | |
4d19939e | 370 | $worker->kill('KILL'); |
9c74e9bb | 371 | } |
c95dfa5b | 372 | $logger->Log("debug", "All workers are stopped now..."); |
9c74e9bb SS |
373 | } |
374 | ||
ba734c53 SS |
375 | # |
376 | ## Reload function. | |
377 | # | |
378 | ## This function will get called if the signal handler recieves a "SIGHUP" signal, | |
379 | ## or the reload command will be sent via socket connection. It is responsible for | |
380 | ## reloading all configure options and stopping/starting the worker threads. | |
381 | # | |
382 | sub Reload () { | |
c95dfa5b SS |
383 | # Log reload. |
384 | $logger->Log("info", "Reload configuration..."); | |
385 | ||
ba734c53 SS |
386 | # Stop all running workers. |
387 | &StopWorkers(); | |
388 | ||
389 | # Re-read configuration file. | |
390 | %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"}); | |
391 | ||
fc555263 | 392 | # Update Logger settings. |
aab61123 | 393 | $logger = Guardian::Logger->Init(%mainsettings); |
fc555263 SS |
394 | |
395 | # Update logger object in mainsettings hash. | |
396 | $mainsettings{Logger} = $logger; | |
397 | ||
ab7c10eb SS |
398 | # Update ignore list. |
399 | &ReloadIgnoreList(); | |
57dc4265 | 400 | |
ba734c53 SS |
401 | # Re-generate hash of monitored files. |
402 | %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files); | |
403 | ||
404 | # Restart the worker threads. | |
405 | &StartWorkers(); | |
406 | } | |
407 | ||
ab7c10eb SS |
408 | # |
409 | ## ReloadIgnoreList function. | |
410 | # | |
411 | ## This function will be called if the signal handler recieves a "SIGUSR1" signal, | |
412 | ## or the reload-ignore-list command will be sent via the socket connection. It just | |
413 | ## performs a simple check if an ignore file has been configured and calls the responsible | |
414 | ## function on the events module. | |
415 | # | |
416 | sub ReloadIgnoreList () { | |
417 | # Update ignore list, if an ignorefile has been specified. | |
418 | if (exists($mainsettings{IgnoreFile})) { | |
419 | # Log reload of the ignore list. | |
420 | $logger->Log("info", "Reloading ignore list..."); | |
421 | ||
422 | # Call responsible function from the events module. | |
423 | &Guardian::Events::GenerateIgnoreList($mainsettings{IgnoreFile}); | |
424 | } | |
425 | } | |
426 | ||
648ca493 SS |
427 | # |
428 | ## Logrotate function. | |
429 | # | |
430 | ## This function only get called when the logrotate command will be sent via | |
431 | ## the socket connection. It is responsible for validating and altering the current | |
432 | ## curser positions of the monitored files and stopping/starting the worker threads. | |
433 | # | |
434 | sub Logrotate () { | |
435 | # Stop all running workers. | |
436 | &StopWorkers(); | |
437 | ||
438 | { | |
439 | # Lock shared hash. | |
440 | lock(%file_positions); | |
441 | ||
442 | # Loop through the hash which contains the current | |
443 | # file positions. | |
444 | foreach my $file (keys(%file_positions)) { | |
445 | # Obtain stored value from hash. | |
446 | my $stored_position = $file_positions{$file}; | |
447 | ||
448 | # Call function to get the current position. | |
449 | my $current_position = &Guardian::Base::GetFileposition($file); | |
450 | ||
451 | # Compare if the current position still matches | |
452 | # the stored one. | |
453 | if ($current_position ne $stored_position) { | |
454 | # Update to the new position, because | |
455 | # they has changed during file rotation. | |
456 | $file_positions{$file} = $current_position; | |
457 | ||
458 | # Log notice about rotated file. | |
459 | $logger->Log("debug", "$file have been rotated - Using new file position."); | |
460 | } | |
461 | } | |
462 | ||
463 | # After this bracket, the lock of the hash will be released. | |
464 | } | |
465 | ||
466 | # Restart all worker threads. | |
467 | &StartWorkers(); | |
468 | } | |
469 | ||
915d9f45 SS |
470 | # |
471 | ## Shutdown function. | |
472 | # | |
473 | ## This function is used to do a clean shutdown of guardian. It will be called | |
474 | ## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals. | |
475 | # | |
476 | sub Shutdown () { | |
c95dfa5b SS |
477 | # Log shutdown. |
478 | $logger->Log("info", "Shutting down..."); | |
479 | ||
9c74e9bb SS |
480 | # Stop all workers. |
481 | &StopWorkers(); | |
482 | ||
915d9f45 SS |
483 | # Remove socket file on exit. |
484 | &Guardian::Socket::RemoveSocketFile(); | |
485 | ||
0eb86493 SS |
486 | # Remove pid file on exit. |
487 | &Guardian::Daemon::RemovePIDFile(); | |
488 | ||
a9ef502c SS |
489 | # Sleep for one second to give perl some |
490 | # time to proper clean up everything before | |
491 | # exiting. | |
492 | sleep(1); | |
493 | ||
c95dfa5b SS |
494 | # Log good bye message. |
495 | $logger->Log("debug", "Good Bye!"); | |
496 | ||
915d9f45 SS |
497 | # Exit guardian. |
498 | exit; | |
499 | } |