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