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