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