]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids.cgi: Add oinkmaster_web () function.
[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 #
e698090e 5# Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> #
70df8302
MT
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 22use strict;
0f1d0b9c 23use experimental 'smartmatch';
ac1cfefa
MT
24
25# enable only the following on debugging purpose
90c2e164
CS
26#use warnings;
27#use CGI::Carp 'fatalsToBrowser';
ac1cfefa 28
986e08d9 29require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
8dcebe53 32require "${General::swroot}/ids-functions.pl";
abffcc99 33require "${General::swroot}/network-functions.pl";
ac1cfefa 34
3e12c6e6
SS
35# Import ruleset providers file.
36require "$IDS::rulesetsourcesfile";
37
f2fdd0c1
CS
38my %color = ();
39my %mainsettings = ();
9d18656b 40my %idsrules = ();
1286e0d4 41my %idssettings=();
2f252efa 42my %used_providers=();
298723b9 43my %cgiparams=();
ac1cfefa 44my %checked=();
5a3e0dca 45my %selected=();
b7e29743 46my %ignored=();
0b89daee
SS
47
48# Read-in main settings, for language, theme and colors.
49&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 50&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
0b89daee 51
1286e0d4
SS
52# Get the available network zones, based on the config type of the system and store
53# the list of zones in an array.
abffcc99 54my @network_zones = &Network::get_available_network_zones();
ac1cfefa 55
51b63b41
SS
56# Check if openvpn is started and add it to the array of network zones.
57if ( -e "/var/run/openvpn.pid") {
58 push(@network_zones, "ovpn");
59}
60
43263ea6
SS
61my $errormessage;
62
00512a5a 63# Create files if they does not exist yet.
b02e30fd 64&IDS::check_and_create_filelayout();
01ba4be4 65
99b372b5
SS
66# Hash which contains the colour code of a network zone.
67my %colourhash = (
68 'red' => $Header::colourred,
69 'green' => $Header::colourgreen,
70 'blue' => $Header::colourblue,
51b63b41
SS
71 'orange' => $Header::colourorange,
72 'ovpn' => $Header::colourovpn
99b372b5
SS
73);
74
ac1cfefa
MT
75&Header::showhttpheaders();
76
298723b9
SS
77#Get GUI values
78&Header::getcgihash(\%cgiparams);
ac1cfefa 79
b7e29743
SS
80## Add/edit an entry to the ignore file.
81#
82if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr{'update'})) {
83
84 # Check if any input has been performed.
85 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
86
87 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
88 if ((!&General::validip($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
89 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
90 }
91 } else {
92 $errormessage = "$Lang::tr{'guardian empty input'}";
93 }
94
95 # Go further if there was no error.
96 if ($errormessage eq '') {
97 my %ignored = ();
98 my $id;
99 my $status;
100
101 # Assign hash values.
102 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
103 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
104
105 # Read-in ignoredfile.
18f0991c 106 &General::readhasharray($IDS::ignored_file, \%ignored) if (-e $IDS::ignored_file);
b7e29743
SS
107
108 # Check if we should edit an existing entry and got an ID.
109 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
110 # Assin the provided id.
111 $id = $cgiparams{'ID'};
112
113 # Undef the given ID.
114 undef($cgiparams{'ID'});
115
116 # Grab the configured status of the corresponding entry.
117 $status = $ignored{$id}[2];
118 } else {
119 # Each newly added entry automatically should be enabled.
120 $status = "enabled";
121
122 # Generate the ID for the new entry.
123 #
124 # Sort the keys by their ID and store them in an array.
125 my @keys = sort { $a <=> $b } keys %ignored;
126
127 # Reverse the key array.
128 my @reversed = reverse(@keys);
129
130 # Obtain the last used id.
131 my $last_id = @reversed[0];
132
133 # Increase the last id by one and use it as id for the new entry.
134 $id = ++$last_id;
135 }
136
137 # Add/Modify the entry to/in the ignored hash.
138 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
139
140 # Write the changed ignored hash to the ignored file.
b02e30fd 141 &General::writehasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
142
143 # Regenerate the ignore file.
9283e9b9 144 &IDS::generate_ignore_file();
b7e29743
SS
145 }
146
147 # Check if the IDS is running.
148 if(&IDS::ids_is_running()) {
149 # Call suricatactrl to perform a reload.
150 &IDS::call_suricatactrl("reload");
151 }
152
153## Toggle Enabled/Disabled for an existing entry on the ignore list.
154#
155
156} elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'toggle enable disable'}) {
157 my %ignored = ();
158
159 # Only go further, if an ID has been passed.
160 if ($cgiparams{'ID'}) {
161 # Assign the given ID.
162 my $id = $cgiparams{'ID'};
163
164 # Undef the given ID.
165 undef($cgiparams{'ID'});
166
167 # Read-in ignoredfile.
18f0991c 168 &General::readhasharray($IDS::ignored_file, \%ignored) if (-e $IDS::ignored_file);
b7e29743
SS
169
170 # Grab the configured status of the corresponding entry.
171 my $status = $ignored{$id}[2];
172
173 # Switch the status.
174 if ($status eq "disabled") {
175 $status = "enabled";
176 } else {
177 $status = "disabled";
178 }
179
180 # Modify the status of the existing entry.
181 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
182
183 # Write the changed ignored hash to the ignored file.
b02e30fd 184 &General::writehasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
185
186 # Regenerate the ignore file.
9283e9b9 187 &IDS::generate_ignore_file();
b7e29743
SS
188
189 # Check if the IDS is running.
190 if(&IDS::ids_is_running()) {
191 # Call suricatactrl to perform a reload.
192 &IDS::call_suricatactrl("reload");
193 }
194 }
195
196## Remove entry from ignore list.
197#
198} elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'remove'}) {
199 my %ignored = ();
200
201 # Read-in ignoredfile.
18f0991c 202 &General::readhasharray($IDS::ignored_file, \%ignored) if (-e $IDS::ignored_file);
b7e29743
SS
203
204 # Drop entry from the hash.
205 delete($ignored{$cgiparams{'ID'}});
206
207 # Undef the given ID.
208 undef($cgiparams{'ID'});
209
210 # Write the changed ignored hash to the ignored file.
b02e30fd 211 &General::writehasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
212
213 # Regenerate the ignore file.
9283e9b9 214 &IDS::generate_ignore_file();
b7e29743
SS
215
216 # Check if the IDS is running.
217 if(&IDS::ids_is_running()) {
218 # Call suricatactrl to perform a reload.
219 &IDS::call_suricatactrl("reload");
220 }
221}
222
9074e3d7
SS
223# Check if the page is locked, in this case, the ids_page_lock_file exists.
224if (-e $IDS::ids_page_lock_file) {
225 # Lock the webpage and print notice about autoupgrade of the ruleset
226 # is in progess.
227 &working_notice("$Lang::tr{'ids ruleset autoupdate in progress'}");
228
229 # Loop and check if the file still exists.
230 while(-e $IDS::ids_page_lock_file) {
231 # Sleep for a second and re-check.
232 sleep 1;
233 }
234
235 # Page has been unlocked, perform a reload.
236 &reload();
237}
238
3983aebd
SS
239# Check if any error has been stored.
240if (-e $IDS::storederrorfile) {
241 # Open file to read in the stored error message.
242 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
243
244 # Read the stored error message.
245 $errormessage = <FILE>;
246
247 # Close file.
248 close (FILE);
249
250 # Delete the file, which is now not longer required.
251 unlink($IDS::storederrorfile);
252}
253
a468b62b
SS
254# Gather ruleset details.
255if ($cgiparams{'RULESET'}) {
256 ## Grab all available rules and store them in the idsrules hash.
257 #
ddaf8ae1
SS
258
259 # Get enabled providers.
260 my @enabled_providers = &IDS::get_enabled_providers();
261
a468b62b
SS
262 # Open rules directory and do a directory listing.
263 opendir(DIR, $IDS::rulespath) or die $!;
264 # Loop through the direcory.
265 while (my $file = readdir(DIR)) {
422204ff 266
a468b62b
SS
267 # We only want files.
268 next unless (-f "$IDS::rulespath/$file");
422204ff 269
a468b62b
SS
270 # Ignore empty files.
271 next if (-z "$IDS::rulespath/$file");
422204ff 272
a468b62b
SS
273 # Use a regular expression to find files ending in .rules
274 next unless ($file =~ m/\.rules$/);
422204ff 275
a468b62b
SS
276 # Ignore files which are not read-able.
277 next unless (-R "$IDS::rulespath/$file");
395e3b90 278
a468b62b
SS
279 # Skip whitelist rules file.
280 next if( $file eq "whitelist.rules");
b7e29743 281
ddaf8ae1
SS
282 # Splitt vendor from filename.
283 my @filename_parts = split(/-/, $file);
284
285 # Assign vendor name for easy processing.
286 my $vendor = @filename_parts[0];
287
288 # Skip rulefile if the provider is disabled.
289 next unless ($vendor ~~ @enabled_providers);
290
a468b62b
SS
291 # Call subfunction to read-in rulefile and add rules to
292 # the idsrules hash.
293 &readrulesfile("$file");
294 }
395e3b90 295
a468b62b 296 closedir(DIR);
395e3b90 297
ddaf8ae1
SS
298 # Loop through the array of used providers.
299 foreach my $provider (@enabled_providers) {
300 # Gather used rulefiles.
30c4a9ff 301 my @used_rulesfiles = &IDS::get_provider_used_rulesfiles($provider);
ddaf8ae1
SS
302
303 # Loop through the array of used rulesfiles.
304 foreach my $rulefile (@used_rulesfiles) {
305 # Check if the current rulefile exists in the %idsrules hash.
306 # If not, the file probably does not exist anymore or contains
307 # no rules.
308 if($idsrules{$rulefile}) {
309 # Add the rulefile state to the %idsrules hash.
310 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
311 }
e5738079
SS
312 }
313 }
314}
315
298723b9 316# Save ruleset.
0943ad8c 317if ($cgiparams{'RULESET'} eq $Lang::tr{'ids apply'}) {
15832b10
SS
318 # Get enabled providers.
319 my @enabled_providers = &IDS::get_enabled_providers();
320
321 # Loop through the array of enabled providers.
322 foreach my $provider (@enabled_providers) {
323 # Hash to store the used-enabled and disabled sids.
324 my %enabled_disabled_sids;
325
30c4a9ff
SS
326 # Hash to store the enabled rulefiles for the current processed provider.
327 my %used_rulefiles;
328
9f353f85
SS
329 # Get name of the file which holds the ruleset modification of the provider.
330 my $modifications_file = &IDS::get_provider_ruleset_modifications_file($provider);
15832b10 331
30c4a9ff
SS
332 # Get the name of the file which contains the used rulefiles for this provider.
333 my $used_rulefiles_file = &IDS::get_provider_used_rulesfiles_file($provider);
334
9f353f85
SS
335 # Read-in modifications file, if exists.
336 &General::readhash("$modifications_file", \%enabled_disabled_sids) if (-f "$modifications_file");
15832b10
SS
337
338 # Loop through the hash of idsrules.
339 foreach my $rulefile (keys %idsrules) {
340 # Split the rulefile to get the vendor.
341 my @filename_parts = split(/-/, $rulefile);
342
343 # Assign rulefile vendor.
344 my $rulefile_vendor = @filename_parts[0];
345
346 # Skip the rulefile if the vendor is not our current processed provider.
347 next unless ($rulefile_vendor eq $provider);
348
30c4a9ff
SS
349 # Check if the rulefile is enabled.
350 if ($cgiparams{$rulefile} eq "on") {
351 # Add the rulefile to the hash of enabled rulefiles of this provider.
352 $used_rulefiles{$rulefile} = "enabled";
353
354 # Drop item from cgiparams hash.
355 delete $cgiparams{$rulefile};
356 }
357
15832b10
SS
358 # Loop through the single rules of the rulefile.
359 foreach my $sid (keys %{$idsrules{$rulefile}}) {
360 # Skip the current sid if it is not numeric.
361 next unless ($sid =~ /\d+/ );
362
363 # Check if there exists a key in the cgiparams hash for this sid.
364 if (exists($cgiparams{$sid})) {
365 # Look if the rule is disabled.
366 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
367 # Check if the state has been set to 'on'.
368 if ($cgiparams{$sid} eq "on") {
369 # Add/Modify the sid to/in the enabled_disabled_sids hash.
370 $enabled_disabled_sids{$sid} = "enabled";
371
372 # Drop item from cgiparams hash.
373 delete $cgiparams{$rulefile}{$sid};
374 }
375 }
376 } else {
377 # Look if the rule is enabled.
378 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
379 # Check if the state is 'on' and should be disabled.
380 # In this case there is no entry
381 # for the sid in the cgiparams hash.
382 # Add/Modify it to/in the enabled_disabled_sids hash.
383 $enabled_disabled_sids{$sid} = "disabled";
298723b9
SS
384
385 # Drop item from cgiparams hash.
60333473 386 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
387 }
388 }
298723b9
SS
389 }
390 }
298723b9 391
15832b10
SS
392 # Check if the hash for enabled/disabled sids contains any entries.
393 if (%enabled_disabled_sids) {
9f353f85
SS
394 # Write the modifications file.
395 &General::writehash("$modifications_file", \%enabled_disabled_sids);
37659505 396 }
ddaf8ae1 397
30c4a9ff
SS
398 # Write the used rulefiles file.
399 &General::writehash("$used_rulefiles_file", \%used_rulefiles);
ddaf8ae1
SS
400 }
401
b02e30fd 402 # Call function to generate and write the used rulefiles file.
30c4a9ff 403 &IDS::write_used_rulefiles_file(@enabled_providers);
52599865 404
27760092 405 # Lock the webpage and print message.
5bd8940d 406 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
27760092 407
52599865 408 # Call oinkmaster to alter the ruleset.
27760092
SS
409 &IDS::oinkmaster();
410
e2e7880d 411 # Check if the IDS is running.
5a28e721 412 if(&IDS::ids_is_running()) {
30c4a9ff
SS
413 # Call suricatactrl to perform a reload.
414 &IDS::call_suricatactrl("reload");
e2e7880d
SS
415 }
416
27760092
SS
417 # Reload page.
418 &reload();
52599865
SS
419
420# Download new ruleset.
f3d421a3
SS
421} elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'ids force ruleset update'}) {
422 # Assign given provider handle.
423 my $provider = $cgiparams{'PROVIDER'};
424
43263ea6
SS
425 # Check if the red device is active.
426 unless (-e "${General::swroot}/red/active") {
013274d7 427 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
43263ea6 428 }
52599865 429
3983aebd 430 # Check if enought free disk space is availabe.
434001d0
SS
431 if(&IDS::checkdiskspace()) {
432 $errormessage = "$Lang::tr{'not enough disk space'}";
433 }
52599865 434
43263ea6
SS
435 # Check if any errors happend.
436 unless ($errormessage) {
27760092
SS
437 # Lock the webpage and print notice about downloading
438 # a new ruleset.
5bd8940d 439 &working_notice("$Lang::tr{'ids download new ruleset'}");
3983aebd 440
43263ea6 441 # Call subfunction to download the ruleset.
f3d421a3 442 if(&IDS::downloadruleset($provider)) {
258924ee 443 $errormessage = "$provider - $Lang::tr{'could not download latest updates'}";
8f22237b 444
3983aebd 445 # Call function to store the errormessage.
434001d0 446 &IDS::_store_error_message($errormessage);
52599865 447
3983aebd
SS
448 # Preform a reload of the page.
449 &reload();
450 } else {
451 # Call subfunction to launch oinkmaster.
452 &IDS::oinkmaster();
43263ea6 453
e2e7880d 454 # Check if the IDS is running.
5a28e721 455 if(&IDS::ids_is_running()) {
e2e7880d
SS
456 # Call suricatactrl to perform a reload.
457 &IDS::call_suricatactrl("reload");
458 }
459
3983aebd
SS
460 # Perform a reload of the page.
461 &reload();
462 }
52599865 463 }
515a694d
SS
464
465# Reset a provider to it's defaults.
466} elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids reset provider'}") {
30c4a9ff
SS
467 # Get enabled providers.
468 my @enabled_providers = &IDS::get_enabled_providers();
469
515a694d
SS
470 # Grab provider handle from cgihash.
471 my $provider = $cgiparams{'PROVIDER'};
472
473 # Lock the webpage and print message.
474 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
475
30c4a9ff
SS
476 # Get the name of the file which contains the used rulefiles for this provider.
477 my $used_rulefiles_file = &IDS::get_provider_used_rulesfiles_file($provider);
478
479 # Remove the file if it exists.
480 unlink("$used_rulefiles_file") if (-f "$used_rulefiles_file");
515a694d 481
4c98be8b
SS
482 # Call function to get the path and name for file which holds the ruleset modifications
483 # for the given provider.
484 my $modifications_file = &IDS::get_provider_ruleset_modifications_file($provider);
515a694d
SS
485
486 # Check if the file exists.
4c98be8b 487 if (-f $modifications_file) {
515a694d 488 # Remove the file, as requested.
4c98be8b 489 unlink("$modifications_file");
515a694d
SS
490 }
491
30c4a9ff
SS
492 # Write used rulesfiles file.
493 &IDS::write_used_rulefiles_file(@enabled_providers);
494
515a694d
SS
495 # Regenerate ruleset.
496 &IDS::oinkmaster();
497
498 # Check if the IDS is running.
499 if(&IDS::ids_is_running()) {
515a694d
SS
500 # Get amount of enabled providers.
501 my $amount = @enabled_providers;
502
503 # Check if at least one enabled provider remains.
504 if ($amount >= 1) {
505 # Call suricatactrl to perform a reload.
30c4a9ff 506 &IDS::call_suricatactrl("reload");
515a694d
SS
507
508 # Stop suricata if no enabled provider remains.
509 } else {
510 # Call suricatactrel to perform the stop.
511 &IDS::call_suricatactrl("stop");
512 }
513 }
514
515 # Undefine providers flag.
516 undef($cgiparams{'PROVIDERS'});
517
518 # Reload page.
519 &reload();
520
5bd8940d 521# Save IDS settings.
e0bfd338 522} elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
bbb6efae
SS
523 my %oldidssettings;
524 my $reload_page;
ebdd0f9a 525 my $monitored_zones = 0;
bbb6efae
SS
526
527 # Read-in current (old) IDS settings.
b02e30fd 528 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
bbb6efae 529
724f98c0
SS
530 # Get enabled providers.
531 my @enabled_providers = &IDS::get_enabled_providers();
532
a232b58c 533 # Prevent form name from been stored in conf file.
e0bfd338 534 delete $cgiparams{'IDS'};
a232b58c 535
ebdd0f9a
SS
536 # Check if the IDS should be enabled.
537 if ($cgiparams{'ENABLE_IDS'} eq "on") {
724f98c0 538 # Check if at least one provider is enabled. Otherwise abort and display an error.
4b6cf2a5
SS
539 unless(@enabled_providers) {
540 $errormessage = $Lang::tr{'ids no enabled ruleset provider'};
ebdd0f9a
SS
541 }
542
543 # Loop through the array of available interfaces.
544 foreach my $zone (@network_zones) {
545 # Convert interface name into upper case.
546 my $zone_upper = uc($zone);
547
800290ce 548 # Check if the IDS is enabled for these interfaces.
ebdd0f9a
SS
549 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
550 # Increase count.
551 $monitored_zones++;
552 }
553 }
554
555 # Check if at least one zone should be monitored, or show an error.
556 unless ($monitored_zones >= 1) {
557 $errormessage = $Lang::tr{'ids no network zone'};
558 }
559 }
560
a232b58c
SS
561 # Go on if there are no error messages.
562 if (!$errormessage) {
563 # Store settings into settings file.
b02e30fd 564 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
a9a91e5f 565 }
8d2f6b0b 566
77351a6b
SS
567 # Check if the the automatic rule update hass been touched.
568 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
569 # Call suricatactrl to set the new interval.
570 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
571 }
572
8d2f6b0b 573 # Generate file to store the home net.
b02e30fd 574 &IDS::generate_home_net_file();
e2e7880d 575
a40ee6b9
SS
576 # Generate file to the store the DNS servers.
577 &IDS::generate_dns_servers_file();
578
e698090e
SS
579 # Generate file to store the HTTP ports.
580 &IDS::generate_http_ports_file();
581
e2e7880d
SS
582 # Check if the IDS currently is running.
583 if(&IDS::ids_is_running()) {
584 # Check if ENABLE_IDS is set to on.
585 if($cgiparams{'ENABLE_IDS'} eq "on") {
586 # Call suricatactrl to perform a reload of suricata.
587 &IDS::call_suricatactrl("reload");
588 } else {
589 # Call suricatactrl to stop suricata.
590 &IDS::call_suricatactrl("stop");
591 }
592 } else {
593 # Call suricatactrl to start suricata.
594 &IDS::call_suricatactrl("start");
595 }
bbb6efae
SS
596
597 # Check if the page should be reloaded.
598 if ($reload_page) {
599 # Perform a reload of the page.
600 &reload();
601 }
4c067847 602
9bf260de
SS
603# Toggle Enable/Disable autoupdate for a provider
604} elsif ($cgiparams{'AUTOUPDATE'} eq $Lang::tr{'toggle enable disable'}) {
605 my %used_providers = ();
606
607 # Only go further, if an ID has been passed.
608 if ($cgiparams{'ID'}) {
609 # Assign the given ID.
610 my $id = $cgiparams{'ID'};
611
612 # Undef the given ID.
613 undef($cgiparams{'ID'});
614
615 # Read-in providers settings file.
616 &General::readhasharray($IDS::providers_settings_file, \%used_providers);
617
618 # Grab the configured status of the corresponding entry.
619 my $status_autoupdate = $used_providers{$id}[2];
620
621 # Switch the status.
622 if ($status_autoupdate eq "disabled") {
623 $status_autoupdate = "enabled";
624 } else {
625 $status_autoupdate = "disabled";
626 }
627
628 # Modify the status of the existing entry.
25652a75 629 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$status_autoupdate", "$used_providers{$id}[3]", "$used_providers{$id}[4]"];
9bf260de
SS
630
631 # Write the changed hash to the providers settings file.
632 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
633 }
634
635# Add/Edit a provider to the list of used providers.
636#
4c067847 637} elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) {
aba3cbe5
SS
638 my %used_providers = ();
639
640 # Read-in providers settings file.
641 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
642
4c067847
SS
643 # Assign some nice human-readable values.
644 my $provider = $cgiparams{'PROVIDER'};
645 my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'};
bb4c30c6 646 my $status_autoupdate;
443ad51d 647 my $mode;
bb4c30c6
SS
648
649 # Handle autoupdate checkbox.
650 if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") {
651 $status_autoupdate = "enabled";
652 } else {
653 $status_autoupdate = "disabled";
654 }
4c067847 655
443ad51d
SS
656 # Handle monitor traffic only checkbox.
657 if ($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq "on") {
658 $mode = "IDS";
659 } else {
660 $mode = "IPS";
661 }
662
4c067847
SS
663 # Check if we are going to add a new provider.
664 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") {
665 # Loop through the hash of used providers.
666 foreach my $id ( keys %used_providers) {
667 # Check if the choosen provider is already in use.
668 if ($used_providers{$id}[0] eq "$provider") {
4c067847 669 # Assign error message.
1fa18733 670 $errormessage = "$Lang::tr{'ids the choosen provider is already in use'}";
4c067847
SS
671 }
672 }
673 }
674
675 # Check if the provider requires a subscription code.
676 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
677 # Check if an subscription code has been provided.
678 if ($subscription_code) {
679 # Check if the code contains unallowed chars.
680 unless ($subscription_code =~ /^[a-z0-9]+$/) {
681 $errormessage = $Lang::tr{'invalid input for subscription code'};
682 }
683 } else {
684 # Print an error message, that an subsription code is required for this
685 # provider.
686 $errormessage = $Lang::tr{'ids subscription code required'};
687 }
688 }
689
690 # Go further if there was no error.
691 if ($errormessage eq '') {
692 my $id;
693 my $status;
694
695 # Check if we should edit an existing entry and got an ID.
696 if (($cgiparams{'PROVIDERS'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
697 # Assin the provided id.
698 $id = $cgiparams{'ID'};
699
700 # Undef the given ID.
701 undef($cgiparams{'ID'});
702
703 # Grab the configured status of the corresponding entry.
704 $status = $used_providers{$id}[3];
705 } else {
706 # Each newly added entry automatically should be enabled.
707 $status = "enabled";
708
709 # Generate the ID for the new entry.
710 #
711 # Sort the keys by their ID and store them in an array.
712 my @keys = sort { $a <=> $b } keys %used_providers;
713
714 # Reverse the key array.
715 my @reversed = reverse(@keys);
716
717 # Obtain the last used id.
718 my $last_id = @reversed[0];
719
720 # Increase the last id by one and use it as id for the new entry.
721 $id = ++$last_id;
722 }
723
724 # Add/Modify the entry to/in the used providers hash..
443ad51d 725 $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status", "$mode"];
4c067847
SS
726
727 # Write the changed hash to the providers settings file.
728 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
729
b734df0e
SS
730 # Check if a new provider will be added.
731 if ($cgiparams{'PROVIDERS'} eq $Lang::tr{'add'}) {
969983eb
SS
732 # Check if the red device is active.
733 unless (-e "${General::swroot}/red/active") {
734 $errormessage = "$Lang::tr{'ids could not add provider'} - $Lang::tr{'system is offline'}";
735 }
b734df0e 736
969983eb
SS
737 # Check if enough free disk space is availabe.
738 if(&IDS::checkdiskspace()) {
739 $errormessage = "$Lang::tr{'ids could not add provider'} - $Lang::tr{'not enough disk space'}";
740 }
b734df0e 741
969983eb
SS
742 # Check if any errors happend.
743 unless ($errormessage) {
744 # Lock the webpage and print notice about downloading
745 # a new ruleset.
746 &working_notice("$Lang::tr{'ids working'}");
b734df0e 747
969983eb
SS
748 # Download the ruleset.
749 if(&IDS::downloadruleset($provider)) {
750 $errormessage = "$Lang::tr{'ids could not add provider'} - $Lang::tr{'ids unable to download the ruleset'}";
b734df0e 751
969983eb
SS
752 # Call function to store the errormessage.
753 &IDS::_store_error_message($errormessage);
b734df0e 754
969983eb
SS
755 # Remove the configured provider again.
756 &remove_provider($id);
757 } else {
758 # Extract the ruleset
759 &IDS::extractruleset($provider);
ddaf8ae1 760
969983eb
SS
761 # Move the ruleset.
762 &IDS::move_tmp_ruleset();
763
764 # Cleanup temporary directory.
765 &IDS::cleanup_tmp_directory();
969983eb
SS
766 }
767
768 # Perform a reload of the page.
769 &reload();
fa7663a1
SS
770 } else {
771 # Remove the configured provider again.
772 &remove_provider($id);
969983eb 773 }
b734df0e
SS
774 }
775
4c067847 776 }
73eb03a3 777
969983eb
SS
778 # Undefine providers flag.
779 undef($cgiparams{'PROVIDERS'});
780
73eb03a3
SS
781## Toggle Enabled/Disabled for an existing provider.
782#
783} elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'toggle enable disable'}) {
784 my %used_providers = ();
eade5468 785 my $provider_includes_action;
73eb03a3 786
1e52a258
SS
787 # Value if oinkmaster has to be executed.
788 my $oinkmaster = "False";
789
73eb03a3
SS
790 # Only go further, if an ID has been passed.
791 if ($cgiparams{'ID'}) {
792 # Assign the given ID.
793 my $id = $cgiparams{'ID'};
794
795 # Undef the given ID.
796 undef($cgiparams{'ID'});
797
798 # Read-in file which contains the provider settings.
799 &General::readhasharray($IDS::providers_settings_file, \%used_providers);
800
801 # Grab the configured status of the corresponding entry.
802 my $status = $used_providers{$id}[3];
803
eade5468
SS
804 # Grab the provider handle.
805 my $provider_handle = $used_providers{$id}[0];
806
73eb03a3
SS
807 # Switch the status.
808 if ($status eq "enabled") {
809 $status = "disabled";
eade5468
SS
810
811 # Set the provider includes action to "remove" for removing the entry.
812 $provider_includes_action = "remove";
73eb03a3
SS
813 } else {
814 $status = "enabled";
eade5468
SS
815
816 # Set the provider includes action to "add".
817 $provider_includes_action = "add";
1e52a258
SS
818
819 # This operation requires to launch oinkmaster.
820 $oinkmaster = "True";
73eb03a3
SS
821 }
822
823 # Modify the status of the existing entry.
25652a75 824 $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$used_providers{$id}[2]", "$status", "$used_providers{$id}[4]"];
73eb03a3
SS
825
826 # Write the changed hash to the providers settings file.
827 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
828
a2b4488a
SS
829 # Get all enabled providers.
830 my @enabled_providers = &IDS::get_enabled_providers();
831
832 # Write the main providers include file.
30c4a9ff 833 &IDS::write_used_rulefiles_file(@enabled_providers);
a2b4488a 834
1e52a258
SS
835 # Check if oinkmaster has to be executed.
836 if ($oinkmaster eq "True") {
837 # Lock the webpage and print message.
838 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
839
840 # Launch oinkmaster.
841 &IDS::oinkmaster();
842 }
843
73eb03a3 844 # Check if the IDS is running.
a2b4488a
SS
845 if(&IDS::ids_is_running()) {
846 # Gather the amount of enabled providers (elements in the array).
847 my $amount = @enabled_providers;
848
849 # Check if there are still enabled ruleset providers.
850 if ($amount >= 1) {
851 # Call suricatactrl to perform a restart.
852 &IDS::call_suricatactrl("restart");
853
854 # No active ruleset provider, suricata has to be stopped.
855 } else {
856 # Stop suricata.
857 &IDS::call_suricatactrl("stop");
858 }
859 }
73eb03a3
SS
860
861 # Undefine providers flag.
862 undef($cgiparams{'PROVIDERS'});
1e52a258
SS
863
864 # Reload page.
865 &reload();
73eb03a3
SS
866 }
867
868## Remove provider from the list of used providers.
869#
870} elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'remove'}) {
214f34ec
SS
871 # Assign a nice human-readable variable.
872 my $id = $cgiparams{'ID'};
73eb03a3 873
214f34ec
SS
874 # Grab the provider name bevore deleting.
875 my $provider = &get_provider_handle($id);
2fded6d2 876
214f34ec
SS
877 # Remove the provider.
878 &remove_provider($id);
73eb03a3
SS
879
880 # Undef the given ID.
881 undef($cgiparams{'ID'});
882
106f00bd
SS
883 # Lock the webpage and print message.
884 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
885
2fded6d2
SS
886 # Drop the stored ruleset file.
887 &IDS::drop_dl_rulesfile($provider);
888
889 # Get the name of the provider rulessets include file.
30c4a9ff 890 my $provider_used_rulefile = &IDS::get_provider_used_rulesfiles_file($provider);
2fded6d2
SS
891
892 # Drop the file, it is not longer needed.
893 unlink("$provider_used_rulefile");
894
0c5b2f6d 895 # Call function to get the path and name for the given providers
4c98be8b
SS
896 # ruleset modifications file..
897 my $modifications_file = &IDS::get_provider_ruleset_modifications_file($provider);
0c5b2f6d
SS
898
899 # Check if the file exists.
4c98be8b 900 if (-f $modifications_file) {
0c5b2f6d 901 # Remove the file, which is not longer needed.
4c98be8b 902 unlink("$modifications_file");
0c5b2f6d
SS
903 }
904
2fded6d2
SS
905 # Regenerate ruleset.
906 &IDS::oinkmaster();
907
908 # Gather all enabled providers.
909 my @enabled_providers = &IDS::get_enabled_providers();
910
911 # Regenerate main providers include file.
30c4a9ff 912 &IDS::write_used_rulefiles_file(@enabled_providers);
2fded6d2 913
73eb03a3 914 # Check if the IDS is running.
2fded6d2
SS
915 if(&IDS::ids_is_running()) {
916 # Get amount of enabled providers.
917 my $amount = @enabled_providers;
918
919 # Check if at least one enabled provider remains.
920 if ($amount >= 1) {
921 # Call suricatactrl to perform a reload.
922 &IDS::call_suricatactrl("restart");
923
924 # Stop suricata if no enabled provider remains.
925 } else {
926 # Call suricatactrel to perform the stop.
927 &IDS::call_suricatactrl("stop");
928 }
929 }
1b939d0e 930
73eb03a3
SS
931 # Undefine providers flag.
932 undef($cgiparams{'PROVIDERS'});
106f00bd
SS
933
934 # Reload page.
935 &reload();
ac1cfefa
MT
936}
937
ac1cfefa
MT
938&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
939
2bbe6ede 940&Header::openbigbox('100%', 'left', '', $errormessage);
e0cec9fe 941
2bbe6ede 942&show_display_error_message();
0d34a479 943
2bbe6ede
SS
944if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) {
945 &show_customize_ruleset();
2f252efa
SS
946} elsif ($cgiparams{'PROVIDERS'} ne "") {
947 &show_add_provider();
2bbe6ede
SS
948} else {
949 &show_mainpage();
950}
029b8ed2 951
2bbe6ede
SS
952&Header::closebigbox();
953&Header::closepage();
e0cec9fe 954
2bbe6ede
SS
955#
956## Tiny function to show if a error message happened.
957#
958sub show_display_error_message() {
959 if ($errormessage) {
960 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
961 print "<class name='base'>$errormessage\n";
962 print "&nbsp;</class>\n";
963 &Header::closebox();
964 }
965}
e0cec9fe 966
2bbe6ede
SS
967#
968## Function to display the main IDS page.
969#
970sub show_mainpage() {
aba3cbe5 971 # Read-in idssettings and provider settings.
2bbe6ede 972 &General::readhash("$IDS::ids_settings_file", \%idssettings);
aba3cbe5 973 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
2bbe6ede
SS
974
975 # If no autoupdate intervall has been configured yet, set default value.
77351a6b 976 unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) {
2bbe6ede 977 # Set default to "weekly".
77351a6b 978 $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
17726644 979 }
2bbe6ede
SS
980
981 # Read-in ignored hosts.
18f0991c 982 &General::readhasharray("$IDS::ignored_file", \%ignored) if (-e $IDS::ignored_file);
2bbe6ede
SS
983
984 $checked{'ENABLE_IDS'}{'off'} = '';
985 $checked{'ENABLE_IDS'}{'on'} = '';
986 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
2bbe6ede
SS
987 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
988 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
989 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
2f252efa 990 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
17726644 991
2bbe6ede
SS
992 # Draw current state of the IDS
993 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375 994
2bbe6ede
SS
995 # Check if the IDS is running and obtain the process-id.
996 my $pid = &IDS::ids_is_running();
87660964 997
2bbe6ede
SS
998 # Display some useful information, if suricata daemon is running.
999 if ($pid) {
1000 # Gather used memory.
1001 my $memory = &get_memory_usage($pid);
87660964 1002
2bbe6ede
SS
1003 print <<END;
1004 <table width='95%' cellspacing='0' class='tbl'>
1005 <tr>
1006 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
1007 </tr>
87660964 1008
2bbe6ede
SS
1009 <tr>
1010 <td class='base'>$Lang::tr{'guardian daemon'}</td>
1011 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
1012 </tr>
87660964 1013
2bbe6ede
SS
1014 <tr>
1015 <td class='base'></td>
1016 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
1017 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
1018 </tr>
87660964 1019
2bbe6ede
SS
1020 <tr>
1021 <td class='base'></td>
1022 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
1023 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
1024 </tr>
1025 </table>
87660964 1026END
2bbe6ede
SS
1027 } else {
1028 # Otherwise display a hint that the service is not launched.
1029 print <<END;
1030 <table width='95%' cellspacing='0' class='tbl'>
1031 <tr>
1032 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
1033 </tr>
87660964 1034
2bbe6ede
SS
1035 <tr>
1036 <td class='base'>$Lang::tr{'guardian daemon'}</td>
1037 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
1038 </tr>
1039 </table>
87660964 1040END
2bbe6ede 1041 }
87660964 1042
2f252efa
SS
1043 # Only show this area, if at least one ruleset provider is configured.
1044 if (%used_providers) {
674912fc 1045
2bbe6ede 1046print <<END
674912fc 1047
2bbe6ede 1048 <br><br><h2>$Lang::tr{'settings'}</h2>
1286e0d4 1049
2bbe6ede
SS
1050 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1051 <table width='100%' border='0'>
1052 <tr>
1053 <td class='base' colspan='2'>
1054 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>&nbsp;$Lang::tr{'ids enable'}
1055 </td>
cf02bf2f 1056
2bbe6ede
SS
1057 </td>
1058 </tr>
1504a375 1059
2bbe6ede
SS
1060 <tr>
1061 <td><br><br></td>
1062 <td><br><br></td>
1063 <td><br><br></td>
1064 <td><br><br></td>
1065 </tr>
a4ccfcbb 1066
2bbe6ede
SS
1067 <tr>
1068 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
1069 </tr>
a4ccfcbb 1070
2bbe6ede 1071 <tr>
ac1cfefa
MT
1072END
1073;
1504a375 1074
2bbe6ede
SS
1075 # Loop through the array of available networks and print config options.
1076 foreach my $zone (@network_zones) {
1077 my $checked_input;
1078 my $checked_forward;
1504a375 1079
2bbe6ede
SS
1080 # Convert current zone name to upper case.
1081 my $zone_upper = uc($zone);
1504a375 1082
2bbe6ede
SS
1083 # Set zone name.
1084 my $zone_name = $zone;
53817b89 1085
2bbe6ede
SS
1086 # Dirty hack to get the correct language string for the red zone.
1087 if ($zone eq "red") {
1088 $zone_name = "red1";
1089 }
53817b89 1090
2bbe6ede
SS
1091 # Grab checkbox status from settings hash.
1092 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
1093 $checked_input = "checked = 'checked'";
1094 }
1504a375 1095
2bbe6ede
SS
1096 print "<td class='base' width='20%'>\n";
1097 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
1098 print "&nbsp;$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
1099 print "</td>\n";
1100 }
1b73b07e 1101
ac1cfefa 1102print <<END
2bbe6ede 1103 </tr>
77351a6b
SS
1104
1105 <tr>
1106 <td><br><br></td>
1107 <td><br><br></td>
1108 <td><br><br></td>
1109 <td><br><br></td>
1110 </tr>
1111
1112 <tr>
1113 <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td>
1114 </tr>
1115
1116 <tr>
1117 <td>
1118 <select name='AUTOUPDATE_INTERVAL'>
1119 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
1120 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
1121 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
1122 </select>
1123 </td>
1124 </tr>
2bbe6ede 1125 </table>
1504a375 1126
2bbe6ede 1127 <br><br>
ea5c8eeb 1128
2bbe6ede
SS
1129 <table width='100%'>
1130 <tr>
1131 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1132 </tr>
1133 </table>
1134 </form>
ea5c8eeb
SS
1135END
1136;
1137
2bbe6ede 1138 }
cf02bf2f 1139
2bbe6ede 1140 &Header::closebox();
ea5c8eeb 1141
2f252efa
SS
1142 #
1143 # Used Ruleset Providers section.
1144 #
2bbe6ede 1145 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
ea5c8eeb 1146
2f252efa
SS
1147print <<END;
1148 <table width='100%' border='0'>
1149 <tr>
1150 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td>
1151 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td>
1152 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td>
834227f2 1153 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'action'}</b></td>
2f252efa
SS
1154 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1155 </tr>
3e12c6e6 1156END
7e1a09f9
SS
1157 my $line = 1;
1158
2f252efa
SS
1159 # Check if some providers has been configured.
1160 if (keys (%used_providers)) {
1161 my $col = "";
3e12c6e6 1162
2f252efa 1163 # Loop through all entries of the hash.
2f252efa
SS
1164 foreach my $id (sort keys(%used_providers)) {
1165 # Assign data array positions to some nice variable names.
1166 my $provider = $used_providers{$id}[0];
1167 my $provider_name = &get_provider_name($provider);
593abb35 1168 my $rulesetdate = &IDS::get_ruleset_date($provider);
2f252efa
SS
1169
1170 my $subscription_code = $used_providers{$id}[1];
1171 my $autoupdate_status = $used_providers{$id}[2];
1172 my $status = $used_providers{$id}[3];
1173
1174 # Check if the item number is even or not.
7e1a09f9 1175 if ($line % 2) {
2f252efa
SS
1176 $col="bgcolor='$color{'color22'}'";
1177 } else {
1178 $col="bgcolor='$color{'color20'}'";
2bbe6ede 1179 }
1504a375 1180
2f252efa
SS
1181 # Choose icons for the checkboxes.
1182 my $status_gif;
1183 my $status_gdesc;
1184 my $autoupdate_status_gif;
1185 my $autoupdate_status_gdesc;
1504a375 1186
2f252efa
SS
1187 # Check if the status is enabled and select the correct image and description.
1188 if ($status eq 'enabled' ) {
1189 $status_gif = 'on.gif';
1190 $status_gdesc = $Lang::tr{'click to disable'};
1191 } else {
1192 $status_gif = 'off.gif';
1193 $status_gdesc = $Lang::tr{'click to enable'};
1194 }
1504a375 1195
2f252efa
SS
1196 # Check if the autoupdate status is enabled and select the correct image and description.
1197 if ($autoupdate_status eq 'enabled') {
1198 $autoupdate_status_gif = 'on.gif';
1199 $autoupdate_status_gdesc = $Lang::tr{'click to disable'};
1200 } else {
1201 $autoupdate_status_gif = 'off.gif';
1202 $autoupdate_status_gdesc = $Lang::tr{'click to enable'};
1203 }
2bbe6ede 1204
2f252efa
SS
1205print <<END;
1206 <tr>
1207 <td width='33%' class='base' $col>$provider_name</td>
593abb35 1208 <td width='30%' class='base' $col>$rulesetdate</td>
2f252efa
SS
1209
1210 <td align='center' $col>
1211 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1212 <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' />
1213 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' />
1214 <input type='hidden' name='ID' value='$id' />
1215 </form>
1216 </td>
1217
1218 <td align='center' $col>
7323c72d 1219 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
2f252efa
SS
1220 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'>
1221 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'>
1222 <input type='hidden' name='ID' value='$id'>
1223 </form>
1224 </td>
1225
1226 <td align='center' $col>
1227 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1228 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'>
1229 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1230 <input type='hidden' name='ID' value='$id'>
1231 </form>
1232 </td>
1504a375 1233
2f252efa
SS
1234 <td align='center' $col>
1235 <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'>
1236 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1237 <input type='hidden' name='ID' value='$id'>
1238 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'>
1239 </form>
1240 </td>
1241 </tr>
ea5c8eeb 1242END
7e1a09f9
SS
1243 # Increment lines value.
1244 $line++;
1245
2bbe6ede 1246 }
2f252efa
SS
1247
1248 } else {
1249 # Print notice that currently no hosts are ignored.
1250 print "<tr>\n";
1251 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1252 print "</tr>\n";
1253 }
1254
1255 print "</table>\n";
1256
1257 # Section to add new elements or edit existing ones.
ea5c8eeb 1258print <<END;
2f252efa
SS
1259 <br>
1260 <hr>
1261 <br>
1504a375 1262
2f252efa
SS
1263 <div align='right'>
1264 <table width='100%'>
1265 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1266 <tr>
1267END
1268
1269 # Only show this button if a ruleset provider is configured.
1270 if (%used_providers) {
1271 print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n";
1272 }
1273print <<END;
1274 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'>
1275 </tr>
1276 </form>
2bbe6ede 1277 </table>
2f252efa 1278 </div>
ac1cfefa 1279END
ac1cfefa 1280
2bbe6ede 1281 &Header::closebox();
fbfdb241 1282
2bbe6ede
SS
1283 #
1284 # Whitelist / Ignorelist
1285 #
1286 &Header::openbox('100%', 'center', $Lang::tr{'ids ignored hosts'});
b7e29743 1287
2bbe6ede 1288 print <<END;
b7e29743
SS
1289 <table width='100%'>
1290 <tr>
1291 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
1292 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
1293 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1294 </tr>
1295END
1296 # Check if some hosts have been added to be ignored.
1297 if (keys (%ignored)) {
1298 my $col = "";
1299
1300 # Loop through all entries of the hash.
1301 while( (my $key) = each %ignored) {
1302 # Assign data array positions to some nice variable names.
1303 my $address = $ignored{$key}[0];
1304 my $remark = $ignored{$key}[1];
1305 my $status = $ignored{$key}[2];
1306
1307 # Check if the key (id) number is even or not.
1308 if ($cgiparams{'ID'} eq $key) {
1309 $col="bgcolor='${Header::colouryellow}'";
1310 } elsif ($key % 2) {
1311 $col="bgcolor='$color{'color22'}'";
1312 } else {
1313 $col="bgcolor='$color{'color20'}'";
1314 }
1315
1316 # Choose icon for the checkbox.
1317 my $gif;
1318 my $gdesc;
1319
1320 # Check if the status is enabled and select the correct image and description.
1321 if ($status eq 'enabled' ) {
1322 $gif = 'on.gif';
1323 $gdesc = $Lang::tr{'click to disable'};
1324 } else {
1325 $gif = 'off.gif';
1326 $gdesc = $Lang::tr{'click to enable'};
1327 }
1328
1329print <<END;
1330 <tr>
1331 <td width='20%' class='base' $col>$address</td>
1332 <td width='65%' class='base' $col>$remark</td>
1333
1334 <td align='center' $col>
1335 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
2f252efa
SS
1336 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'>
1337 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'>
1338 <input type='hidden' name='ID' value='$key'>
b7e29743
SS
1339 </form>
1340 </td>
1341
1342 <td align='center' $col>
1343 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
2f252efa
SS
1344 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'>
1345 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1346 <input type='hidden' name='ID' value='$key'>
b7e29743
SS
1347 </form>
1348 </td>
1349
1350 <td align='center' $col>
1351 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1352 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1353 <input type='hidden' name='ID' value='$key'>
1354 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1355 </form>
2bbe6ede
SS
1356 </td>
1357 </tr>
b7e29743 1358END
2bbe6ede
SS
1359 }
1360 } else {
1361 # Print notice that currently no hosts are ignored.
1362 print "<tr>\n";
1363 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1364 print "</tr>\n";
b7e29743 1365 }
b7e29743 1366
2bbe6ede 1367 print "</table>\n";
b7e29743 1368
2bbe6ede 1369 # Section to add new elements or edit existing ones.
b7e29743 1370print <<END;
2bbe6ede
SS
1371 <br>
1372 <hr>
1373 <br>
1b939d0e 1374
2bbe6ede
SS
1375 <div align='center'>
1376 <table width='100%'>
b7e29743
SS
1377END
1378
2bbe6ede
SS
1379 # Assign correct headline and button text.
1380 my $buttontext;
1381 my $entry_address;
1382 my $entry_remark;
b7e29743 1383
2bbe6ede
SS
1384 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1385 if ($cgiparams{'ID'} ne '') {
1386 $buttontext = $Lang::tr{'update'};
1387 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
b7e29743 1388
2bbe6ede
SS
1389 # Grab address and remark for the given key.
1390 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1391 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1392 } else {
1393 $buttontext = $Lang::tr{'add'};
1394 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1395 }
b7e29743
SS
1396
1397print <<END;
2bbe6ede
SS
1398 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1399 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1400 <tr>
1401 <td width='30%'>$Lang::tr{'ip address'}: </td>
1402 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
b7e29743 1403
2bbe6ede
SS
1404 <td width='30%'>$Lang::tr{'remark'}: </td>
1405 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1406 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1407 </tr>
1408 </form>
1409 </table>
1410 </div>
b7e29743
SS
1411END
1412
2bbe6ede 1413 &Header::closebox();
fed57fe7
SS
1414}
1415
fed57fe7
SS
1416#
1417## Function to show the customize ruleset section.
1418#
1419sub show_customize_ruleset() {
2bbe6ede
SS
1420 ### Java Script ###
1421 print"<script>\n";
1422
1423 # Java script variable declaration for show and hide.
1424 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
1425 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
1426
1427print <<END
1b939d0e 1428 // Tiny javascript function to show/hide the rules
2bbe6ede
SS
1429 // of a given category.
1430 function showhide(tblname) {
1431 \$("#" + tblname).toggle();
1432
1433 // Get current content of the span element.
1434 var content = document.getElementById("span_" + tblname);
1435
1436 if (content.innerHTML === show) {
1437 content.innerHTML = hide;
1438 } else {
1439 content.innerHTML = show;
1440 }
1441 }
1442 </script>
1443END
1444;
87df37da 1445 &Header::openbox('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" );
1b939d0e 1446 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
f7fcd1c0 1447
1b939d0e
PM
1448 # Output display table for rule files
1449 print "<table width='100%'>\n";
3ffee04b 1450
1b939d0e
PM
1451 # Loop over each rule file
1452 foreach my $rulefile (sort keys(%idsrules)) {
1453 my $rulechecked = '';
0a1bba1a 1454
1b939d0e
PM
1455 # Check if rule file is enabled
1456 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1457 $rulechecked = 'CHECKED';
1458 }
80bcd4dd 1459
1b939d0e
PM
1460 # Convert rulefile name into category name.
1461 my $categoryname = &_rulefile_to_category($rulefile);
1462
1463 # Table and rows for the rule files.
1464 print"<tr>\n";
1465 print"<td class='base' width='5%'>\n";
1466 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1467 print"</td>\n";
1468 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1469 print"<td class='base' width='5%' align='right'>\n";
1470 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1471 print"</td>\n";
1472 print"</tr>\n";
1473
1474 # Rows which will be hidden per default and will contain the single rules.
1475 print"<tr style='display:none' id='$categoryname'>\n";
1476 print"<td colspan='3'>\n";
1477
1478 # Local vars
1479 my $lines;
1480 my $rows;
1481 my $col;
1482
1483 # New table for the single rules.
1484 print "<table width='100%'>\n";
17726644 1485
1b939d0e
PM
1486 # Loop over rule file rules
1487 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
f7fcd1c0 1488 # Local vars
1b939d0e 1489 my $ruledefchecked = '';
f9c2147d 1490
1b939d0e
PM
1491 # Skip rulefile itself.
1492 next if ($sid eq "Rulefile");
f7fcd1c0 1493
1b939d0e
PM
1494 # If 2 rules have been displayed, start a new row
1495 if (($lines % 2) == 0) {
1496 print "</tr><tr>\n";
80bcd4dd 1497
1b939d0e
PM
1498 # Increase rows by once.
1499 $rows++;
1500 }
80bcd4dd 1501
1b939d0e
PM
1502 # Colour lines.
1503 if ($rows % 2) {
1504 $col="bgcolor='$color{'color20'}'";
1505 } else {
1506 $col="bgcolor='$color{'color22'}'";
f7fcd1c0 1507 }
3ffee04b 1508
1b939d0e
PM
1509 # Set rule state
1510 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1511 $ruledefchecked = 'CHECKED';
80bcd4dd 1512 }
17726644 1513
1b939d0e
PM
1514 # Create rule checkbox and display rule description
1515 print "<td class='base' width='5%' align='right' $col>\n";
1516 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1517 print "</td>\n";
1518 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1519
1520 # Increment rule count
1521 $lines++;
1522 }
1523
1524 # If do not have a second rule for row, create empty cell
1525 if (($lines % 2) != 0) {
1526 print "<td class='base'></td>";
f7fcd1c0
SS
1527 }
1528
1529 # Close display table
1b939d0e
PM
1530 print "</tr></table></td></tr>";
1531 }
17726644 1532
1b939d0e
PM
1533 # Close display table
1534 print "</table>";
1535
1536 print <<END
2999f1d2
CS
1537<table width='100%'>
1538<tr>
4efc8ccd
SS
1539 <td width='100%' align='right'>
1540 <input type='submit' value='$Lang::tr{'fwhost back'}'>
1541 <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'>
1542 </td>
2999f1d2
CS
1543</tr>
1544</table>
298723b9 1545</form>
3ffee04b
CS
1546END
1547;
1b939d0e 1548 &Header::closebox();
80bcd4dd
SS
1549}
1550
2f252efa
SS
1551#
1552## Function to show section for add/edit a provider.
1553#
1554sub show_add_provider() {
aba3cbe5 1555 my %used_providers = ();
2f252efa
SS
1556 my @subscription_providers;
1557
aba3cbe5
SS
1558 # Read -in providers settings file.
1559 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
1560
2f252efa
SS
1561 # Get all supported ruleset providers.
1562 my @ruleset_providers = &IDS::get_ruleset_providers();
1563
1564 ### Java Script ###
1565 print "<script>\n";
1566
1567 # Generate Java Script Object which contains the URL of the providers.
1568 print "\t// Object, which contains the webpages of the ruleset providers.\n";
1569 print "\tvar url = {\n";
1570
1571 # Loop through the array of supported providers.
1572 foreach my $provider (@ruleset_providers) {
1573 # Check if the provider requires a subscription.
1574 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
1575 # Add the provider to the array of subscription_providers.
1576 push(@subscription_providers, $provider);
1577 }
1578
1579 # Grab the URL for the provider.
1580 my $url = $IDS::Ruleset::Providers{$provider}{'website'};
1581
1582 # Print the URL to the Java Script Object.
1583 print "\t\t$provider: \"$url\"\,\n";
1584 }
1585
1586 # Close the Java Script Object declaration.
1587 print "\t}\;\n\n";
1588
1589 # Generate Java Script Array which contains the provider that requires a subscription.
1590 my $line = "";
1591 $line = join("', '", @subscription_providers);
1592
1593 print "\t// Array which contains the providers that requires a subscription.\n";
1594 print "\tsubscription_provider = ['$line']\;\n\n";
1595
1596print <<END
1597 // Java Script function to swap the text input field for
1598 // entering a subscription code.
1599 var update_provider = function() {
1600 if(inArray(\$('#PROVIDER').val(), subscription_provider)) {
1601 \$('.subscription_code').show();
1602 } else {
1603 \$('.subscription_code').hide();
1604 }
1605
1606 // Call function to change the website url.
1607 change_url(\$('#PROVIDER').val());
1608 };
1609
1610 // Java Script function to check if a given value is part of
1611 // an array.
1612 function inArray(value,array) {
1613 var count=array.length;
1614
1615 for(var i=0;i<count;i++) {
1616 if(array[i]===value){
1617 return true;
1618 }
1619 }
1620
1621 return false;
1622 }
1623
1624 // Tiny function to change the website url based on the selected element in the "PROVIDERS"
1625 // dropdown menu.
1626 function change_url(provider) {
1627 // Get and change the href to the corresponding url.
1628 document.getElementById("website").href = url[provider];
1629 }
1630
1631 // JQuery function to call corresponding function when
1632 // the ruleset provider is changed or the page is loaded for showing/hiding
1633 // the subscription_code area.
1634 \$(document).ready(function() {
1635 \$('#PROVIDER').change(update_provider);
1636 update_provider();
1637 });
1638
1639 </script>
1640END
1641;
1642
2f252efa
SS
1643 # Check if an existing provider should be edited.
1644 if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1645 # Check if autoupdate is enabled for this provider.
bb4c30c6 1646 if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") {
2f252efa
SS
1647 # Set the checkbox to be checked.
1648 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1649 }
962e58cd 1650
443ad51d
SS
1651 # Check if the monitor traffic only mode is set for this provider.
1652 if ($used_providers{$cgiparams{'ID'}}[4] eq "IDS") {
1653 # Set the checkbox to be checked.
1654 $checked{'MONITOR_TRAFFIC_ONLY'} = "checked='checked'";
1655 }
1656
962e58cd
SS
1657 # Display section to force an rules update and to reset the provider.
1658 &show_additional_provider_actions();
1659
2f252efa
SS
1660 } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") {
1661 # Set the autoupdate to true as default.
1662 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1663 }
1664
962e58cd
SS
1665 &Header::openbox('100%', 'center', $Lang::tr{'ids provider settings'});
1666
2f252efa
SS
1667print <<END
1668 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1669 <table width='100%' border='0'>
1670 <tr>
1671 <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td>
1672 </tr>
1673
1674 <tr>
1675 <td width='40%'>
1676 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
2f252efa
SS
1677END
1678;
02fee15e
SS
1679 # Value to allow disabling the dropdown menu.
1680 my $disabled;
1681
1682 # Check if we are in edit mode.
1683 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1684 $disabled = "disabled";
1685
1686 # Add hidden input with the provider because the disable select does not provider
1687 # this.
1688 print "<input type='hidden' name='PROVIDER' value='$used_providers{$cgiparams{'ID'}}[0]'>\n";
1689 }
1690
1691 print "<select name='PROVIDER' id='PROVIDER' $disabled>\n";
4015d3f4
SS
1692 # Temporary hash to store the provier names and their handles.
1693 my %tmphash = ();
1694
2f252efa 1695 # Loop through the array of ruleset providers.
4015d3f4 1696 foreach my $handle (@ruleset_providers) {
2f252efa 1697 # Get the provider name.
4015d3f4
SS
1698 my $name = &get_provider_name($handle);
1699
1700 # Add the grabbed provider name and handle to the
1701 # temporary hash.
1702 $tmphash{$name} = "$handle";
1703 }
1704
1705 # Sort and loop through the temporary hash.
1706 foreach my $provider_name ( sort keys %tmphash ) {
1707 # Grab the provider handle.
1708 my $provider = $tmphash{$provider_name};
2f252efa
SS
1709
1710 # Pre-select the provider if one is given.
1711 if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) {
1712 $selected{$provider} = "selected='selected'";
1713 }
1714
1715 # Add the provider to the dropdown menu.
1716 print "<option value='$provider' $selected{$provider}>$provider_name</option>\n";
1717 }
1718print <<END
1719 </select>
1720 </td>
1721
1722 <td width='60%'>
1723 <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b>
1724 </td>
1725 </tr>
1726
1727 <tr>
1728 <td colspan='2'><br><br></td>
1729 </tr>
1730
1731 <tr class='subscription_code' style='display:none' id='subscription_code'>
1732 <td colspan='2'>
1733 <table border='0'>
1734 <tr>
1735 <td>
1736 <b>$Lang::tr{'subscription code'}</b>
1737 </td>
1738 </tr>
1739
1740 <tr>
1741 <td>
1742 <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'>
1743 </td>
1744 </tr>
1745
1746 <tr>
1747 <td><br><br></td>
1748 </tr>
1749 </table>
1750 </td>
1751 </tr>
1752
1753 <tr>
443ad51d 1754 <td>
2f252efa
SS
1755 <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}>&nbsp;$Lang::tr{'ids enable automatic updates'}
1756 </td>
443ad51d
SS
1757
1758 <td>
1759 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}>&nbsp;$Lang::tr{'ids monitor traffic only'}
1760 </td>
2f252efa
SS
1761 </tr>
1762
1763 <tr>
1764 <td colspan='2' align='right'>
1765 <input type='submit' value='$Lang::tr{'back'}'>
1766END
1767;
1768 # Check if a provider should be added or edited.
1769 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1770 # Display button for updating the existing provider.
1771 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n";
1772 } else {
1773 # Display button to add the new provider.
1774 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n";
1775 }
1776print <<END
1777 </td>
1778 </tr>
1779 </table>
1780 </form>
1781END
1782;
1783 &Header::closebox();
1784}
1785
962e58cd
SS
1786#
1787## Function to show the area where additional provider actions can be done.
1788#
1789sub show_additional_provider_actions() {
1790 my $disabled;
1791 my %used_providers = ();
1792
1793 # Read-in providers settings file.
1794 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
1795
1796 # Assign variable for provider handle.
1797 my $provider = "$used_providers{$cgiparams{'ID'}}[0]";
1798
4c98be8b
SS
1799 # Call function to get the path and name for the given provider
1800 # ruleset modifications file.
1801 my $modifications_file = &IDS::get_provider_ruleset_modifications_file($provider);
962e58cd
SS
1802
1803 # Disable the reset provider button if no provider modified sids file exists.
4c98be8b 1804 unless (-f $modifications_file) {
962e58cd
SS
1805 $disabled = "disabled";
1806 }
1807
1808 &Header::openbox('100%', 'center', "");
1809 print <<END
1810 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1811 <table width='100%' border="0">
1812 <tr>
1813 <td align='center'>
1814 <input type='hidden' name='PROVIDER' value='$provider'>
1815 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids reset provider'}' $disabled>
1816 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids force ruleset update'}'>
1817 </td>
1818 </tr>
1819 </table>
1b939d0e 1820 </form>
962e58cd
SS
1821END
1822;
1823 &Header::closebox();
1824}
1825
27760092
SS
1826#
1827## A function to display a notice, to lock the webpage and
1828## tell the user which action currently will be performed.
1829#
1830sub working_notice ($) {
1831 my ($message) = @_;
1832
1aaa3477
SS
1833 &_open_working_notice ($message);
1834 &_close_working_notice();
1835}
1836
1837#
1838## Private function to lock the page and tell the user what is going on.
1839#
1840sub _open_working_notice ($) {
1841 my ($message) = @_;
1842
27760092
SS
1843 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1844 &Header::openbigbox('100%', 'left', '', $errormessage);
1845 &Header::openbox( 'Waiting', 1,);
1846 print <<END;
1847 <table>
1848 <tr>
1849 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1850 <td>$message</td>
1851 </tr>
27760092 1852END
1aaa3477
SS
1853}
1854
1855#
1856## Private function to close a working notice.
1857#
1858sub _close_working_notice () {
1859 print "</table>\n";
1860
27760092
SS
1861 &Header::closebox();
1862 &Header::closebigbox();
1863 &Header::closepage();
1864}
1865
44d41fd6
SS
1866#
1867## Function which locks the webpage and basically does the same as the
1868## oinkmaster function, but provides a lot of HTML formated status output.
1869#
1870sub oinkmaster_web () {
1871 my @enabled_providers = &IDS::get_enabled_providers();
1872
1873 # Lock the webpage and print message.
1874 &_open_working_notice("$Lang::tr{'ids apply ruleset changes'}");
1875
1876 # Check if the files in rulesdir have the correct permissions.
1877 &IDS::_check_rulesdir_permissions();
1878
1879 print "<tr><td><br><br></td></tr>\n";
1880
1881 # Cleanup the rules directory before filling it with the new rulests.
1882 &_add_to_notice("Remove old rule structures...");
1883 &IDS::_cleanup_rulesdir();
1884
1885 # Loop through the array of enabled providers.
1886 foreach my $provider (@enabled_providers) {
1887 &_add_to_notice("Extracting ruleset for provider: $provider");
1888 # Call the extractruleset function.
1889 &IDS::extractruleset($provider);
1890 }
1891
1892 # Call function to process the ruleset and do all modifications.
1893 &_add_to_notice("Adjust rules and add user defined customizations...");
1894 &IDS::process_ruleset(@enabled_providers);
1895
1896 # Call function to merge the classification files.
1897 &_add_to_notice("Merging classifications ...");
1898 &IDS::merge_classifications(@enabled_providers);
1899
1900 # Call function to merge the sid to message mapping files.
1901 &_add_to_notice("Merging sid to message files ...");
1902 &IDS::merge_sid_msg(@enabled_providers);
1903
1904 # Cleanup temporary directory.
1905 &_add_to_notice("Cleanup temporary directory ...");
1906 &IDS::cleanup_tmp_directory();
1907
1908 # Finished - print a notice.
1909 &_add_to_notice("Finished...");
1910
1911 # Close the working notice.
1912 &_close_working_notice();
1913}
1914
1915#
1916## Tiny private function to add a given message to a notice table.
1917#
1918sub _add_to_notice ($) {
1919 my ($message) = @_;
1920
1921 print "<tr><td colspan='2'><li><b>$message</b></li></td></tr>\n";
1922}
1923
3983aebd
SS
1924#
1925## A tiny function to perform a reload of the webpage after one second.
1926#
1927sub reload () {
1928 print "<meta http-equiv='refresh' content='1'>\n";
1929
1930 # Stop the script.
1931 exit;
a70d269a
SS
1932}
1933
25f5cb0d
SS
1934#
1935## Private function to read-in and parse rules of a given rulefile.
1936#
1937## The given file will be read, parsed and all valid rules will be stored by ID,
9d18656b 1938## message/description and it's state in the idsrules hash.
25f5cb0d 1939#
3da6e01b
SS
1940sub readrulesfile ($) {
1941 my $rulefile = shift;
1942
1943 # Open rule file and read in contents
298ef5ba 1944 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
3da6e01b
SS
1945
1946 # Store file content in an array.
1947 my @lines = <RULEFILE>;
1948
1949 # Close file.
1950 close(RULEFILE);
1951
1952 # Loop over rule file contents
1953 foreach my $line (@lines) {
1954 # Remove whitespaces.
1955 chomp $line;
1956
1957 # Skip blank lines.
1958 next if ($line =~ /^\s*$/);
1959
1960 # Local vars.
1961 my $sid;
1962 my $msg;
1963
1964 # Gather rule sid and message from the ruleline.
4d438241 1965 if ($line =~ m/.*msg:\s*\"(.*?)\"\;.*sid:\s*(.*?); /) {
3da6e01b
SS
1966 $msg = $1;
1967 $sid = $2;
1968
1969 # Check if a rule has been found.
1970 if ($sid && $msg) {
9d18656b
SS
1971 # Add rule to the idsrules hash.
1972 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
3da6e01b
SS
1973
1974 # Grab status of the rule. Check if ruleline starts with a "dash".
1975 if ($line =~ /^\#/) {
1976 # If yes, the rule is disabled.
9d18656b 1977 $idsrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
1978 } else {
1979 # Otherwise the rule is enabled.
9d18656b 1980 $idsrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
1981 }
1982 }
1983 }
b7e29743 1984 }
3da6e01b 1985}
87660964 1986
8d2f6b0b
SS
1987#
1988## Function to get the used memory of a given process-id.
1989#
87660964 1990sub get_memory_usage($) {
004b13b7 1991 my ($pid) = @_;
87660964 1992
004b13b7 1993 my $memory = 0;
87660964 1994
004b13b7 1995 # Try to open the status file for the given process-id on the pseudo
87660964 1996 # file system proc.
004b13b7
SS
1997 if (open(FILE, "/proc/$pid/status")) {
1998 # Loop through the entire file.
1999 while (<FILE>) {
2000 # Splitt current line content and store them into variables.
2001 my ($key, $value) = split(":", $_, 2);
2002
2003 # Check if the current key is the one which contains the memory usage.
2004 # The wanted one is VmRSS which contains the Real-memory (resident set)
2005 # of the entire process.
2006 if ($key eq "VmRSS") {
2007 # Found the memory usage add it to the memory variable.
2008 $memory += $value;
2009
2010 # Break the loop.
2011 last;
2012 }
2013 }
87660964
SS
2014
2015 # Close file handle.
004b13b7 2016 close(FILE);
87660964
SS
2017
2018 # Return memory usage.
2019 return $memory;
004b13b7 2020 }
87660964
SS
2021
2022 # If the file could not be open, return nothing.
2023 return;
2024}
2025
697787c9
SS
2026#
2027## Function to get the provider handle by a given ID.
2028#
2029sub get_provider_handle($) {
2030 my ($id) = @_;
2031
2032 my %used_providers = ();
2033
2034 # Read-in provider settings file.
2035 &General::readhasharray($IDS::providers_settings_file, \%used_providers);
2036
2037 # Obtain the provider handle for the given ID.
2038 my $provider_handle = $used_providers{$cgiparams{'ID'}}[0];
2039
2040 # Return the handle.
2041 return $provider_handle;
2042}
2043
019e5e9b
SS
2044#
2045## Function to get the provider name from the language file or providers file for a given handle.
2046#
2047sub get_provider_name($) {
2048 my ($handle) = @_;
2049 my $provider_name;
2050
2051 # Get the required translation string for the given provider handle.
2052 my $tr_string = $IDS::Ruleset::Providers{$handle}{'tr_string'};
2053
2054 # Check if the translation string is available in the language files.
2055 if ($Lang::tr{$tr_string}) {
2056 # Use the translated string from the language file.
2057 $provider_name = $Lang::tr{$tr_string};
2058 } else {
2059 # Fallback and use the provider summary from the providers file.
2060 $provider_name = $IDS::Ruleset::Providers{$handle}{'summary'};
2061 }
2062
2063 # Return the obtained provider name.
2064 return $provider_name;
2065}
2066
63cf95af
SS
2067#
2068## Function to remove a provider by a given ID.
2069#
2070sub remove_provider($) {
2071 my ($id) = @_;
2072
2073 my %used_providers = ();
2074
2075 # Read-in provider settings file.
2076 &General::readhasharray($IDS::providers_settings_file, \%used_providers);
2077
2078 # Drop entry from the hash.
2079 delete($used_providers{$id});
2080
2081 # Write the changed hash to the provider settings file.
2082 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
2083}
2084
0a1bba1a
SS
2085#
2086## Private function to convert a given rulefile to a category name.
2087## ( No file extension anymore and if the name contained a dot, it
2088## would be replaced by a underline sign.)
2089#
2090sub _rulefile_to_category($) {
2091 my ($filename) = @_;
2092
2093 # Splitt the filename into single chunks and store them in a
2094 # temorary array.
2095 my @parts = split(/\./, $filename);
2096
2097 # Return / Remove last element of the temporary array.
2098 # This removes the file extension.
2099 pop @parts;
2100
2101 # Join together the single elements of the temporary array.
2102 # If these are more than one, use a "underline" for joining.
2103 my $category = join '_', @parts;
2104
2105 # Return the converted filename.
2106 return $category;
2107}