From c8dcd46537bebe4f59cd7c22d09c45e98bfecb1f Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Thu, 9 Jan 2020 16:08:13 +0100 Subject: [PATCH] general-functions.pl: Add get_nameservers(). This function simply return an array of all used nameservers. It also takes care if the usage of ISP assigned nameservers is enabled or not and if user-added nameservers are enabled or not. Signed-off-by: Stefan Schantl --- config/cfgroot/general-functions.pl | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl index 8450194ce1..448f4c6355 100644 --- a/config/cfgroot/general-functions.pl +++ b/config/cfgroot/general-functions.pl @@ -1208,4 +1208,54 @@ sub grab_address_from_file($) { return; } +# Function to get all configured and enabled nameservers. +sub get_nameservers () { + my %settings; + my %servers; + + my @nameservers; + + # Read DNS configuration. + &readhash("$General::swroot/dns/settings", \%settings); + + # Read configured DNS servers. + &readhasharray("$General::swroot/dns/servers", \%servers); + + # Check if the ISP assigned server should be used. + if ($settings{'USE_ISP_NAMESERVERS'} eq "on") { + # Assign ISP nameserver files. + my @ISP_nameserver_files = ( "/var/run/dns1", "/var/run/dns2" ); + + # Loop through the array of ISP assigned DNS servers. + foreach my $file (@ISP_nameserver_files) { + # Grab the IP address. + my $address = &grab_address_from_file($file); + + # Check if an address has been grabbed. + if ($address) { + # Add the address to the array of nameservers. + push(@nameservers, $address); + } + } + } + + # Check if DNS servers are configured. + if (%servers) { + # Loop through the hash of configured DNS servers. + foreach my $id (keys %servers) { + my $address = $servers{$id}[0]; + my $status = $servers{$id}[2]; + + # Check if the current processed server is enabled. + if ($status eq "enabled") { + # Add the address to the array of nameservers. + push(@nameservers, $address); + } + } + } + + # Return the array. + return @nameservers; +} + 1; -- 2.39.5