]> git.ipfire.org Git - ipfire-2.x.git/blame - config/cfgroot/ids-functions.pl
IDS: Introduce settingsdir variable
[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# #
20# Copyright (C) 2018 IPFire Team <info@ipfire.org>. #
21# #
22############################################################################
23
24package IDS;
25
26require '/var/ipfire/general-functions.pl';
27require "${General::swroot}/lang.pl";
28
02844177
SS
29# Location where all config and settings files are stored.
30our $settingsdir = "${General::swroot}/snort";
31
eea2670b 32# Location and name of the tarball which contains the ruleset.
77910792 33our $rulestarball = "/var/tmp/snortrules.tar.gz";
eea2670b 34
3983aebd 35# File to store any errors, which also will be read and displayed by the wui.
77910792 36our $storederrorfile = "/tmp/ids_storederror";
3983aebd 37
298ef5ba
SS
38# Location where the rulefiles are stored.
39our $rulespath = "/etc/snort/rules";
40
8dcebe53
SS
41#
42## Function for checking if at least 300MB of free disk space are available
43## on the "/var" partition.
44#
45sub checkdiskspace () {
46 # Call diskfree to gather the free disk space of /var.
47 my @df = `/bin/df -B M /var`;
48
49 # Loop through the output.
50 foreach my $line (@df) {
51 # Ignore header line.
52 next if $line =~ m/^Filesystem/;
53
54 # Search for a line with the device information.
55 if ($line =~ m/dev/ ) {
56 # Split the line into single pieces.
57 my @values = split(' ', $line);
58 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
59
60 # Check if the available disk space is more than 300MB.
61 if ($available < 300) {
62 # If there is not enough space, print out an error message.
63 my $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $available MB";
64
65 # Exit function and return the error message.
66 return $errormessage;
67 }
68 }
69 }
70
71 # Everything okay, return nothing.
72 return;
73}
74
eea2670b
SS
75#
76## This function is responsible for downloading the configured snort ruleset.
77##
78## * At first it obtains from the stored snortsettings which ruleset should be downloaded.
79## * The next step is to get the download locations for all available rulesets.
80## * After that, the function will check if an upstream proxy should be used and grab the settings.
81## * The last step will be to generate the final download url, by obtaining the URL for the desired
82## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server.
83#
84sub downloadruleset {
85 # Get snort settings.
86 my %snortsettings=();
02844177 87 &General::readhash("$settingsdir/settings", \%snortsettings);
eea2670b
SS
88
89 # Get all available ruleset locations.
90 my %rulesetsources=();
02844177 91 &General::readhash("$settingsdir/ruleset-sources.list", \%rulesetsources);
eea2670b
SS
92
93 # Read proxysettings.
94 my %proxysettings=();
95 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
96
97 # Load required perl module to handle the download.
98 use LWP::UserAgent;
99
100 # Init the download module.
101 my $downloader = LWP::UserAgent->new;
102
103 # Set timeout to 10 seconds.
104 $downloader->timeout(10);
105
106 # Check if an upstream proxy is configured.
107 if ($proxysettings{'UPSTREAM_PROXY'}) {
108 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
109 my $proxy_url;
110
111 # Check if we got a peer.
112 if ($peer) {
113 $proxy_url = "http://";
114
115 # Check if the proxy requires authentication.
116 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
117 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
118 }
119
120 # Add proxy server address and port.
121 $proxy_url .= "$peer\:$peerport";
122 } else {
123 # Break and return error message.
124 return "$Lang::tr{'could not download latest updates'}";
125 }
126
127 # Setup proxy settings.
128 $downloader->proxy('http', $proxy_url);
129 }
130
131 # Grab the right url based on the configured vendor.
132 my $url = $rulesetsources{$snortsettings{'RULES'}};
133
134 # Check if the vendor requires an oinkcode and add it if needed.
135 $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g;
136
137 # Abort if no url could be determined for the vendor.
138 unless ($url) {
139 # Abort and return errormessage.
140 return "$Lang::tr{'could not download latest updates'}";
141 }
142
143 # Pass the requested url to the downloader.
144 my $request = HTTP::Request->new(GET => $url);
145
146 # Perform the request and save the output into the "$rulestarball" file.
147 my $response = $downloader->request($request, $rulestarball);
148
149 # Check if there was any error.
150 unless ($response->is_success) {
151 # Return error message.
152 return "$response->status_line";
153 }
154
155 # If we got here, everything worked fine. Return nothing.
156 return;
157}
8dcebe53 158
25f5cb0d
SS
159#
160## A tiny wrapper function to call the oinkmaster script.
161#
162sub oinkmaster () {
0e40e1e7
SS
163 # Load perl module to talk to the kernel syslog.
164 use Sys::Syslog qw(:DEFAULT setlogsock);
165
166 # Establish the connection to the syslog service.
167 openlog('oinkmaster', 'cons,pid', 'user');
168
25f5cb0d 169 # Call oinkmaster to generate ruleset.
02844177 170 open(OINKMASTER, "/usr/local/bin/oinkmaster.pl -v -s -u file://$rulestarball -C $settingsdir/oinkmaster.conf -o $rulespath|");
0e40e1e7
SS
171
172 # Log output of oinkmaster to syslog.
173 while(<OINKMASTER>) {
174 # The syslog function works best with an array based input,
175 # so generate one before passing the message details to syslog.
176 my @syslog = ("INFO", "$_");
177
178 # Send the log message.
179 syslog(@syslog);
180 }
181
182 # Close the pipe to oinkmaster process.
183 close(OINKMASTER);
184
185 # Close the log handle.
186 closelog();
25f5cb0d
SS
187}
188
3983aebd
SS
189#
190## Function to do all the logging stuff if the downloading or updating of the ruleset fails.
191#
192sub log_error ($) {
193 my ($error) = @_;
194
195 # Remove any newline.
196 chomp($error);
197
eb5592c1
SS
198 # Call private function to log the error message to syslog.
199 &_log_to_syslog($error);
200
3983aebd
SS
201 # Call private function to write/store the error message in the storederrorfile.
202 &_store_error_message($error);
203}
204
eb5592c1
SS
205#
206## Function to log a given error message to the kernel syslog.
207#
208sub _log_to_syslog ($) {
209 my ($message) = @_;
210
211 # Load perl module to talk to the kernel syslog.
212 use Sys::Syslog qw(:DEFAULT setlogsock);
213
214 # The syslog function works best with an array based input,
215 # so generate one before passing the message details to syslog.
216 my @syslog = ("ERR", "<ERROR> $message");
217
218 # Establish the connection to the syslog service.
219 openlog('oinkmaster', 'cons,pid', 'user');
220
221 # Send the log message.
222 syslog(@syslog);
223
224 # Close the log handle.
225 closelog();
226}
227
3983aebd
SS
228#
229## Private function to write a given error message to the storederror file.
230#
231sub _store_error_message ($) {
232 my ($message) = @_;
233
234 # Remove any newline.
235 chomp($message);
236
237 # Open file for writing.
238 open (ERRORFILE, ">$storederrorfile") or die "Could not write to $storederrorfile. $!\n";
239
240 # Write error to file.
241 print ERRORFILE "$message\n";
242
243 # Close file.
244 close (ERRORFILE);
245}
246
8dcebe53 2471;