From: Stefan Schantl Date: Fri, 29 Apr 2016 07:09:25 +0000 (+0200) Subject: guardian.cgi: Use ignored config file. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7edbe063742d0c65e2f229dc366da8b18ea41482;p=people%2Fms%2Fipfire-2.x.git guardian.cgi: Use ignored config file. The CGI now uses an own ignored configuration file for storing host addresses and/or subnets which should be ignored by guardian. This allows to add remarks for them and to enable or disable each entry individally at any time. Signed-off-by: Stefan Schantl --- diff --git a/html/cgi-bin/guardian.cgi b/html/cgi-bin/guardian.cgi index 1941ff4878..eddbb6e956 100644 --- a/html/cgi-bin/guardian.cgi +++ b/html/cgi-bin/guardian.cgi @@ -69,7 +69,17 @@ our %mainsettings = (); # (File exists when the addon is installed.) my $owncloud_meta = "/opt/pakfire/db/installed/meta-owncloud"; + +# File declarations. +my $settingsfile = "${General::swroot}/guardian/settings"; +my $ignoredfile = "${General::swroot}/guardian/ignored"; + +# Create empty settings and ignoredfile if they do not exist yet. +unless (-e "$settingsfile") { system("touch $settingsfile"); } +unless (-e "$ignoredfile") { system("touch $ignoredfile"); } + our %settings = (); +our %ignored = (); $settings{'ACTION'} = ''; @@ -128,15 +138,15 @@ if ($settings{'ACTION'} eq $Lang::tr{'save'}) { &BuildConfiguration(); } -## Add a new entry to the ignore file. +## Add/edit an entry to the ignore file. # -} elsif ($settings{'ACTION'} eq $Lang::tr{'add'}) { +} elsif (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::tr{'update'})) { # Check if any input has been performed. - if ($settings{'NEW_IGNORE_ENTRY'} ne '') { + if ($settings{'IGNORE_ENTRY_ADDRESS'} ne '') { # Check if the given input is no valid IP-address or IP-address with subnet, display an error message. - if ((!&General::validip($settings{'NEW_IGNORE_ENTRY'})) && (!&General::validipandmask($settings{'NEW_IGNORE_ENTRY'}))) { + if ((!&General::validip($settings{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($settings{'IGNORE_ENTRY_ADDRESS'}))) { $errormessage = "$Lang::tr{'guardian invalid address or subnet'}"; } } else { @@ -145,14 +155,54 @@ if ($settings{'ACTION'} eq $Lang::tr{'save'}) { # Go further if there was no error. if ($errormessage eq '') { - my $new_entry = $settings{'NEW_IGNORE_ENTRY'}; + my %ignored = (); + my $id; + my $status; + + # Assign hash values. + my $new_entry_address = $settings{'IGNORE_ENTRY_ADDRESS'}; + my $new_entry_remark = $settings{'IGNORE_ENTRY_REMARK'}; + + # Read-in ignoredfile. + &General::readhasharray($ignoredfile, \%ignored); + + # Check if we should edit an existing entry and got an ID. + if (($settings{'ACTION'} eq $Lang::tr{'update'}) && ($settings{'ID'})) { + # Assin the provided id. + $id = $settings{'ID'}; + + # Undef the given ID. + undef($settings{'ID'}); + + # Grab the configured status of the corresponding entry. + $status = $ignored{$id}[2]; + } else { + # Each newly added entry automatically should be enabled. + $status = "enabled"; + + # Generate the ID for the new entry. + # + # Sort the keys by it's ID and store them in an array. + my @keys = sort { $a <=> $b } keys %ignored; + + # Reverse the key array. + my @reversed = reverse(@keys); + + # Obtain the last used id. + my $last_id = @reversed[0]; - # Open file for appending the new entry. - open (FILE, ">>$ignorefile") or die "Unable to open $ignorefile for writing"; + # Increase the last id by one and use it as id for the new entry. + $id = ++$last_id; + } + + # Add/Modify the entry to/in the ignored hash. + $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"]; - # Write the new entry to the ignore file. - print FILE "$new_entry\n"; - close(FILE); + # Write the changed ignored hash to the ignored file. + &General::writehasharray($ignoredfile, \%ignored); + + # Regenerate the ignore file. + # &GenerateIgnoreFile(); } # Check if guardian is running. @@ -161,29 +211,68 @@ if ($settings{'ACTION'} eq $Lang::tr{'save'}) { &Guardian::Socket::Client("reload"); } -## Remove entry from ignore list. +## Toggle Enabled/Disabled for an existing entry on the ignore list. # -} elsif ($settings{'ACTION'} eq $Lang::tr{'remove'}) { - my $id = 0; - # Open ignorefile and read content. - open(FILE, "<$ignorefile") or die "Unable to open $ignorefile."; - my @current = ; - close(FILE); +} elsif ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) { + my %ignored = (); + + # Only go further, if an ID has been passed. + if ($settings{'ID'}) { + # Assign the given ID. + my $id = $settings{'ID'}; + + # Undef the given ID. + undef($settings{'ID'}); + + # Read-in ignoredfile. + &General::readhasharray($ignoredfile, \%ignored); + + # Grab the configured status of the corresponding entry. + my $status = $ignored{$id}[2]; + + # Switch the status. + if ($status eq "disabled") { + $status = "enabled"; + } else { + $status = "disabled"; + } + + # Modify the status of the existing entry. + $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"]; - # Re-open ignorefile for writing. - open(FILE, ">$ignorefile") or die "Unable to open $ignorefile for writing"; - flock FILE, 2; + # Write the changed ignored hash to the ignored file. + &General::writehasharray($ignoredfile, \%ignored); - # Read line by line from ignorefile and write them back except the line with the given ID. - # So this line is missing in the new file and the entry has been deleted. - foreach my $line (@current) { - $id++; - unless ($settings{'ID'} eq $id) { - print FILE "$line"; + # Regenerate the ignore file. + # &GenerateIgnoreFile(); + + # Check if guardian is running. + if ($pid > 0) { + # Send reload command through socket connection. + &Guardian::Socket::Client("reload"); } } - close(FILE); + +## Remove entry from ignore list. +# +} elsif ($settings{'ACTION'} eq $Lang::tr{'remove'}) { + my %ignored = (); + + # Read-in ignoredfile. + &General::readhasharray($ignoredfile, \%ignored); + + # Drop entry from the hash. + delete($ignored{$settings{'ID'}}); + + # Undef the given ID. + undef($settings{'ID'}); + + # Write the changed ignored hash to the ignored file. + &General::writehasharray($ignoredfile, \%ignored); + + # Regenerate the ignore file. + # &GenerateIgnoreFile(); # Check if guardian is running. if ($pid > 0) { @@ -270,8 +359,9 @@ if ($settings{'ACTION'} eq $Lang::tr{'save'}) { &Guardian::Socket::Client("flush"); } -# Load settings from file. +# Load settings from files. &General::readhash("${General::swroot}/guardian/settings", \%settings); +&General::readhasharray("${General::swroot}/guardian/ignored", \%ignored); # Call functions to generate whole page. &showMainBox(); @@ -480,49 +570,79 @@ sub showIgnoreBox() { &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'}); print < + - + + + END - # Check if the guardian ignore file contains any content. - if (-s $ignorefile) { - - # Open file and print contents. - open FILE, $ignorefile or die "Could not open $ignorefile"; - - my $id = 0; + # Check if some hosts have been add to be ignored. + if (keys (%ignored)) { my $col = ""; - # Read file line by line and print out the elements. - while( my $ignored_element = ) { - - # Increase id number for each element in the ignore file. - $id++; - - # Check if the id number is even or not. - if ($id % 2) { + # Loop through all entries of the hash.. + while( (my $key) = each %ignored) { + # Assign data array positions to some nice variable names. + my $address = $ignored{$key}[0]; + my $remark = $ignored{$key}[1]; + my $status = $ignored{$key}[2]; + + # Check if the key (id) number is even or not. + if ($settings{'ID'} eq $key) { + $col="bgcolor='${Header::colouryellow}'"; + } elsif ($key % 2) { $col="bgcolor='$color{'color22'}'"; } else { $col="bgcolor='$color{'color20'}'"; } + # Choose icon for the checkbox. + my $gif; + my $gdesc; + + # Check if the status is enabled and select the correct image and description. + if ($status eq 'enabled' ) { + $gif = 'on.gif'; + $gdesc = $Lang::tr{'click to disable'}; + } else { + $gif = 'off.gif'; + $gdesc = $Lang::tr{'click to enable'}; + } + print < - - + + + + + + + END } - close (FILE); } else { - # Print notice that currently no elements are stored in the ignore file. + # Print notice that currently no hosts are ignored. print "\n"; print "\n"; print "\n"; @@ -530,16 +650,44 @@ END print "
$Lang::tr{'guardian ignored hosts'}$Lang::tr{'ip address'}$Lang::tr{'remark'}
$ignored_element -
+
$address$remark + + + + + + +
+ + + +
+
+
- +
$Lang::tr{'guardian no entries'}
\n"; - # Section to add new elements to the ignore list. + # Section to add new elements or edit existing ones. print < +
+
+
- +
+END + + # Assign correct headline and button text. + my $buttontext; + my $entry_address; + my $entry_remark; + + # Check if an ID (key) has been given, in this case an existing entry should be edited. + if ($settings{'ID'} ne '') { + $buttontext = $Lang::tr{'update'}; + print "\n"; + + # Grab address and remark for the given key. + $entry_address = $ignored{$settings{'ID'}}[0]; + $entry_remark = $ignored{$settings{'ID'}}[1]; + } else { + $buttontext = $Lang::tr{'add'}; + print "\n"; + } + + print < + - - - + + + + + +
$Lang::tr{'update'}
$Lang::tr{'dnsforward add a new entry'}
$Lang::tr{'dnsforward add a new entry'}: $Lang::tr{'ip address'}: $Lang::tr{'remark'}: