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