]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Drop old code for uploading a 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 sub refreshpage{&Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='1;'>" );print "<center><img src='/images/clock.gif' alt='' /><br/><font color='red'>$Lang::tr{'pagerefresh'}</font></center>";&Header::closebox();}
34
35 my %color = ();
36 my %mainsettings = ();
37 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
38 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
39
40 my %snortsettings=();
41 my %checked=();
42 my %selected=();
43 my %netsettings=();
44 our $errormessage = '';
45 our $results = '';
46 our $tempdir = '';
47 our $url='';
48 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
49
50 &Header::showhttpheaders();
51
52 $snortsettings{'ENABLE_SNORT'} = 'off';
53 $snortsettings{'ENABLE_SNORT_GREEN'} = 'off';
54 $snortsettings{'ENABLE_SNORT_BLUE'} = 'off';
55 $snortsettings{'ENABLE_SNORT_ORANGE'} = 'off';
56 $snortsettings{'ACTION'} = '';
57 $snortsettings{'RULES'} = '';
58 $snortsettings{'OINKCODE'} = '';
59 $snortsettings{'INSTALLDATE'} = '';
60 $snortsettings{'FILE'} = '';
61
62 &Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
63
64 ####################### Added for snort rules control #################################
65 my $snortrulepath; # change to "/etc/snort/rules" - maniac
66 my @snortconfig;
67 my $restartsnortrequired = 0;
68 my %snortrules;
69 my $rule = '';
70 my $table1colour = '';
71 my $table2colour = '';
72 my $var = '';
73 my $value = '';
74 my $tmp = '';
75 my $linkedrulefile = '';
76 my $border = '';
77 my $checkboxname = '';
78
79 if (-e "/etc/snort/snort.conf") {
80
81
82 # Open snort.conf file, read it in, close it, and re-open for writing
83 open(FILE, "/etc/snort/snort.conf") or die 'Unable to read snort config file.';
84 @snortconfig = <FILE>;
85 close(FILE);
86 open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
87
88 my @rules = `cd /etc/snort/rules/ && ls *.rules 2>/dev/null`; # With this loop the rule might be display with correct rulepath set
89 foreach (@rules) {
90 chomp $_;
91 my $temp = join(";",@snortconfig);
92 if ( $temp =~ /$_/ ){next;}
93 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
94 }
95
96 # Loop over each line
97 foreach my $line (@snortconfig) {
98 # Trim the line
99 chomp $line;
100
101 # Check for a line with .rules
102 if ($line =~ /\.rules$/) {
103 # Parse out rule file name
104 $rule = $line;
105 $rule =~ s/\$RULE_PATH\///i;
106 $rule =~ s/ ?include ?//i;
107 $rule =~ s/\#//i;
108 my $snortrulepathrule = "$snortrulepath/$rule";
109
110 # Open rule file and read in contents
111 open(RULEFILE, "$snortrulepath/$rule") or die "Unable to read snort rule file for reading => $snortrulepath/$rule.";
112 my @snortrulefile = <RULEFILE>;
113 close(RULEFILE);
114 open(RULEFILE, ">$snortrulepath/$rule") or die "Unable to write snort rule file for writing $snortrulepath/$rule";
115
116 # Local vars
117 my $dashlinecnt = 0;
118 my $desclook = 1;
119 my $snortruledesc = '';
120 my %snortruledef = ();
121 my $rulecnt = 1;
122
123 # Loop over rule file contents
124 foreach my $ruleline (@snortrulefile) {
125 chomp $ruleline;
126
127 # If still looking for a description
128 if ($desclook) {
129 # If line does not start with a # anymore, then done looking for a description
130 if ($ruleline !~ /^\#/) {
131 $desclook = 0;
132 }
133
134 # If see more than one dashed line, (start to) create rule file description
135 if ($dashlinecnt > 1) {
136 # Check for a line starting with a #
137 if ($ruleline =~ /^\#/ and $ruleline !~ /^\#alert/) {
138 # Create tempruleline
139 my $tempruleline = $ruleline;
140
141 # Strip off # and clean up line
142 $tempruleline =~ s/\# ?//i;
143
144 # Check for part of a description
145 if ($snortruledesc eq '') {
146 $snortruledesc = $tempruleline;
147 } else {
148 $snortruledesc .= " $tempruleline";
149 }
150 } else {
151 # Must be done
152 $desclook = 0;
153 }
154 }
155
156 # If have a dashed line, increment count
157 if ($ruleline =~ /\# ?\-+/) {
158 $dashlinecnt++;
159 }
160 } else {
161 # Parse out rule file rule's message for display
162 if ($ruleline =~ /(msg\:\"[^\"]+\";)/) {
163 my $msg = '';
164 $msg = $1;
165 $msg =~ s/msg\:\"//i;
166 $msg =~ s/\";//i;
167 $snortruledef{$rulecnt}{'Description'} = $msg;
168
169 # Check for 'Save' and rule file displayed in query string
170 if (($snortsettings{'ACTION'} eq $Lang::tr{'update'}) && ($ENV{'QUERY_STRING'} =~ /$rule/i)) {
171 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
172 if ((($ruleline =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"})) || (($ruleline !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}))) {
173 $restartsnortrequired = 1;
174 }
175
176 # Strip out leading # from rule line
177 $ruleline =~ s/\# ?//i;
178
179 # Check if it does not exists (which means it is disabled), append a #
180 if (!exists $snortsettings{"SNORT_RULE_$rule\_$rulecnt"}) {
181 $ruleline = "#"." $ruleline";
182 }
183 }
184
185 # Check if ruleline does not begin with a #, so it is enabled
186 if ($ruleline !~ /^\#/) {
187 $snortruledef{$rulecnt++}{'State'} = 'Enabled';
188 } else {
189 # Otherwise it is disabled
190 $snortruledef{$rulecnt++}{'State'} = 'Disabled';
191 }
192 }
193 }
194
195 # Print ruleline to RULEFILE
196 print RULEFILE "$ruleline\n";
197 }
198
199 # Close RULEFILE
200 close(RULEFILE);
201
202 # Check for 'Save'
203 if ($snortsettings{'ACTION'} eq $Lang::tr{'update'}) {
204 # Check for a disable rule which is now enabled, or an enabled rule which is now disabled
205 if ((($line =~ /^\#/) && (exists $snortsettings{"SNORT_RULE_$rule"})) || (($line !~ /^\#/) && (!exists $snortsettings{"SNORT_RULE_$rule"}))) {
206 $restartsnortrequired = 1;
207 }
208
209 # Strip out leading # from rule line
210 $line =~ s/\# ?//i;
211
212 # Check if it does not exists (which means it is disabled), append a #
213 if (!exists $snortsettings{"SNORT_RULE_$rule"}) {
214 $line = "# $line";
215 }
216
217 }
218
219 # Check for rule state
220 if ($line =~ /^\#/) {
221 $snortrules{$rule}{"State"} = "Disabled";
222 } else {
223 $snortrules{$rule}{"State"} = "Enabled";
224 }
225
226 # Set rule description
227 $snortrules{$rule}{"Description"} = $snortruledesc;
228
229 # Loop over sorted rules
230 foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) {
231 $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'};
232 $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'};
233 }
234
235 $snortruledesc = '';
236 print FILE "$line\n";
237 } elsif ($line =~ /var RULE_PATH/) {
238 ($tmp, $tmp, $snortrulepath) = split(' ', $line);
239 print FILE "$line\n";
240 } else {
241 print FILE "$line\n";
242 }
243 }
244 close(FILE);
245
246 if ($restartsnortrequired) {
247 system('/usr/local/bin/snortctrl restart >/dev/null');
248 }
249 }
250
251 ####################### End added for snort rules control #################################
252
253 if ($snortsettings{'OINKCODE'} ne "") {
254 $errormessage = $Lang::tr{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
255 }
256
257 if (!$errormessage) {
258 if ($snortsettings{'RULES'} eq 'subscripted') {
259 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
260 } elsif ($snortsettings{'RULES'} eq 'registered') {
261 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
262 } elsif ($snortsettings{'RULES'} eq 'community') {
263 $url=" https://www.snort.org/rules/community";
264 } else {
265 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
266 }
267
268 if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) {
269 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
270 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
271 {
272 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
273 } else {
274 unlink "${General::swroot}/snort/enable";
275 }
276 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
277 {
278 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
279 } else {
280 unlink "${General::swroot}/snort/enable_green";
281 }
282 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
283 {
284 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
285 } else {
286 unlink "${General::swroot}/snort/enable_blue";
287 }
288 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
289 {
290 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
291 } else {
292 unlink "${General::swroot}/snort/enable_orange";
293 }
294 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
295 {
296 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
297 } else {
298 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
299 }
300
301 system('/usr/local/bin/snortctrl restart >/dev/null');
302 }
303
304 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
305 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
306
307 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
308 my @df = `/bin/df -B M /var`;
309 foreach my $line (@df) {
310 next if $line =~ m/^Filesystem/;
311 my $return;
312
313 if ($line =~ m/dev/ ) {
314 $line =~ m/^.* (\d+)M.*$/;
315 my @temp = split(/ +/,$line);
316 if ($1<300) {
317 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
318 } else {
319 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
320 &downloadrulesfile();
321 sleep(3);
322 $return = `cat /var/tmp/log 2>/dev/null`;
323
324 }
325
326 if ($return =~ "ERROR") {
327 $errormessage = "<br /><pre>".$return."</pre>";
328 } else {
329 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 &");
330 sleep(2);
331 }
332 }
333 }
334 }
335 }
336 }
337
338 $checked{'ENABLE_SNORT'}{'off'} = '';
339 $checked{'ENABLE_SNORT'}{'on'} = '';
340 $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
341 $checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
342 $checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
343 $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
344 $checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
345 $checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
346 $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
347 $checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
348 $checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
349 $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
350 $selected{'RULES'}{'nothing'} = '';
351 $selected{'RULES'}{'community'} = '';
352 $selected{'RULES'}{'emerging'} = '';
353 $selected{'RULES'}{'registered'} = '';
354 $selected{'RULES'}{'subscripted'} = '';
355 $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
356
357 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
358
359 ####################### Added for snort rules control #################################
360 print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
361 print <<END
362 <style type="text/css">
363 <!--
364 .section {
365 border: groove;
366 }
367 .row1color {
368 border: ridge;
369 background-color: $color{'color22'};
370 }
371 .row2color {
372 border: ridge;
373 background-color: $color{'color20'};
374 }
375 .rowselected {
376 border: double #FF0000;
377 background-color: #DCDCDC;
378 }
379 -->
380 </style>
381 END
382 ;
383 ####################### End added for snort rules control #################################
384
385 &Header::openbigbox('100%', 'left', '', $errormessage);
386
387 ###############
388 # DEBUG DEBUG
389 # &Header::openbox('100%', 'left', 'DEBUG');
390 # my $debugCount = 0;
391 # foreach my $line (sort keys %snortsettings) {
392 # print "$line = $snortsettings{$line}<br />\n";
393 # $debugCount++;
394 # }
395 # print "&nbsp;Count: $debugCount\n";
396 # &Header::closebox();
397 # DEBUG DEBUG
398 ###############
399
400 if ($errormessage) {
401 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
402 print "<class name='base'>$errormessage\n";
403 print "&nbsp;</class>\n";
404 &Header::closebox();
405 }
406
407 my $return = `pidof oinkmaster.pl -x`;
408 chomp($return);
409 if ($return) {
410 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
411 print <<END;
412 <table>
413 <tr><td>
414 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />&nbsp;
415 <td>
416 $Lang::tr{'snort working'}
417 <tr><td colspan='2' align='center'>
418 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
419 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
420 </form>
421 <tr><td colspan='2' align='left'><pre>
422 END
423 my @output = `tail -20 /var/tmp/log`;
424 foreach (@output) {
425 print "$_";
426 }
427 print <<END;
428 </pre>
429 </table>
430 END
431 &Header::closebox();
432 &Header::closebigbox();
433 &Header::closepage();
434 exit;
435 refreshpage();
436 }
437
438 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
439 print <<END
440 <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
441 <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
442 END
443 ;
444 if ($netsettings{'BLUE_DEV'} ne '') {
445 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
446 }
447 if ($netsettings{'ORANGE_DEV'} ne '') {
448 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
449 }
450 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
451
452 print <<END
453 </td></tr>
454 <tr>
455 <td><br><br></td>
456 </tr>
457 <tr>
458 <td><b>$Lang::tr{'ids rules update'}</b></td>
459 </tr>
460 <tr>
461 <td><select name='RULES'>
462 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
463 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
464 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
465 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
466 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
467 </select>
468 </td>
469 </tr>
470 <tr>
471 <td><br />
472 $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 />
473 $Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}
474 </td>
475 </tr>
476 <tr>
477 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
478 </tr>
479 <tr>
480 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
481 END
482 ;
483 if ( -e "/var/tmp/snortrules.tar.gz"){
484 my @Info = stat("/var/tmp/snortrules.tar.gz");
485 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
486 }
487 print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
488
489 print <<END
490 </tr>
491 </table>
492 <br><br>
493 <table width='100%'>
494 <tr>
495 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
496 </tr>
497 </table>
498 </form>
499 END
500 ;
501
502 if ($results ne '') {
503 print "$results";
504 }
505
506 &Header::closebox();
507
508 ####################### Added for snort rules control #################################
509 if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) {
510 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
511 # Output display table for rule files
512 print "<table width='100%'><tr><td valign='top'><table>";
513
514 print "<form method='post'>";
515
516 # Local vars
517 my $ruledisplaycnt = 1;
518 my $rulecnt = keys %snortrules;
519 $rulecnt++;
520 $rulecnt = $rulecnt / 2;
521
522 # Loop over each rule file
523 foreach my $rulefile (sort keys(%snortrules)) {
524 my $rulechecked = '';
525
526 # Hide inkompatible Block rules
527 if ($rulefile =~'-BLOCK.rules') {
528 next;
529 }
530
531 # Check if reached half-way through rule file rules to start new column
532 if ($ruledisplaycnt > $rulecnt) {
533 print "</table></td><td valign='top'><table>";
534 $ruledisplaycnt = 0;
535 }
536
537 # Check if rule file is enabled
538 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
539 $rulechecked = 'CHECKED';
540 }
541
542 # Create rule file link, vars array, and display flag
543 my $rulefilelink = "?RULEFILE=$rulefile";
544 my $rulefiletoclose = '';
545 my @queryvars = ();
546 my $displayrulefilerules = 0;
547
548 # Check for passed in query string
549 if ($ENV{'QUERY_STRING'}) {
550 # Split out vars
551 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
552
553 # Loop over values
554 foreach $value (@queryvars) {
555 # Split out var pairs
556 ($var, $linkedrulefile) = split(/=/, $value);
557
558 # Check if var is 'RULEFILE'
559 if ($var eq 'RULEFILE') {
560 # Check if rulefile equals linkedrulefile
561 if ($rulefile eq $linkedrulefile) {
562 # Set display flag
563 $displayrulefilerules = 1;
564
565 # Strip out rulefile from rulefilelink
566 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
567 } else {
568 # Add linked rule file to rulefilelink
569 $rulefilelink .= "&RULEFILE=$linkedrulefile";
570 }
571 }
572 }
573 }
574
575 # Strip out extra & & ? from rulefilelink
576 $rulefilelink =~ s/^\?\&/\?/i;
577
578 # Check for a single '?' and replace with page for proper link display
579 if ($rulefilelink eq '?') {
580 $rulefilelink = "ids.cgi";
581 }
582
583 # Output rule file name and checkbox
584 print "<tr><td class='base' valign='top'><input type='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <a href='$rulefilelink'>$rulefile</a></td></tr>";
585 print "<tr><td class='base' valign='top'>";
586
587 # Check for empty 'Description'
588 if ($snortrules{$rulefile}{'Description'} eq '') {
589 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
590 } else {
591 # Output rule file 'Description'
592 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
593 }
594
595 # Check for display flag
596 if ($displayrulefilerules) {
597 # Rule file definition rule display
598 print "<tr><td class='base' valign='top'><table border='0'><tr>";
599
600 # Local vars
601 my $ruledefdisplaycnt = 0;
602 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
603 $ruledefcnt++;
604 $ruledefcnt = $ruledefcnt / 2;
605
606 # Loop over rule file rules
607 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
608 # Local vars
609 my $ruledefchecked = '';
610
611 # If have display 2 rules, start new row
612 if (($ruledefdisplaycnt % 2) == 0) {
613 print "</tr><tr>";
614 $ruledefdisplaycnt = 0;
615 }
616
617 # Check for rules state
618 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
619 $ruledefchecked = 'CHECKED';
620 }
621
622 # Create rule file rule's checkbox
623 $checkboxname = "SNORT_RULE_$rulefile";
624 $checkboxname .= "_$ruledef";
625 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
626
627 # Increment count
628 $ruledefdisplaycnt++;
629 }
630
631 # If do not have second rule for row, create empty cell
632 if (($ruledefdisplaycnt % 2) != 0) {
633 print "<td class='base'></td>";
634 }
635
636 # Close display table
637 print "</tr></table></td></tr>";
638 }
639
640 # Close display table
641 print "</table>";
642
643 # Increment ruledisplaycnt
644 $ruledisplaycnt++;
645 }
646 print "</td></tr></table></td></tr></table>";
647 print <<END
648 <table width='100%'>
649 <tr>
650 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
651 &nbsp; <!-- space for future online help link -->
652 </td>
653 </tr>
654 </table>
655 </form>
656 END
657 ;
658 &Header::closebox();
659 }
660
661 ####################### End added for snort rules control #################################
662 &Header::closebigbox();
663 &Header::closepage();
664
665 sub downloadrulesfile {
666 my $peer;
667 my $peerport;
668
669 unlink("/var/tmp/log");
670
671 unless (-e "${General::swroot}/red/active") {
672 $errormessage = $Lang::tr{'could not download latest updates'};
673 return undef;
674 }
675
676 my %proxysettings=();
677 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
678
679 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
680 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
681 }
682
683 if ($peer) {
684 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");
685 } else {
686 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
687 }
688 }