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