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