[Ovirt-devel] [PATCH] Prevents duplicate DNS and NTP server entries.

Darryl L. Pierce dpierce at redhat.com
Mon Jan 18 19:50:57 UTC 2010


Checks any entered address for DNS and NTP servers against previously
entered ones in order to prevent duplicates.

Resolves: rhbz#555373

Signed-off-by: Darryl L. Pierce <dpierce at redhat.com>
---
 scripts/ovirt-config-networking |   54 +++++++++++++++++++++++++++-----------
 scripts/ovirt-functions         |    7 +++++
 2 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/scripts/ovirt-config-networking b/scripts/ovirt-config-networking
index ec154c2..f71d44a 100755
--- a/scripts/ovirt-config-networking
+++ b/scripts/ovirt-config-networking
@@ -358,7 +358,8 @@ function configure_dns
                     while true; do
                         printf "\n"
                         read -ep "Please enter the ${dns} DNS server (or ENTER to exit): "
-                        if [[ -z "${REPLY}" ]]; then
+                        local ADDRESS=$(trim_whitespace "$REPLY")
+                        if [[ -z "${ADDRESS}" ]]; then
                             if [[ -z "${DNS}" ]]; then
                                 printf "\nAborted...\n"
                                 return
@@ -366,15 +367,20 @@ function configure_dns
                                 break
                             fi
                         fi
-                        if is_valid_ipv4 $REPLY; then
+                        if is_valid_ipv4 $ADDRESS; then
                             if [[ -z "${DNS}" ]]; then
-                                DNS="${REPLY}"
-                            elif [[ -n "${REPLY}" ]]; then
-                                DNS="${DNS}:${REPLY}"
+                                DNS="${ADDRESS}"
+                                break
+                            elif [[ -n "${ADDRESS}" ]]; then
+                                if [[ ! $DNS =~ "^${ADDRESS}$" ]]; then
+                                    DNS="${DNS}:${ADDRESS}"
+                                    break
+                                else
+                                    printf "${ADDRESS} is already defined as a DNS server.\n"
+                                fi
                             fi
-                            break
                         else
-                            printf "${REPLY} is an invalid address.\n"
+                            printf "${ADDRESS} is an invalid address.\n"
                         fi
                     done
                 done
@@ -403,25 +409,41 @@ function configure_ntp
 {
     local AUTO=$2
     if [[ "$AUTO" == "AUTO" && -n "$OVIRT_NTP" ]]; then
-	NTPSERVERS=$OVIRT_NTP
+        SERVERS=$OVIRT_NTP
     else
-	NTPSERVERS=""
+        SERVERS=""
     fi
 
     if [ -z "$AUTO" ]; then
         if has_configured_interface true; then
-	    while true; do
-	        read -ep "Enter an NTP server (hit return when finished): "
+            while true; do
+                read -ep "Enter an NTP server (hit return when finished): "
+                local address=$(trim_whitespace "$REPLY")
 
-	        if [ -z "$REPLY" ]; then break; fi
+                if [ -z "$address" ]; then break; fi
 
-                if is_valid_ipv4 $REPLY; then
-                    NTPSERVERS="${NTPSERVERS}:${REPLY}"
+                if is_valid_ipv4 $address; then
+                    local unique=true
+                    for server in $SERVERS; do
+                        if [[ $server =~ "^${address}$" ]]; then
+                            unique=false
+                        fi
+                    done
+                    if $unique; then
+                        SERVERS="${SERVERS} ${address}"
+                    else
+                        printf "${address} is already defined as an NTP server.\n"
+                    fi
                 else
-                    printf "${REPLY} is an invalid address.\n"
+                    printf "${address} is an invalid address.\n"
                 fi
-	    done
+            done
         fi
+        NTPSERVERS=""
+        for server in $SERVERS; do
+            if [ -n "${NTPSERVERS}" ]; then server=":${server}"; fi
+            NTPSERVERS="${NTPSERVERS}${server}"
+        done
     fi
 }
 
diff --git a/scripts/ovirt-functions b/scripts/ovirt-functions
index 6435387..0d47dc8 100644
--- a/scripts/ovirt-functions
+++ b/scripts/ovirt-functions
@@ -598,6 +598,13 @@ add_if_not_exist() {
         || echo "$string" >> "$file"
 }
 
+# $1 - the string to be trimmed
+trim_whitespace () {
+    local text=${1}
+
+    printf "$text" | awk '{gsub(/^[ ]*/,"",$0); gsub(/[ ]*$/,"",$0) ; print }'
+}
+
 is_numeric() {
     printf "$1" | grep -q -E '^[0-9]+$'
 }
-- 
1.6.5.2




More information about the ovirt-devel mailing list