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