]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/cfgroot/ids-functions.pl
ids-functions.pl: Add process_ruleset() function.
[people/pmueller/ipfire-2.x.git] / config / cfgroot / ids-functions.pl
CommitLineData
8dcebe53
SS
1#!/usr/bin/perl -w
2############################################################################
3# #
4# This file is part of the IPFire Firewall. #
5# #
6# IPFire is free software; you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation; either version 2 of the License, or #
9# (at your option) any later version. #
10# #
11# IPFire is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with IPFire; if not, write to the Free Software #
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
19# #
fd2dccaa 20# Copyright (C) 2018-2019 IPFire Team <info@ipfire.org> #
8dcebe53
SS
21# #
22############################################################################
23
e55fa2f7
SS
24use strict;
25
8dcebe53
SS
26package IDS;
27
28require '/var/ipfire/general-functions.pl';
abffcc99 29require "${General::swroot}/network-functions.pl";
35bc92a3 30require "${General::swroot}/suricata/ruleset-sources";
8dcebe53 31
7d895608
SS
32# Load perl module to deal with Archives.
33use Archive::Tar;
34
35# Load perl module to deal with files and path.
36use File::Basename;
37
38# Load module to move files.
39use File::Copy;
40
41# Load module to recursely remove files and a folder.
42use File::Path qw(rmtree);
43
44# Load module to get file stats.
45use File::stat;
46
47# Load module to deal with temporary files.
48use File::Temp;
49
061391e7
SS
50# Load module to deal with the date formats used by the HTTP protocol.
51use HTTP::Date;
52
7d895608
SS
53# Load the libwwwperl User Agent module.
54use LWP::UserAgent;
55
56# Load function from posix module to format time strings.
57use POSIX qw (strftime);
58
59# Load module to talk to the kernel log daemon.
60use Sys::Syslog qw(:DEFAULT setlogsock);
61
02844177 62# Location where all config and settings files are stored.
164eab66 63our $settingsdir = "${General::swroot}/suricata";
02844177 64
ddaf8ae1
SS
65# File where the main file for providers ruleset inclusion exists.
66our $suricata_used_providers_file = "$settingsdir/suricata-used-providers.yaml";
67
88eb5626 68# File for static ruleset inclusions.
43d12991 69our $suricata_default_rulefiles_file = "$settingsdir/suricata-default-rules.yaml";
88eb5626 70
b02e30fd
SS
71# File where the addresses of the homenet are stored.
72our $homenet_file = "$settingsdir/suricata-homenet.yaml";
73
30ee98e9
SS
74# File where the addresses of the used DNS servers are stored.
75our $dns_servers_file = "$settingsdir/suricata-dns-servers.yaml";
76
e698090e
SS
77# File where the HTTP ports definition is stored.
78our $http_ports_file = "$settingsdir/suricata-http-ports.yaml";
79
15832b10
SS
80# File which contains includes for provider specific rule modifications.
81our $oinkmaster_provider_includes_file = "$settingsdir/oinkmaster-provider-includes.conf";
82
b02e30fd
SS
83# File which contains wheater the rules should be changed.
84our $modify_sids_file = "$settingsdir/oinkmaster-modify-sids.conf";
85
86# File which stores the configured IPS settings.
87our $ids_settings_file = "$settingsdir/settings";
88
a8d36d3e
SS
89# File which stores the used and configured ruleset providers.
90our $providers_settings_file = "$settingsdir/providers-settings";
91
b02e30fd
SS
92# File which stores the configured settings for whitelisted addresses.
93our $ignored_file = "$settingsdir/ignored";
94
788a71f5
SS
95# Location where the downloaded rulesets are stored.
96our $dl_rules_path = "/var/tmp";
97
3983aebd 98# File to store any errors, which also will be read and displayed by the wui.
77910792 99our $storederrorfile = "/tmp/ids_storederror";
3983aebd 100
8076deba
SS
101# File to lock the WUI, while the autoupdate script runs.
102our $ids_page_lock_file = "/tmp/ids_page_locked";
103
298ef5ba 104# Location where the rulefiles are stored.
21cab141 105our $rulespath = "/var/lib/suricata";
298ef5ba 106
43d12991
SS
107# Location where the default rulefils are stored.
108our $default_rulespath = "/usr/share/suricata/rules";
109
6983a96e
SS
110# Location where the addition config files are stored.
111our $configspath = "/usr/share/suricata";
112
23b56052 113# Location of the classification file.
6983a96e 114our $classification_file = "$configspath/classification.config";
23b56052 115
8bd74e12
SS
116# Location of the sid to msg mappings file.
117our $sid_msg_file = "$rulespath/sid-msg.map";
118
612bb2df
SS
119# Location to store local rules. This file will not be touched.
120our $local_rules_file = "$rulespath/local.rules";
121
b02e30fd
SS
122# File which contains the rules to whitelist addresses on suricata.
123our $whitelist_file = "$rulespath/whitelist.rules";
124
bce84f39
SS
125# File which contains a list of all supported ruleset sources.
126# (Sourcefire, Emergingthreads, etc..)
127our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
128
796eea21
SS
129# The pidfile of the IDS.
130our $idspidfile = "/var/run/suricata.pid";
131
5240a809
SS
132# Location of suricatactrl.
133my $suricatactrl = "/usr/local/bin/suricatactrl";
134
788a71f5
SS
135# Prefix for each downloaded ruleset.
136my $dl_rulesfile_prefix = "idsrules";
137
0fbfffea
SS
138# Temporary directory where the rulesets will be extracted.
139my $tmp_directory = "/tmp/ids_tmp";
140
6c9d3eee
SS
141# Temporary directory where the extracted rules files will be stored.
142my $tmp_rules_directory = "$tmp_directory/rules";
143
144# Temporary directory where the extracted additional config files will be stored.
145my $tmp_conf_directory = "$tmp_directory/conf";
146
5240a809 147# Array with allowed commands of suricatactrl.
ed06bc81
SS
148my @suricatactrl_cmds = ( 'start', 'stop', 'restart', 'reload', 'fix-rules-dir', 'cron' );
149
150# Array with supported cron intervals.
151my @cron_intervals = ('off', 'daily', 'weekly' );
5240a809 152
e698090e
SS
153# Array which contains the HTTP ports, which statically will be declared as HTTP_PORTS in the
154# http_ports_file.
155my @http_ports = ('80', '81');
156
88eb5626 157# Array which contains a list of rulefiles which always will be included if they exist.
bb39fac4
SS
158my @static_included_rulefiles = ('local.rules', 'whitelist.rules');
159
160# Array which contains a list of allways enabled application layer protocols.
161my @static_enabled_app_layer_protos = ('app-layer', 'decoder', 'files', 'stream');
88eb5626 162
788a71f5
SS
163# Hash which allows to convert the download type (dl_type) to a file suffix.
164my %dl_type_to_suffix = (
165 "archive" => ".tar.gz",
166 "plain" => ".rules",
167);
168
43d12991
SS
169# Hash to translate an application layer protocol to the application name.
170my %tr_app_layer_proto = (
171 "ikev2" => "ipsec",
172 "krb5" => "kerberos",
173);
174
b02e30fd
SS
175#
176## Function to check and create all IDS related files, if the does not exist.
177#
178sub check_and_create_filelayout() {
179 # Check if the files exist and if not, create them.
15832b10 180 unless (-f "$oinkmaster_provider_includes_file") { &create_empty_file($oinkmaster_provider_includes_file); }
b02e30fd 181 unless (-f "$modify_sids_file") { &create_empty_file($modify_sids_file); }
ddaf8ae1 182 unless (-f "$suricata_used_providers_file") { &create_empty_file($suricata_used_providers_file); }
2f62fd00 183 unless (-f "$suricata_default_rulefiles_file") { &create_empty_file($suricata_default_rulefiles_file); }
b02e30fd 184 unless (-f "$ids_settings_file") { &create_empty_file($ids_settings_file); }
a8d36d3e 185 unless (-f "$providers_settings_file") { &create_empty_file($providers_settings_file); }
b02e30fd
SS
186 unless (-f "$whitelist_file" ) { &create_empty_file($whitelist_file); }
187}
188
70cc1315
SS
189#
190## Function to get a list of all available ruleset providers.
191##
192## They will be returned as a sorted array.
193#
194sub get_ruleset_providers() {
195 my @providers;
196
197 # Loop through the hash of providers.
198 foreach my $provider ( keys %IDS::Ruleset::Providers ) {
199 # Add the provider to the array.
200 push(@providers, $provider);
201 }
202
203 # Sort and return the array.
204 return sort(@providers);
205}
206
5e20d6cb
SS
207#
208## Function to get a list of all enabled ruleset providers.
209##
210## They will be returned as an array.
211#
212sub get_enabled_providers () {
213 my %used_providers = ();
214
215 # Array to store the enabled providers.
216 my @enabled_providers = ();
217
218 # Read-in the providers config file.
219 &General::readhasharray("$providers_settings_file", \%used_providers);
220
221 # Loop through the hash of used_providers.
222 foreach my $id (keys %used_providers) {
223 # Skip disabled providers.
224 next unless ($used_providers{$id}[3] eq "enabled");
225
226 # Grab the provider handle.
227 my $provider = "$used_providers{$id}[0]";
228
229 # Add the provider to the array of enabled providers.
230 push(@enabled_providers, $provider);
231 }
232
233 # Return the array.
234 return @enabled_providers;
235}
236
e0eb5bc7
SS
237#
238## Function to get a hash of provider handles and their configured modes (IDS/IPS).
239#
240sub get_providers_mode () {
241 my %used_providers = ();
242
243 # Hash to store the providers and their configured modes.
244 my %providers_mode = ();
245
246 # Read-in the providers config file.
247 &General::readhasharray("$providers_settings_file", \%used_providers);
248
249 # Loop through the hash of used_providers.
250 foreach my $id (keys %used_providers) {
251 # Skip disabled providers.
252 next unless ($used_providers{$id}[3] eq "enabled");
253
254 # Grab the provider handle.
255 my $provider = "$used_providers{$id}[0]";
256
257 # Grab the provider mode.
258 my $mode = "$used_providers{$id}[4]";
259
260 # Fall back to IDS if no mode could be obtained.
261 unless($mode) {
262 $mode = "IDS";
263 }
264
265 # Add details to provider_modes hash.
266 $providers_mode{$provider} = $mode;
267 }
268
269 # Return the hash.
270 return %providers_mode;
271}
272
8dcebe53
SS
273#
274## Function for checking if at least 300MB of free disk space are available
275## on the "/var" partition.
276#
277sub checkdiskspace () {
278 # Call diskfree to gather the free disk space of /var.
81631920 279 my @df = &General::system_output("/bin/df", "-B", "M", "/var");
8dcebe53
SS
280
281 # Loop through the output.
282 foreach my $line (@df) {
283 # Ignore header line.
284 next if $line =~ m/^Filesystem/;
285
286 # Search for a line with the device information.
287 if ($line =~ m/dev/ ) {
288 # Split the line into single pieces.
289 my @values = split(' ', $line);
290 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
291
292 # Check if the available disk space is more than 300MB.
293 if ($available < 300) {
65e3aef5
SS
294 # Exit function and return the available disk space.
295 return $available;
8dcebe53
SS
296 }
297 }
298 }
299
300 # Everything okay, return nothing.
301 return;
302}
303
eea2670b 304#
0f2c5211 305## This function is responsible for downloading the ruleset for a given provider.
eea2670b 306##
0f2c5211 307## * At first it initialize the downloader and sets an upstream proxy if configured.
b3c2c336 308## * The next step will be to generate the final download url, by obtaining the URL for the desired
0f2c5211
SS
309## ruleset and add the settings for the upstream proxy.
310## * Finally the function will grab the rule file or tarball from the server.
311## It tries to reduce the amount of download by using the "If-Modified-Since" HTTP header.
eea2670b 312#
e26edcc1
SS
313## Return codes:
314##
315## * "no url" - If no download URL could be gathered for the provider.
316## * "not modified" - In case the already stored rules file is up to date.
317## * "incomplete download" - When the remote file size differs from the downloaded file size.
318## * "$error" - The error message generated from the LWP::User Agent module.
319#
b3c2c336
SS
320sub downloadruleset ($) {
321 my ($provider) = @_;
322
e71804fb
SS
323 # The amount of download attempts before giving up and
324 # logging an error.
325 my $max_dl_attempts = 3;
326
eea2670b
SS
327 # Read proxysettings.
328 my %proxysettings=();
329 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
330
eea2670b 331 # Init the download module.
954dbdad
SS
332 #
333 # Request SSL hostname verification and specify path
334 # to the CA file.
335 my $downloader = LWP::UserAgent->new(
336 ssl_opts => {
337 SSL_ca_file => '/etc/ssl/cert.pem',
338 verify_hostname => 1,
339 }
340 );
eea2670b
SS
341
342 # Set timeout to 10 seconds.
343 $downloader->timeout(10);
344
345 # Check if an upstream proxy is configured.
346 if ($proxysettings{'UPSTREAM_PROXY'}) {
eea2670b
SS
347 my $proxy_url;
348
40407aee 349 $proxy_url = "http://";
eea2670b 350
40407aee
PM
351 # Check if the proxy requires authentication.
352 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
353 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
eea2670b
SS
354 }
355
40407aee
PM
356 # Add proxy server address and port.
357 $proxy_url .= $proxysettings{'UPSTREAM_PROXY'};
358
eea2670b 359 # Setup proxy settings.
6994f001 360 $downloader->proxy(['http', 'https'], $proxy_url);
eea2670b
SS
361 }
362
0f2c5211
SS
363 # Grab the download url for the provider.
364 my $url = $IDS::Ruleset::Providers{$provider}{'dl_url'};
b3c2c336 365
0f2c5211
SS
366 # Check if the provider requires a subscription.
367 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
368 # Grab the subscription code.
369 my $subscription_code = &get_subscription_code($provider);
b3c2c336 370
0f2c5211
SS
371 # Add the subscription code to the download url.
372 $url =~ s/\<subscription_code\>/$subscription_code/g;
b3c2c336 373
0f2c5211 374 }
f264adda 375
52a557a8
SS
376 # Abort and return "no url", if no url could be determined for the provider.
377 return "no url" unless ($url);
96da5803 378
0f2c5211
SS
379 # Pass the requested URL to the downloader.
380 my $request = HTTP::Request->new(GET => $url);
061391e7 381
0f2c5211
SS
382 # Generate temporary file name, located in "/var/tmp" and with a suffix of ".tmp".
383 # The downloaded file will be stored there until some sanity checks are performed.
384 my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "/var/tmp/", UNLINK => 0 );
385 my $tmpfile = $tmp->filename();
061391e7 386
0f2c5211
SS
387 # Call function to get the final path and filename for the downloaded file.
388 my $dl_rulesfile = &_get_dl_rulesfile($provider);
061391e7 389
0f2c5211
SS
390 # Check if the rulesfile already exits, because it has been downloaded in the past.
391 #
392 # In this case we are requesting the server if the remote file has been changed or not.
393 # This will be done by sending the modification time in a special HTTP header.
394 if (-f $dl_rulesfile) {
395 # Call stat on the file.
396 my $stat = stat($dl_rulesfile);
061391e7 397
0f2c5211
SS
398 # Omit the mtime of the existing file.
399 my $mtime = $stat->mtime;
061391e7 400
0f2c5211
SS
401 # Convert the timestamp into right format.
402 my $http_date = time2str($mtime);
96da5803 403
0f2c5211
SS
404 # Add the If-Modified-Since header to the request to ask the server if the
405 # file has been modified.
406 $request->header( 'If-Modified-Since' => "$http_date" );
407 }
96da5803 408
0f2c5211
SS
409 my $dl_attempt = 1;
410 my $response;
155b3b56 411
0f2c5211
SS
412 # Download and retry on failure.
413 while ($dl_attempt <= $max_dl_attempts) {
414 # Perform the request and save the output into the tmpfile.
415 $response = $downloader->request($request, $tmpfile);
061391e7 416
0f2c5211
SS
417 # Check if the download was successfull.
418 if($response->is_success) {
419 # Break loop.
420 last;
061391e7 421
0f2c5211
SS
422 # Check if the server responds with 304 (Not Modified).
423 } elsif ($response->code == 304) {
e26edcc1
SS
424 # Return "not modified".
425 return "not modified";
e71804fb 426
0f2c5211
SS
427 # Check if we ran out of download re-tries.
428 } elsif ($dl_attempt eq $max_dl_attempts) {
429 # Obtain error.
430 my $error = $response->content;
e71804fb 431
e26edcc1
SS
432 # Return the error message from response..
433 return "$error";
155b3b56 434 }
96da5803 435
0f2c5211
SS
436 # Remove temporary file, if one exists.
437 unlink("$tmpfile") if (-e "$tmpfile");
de1199e2 438
0f2c5211
SS
439 # Increase download attempt counter.
440 $dl_attempt++;
441 }
061391e7 442
0f2c5211
SS
443 # Obtain the connection headers.
444 my $headers = $response->headers;
de1199e2 445
0f2c5211
SS
446 # Get the timestamp from header, when the file has been modified the
447 # last time.
448 my $last_modified = $headers->last_modified;
96da5803 449
0f2c5211
SS
450 # Get the remote size of the downloaded file.
451 my $remote_filesize = $headers->content_length;
25b6545a 452
0f2c5211
SS
453 # Perform stat on the tmpfile.
454 my $stat = stat($tmpfile);
eea2670b 455
0f2c5211
SS
456 # Grab the local filesize of the downloaded tarball.
457 my $local_filesize = $stat->size;
88daf7eb 458
0f2c5211
SS
459 # Check if both file sizes match.
460 if (($remote_filesize) && ($remote_filesize ne $local_filesize)) {
0f2c5211
SS
461 # Delete temporary file.
462 unlink("$tmpfile");
96da5803 463
0f2c5211 464 # Return "1" - false.
e26edcc1 465 return "incomplete download";
0f2c5211 466 }
96da5803 467
0f2c5211
SS
468 # Overwrite the may existing rulefile or tarball with the downloaded one.
469 move("$tmpfile", "$dl_rulesfile");
ae226132 470
0f2c5211
SS
471 # Check if we got a last-modified value from the server.
472 if ($last_modified) {
473 # Assign the last-modified timestamp as mtime to the
474 # rules file.
475 utime(time(), "$last_modified", "$dl_rulesfile");
96da5803
SS
476 }
477
0f2c5211
SS
478 # Delete temporary file.
479 unlink("$tmpfile");
480
481 # Set correct ownership for the tarball.
482 set_ownership("$dl_rulesfile");
483
eea2670b
SS
484 # If we got here, everything worked fine. Return nothing.
485 return;
486}
8dcebe53 487
0fbfffea
SS
488#
489## Function to extract a given ruleset.
1b5aec1b
SS
490##
491## In case the ruleset provider offers a plain file, it simply will
492## be copied.
0fbfffea
SS
493#
494sub extractruleset ($) {
495 my ($provider) = @_;
496
8353e28a
SS
497 # Disable chown functionality when uncompressing files.
498 $Archive::Tar::CHOWN = "0";
499
0fbfffea
SS
500 # Get full path and downloaded rulesfile for the given provider.
501 my $tarball = &_get_dl_rulesfile($provider);
502
503 # Check if the file exists.
504 unless (-f $tarball) {
1b5aec1b 505 &_log_to_syslog("Could not find ruleset file: $tarball");
0fbfffea
SS
506
507 # Return nothing.
508 return;
509 }
510
0fbfffea
SS
511 # Check if the temporary directories exist, otherwise create them.
512 mkdir("$tmp_directory") unless (-d "$tmp_directory");
6c9d3eee
SS
513 mkdir("$tmp_rules_directory") unless (-d "$tmp_rules_directory");
514 mkdir("$tmp_conf_directory") unless (-d "$tmp_conf_directory");
0fbfffea 515
1b5aec1b
SS
516 # Omit the type (dl_type) of the stored ruleset.
517 my $type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
0fbfffea 518
1b5aec1b
SS
519 # Handle the different ruleset types.
520 if ($type eq "plain") {
521 # Generate destination filename an full path.
522 my $destination = "$tmp_rules_directory/$provider\-ruleset.rules";
0fbfffea 523
1b5aec1b
SS
524 # Copy the file into the temporary rules directory.
525 copy($tarball, $destination);
0fbfffea 526
1b5aec1b
SS
527 } elsif ( $type eq "archive") {
528 # Initialize the tar module.
529 my $tar = Archive::Tar->new($tarball);
0fbfffea 530
1b5aec1b
SS
531 # Get the filelist inside the tarball.
532 my @packed_files = $tar->list_files;
0fbfffea 533
1b5aec1b
SS
534 # Loop through the filelist.
535 foreach my $packed_file (@packed_files) {
536 my $destination;
0fbfffea 537
1b5aec1b
SS
538 # Splitt the packed file into chunks.
539 my $file = fileparse($packed_file);
0fbfffea 540
1b5aec1b
SS
541 # Handle msg-id.map file.
542 if ("$file" eq "sid-msg.map") {
543 # Set extract destination to temporary config_dir.
544 $destination = "$tmp_conf_directory/$provider\-sid-msg.map";
0fbfffea 545
1b5aec1b
SS
546 # Handle classification.conf
547 } elsif ("$file" eq "classification.config") {
548 # Set extract destination to temporary config_dir.
549 $destination = "$tmp_conf_directory/$provider\-classification.config";
0fbfffea 550
1b5aec1b
SS
551 # Handle rules files.
552 } elsif ($file =~ m/\.rules$/) {
f349c960
SS
553 # Skip rule files which are not located in the rules directory or archive root.
554 next unless(($packed_file =~ /^rules\//) || ($packed_file !~ /\//));
555
10beae64
SS
556 # Skip deleted.rules.
557 #
558 # Mostly they have been taken out for correctness or performance reasons and therfore
559 # it is not a great idea to enable any of them.
560 next if($file =~ m/deleted.rules$/);
561
1b5aec1b 562 my $rulesfilename;
0fbfffea 563
1b5aec1b
SS
564 # Splitt the filename into chunks.
565 my @filename = split("-", $file);
0fbfffea 566
1b5aec1b
SS
567 # Reverse the array.
568 @filename = reverse(@filename);
0fbfffea 569
1b5aec1b
SS
570 # Get the amount of elements in the array.
571 my $elements = @filename;
0fbfffea 572
1b5aec1b
SS
573 # Remove last element of the hash.
574 # It contains the vendor name, which will be replaced.
575 if ($elements >= 3) {
576 # Remove last element from hash.
577 pop(@filename);
578 }
579
580 # Check if the last element of the filename does not
581 # contain the providers name.
582 if ($filename[-1] ne "$provider") {
583 # Add provider name as last element.
584 push(@filename, $provider);
585 }
586
587 # Reverse the array back.
588 @filename = reverse(@filename);
589
590 # Generate the name for the rulesfile.
591 $rulesfilename = join("-", @filename);
592
593 # Set extract destination to temporaray rules_dir.
594 $destination = "$tmp_rules_directory/$rulesfilename";
595 } else {
596 # Skip all other files.
597 next;
598 }
599
2b12a010
SS
600 # Check if the destination file exists.
601 unless(-e "$destination") {
602 # Extract the file to the temporary directory.
603 $tar->extract_file("$packed_file", "$destination");
604 } else {
2b12a010
SS
605 # Generate temporary file name, located in the temporary rules directory and a suffix of ".tmp".
606 my $tmp = File::Temp->new( SUFFIX => ".tmp", DIR => "$tmp_rules_directory", UNLINK => 0 );
607 my $tmpfile = $tmp->filename();
608
609 # Extract the file to the new temporary file name.
610 $tar->extract_file("$packed_file", "$tmpfile");
611
612 # Open the the existing file.
613 open(DESTFILE, ">>", "$destination") or die "Could not open $destination. $!\n";
614 open(TMPFILE, "<", "$tmpfile") or die "Could not open $tmpfile. $!\n";
615
616 # Loop through the content of the temporary file.
617 while (<TMPFILE>) {
618 # Append the content line by line to the destination file.
619 print DESTFILE "$_";
620 }
621
622 # Close the file handles.
623 close(TMPFILE);
624 close(DESTFILE);
625
626 # Remove the temporary file.
627 unlink("$tmpfile");
628 }
1b5aec1b 629 }
0fbfffea
SS
630 }
631}
632
25f5cb0d 633#
b953677b
SS
634## A wrapper function to call the oinkmaster script, setup the rules structues and
635## call the functions to merge the additional config files. (classification, sid-msg, etc.).
25f5cb0d
SS
636#
637sub oinkmaster () {
330759d8
SS
638 # Check if the files in rulesdir have the correct permissions.
639 &_check_rulesdir_permissions();
640
b953677b 641 # Cleanup the rules directory before filling it with the new rulests.
883820bd
SS
642 &_cleanup_rulesdir();
643
0130e0d1 644 # Get all enabled providers.
e31458de 645 my @enabled_providers = &get_enabled_providers();
b953677b 646
0130e0d1
SS
647 # Loop through the array of enabled providers.
648 foreach my $provider (@enabled_providers) {
1b5aec1b
SS
649 # Call the extractruleset function.
650 &extractruleset($provider);
b953677b
SS
651 }
652
0e40e1e7
SS
653 # Establish the connection to the syslog service.
654 openlog('oinkmaster', 'cons,pid', 'user');
655
25f5cb0d 656 # Call oinkmaster to generate ruleset.
b953677b 657 open(OINKMASTER, "/usr/local/bin/oinkmaster.pl -s -u dir://$tmp_rules_directory -C $settingsdir/oinkmaster.conf -o $rulespath 2>&1 |") or die "Could not execute oinkmaster $!\n";
0e40e1e7
SS
658
659 # Log output of oinkmaster to syslog.
660 while(<OINKMASTER>) {
661 # The syslog function works best with an array based input,
662 # so generate one before passing the message details to syslog.
663 my @syslog = ("INFO", "$_");
664
665 # Send the log message.
666 syslog(@syslog);
667 }
668
669 # Close the pipe to oinkmaster process.
670 close(OINKMASTER);
671
672 # Close the log handle.
673 closelog();
b953677b 674
b953677b
SS
675 # Call function to merge the classification files.
676 &merge_classifications(@enabled_providers);
677
678 # Call function to merge the sid to message mapping files.
679 &merge_sid_msg(@enabled_providers);
680
681 # Cleanup temporary directory.
16b2d281 682 &cleanup_tmp_directory();
25f5cb0d
SS
683}
684
5a6c7bbe
SS
685#
686## Function to alter the ruleset.
687#
688sub process_ruleset(@) {
689 my (@providers) = @_;
690
691 # Hash to store the configured provider modes.
692 my %providers_mode = &get_providers_mode();
693
694 # Array to store the extracted rulefile from the temporary rules directory.
695 my @extracted_rulefiles;
696
697 # Get names of the extracted raw rulefiles.
698 opendir(DIR, $tmp_rules_directory) or die "Could not read from $tmp_rules_directory. $!\n";
699 while (my $file = readdir(DIR)) {
700 # Ignore single and double dotted files.
701 next if $file =~ /^\.\.?$/;
702
703 # Add file to the array of extracted files.
704 push(@extracted_rulefiles, $file);
705 }
706
707 # Close directory handle.
708 closedir(DIR);
709
710 # Loop through the array of providers.
711 foreach my $provider (@providers) {
712 # Hash to store the obtained SIDs and REV of each provider.
713 my %rules = ();
714
715 # Hash which holds modifications to apply to the rules.
716 my %modifications = ();
717
718 # Loop through the array of extraced rulefiles.
719 foreach my $file (@extracted_rulefiles) {
720 # Skip file if it does not belong to the current processed provider.
721 next unless ($file =~ m/^$provider/);
722
723 # Open the rulefile.
724 open(FILE, "$tmp_rules_directory/$file") or die "Could not read $tmp_rules_directory/$file. $!\n";
725
726 # Loop through the file content.
727 while (my $line = <FILE>) {
728 # Skip blank lines.
729 next if ($line =~ /^\s*$/);
730
731 # Call function to get the sid and rev of the rule.
732 my ($sid, $rev) = &_get_sid_and_rev($line);
733
734 # Skip rule if a sid with a higher rev already has added to the rules hash.
735 next if ($rev le $rules{$sid});
736
737 # Add the new or rule with higher rev to the hash of rules.
738 $rules{$sid} = $rev;
739 }
740
741 # Close file handle.
742 close(FILE);
743 }
744
745 # Get filename which contains the ruleset modifications for this provider.
746 my $modification_file = &get_provider_ruleset_modifications_file($provider);
747
748 # Read file which holds the modifications of the ruleset for the current provider.
749 &General::readhash($modification_file, \%modifications) if (-f $modification_file);
750
751 # Loop again through the array of extracted rulesfiles.
752 foreach my $file (@extracted_rulefiles) {
753 # Skip the file if it does not belong to the current provider.
754 next unless ($file =~ m/^$provider/);
755
756 # Open the rulefile for writing.
757 open(RULEFILE, ">", "$rulespath/$file") or die "Could not write to file $rulespath/$file. $!\n";
758
759 # Open the rulefile for reading.
760 open(TMP_RULEFILE, "$tmp_rules_directory/$file") or die "Could not read $tmp_rules_directory/$file. $!\n";
761
762 # Loop through the raw temporary rulefile.
763 while (my $line = <TMP_RULEFILE>) {
764 # Get the sid and rev of the rule.
765 my ($sid, $rev) = &_get_sid_and_rev($line);
766
767 # Check if the current rule is obsoleted by a newer one.
768 #
769 # In this case the rev number in the rules hash is higher than the current one.
770 next if ($rev lt $rules{$sid});
771
772 # Check if the rule should be enabled or disabled.
773 if ($modifications{$sid} eq "on") {
774 # Drop the # at the start of the line.
775 $line =~ s/^\#//;
776 } elsif ($modifications{$sid} eq "off") {
777 # Add a # at the start of the line to disable the rule.
778 $line = "#$line" unless ($line =~ /^#/);
779 }
780
781 # Check if the Provider is set so IPS mode.
782 if ($providers_mode{$provider} eq "IPS") {
783 # Replacements for sourcefire rules.
784 $line =~ s/^#\s*(?:alert|drop)(.+policy balanced-ips alert)/alert${1}/;
785 $line =~ s/^#\s*(?:alert|drop)(.+policy balanced-ips drop)/drop${1}/;
786
787 # Replacements for generic rules.
788 $line =~ s/^(#?)\s*(?:alert|drop)/${1}drop/;
789 $line =~ s/^(#?)\s*drop(.+flowbits:noalert;)/${1}alert${2}/;
790 }
791
792 # Write line / rule to the target rule file.
793 print RULEFILE "$line";
794 }
795
796 # Close filehandles.
797 close(RULEFILE);
798 close(TMP_RULEFILE);
799 }
800 }
801}
802
23b56052
SS
803#
804## Function to merge the classifications for a given amount of providers and write them
805## to the classifications file.
806#
807sub merge_classifications(@) {
808 my @providers = @_;
809
810 # Hash to store all collected classifications.
811 my %classifications = ();
812
813 # Loop through the given array of providers.
814 foreach my $provider (@providers) {
815 # Generate full path to classification file.
6c9d3eee 816 my $classification_file = "$tmp_conf_directory/$provider\-classification.config";
23b56052
SS
817
818 # Skip provider if no classification file exists.
819 next unless (-f "$classification_file");
820
821 # Open the classification file.
822 open(CLASSIFICATION, $classification_file) or die "Could not open file $classification_file. $!\n";
823
824 # Loop through the file content.
825 while(<CLASSIFICATION>) {
826 # Parse the file and grab the classification details.
827 if ($_ =~/.*config classification\: (.*)/) {
828 # Split the grabbed details.
829 my ($short_name, $short_desc, $priority) = split("\,", $1);
830
831 # Check if the grabbed classification is allready known and the priority value is greater
832 # than the stored one (which causes less priority in the IDS).
833 if (($classifications{$short_name}) && ($classifications{$short_name}[1] >= $priority)) {
834 #Change the priority value to the stricter one.
835 $classifications{$short_name} = [ "$classifications{$short_name}[0]", "$priority" ];
836 } else {
837 # Add the classification to the hash.
838 $classifications{$short_name} = [ "$short_desc", "$priority" ];
839 }
840 }
841 }
842
843 # Close the file.
844 close(CLASSIFICATION);
845 }
846
847 # Open classification file for writing.
848 open(FILE, ">", "$classification_file") or die "Could not write to $classification_file. $!\n";
849
850 # Print notice about autogenerated file.
851 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n\n";
852
853 # Sort and loop through the hash of classifications.
854 foreach my $key (sort keys %classifications) {
855 # Assign some nice variable names for the items.
856 my $short_name = $key;
857 my $short_desc = $classifications{$key}[0];
858 my $priority = $classifications{$key}[1];
859
860 # Write the classification to the file.
861 print FILE "config classification: $short_name,$short_desc,$priority\n";
862 }
863
864 # Close file handle.
865 close(FILE);
866}
867
8bd74e12
SS
868#
869## Function to merge the "sid to message mapping" files of various given providers.
870#
871sub merge_sid_msg (@) {
872 my @providers = @_;
873
874 # Hash which contains all the sid to message mappings.
875 my %mappings = ();
876
877 # Loop through the array of given providers.
878 foreach my $provider (@providers) {
879 # Generate full path and filename.
6c9d3eee 880 my $sid_msg_file = "$tmp_conf_directory/$provider\-sid-msg.map";
8bd74e12
SS
881
882 # Skip provider if no sid to msg mapping file for this provider exists.
883 next unless (-f $sid_msg_file);
884
885 # Open the file.
886 open(MAPPING, $sid_msg_file) or die "Could not open $sid_msg_file. $!\n";
887
888 # Loop through the file content.
889 while (<MAPPING>) {
890 # Remove newlines.
891 chomp($_);
892
893 # Skip lines which do not start with a number,
894 next unless ($_ =~ /^\d+/);
895
896 # Split line content and assign it to an array.
897 my @line = split(/ \|\| /, $_);
898
899 # Grab the first element (and remove it) from the line array.
900 # It contains the sid.
901 my $sid = shift(@line);
902
903 # Store the grabbed sid and the remain array as hash value.
904 # It still contains the messages, references etc.
905 $mappings{$sid} = [@line];
906 }
907
908 # Close file handle.
909 close(MAPPING);
910 }
911
912 # Open mappings file for writing.
913 open(FILE, ">", $sid_msg_file) or die "Could not write $sid_msg_file. $!\n";
914
915 # Write notice about autogenerated file.
916 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n\n";
917
918 # Loop through the hash of mappings.
919 foreach my $sid ( sort keys %mappings) {
920 # Grab data for the sid.
921 my @data = @{$mappings{$sid}};
922
923 # Add the sid to the data array.
924 unshift(@data, $sid);
925
926 # Generate line.
927 my $line = join(" \|\| ", @data);
928
929 print FILE "$line\n";
930
931 }
932
933 # Close file handle.
934 close(FILE);
935}
936
50f348f6
SS
937#
938## A very tiny function to move an extracted ruleset from the temporary directory into
939## the rules directory.
940#
941sub move_tmp_ruleset() {
50f348f6
SS
942 # Do a directory listing of the temporary directory.
943 opendir DH, $tmp_rules_directory;
944
945 # Loop over all files.
946 while(my $file = readdir DH) {
947 # Move them to the rules directory.
948 move "$tmp_rules_directory/$file" , "$rulespath/$file";
949 }
950
951 # Close directory handle.
952 closedir DH;
953}
954
16b2d281
SS
955#
956## Function to cleanup the temporary IDS directroy.
957#
958sub cleanup_tmp_directory () {
16b2d281
SS
959
960 # Delete temporary directory and all containing files.
961 rmtree([ "$tmp_directory" ]);
962}
963
3983aebd
SS
964#
965## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
966#
967sub log_error ($) {
968 my ($error) = @_;
969
970 # Remove any newline.
971 chomp($error);
972
eb5592c1
SS
973 # Call private function to log the error message to syslog.
974 &_log_to_syslog($error);
975
3983aebd
SS
976 # Call private function to write/store the error message in the storederrorfile.
977 &_store_error_message($error);
978}
979
eb5592c1
SS
980#
981## Function to log a given error message to the kernel syslog.
982#
983sub _log_to_syslog ($) {
984 my ($message) = @_;
985
eb5592c1
SS
986 # The syslog function works best with an array based input,
987 # so generate one before passing the message details to syslog.
988 my @syslog = ("ERR", "<ERROR> $message");
989
990 # Establish the connection to the syslog service.
991 openlog('oinkmaster', 'cons,pid', 'user');
992
993 # Send the log message.
994 syslog(@syslog);
995
996 # Close the log handle.
997 closelog();
998}
999
3983aebd
SS
1000#
1001## Private function to write a given error message to the storederror file.
1002#
1003sub _store_error_message ($) {
1004 my ($message) = @_;
1005
1006 # Remove any newline.
1007 chomp($message);
1008
1009 # Open file for writing.
1010 open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
1011
1012 # Write error to file.
1013 print ERRORFILE "$message\n";
1014
1015 # Close file.
1016 close (ERRORFILE);
3c59b1fa
SS
1017
1018 # Set correct ownership for the file.
1019 &set_ownership("$storederrorfile");
3983aebd
SS
1020}
1021
788a71f5
SS
1022#
1023## Private function to get the path and filename for a downloaded ruleset by a given provider.
1024#
1025sub _get_dl_rulesfile($) {
1026 my ($provider) = @_;
1027
1028 # Gather the download type for the given provider.
1029 my $dl_type = $IDS::Ruleset::Providers{$provider}{'dl_type'};
1030
1031 # Obtain the file suffix for the download file type.
1032 my $suffix = $dl_type_to_suffix{$dl_type};
1033
1034 # Check if a suffix has been found.
1035 unless ($suffix) {
1036 # Abort return - nothing.
1037 return;
1038 }
1039
1040 # Generate the full filename and path for the stored rules file.
1041 my $rulesfile = "$dl_rules_path/$dl_rulesfile_prefix-$provider$suffix";
1042
1043 # Return the generated filename.
1044 return $rulesfile;
1045}
1046
e246285a
SS
1047#
1048## Private function to obtain the sid and rev of a rule.
1049#
1050## Returns an array with the sid as first and the rev as second value.
1051#
1052sub _get_sid_and_rev ($) {
1053 my ($line) = @_;
1054
1055 my @ret;
1056
1057 # Use regex to obtain the sid and rev.
1058 if ($line =~ m/.*sid:\s*(.*?);.*rev:\s*(.*?);/) {
1059 # Add the sid and rev to the array.
1060 push(@ret, $1);
1061 push(@ret, $2);
1062 }
1063
1064 # Return the array.
1065 return @ret;
1066}
1067
61b92664
SS
1068#
1069## Tiny function to delete the stored ruleset file or tarball for a given provider.
1070#
1071sub drop_dl_rulesfile ($) {
1072 my ($provider) = @_;
1073
1074 # Gather the full path and name of the stored rulesfile.
1075 my $rulesfile = &_get_dl_rulesfile($provider);
1076
1077 # Check if the given rulesfile exists.
1078 if (-f $rulesfile) {
1079 # Delete the stored rulesfile.
1080 unlink($rulesfile) or die "Could not delete $rulesfile. $!\n";
1081 }
1082}
1083
5b9d877d
SS
1084#
1085## Tiny function to get/generate the full path and filename for the providers oinkmaster
1086## modified sids file.
1087#
1088sub get_oinkmaster_provider_modified_sids_file ($) {
1089 my ($provider) = @_;
1090
1091 # Generate the filename.
1092 my $filename = "$settingsdir/oinkmaster-$provider-modified-sids.conf";
1093
1094 # Return the filename.
1095 return $filename;
1096}
1097
d878d9c0
SS
1098#
1099## Function to directly altering the oinkmaster provider includes file.
1100##
1101## Requires tha acition "remove" or "add" and a provider handle.
1102#
1103sub alter_oinkmaster_provider_includes_file ($$) {
1104 my ($action, $provider) = @_;
1105
1106 # Call function to get the path and name for the given providers
1107 # oinkmaster modified sids file.
1108 my $provider_modified_sids_file = &get_oinkmaster_provider_modified_sids_file($provider);
1109
1110 # Open the file for reading..
1111 open (FILE, $oinkmaster_provider_includes_file) or die "Could not read $oinkmaster_provider_includes_file. $!\n";
1112
1113 # Read-in file content.
1114 my @lines = <FILE>;
1115
1116 # Close file after reading.
1117 close(FILE);
1118
1119 # Re-open the file for writing.
1120 open(FILE, ">", $oinkmaster_provider_includes_file) or die "Could not write to $oinkmaster_provider_includes_file. $!\n";
1121
1122 # Loop through the file content.
1123 foreach my $line (@lines) {
1124 # Remove newlines.
1125 chomp($line);
1126
1127 # Skip line if we found our given provider and the action should be remove.
1128 next if (($line =~ /$provider/) && ($action eq "remove"));
1129
1130 # Write the read-in line back to the file.
1131 print FILE "$line\n";
1132 }
1133
1d860d89
SS
1134 # Check if the file exists and add the provider if requested.
1135 if ((-f $provider_modified_sids_file) && ($action eq "add")) {
d878d9c0
SS
1136 print FILE "include $provider_modified_sids_file\n";
1137 }
1138
1139 # Close file handle.
1140 close(FILE);
1141}
1142
69b3156f
SS
1143#
1144## Function to read-in the given enabled or disables sids file.
1145#
1146sub read_enabled_disabled_sids_file($) {
1147 my ($file) = @_;
1148
1149 # Temporary hash to store the sids and their state. It will be
1150 # returned at the end of this function.
1151 my %temphash;
1152
1153 # Open the given filename.
1154 open(FILE, "$file") or die "Could not open $file. $!\n";
1155
1156 # Loop through the file.
1157 while(<FILE>) {
1158 # Remove newlines.
1159 chomp $_;
1160
1161 # Skip blank lines.
1162 next if ($_ =~ /^\s*$/);
1163
1164 # Skip coments.
1165 next if ($_ =~ /^\#/);
1166
1167 # Splitt line into sid and state part.
1168 my ($state, $sid) = split(" ", $_);
1169
1170 # Skip line if the sid is not numeric.
1171 next unless ($sid =~ /\d+/ );
1172
1173 # Check if the sid was enabled.
1174 if ($state eq "enablesid") {
1175 # Add the sid and its state as enabled to the temporary hash.
1176 $temphash{$sid} = "enabled";
1177 # Check if the sid was disabled.
1178 } elsif ($state eq "disablesid") {
1179 # Add the sid and its state as disabled to the temporary hash.
1180 $temphash{$sid} = "disabled";
1181 # Invalid state - skip the current sid and state.
1182 } else {
1183 next;
1184 }
1185 }
1186
1187 # Close filehandle.
1188 close(FILE);
1189
1190 # Return the hash.
1191 return %temphash;
1192}
1193
796eea21
SS
1194#
1195## Function to check if the IDS is running.
1196#
1197sub ids_is_running () {
1198 if(-f $idspidfile) {
1199 # Open PID file for reading.
1200 open(PIDFILE, "$idspidfile") or die "Could not open $idspidfile. $!\n";
1201
1202 # Grab the process-id.
1203 my $pid = <PIDFILE>;
1204
1205 # Close filehandle.
1206 close(PIDFILE);
1207
1208 # Remove any newline.
1209 chomp($pid);
1210
1211 # Check if a directory for the process-id exists in proc.
1212 if(-d "/proc/$pid") {
1213 # The IDS daemon is running return the process id.
1214 return $pid;
1215 }
1216 }
1217
1218 # Return nothing - IDS is not running.
1219 return;
1220}
1221
5240a809
SS
1222#
1223## Function to call suricatactrl binary with a given command.
1224#
1225sub call_suricatactrl ($) {
1226 # Get called option.
ed06bc81 1227 my ($option, $interval) = @_;
5240a809
SS
1228
1229 # Loop through the array of supported commands and check if
1230 # the given one is part of it.
1231 foreach my $cmd (@suricatactrl_cmds) {
1232 # Skip current command unless the given one has been found.
1233 next unless($cmd eq $option);
1234
ed06bc81
SS
1235 # Check if the given command is "cron".
1236 if ($option eq "cron") {
1237 # Check if an interval has been given.
1238 if ($interval) {
1239 # Check if the given interval is valid.
1240 foreach my $element (@cron_intervals) {
1241 # Skip current element until the given one has been found.
1242 next unless($element eq $interval);
1243
1244 # Call the suricatactrl binary and pass the "cron" command
1245 # with the requrested interval.
81631920 1246 &General::system("$suricatactrl", "$option", "$interval");
ed06bc81
SS
1247
1248 # Return "1" - True.
1249 return 1;
1250 }
1251 }
5240a809 1252
ed06bc81
SS
1253 # If we got here, the given interval is not supported or none has been given. - Return nothing.
1254 return;
1255 } else {
1256 # Call the suricatactrl binary and pass the requrested
1257 # option to it.
81631920 1258 &General::system("$suricatactrl", "$option");
ed06bc81
SS
1259
1260 # Return "1" - True.
1261 return 1;
1262 }
5240a809
SS
1263 }
1264
1265 # Command not found - return nothing.
1266 return;
1267}
1268
308ba5e7
SS
1269#
1270## Function to create a new empty file.
1271#
1272sub create_empty_file($) {
1273 my ($file) = @_;
1274
1275 # Check if the given file exists.
1276 if(-e $file) {
1277 # Do nothing to prevent from overwriting existing files.
1278 return;
1279 }
1280
1281 # Open the file for writing.
1282 open(FILE, ">$file") or die "Could not write to $file. $!\n";
1283
1284 # Close file handle.
1285 close(FILE);
1286
1287 # Return true.
1288 return 1;
1289}
1290
330759d8
SS
1291#
1292## Private function to check if the file permission of the rulespath are correct.
1293## If not, call suricatactrl to fix them.
1294#
1295sub _check_rulesdir_permissions() {
e568796b
SS
1296 # Check if the rulepath main directory is writable.
1297 unless (-W $rulespath) {
1298 # If not call suricatctrl to fix it.
1299 &call_suricatactrl("fix-rules-dir");
1300 }
1301
330759d8
SS
1302 # Open snort rules directory and do a directory listing.
1303 opendir(DIR, $rulespath) or die $!;
1304 # Loop through the direcory.
1305 while (my $file = readdir(DIR)) {
1306 # We only want files.
1307 next unless (-f "$rulespath/$file");
1308
1309 # Check if the file is writable by the user.
1310 if (-W "$rulespath/$file") {
1311 # Everything is okay - go on to the next file.
1312 next;
1313 } else {
1314 # There are wrong permissions, call suricatactrl to fix it.
1315 &call_suricatactrl("fix-rules-dir");
1316 }
1317 }
1318}
1319
b59cdbee
SS
1320#
1321## Private function to cleanup the directory which contains
1322## the IDS rules, before extracting and modifing the new ruleset.
1323#
1324sub _cleanup_rulesdir() {
8cf04a16
SS
1325 # Open rules directory and do a directory listing.
1326 opendir(DIR, $rulespath) or die $!;
1327
1328 # Loop through the direcory.
1329 while (my $file = readdir(DIR)) {
1330 # We only want files.
1331 next unless (-f "$rulespath/$file");
1332
fefb5173
SS
1333 # Skip rules file for whitelisted hosts.
1334 next if ("$rulespath/$file" eq $whitelist_file);
1335
612bb2df
SS
1336 # Skip rules file with local rules.
1337 next if ("$rulespath/$file" eq $local_rules_file);
1338
8cf04a16 1339 # Delete the current processed file, if not, exit this function
b59cdbee 1340 # and return an error message.
1201c1e7 1341 unlink("$rulespath/$file") or return "Could not delete $rulespath/$file. $!\n";
b59cdbee
SS
1342 }
1343
4ce42488 1344 # Return nothing;
b59cdbee
SS
1345 return;
1346}
1347
b02e30fd
SS
1348#
1349## Function to generate the file which contains the home net information.
1350#
1351sub generate_home_net_file() {
1352 my %netsettings;
1353
1354 # Read-in network settings.
1355 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
1356
1357 # Get available network zones.
abffcc99 1358 my @network_zones = &Network::get_available_network_zones();
b02e30fd
SS
1359
1360 # Temporary array to store network address and prefix of the configured
1361 # networks.
1362 my @networks;
1363
1364 # Loop through the array of available network zones.
1365 foreach my $zone (@network_zones) {
bcbc9897
SS
1366 # Check if the current processed zone is red.
1367 if($zone eq "red") {
1368 # Grab the IP-address of the red interface.
1369 my $red_address = &get_red_address();
b02e30fd 1370
bcbc9897
SS
1371 # Check if an address has been obtained.
1372 if ($red_address) {
1373 # Generate full network string.
1374 my $red_network = join("/", $red_address, "32");
b02e30fd 1375
bcbc9897
SS
1376 # Add the red network to the array of networks.
1377 push(@networks, $red_network);
1378 }
23c0347a 1379
23c0347a
SS
1380 # Check if the configured RED_TYPE is static.
1381 if ($netsettings{'RED_TYPE'} eq "STATIC") {
1382 # Get configured and enabled aliases.
1383 my @aliases = &get_aliases();
1384
1385 # Loop through the array.
1386 foreach my $alias (@aliases) {
1387 # Add "/32" prefix.
1388 my $network = join("/", $alias, "32");
1389
1390 # Add the generated network to the array of networks.
1391 push(@networks, $network);
1392 }
1393 }
bcbc9897
SS
1394 # Process remaining network zones.
1395 } else {
1396 # Convert current zone name into upper case.
1397 $zone = uc($zone);
1398
1399 # Generate key to access the required data from the netsettings hash.
1400 my $zone_netaddress = $zone . "_NETADDRESS";
1401 my $zone_netmask = $zone . "_NETMASK";
1402
1403 # Obtain the settings from the netsettings hash.
1404 my $netaddress = $netsettings{$zone_netaddress};
1405 my $netmask = $netsettings{$zone_netmask};
1406
1407 # Convert the subnetmask into prefix notation.
1408 my $prefix = &Network::convert_netmask2prefix($netmask);
1409
1410 # Generate full network string.
1411 my $network = join("/", $netaddress,$prefix);
1412
1413 # Check if the network is valid.
1414 if(&Network::check_subnet($network)) {
1415 # Add the generated network to the array of networks.
1416 push(@networks, $network);
1417 }
23c0347a 1418 }
b02e30fd
SS
1419 }
1420
1421 # Format home net declaration.
7479c993 1422 my $line = "\"[" . join(',', @networks) . "]\"";
b02e30fd
SS
1423
1424 # Open file to store the addresses of the home net.
1425 open(FILE, ">$homenet_file") or die "Could not open $homenet_file. $!\n";
1426
1427 # Print yaml header.
1428 print FILE "%YAML 1.1\n";
1429 print FILE "---\n\n";
1430
1431 # Print notice about autogenerated file.
1432 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1433
1434 # Print the generated and required HOME_NET declaration to the file.
1435 print FILE "HOME_NET:\t$line\n";
1436
1437 # Close file handle.
1438 close(FILE);
1439}
1440
30ee98e9
SS
1441#
1442# Function to generate and write the file which contains the configured and used DNS servers.
1443#
1444sub generate_dns_servers_file() {
12c49915
SS
1445 # Get the used DNS servers.
1446 my @nameservers = &General::get_nameservers();
30ee98e9 1447
7b97359b
SS
1448 # Get network settings.
1449 my %netsettings;
1450 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
1451
30ee98e9 1452 # Format dns servers declaration.
b76118c3 1453 my $line = "";
30ee98e9 1454
12c49915
SS
1455 # Check if the system has configured nameservers.
1456 if (@nameservers) {
7b97359b
SS
1457 # Add the GREEN address as DNS servers.
1458 push(@nameservers, $netsettings{'GREEN_ADDRESS'});
1459
1460 # Check if a BLUE zone exists.
1461 if ($netsettings{'BLUE_ADDRESS'}) {
1462 # Add the BLUE address to the array of nameservers.
1463 push(@nameservers, $netsettings{'BLUE_ADDRESS'});
1464 }
1465
1466 # Generate the line which will be written to the DNS servers file.
b76118c3 1467 $line = join(",", @nameservers);
12c49915 1468 } else {
7b97359b 1469 # External net simply contains (any).
99cadb74 1470 $line = "\$EXTERNAL_NET";
fd2dccaa 1471 }
30ee98e9 1472
30ee98e9
SS
1473 # Open file to store the used DNS server addresses.
1474 open(FILE, ">$dns_servers_file") or die "Could not open $dns_servers_file. $!\n";
1475
1476 # Print yaml header.
1477 print FILE "%YAML 1.1\n";
1478 print FILE "---\n\n";
1479
1480 # Print notice about autogenerated file.
1481 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1482
1483 # Print the generated DNS declaration to the file.
b76118c3 1484 print FILE "DNS_SERVERS:\t\"[$line]\"\n";
30ee98e9
SS
1485
1486 # Close file handle.
1487 close(FILE);
1488}
1489
e698090e
SS
1490#
1491# Function to generate and write the file which contains the HTTP_PORTS definition.
1492#
1493sub generate_http_ports_file() {
1494 my %proxysettings;
1495
1496 # Read-in proxy settings
1497 &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings);
1498
1499 # Check if the proxy is enabled.
1500 if (( -e "${General::swroot}/proxy/enable") || (-e "${General::swroot}/proxy/enable_blue")) {
1501 # Add the proxy port to the array of HTTP ports.
1502 push(@http_ports, $proxysettings{'PROXY_PORT'});
1503 }
1504
1505 # Check if the transparent mode of the proxy is enabled.
1506 if ((-e "${General::swroot}/proxy/transparent") || (-e "${General::swroot}/proxy/transparent_blue")) {
1507 # Add the transparent proxy port to the array of HTTP ports.
1508 push(@http_ports, $proxysettings{'TRANSPARENT_PORT'});
1509 }
1510
1511 # Format HTTP_PORTS declaration.
1512 my $line = "";
1513
1514 # Generate line which will be written to the http ports file.
1515 $line = join(",", @http_ports);
1516
1517 # Open file to store the HTTP_PORTS.
1518 open(FILE, ">$http_ports_file") or die "Could not open $http_ports_file. $!\n";
1519
1520 # Print yaml header.
1521 print FILE "%YAML 1.1\n";
1522 print FILE "---\n\n";
1523
1524 # Print notice about autogenerated file.
1525 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1526
1527 # Print the generated HTTP_PORTS declaration to the file.
1528 print FILE "HTTP_PORTS:\t\"[$line]\"\n";
1529
1530 # Close file handle.
1531 close(FILE);
1532}
1533
b02e30fd 1534#
ddaf8ae1
SS
1535## Function to generate and write the file for used rulefiles file for a given provider.
1536##
1537## The function requires as first argument a provider handle, and as second an array with files.
b02e30fd 1538#
ddaf8ae1
SS
1539sub write_used_provider_rulefiles_file($@) {
1540 my ($provider, @files) = @_;
1541
1542 # Get the path and file for the provider specific used rulefiles file.
1543 my $used_provider_rulesfile_file = &get_used_provider_rulesfile_file($provider);
b02e30fd
SS
1544
1545 # Open file for used rulefiles.
a081f203 1546 open (FILE, ">", "$used_provider_rulesfile_file") or die "Could not write to $used_provider_rulesfile_file. $!\n";
b02e30fd
SS
1547
1548 # Write yaml header to the file.
1549 print FILE "%YAML 1.1\n";
1550 print FILE "---\n\n";
1551
1552 # Write header to file.
1553 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1554
b02e30fd
SS
1555 # Loop through the array of given files.
1556 foreach my $file (@files) {
1557 # Check if the given filename exists and write it to the file of used rulefiles.
1558 if(-f "$rulespath/$file") {
1559 print FILE " - $file\n";
1560 }
1561 }
1562
1563 # Close file after writing.
1564 close(FILE);
1565}
1566
ddaf8ae1
SS
1567#
1568## Function to write the main file for provider rulesfiles inclusions.
1569##
1570## This function requires an array of provider handles.
1571#
1572sub write_main_used_rulefiles_file (@) {
1573 my (@providers) = @_;
1574
88eb5626 1575 # Call function to write the static rulefiles file.
43d12991 1576 &_write_default_rulefiles_file();
88eb5626 1577
ddaf8ae1
SS
1578 # Open file for used rulefils inclusion.
1579 open (FILE, ">", "$suricata_used_providers_file") or die "Could not write to $suricata_used_providers_file. $!\n";
1580
1581 # Write yaml header to the file.
1582 print FILE "%YAML 1.1\n";
1583 print FILE "---\n\n";
1584
1585 # Write header to file.
1586 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1587
1588 # Loop through the list of given providers.
1589 foreach my $provider (@providers) {
1590 # Call function to get the providers used rulefiles file.
1591 my $filename = &get_used_provider_rulesfile_file($provider);
1592
1b0e555f
SS
1593 # Check if the file exists and write it into the used rulefiles file.
1594 if (-f $filename) {
1595 # Print the provider to the file.
1596 print FILE "include\: $filename\n";
1597 }
ddaf8ae1
SS
1598 }
1599
ddaf8ae1
SS
1600 # Close the filehandle after writing.
1601 close(FILE);
1602}
1603
43d12991
SS
1604sub _write_default_rulefiles_file () {
1605 # Get enabled application layer protocols.
1606 my @enabled_app_layer_protos = &get_suricata_enabled_app_layer_protos();
1607
88eb5626 1608 # Open file.
43d12991 1609 open (FILE, ">", $suricata_default_rulefiles_file) or die "Could not write to $suricata_default_rulefiles_file. $!\n";
88eb5626
SS
1610
1611 # Write yaml header to the file.
1612 print FILE "%YAML 1.1\n";
1613 print FILE "---\n\n";
1614
1615 # Write notice about autogenerated file.
1616 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1617
1618 # Loop through the array of static included rulesfiles.
1619 foreach my $file (@static_included_rulefiles) {
1620 # Check if the file exists.
1621 if (-f "$rulespath/$file") {
1622 # Write the rulesfile name to the file.
43d12991
SS
1623 print FILE " - $rulespath/$file\n";
1624 }
1625 }
1626
1627 print FILE "\n#Default rules for used application layer protocols.\n";
1628 foreach my $enabled_app_layer_proto (@enabled_app_layer_protos) {
1629 # Check if the current processed app layer proto needs to be translated
1630 # into an application name.
1631 if (exists($tr_app_layer_proto{$enabled_app_layer_proto})) {
43d12991
SS
1632 # Obtain the translated application name for this protocol.
1633 $enabled_app_layer_proto = $tr_app_layer_proto{$enabled_app_layer_proto};
1634 }
1635
1636 # Generate filename.
1637 my $rulesfile = "$default_rulespath/$enabled_app_layer_proto\.rules";
1638
1639 # Check if such a file exists.
1640 if (-f "$rulesfile") {
1641 # Write the rulesfile name to the file.
1642 print FILE " - $rulesfile\n";
1643 }
1644
1645 # Generate filename with "events" in filename.
1646 $rulesfile = "$default_rulespath/$enabled_app_layer_proto\-events.rules";
1647
1648 # Check if this file exists.
1649 if (-f "$rulesfile" ) {
1650 # Write the rulesfile name to the file.
1651 print FILE " - $rulesfile\n";
88eb5626
SS
1652 }
1653 }
1654
1655 # Close the file handle
1656 close(FILE);
1657}
1658
ddaf8ae1
SS
1659#
1660## Tiny function to generate the full path and name for the used_provider_rulesfile file of a given provider.
1661#
1662sub get_used_provider_rulesfile_file ($) {
1663 my ($provider) = @_;
1664
1665 my $filename = "$settingsdir/suricata\-$provider\-used\-rulefiles.yaml";
1666
1667 # Return the gernerated file.
1668 return $filename;
1669}
1670
518cbdd3
SS
1671#
1672## Tiny function to generate the full path and name for the file which stores the modifications of a ruleset.
1673#
1674sub get_provider_ruleset_modifications_file($) {
1675 my ($provider) = @_;
1676
1677 my $filename = "$settingsdir/$provider\-modifications";
1678
1679 # Return the filename.
1680 return $filename;
1681}
1682
74cc8f5a
SS
1683#
1684## Function to generate and write the file for modify the ruleset.
1685#
81bae51f
SS
1686sub write_modify_sids_file() {
1687 # Get configured settings.
1688 my %idssettings=();
81bae51f 1689 &General::readhash("$ids_settings_file", \%idssettings);
74cc8f5a
SS
1690
1691 # Open modify sid's file for writing.
2ee51088 1692 open(FILE, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
74cc8f5a
SS
1693
1694 # Write file header.
1695 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
1696
1697 # Check if the traffic only should be monitored.
81bae51f 1698 unless($idssettings{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
a5ba473c
TF
1699 # Suricata is in IPS mode, which means that the rule actions have to be changed
1700 # from 'alert' to 'drop', however not all rules should be changed. Some rules
1701 # exist purely to set a flowbit which is used to convey other information, such
1702 # as a specific type of file being downloaded, to other rulewhich then check for
1703 # malware in that file. Rules which fall into the first category should stay as
1704 # alert since not all flows of that type contain malware.
1705
aac869c4
SS
1706 # These types of rulesfiles contain meta-data which gives the action that should
1707 # be used when in IPS mode. Do the following:
1708 #
1709 # 1. Disable all rules and set the action to 'drop'
1710 # 2. Set the action back to 'alert' if the rule contains 'flowbits:noalert;'
1711 # This should give rules not in the policy a reasonable default if the user
1712 # manually enables them.
1713 # 3. Enable rules and set actions according to the meta-data strings.
a5ba473c 1714
aac869c4 1715 my $policy = 'balanced'; # Placeholder to allow policy to be changed.
a5ba473c
TF
1716
1717 print FILE <<END;
a5ba473c
TF
1718modifysid * "^#(?:alert|drop)(.+policy $policy-ips alert)" | "alert\${1}"
1719modifysid * "^#(?:alert|drop)(.+policy $policy-ips drop)" | "drop\${1}"
a5ba473c
TF
1720modifysid * "^(#?)(?:alert|drop)" | "\${1}drop"
1721modifysid * "^(#?)drop(.+flowbits:noalert;)" | "\${1}alert\${2}"
1722END
1723 }
74cc8f5a
SS
1724
1725 # Close file handle.
1726 close(FILE);
1727}
1728
235e3e92
SS
1729#
1730## Function to get the subscription code of a configured provider.
1731#
1732sub get_subscription_code($) {
1733 my ($provider) = @_;
1734
1735 my %configured_providers = ();
1736
1737 # Read-in providers settings file.
1738 &General::readhasharray($providers_settings_file, \%configured_providers);
1739
1740 # Loop through the hash of configured providers.
1741 foreach my $id (keys %configured_providers) {
1742 # Assign nice human-readable values to the data fields.
1743 my $provider_handle = $configured_providers{$id}[0];
1744 my $subscription_code = $configured_providers{$id}[1];
1745
1746 # Check if the current processed provider is the requested one.
1747 if ($provider_handle eq $provider) {
1748 # Return the obtained subscription code.
1749 return $subscription_code;
1750 }
1751 }
1752
1753 # No subscription code found - return nothing.
1754 return;
1755}
1756
f580aa8c
SS
1757#
1758## Function to get the ruleset date for a given provider.
1759##
1760## The function simply return the creation date in a human read-able format
1761## of the stored providers rulesfile.
1762#
1763sub get_ruleset_date($) {
1764 my ($provider) = @_;
fc685a36 1765 my $date;
c68bcbb2 1766 my $mtime;
f580aa8c 1767
f580aa8c
SS
1768 # Get the stored rulesfile for this provider.
1769 my $stored_rulesfile = &_get_dl_rulesfile($provider);
1770
c68bcbb2 1771 # Check if we got a file.
cad087c7 1772 if (-f $stored_rulesfile) {
c68bcbb2
SS
1773 # Call stat on the rulestarball.
1774 my $stat = stat("$stored_rulesfile");
f580aa8c 1775
c68bcbb2
SS
1776 # Get timestamp the file creation.
1777 $mtime = $stat->mtime;
1778 }
f580aa8c 1779
057e8953
SS
1780 # Check if the timestamp has not been grabbed.
1781 unless ($mtime) {
1782 # Return N/A for Not available.
1783 return "N/A";
1784 }
1785
f580aa8c 1786 # Convert into human read-able format.
fc685a36
SS
1787 $date = strftime('%Y-%m-%d %H:%M:%S', localtime($mtime));
1788
f580aa8c
SS
1789 # Return the date.
1790 return $date;
1791}
1792
04a0d07c
SS
1793#
1794## Function to gather the version of suricata.
1795#
1796sub get_suricata_version($) {
1797 my ($format) = @_;
1798
1799 # Execute piped suricata command and return the version information.
1800 open(SURICATA, "suricata -V |") or die "Couldn't execute program: $!";
1801
1802 # Grab and store the output of the piped program.
1803 my $version_string = <SURICATA>;
1804
1805 # Close pipe.
1806 close(SURICATA);
1807
1808 # Remove newlines.
1809 chomp($version_string);
1810
fd2dccaa 1811 # Grab the version from the version string.
04a0d07c
SS
1812 $version_string =~ /([0-9]+([.][0-9]+)+)/;
1813
1814 # Splitt the version into single chunks.
1815 my ($major_ver, $minor_ver, $patchlevel) = split(/\./, $1);
1816
1817 # Check and return the requested version sheme.
1818 if ($format eq "major") {
1819 # Return the full version.
1820 return "$major_ver";
1821 } elsif ($format eq "minor") {
1822 # Return the major and minor part.
1823 return "$major_ver.$minor_ver";
1824 } else {
1825 # Return the full version string.
1826 return "$major_ver.$minor_ver.$patchlevel";
fd2dccaa 1827 }
04a0d07c
SS
1828}
1829
bb39fac4
SS
1830#
1831## Function to get the enabled application layer protocols.
1832#
1833sub get_suricata_enabled_app_layer_protos() {
1834 # Array to store and return the enabled app layer protos.
1835 my @enabled_app_layer_protos = ();
1836
1837 # Execute piped suricata command and return the list of
1838 # enabled application layer protocols.
1839 open(SURICATA, "suricata --list-app-layer-protos |") or die "Could not execute program: $!";
1840
1841 # Grab and store the list of enabled application layer protocols.
1842 my @output = <SURICATA>;
1843
1844 # Close pipe.
1845 close(SURICATA);
1846
1847 # Merge allways enabled static application layers protocols array.
1848 @enabled_app_layer_protos = @static_enabled_app_layer_protos;
1849
1850 # Loop through the array which contains the output of suricata.
1851 foreach my $line (@output) {
1852 # Skip header line which starts with "===".
1853 next if ($line =~ /^\s*=/);
1854
1855 # Skip info or warning lines.
1856 next if ($line =~ /\s*--/);
1857
1858 # Remove newlines.
1859 chomp($line);
1860
1861 # Add enabled app layer proto to the array.
1862 push(@enabled_app_layer_protos, $line);
1863 }
1864
1865 # Sort the array.
1866 @enabled_app_layer_protos = sort(@enabled_app_layer_protos);
1867
1868 # Return the array.
1869 return @enabled_app_layer_protos;
1870}
1871
9283e9b9
SS
1872#
1873## Function to generate the rules file with whitelisted addresses.
1874#
1875sub generate_ignore_file() {
1876 my %ignored = ();
1877
1878 # SID range 1000000-1999999 Reserved for Local Use
1879 # Put your custom rules in this range to avoid conflicts
1880 my $sid = 1500000;
1881
1882 # Read-in ignoredfile.
1883 &General::readhasharray($IDS::ignored_file, \%ignored);
1884
1885 # Open ignorefile for writing.
1886 open(FILE, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n";
1887
1888 # Config file header.
1889 print FILE "# Autogenerated file.\n";
1890 print FILE "# All user modifications will be overwritten.\n\n";
1891
1892 # Add all user defined addresses to the whitelist.
1893 #
1894 # Check if the hash contains any elements.
1895 if (keys (%ignored)) {
1896 # Loop through the entire hash and write the host/network
1897 # and remark to the ignore file.
1898 while ( (my $key) = each %ignored) {
1899 my $address = $ignored{$key}[0];
1900 my $remark = $ignored{$key}[1];
1901 my $status = $ignored{$key}[2];
1902
1903 # Check if the status of the entry is "enabled".
1904 if ($status eq "enabled") {
1905 # Check if the address/network is valid.
1906 if ((&General::validip($address)) || (&General::validipandmask($address))) {
1907 # Write rule line to the file to pass any traffic from this IP
c1ccae1c 1908 print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; bypass; sid:$sid\;)\n";
9283e9b9
SS
1909
1910 # Increment sid.
1911 $sid++;
1912 }
1913 }
1914 }
1915 }
1916
1917 close(FILE);
1918}
1919
1fedede6
SS
1920#
1921## Function to set correct ownership for single files and directories.
1922#
1923
1924sub set_ownership($) {
1925 my ($target) = @_;
1926
1927 # User and group of the WUI.
1928 my $uname = "nobody";
1929 my $grname = "nobody";
1930
1931 # The chown function implemented in perl requies the user and group as nummeric id's.
1932 my $uid = getpwnam($uname);
1933 my $gid = getgrnam($grname);
1934
1935 # Check if the given target exists.
1936 unless ($target) {
1937 # Stop the script and print error message.
1938 die "The $target does not exist. Cannot change the ownership!\n";
1939 }
1940
1941 # Check weather the target is a file or directory.
1942 if (-f $target) {
1943 # Change ownership ot the single file.
1944 chown($uid, $gid, "$target");
1945 } elsif (-d $target) {
1946 # Do a directory listing.
1947 opendir(DIR, $target) or die $!;
1948 # Loop through the direcory.
1949 while (my $file = readdir(DIR)) {
1950
1951 # We only want files.
1952 next unless (-f "$target/$file");
1953
1954 # Set correct ownership for the files.
1955 chown($uid, $gid, "$target/$file");
1956 }
1957
1958 closedir(DIR);
1959
1960 # Change ownership of the directory.
1961 chown($uid, $gid, "$target");
1962 }
1963}
77c31301
SS
1964
1965#
1966## Function to read-in the aliases file and returns all configured and enabled aliases.
1967#
1968sub get_aliases() {
1969 # Location of the aliases file.
1970 my $aliases_file = "${General::swroot}/ethernet/aliases";
1971
1972 # Array to store the aliases.
1973 my @aliases;
1974
1975 # Check if the file is empty.
1976 if (-z $aliases_file) {
1977 # Abort nothing to do.
1978 return;
1979 }
1980
1981 # Open the aliases file.
1982 open(ALIASES, $aliases_file) or die "Could not open $aliases_file. $!\n";
1983
1984 # Loop through the file content.
1985 while (my $line = <ALIASES>) {
1986 # Remove newlines.
1987 chomp($line);
1988
1989 # Splitt line content into single chunks.
1990 my ($address, $state, $remark) = split(/\,/, $line);
1991
1992 # Check if the state of the current processed alias is "on".
1993 if ($state eq "on") {
1994 # Check if the address is valid.
1995 if(&Network::check_ip_address($address)) {
1996 # Add the alias to the array of aliases.
1997 push(@aliases, $address);
1998 }
1999 }
2000 }
2001
2002 # Close file handle.
2003 close(ALIASES);
2004
2005 # Return the array.
2006 return @aliases;
2007}
2008
de8e1e5b
SS
2009#
2010## Function to grab the current assigned IP-address on red.
2011#
2012sub get_red_address() {
2013 # File, which contains the current IP-address of the red interface.
2014 my $file = "${General::swroot}/red/local-ipaddress";
2015
2016 # Check if the file exists.
2017 if (-e $file) {
2018 # Open the given file.
2019 open(FILE, "$file") or die "Could not open $file.";
2020
2021 # Obtain the address from the first line of the file.
2022 my $address = <FILE>;
2023
2024 # Close filehandle
2025 close(FILE);
2026
2027 # Remove newlines.
2028 chomp $address;
2029
2030 # Check if the grabbed address is valid.
2031 if (&General::validip($address)) {
2032 # Return the address.
2033 return $address;
2034 }
2035 }
2036
2037 # Return nothing.
2038 return;
2039}
77c31301 2040
6563d449 2041#
ddaf8ae1 2042## Function to get the used rules files of a given provider.
6563d449 2043#
ddaf8ae1
SS
2044sub read_used_provider_rulesfiles($) {
2045 my ($provider) = @_;
2046
6563d449
SS
2047 # Array to store the used rulefiles.
2048 my @used_rulesfiles = ();
2049
ddaf8ae1
SS
2050 # Get the used rulesefile file for the provider.
2051 my $rulesfile_file = &get_used_provider_rulesfile_file($provider);
2052
01fc880c
SS
2053 # Check if the a used rulesfile exists for this provider.
2054 if (-f $rulesfile_file) {
6563d449 2055 # Open the file or used rulefiles and read-in content.
ddaf8ae1 2056 open(FILE, $rulesfile_file) or die "Could not open $rulesfile_file. $!\n";
6563d449
SS
2057
2058 while (<FILE>) {
2059 # Assign the current line to a nice variable.
2060 my $line = $_;
2061
2062 # Remove newlines.
2063 chomp($line);
2064
2065 # Skip comments.
2066 next if ($line =~ /\#/);
2067
2068 # Skip blank lines.
2069 next if ($line =~ /^\s*$/);
2070
2071 # Gather the rulefile.
2072 if ($line =~ /.*- (.*)/) {
2073 my $rulefile = $1;
2074
2075 # Add the rulefile to the array of used rulesfiles.
2076 push(@used_rulesfiles, $rulefile);
2077 }
2078 }
2079
2080 # Close the file.
2081 close(FILE);
2082 }
2083
2084 # Return the array of used rulesfiles.
2085 return @used_rulesfiles;
2086}
2087
8076deba
SS
2088#
2089## Function to write the lock file for locking the WUI, while
2090## the autoupdate script runs.
2091#
2092sub lock_ids_page() {
2093 # Call subfunction to create the file.
2094 &create_empty_file($ids_page_lock_file);
2095}
2096
2097#
2098## Function to release the lock of the WUI, again.
2099#
2100sub unlock_ids_page() {
2101 # Delete lock file.
2102 unlink($ids_page_lock_file);
2103}
2104
8dcebe53 21051;