2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2018 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
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";
34 my %mainsettings = ();
37 my %rulesetsources = ();
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);
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
();
50 # File where the used rulefiles are stored.
51 my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml";
53 # File where the addresses of the homenet are stored.
54 my $idshomenetfile = "$IDS::settingsdir/suricata-homenet.yaml";
56 # File which contains the enabled sids.
57 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
59 # File which contains the disabled sids.
60 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
62 # File which contains wheater the rules should be changed.
63 my $modify_sids_file = "$IDS::settingsdir/oinkmaster-modify-sids.conf";
67 # Create oinkmaster related files if they does not exist yet.
68 unless (-f
"$enabled_sids_file") { &IDS
::create_empty_file
($enabled_sids_file); }
69 unless (-f
"$disabled_sids_file") { &IDS
::create_empty_file
($disabled_sids_file); }
70 unless (-f
"$modify_sids_file") { &IDS
::create_empty_file
($modify_sids_file); }
72 &Header
::showhttpheaders
();
75 &Header
::getcgihash
(\
%cgiparams);
77 # Check if any error has been stored.
78 if (-e
$IDS::storederrorfile
) {
79 # Open file to read in the stored error message.
80 open(FILE
, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
82 # Read the stored error message.
83 $errormessage = <FILE
>;
88 # Delete the file, which is now not longer required.
89 unlink($IDS::storederrorfile
);
93 ## Grab all available snort rules and store them in the idsrules hash.
95 # Open snort rules directory and do a directory listing.
96 opendir(DIR
, $IDS::rulespath
) or die $!;
97 # Loop through the direcory.
98 while (my $file = readdir(DIR
)) {
100 # We only want files.
101 next unless (-f
"$IDS::rulespath/$file");
103 # Ignore empty files.
104 next if (-z
"$IDS::rulespath/$file");
106 # Use a regular expression to find files ending in .rules
107 next unless ($file =~ m/\.rules$/);
109 # Ignore files which are not read-able.
110 next unless (-R
"$IDS::rulespath/$file");
112 # Call subfunction to read-in rulefile and add rules to
114 &readrulesfile
("$file");
119 # Gather used rulefiles.
121 # Check if the file for activated rulefiles is not empty.
122 if(-f
$idsusedrulefilesfile) {
123 # Open the file for used rulefile and read-in content.
124 open(FILE
, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
132 # Loop through the array.
133 foreach my $line (@lines) {
138 next if ($line =~ /\#/);
141 next if ($line =~ /^\s*$/);
143 # Gather rule sid and message from the ruleline.
144 if ($line =~ /.*- (.*)/) {
147 # Add the rulefile to the %idsrules hash.
148 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
154 if ($cgiparams{'RULESET'} eq $Lang::tr
{'update'}) {
155 # Arrays to store which rulefiles have been enabled and will be used.
156 my @enabled_rulefiles;
158 # Hash to store the user-enabled and disabled sids.
159 my %enabled_disabled_sids;
161 # Loop through the hash of idsrules.
162 foreach my $rulefile(keys %idsrules) {
163 # Check if the rulefile is enabled.
164 if ($cgiparams{$rulefile} eq "on") {
165 # Add rulefile to the array of enabled rulefiles.
166 push(@enabled_rulefiles, $rulefile);
168 # Drop item from cgiparams hash.
169 delete $cgiparams{$rulefile};
173 # Read-in the files for enabled/disabled sids.
174 # This will be done by calling the read_enabled_disabled_sids_file function two times
175 # and merge the returned hashes together into the enabled_disabled_sids hash.
176 %enabled_disabled_sids = (
177 &read_enabled_disabled_sids_file
($disabled_sids_file),
178 &read_enabled_disabled_sids_file
($enabled_sids_file));
180 # Loop through the hash of idsrules.
181 foreach my $rulefile (keys %idsrules) {
182 # Loop through the single rules of the rulefile.
183 foreach my $sid (keys %{$idsrules{$rulefile}}) {
184 # Skip the current sid if it is not numeric.
185 next unless ($sid =~ /\d+/ );
187 # Check if there exists a key in the cgiparams hash for this sid.
188 if (exists($cgiparams{$sid})) {
189 # Look if the rule is disabled.
190 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
191 # Check if the state has been set to 'on'.
192 if ($cgiparams{$sid} eq "on") {
193 # Add/Modify the sid to/in the enabled_disabled_sids hash.
194 $enabled_disabled_sids{$sid} = "enabled";
196 # Drop item from cgiparams hash.
197 delete $cgiparams{$rulefile}{$sid};
201 # Look if the rule is enabled.
202 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
203 # Check if the state is 'on' and should be disabled.
204 # In this case there is no entry
205 # for the sid in the cgiparams hash.
206 # Add/Modify it to/in the enabled_disabled_sids hash.
207 $enabled_disabled_sids{$sid} = "disabled";
209 # Drop item from cgiparams hash.
210 delete $cgiparams{$rulefile}{$sid};
216 # Open enabled sid's file for writing.
217 open(ENABLED_FILE
, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
219 # Open disabled sid's file for writing.
220 open(DISABLED_FILE
, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
222 # Write header to the files.
223 print ENABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
224 print DISABLED_FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
226 # Check if the hash for enabled/disabled files contains any entries.
227 if (%enabled_disabled_sids) {
228 # Loop through the hash.
229 foreach my $sid (keys %enabled_disabled_sids) {
230 # Check if the sid is enabled.
231 if ($enabled_disabled_sids{$sid} eq "enabled") {
232 # Print the sid to the enabled_sids file.
233 print ENABLED_FILE
"enablesid $sid\n";
234 # Check if the sid is disabled.
235 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
236 # Print the sid to the disabled_sids file.
237 print DISABLED_FILE
"disablesid $sid\n";
238 # Something strange happende - skip the current sid.
245 # Close file for enabled_sids after writing.
248 # Close file for disabled_sids after writing.
249 close(DISABLED_FILE
);
251 # Open file for used rulefiles.
252 open (FILE
, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
254 # Write yaml header to the file.
255 print FILE
"%YAML 1.1\n";
256 print FILE
"---\n\n";
258 # Write header to file.
259 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
261 # Check if the enabled_rulefiles array contains any entries.
262 if (@enabled_rulefiles) {
263 # Loop through the array of rulefiles which should be loaded and write the to the file.
264 foreach my $file (@enabled_rulefiles) {
265 print FILE
" - $file\n";
269 # Close file after writing.
272 # Lock the webpage and print message.
273 &working_notice
("$Lang::tr{'snort working'}");
275 # Call oinkmaster to alter the ruleset.
278 # Check if the IDS is running.
279 if(&IDS
::ids_is_running
()) {
280 # Call suricatactrl to perform a reload.
281 &IDS
::call_suricatactrl
("reload");
287 # Download new ruleset.
288 } elsif ($cgiparams{'RULESET'} eq $Lang::tr
{'download new ruleset'}) {
289 # Check if the red device is active.
290 unless (-e
"${General::swroot}/red/active") {
291 $errormessage = $Lang::tr
{'could not download latest updates'};
294 # Check if enought free disk space is availabe.
295 if(&IDS
::checkdiskspace
()) {
296 $errormessage = "$Lang::tr{'not enough disk space'}";
299 # Check if any errors happend.
300 unless ($errormessage) {
301 # Lock the webpage and print notice about downloading
303 &working_notice
("$Lang::tr{'snort working'}");
305 # Call subfunction to download the ruleset.
306 if(&IDS
::downloadruleset
()) {
307 $errormessage = $Lang::tr
{'could not download latest updates'};
309 # Call function to store the errormessage.
310 &IDS
::_store_error_message
($errormessage);
312 # Preform a reload of the page.
315 # Call subfunction to launch oinkmaster.
318 # Check if the IDS is running.
319 if(&IDS
::ids_is_running
()) {
320 # Call suricatactrl to perform a reload.
321 &IDS
::call_suricatactrl
("reload");
324 # Perform a reload of the page.
328 # Save snort settings.
329 } elsif ($cgiparams{'IDS'} eq $Lang::tr
{'save'}) {
333 # Read-in current (old) IDS settings.
334 &General
::readhash
("$IDS::settingsdir/settings", \
%oldidssettings);
336 # Prevent form name from been stored in conf file.
337 delete $cgiparams{'IDS'};
339 # Check if an oinkcode has been provided.
340 if ($cgiparams{'OINKCODE'}) {
341 # Check if the oinkcode contains unallowed chars.
342 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
343 $errormessage = $Lang::tr
{'invalid input for oink code'};
347 # Go on if there are no error messages.
348 if (!$errormessage) {
349 # Store settings into settings file.
350 &General
::writehash
("$IDS::settingsdir/settings", \
%cgiparams);
353 # Generate file to store the home net.
354 &generate_home_net_file
();
356 # Open modify sid's file for writing.
357 open(FILE
, ">$modify_sids_file") or die "Could not write to $modify_sids_file. $!\n";
360 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
362 # Check if the configured runmode is IPS.
363 if ($cgiparams{'RUN_MODE'} eq 'IPS') {
364 # Tell oinkmaster to switch all rules from alert to drop.
365 print FILE
"modifysid \* \"alert\" \| \"drop\"\n";
371 # Check if the runmode has been changed.
372 if($cgiparams{'RUN_MODE'} ne $oldidssettings{'RUN_MODE'}) {
373 # Check if a ruleset exists.
375 # Lock the webpage and print message.
376 &working_notice
("$Lang::tr{'snort working'}");
378 # Call oinkmaster to alter the ruleset.
381 # Set reload_page to "True".
386 # Check if the IDS currently is running.
387 if(&IDS
::ids_is_running
()) {
388 # Check if ENABLE_IDS is set to on.
389 if($cgiparams{'ENABLE_IDS'} eq "on") {
390 # Call suricatactrl to perform a reload of suricata.
391 &IDS
::call_suricatactrl
("reload");
393 # Call suricatactrl to stop suricata.
394 &IDS
::call_suricatactrl
("stop");
397 # Call suricatactrl to start suricata.
398 &IDS
::call_suricatactrl
("start");
401 # Check if the page should be reloaded.
403 # Perform a reload of the page.
408 # Read-in idssettings
409 &General
::readhash
("$IDS::settingsdir/settings", \
%idssettings);
411 # If the runmode has not been configured yet, set default value.
412 unless(exists($idssettings{'RUN_MODE'})) {
413 # Set default to IPS.
414 $idssettings{'RUN_MODE'} = 'IPS';
417 $checked{'ENABLE_IDS'}{'off'} = '';
418 $checked{'ENABLE_IDS'}{'on'} = '';
419 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
420 $checked{'RUN_MODE'}{'IDS'} = '';
421 $checked{'RUN_MODE'}{'IPS'} = '';
422 $checked{'RUN_MODE'}{$idssettings{'RUN_MODE'}} = "checked='checked'";
423 $selected{'RULES'}{'nothing'} = '';
424 $selected{'RULES'}{'community'} = '';
425 $selected{'RULES'}{'emerging'} = '';
426 $selected{'RULES'}{'registered'} = '';
427 $selected{'RULES'}{'subscripted'} = '';
428 $selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
430 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
435 // Tiny java script function to show/hide the rules
436 // of a given category.
437 function showhide(tblname) {
438 \$("#" + tblname).toggle();
444 &Header
::openbigbox
('100%', 'left', '', $errormessage);
447 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
448 print "<class name='base'>$errormessage\n";
449 print " </class>\n";
453 # Draw current state of the IDS
454 &Header
::openbox
('100%', 'left', $Lang::tr
{'intrusion detection system'});
456 # Check if the IDS is running and obtain the process-id.
457 my $pid = &IDS
::ids_is_running
();
459 # Display some useful information, if suricata daemon is running.
461 # Gather used memory.
462 my $memory = &get_memory_usage
($pid);
465 <table width='95%' cellspacing='0' class='tbl'>
467 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
471 <td class='base'>$Lang::tr{'guardian daemon'}</td>
472 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
476 <td class='base'></td>
477 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
478 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
482 <td class='base'></td>
483 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
484 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
489 # Otherwise display a hint that the service is not launched.
491 <table width='95%' cellspacing='0' class='tbl'>
493 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
497 <td class='base'>$Lang::tr{'guardian daemon'}</td>
498 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
505 # Draw elements for IDS configuration.
506 &Header
::openbox
('100%', 'center', $Lang::tr
{'settings'});
510 # Check if a ruleset allready has been downloaded.
511 if ( -f
"$IDS::rulestarball"){
512 # Call stat on the filename to obtain detailed information.
513 my @Info = stat("$IDS::rulestarball");
515 # Grab details about the creation time.
516 $rulesdate = localtime($Info[9]);
520 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
521 <table width='100%' border='0'>
523 <td class='base' colspan='4'>
524 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
529 <td colspan='4'><br><br></td>
533 <td class='base' colspan='4'><b>$Lang::tr{'runmode'}</b></td>
537 <td class='base' colspan='4'>
538 <input type='radio' name='RUN_MODE' value='IDS' $checked{'RUN_MODE'}{'IDS'}>$Lang::tr{'intrusion detection system2'}    
539 <input type='radio' name='RUN_MODE' value='IPS' $checked{'RUN_MODE'}{'IPS'}>$Lang::tr{'intrusion prevention system'}
544 <td colspan='4'><br></td>
548 <td colspan='4'><b>$Lang::tr{'ids traffic analyze'}</b><br></td>
555 # Loop through the array of available networks and print config options.
556 foreach my $zone (@network_zones) {
560 # Convert current zone name to upper case.
561 my $zone_upper = uc($zone);
563 # Grab checkbox status from settings hash.
564 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
565 $checked_input = "checked = 'checked'";
568 print "<td class='base' width='25%'>\n";
569 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>$Lang::tr{'enabled on'} $Lang::tr{$zone}\n";
577 <td colspan='4'><br><br></td>
581 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
585 <td colspan='4'><select name='RULES'>
586 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
587 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
588 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
589 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
590 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
597 <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>
598 <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>
603 <td colspan='4' nowrap='nowrap'>Oinkcode: <input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
607 <td colspan='4' align='left'><br>
608 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'> $Lang::tr{'updates installed'}: $rulesdate
618 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
627 &Header
::openbox
('100%', 'LEFT', $Lang::tr
{'intrusion detection system rules'});
628 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
630 # Output display table for rule files
631 print "<table width='100%'>\n";
633 # Local variable required for java script to show/hide
634 # rules of a rulefile.
635 my $rulesetcount = 1;
637 # Loop over each rule file
638 foreach my $rulefile (sort keys(%idsrules)) {
639 my $rulechecked = '';
641 # Check if rule file is enabled
642 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
643 $rulechecked = 'CHECKED';
646 # Table and rows for the rule files.
648 print"<td class='base' width='5%'>\n";
649 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
651 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
652 print"<td class='base' width='5%' align='right'>\n";
653 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
657 # Rows which will be hidden per default and will contain the single rules.
658 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
659 print"<td colspan='3'>\n";
666 # New table for the single rules.
667 print "<table width='100%'>\n";
669 # Loop over rule file rules
670 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
672 my $ruledefchecked = '';
674 # Skip rulefile itself.
675 next if ($sid eq "Rulefile");
677 # If 2 rules have been displayed, start a new row
678 if (($lines % 2) == 0) {
681 # Increase rows by once.
687 $col="bgcolor='$color{'color20'}'";
689 $col="bgcolor='$color{'color22'}'";
693 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
694 $ruledefchecked = 'CHECKED';
697 # Create rule checkbox and display rule description
698 print "<td class='base' width='5%' align='right' $col>\n";
699 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
701 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
703 # Increment rule count
707 # If do not have a second rule for row, create empty cell
708 if (($lines % 2) != 0) {
709 print "<td class='base'></td>";
712 # Close display table
713 print "</tr></table></td></tr>";
715 # Finished whith the rule file, increase count.
719 # Close display table
725 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
726 <!-- space for future online help link -->
734 &Header
::closebigbox
();
735 &Header
::closepage
();
738 ## A function to display a notice, to lock the webpage and
739 ## tell the user which action currently will be performed.
741 sub working_notice
($) {
744 &Header
::openpage
($Lang::tr
{'intrusion detection system'}, 1, '');
745 &Header
::openbigbox
('100%', 'left', '', $errormessage);
746 &Header
::openbox
( 'Waiting', 1,);
750 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
756 &Header
::closebigbox
();
757 &Header
::closepage
();
761 ## A tiny function to perform a reload of the webpage after one second.
764 print "<meta http-equiv='refresh' content='1'>\n";
771 ## Private function to read-in and parse rules of a given rulefile.
773 ## The given file will be read, parsed and all valid rules will be stored by ID,
774 ## message/description and it's state in the idsrules hash.
776 sub readrulesfile
($) {
777 my $rulefile = shift;
779 # Open rule file and read in contents
780 open(RULEFILE
, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
782 # Store file content in an array.
783 my @lines = <RULEFILE
>;
788 # Loop over rule file contents
789 foreach my $line (@lines) {
790 # Remove whitespaces.
794 next if ($line =~ /^\s*$/);
800 # Gather rule sid and message from the ruleline.
801 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
805 # Check if a rule has been found.
807 # Add rule to the idsrules hash.
808 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
810 # Grab status of the rule. Check if ruleline starts with a "dash".
811 if ($line =~ /^\#/) {
812 # If yes, the rule is disabled.
813 $idsrules{$rulefile}{$sid}{'State'} = "off";
815 # Otherwise the rule is enabled.
816 $idsrules{$rulefile}{$sid}{'State'} = "on";
824 ## Function to get the used memory of a given process-id.
826 sub get_memory_usage
($) {
831 # Try to open the status file for the given process-id on the pseudo
833 if (open(FILE
, "/proc/$pid/status")) {
834 # Loop through the entire file.
836 # Splitt current line content and store them into variables.
837 my ($key, $value) = split(":", $_, 2);
839 # Check if the current key is the one which contains the memory usage.
840 # The wanted one is VmRSS which contains the Real-memory (resident set)
841 # of the entire process.
842 if ($key eq "VmRSS") {
843 # Found the memory usage add it to the memory variable.
854 # Return memory usage.
858 # If the file could not be open, return nothing.
863 ## Function to generate the file which contains the home net information.
865 sub generate_home_net_file
() {
868 # Read-in network settings.
869 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
871 # Get available network zones.
872 my @network_zones = &IDS
::get_available_network_zones
();
874 # Temporary array to store network address and prefix of the configured
878 # Loop through the array of available network zones.
879 foreach my $zone (@network_zones) {
880 # Skip the red network - It never can be part to the home_net!
881 next if($zone eq "red");
883 # Convert current zone name into upper case.
886 # Generate key to access the required data from the netsettings hash.
887 my $zone_netaddress = $zone . "_NETADDRESS";
888 my $zone_netmask = $zone . "_NETMASK";
890 # Obtain the settings from the netsettings hash.
891 my $netaddress = $netsettings{$zone_netaddress};
892 my $netmask = $netsettings{$zone_netmask};
894 # Convert the subnetmask into prefix notation.
895 my $prefix = &Network
::convert_netmask2prefix
($netmask);
897 # Generate full network string.
898 my $network = join("/", $netaddress,$prefix);
900 # Check if the network is valid.
901 if(&Network
::check_subnet
($network)) {
902 # Add the generated network to the array of networks.
903 push(@networks, $network);
907 # Format home net declaration.
910 # Loop through the array of networks.
911 foreach my $network (@networks) {
912 # Add the network to the line.
913 $line = "$line" . "$network";
915 # Check if the current network was the last in the array.
916 if ($network eq $networks[-1]) {
918 $line = "$line" . "\]\"";
920 # Add "," for the next network.
921 $line = "$line" . "\,";
925 # Open file to store the addresses of the home net.
926 open(FILE
, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
929 print FILE
"%YAML 1.1\n";
930 print FILE
"---\n\n";
932 # Print notice about autogenerated file.
933 print FILE
"#Autogenerated file. Any custom changes will be overwritten!\n";
935 # Print the generated and required HOME_NET declaration to the file.
936 print FILE
"HOME_NET:\t$line\n";
944 ## Function to read-in the given enabled or disables sids file.
946 sub read_enabled_disabled_sids_file
($) {
949 # Temporary hash to store the sids and their state. It will be
950 # returned at the end of this function.
953 # Open the given filename.
954 open(FILE
, "$file") or die "Could not open $file. $!\n";
956 # Loop through the file.
962 next if ($_ =~ /^\s*$/);
965 next if ($_ =~ /^\#/);
967 # Splitt line into sid and state part.
968 my ($state, $sid) = split(" ", $_);
970 # Skip line if the sid is not numeric.
971 next unless ($sid =~ /\d+/ );
973 # Check if the sid was enabled.
974 if ($state eq "enablesid") {
975 # Add the sid and its state as enabled to the temporary hash.
976 $temphash{$sid} = "enabled";
977 # Check if the sid was disabled.
978 } elsif ($state eq "disablesid") {
979 # Add the sid and its state as disabled to the temporary hash.
980 $temphash{$sid} = "disabled";
981 # Invalid state - skip the current sid and state.