]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
kernel: update to 4.9.34
[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
35$a = new CGI;
36
f2fdd0c1
CS
37my %color = ();
38my %mainsettings = ();
39&General::readhash("${General::swroot}/main/settings", \%mainsettings);
40&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
41
ac1cfefa
MT
42my %snortsettings=();
43my %checked=();
5a3e0dca 44my %selected=();
ac1cfefa
MT
45my %netsettings=();
46our $errormessage = '';
ac1cfefa
MT
47our $results = '';
48our $tempdir = '';
49our $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'} = '';
5a3e0dca 59$snortsettings{'RULES'} = '';
ac1cfefa
MT
60$snortsettings{'OINKCODE'} = '';
61$snortsettings{'INSTALLDATE'} = '';
73231650
CS
62$snortsettings{'FILE'} = '';
63$snortsettings{'UPLOAD'} = '';
ac1cfefa
MT
64
65&Header::getcgihash(\%snortsettings, {'wantfile' => 1, 'filevar' => 'FH'});
66
395e3b90 67####################### Added for snort rules control #################################
cf29614f 68my $snortrulepath; # change to "/etc/snort/rules" - maniac
395e3b90
MT
69my @snortconfig;
70my $restartsnortrequired = 0;
71my %snortrules;
72my $rule = '';
73my $table1colour = '';
74my $table2colour = '';
75my $var = '';
76my $value = '';
77my $tmp = '';
78my $linkedrulefile = '';
79my $border = '';
80my $checkboxname = '';
81
82if (-e "/etc/snort/snort.conf") {
2999f1d2
CS
83
84
395e3b90
MT
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);
d192af92 89 open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.';
395e3b90 90
324bb888 91 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
92 foreach (@rules) {
93 chomp $_;
94 my $temp = join(";",@snortconfig);
95 if ( $temp =~ /$_/ ){next;}
96 else { push(@snortconfig,"#include \$RULE_PATH/".$_);}
97 }
f9c2147d 98
395e3b90
MT
99 # Loop over each line
100 foreach my $line (@snortconfig) {
429f7008 101 # Trim the line
395e3b90
MT
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 #
61196506 140 if ($ruleline =~ /^\#/ and $ruleline !~ /^\#alert/) {
395e3b90
MT
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 }
f9c2147d 219
395e3b90
MT
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) {
18322edf 250 system('/usr/local/bin/snortctrl restart >/dev/null');
395e3b90
MT
251 }
252}
253
254####################### End added for snort rules control #################################
255
5a3e0dca 256if ($snortsettings{'RULES'} eq 'subscripted') {
ba6fc476 257 $url=" https://www.snort.org/rules/snortrules-snapshot-2990.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
5a3e0dca 258} elsif ($snortsettings{'RULES'} eq 'registered') {
ba6fc476 259 $url=" https://www.snort.org/rules/snortrules-snapshot-2990.tar.gz?oinkcode=$snortsettings{'OINKCODE'}";
a0fa489f 260} elsif ($snortsettings{'RULES'} eq 'community') {
b3f401fb 261 $url=" https://www.snort.org/rules/community";
ac1cfefa 262} else {
ac93c5d0 263 $url="http://rules.emergingthreats.net/open/snort-2.9.0/emerging.rules.tar.gz";
ac1cfefa
MT
264}
265
fbfdb241 266if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" )
ac1cfefa
MT
267{
268 $errormessage = $Lang::tr{'invalid input for oink code'} unless (
269 ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) ||
a0fa489f
AF
270 ($snortsettings{'RULES'} eq 'nothing' ) ||
271 ($snortsettings{'RULES'} eq 'emerging' ) ||
272 ($snortsettings{'RULES'} eq 'community' ));
ac1cfefa
MT
273
274 &General::writehash("${General::swroot}/snort/settings", \%snortsettings);
275 if ($snortsettings{'ENABLE_SNORT'} eq 'on')
276 {
9833e7d8 277 system ('/usr/bin/touch', "${General::swroot}/snort/enable");
ac1cfefa
MT
278 } else {
279 unlink "${General::swroot}/snort/enable";
f9c2147d 280 }
ac1cfefa
MT
281 if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on')
282 {
9833e7d8 283 system ('/usr/bin/touch', "${General::swroot}/snort/enable_green");
ac1cfefa
MT
284 } else {
285 unlink "${General::swroot}/snort/enable_green";
f9c2147d 286 }
ac1cfefa
MT
287 if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on')
288 {
9833e7d8 289 system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue");
ac1cfefa
MT
290 } else {
291 unlink "${General::swroot}/snort/enable_blue";
f9c2147d 292 }
ac1cfefa
MT
293 if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on')
294 {
9833e7d8 295 system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange");
ac1cfefa
MT
296 } else {
297 unlink "${General::swroot}/snort/enable_orange";
298 }
429f7008
DG
299 if ($snortsettings{'ENABLE_PREPROCESSOR_HTTP_INSPECT'} eq 'on')
300 {
301 system ('/usr/bin/touch', "${General::swroot}/snort/enable_preprocessor_http_inspect");
302 } else {
303 unlink "${General::swroot}/snort/enable_preprocessor_http_inspect";
f9c2147d 304 }
ac1cfefa 305
429f7008 306 system('/usr/local/bin/snortctrl restart >/dev/null');
395e3b90 307
75b5a616 308}
a27c40a0 309
ac1cfefa
MT
310 # INSTALLMD5 is not in the form, so not retrieved by getcgihash
311 &General::readhash("${General::swroot}/snort/settings", \%snortsettings);
ac1cfefa 312
73231650 313if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} || $snortsettings{'ACTION'} eq $Lang::tr{'upload new ruleset'}) {
32810952
CS
314
315 my @df = `/bin/df -B M /var`;
316 foreach my $line (@df) {
317 next if $line =~ m/^Filesystem/;
73231650 318 my $return;
32810952
CS
319
320 if ($line =~ m/dev/ ) {
321 $line =~ m/^.* (\d+)M.*$/;
322 my @temp = split(/ +/,$line);
f805321a
CS
323 if ($1<300) {
324 $errormessage = "$Lang::tr{'not enough disk space'} < 300MB, /var $1MB";
ac1cfefa 325 } else {
73231650
CS
326
327 if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} ){
328
329 &downloadrulesfile();
330 sleep(3);
331 $return = `cat /var/tmp/log 2>/dev/null`;
332
333 } elsif ( $snortsettings{'ACTION'} eq $Lang::tr{'upload new ruleset'} ) {
334 my $upload = $a->param("UPLOAD");
335 open UPLOADFILE, ">/var/tmp/snortrules.tar.gz";
336 binmode $upload;
337 while ( <$upload> ) {
338 print UPLOADFILE;
339 }
340 close UPLOADFILE;
341 }
342
343 if ($return =~ "ERROR"){
344 $errormessage = "<br /><pre>".$return."</pre>";
345 } else {
346 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 &");
347 sleep(2);
32810952 348 }
ac1cfefa 349 }
ac1cfefa
MT
350 }
351 }
352}
353
354$checked{'ENABLE_SNORT'}{'off'} = '';
355$checked{'ENABLE_SNORT'}{'on'} = '';
356$checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'";
357$checked{'ENABLE_SNORT_GREEN'}{'off'} = '';
358$checked{'ENABLE_SNORT_GREEN'}{'on'} = '';
359$checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'";
360$checked{'ENABLE_SNORT_BLUE'}{'off'} = '';
361$checked{'ENABLE_SNORT_BLUE'}{'on'} = '';
362$checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'";
363$checked{'ENABLE_SNORT_ORANGE'}{'off'} = '';
364$checked{'ENABLE_SNORT_ORANGE'}{'on'} = '';
365$checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'";
5a3e0dca 366$selected{'RULES'}{'nothing'} = '';
5a3e0dca 367$selected{'RULES'}{'community'} = '';
a0fa489f 368$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
369$selected{'RULES'}{'registered'} = '';
370$selected{'RULES'}{'subscripted'} = '';
371$selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'";
ac1cfefa
MT
372
373&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
374
395e3b90 375####################### Added for snort rules control #################################
2c01f467 376print "<script type='text/javascript' src='/include/snortupdateutility.js'></script>";
395e3b90 377print <<END
e6d8a421 378<style type="text/css">
395e3b90
MT
379<!--
380.section {
381 border: groove;
382}
383.row1color {
384 border: ridge;
f2fdd0c1 385 background-color: $color{'color22'};
395e3b90
MT
386}
387.row2color {
388 border: ridge;
f2fdd0c1 389 background-color: $color{'color20'};
395e3b90
MT
390}
391.rowselected {
392 border: double #FF0000;
393 background-color: #DCDCDC;
394}
395-->
e6d8a421 396</style>
395e3b90
MT
397END
398;
399####################### End added for snort rules control #################################
400
ac1cfefa
MT
401&Header::openbigbox('100%', 'left', '', $errormessage);
402
90c2e164
CS
403###############
404# DEBUG DEBUG
405# &Header::openbox('100%', 'left', 'DEBUG');
406# my $debugCount = 0;
407# foreach my $line (sort keys %snortsettings) {
408# print "$line = $snortsettings{$line}<br />\n";
409# $debugCount++;
410# }
411# print "&nbsp;Count: $debugCount\n";
412# &Header::closebox();
413# DEBUG DEBUG
414###############
415
ac1cfefa
MT
416if ($errormessage) {
417 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
418 print "<class name='base'>$errormessage\n";
419 print "&nbsp;</class>\n";
420 &Header::closebox();
421}
422
73231650
CS
423my $return = `pidof oinkmaster.pl -x`;
424chomp($return);
425if ($return) {
426 &Header::openbox( 'Waiting', 1, "<meta http-equiv='refresh' content='10;'>" );
427 print <<END;
428 <table>
429 <tr><td>
430 <img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' />&nbsp;
431 <td>
432 $Lang::tr{'snort working'}
433 <tr><td colspan='2' align='center'>
434 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
f8aa0679 435 <input type='image' alt='$Lang::tr{'reload'}' title='$Lang::tr{'reload'}' src='/images/view-refresh.png' />
73231650
CS
436 </form>
437 <tr><td colspan='2' align='left'><pre>
438END
439 my @output = `tail -20 /var/tmp/log`;
440 foreach (@output) {
441 print "$_";
442 }
443 print <<END;
444 </pre>
445 </table>
446END
447 &Header::closebox();
448 &Header::closebigbox();
449 &Header::closepage();
450 exit;
451 refreshpage();
452}
453
7cc8a0e5 454&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
ac1cfefa
MT
455print <<END
456<form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'>
1b73b07e 457<tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort
ac1cfefa
MT
458END
459;
460if ($netsettings{'BLUE_DEV'} ne '') {
1b73b07e 461 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort";
ac1cfefa
MT
462}
463if ($netsettings{'ORANGE_DEV'} ne '') {
1b73b07e 464 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort";
ac1cfefa 465}
1b73b07e 466 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort";
1b73b07e 467
ac1cfefa 468print <<END
1b73b07e 469</td></tr>
ac1cfefa 470<tr>
7cc8a0e5 471 <td><br><br></td>
ac1cfefa
MT
472</tr>
473<tr>
474 <td><b>$Lang::tr{'ids rules update'}</b></td>
475</tr>
476<tr>
5a3e0dca
MT
477 <td><select name='RULES'>
478 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 479 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
480 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
481 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
482 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
483 </select>
484 </td>
ac1cfefa
MT
485</tr>
486<tr>
487 <td><br />
8d29504c 488 $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 489 $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
490 </td>
491</tr>
492<tr>
5a3e0dca 493 <td nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td>
ac1cfefa
MT
494</tr>
495<tr>
7cc8a0e5 496 <td width='30%' align='left'><br><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' />
ac1cfefa
MT
497END
498;
42d9192e 499if ( -e "/var/tmp/snortrules.tar.gz"){
0972c650
CS
500 my @Info = stat("/var/tmp/snortrules.tar.gz");
501 $snortsettings{'INSTALLDATE'} = localtime($Info[9]);
ac1cfefa 502}
32810952
CS
503print "&nbsp;$Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>";
504
ac1cfefa
MT
505print <<END
506</tr>
507</table>
7cc8a0e5 508<br><br>
ac1cfefa
MT
509<table width='100%'>
510<tr>
7cc8a0e5 511 <td align='right'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
ac1cfefa
MT
512</tr>
513</table>
514</form>
515END
516;
517
518if ($results ne '') {
519 print "$results";
520}
521
522&Header::closebox();
fbfdb241 523
395e3b90 524####################### Added for snort rules control #################################
3ffee04b
CS
525if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) {
526 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
527 # Output display table for rule files
631b67b7 528 print "<table width='100%'><tr><td valign='top'><table>";
f9c2147d 529
3ffee04b
CS
530 print "<form method='post'>";
531
532 # Local vars
533 my $ruledisplaycnt = 1;
534 my $rulecnt = keys %snortrules;
535 $rulecnt++;
536 $rulecnt = $rulecnt / 2;
537
538 # Loop over each rule file
539 foreach my $rulefile (sort keys(%snortrules)) {
540 my $rulechecked = '';
541
ce0e83b3
AF
542 # Hide inkompatible Block rules
543 if ($rulefile =~'-BLOCK.rules') {
544 next;
545 }
546
3ffee04b
CS
547 # Check if reached half-way through rule file rules to start new column
548 if ($ruledisplaycnt > $rulecnt) {
631b67b7 549 print "</table></td><td valign='top'><table>";
3ffee04b 550 $ruledisplaycnt = 0;
2999f1d2 551 }
3ffee04b
CS
552
553 # Check if rule file is enabled
554 if ($snortrules{$rulefile}{"State"} eq 'Enabled') {
555 $rulechecked = 'CHECKED';
556 }
557
558 # Create rule file link, vars array, and display flag
2999f1d2 559 my $rulefilelink = "?RULEFILE=$rulefile";
3ffee04b
CS
560 my $rulefiletoclose = '';
561 my @queryvars = ();
562 my $displayrulefilerules = 0;
563
564 # Check for passed in query string
565 if ($ENV{'QUERY_STRING'}) {
566 # Split out vars
2999f1d2 567 @queryvars = split(/\&/, $ENV{'QUERY_STRING'});
3ffee04b
CS
568
569 # Loop over values
570 foreach $value (@queryvars) {
571 # Split out var pairs
2999f1d2 572 ($var, $linkedrulefile) = split(/=/, $value);
3ffee04b
CS
573
574 # Check if var is 'RULEFILE'
575 if ($var eq 'RULEFILE') {
576 # Check if rulefile equals linkedrulefile
577 if ($rulefile eq $linkedrulefile) {
578 # Set display flag
579 $displayrulefilerules = 1;
580
581 # Strip out rulefile from rulefilelink
582 $rulefilelink =~ s/RULEFILE=$linkedrulefile//g;
583 } else {
584 # Add linked rule file to rulefilelink
585 $rulefilelink .= "&RULEFILE=$linkedrulefile";
586 }
587 }
588 }
589 }
2999f1d2 590
3ffee04b
CS
591 # Strip out extra & & ? from rulefilelink
592 $rulefilelink =~ s/^\?\&/\?/i;
593
594 # Check for a single '?' and replace with page for proper link display
595 if ($rulefilelink eq '?') {
596 $rulefilelink = "ids.cgi";
597 }
598
599 # Output rule file name and checkbox
631b67b7
AH
600 print "<tr><td class='base' valign='top'><input type='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <a href='$rulefilelink'>$rulefile</a></td></tr>";
601 print "<tr><td class='base' valign='top'>";
3ffee04b
CS
602
603 # Check for empty 'Description'
604 if ($snortrules{$rulefile}{'Description'} eq '') {
e6d8a421 605 print "<table width='100%'><tr><td class='base'>No description available</td></tr>";
3ffee04b
CS
606 } else {
607 # Output rule file 'Description'
e6d8a421 608 print "<table width='100%'><tr><td class='base'>$snortrules{$rulefile}{'Description'}</td></tr>";
3ffee04b
CS
609 }
610
611 # Check for display flag
612 if ($displayrulefilerules) {
613 # Rule file definition rule display
631b67b7 614 print "<tr><td class='base' valign='top'><table border='0'><tr>";
3ffee04b
CS
615
616 # Local vars
617 my $ruledefdisplaycnt = 0;
618 my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}};
619 $ruledefcnt++;
620 $ruledefcnt = $ruledefcnt / 2;
621
622 # Loop over rule file rules
623 foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) {
624 # Local vars
625 my $ruledefchecked = '';
626
627 # If have display 2 rules, start new row
628 if (($ruledefdisplaycnt % 2) == 0) {
e6d8a421 629 print "</tr><tr>";
3ffee04b
CS
630 $ruledefdisplaycnt = 0;
631 }
632
633 # Check for rules state
634 if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') {
635 $ruledefchecked = 'CHECKED';
636 }
637
638 # Create rule file rule's checkbox
639 $checkboxname = "SNORT_RULE_$rulefile";
640 $checkboxname .= "_$ruledef";
e6d8a421 641 print "<td class='base'><input type='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</td>";
3ffee04b
CS
642
643 # Increment count
644 $ruledefdisplaycnt++;
645 }
f9c2147d 646
3ffee04b
CS
647 # If do not have second rule for row, create empty cell
648 if (($ruledefdisplaycnt % 2) != 0) {
e6d8a421 649 print "<td class='base'></td>";
3ffee04b
CS
650 }
651
652 # Close display table
e6d8a421 653 print "</tr></table></td></tr>";
2999f1d2 654 }
3ffee04b
CS
655
656 # Close display table
e6d8a421 657 print "</table>";
3ffee04b
CS
658
659 # Increment ruledisplaycnt
2999f1d2 660 $ruledisplaycnt++;
3ffee04b 661 }
e6d8a421 662 print "</td></tr></table></td></tr></table>";
3ffee04b 663 print <<END
2999f1d2
CS
664<table width='100%'>
665<tr>
7cc8a0e5 666 <td width='100%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
3ffee04b
CS
667 &nbsp; <!-- space for future online help link -->
668 </td>
2999f1d2
CS
669</tr>
670</table>
671</form>
3ffee04b
CS
672END
673;
674 &Header::closebox();
675}
395e3b90 676
395e3b90 677####################### End added for snort rules control #################################
ac1cfefa
MT
678&Header::closebigbox();
679&Header::closepage();
680
ac1cfefa 681sub downloadrulesfile {
73231650
CS
682 my $peer;
683 my $peerport;
ac1cfefa 684
73231650 685 unlink("/var/tmp/log");
ac1cfefa
MT
686
687 unless (-e "${General::swroot}/red/active") {
688 $errormessage = $Lang::tr{'could not download latest updates'};
689 return undef;
690 }
691
ac1cfefa
MT
692 my %proxysettings=();
693 &General::readhash("${General::swroot}/proxy/settings", \%proxysettings);
694
695 if ($_=$proxysettings{'UPSTREAM_PROXY'}) {
73231650 696 ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/);
ac1cfefa
MT
697 }
698
73231650
CS
699 if ($peer) {
700 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 --no-check-certificate --output-document=/var/tmp/snortrules.tar.gz $url");
701 } else {
702 system("wget -r --no-check-certificate -o /var/tmp/log --output-document=/var/tmp/snortrules.tar.gz $url");
ac1cfefa 703 }
ac1cfefa 704}