]> git.ipfire.org Git - ipfire-2.x.git/blob - config/cfgroot/ids-functions.pl
ids.cgi: Move function to call oinkmaster 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 my $rulestarball = "/var/tmp/snortrules.tar.gz";
31
32 #
33 ## Function for checking if at least 300MB of free disk space are available
34 ## on the "/var" partition.
35 #
36 sub checkdiskspace () {
37 # Call diskfree to gather the free disk space of /var.
38 my @df = `/bin/df -B M /var`;
39
40 # Loop through the output.
41 foreach my $line (@df) {
42 # Ignore header line.
43 next if $line =~ m/^Filesystem/;
44
45 # Search for a line with the device information.
46 if ($line =~ m/dev/ ) {
47 # Split the line into single pieces.
48 my @values = split(' ', $line);
49 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
50
51 # Check if the available disk space is more than 300MB.
52 if ($available < 300) {
53 # If there is not enough space, print out an error message.
54 my $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $available MB";
55
56 # Exit function and return the error message.
57 return $errormessage;
58 }
59 }
60 }
61
62 # Everything okay, return nothing.
63 return;
64 }
65
66 #
67 ## This function is responsible for downloading the configured snort ruleset.
68 ##
69 ## * At first it obtains from the stored snortsettings which ruleset should be downloaded.
70 ## * The next step is to get the download locations for all available rulesets.
71 ## * After that, the function will check if an upstream proxy should be used and grab the settings.
72 ## * The last step will be to generate the final download url, by obtaining the URL for the desired
73 ## ruleset, add the settings for the upstream proxy and final grab the rules tarball from the server.
74 #
75 sub downloadruleset {
76 # Get snort settings.
77 my %snortsettings=();
78 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
79
80 # Get all available ruleset locations.
81 my %rulesetsources=();
82 &General::readhash("${General::swroot}/snort/ruleset-sources.list", \%rulesetsources);
83
84 # Read proxysettings.
85 my %proxysettings=();
86 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
87
88 # Load required perl module to handle the download.
89 use LWP::UserAgent;
90
91 # Init the download module.
92 my $downloader = LWP::UserAgent->new;
93
94 # Set timeout to 10 seconds.
95 $downloader->timeout(10);
96
97 # Check if an upstream proxy is configured.
98 if ($proxysettings{'UPSTREAM_PROXY'}) {
99 my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
100 my $proxy_url;
101
102 # Check if we got a peer.
103 if ($peer) {
104 $proxy_url = "http://";
105
106 # Check if the proxy requires authentication.
107 if (($proxysettings{'UPSTREAM_USER'}) && ($proxysettings{'UPSTREAM_PASSWORD'})) {
108 $proxy_url .= "$proxysettings{'UPSTREAM_USER'}\:$proxysettings{'UPSTREAM_PASSWORD'}\@";
109 }
110
111 # Add proxy server address and port.
112 $proxy_url .= "$peer\:$peerport";
113 } else {
114 # Break and return error message.
115 return "$Lang::tr{'could not download latest updates'}";
116 }
117
118 # Setup proxy settings.
119 $downloader->proxy('http', $proxy_url);
120 }
121
122 # Grab the right url based on the configured vendor.
123 my $url = $rulesetsources{$snortsettings{'RULES'}};
124
125 # Check if the vendor requires an oinkcode and add it if needed.
126 $url =~ s/\<oinkcode\>/$snortsettings{'OINKCODE'}/g;
127
128 # Abort if no url could be determined for the vendor.
129 unless ($url) {
130 # Abort and return errormessage.
131 return "$Lang::tr{'could not download latest updates'}";
132 }
133
134 # Pass the requested url to the downloader.
135 my $request = HTTP::Request->new(GET => $url);
136
137 # Perform the request and save the output into the "$rulestarball" file.
138 my $response = $downloader->request($request, $rulestarball);
139
140 # Check if there was any error.
141 unless ($response->is_success) {
142 # Return error message.
143 return "$response->status_line";
144 }
145
146 # If we got here, everything worked fine. Return nothing.
147 return;
148 }
149
150 #
151 ## A tiny wrapper function to call the oinkmaster script.
152 #
153 sub oinkmaster () {
154 # Call oinkmaster to generate ruleset.
155 system("/usr/local/bin/oinkmaster.pl -v -s -u file://$rulestarball -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1 |logger -t oinkmaster");
156 }
157
158 1;