]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids.cgi: Drop old code for uploading a ruleset
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
b6f571fa 5# Copyright (C) 2007-2015 IPFire Team <info@ipfire.org> #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
ac1cfefa
MT
22use strict;
23
24# enable only the following on debugging purpose
90c2e164
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
73231650 27use File::Copy;
ac1cfefa 28
986e08d9 29require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
32
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 #################################
cf29614f 65my $snortrulepath; # change to "/etc/snort/rules" - maniac
395e3b90
MT
66my @snortconfig;
67my $restartsnortrequired = 0;
68my %snortrules;
69my $rule = '';
70my $table1colour = '';
71my $table2colour = '';
72my $var = '';
73my $value = '';
74my $tmp = '';
75my $linkedrulefile = '';
76my $border = '';
77my $checkboxname = '';
78
79if (-e "/etc/snort/snort.conf") {
2999f1d2
CS
80
81
395e3b90
MT
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);
d192af92 86 open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
395e3b90 87
324bb888 88 my @rules = `cd /etc/snort/rules/ && ls *.rules 2>/dev/null`; # With this loop the rule might be display with correct rulepath set
2999f1d2
CS
89 foreach (@rules) {
90 chomp $_;
91 my $temp = join(";",@snortconfig);
92 if ( $temp =~ /$_/ ){next;}
93 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
94 }
f9c2147d 95
395e3b90
MT
96 # Loop over each line
97 foreach my $line (@snortconfig) {
429f7008 98 # Trim the line
395e3b90
MT
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 #
61196506 137 if ($ruleline =~ /^\#/ and $ruleline !~ /^\#alert/) {
395e3b90
MT
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 }
f9c2147d 216
395e3b90
MT
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) {
18322edf 247 system('/usr/local/bin/snortctrl restart >/dev/null');
395e3b90
MT
248 }
249}
250
251####################### End added for snort rules control #################################
252
a9a91e5f
MT
253if ($snortsettings{'OINKCODE'} ne "") {
254 $errormessage = $Lang::tr{'invalid input for oink code'} unless ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/);
ac1cfefa
MT
255}
256
a9a91e5f
MT
257if (!$errormessage) {
258 if ($snortsettings{'RULES'} eq 'subscripted') {
9d79aea2 259 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
a9a91e5f 260 } elsif ($snortsettings{'RULES'} eq 'registered') {
9d79aea2 261 $url=" https://www.snort.org/rules/snortrules-snapshot-29111.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
a9a91e5f
MT
262 } elsif ($snortsettings{'RULES'} eq 'community') {
263 $url=" https://www.snort.org/rules/community";
ac1cfefa 264 } else {
a9a91e5f 265 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
f9c2147d 266 }
ac1cfefa 267
a9a91e5f
MT
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 }
395e3b90 300
a9a91e5f
MT
301 system('/usr/local/bin/snortctrl restart >/dev/null');
302 }
a27c40a0 303
a9a91e5f 304 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
ac1cfefa 305 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
ac1cfefa 306
fbd43017 307 if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) {
a9a91e5f
MT
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
73231650 324 }
73231650 325
a9a91e5f 326 if ($return =~ "ERROR") {
73231650
CS
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);
32810952 331 }
a9a91e5f 332 }
ac1cfefa 333 }
ac1cfefa
MT
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'";
5a3e0dca 350$selected{'RULES'}{'nothing'} = '';
5a3e0dca 351$selected{'RULES'}{'community'} = '';
a0fa489f 352$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
353$selected{'RULES'}{'registered'} = '';
354$selected{'RULES'}{'subscripted'} = '';
355$selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
ac1cfefa
MT
356
357&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
358
395e3b90 359####################### Added for snort rules control #################################
2c01f467 360print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
395e3b90 361print <<END
e6d8a421 362<style type="text/css">
395e3b90
MT
363<!--
364.section {
365 border: groove;
366}
367.row1color {
368 border: ridge;
f2fdd0c1 369 background-color: $color{'color22'};
395e3b90
MT
370}
371.row2color {
372 border: ridge;
f2fdd0c1 373 background-color: $color{'color20'};
395e3b90
MT
374}
375.rowselected {
376 border: double #FF0000;
377 background-color: #DCDCDC;
378}
379-->
e6d8a421 380</style>
395e3b90
MT
381END
382;
383####################### End added for snort rules control #################################
384
ac1cfefa
MT
385&Header::openbigbox('100%', 'left', '', $errormessage);
386
90c2e164
CS
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
ac1cfefa
MT
400if ($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
73231650
CS
407my $return = `pidof oinkmaster.pl -x`;
408chomp($return);
409if ($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'}'>
f8aa0679 419 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
73231650
CS
420 </form>
421 <tr><td colspan='2' align='left'><pre>
422END
423 my @output = `tail -20 /var/tmp/log`;
424 foreach (@output) {
425 print "$_";
426 }
427 print <<END;
428 </pre>
429 </table>
430END
431 &Header::closebox();
432 &Header::closebigbox();
433 &Header::closepage();
434 exit;
435 refreshpage();
436}
437
7cc8a0e5 438&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
ac1cfefa
MT
439print <<END
440<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
1b73b07e 441<tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
ac1cfefa
MT
442END
443;
444if ($netsettings{'BLUE_DEV'} ne '') {
1b73b07e 445 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
ac1cfefa
MT
446}
447if ($netsettings{'ORANGE_DEV'} ne '') {
1b73b07e 448 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
ac1cfefa 449}
1b73b07e 450 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
1b73b07e 451
ac1cfefa 452print <<END
1b73b07e 453</td></tr>
ac1cfefa 454<tr>
7cc8a0e5 455 <td><br><br></td>
ac1cfefa
MT
456</tr>
457<tr>
458 <td><b>$Lang::tr{'ids rules update'}</b></td>
459</tr>
460<tr>
5a3e0dca
MT
461 <td><select name='RULES'>
462 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 463 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
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>
ac1cfefa
MT
469</tr>
470<tr>
471 <td><br />
8d29504c 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 />
4935eb8b 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'}
ac1cfefa
MT
474 </td>
475</tr>
476<tr>
5a3e0dca 477 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
ac1cfefa
MT
478</tr>
479<tr>
7cc8a0e5 480 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
ac1cfefa
MT
481END
482;
42d9192e 483if ( -e "/var/tmp/snortrules.tar.gz"){
0972c650
CS
484 my @Info = stat("/var/tmp/snortrules.tar.gz");
485 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
ac1cfefa 486}
32810952
CS
487print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
488
ac1cfefa
MT
489print <<END
490</tr>
491</table>
7cc8a0e5 492<br><br>
ac1cfefa
MT
493<table width='100%'>
494<tr>
7cc8a0e5 495 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
ac1cfefa
MT
496</tr>
497</table>
498</form>
499END
500;
501
502if ($results ne '') {
503 print "$results";
504}
505
506&Header::closebox();
fbfdb241 507
395e3b90 508####################### Added for snort rules control #################################
3ffee04b
CS
509if ( -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
631b67b7 512 print "<table width='100%'><tr><td valign='top'><table>";
f9c2147d 513
3ffee04b
CS
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
ce0e83b3
AF
526 # Hide inkompatible Block rules
527 if ($rulefile =~'-BLOCK.rules') {
528 next;
529 }
530
3ffee04b
CS
531 # Check if reached half-way through rule file rules to start new column
532 if ($ruledisplaycnt > $rulecnt) {
631b67b7 533 print "</table></td><td valign='top'><table>";
3ffee04b 534 $ruledisplaycnt = 0;
2999f1d2 535 }
3ffee04b
CS
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
2999f1d2 543 my $rulefilelink = "?RULEFILE=$rulefile";
3ffee04b
CS
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
2999f1d2 551 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
3ffee04b
CS
552
553 # Loop over values
554 foreach $value (@queryvars) {
555 # Split out var pairs
2999f1d2 556 ($var, $linkedrulefile) = split(/=/, $value);
3ffee04b
CS
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 }
2999f1d2 574
3ffee04b
CS
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
631b67b7
AH
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'>";
3ffee04b
CS
586
587 # Check for empty 'Description'
588 if ($snortrules{$rulefile}{'Description'} eq '') {
e6d8a421 589 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
3ffee04b
CS
590 } else {
591 # Output rule file 'Description'
e6d8a421 592 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
3ffee04b
CS
593 }
594
595 # Check for display flag
596 if ($displayrulefilerules) {
597 # Rule file definition rule display
631b67b7 598 print "<tr><td class='base' valign='top'><table border='0'><tr>";
3ffee04b
CS
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) {
e6d8a421 613 print "</tr><tr>";
3ffee04b
CS
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";
e6d8a421 625 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
3ffee04b
CS
626
627 # Increment count
628 $ruledefdisplaycnt++;
629 }
f9c2147d 630
3ffee04b
CS
631 # If do not have second rule for row, create empty cell
632 if (($ruledefdisplaycnt % 2) != 0) {
e6d8a421 633 print "<td class='base'></td>";
3ffee04b
CS
634 }
635
636 # Close display table
e6d8a421 637 print "</tr></table></td></tr>";
2999f1d2 638 }
3ffee04b
CS
639
640 # Close display table
e6d8a421 641 print "</table>";
3ffee04b
CS
642
643 # Increment ruledisplaycnt
2999f1d2 644 $ruledisplaycnt++;
3ffee04b 645 }
e6d8a421 646 print "</td></tr></table></td></tr></table>";
3ffee04b 647 print <<END
2999f1d2
CS
648<table width='100%'>
649<tr>
7cc8a0e5 650 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
3ffee04b
CS
651 &nbsp; <!-- space for future online help link -->
652 </td>
2999f1d2
CS
653</tr>
654</table>
655</form>
3ffee04b
CS
656END
657;
658 &Header::closebox();
659}
395e3b90 660
395e3b90 661####################### End added for snort rules control #################################
ac1cfefa
MT
662&Header::closebigbox();
663&Header::closepage();
664
ac1cfefa 665sub downloadrulesfile {
73231650
CS
666 my $peer;
667 my $peerport;
ac1cfefa 668
73231650 669 unlink("/var/tmp/log");
ac1cfefa
MT
670
671 unless (-e "${General::swroot}/red/active") {
672 $errormessage = $Lang::tr{'could not download latest updates'};
673 return undef;
674 }
675
ac1cfefa
MT
676 my %proxysettings=();
677 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
678
679 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
73231650 680 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
ac1cfefa
MT
681 }
682
73231650 683 if ($peer) {
1f606aef 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");
73231650 685 } else {
1f606aef 686 system("wget -r -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
ac1cfefa 687 }
ac1cfefa 688}