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