]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids.cgi: Rework code which shows if oinkmaster is working
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / ids.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
b6f571fa 5# Copyright (C) 2007-2015 IPFire Team <info@ipfire.org> #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
ac1cfefa
MT
22use strict;
23
24# enable only the following on debugging purpose
90c2e164
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
73231650 27use File::Copy;
ac1cfefa 28
986e08d9 29require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
32
f2fdd0c1
CS
33my %color = ();
34my %mainsettings = ();
0b89daee 35my %netsettings = ();
ac1cfefa 36my %snortsettings=();
298723b9 37my %cgiparams=();
ac1cfefa 38my %checked=();
5a3e0dca 39my %selected=();
0b89daee
SS
40
41# Read-in main settings, for language, theme and colors.
42&General::readhash("${General::swroot}/main/settings", \%mainsettings);
43&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
44
45# Get netsettings.
ac1cfefa
MT
46&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
47
48&Header::showhttpheaders();
49
0b89daee 50# Default settings for snort.
ac1cfefa
MT
51$snortsettings{'ENABLE_SNORT'} = 'off';
52$snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
53$snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
54$snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
55$snortsettings{'ACTION'} = '';
5a3e0dca 56$snortsettings{'RULES'} = '';
ac1cfefa
MT
57$snortsettings{'OINKCODE'} = '';
58$snortsettings{'INSTALLDATE'} = '';
ac1cfefa 59
298723b9
SS
60#Get GUI values
61&Header::getcgihash(\%cgiparams);
ac1cfefa 62
422204ff 63my $snortrulepath = "/etc/snort/rules";
e5738079 64my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
395e3b90
MT
65my $restartsnortrequired = 0;
66my %snortrules;
0b89daee
SS
67my $errormessage;
68my $url;
395e3b90 69
500c5c55
SS
70# Try to determine if oinkmaster is running.
71my $oinkmaster_pid = `pidof oinkmaster.pl -x`;
72
73# If oinkmaster is running display output.
74if ($oinkmaster_pid) {
75 &working("$Lang::tr{'snort working'}");
76}
77
3da6e01b
SS
78## Grab all available snort rules and store them in the snortrules hash.
79#
422204ff
SS
80# Open snort rules directory and do a directory listing.
81opendir(DIR, $snortrulepath) or die $!;
82 # Loop through the direcory.
83 while (my $file = readdir(DIR)) {
84
85 # We only want files.
86 next unless (-f "$snortrulepath/$file");
87
88 # Ignore empty files.
89 next if (-z "$snortrulepath/$file");
90
3da6e01b 91 # Use a regular expression to find files ending in .rules
422204ff
SS
92 next unless ($file =~ m/\.rules$/);
93
3da6e01b
SS
94 # Ignore files which are not read-able.
95 next unless (-R "$snortrulepath/$file");
395e3b90 96
3da6e01b
SS
97 # Call subfunction to read-in rulefile and add rules to
98 # the snortrules hash.
99 &readrulesfile("$file");
395e3b90 100 }
395e3b90 101
3da6e01b 102closedir(DIR);
395e3b90 103
e5738079
SS
104# Gather used rulefiles.
105#
106# Check if the file for activated rulefiles is not empty.
107if(-f $snortusedrulefilesfile) {
108 # Open the file for used rulefile and read-in content.
109 open(FILE, $snortusedrulefilesfile) or die "Could not open $snortusedrulefilesfile. $!\n";
110
111 # Read-in content.
112 my @lines = <FILE>;
113
114 # Close file.
115 close(FILE);
116
117 # Loop through the array.
118 foreach my $line (@lines) {
119 # Remove newlines.
120 chomp($line);
121
122 # Skip comments.
123 next if ($line =~ /\#/);
124
125 # Skip blank lines.
126 next if ($line =~ /^\s*$/);
127
128 # Gather rule sid and message from the ruleline.
129 if ($line =~ /.*include \$RULE_PATH\/(.*)/) {
130 my $rulefile = $1;
131
132 # Add the rulefile to the %snortrules hash.
133 $snortrules{$rulefile}{'Rulefile'}{'State'} = "on";
134 }
135 }
136}
137
298723b9
SS
138# Save ruleset.
139if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
140 my $enabled_sids_file = "${General::swroot}/snort/oinkmaster-enabled-sids.conf";
141 my $disabled_sids_file = "${General::swroot}/snort/oinkmaster-disabled-sids.conf";
142
143 # Arrays to store sid which should be added to the corresponding files.
144 my @enabled_sids;
145 my @disabled_sids;
e5738079 146 my @enabled_rulefiles;
298723b9
SS
147
148 # Loop through the hash of snortrules.
149 foreach my $rulefile(keys %snortrules) {
e5738079
SS
150 # Check if the rulefile is enabled.
151 if ($cgiparams{$rulefile} eq "on") {
152 # Add rulefile to the array of enabled rulefiles.
153 push(@enabled_rulefiles, $rulefile);
b65b5ef3
SS
154
155 # Drop item from cgiparams hash.
156 delete $cgiparams{$rulefile};
e5738079 157 }
466c6779 158 }
e5738079 159
466c6779
SS
160 # Loop through the hash of snortrules.
161 foreach my $rulefile (keys %snortrules) {
298723b9
SS
162 # Loop through the single rules of the rulefile.
163 foreach my $sid (keys %{$snortrules{$rulefile}}) {
c51a044a
SS
164 # Skip the current sid if it is not numeric.
165 next unless ($sid =~ /\d+/ );
166
298723b9
SS
167 # Check if there exists a key in the cgiparams hash for this sid.
168 if (exists($cgiparams{$sid})) {
169 # Look if the rule is disabled.
170 if ($snortrules{$rulefile}{$sid}{'State'} eq "off") {
171 # Check if the state has been set to 'on'.
172 if ($cgiparams{$sid} eq "on") {
173 # Add the sid to the enabled_sids array.
174 push(@enabled_sids, $sid);
175
176 # Drop item from cgiparams hash.
60333473 177 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
178 }
179 }
180 } else {
181 # Look if the rule is enabled.
182 if ($snortrules{$rulefile}{$sid}{'State'} eq "on") {
183 # Check if the state is 'on' and should be disabled.
184 # In this case there is no entry
185 # for the sid in the cgiparams hash.
186 # Add it to the disabled_sids array.
187 push(@disabled_sids, $sid);
188
189 # Drop item from cgiparams hash.
60333473 190 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
191 }
192 }
193 }
194 }
195
37659505
SS
196 # Open enabled sid's file for writing.
197 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
298723b9 198
37659505
SS
199 # Write header to file.
200 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
298723b9 201
37659505
SS
202 # Check if the enabled_sids array contains any sid's.
203 if (@enabled_sids) {
298723b9
SS
204 # Loop through the array of enabled sids and write them to the file.
205 foreach my $sid (@enabled_sids) {
37659505 206 print FILE "enablesid $sid\n";
298723b9 207 }
298723b9
SS
208 }
209
37659505
SS
210 # Close file after writing.
211 close(FILE);
212
213 # Open disabled sid's file for writing.
214 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
298723b9 215
37659505
SS
216 # Write header to file.
217 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
298723b9 218
37659505
SS
219 # Check if the enabled_sids array contains any sid's.
220 if (@disabled_sids) {
221 # Loop through the array of disabled sids and write them to the file.
222 foreach my $sid (@disabled_sids) {
223 print FILE "disablesid $sid\n";
224 }
225 }
298723b9 226
37659505
SS
227 # Close file after writing.
228 close(FILE);
e5738079
SS
229
230 # Open file for used rulefiles.
37659505 231 open (FILE, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
e5738079
SS
232
233 # Write header to file.
234 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
235
236 # Check if the enabled_rulefiles array contains any entries.
237 if (@enabled_rulefiles) {
238 # Loop through the array of rulefiles which should be loaded and write the to the file.
239 foreach my $file (@enabled_rulefiles) {
240 print FILE "include \$RULE_PATH/$file\n";
241 }
242 }
243
244 # Close file after writing.
245 close(FILE);
52599865
SS
246
247 # Call oinkmaster to alter the ruleset.
248 &oinkmaster();
249
250# Download new ruleset.
251} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
252 # Local var.
253 my $return;
254
255 # Call diskfree to gather the free disk space of /var.
256 my @df = `/bin/df -B M /var`;
257
258 # Loop through the output.
259 foreach my $line (@df) {
260 # Ignore header line.
261 next if $line =~ m/^Filesystem/;
262
263 # Search for a line with the device information.
264 if ($line =~ m/dev/ ) {
265 # Split the line into single pieces.
266 my @values = split(' ', $line);
267 my ($filesystem, $blocks, $used, $available, $used_perenctage, $mounted_on) = @values;
268
269 # Check if the available disk space is more than 300MB.
270 if ($available < 300) {
271 # If there is not enough space, print out an error message.
272 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
273 } else {
274 # Call subfunction to download the ruleset.
275 &downloadrulesfile();
276
277 # Sleep for 3 seconds.
278 sleep(3);
279
280 # Gather return of the external wget.
281 $return = `cat /var/tmp/log 2>/dev/null`;
282 }
283
284 # Check if there was an error.
285 if ($return =~ "ERROR") {
286 # Store error message for display.
287 $errormessage = "<br /><pre>".$return."</pre>";
288 } else {
289 # Call subfunction to launch oinkmaster.
290 &oinkmaster();
291
292 # Sleep for 2 seconds.
293 sleep(2);
294 }
295 }
296 }
298723b9
SS
297}
298
52599865 299
a9a91e5f
MT
300if ($snortsettings{'OINKCODE'} ne "") {
301 $errormessage = $Lang::tr{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
ac1cfefa
MT
302}
303
a9a91e5f
MT
304if (!$errormessage) {
305 if ($snortsettings{'RULES'} eq 'subscripted') {
9d79aea2 306 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
a9a91e5f 307 } elsif ($snortsettings{'RULES'} eq 'registered') {
9d79aea2 308 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
a9a91e5f
MT
309 } elsif ($snortsettings{'RULES'} eq 'community') {
310 $url=" https://www.snort.org/rules/community";
ac1cfefa 311 } else {
a9a91e5f 312 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
f9c2147d 313 }
ac1cfefa 314
a9a91e5f
MT
315 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
316 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
317 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
318 {
319 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
320 } else {
321 unlink "${General::swroot}/snort/enable";
322 }
323 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
324 {
325 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
326 } else {
327 unlink "${General::swroot}/snort/enable_green";
328 }
329 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
330 {
331 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
332 } else {
333 unlink "${General::swroot}/snort/enable_blue";
334 }
335 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
336 {
337 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
338 } else {
339 unlink "${General::swroot}/snort/enable_orange";
340 }
341 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
342 {
343 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
344 } else {
345 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
346 }
395e3b90 347
a9a91e5f
MT
348 system('/usr/local/bin/snortctrl restart >/dev/null');
349 }
ac1cfefa
MT
350}
351
52599865
SS
352# Read-in snortsettings
353&General::readhash("${General::swroot}/snort/settings", \%snortsettings);
354
ac1cfefa
MT
355$checked{'ENABLE_SNORT'}{'off'} = '';
356$checked{'ENABLE_SNORT'}{'on'} = '';
357$checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
358$checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
359$checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
360$checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
361$checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
362$checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
363$checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
364$checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
365$checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
366$checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
5a3e0dca 367$selected{'RULES'}{'nothing'} = '';
5a3e0dca 368$selected{'RULES'}{'community'} = '';
a0fa489f 369$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
370$selected{'RULES'}{'registered'} = '';
371$selected{'RULES'}{'subscripted'} = '';
372$selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
ac1cfefa
MT
373
374&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
375
17726644
SS
376### Java Script ###
377print <<END
378<script>
379 // Tiny java script function to show/hide the rules
380 // of a given category.
381 function showhide(tblname) {
382 \$("#" + tblname).toggle();
383 }
384</script>
385END
386;
387
ac1cfefa
MT
388&Header::openbigbox('100%', 'left', '', $errormessage);
389
390if ($errormessage) {
391 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
392 print "<class name='base'>$errormessage\n";
393 print "&nbsp;</class>\n";
394 &Header::closebox();
395}
396
7cc8a0e5 397&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
ac1cfefa
MT
398print <<END
399<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
1b73b07e 400<tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
ac1cfefa
MT
401END
402;
403if ($netsettings{'BLUE_DEV'} ne '') {
1b73b07e 404 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
ac1cfefa
MT
405}
406if ($netsettings{'ORANGE_DEV'} ne '') {
1b73b07e 407 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
ac1cfefa 408}
1b73b07e 409 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
1b73b07e 410
ac1cfefa 411print <<END
1b73b07e 412</td></tr>
ac1cfefa 413<tr>
7cc8a0e5 414 <td><br><br></td>
ac1cfefa
MT
415</tr>
416<tr>
417 <td><b>$Lang::tr{'ids rules update'}</b></td>
418</tr>
419<tr>
5a3e0dca
MT
420 <td><select name='RULES'>
421 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 422 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
423 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
424 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
425 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
426 </select>
427 </td>
ac1cfefa
MT
428</tr>
429<tr>
430 <td><br />
8d29504c 431 $Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}<br /><br />
4935eb8b 432 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
ac1cfefa
MT
433 </td>
434</tr>
435<tr>
5a3e0dca 436 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
ac1cfefa
MT
437</tr>
438<tr>
7cc8a0e5 439 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
ac1cfefa
MT
440END
441;
42d9192e 442if ( -e "/var/tmp/snortrules.tar.gz"){
0972c650
CS
443 my @Info = stat("/var/tmp/snortrules.tar.gz");
444 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
ac1cfefa 445}
32810952
CS
446print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
447
ac1cfefa
MT
448print <<END
449</tr>
450</table>
7cc8a0e5 451<br><br>
ac1cfefa
MT
452<table width='100%'>
453<tr>
7cc8a0e5 454 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
ac1cfefa
MT
455</tr>
456</table>
457</form>
458END
459;
460
ac1cfefa 461&Header::closebox();
fbfdb241 462
f7fcd1c0 463&Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
298723b9
SS
464 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
465
f7fcd1c0 466 # Output display table for rule files
17726644 467 print "<table width='100%'>\n";
ce0e83b3 468
17726644
SS
469 # Local variable required for java script to show/hide
470 # rules of a rulefile.
471 my $rulesetcount = 1;
f7fcd1c0
SS
472
473 # Loop over each rule file
474 foreach my $rulefile (sort keys(%snortrules)) {
475 my $rulechecked = '';
476
f7fcd1c0 477 # Check if rule file is enabled
e5738079 478 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
f7fcd1c0
SS
479 $rulechecked = 'CHECKED';
480 }
3ffee04b 481
17726644
SS
482 # Table and rows for the rule files.
483 print"<tr>\n";
484 print"<td class='base' width='5%'>\n";
e5738079 485 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
17726644
SS
486 print"</td>\n";
487 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
488 print"<td class='base' width='5%' align='right'>\n";
489 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
490 print"</td>\n";
491 print"</tr>\n";
492
493 # Rows which will be hidden per default and will contain the single rules.
494 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
495 print"<td colspan='3'>\n";
496
497 # Local vars
498 my $lines;
499 my $rows;
500 my $col;
501
502 # New table for the single rules.
503 print "<table width='100%'>\n";
504
505 # Loop over rule file rules
506 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
f7fcd1c0 507 # Local vars
17726644 508 my $ruledefchecked = '';
f9c2147d 509
e5738079
SS
510 # Skip rulefile itself.
511 next if ($sid eq "Rulefile");
512
17726644
SS
513 # If 2 rules have been displayed, start a new row
514 if (($lines % 2) == 0) {
515 print "</tr><tr>\n";
3ffee04b 516
17726644
SS
517 # Increase rows by once.
518 $rows++;
519 }
f7fcd1c0 520
17726644
SS
521 # Colour lines.
522 if ($rows % 2) {
523 $col="bgcolor='$color{'color20'}'";
524 } else {
525 $col="bgcolor='$color{'color22'}'";
f7fcd1c0
SS
526 }
527
17726644 528 # Set rule state
298723b9 529 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
17726644 530 $ruledefchecked = 'CHECKED';
f7fcd1c0 531 }
3ffee04b 532
17726644
SS
533 # Create rule checkbox and display rule description
534 print "<td class='base' width='5%' align='right' $col>\n";
535 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
536 print "</td>\n";
537 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
538
539 # Increment rule count
540 $lines++;
541 }
542
543 # If do not have a second rule for row, create empty cell
544 if (($lines % 2) != 0) {
545 print "<td class='base'></td>";
f7fcd1c0
SS
546 }
547
548 # Close display table
17726644 549 print "</tr></table></td></tr>";
3ffee04b 550
17726644
SS
551 # Finished whith the rule file, increase count.
552 $rulesetcount++;
f7fcd1c0 553 }
17726644
SS
554
555 # Close display table
556 print "</table>";
557
f7fcd1c0 558print <<END
2999f1d2
CS
559<table width='100%'>
560<tr>
298723b9 561 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
3ffee04b
CS
562 &nbsp; <!-- space for future online help link -->
563 </td>
2999f1d2
CS
564</tr>
565</table>
298723b9 566</form>
3ffee04b
CS
567END
568;
f7fcd1c0 569&Header::closebox();
ac1cfefa
MT
570&Header::closebigbox();
571&Header::closepage();
572
500c5c55
SS
573sub working ($) {
574 my $message = $_[0];
575
576 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
577 &Header::openbigbox('100%', 'left', '', $errormessage);
578 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1'>" );
579 print <<END;
580 <table>
581 <tr>
582 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
583 <td>$message</td>
584 </tr>
585 <tr>
586 <td colspan='2' align='center'>
587 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
588 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
589 </form>
590 </tr>
591 </table>
592END
593 &Header::closebox();
594 &Header::closebigbox();
595 &Header::closepage();
596 exit;
a70d269a
SS
597}
598
ac1cfefa 599sub downloadrulesfile {
73231650
CS
600 my $peer;
601 my $peerport;
ac1cfefa 602
73231650 603 unlink("/var/tmp/log");
ac1cfefa
MT
604
605 unless (-e "${General::swroot}/red/active") {
606 $errormessage = $Lang::tr{'could not download latest updates'};
607 return undef;
608 }
609
ac1cfefa
MT
610 my %proxysettings=();
611 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
612
613 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
73231650 614 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
ac1cfefa
MT
615 }
616
73231650 617 if ($peer) {
1f606aef 618 system("wget -r --proxy=on --proxy-user=$proxysettings{'UPSTREAM_USER'} --proxy-passwd=$proxysettings{'UPSTREAM_PASSWORD'} -e http_proxy=http://$peer:$peerport/ -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
73231650 619 } else {
1f606aef 620 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
ac1cfefa 621 }
ac1cfefa 622}
3da6e01b 623
56dacb58
SS
624sub oinkmaster () {
625 # Call oinkmaster to generate ruleset.
626 system("/usr/local/bin/oinkmaster.pl -v -s -u file:///var/tmp/snortrules.tar.gz -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules >>/var/tmp/log 2>&1 &");
627}
628
3da6e01b
SS
629sub readrulesfile ($) {
630 my $rulefile = shift;
631
632 # Open rule file and read in contents
633 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
634
635 # Store file content in an array.
636 my @lines = <RULEFILE>;
637
638 # Close file.
639 close(RULEFILE);
640
641 # Loop over rule file contents
642 foreach my $line (@lines) {
643 # Remove whitespaces.
644 chomp $line;
645
646 # Skip blank lines.
647 next if ($line =~ /^\s*$/);
648
649 # Local vars.
650 my $sid;
651 my $msg;
652
653 # Gather rule sid and message from the ruleline.
654 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
655 $msg = $1;
656 $sid = $2;
657
658 # Check if a rule has been found.
659 if ($sid && $msg) {
660 # Add rule to the snortrules hash.
661 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
662
663 # Grab status of the rule. Check if ruleline starts with a "dash".
664 if ($line =~ /^\#/) {
665 # If yes, the rule is disabled.
298723b9 666 $snortrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
667 } else {
668 # Otherwise the rule is enabled.
298723b9 669 $snortrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
670 }
671 }
672 }
673 }
674}