X-Git-Url: http://git.grml.org/?p=grml-scripts.git;a=blobdiff_plain;f=usr_sbin%2Fgrml-hostname;h=ade8631e240056bb1f666c115bf8369a83e11d2e;hp=b529d1772107c632ef79b92b46880a7b0ca907c6;hb=HEAD;hpb=9375a86c1fe0f2936a7ff20f46aa98a4d5f1ffb3 diff --git a/usr_sbin/grml-hostname b/usr_sbin/grml-hostname index b529d17..400aeef 100755 --- a/usr_sbin/grml-hostname +++ b/usr_sbin/grml-hostname @@ -6,6 +6,7 @@ # License: This file is licensed under the GPL v2. ################################################################################ +# shellcheck disable=SC1091 . /etc/grml/script-functions check4root || exit 1 @@ -31,17 +32,33 @@ fi if [ -z "$NONINTERACTIVE" ] ; then NEW_HOSTNAME="$(dialog --stdout --title "${PN}" --extra-button --extra-label "Propose hostname" \ --inputbox "Set hostname (/etc/hostname, /etc/hosts and /etc/postfix/main.cf will be adjusted)\ -\n\nTip: press 'Propose hostname' for another proposal" 14 70 $NEW_HOSTNAME)" +\\n\\nTip: press 'Propose hostname' for another proposal" 14 70 "$NEW_HOSTNAME")" retval=$? 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 + # shellcheck disable=SC1117 sed -ir "s/^\(127.0.0.1\s*\).*localhost.*/\1$NEW_HOSTNAME localhost/" /etc/hosts + # shellcheck disable=SC1117 sed -ir "s/^\(::1\s*\).*ip6-localhost.*/\1ip6-localhost ip6-loopback $NEW_HOSTNAME/" /etc/hosts POSTFIX='' @@ -52,7 +69,7 @@ Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. D 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 + 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 @@ -61,7 +78,7 @@ Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. D /etc/init.d/avahi-daemon restart fi - /etc/init.d/hostname.sh + hostname "$NEW_HOSTNAME" if [ -z "$NONINTERACTIVE" ] ; then dialog --stdout --title "${PN}" --msgbox "Setting hostname to $NEW_HOSTNAME was successful.$POSTFIX" 0 0