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