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