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