]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Remove logfile after wget has successfully downloaded the ruleset
[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 use File::Copy;
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %netsettings = ();
36 my %snortsettings=();
37 my %cgiparams=();
38 my %checked=();
39 my %selected=();
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.
46 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
47
48 &Header::showhttpheaders();
49
50 # Default settings for snort.
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'} = '';
56 $snortsettings{'RULES'} = '';
57 $snortsettings{'OINKCODE'} = '';
58 $snortsettings{'INSTALLDATE'} = '';
59
60 #Get GUI values
61 &Header::getcgihash(\%cgiparams);
62
63 my $snortrulepath = "/etc/snort/rules";
64 my $snortusedrulefilesfile = "${General::swroot}/snort/snort-used-rulefiles.conf";
65 my $restartsnortrequired = 0;
66 my %snortrules;
67 my $errormessage;
68 my $url;
69
70 # Try to determine if oinkmaster is running.
71 my $oinkmaster_pid = `pidof oinkmaster.pl -x`;
72
73 # If oinkmaster is running display output.
74 if ($oinkmaster_pid) {
75 &working("$Lang::tr{'snort working'}");
76 }
77
78 ## Grab all available snort rules and store them in the snortrules hash.
79 #
80 # Open snort rules directory and do a directory listing.
81 opendir(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
91 # Use a regular expression to find files ending in .rules
92 next unless ($file =~ m/\.rules$/);
93
94 # Ignore files which are not read-able.
95 next unless (-R "$snortrulepath/$file");
96
97 # Call subfunction to read-in rulefile and add rules to
98 # the snortrules hash.
99 &readrulesfile("$file");
100 }
101
102 closedir(DIR);
103
104 # Gather used rulefiles.
105 #
106 # Check if the file for activated rulefiles is not empty.
107 if(-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
138 # Save ruleset.
139 if ($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;
146 my @enabled_rulefiles;
147
148 # Loop through the hash of snortrules.
149 foreach my $rulefile(keys %snortrules) {
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);
154
155 # Drop item from cgiparams hash.
156 delete $cgiparams{$rulefile};
157 }
158 }
159
160 # Loop through the hash of snortrules.
161 foreach my $rulefile (keys %snortrules) {
162 # Loop through the single rules of the rulefile.
163 foreach my $sid (keys %{$snortrules{$rulefile}}) {
164 # Skip the current sid if it is not numeric.
165 next unless ($sid =~ /\d+/ );
166
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.
177 delete $cgiparams{$rulefile}{$sid};
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.
190 delete $cgiparams{$rulefile}{$sid};
191 }
192 }
193 }
194 }
195
196 # Open enabled sid's file for writing.
197 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
198
199 # Write header to file.
200 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
201
202 # Check if the enabled_sids array contains any sid's.
203 if (@enabled_sids) {
204 # Loop through the array of enabled sids and write them to the file.
205 foreach my $sid (@enabled_sids) {
206 print FILE "enablesid $sid\n";
207 }
208 }
209
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";
215
216 # Write header to file.
217 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
218
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 }
226
227 # Close file after writing.
228 close(FILE);
229
230 # Open file for used rulefiles.
231 open (FILE, ">$snortusedrulefilesfile") or die "Could not write to $snortusedrulefilesfile. $!\n";
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);
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 # Remove logfile.
290 unlink("/var/tmp/log");
291
292 # Call subfunction to launch oinkmaster.
293 &oinkmaster();
294
295 # Sleep for 2 seconds.
296 sleep(2);
297 }
298 }
299 }
300 }
301
302
303 if ($snortsettings{'OINKCODE'} ne "") {
304 $errormessage = $Lang::tr{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
305 }
306
307 if (!$errormessage) {
308 if ($snortsettings{'RULES'} eq 'subscripted') {
309 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
310 } elsif ($snortsettings{'RULES'} eq 'registered') {
311 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
312 } elsif ($snortsettings{'RULES'} eq 'community') {
313 $url=" https://www.snort.org/rules/community";
314 } else {
315 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
316 }
317
318 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
319 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
320 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
321 {
322 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
323 } else {
324 unlink "${General::swroot}/snort/enable";
325 }
326 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
327 {
328 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
329 } else {
330 unlink "${General::swroot}/snort/enable_green";
331 }
332 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
333 {
334 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
335 } else {
336 unlink "${General::swroot}/snort/enable_blue";
337 }
338 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
339 {
340 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
341 } else {
342 unlink "${General::swroot}/snort/enable_orange";
343 }
344 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
345 {
346 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
347 } else {
348 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
349 }
350
351 system('/usr/local/bin/snortctrl restart >/dev/null');
352 }
353 }
354
355 # Read-in snortsettings
356 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
357
358 $checked{'ENABLE_SNORT'}{'off'} = '';
359 $checked{'ENABLE_SNORT'}{'on'} = '';
360 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
361 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
362 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
363 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
364 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
365 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
366 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
367 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
368 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
369 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
370 $selected{'RULES'}{'nothing'} = '';
371 $selected{'RULES'}{'community'} = '';
372 $selected{'RULES'}{'emerging'} = '';
373 $selected{'RULES'}{'registered'} = '';
374 $selected{'RULES'}{'subscripted'} = '';
375 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
376
377 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
378
379 ### Java Script ###
380 print <<END
381 <script>
382 // Tiny java script function to show/hide the rules
383 // of a given category.
384 function showhide(tblname) {
385 \$("#" + tblname).toggle();
386 }
387 </script>
388 END
389 ;
390
391 &Header::openbigbox('100%', 'left', '', $errormessage);
392
393 if ($errormessage) {
394 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
395 print "<class name='base'>$errormessage\n";
396 print "&nbsp;</class>\n";
397 &Header::closebox();
398 }
399
400 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
401 print <<END
402 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
403 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
404 END
405 ;
406 if ($netsettings{'BLUE_DEV'} ne '') {
407 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
408 }
409 if ($netsettings{'ORANGE_DEV'} ne '') {
410 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
411 }
412 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
413
414 print <<END
415 </td></tr>
416 <tr>
417 <td><br><br></td>
418 </tr>
419 <tr>
420 <td><b>$Lang::tr{'ids rules update'}</b></td>
421 </tr>
422 <tr>
423 <td><select name='RULES'>
424 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
425 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
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>
430 </td>
431 </tr>
432 <tr>
433 <td><br />
434 $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 />
435 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
436 </td>
437 </tr>
438 <tr>
439 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
440 </tr>
441 <tr>
442 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
443 END
444 ;
445 if ( -e "/var/tmp/snortrules.tar.gz"){
446 my @Info = stat("/var/tmp/snortrules.tar.gz");
447 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
448 }
449 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
450
451 print <<END
452 </tr>
453 </table>
454 <br><br>
455 <table width='100%'>
456 <tr>
457 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
458 </tr>
459 </table>
460 </form>
461 END
462 ;
463
464 &Header::closebox();
465
466 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
467 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
468
469 # Output display table for rule files
470 print "<table width='100%'>\n";
471
472 # Local variable required for java script to show/hide
473 # rules of a rulefile.
474 my $rulesetcount = 1;
475
476 # Loop over each rule file
477 foreach my $rulefile (sort keys(%snortrules)) {
478 my $rulechecked = '';
479
480 # Check if rule file is enabled
481 if ($snortrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
482 $rulechecked = 'CHECKED';
483 }
484
485 # Table and rows for the rule files.
486 print"<tr>\n";
487 print"<td class='base' width='5%'>\n";
488 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
489 print"</td>\n";
490 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
491 print"<td class='base' width='5%' align='right'>\n";
492 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
493 print"</td>\n";
494 print"</tr>\n";
495
496 # Rows which will be hidden per default and will contain the single rules.
497 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
498 print"<td colspan='3'>\n";
499
500 # Local vars
501 my $lines;
502 my $rows;
503 my $col;
504
505 # New table for the single rules.
506 print "<table width='100%'>\n";
507
508 # Loop over rule file rules
509 foreach my $sid (sort {$a <=> $b} keys(%{$snortrules{$rulefile}})) {
510 # Local vars
511 my $ruledefchecked = '';
512
513 # Skip rulefile itself.
514 next if ($sid eq "Rulefile");
515
516 # If 2 rules have been displayed, start a new row
517 if (($lines % 2) == 0) {
518 print "</tr><tr>\n";
519
520 # Increase rows by once.
521 $rows++;
522 }
523
524 # Colour lines.
525 if ($rows % 2) {
526 $col="bgcolor='$color{'color20'}'";
527 } else {
528 $col="bgcolor='$color{'color22'}'";
529 }
530
531 # Set rule state
532 if ($snortrules{$rulefile}{$sid}{'State'} eq 'on') {
533 $ruledefchecked = 'CHECKED';
534 }
535
536 # Create rule checkbox and display rule description
537 print "<td class='base' width='5%' align='right' $col>\n";
538 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
539 print "</td>\n";
540 print "<td class='base' width='45%' $col>$snortrules{$rulefile}{$sid}{'Description'}</td>";
541
542 # Increment rule count
543 $lines++;
544 }
545
546 # If do not have a second rule for row, create empty cell
547 if (($lines % 2) != 0) {
548 print "<td class='base'></td>";
549 }
550
551 # Close display table
552 print "</tr></table></td></tr>";
553
554 # Finished whith the rule file, increase count.
555 $rulesetcount++;
556 }
557
558 # Close display table
559 print "</table>";
560
561 print <<END
562 <table width='100%'>
563 <tr>
564 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
565 &nbsp; <!-- space for future online help link -->
566 </td>
567 </tr>
568 </table>
569 </form>
570 END
571 ;
572 &Header::closebox();
573 &Header::closebigbox();
574 &Header::closepage();
575
576 sub working ($) {
577 my $message = $_[0];
578
579 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
580 &Header::openbigbox('100%', 'left', '', $errormessage);
581 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1'>" );
582 print <<END;
583 <table>
584 <tr>
585 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
586 <td>$message</td>
587 </tr>
588 <tr>
589 <td colspan='2' align='center'>
590 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
591 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
592 </form>
593 </tr>
594 </table>
595 END
596 &Header::closebox();
597 &Header::closebigbox();
598 &Header::closepage();
599 exit;
600 }
601
602 sub downloadrulesfile {
603 my $peer;
604 my $peerport;
605
606 unlink("/var/tmp/log");
607
608 unless (-e "${General::swroot}/red/active") {
609 $errormessage = $Lang::tr{'could not download latest updates'};
610 return undef;
611 }
612
613 my %proxysettings=();
614 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
615
616 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
617 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
618 }
619
620 if ($peer) {
621 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");
622 } else {
623 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
624 }
625 }
626
627 sub oinkmaster () {
628 # Call oinkmaster to generate ruleset.
629 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 &");
630 }
631
632 sub readrulesfile ($) {
633 my $rulefile = shift;
634
635 # Open rule file and read in contents
636 open(RULEFILE, "$snortrulepath/$rulefile") or die "Unable to read $rulefile!";
637
638 # Store file content in an array.
639 my @lines = <RULEFILE>;
640
641 # Close file.
642 close(RULEFILE);
643
644 # Loop over rule file contents
645 foreach my $line (@lines) {
646 # Remove whitespaces.
647 chomp $line;
648
649 # Skip blank lines.
650 next if ($line =~ /^\s*$/);
651
652 # Local vars.
653 my $sid;
654 my $msg;
655
656 # Gather rule sid and message from the ruleline.
657 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
658 $msg = $1;
659 $sid = $2;
660
661 # Check if a rule has been found.
662 if ($sid && $msg) {
663 # Add rule to the snortrules hash.
664 $snortrules{$rulefile}{$sid}{'Description'} = $msg;
665
666 # Grab status of the rule. Check if ruleline starts with a "dash".
667 if ($line =~ /^\#/) {
668 # If yes, the rule is disabled.
669 $snortrules{$rulefile}{$sid}{'State'} = "off";
670 } else {
671 # Otherwise the rule is enabled.
672 $snortrules{$rulefile}{$sid}{'State'} = "on";
673 }
674 }
675 }
676 }
677 }