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