]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - src/scripts/convert-dns-settings
Early spring clean: Remove trailing whitespaces, and correct licence headers
[people/pmueller/ipfire-2.x.git] / src / scripts / convert-dns-settings
index f70d1382a33bbec59f97f5d30456e2a82e62c235..66c8f78eced0c0003157ccdb60080c15700604d8 100755 (executable)
@@ -1,8 +1,8 @@
 #!/bin/bash
 ###############################################################################
 #                                                                             #
-# IPFire.org - A Linux-based firewall                                         #
-# Copyright (C) 2020 IPFire Team <info@ipfire.org>                            #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
 #                                                                             #
 ###############################################################################
 
+FILES=(
+       /var/ipfire/ethernet/settings
+       /var/ipfire/dns/settings
+       /var/ipfire/ppp/settings
+       /var/ipfire/ppp/settings-*
+)
+
 main() {
        # Do not convert anything if we already have some servers set
        if [ ! -s "/var/ipfire/dns/servers" ]; then
                # Array to store all found DNS servers.
-               SERVERS=()
-
-               # Try to get the DNS servers from ethernet settings file.
-               local DNS1 DNS2
-               eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)
-
-               # Add the grabbed DNS servers to the servers array.
-               SERVERS+=($DNS1 $DNS2)
-
-               # Check if the ppp settings file is not empty.
-               if [ -s "/var/ipfire/ppp/settings" ]; then
-                       # Loop though all profile files.
-                       for file in /var/ipfire/ppp/settings*; do
-                               local DNS1 DNS2
-                               eval $(/usr/local/bin/readhash $file)
-
-                               # Add the DNS servers to the array of SERVERS.
-                               for var in DNS1 DNS2; do
-                                       local server="${!var}"
-
-                                       # Check if the servers array is empty.
-                                       if [ ${#SERVERS[@]} -eq 0 ]; then
-                                               # Allways add the first found nameserver to the array.
-                                               SERVERS+=($server)
-                                       else
-                                               # Check if the current server is allready part ot the array.
-                                               if [[ ! "${SERVERS[@]}" =~ "${server}" ]]; then
-                                                       # Add the server to the array.
-                                                       SERVERS+=($server)
-                                               fi
-                                       fi
+               local SERVERS=()
+
+               # Loop through all PPP profiles
+               local file
+               for file in "${FILES[@]}"; do
+                       if [ -s "${file}" ]; then
+                               local DNS0 DNS1 DNS2
+                               eval $(/usr/local/bin/readhash "${file}")
+
+                               # Add the DNS servers to the array of SERVERS
+                               local var
+                               for var in DNS0 DNS1 DNS2; do
+                                       SERVERS+=( "${!var}" )
                                done
-
-                               # Remove DNS1 and DNS2 settings from profile file.
-                               sed -i "/^DNS[12]=/d" $file
-
-                               # Unset the local variables for the next round.
-                               unset DNS1 DNS2
-                       done
-
-               elif [ -s "/var/ipfire/dns/settings" ]; then
-                       eval $(/usr/local/bin/readhash /var/ipfire/dns/settings)
-               fi
+                       fi
+               done
 
                local server
                local i=3
-               for server in "${SERVERS[@]}"; do
-                       echo "${i},${server},,enabled,"
-                       (( i++ ))
+               for server in $(printf "%s\n" "${SERVERS[@]}" | sort -u); do
+                       if [ -n "${server}" ]; then
+                               echo "${i},${server},,enabled,"
+                               (( i++ ))
+                       fi
                done > /var/ipfire/dns/servers
 
                # Empty the old settings file
                : > /var/ipfire/dns/settings
 
-               # Disable using ISP name servers when we already have some configured
-               if [ ${i} -gt 3 ]; then
-                       echo "USE_ISP_NAMESERVERS=off" \
+               # Enable using ISP name servers when no servers are configured
+               if [ ${i} -eq 3 ]; then
+                       echo "USE_ISP_NAMESERVERS=on" \
                                >> /var/ipfire/dns/settings
                fi
        fi
 
+       # Remove all old settings from files
+       local file
+       for file in "${FILES[@]}"; do
+               # Remove DNS, DNS0, DNS1 and DNS2
+               sed -Ei "/^DNS[012]?=/d" "${file}"
+       done
+
        # Set correct ownership.
        chown nobody:nobody /var/ipfire/dns/settings
 
@@ -106,7 +95,7 @@ main() {
                if [ "${FORCE_TCP}" = "on" ]; then
                        echo "PROTO=TCP" >> /var/ipfire/dns/settings
                fi
-       
+
                # Run in recursor mode
                if [ "${USE_FORWARDERS}" = "0" ]; then
                        # Remove all servers