]> git.ipfire.org Git - people/ms/ipfire-2.x.git/commitdiff
general-functions.pl: Add get_nameservers().
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 9 Jan 2020 15:08:13 +0000 (16:08 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 9 Jan 2020 15:08:13 +0000 (16:08 +0100)
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 <stefan.schantl@ipfire.org>
config/cfgroot/general-functions.pl

index 8450194ce1035c21df7edbf41f5f3946443fd2c5..448f4c6355ba2dfe26a352f7e36e4d24c4ebcf90 100644 (file)
@@ -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;