Browse Source

Add email validation and hostname check (#2216)

Jaap Marcus 4 years ago
parent
commit
ec32653131
2 changed files with 47 additions and 4 deletions
  1. 22 0
      install/hst-install-debian.sh
  2. 25 4
      install/hst-install-ubuntu.sh

+ 22 - 0
install/hst-install-debian.sh

@@ -180,6 +180,16 @@ validate_hostname () {
     fi
 }
 
+validate_email (){
+  if [[ ! "$email" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.[A-Za-z]{2,63}$ ]] ; then
+    # Email invalid
+    return 0
+  else
+    # Email valid
+    return 1
+  fi
+}
+
 
 #----------------------------------------------------------#
 #                    Verifications                         #
@@ -586,7 +596,15 @@ if [ "$interactive" = 'yes' ]; then
 
     # Asking for contact email
     if [ -z "$email" ]; then
+        while validate_email; do
+        echo -e "\nPlease use a valid emailadress (ex. [email protected])."
         read -p 'Please enter admin email address: ' email
+        done
+    else
+      if validate_email; then
+      echo "Please use a valid emailadress (ex. [email protected])."
+      exit 1
+      fi
     fi
 
     # Asking to set FQDN hostname
@@ -630,6 +648,10 @@ if ! [[ "$servername" =~ ^${mask1}${mask2}$ ]]; then
     echo "127.0.0.1 $servername" >> /etc/hosts
 fi
 
+if [ -z $(grep -i "$servername" /etc/hosts) ]; then
+  echo "127.0.0.1 $servername" >> /etc/hosts
+fi
+
 # Set email if it wasn't set
 if [ -z "$email" ]; then
     email="admin@$servername"

+ 25 - 4
install/hst-install-ubuntu.sh

@@ -161,6 +161,15 @@ validate_hostname () {
     fi
 }
 
+validate_email (){
+  if [[ ! "$email" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.[A-Za-z]{2,63}$ ]] ; then
+    # Email invalid
+    return 0
+  else
+    # Email valid
+    return 1
+  fi
+}
 
 #----------------------------------------------------------#
 #                    Verifications                         #
@@ -557,11 +566,19 @@ if [ "$interactive" = 'yes' ]; then
         exit 1
     fi
 
-    # Asking for contact email
-    if [ -z "$email" ]; then
-        read -p 'Please enter admin email address: ' email
+  # Asking for contact email
+  if [ -z "$email" ]; then
+      while validate_email; do
+      echo -e "\nPlease use a valid emailadress (ex. [email protected])."
+      read -p 'Please enter admin email address: ' email
+      done
+  else
+    if validate_email; then
+    echo "Please use a valid emailadress (ex. [email protected])."
+    exit 1
     fi
-
+  fi
+  
     # Asking to set FQDN hostname
     if [ -z "$servername" ]; then
         # Ask and validate FQDN hostname.
@@ -603,6 +620,10 @@ if ! [[ "$servername" =~ ^${mask1}${mask2}$ ]]; then
     echo "127.0.0.1 $servername" >> /etc/hosts
 fi
 
+if [ -z $(grep -i "$servername" /etc/hosts) ];  then
+  echo "127.0.0.1 $servername" >> /etc/hosts
+fi
+
 # Set email if it wasn't set
 if [ -z "$email" ]; then
     email="admin@$servername"