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}/suricata";
31 # Location and name of the tarball which contains the ruleset.
32 our $rulestarball = "/var/tmp/idsrules.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/suricata/rules";
40 # File which contains a list of all supported ruleset sources.
41 # (Sourcefire, Emergingthreads, etc..)
42 our $rulesetsourcesfile = "$settingsdir/ruleset-sources";
44 # The pidfile of the IDS.
45 our $idspidfile = "/var/run/suricata.pid";
48 ## Function for checking if at least 300MB of free disk space are available
49 ## on the "/var" partition.
51 sub checkdiskspace
() {
52 # Call diskfree to gather the free disk space of /var.
53 my @df = `/bin/df -B M /var`;
55 # Loop through the output.
56 foreach my $line (@df) {
58 next if $line =~ m/^Filesystem/;
60 # Search for a line with the device information.
61 if ($line =~ m/dev/ ) {
62 # Split the line into single pieces.
63 my @values = split(' ', $line);
64 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
66 # Check if the available disk space is more than 300MB.
67 if ($available < 300) {
68 # Log error to syslog.
69 &_log_to_syslog
("Not enough free disk space on /var. Only $available MB from 300 MB available.");
71 # Exit function and return "1" - False.
77 # Everything okay, return nothing.
82 ## This function is responsible for downloading the configured snort ruleset.
84 ## * At first it obtains from the stored snortsettings which ruleset should be downloaded.
85 ## * The next step is to get the download locations for all available rulesets.
86 ## * After that, the function will check if an upstream proxy should be used and grab the settings.
87 ## * The last step will be to generate the final download url, by obtaining the URL for the desired
88 ## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server.
93 &General
::readhash
("$settingsdir/settings", \
%snortsettings);
95 # Get all available ruleset locations.
96 my %rulesetsources=();
97 &General
::readhash
($rulesetsourcesfile, \
%rulesetsources);
100 my %proxysettings=();
101 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
103 # Load required perl module to handle the download.
106 # Init the download module.
107 my $downloader = LWP
::UserAgent
->new;
109 # Set timeout to 10 seconds.
110 $downloader->timeout(10);
112 # Check if an upstream proxy is configured.
113 if ($proxysettings{'UPSTREAM_PROXY'}) {
114 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\
/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?
)?
$/);
117 # Check if we got a peer.
119 $proxy_url = "http://";
121 # Check if the proxy requires authentication.
122 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
123 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
126 # Add proxy server address and port.
127 $proxy_url .= "$peer\:$peerport";
129 # Log error message and break.
130 &_log_to_syslog
("Could not proper configure the proxy server access.");
132 # Return "1" - false.
136 # Setup proxy settings.
137 $downloader->proxy('http', $proxy_url);
140 # Grab the right url based on the configured vendor.
141 my $url = $rulesetsources{$snortsettings{'RULES'}};
143 # Check if the vendor requires an oinkcode and add it if needed.
144 $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g;
146 # Abort if no url could be determined for the vendor.
148 # Log error and abort.
149 &_log_to_syslog
("Unable to gather a download URL for the selected ruleset.");
153 # Pass the requested url to the downloader.
154 my $request = HTTP
::Request
->new(GET
=> $url);
156 # Perform the request and save the output into the "$rulestarball" file.
157 my $response = $downloader->request($request, $rulestarball);
159 # Check if there was any error.
160 unless ($response->is_success) {
162 &_log_to_syslog
("Unable to download the ruleset. $response->status_line");
164 # Return "1" - false.
168 # If we got here, everything worked fine. Return nothing.
173 ## A tiny wrapper function to call the oinkmaster script.
176 # Load perl module to talk to the kernel syslog.
177 use Sys
::Syslog
qw(:DEFAULT setlogsock);
179 # Establish the connection to the syslog service.
180 openlog
('oinkmaster', 'cons,pid', 'user');
182 # Call oinkmaster to generate ruleset.
183 open(OINKMASTER
, "/usr/local/bin/oinkmaster.pl -v -s -u file://$rulestarball -C $settingsdir/oinkmaster.conf -o $rulespath|");
185 # Log output of oinkmaster to syslog.
186 while(<OINKMASTER
>) {
187 # The syslog function works best with an array based input,
188 # so generate one before passing the message details to syslog.
189 my @syslog = ("INFO", "$_");
191 # Send the log message.
195 # Close the pipe to oinkmaster process.
198 # Close the log handle.
203 ## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
208 # Remove any newline.
211 # Call private function to log the error message to syslog.
212 &_log_to_syslog
($error);
214 # Call private function to write/store the error message in the storederrorfile.
215 &_store_error_message
($error);
219 ## Function to log a given error message to the kernel syslog.
221 sub _log_to_syslog
($) {
224 # Load perl module to talk to the kernel syslog.
225 use Sys
::Syslog
qw(:DEFAULT setlogsock);
227 # The syslog function works best with an array based input,
228 # so generate one before passing the message details to syslog.
229 my @syslog = ("ERR", "<ERROR> $message");
231 # Establish the connection to the syslog service.
232 openlog
('oinkmaster', 'cons,pid', 'user');
234 # Send the log message.
237 # Close the log handle.
242 ## Private function to write a given error message to the storederror file.
244 sub _store_error_message
($) {
247 # Remove any newline.
250 # Open file for writing.
251 open (ERRORFILE
, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
253 # Write error to file.
254 print ERRORFILE
"$message\n";
261 ## Function to get a list of all available network zones.
263 sub get_available_network_zones
() {
265 my %netsettings = ();
266 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
268 # Obtain the configuration type from the netsettings hash.
269 my $config_type = $netsettings{'CONFIG_TYPE'};
271 # Hash which contains the conversation from the config mode
272 # to the existing network interface names. They are stored like
275 # Mode "0" red is a modem and green
276 # Mode "1" red is a netdev and green
277 # Mode "2" red, green and orange
278 # Mode "3" red, green and blue
279 # Mode "4" red, green, blue, orange
280 my %config_type_to_interfaces = (
281 "0" => [ "red", "green" ],
282 "1" => [ "red", "green" ],
283 "2" => [ "red", "green", "orange" ],
284 "3" => [ "red", "green", "blue" ],
285 "4" => [ "red", "green", "blue", "orange" ]
288 # Obtain and dereference the corresponding network interaces based on the read
289 # network config type.
290 my @network_zones = @
{ $config_type_to_interfaces{$config_type} };
293 return @network_zones;
297 ## Function to check if the IDS is running.
299 sub ids_is_running
() {
301 # Open PID file for reading.
302 open(PIDFILE
, "$idspidfile") or die "Could not open $idspidfile. $!\n";
304 # Grab the process-id.
310 # Remove any newline.
313 # Check if a directory for the process-id exists in proc.
314 if(-d
"/proc/$pid") {
315 # The IDS daemon is running return the process id.
320 # Return nothing - IDS is not running.