]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Add code to start/stop/reload the IDS when neccessary
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2018 IPFire Team <info@ipfire.org> #
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
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "${General::swroot}/ids-functions.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %idsrules = ();
36 my %idssettings=();
37 my %rulesetsources = ();
38 my %cgiparams=();
39 my %checked=();
40 my %selected=();
41
42 # Read-in main settings, for language, theme and colors.
43 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
44 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
45
46 # Get the available network zones, based on the config type of the system and store
47 # the list of zones in an array.
48 my @network_zones = &IDS::get_available_network_zones();
49
50 # File where the used rulefiles are stored.
51 my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml";
52
53 # File where the addresses of the homenet are stored.
54 my $idshomenetfile = "$IDS::settingsdir/suricata-homenet.yaml";
55
56 my $errormessage;
57
58 &Header::showhttpheaders();
59
60 #Get GUI values
61 &Header::getcgihash(\%cgiparams);
62
63 # Check if any error has been stored.
64 if (-e $IDS::storederrorfile) {
65 # Open file to read in the stored error message.
66 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
67
68 # Read the stored error message.
69 $errormessage = <FILE>;
70
71 # Close file.
72 close (FILE);
73
74 # Delete the file, which is now not longer required.
75 unlink($IDS::storederrorfile);
76 }
77
78
79 ## Grab all available snort rules and store them in the idsrules hash.
80 #
81 # Open snort rules directory and do a directory listing.
82 opendir(DIR, $IDS::rulespath) or die $!;
83 # Loop through the direcory.
84 while (my $file = readdir(DIR)) {
85
86 # We only want files.
87 next unless (-f "$IDS::rulespath/$file");
88
89 # Ignore empty files.
90 next if (-z "$IDS::rulespath/$file");
91
92 # Use a regular expression to find files ending in .rules
93 next unless ($file =~ m/\.rules$/);
94
95 # Ignore files which are not read-able.
96 next unless (-R "$IDS::rulespath/$file");
97
98 # Call subfunction to read-in rulefile and add rules to
99 # the idsrules hash.
100 &readrulesfile("$file");
101 }
102
103 closedir(DIR);
104
105 # Gather used rulefiles.
106 #
107 # Check if the file for activated rulefiles is not empty.
108 if(-f $idsusedrulefilesfile) {
109 # Open the file for used rulefile and read-in content.
110 open(FILE, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
111
112 # Read-in content.
113 my @lines = <FILE>;
114
115 # Close file.
116 close(FILE);
117
118 # Loop through the array.
119 foreach my $line (@lines) {
120 # Remove newlines.
121 chomp($line);
122
123 # Skip comments.
124 next if ($line =~ /\#/);
125
126 # Skip blank lines.
127 next if ($line =~ /^\s*$/);
128
129 # Gather rule sid and message from the ruleline.
130 if ($line =~ /.*- (.*)/) {
131 my $rulefile = $1;
132
133 # Add the rulefile to the %idsrules hash.
134 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
135 }
136 }
137 }
138
139 # Save ruleset.
140 if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
141 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
142 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
143
144 # Arrays to store sid which should be added to the corresponding files.
145 my @enabled_sids;
146 my @disabled_sids;
147 my @enabled_rulefiles;
148
149 # Loop through the hash of idsrules.
150 foreach my $rulefile(keys %idsrules) {
151 # Check if the rulefile is enabled.
152 if ($cgiparams{$rulefile} eq "on") {
153 # Add rulefile to the array of enabled rulefiles.
154 push(@enabled_rulefiles, $rulefile);
155
156 # Drop item from cgiparams hash.
157 delete $cgiparams{$rulefile};
158 }
159 }
160
161 # Loop through the hash of idsrules.
162 foreach my $rulefile (keys %idsrules) {
163 # Loop through the single rules of the rulefile.
164 foreach my $sid (keys %{$idsrules{$rulefile}}) {
165 # Skip the current sid if it is not numeric.
166 next unless ($sid =~ /\d+/ );
167
168 # Check if there exists a key in the cgiparams hash for this sid.
169 if (exists($cgiparams{$sid})) {
170 # Look if the rule is disabled.
171 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
172 # Check if the state has been set to 'on'.
173 if ($cgiparams{$sid} eq "on") {
174 # Add the sid to the enabled_sids array.
175 push(@enabled_sids, $sid);
176
177 # Drop item from cgiparams hash.
178 delete $cgiparams{$rulefile}{$sid};
179 }
180 }
181 } else {
182 # Look if the rule is enabled.
183 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
184 # Check if the state is 'on' and should be disabled.
185 # In this case there is no entry
186 # for the sid in the cgiparams hash.
187 # Add it to the disabled_sids array.
188 push(@disabled_sids, $sid);
189
190 # Drop item from cgiparams hash.
191 delete $cgiparams{$rulefile}{$sid};
192 }
193 }
194 }
195 }
196
197 # Open enabled sid's file for writing.
198 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
199
200 # Write header to file.
201 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
202
203 # Check if the enabled_sids array contains any sid's.
204 if (@enabled_sids) {
205 # Loop through the array of enabled sids and write them to the file.
206 foreach my $sid (@enabled_sids) {
207 print FILE "enablesid $sid\n";
208 }
209 }
210
211 # Close file after writing.
212 close(FILE);
213
214 # Open disabled sid's file for writing.
215 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
216
217 # Write header to file.
218 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
219
220 # Check if the enabled_sids array contains any sid's.
221 if (@disabled_sids) {
222 # Loop through the array of disabled sids and write them to the file.
223 foreach my $sid (@disabled_sids) {
224 print FILE "disablesid $sid\n";
225 }
226 }
227
228 # Close file after writing.
229 close(FILE);
230
231 # Open file for used rulefiles.
232 open (FILE, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
233
234 # Write yaml header to the file.
235 print FILE "%YAML 1.1\n";
236 print FILE "---\n\n";
237
238 # Write header to file.
239 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
240
241 # Check if the enabled_rulefiles array contains any entries.
242 if (@enabled_rulefiles) {
243 # Loop through the array of rulefiles which should be loaded and write the to the file.
244 foreach my $file (@enabled_rulefiles) {
245 print FILE " - $file\n";
246 }
247 }
248
249 # Close file after writing.
250 close(FILE);
251
252 # Lock the webpage and print message.
253 &working_notice("$Lang::tr{'snort working'}");
254
255 # Call oinkmaster to alter the ruleset.
256 &IDS::oinkmaster();
257
258 # Check if the IDS is running.
259 if(&IDS::is_ids_running()) {
260 # Call suricatactrl to perform a reload.
261 &IDS::call_suricatactrl("reload");
262 }
263
264 # Reload page.
265 &reload();
266
267 # Download new ruleset.
268 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
269 # Check if the red device is active.
270 unless (-e "${General::swroot}/red/active") {
271 $errormessage = $Lang::tr{'could not download latest updates'};
272 }
273
274 # Check if enought free disk space is availabe.
275 if(&IDS::checkdiskspace()) {
276 $errormessage = "$Lang::tr{'not enough disk space'}";
277 }
278
279 # Check if any errors happend.
280 unless ($errormessage) {
281 # Lock the webpage and print notice about downloading
282 # a new ruleset.
283 &working_notice("$Lang::tr{'snort working'}");
284
285 # Call subfunction to download the ruleset.
286 if(&IDS::downloadruleset()) {
287 $errormessage = $Lang::tr{'could not download latest updates'};
288
289 # Call function to store the errormessage.
290 &IDS::_store_error_message($errormessage);
291
292 # Preform a reload of the page.
293 &reload();
294 } else {
295 # Call subfunction to launch oinkmaster.
296 &IDS::oinkmaster();
297
298 # Check if the IDS is running.
299 if(&IDS::is_ids_running()) {
300 # Call suricatactrl to perform a reload.
301 &IDS::call_suricatactrl("reload");
302 }
303
304 # Perform a reload of the page.
305 &reload();
306 }
307 }
308 # Save snort settings.
309 } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
310 # Prevent form name from been stored in conf file.
311 delete $cgiparams{'IDS'};
312
313 # Check if an oinkcode has been provided.
314 if ($cgiparams{'OINKCODE'}) {
315 # Check if the oinkcode contains unallowed chars.
316 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
317 $errormessage = $Lang::tr{'invalid input for oink code'};
318 }
319 }
320
321 # Go on if there are no error messages.
322 if (!$errormessage) {
323 # Store settings into settings file.
324 &General::writehash("$IDS::settingsdir/settings", \%cgiparams);
325 }
326
327 # Generate file to store the home net.
328 &generate_home_net_file();
329
330 # Check if the IDS currently is running.
331 if(&IDS::ids_is_running()) {
332 # Check if ENABLE_IDS is set to on.
333 if($cgiparams{'ENABLE_IDS'} eq "on") {
334 # Call suricatactrl to perform a reload of suricata.
335 &IDS::call_suricatactrl("reload");
336 } else {
337 # Call suricatactrl to stop suricata.
338 &IDS::call_suricatactrl("stop");
339 }
340 } else {
341 # Call suricatactrl to start suricata.
342 &IDS::call_suricatactrl("start");
343 }
344 }
345
346 # Read-in idssettings
347 &General::readhash("$IDS::settingsdir/settings", \%idssettings);
348
349 $checked{'ENABLE_IDS'}{'off'} = '';
350 $checked{'ENABLE_IDS'}{'on'} = '';
351 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
352 $selected{'RULES'}{'nothing'} = '';
353 $selected{'RULES'}{'community'} = '';
354 $selected{'RULES'}{'emerging'} = '';
355 $selected{'RULES'}{'registered'} = '';
356 $selected{'RULES'}{'subscripted'} = '';
357 $selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
358
359 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
360
361 ### Java Script ###
362 print <<END
363 <script>
364 // Tiny java script function to show/hide the rules
365 // of a given category.
366 function showhide(tblname) {
367 \$("#" + tblname).toggle();
368 }
369 </script>
370 END
371 ;
372
373 &Header::openbigbox('100%', 'left', '', $errormessage);
374
375 if ($errormessage) {
376 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
377 print "<class name='base'>$errormessage\n";
378 print "&nbsp;</class>\n";
379 &Header::closebox();
380 }
381
382 # Draw current state of the IDS
383 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
384
385 # Check if the IDS is running and obtain the process-id.
386 my $pid = &IDS::ids_is_running();
387
388 # Display some useful information, if suricata daemon is running.
389 if ($pid) {
390 # Gather used memory.
391 my $memory = &get_memory_usage($pid);
392
393 print <<END;
394 <table width='95%' cellspacing='0' class='tbl'>
395 <tr>
396 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
397 </tr>
398
399 <tr>
400 <td class='base'>$Lang::tr{'guardian daemon'}</td>
401 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
402 </tr>
403
404 <tr>
405 <td class='base'></td>
406 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
407 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
408 </tr>
409
410 <tr>
411 <td class='base'></td>
412 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
413 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
414 </tr>
415 </table>
416 END
417 } else {
418 # Otherwise display a hint that the service is not launched.
419 print <<END;
420 <table width='95%' cellspacing='0' class='tbl'>
421 <tr>
422 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
423 </tr>
424
425 <tr>
426 <td class='base'>$Lang::tr{'guardian daemon'}</td>
427 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
428 </tr>
429 </table>
430 END
431 }
432 &Header::closebox();
433
434 # Draw elements for IDS configuration.
435 &Header::openbox('100%', 'center', $Lang::tr{'settings'});
436
437 my $rulesdate;
438
439 # Check if a ruleset allready has been downloaded.
440 if ( -f "$IDS::rulestarball"){
441 # Call stat on the filename to obtain detailed information.
442 my @Info = stat("$IDS::rulestarball");
443
444 # Grab details about the creation time.
445 $rulesdate = localtime($Info[9]);
446 }
447
448 print <<END
449 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
450 <table width='100%' border='0'>
451 <tr>
452 <td class='base' width='25%'>
453 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
454 </td>
455
456 <td class='base' width='25%'>
457 &nbsp
458 </td>
459 </tr>
460
461 <tr>
462 <td colspan='2'><br><br>
463 </tr>
464
465 <tr>
466 <td class='base' width='25%'>
467 <b>$Lang::tr{'ids analyze incomming traffic'}</b>
468 </td>
469
470 <td class='base' width='25%'>
471 <b>$Lang::tr{'ids analyze routing traffic'}</b>
472 </td>
473 </tr>
474 END
475 ;
476
477 # Loop through the array of available networks and print config options.
478 foreach my $zone (@network_zones) {
479 my $checked_input;
480 my $checked_forward;
481
482 # Convert current zone name to upper case.
483 my $zone_upper = uc($zone);
484
485 # Grab checkbox status from settings hash.
486 if ($idssettings{"ENABLE_IDS_INPUT_$zone_upper"} eq "on") {
487 $checked_input = "checked = 'checked'";
488 }
489
490 # Do the same for the forward setting.
491 if ($idssettings{"ENABLE_IDS_FORWARD_$zone_upper"} eq "on") {
492 $checked_forward = "checked = 'checked'";
493 }
494
495 print "<tr>\n";
496 print "<td class='base' width='25%'>\n";
497 print "<input type='checkbox' name='ENABLE_IDS_INPUT_$zone_upper' $checked_input>$Lang::tr{'ids active on'} $Lang::tr{$zone}\n";
498 print "</td>\n";
499
500 print "<td class='base' width='25%'>\n";
501 print "<input type='checkbox' name='ENABLE_IDS_FORWARD_$zone_upper' $checked_forward>$Lang::tr{'ids active on'} $Lang::tr{$zone}\n";
502 print "</td>\n";
503 print "</tr>\n";
504 }
505
506 print <<END
507 </tr>
508
509 <tr>
510 <td colspan='4'><br><br></td>
511 </tr>
512
513 <tr>
514 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
515 </tr>
516
517 <tr>
518 <td colspan='4'><select name='RULES'>
519 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
520 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
521 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
522 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
523 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
524 </select>
525 </td>
526 </tr>
527
528 <tr>
529 <td colspan='4'>
530 <br>$Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}</br>
531 <br>$Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}</br>
532 </td>
533 </tr>
534
535 <tr>
536 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
537 </tr>
538
539 <tr>
540 <td colspan='4' align='left'><br>
541 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
542 </td>
543
544 </tr>
545 </table>
546
547 <br><br>
548
549 <table width='100%'>
550 <tr>
551 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
552 </tr>
553 </table>
554 </form>
555 END
556 ;
557
558 &Header::closebox();
559
560 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
561 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
562
563 # Output display table for rule files
564 print "<table width='100%'>\n";
565
566 # Local variable required for java script to show/hide
567 # rules of a rulefile.
568 my $rulesetcount = 1;
569
570 # Loop over each rule file
571 foreach my $rulefile (sort keys(%idsrules)) {
572 my $rulechecked = '';
573
574 # Check if rule file is enabled
575 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
576 $rulechecked = 'CHECKED';
577 }
578
579 # Table and rows for the rule files.
580 print"<tr>\n";
581 print"<td class='base' width='5%'>\n";
582 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
583 print"</td>\n";
584 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
585 print"<td class='base' width='5%' align='right'>\n";
586 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
587 print"</td>\n";
588 print"</tr>\n";
589
590 # Rows which will be hidden per default and will contain the single rules.
591 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
592 print"<td colspan='3'>\n";
593
594 # Local vars
595 my $lines;
596 my $rows;
597 my $col;
598
599 # New table for the single rules.
600 print "<table width='100%'>\n";
601
602 # Loop over rule file rules
603 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
604 # Local vars
605 my $ruledefchecked = '';
606
607 # Skip rulefile itself.
608 next if ($sid eq "Rulefile");
609
610 # If 2 rules have been displayed, start a new row
611 if (($lines % 2) == 0) {
612 print "</tr><tr>\n";
613
614 # Increase rows by once.
615 $rows++;
616 }
617
618 # Colour lines.
619 if ($rows % 2) {
620 $col="bgcolor='$color{'color20'}'";
621 } else {
622 $col="bgcolor='$color{'color22'}'";
623 }
624
625 # Set rule state
626 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
627 $ruledefchecked = 'CHECKED';
628 }
629
630 # Create rule checkbox and display rule description
631 print "<td class='base' width='5%' align='right' $col>\n";
632 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
633 print "</td>\n";
634 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
635
636 # Increment rule count
637 $lines++;
638 }
639
640 # If do not have a second rule for row, create empty cell
641 if (($lines % 2) != 0) {
642 print "<td class='base'></td>";
643 }
644
645 # Close display table
646 print "</tr></table></td></tr>";
647
648 # Finished whith the rule file, increase count.
649 $rulesetcount++;
650 }
651
652 # Close display table
653 print "</table>";
654
655 print <<END
656 <table width='100%'>
657 <tr>
658 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
659 &nbsp; <!-- space for future online help link -->
660 </td>
661 </tr>
662 </table>
663 </form>
664 END
665 ;
666 &Header::closebox();
667 &Header::closebigbox();
668 &Header::closepage();
669
670 #
671 ## A function to display a notice, to lock the webpage and
672 ## tell the user which action currently will be performed.
673 #
674 sub working_notice ($) {
675 my ($message) = @_;
676
677 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
678 &Header::openbigbox('100%', 'left', '', $errormessage);
679 &Header::openbox( 'Waiting', 1,);
680 print <<END;
681 <table>
682 <tr>
683 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
684 <td>$message</td>
685 </tr>
686 </table>
687 END
688 &Header::closebox();
689 &Header::closebigbox();
690 &Header::closepage();
691 }
692
693 #
694 ## A tiny function to perform a reload of the webpage after one second.
695 #
696 sub reload () {
697 print "<meta http-equiv='refresh' content='1'>\n";
698
699 # Stop the script.
700 exit;
701 }
702
703 #
704 ## Private function to read-in and parse rules of a given rulefile.
705 #
706 ## The given file will be read, parsed and all valid rules will be stored by ID,
707 ## message/description and it's state in the idsrules hash.
708 #
709 sub readrulesfile ($) {
710 my $rulefile = shift;
711
712 # Open rule file and read in contents
713 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
714
715 # Store file content in an array.
716 my @lines = <RULEFILE>;
717
718 # Close file.
719 close(RULEFILE);
720
721 # Loop over rule file contents
722 foreach my $line (@lines) {
723 # Remove whitespaces.
724 chomp $line;
725
726 # Skip blank lines.
727 next if ($line =~ /^\s*$/);
728
729 # Local vars.
730 my $sid;
731 my $msg;
732
733 # Gather rule sid and message from the ruleline.
734 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
735 $msg = $1;
736 $sid = $2;
737
738 # Check if a rule has been found.
739 if ($sid && $msg) {
740 # Add rule to the idsrules hash.
741 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
742
743 # Grab status of the rule. Check if ruleline starts with a "dash".
744 if ($line =~ /^\#/) {
745 # If yes, the rule is disabled.
746 $idsrules{$rulefile}{$sid}{'State'} = "off";
747 } else {
748 # Otherwise the rule is enabled.
749 $idsrules{$rulefile}{$sid}{'State'} = "on";
750 }
751 }
752 }
753 }
754 }
755
756 #
757 ## Function to get the used memory of a given process-id.
758 #
759 sub get_memory_usage($) {
760 my $pid = @_;
761
762 my $memory=0;
763
764 # Try to open statm file for the given process-id on the pseudo
765 # file system proc.
766 if (open(FILE, "/proc/$pid/statm")) {
767 # Read file content.
768 my $temp = <FILE>;
769
770 # Splitt file content and store in an array.
771 my @memory = split(/ /,$temp);
772
773 # Close file handle.
774 close(FILE);
775
776 # Calculate memory usage.
777 $memory+=$memory[0];
778
779 # Return memory usage.
780 return $memory;
781 }
782
783 # If the file could not be open, return nothing.
784 return;
785 }
786
787 #
788 ## Function to generate the file which contains the home net information.
789 #
790 sub generate_home_net_file() {
791 my %netsettings;
792
793 # Read-in network settings.
794 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
795
796 # Get available network zones.
797 my @network_zones = &IDS::get_available_network_zones();
798
799 # Temporary array to store network address and prefix of the configured
800 # networks.
801 my @networks;
802
803 # Loop through the array of available network zones.
804 foreach my $zone (@network_zones) {
805 # Skip the red network - It never can be part to the home_net!
806 next if($zone eq "red");
807
808 # Convert current zone name into upper case.
809 $zone = uc($zone);
810
811 # Generate key to access the required data from the netsettings hash.
812 my $zone_netaddress = $zone . "_NETADDRESS";
813 my $zone_netmask = $zone . "_NETMASK";
814
815 # Obtain the settings from the netsettings hash.
816 my $netaddress = $netsettings{$zone_netaddress};
817 my $netmask = $netsettings{$zone_netmask};
818
819 # Convert the subnetmask into prefix notation.
820 my $prefix = &Network::convert_netmask2prefix($netmask);
821
822 # Generate full network string.
823 my $network = join("/", $netaddress,$prefix);
824
825 # Check if the network is valid.
826 if(&Network::check_subnet($network)) {
827 # Add the generated network to the array of networks.
828 push(@networks, $network);
829 }
830 }
831
832 # Format home net declaration.
833 my $line = "\"\[";
834
835 # Loop through the array of networks.
836 foreach my $network (@networks) {
837 # Add the network to the line.
838 $line = "$line" . "$network";
839
840 # Check if the current network was the last in the array.
841 if ($network eq $networks[-1]) {
842 # Close the line.
843 $line = "$line" . "\]\"";
844 } else {
845 # Add "," for the next network.
846 $line = "$line" . "\,";
847 }
848 }
849
850 # Open file to store the addresses of the home net.
851 open(FILE, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
852
853 # Print yaml header.
854 print FILE "%YAML 1.1\n";
855 print FILE "---\n\n";
856
857 # Print notice about autogenerated file.
858 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
859
860 # Print the generated and required HOME_NET declaration to the file.
861 print FILE "HOME_NET:\t$line\n";
862
863 # Close file handle.
864 close(FILE);
865
866 }