X-Git-Url: http://git.grml.org/?p=grml-scripts.git;a=blobdiff_plain;f=usr_sbin%2Fgrml-hostname;h=ade8631e240056bb1f666c115bf8369a83e11d2e;hp=b48449edcd4ca12b49b43bb1b354964f8f500cb7;hb=HEAD;hpb=9dea3087a59ada8a7595a8d8d9777c1f55d1b55e diff --git a/usr_sbin/grml-hostname b/usr_sbin/grml-hostname index b48449e..400aeef 100755 --- a/usr_sbin/grml-hostname +++ b/usr_sbin/grml-hostname @@ -4,49 +4,88 @@ # Authors: (c) Michael Prokop # Bug-Reports: see http://grml.org/bugs/ # License: This file is licensed under the GPL v2. -# Latest change: Son Dez 17 16:23:34 CET 2006 [mika] ################################################################################ -if [ $UID != 0 ] ; then - echo Error: become root before starting $0 >& 2 - exit 100 -fi +# shellcheck disable=SC1091 +. /etc/grml/script-functions + +check4root || exit 1 + +[ -n "$SUDO_USER" ] && RUNASUSER=$SUDO_USER || RUNASUSER=grml PN='grml-hostname' -TMP=$(mktemp) OLDHOSTNAME="$(hostname)" -if [ -x /usr/bin/random-hostname ] ; then - HOSTNAME="$(/usr/bin/random-hostname)" +case "$1" in -h|--help) echo "Usage: $0 [hostname]">&2; exit 1 ;; esac + +if [ -n "$1" ] ; then + NEW_HOSTNAME="$1" + NONINTERACTIVE=1 else - HOSTNAME="$(hostname)" + if [ -x /usr/bin/random-hostname ] ; then + NEW_HOSTNAME="$(/usr/bin/random-hostname)" + else + NEW_HOSTNAME="$(hostname)" + fi fi -bailout() { - rm -f $TMP - exit 1 -} -trap bailout 1 2 3 15 - -# running inside grml2hd? Don't warn because of hostname + X -[ -n "$GRML2HD" ] || MSG='\n\nNotice: setting a different hostname while running X is *not* recommend!' +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")" + retval=$? +else + retval=0 +fi -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${MSG}" 14 70 $HOSTNAME)" -retval=$? +# 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 "$HOSTNAME" > /etc/hostname - sed -i "s/^127.0.0.1[ \t]*$OLDHOSTNAME[ \t]*localhost/127.0.0.1 $HOSTNAME localhost/" /etc/hosts - sed -i "s/^::1[ \t]*ip6-localhost ip6-loopback[ \t]*$OLDHOSTNAME/::1 ip6-localhost ip6-loopback $HOSTNAME/" /etc/hosts + 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='' if [ -r /etc/postfix/main.cf ] ; then - sed -i "s/^myhostname = .*/myhostname = $HOSTNAME/" /etc/postfix/main.cf && POSTFIX=' + 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 - dialog --stdout --title "${PN}" --msgbox "Setting hostname to $HOSTNAME was successful.$POSTFIX" 0 0 + + 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 + + hostname "$NEW_HOSTNAME" + + if [ -z "$NONINTERACTIVE" ] ; then + dialog --stdout --title "${PN}" --msgbox "Setting hostname to $NEW_HOSTNAME was successful.$POSTFIX" 0 0 + else + echo "Setting hostname to $NEW_HOSTNAME: done" + fi + exit 0 ;; 1) echo "Cancel pressed." @@ -58,6 +97,4 @@ Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. D ;; esac -rm -f $TMP 2>/dev/null - ## END OF FILE #################################################################