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