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