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