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