grml-hostname: reject invalid hostnames mika/validate_hostname
authorMichael Prokop <mika@grml.org>
Mon, 3 Sep 2012 10:43:44 +0000 (12:43 +0200)
committerMichael Prokop <mika@grml.org>
Mon, 3 Sep 2012 11:59:06 +0000 (13:59 +0200)
The check doesn't fully verify RFC 952, but it catches some
known-problematic situations and it's better than nothing.

Closes: gf-issue127

Thanks: Evgeni Golov for review

usr_sbin/grml-hostname

index b529d17..b7a6eab 100755 (executable)
@@ -37,6 +37,20 @@ else
   retval=0
 fi
 
+# stupid + simplified checking for valid hostname (according to RFC 952)
+VALIDATE=$(echo "$NEW_HOSTNAME" | tr --delete '[:alnum:]-')
+if [ -n "$VALIDATE" ] ; then
+  if [ -z "$NONINTERACTIVE" ] ; then
+    dialog --title "${PN}" \
+      --msgbox "Error: invalid characters specified in hostname: '$VALIDATE' can not be used inside hostnames." \
+      0 0
+    exec "$0"
+  else
+    echo "Error: invalid characters specified in hostname: '$VALIDATE' can not be used inside hostnames." >&2
+    exit 1
+  fi
+fi
+
 case $retval in
   0)
      echo "$NEW_HOSTNAME" > /etc/hostname