]>
git.ipfire.org Git - ipfire-2.x.git/blob - config/cfgroot/ids-functions.pl
2 ############################################################################
4 # This file is part of the IPFire Firewall. #
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. #
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. #
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 #
20 # Copyright (C) 2018 IPFire Team <info@ipfire.org>. #
22 ############################################################################
26 require '/var/ipfire/general-functions.pl';
28 # Location where all config and settings files are stored.
29 our $settingsdir = "${General::swroot}/snort";
31 # Location and name of the tarball which contains the ruleset.
32 our $rulestarball = "/var/tmp/snortrules.tar.gz";
34 # File to store any errors, which also will be read and displayed by the wui.
35 our $storederrorfile = "/tmp/ids_storederror";
37 # Location where the rulefiles are stored.
38 our $rulespath = "/etc/snort/rules";
41 ## Function for checking if at least 300MB of free disk space are available
42 ## on the "/var" partition.
44 sub checkdiskspace
() {
45 # Call diskfree to gather the free disk space of /var.
46 my @df = `/bin/df -B M /var`;
48 # Loop through the output.
49 foreach my $line (@df) {
51 next if $line =~ m/^Filesystem/;
53 # Search for a line with the device information.
54 if ($line =~ m/dev/ ) {
55 # Split the line into single pieces.
56 my @values = split(' ', $line);
57 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
59 # Check if the available disk space is more than 300MB.
60 if ($available < 300) {
61 # Log error to syslog.
62 &_log_to_syslog
("Not enough free disk space on /var. Only $available MB from 300 MB available.");
64 # Exit function and return "1" - False.
70 # Everything okay, return nothing.
75 ## This function is responsible for downloading the configured snort ruleset.
77 ## * At first it obtains from the stored snortsettings which ruleset should be downloaded.
78 ## * The next step is to get the download locations for all available rulesets.
79 ## * After that, the function will check if an upstream proxy should be used and grab the settings.
80 ## * The last step will be to generate the final download url, by obtaining the URL for the desired
81 ## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server.
86 &General
::readhash
("$settingsdir/settings", \
%snortsettings);
88 # Get all available ruleset locations.
89 my %rulesetsources=();
90 &General
::readhash
("$settingsdir/ruleset-sources.list", \
%rulesetsources);
94 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
96 # Load required perl module to handle the download.
99 # Init the download module.
100 my $downloader = LWP
::UserAgent
->new;
102 # Set timeout to 10 seconds.
103 $downloader->timeout(10);
105 # Check if an upstream proxy is configured.
106 if ($proxysettings{'UPSTREAM_PROXY'}) {
107 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
110 # Check if we got a peer.
112 $proxy_url = "http://";
114 # Check if the proxy requires authentication.
115 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
116 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
119 # Add proxy server address and port.
120 $proxy_url .= "$peer\:$peerport";
122 # Log error message and break.
123 &_log_to_syslog
("Could not proper configure the proxy server access.");
125 # Return "1" - false.
129 # Setup proxy settings.
130 $downloader->proxy('http', $proxy_url);
133 # Grab the right url based on the configured vendor.
134 my $url = $rulesetsources{$snortsettings{'RULES'}};
136 # Check if the vendor requires an oinkcode and add it if needed.
137 $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g;
139 # Abort if no url could be determined for the vendor.
141 # Log error and abort.
142 &_log_to_syslog
("Unable to gather a download URL for the selected ruleset.");
146 # Pass the requested url to the downloader.
147 my $request = HTTP
::Request
->new(GET
=> $url);
149 # Perform the request and save the output into the "$rulestarball" file.
150 my $response = $downloader->request($request, $rulestarball);
152 # Check if there was any error.
153 unless ($response->is_success) {
155 &_log_to_syslog
("Unable to download the ruleset. $response->status_line");
157 # Return "1" - false.
161 # If we got here, everything worked fine. Return nothing.
166 ## A tiny wrapper function to call the oinkmaster script.
169 # Load perl module to talk to the kernel syslog.
170 use Sys
::Syslog
qw(:DEFAULT setlogsock);
172 # Establish the connection to the syslog service.
173 openlog
('oinkmaster', 'cons,pid', 'user');
175 # Call oinkmaster to generate ruleset.
176 open(OINKMASTER
, "/usr/local/bin/oinkmaster.pl -v -s -u file://$rulestarball -C $settingsdir/oinkmaster.conf -o $rulespath|");
178 # Log output of oinkmaster to syslog.
179 while(<OINKMASTER
>) {
180 # The syslog function works best with an array based input,
181 # so generate one before passing the message details to syslog.
182 my @syslog = ("INFO", "$_");
184 # Send the log message.
188 # Close the pipe to oinkmaster process.
191 # Close the log handle.
196 ## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
201 # Remove any newline.
204 # Call private function to log the error message to syslog.
205 &_log_to_syslog
($error);
207 # Call private function to write/store the error message in the storederrorfile.
208 &_store_error_message
($error);
212 ## Function to log a given error message to the kernel syslog.
214 sub _log_to_syslog
($) {
217 # Load perl module to talk to the kernel syslog.
218 use Sys
::Syslog
qw(:DEFAULT setlogsock);
220 # The syslog function works best with an array based input,
221 # so generate one before passing the message details to syslog.
222 my @syslog = ("ERR", "<ERROR> $message");
224 # Establish the connection to the syslog service.
225 openlog
('oinkmaster', 'cons,pid', 'user');
227 # Send the log message.
230 # Close the log handle.
235 ## Private function to write a given error message to the storederror file.
237 sub _store_error_message
($) {
240 # Remove any newline.
243 # Open file for writing.
244 open (ERRORFILE
, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
246 # Write error to file.
247 print ERRORFILE
"$message\n";