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