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