]>
Commit | Line | Data |
---|---|---|
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 | |
23 | use LWP::UserAgent; | |
24 | use File::Copy; | |
25 | use File::Temp qw/ tempfile tempdir /; | |
26 | use strict; | |
27 | ||
28 | # enable only the following on debugging purpose | |
cb5e9c6c CS |
29 | #use warnings; |
30 | #use CGI::Carp 'fatalsToBrowser'; | |
ac1cfefa | 31 | |
986e08d9 | 32 | require '/var/ipfire/general-functions.pl'; |
ac1cfefa MT |
33 | require "${General::swroot}/lang.pl"; |
34 | require "${General::swroot}/header.pl"; | |
35 | ||
f2fdd0c1 CS |
36 | my %color = (); |
37 | my %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 |
41 | my %snortsettings=(); |
42 | my %checked=(); | |
5a3e0dca | 43 | my %selected=(); |
ac1cfefa MT |
44 | my %netsettings=(); |
45 | our $errormessage = ''; | |
46 | our $md5 = '0';# not '' to avoid displaying the wrong message when INSTALLMD5 not set | |
47 | our $realmd5 = ''; | |
48 | our $results = ''; | |
49 | our $tempdir = ''; | |
50 | our $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 | 78 | my $snortrulepath; # change to "/etc/snort/rules" - maniac |
395e3b90 MT |
79 | my @snortconfig; |
80 | my $restartsnortrequired = 0; | |
81 | my %snortrules; | |
82 | my $rule = ''; | |
83 | my $table1colour = ''; | |
84 | my $table2colour = ''; | |
85 | my $var = ''; | |
86 | my $value = ''; | |
87 | my $tmp = ''; | |
88 | my $linkedrulefile = ''; | |
89 | my $border = ''; | |
90 | my $checkboxname = ''; | |
91 | ||
92 | if (-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); | |
99 | open(FILE, ">/etc/snort/snort.conf") or die 'Unable to write snort config file.'; | |
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) { | |
cf29614f | 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 | } | |
229 | } | |
230 | ||
231 | # Check for rule state | |
232 | if ($line =~ /^\#/) { | |
233 | $snortrules{$rule}{"State"} = "Disabled"; | |
234 | } else { | |
235 | $snortrules{$rule}{"State"} = "Enabled"; | |
236 | } | |
237 | ||
238 | # Set rule description | |
239 | $snortrules{$rule}{"Description"} = $snortruledesc; | |
240 | ||
241 | # Loop over sorted rules | |
242 | foreach my $ruledef (sort {$a <=> $b} keys(%snortruledef)) { | |
243 | $snortrules{$rule}{"Definition"}{$ruledef}{'Description'} = $snortruledef{$ruledef}{'Description'}; | |
244 | $snortrules{$rule}{"Definition"}{$ruledef}{'State'} = $snortruledef{$ruledef}{'State'}; | |
245 | } | |
246 | ||
247 | $snortruledesc = ''; | |
248 | print FILE "$line\n"; | |
249 | } elsif ($line =~ /var RULE_PATH/) { | |
250 | ($tmp, $tmp, $snortrulepath) = split(' ', $line); | |
251 | print FILE "$line\n"; | |
252 | } else { | |
253 | print FILE "$line\n"; | |
254 | } | |
255 | } | |
256 | close(FILE); | |
257 | ||
258 | if ($restartsnortrequired) { | |
18322edf | 259 | system('/usr/local/bin/snortctrl restart >/dev/null'); |
395e3b90 MT |
260 | } |
261 | } | |
262 | ||
263 | ####################### End added for snort rules control ################################# | |
264 | ||
5a3e0dca MT |
265 | if ($snortsettings{'RULES'} eq 'subscripted') { |
266 | $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT_s.tar.gz"; | |
267 | } elsif ($snortsettings{'RULES'} eq 'registered') { | |
268 | $url="http://www.snort.org/pub-bin/oinkmaster.cgi/$snortsettings{'OINKCODE'}/snortrules-snapshot-CURRENT.tar.gz"; | |
ac1cfefa | 269 | } else { |
5a3e0dca | 270 | $url="http://www.snort.org/pub-bin/downloads.cgi/Download/comm_rules/Community-Rules-CURRENT.tar.gz"; |
ac1cfefa MT |
271 | } |
272 | ||
fbfdb241 | 273 | if ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "snort" ) |
ac1cfefa MT |
274 | { |
275 | $errormessage = $Lang::tr{'invalid input for oink code'} unless ( | |
276 | ($snortsettings{'OINKCODE'} =~ /^[a-z0-9]+$/) || | |
277 | ($snortsettings{'RULESTYPE'} eq 'nothing' ) ); | |
278 | ||
279 | &General::writehash("${General::swroot}/snort/settings", \%snortsettings); | |
280 | if ($snortsettings{'ENABLE_SNORT'} eq 'on') | |
281 | { | |
9833e7d8 | 282 | system ('/usr/bin/touch', "${General::swroot}/snort/enable"); |
ac1cfefa MT |
283 | } else { |
284 | unlink "${General::swroot}/snort/enable"; | |
285 | } | |
286 | if ($snortsettings{'ENABLE_SNORT_GREEN'} eq 'on') | |
287 | { | |
9833e7d8 | 288 | system ('/usr/bin/touch', "${General::swroot}/snort/enable_green"); |
ac1cfefa MT |
289 | } else { |
290 | unlink "${General::swroot}/snort/enable_green"; | |
291 | } | |
292 | if ($snortsettings{'ENABLE_SNORT_BLUE'} eq 'on') | |
293 | { | |
9833e7d8 | 294 | system ('/usr/bin/touch', "${General::swroot}/snort/enable_blue"); |
ac1cfefa MT |
295 | } else { |
296 | unlink "${General::swroot}/snort/enable_blue"; | |
297 | } | |
298 | if ($snortsettings{'ENABLE_SNORT_ORANGE'} eq 'on') | |
299 | { | |
9833e7d8 | 300 | system ('/usr/bin/touch', "${General::swroot}/snort/enable_orange"); |
ac1cfefa MT |
301 | } else { |
302 | unlink "${General::swroot}/snort/enable_orange"; | |
303 | } | |
1b73b07e CS |
304 | if ($snortsettings{'ENABLE_GUARDIAN'} eq 'on') |
305 | { | |
306 | system ('/usr/bin/touch', "${General::swroot}/guardian/enable"); | |
307 | } else { | |
308 | unlink "${General::swroot}/guardian/enable"; | |
309 | } | |
ac1cfefa | 310 | |
18322edf | 311 | system('/usr/local/bin/snortctrl restart >/dev/null'); |
395e3b90 | 312 | |
fbfdb241 CS |
313 | } elsif ($snortsettings{'ACTION'} eq $Lang::tr{'save'} && $snortsettings{'ACTION2'} eq "guardian" ){ |
314 | open(IGNOREFILE, ">$snortsettings{'GUARDIAN_IGNOREFILE'}") or die "Unable to write guardian ignore file $snortsettings{'GUARDIAN_IGNOREFILE'}"; | |
315 | print IGNOREFILE $snortsettings{'GUARDIAN_IGNOREFILE_CONTENT'}; | |
316 | close(IGNOREFILE); | |
317 | open(GUARDIAN, ">/var/ipfire/guardian/guardian.conf") or die "Unable to write guardian conf /var/ipfire/guardian/guardian.conf"; | |
318 | print GUARDIAN <<END | |
319 | Interface $snortsettings{'GUARDIAN_INTERFACE'} | |
320 | HostGatewayByte $snortsettings{'GUARDIAN_HOSTGATEWAYBYTE'} | |
321 | LogFile $snortsettings{'GUARDIAN_LOGFILE'} | |
322 | AlertFile $snortsettings{'GUARDIAN_ALERTFILE'} | |
323 | IgnoreFile $snortsettings{'GUARDIAN_IGNOREFILE'} | |
324 | TargetFile $snortsettings{'GUARDIAN_TARGETFILE'} | |
325 | TimeLimit $snortsettings{'GUARDIAN_TIMELIMIT'} | |
326 | END | |
327 | ; | |
328 | close(GUARDIAN); | |
329 | system('/usr/local/bin/snortctrl restart >/dev/null'); | |
ac1cfefa MT |
330 | } else { |
331 | # INSTALLMD5 is not in the form, so not retrieved by getcgihash | |
332 | &General::readhash("${General::swroot}/snort/settings", \%snortsettings); | |
333 | } | |
334 | ||
335 | if ($snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'}) { | |
336 | $md5 = &getmd5; | |
337 | if (($snortsettings{'INSTALLMD5'} ne $md5) && defined $md5 ) { | |
338 | chomp($md5); | |
339 | my $filename = &downloadrulesfile(); | |
340 | if (defined $filename) { | |
341 | # Check MD5sum | |
342 | $realmd5 = `/usr/bin/md5sum $filename`; | |
343 | chomp ($realmd5); | |
344 | $realmd5 =~ s/^(\w+)\s.*$/$1/; | |
345 | if ($md5 ne $realmd5) { | |
346 | $errormessage = "$Lang::tr{'invalid md5sum'}"; | |
347 | } else { | |
348 | $results = "<b>$Lang::tr{'installed updates'}</b>\n<pre>"; | |
395e3b90 | 349 | $results .=`/usr/local/bin/oinkmaster.pl -s -u file://$filename -C /var/ipfire/snort/oinkmaster.conf -o /etc/snort/rules 2>&1`; |
ac1cfefa MT |
350 | $results .= "</pre>"; |
351 | } | |
352 | unlink ($filename); | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | $checked{'ENABLE_SNORT'}{'off'} = ''; | |
358 | $checked{'ENABLE_SNORT'}{'on'} = ''; | |
359 | $checked{'ENABLE_SNORT'}{$snortsettings{'ENABLE_SNORT'}} = "checked='checked'"; | |
360 | $checked{'ENABLE_SNORT_GREEN'}{'off'} = ''; | |
361 | $checked{'ENABLE_SNORT_GREEN'}{'on'} = ''; | |
362 | $checked{'ENABLE_SNORT_GREEN'}{$snortsettings{'ENABLE_SNORT_GREEN'}} = "checked='checked'"; | |
363 | $checked{'ENABLE_SNORT_BLUE'}{'off'} = ''; | |
364 | $checked{'ENABLE_SNORT_BLUE'}{'on'} = ''; | |
365 | $checked{'ENABLE_SNORT_BLUE'}{$snortsettings{'ENABLE_SNORT_BLUE'}} = "checked='checked'"; | |
366 | $checked{'ENABLE_SNORT_ORANGE'}{'off'} = ''; | |
367 | $checked{'ENABLE_SNORT_ORANGE'}{'on'} = ''; | |
368 | $checked{'ENABLE_SNORT_ORANGE'}{$snortsettings{'ENABLE_SNORT_ORANGE'}} = "checked='checked'"; | |
1b73b07e CS |
369 | $checked{'ENABLE_GUARDIAN'}{'off'} = ''; |
370 | $checked{'ENABLE_GUARDIAN'}{'on'} = ''; | |
371 | $checked{'ENABLE_GUARDIAN'}{$snortsettings{'ENABLE_GUARDIAN'}} = "checked='checked'"; | |
5a3e0dca | 372 | $selected{'RULES'}{'nothing'} = ''; |
5a3e0dca MT |
373 | $selected{'RULES'}{'community'} = ''; |
374 | $selected{'RULES'}{'registered'} = ''; | |
375 | $selected{'RULES'}{'subscripted'} = ''; | |
376 | $selected{'RULES'}{$snortsettings{'RULES'}} = "selected='selected'"; | |
ac1cfefa MT |
377 | |
378 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
379 | ||
395e3b90 MT |
380 | ####################### Added for snort rules control ################################# |
381 | print "<SCRIPT LANGUAGE='JavaScript' SRC='/include/snortupdateutility.js'></SCRIPT>"; | |
382 | print <<END | |
383 | <STYLE TYPE="text/css"> | |
384 | <!-- | |
385 | .section { | |
386 | border: groove; | |
387 | } | |
388 | .row1color { | |
389 | border: ridge; | |
f2fdd0c1 | 390 | background-color: $color{'color22'}; |
395e3b90 MT |
391 | } |
392 | .row2color { | |
393 | border: ridge; | |
f2fdd0c1 | 394 | background-color: $color{'color20'}; |
395e3b90 MT |
395 | } |
396 | .rowselected { | |
397 | border: double #FF0000; | |
398 | background-color: #DCDCDC; | |
399 | } | |
400 | --> | |
401 | </STYLE> | |
402 | END | |
403 | ; | |
404 | ####################### End added for snort rules control ################################# | |
405 | ||
ac1cfefa MT |
406 | &Header::openbigbox('100%', 'left', '', $errormessage); |
407 | ||
408 | if ($errormessage) { | |
409 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
410 | print "<class name='base'>$errormessage\n"; | |
411 | print " </class>\n"; | |
412 | &Header::closebox(); | |
413 | } | |
414 | ||
415 | &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system2'}); | |
416 | print <<END | |
417 | <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'> | |
1b73b07e | 418 | <tr><td class='base'><input type='checkbox' name='ENABLE_SNORT_GREEN' $checked{'ENABLE_SNORT_GREEN'}{'on'} />GREEN Snort |
ac1cfefa MT |
419 | END |
420 | ; | |
421 | if ($netsettings{'BLUE_DEV'} ne '') { | |
1b73b07e | 422 | print " <input type='checkbox' name='ENABLE_SNORT_BLUE' $checked{'ENABLE_SNORT_BLUE'}{'on'} /> BLUE Snort"; |
ac1cfefa MT |
423 | } |
424 | if ($netsettings{'ORANGE_DEV'} ne '') { | |
1b73b07e | 425 | print " <input type='checkbox' name='ENABLE_SNORT_ORANGE' $checked{'ENABLE_SNORT_ORANGE'}{'on'} /> ORANGE Snort"; |
ac1cfefa | 426 | } |
1b73b07e CS |
427 | print " <input type='checkbox' name='ENABLE_SNORT' $checked{'ENABLE_SNORT'}{'on'} /> RED Snort"; |
428 | if ( -e "/var/ipfire/guardian/guardian.conf" ) { | |
429 | print " <input type='checkbox' name='ENABLE_GUARDIAN' $checked{'ENABLE_GUARDIAN'}{'on'} /> Guardian"; | |
430 | } | |
431 | ||
ac1cfefa | 432 | print <<END |
1b73b07e | 433 | </td></tr> |
ac1cfefa MT |
434 | <tr> |
435 | <td><hr /></td> | |
436 | </tr> | |
437 | <tr> | |
438 | <td><b>$Lang::tr{'ids rules update'}</b></td> | |
439 | </tr> | |
440 | <tr> | |
5a3e0dca MT |
441 | <td><select name='RULES'> |
442 | <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option> | |
5a3e0dca MT |
443 | <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option> |
444 | <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option> | |
445 | <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option> | |
446 | </select> | |
447 | </td> | |
ac1cfefa MT |
448 | </tr> |
449 | <tr> | |
450 | <td><br /> | |
451 | $Lang::tr{'ids rules license'} <a href='http://www.snort.org/' target='_blank'>http://www.snort.org</a>.<br /> | |
452 | <br /> | |
453 | $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 /> | |
454 | </td> | |
455 | </tr> | |
456 | <tr> | |
5a3e0dca | 457 | <td nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$snortsettings{'OINKCODE'}' /></td> |
ac1cfefa MT |
458 | </tr> |
459 | <tr> | |
460 | <td width='30%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'download new ruleset'}' /> | |
461 | END | |
462 | ; | |
463 | ||
464 | if ($snortsettings{'INSTALLMD5'} eq $md5) { | |
465 | print " $Lang::tr{'rules already up to date'}</td>"; | |
466 | } else { | |
467 | if ( $snortsettings{'ACTION'} eq $Lang::tr{'download new ruleset'} && $md5 eq $realmd5 ) { | |
468 | $snortsettings{'INSTALLMD5'} = $realmd5; | |
469 | $snortsettings{'INSTALLDATE'} = `/bin/date +'%Y-%m-%d'`; | |
470 | &General::writehash("${General::swroot}/snort/settings", \%snortsettings); | |
471 | } | |
472 | print " $Lang::tr{'updates installed'}: $snortsettings{'INSTALLDATE'}</td>"; | |
473 | } | |
474 | print <<END | |
475 | </tr> | |
476 | </table> | |
477 | <hr /> | |
478 | <table width='100%'> | |
479 | <tr> | |
fbfdb241 | 480 | <td align='center'><input type='hidden' name='ACTION2' value='snort' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td> |
ac1cfefa MT |
481 | </tr> |
482 | </table> | |
483 | </form> | |
484 | END | |
485 | ; | |
486 | ||
487 | if ($results ne '') { | |
488 | print "$results"; | |
489 | } | |
490 | ||
491 | &Header::closebox(); | |
fbfdb241 CS |
492 | |
493 | ####################### Added for guardian control #################################### | |
494 | if ( -e "/var/ipfire/guardian/guardian.conf" ) { | |
495 | &Header::openbox('100%', 'LEFT', $Lang::tr{'guardian configuration'}); | |
496 | print <<END | |
497 | <form method='post' action='$ENV{'SCRIPT_NAME'}'><table width='100%'> | |
498 | <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> | |
499 | <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> | |
500 | <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> | |
501 | <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> | |
502 | <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> | |
503 | <tr><td align='center' colspan='2'><input type='hidden' name='ACTION2' value='guardian' /><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td></tr> | |
504 | </table> | |
505 | </form> | |
506 | END | |
507 | ; | |
508 | &Header::closebox(); | |
509 | } | |
510 | ||
511 | ||
512 | ||
513 | ||
395e3b90 | 514 | ####################### Added for snort rules control ################################# |
3ffee04b CS |
515 | if ( -e "${General::swroot}/snort/enable" || -e "${General::swroot}/snort/enable_green" || -e "${General::swroot}/snort/enable_blue" || -e "${General::swroot}/snort/enable_orange" ) { |
516 | &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'}); | |
517 | # Output display table for rule files | |
518 | print "<TABLE width='100%'><TR><TD VALIGN='TOP'><TABLE>"; | |
519 | ||
520 | print "<form method='post'>"; | |
521 | ||
522 | # Local vars | |
523 | my $ruledisplaycnt = 1; | |
524 | my $rulecnt = keys %snortrules; | |
525 | $rulecnt++; | |
526 | $rulecnt = $rulecnt / 2; | |
527 | ||
528 | # Loop over each rule file | |
529 | foreach my $rulefile (sort keys(%snortrules)) { | |
530 | my $rulechecked = ''; | |
531 | ||
532 | # Check if reached half-way through rule file rules to start new column | |
533 | if ($ruledisplaycnt > $rulecnt) { | |
534 | print "</TABLE></TD><TD VALIGN='TOP'><TABLE>"; | |
535 | $ruledisplaycnt = 0; | |
2999f1d2 | 536 | } |
3ffee04b CS |
537 | |
538 | # Check if rule file is enabled | |
539 | if ($snortrules{$rulefile}{"State"} eq 'Enabled') { | |
540 | $rulechecked = 'CHECKED'; | |
541 | } | |
542 | ||
543 | # Create rule file link, vars array, and display flag | |
2999f1d2 | 544 | my $rulefilelink = "?RULEFILE=$rulefile"; |
3ffee04b CS |
545 | my $rulefiletoclose = ''; |
546 | my @queryvars = (); | |
547 | my $displayrulefilerules = 0; | |
548 | ||
549 | # Check for passed in query string | |
550 | if ($ENV{'QUERY_STRING'}) { | |
551 | # Split out vars | |
2999f1d2 | 552 | @queryvars = split(/\&/, $ENV{'QUERY_STRING'}); |
3ffee04b CS |
553 | |
554 | # Loop over values | |
555 | foreach $value (@queryvars) { | |
556 | # Split out var pairs | |
2999f1d2 | 557 | ($var, $linkedrulefile) = split(/=/, $value); |
3ffee04b CS |
558 | |
559 | # Check if var is 'RULEFILE' | |
560 | if ($var eq 'RULEFILE') { | |
561 | # Check if rulefile equals linkedrulefile | |
562 | if ($rulefile eq $linkedrulefile) { | |
563 | # Set display flag | |
564 | $displayrulefilerules = 1; | |
565 | ||
566 | # Strip out rulefile from rulefilelink | |
567 | $rulefilelink =~ s/RULEFILE=$linkedrulefile//g; | |
568 | } else { | |
569 | # Add linked rule file to rulefilelink | |
570 | $rulefilelink .= "&RULEFILE=$linkedrulefile"; | |
571 | } | |
572 | } | |
573 | } | |
574 | } | |
2999f1d2 | 575 | |
3ffee04b CS |
576 | # Strip out extra & & ? from rulefilelink |
577 | $rulefilelink =~ s/^\?\&/\?/i; | |
578 | ||
579 | # Check for a single '?' and replace with page for proper link display | |
580 | if ($rulefilelink eq '?') { | |
581 | $rulefilelink = "ids.cgi"; | |
582 | } | |
583 | ||
584 | # Output rule file name and checkbox | |
585 | print "<TR><TD CLASS='base' VALIGN='TOP'><INPUT TYPE='checkbox' NAME='SNORT_RULE_$rulefile' $rulechecked> <A HREF='$rulefilelink'>$rulefile</A></TD></TR>"; | |
586 | print "<TR><TD CLASS='base' VALIGN='TOP'>"; | |
587 | ||
588 | # Check for empty 'Description' | |
589 | if ($snortrules{$rulefile}{'Description'} eq '') { | |
590 | print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>No description available</TD></TR>"; | |
591 | } else { | |
592 | # Output rule file 'Description' | |
593 | print "<TABLE WIDTH='100%'><TR><TD CLASS='base'>$snortrules{$rulefile}{'Description'}</TD></TR>"; | |
594 | } | |
595 | ||
596 | # Check for display flag | |
597 | if ($displayrulefilerules) { | |
598 | # Rule file definition rule display | |
599 | print "<TR><TD CLASS='base' VALIGN='TOP'><TABLE border=1><TR>"; | |
600 | ||
601 | # Local vars | |
602 | my $ruledefdisplaycnt = 0; | |
603 | my $ruledefcnt = keys %{$snortrules{$rulefile}{"Definition"}}; | |
604 | $ruledefcnt++; | |
605 | $ruledefcnt = $ruledefcnt / 2; | |
606 | ||
607 | # Loop over rule file rules | |
608 | foreach my $ruledef (sort {$a <=> $b} keys(%{$snortrules{$rulefile}{"Definition"}})) { | |
609 | # Local vars | |
610 | my $ruledefchecked = ''; | |
611 | ||
612 | # If have display 2 rules, start new row | |
613 | if (($ruledefdisplaycnt % 2) == 0) { | |
614 | print "</TR><TR>"; | |
615 | $ruledefdisplaycnt = 0; | |
616 | } | |
617 | ||
618 | # Check for rules state | |
619 | if ($snortrules{$rulefile}{'Definition'}{$ruledef}{'State'} eq 'Enabled') { | |
620 | $ruledefchecked = 'CHECKED'; | |
621 | } | |
622 | ||
623 | # Create rule file rule's checkbox | |
624 | $checkboxname = "SNORT_RULE_$rulefile"; | |
625 | $checkboxname .= "_$ruledef"; | |
626 | print "<TD CLASS='base'><INPUT TYPE='checkbox' NAME='$checkboxname' $ruledefchecked> $snortrules{$rulefile}{'Definition'}{$ruledef}{'Description'}</TD>"; | |
627 | ||
628 | # Increment count | |
629 | $ruledefdisplaycnt++; | |
630 | } | |
631 | ||
632 | # If do not have second rule for row, create empty cell | |
633 | if (($ruledefdisplaycnt % 2) != 0) { | |
634 | print "<TD CLASS='base'></TD>"; | |
635 | } | |
636 | ||
637 | # Close display table | |
638 | print "</TR></TABLE></TD></TR>"; | |
2999f1d2 | 639 | } |
3ffee04b CS |
640 | |
641 | # Close display table | |
642 | print "</TABLE>"; | |
643 | ||
644 | # Increment ruledisplaycnt | |
2999f1d2 | 645 | $ruledisplaycnt++; |
3ffee04b | 646 | } |
3ffee04b CS |
647 | print "</TD></TR></TABLE></TD></TR></TABLE>"; |
648 | print <<END | |
2999f1d2 CS |
649 | <table width='100%'> |
650 | <tr> | |
3ffee04b CS |
651 | <td width='33%'> </td> |
652 | <td width='33%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td> | |
653 | <td width='33%'> | |
654 | <!-- space for future online help link --> | |
655 | </td> | |
2999f1d2 CS |
656 | </tr> |
657 | </table> | |
658 | </form> | |
3ffee04b CS |
659 | END |
660 | ; | |
661 | &Header::closebox(); | |
662 | } | |
395e3b90 | 663 | |
395e3b90 | 664 | ####################### End added for snort rules control ################################# |
ac1cfefa MT |
665 | &Header::closebigbox(); |
666 | &Header::closepage(); | |
667 | ||
668 | sub getmd5 { | |
669 | # Retrieve MD5 sum from $url.md5 file | |
670 | # | |
671 | my $md5buf = &geturl("$url.md5"); | |
672 | return undef unless $md5buf; | |
673 | ||
674 | if (0) { # 1 to debug | |
675 | my $filename=''; | |
676 | my $fh=''; | |
677 | ($fh, $filename) = tempfile('/tmp/XXXXXXXX',SUFFIX => '.md5' ); | |
678 | binmode ($fh); | |
679 | syswrite ($fh, $md5buf->content); | |
680 | close($fh); | |
681 | } | |
682 | return $md5buf->content; | |
683 | } | |
684 | sub downloadrulesfile { | |
685 | my $return = &geturl($url); | |
686 | return undef unless $return; | |
687 | ||
688 | if (index($return->content, "\037\213") == -1 ) { # \037\213 is .gz beginning | |
689 | $errormessage = $Lang::tr{'invalid loaded file'}; | |
690 | return undef; | |
691 | } | |
692 | ||
693 | my $filename=''; | |
694 | my $fh=''; | |
695 | ($fh, $filename) = tempfile('/tmp/XXXXXXXX',SUFFIX => '.tar.gz' );#oinkmaster work only with this extension | |
696 | binmode ($fh); | |
697 | syswrite ($fh, $return->content); | |
698 | close($fh); | |
699 | return $filename; | |
700 | } | |
701 | ||
702 | sub geturl ($) { | |
703 | my $url=$_[0]; | |
704 | ||
705 | unless (-e "${General::swroot}/red/active") { | |
706 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
707 | return undef; | |
708 | } | |
709 | ||
710 | my $downloader = LWP::UserAgent->new; | |
711 | $downloader->timeout(5); | |
712 | ||
713 | my %proxysettings=(); | |
714 | &General::readhash("${General::swroot}/proxy/settings", \%proxysettings); | |
715 | ||
716 | if ($_=$proxysettings{'UPSTREAM_PROXY'}) { | |
717 | my ($peer, $peerport) = (/^(?:[a-zA-Z ]+\:\/\/)?(?:[A-Za-z0-9\_\.\-]*?(?:\:[A-Za-z0-9\_\.\-]*?)?\@)?([a-zA-Z0-9\.\_\-]*?)(?:\:([0-9]{1,5}))?(?:\/.*?)?$/); | |
718 | if ($proxysettings{'UPSTREAM_USER'}) { | |
719 | $downloader->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$peer:$peerport/"); | |
720 | } else { | |
721 | $downloader->proxy("http","http://$peer:$peerport/"); | |
722 | } | |
723 | } | |
724 | ||
725 | my $return = $downloader->get($url,'Cache-Control','no-cache'); | |
726 | ||
727 | if ($return->code == 403) { | |
728 | $errormessage = $Lang::tr{'access refused with this oinkcode'}; | |
729 | return undef; | |
730 | } elsif (!$return->is_success()) { | |
731 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
732 | return undef; | |
733 | } | |
734 | ||
735 | return $return; | |
736 | ||
737 | } |