Fix: Erase all forgotten stuff from grml-config-user [Closes: issue1272]
[grml-scripts.git] / usr_sbin / grml-hostname
index d512823..b7a6eab 100755 (executable)
@@ -37,24 +37,44 @@ 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
      echo "$NEW_HOSTNAME" > /etc/mailname
-     sed -i "s/^127.0.0.1[ \t]*$OLDHOSTNAME[ \t]*localhost/127.0.0.1       $NEW_HOSTNAME localhost/" /etc/hosts
-     sed -i "s/^::1[ \t]*ip6-localhost ip6-loopback[ \t]*$OLDHOSTNAME/::1     ip6-localhost ip6-loopback $NEW_HOSTNAME/" /etc/hosts
+     sed -ir "s/^\(127.0.0.1\s*\).*localhost.*/\1$NEW_HOSTNAME localhost/" /etc/hosts
+     sed -ir "s/^\(::1\s*\).*ip6-localhost.*/\1ip6-localhost ip6-loopback $NEW_HOSTNAME/" /etc/hosts
+
      POSTFIX=''
      if [ -r /etc/postfix/main.cf ] ; then
        sed -i "s/^mydestination = .*/mydestination = $NEW_HOSTNAME, localhost, localhost.localdomain/" /etc/postfix/main.cf && \
        sed -i "s/^myhostname = .*/myhostname = $NEW_HOSTNAME/" /etc/postfix/main.cf && POSTFIX='
 Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. Do not forget to restart postfix if necessary.'
      fi
+
      if [ -n "$DISPLAY" ] ; then
        if sudo -u $RUNASUSER xauth add $(xauth -n list | grep "${OLDHOSTNAME}/unix:0" | sed "s|${OLDHOSTNAME}/unix:0|${NEW_HOSTNAME}/unix:0|") ; then
          sudo -u $RUNASUSER xauth remove "${OLDHOSTNAME}/unix:0"
        fi
      fi
 
+     if [ -x /etc/init.d/avahi-daemon ] && pgrep avahi-daemon >/dev/null ; then
+       /etc/init.d/avahi-daemon restart
+     fi
+
      /etc/init.d/hostname.sh
 
      if [ -z "$NONINTERACTIVE" ] ; then