[Ovirt-devel] [PATCH node] validate hostname in ovirt-config-hostname

Joey Boggs jboggs at redhat.com
Tue Mar 23 19:44:52 UTC 2010


---
 scripts/ovirt-config-hostname |   24 ++++++++++++++++++++----
 1 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/scripts/ovirt-config-hostname b/scripts/ovirt-config-hostname
index 00d4214..0a86884 100755
--- a/scripts/ovirt-config-hostname
+++ b/scripts/ovirt-config-hostname
@@ -42,16 +42,31 @@ EOF
     return $rc
 }
 
+is_valid_hostname () {
+    local host=${1}
+    local result=1
+    if [[ $host =~ "^([a-zA-Z0-9._-]+)$" ]]; then
+        result=$?
+    fi
+    return $result
+}
+
 function prompt_user {
+  rc=0
+  while true; do
     printf "\n"
     read -p "What is the hostname for this node? "
 
     if [ -n "$REPLY" ]; then
+        if ! is_valid_hostname "$REPLY"; then
+            printf "\nInvalid Hostname\n"
+            continue
+        fi
         if set_hostname $REPLY; then
             printf "\nHostname has been set\n"
         else
             printf "\nSetting hostname failed\n"
-            return 1
+            rc=1
         fi
     else
         printf "\n"
@@ -60,18 +75,19 @@ function prompt_user {
             0)
                 if remove_hostname; then
                     printf "\nHostname was removed.\n"
-                    return 0
                 else
                     printf "\nRemoving hostname failed\n"
-                    return 1
+                    rc=1
                 fi
                 ;;
             1)
                 printf "\nNo changes made.\n"
-                return 0
                 ;;
         esac
     fi
+    break
+  done
+  return $rc
 }
 
 # AUTO for auto-install
-- 
1.6.6.1




More information about the ovirt-devel mailing list