Merge remote-tracking branch 'origin/github/pr/11'
[grml-scripts.git] / usr_sbin / grml-hostname
1 #!/bin/sh
2 # Filename:      grml-hostname
3 # Purpose:       simple frontend to set hostname
4 # Authors:       (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 # shellcheck disable=SC1091
10 . /etc/grml/script-functions
11
12 check4root || exit 1
13
14 [ -n "$SUDO_USER" ] && RUNASUSER=$SUDO_USER || RUNASUSER=grml
15
16 PN='grml-hostname'
17 OLDHOSTNAME="$(hostname)"
18
19 case "$1" in -h|--help) echo "Usage: $0 [hostname]">&2; exit 1 ;; esac
20
21 if [ -n "$1" ] ; then
22    NEW_HOSTNAME="$1"
23    NONINTERACTIVE=1
24 else
25    if [ -x /usr/bin/random-hostname ] ; then
26       NEW_HOSTNAME="$(/usr/bin/random-hostname)"
27    else
28       NEW_HOSTNAME="$(hostname)"
29    fi
30 fi
31
32 if [ -z "$NONINTERACTIVE" ] ; then
33    NEW_HOSTNAME="$(dialog --stdout --title "${PN}" --extra-button --extra-label "Propose hostname" \
34    --inputbox "Set hostname (/etc/hostname, /etc/hosts and /etc/postfix/main.cf will be adjusted)\
35 \\n\\nTip: press 'Propose hostname' for another proposal" 14 70 "$NEW_HOSTNAME")"
36   retval=$?
37 else
38   retval=0
39 fi
40
41 # stupid + simplified checking for valid hostname (according to RFC 952)
42 VALIDATE=$(echo "$NEW_HOSTNAME" | tr --delete '[:alnum:]-')
43 if [ -n "$VALIDATE" ] ; then
44   if [ -z "$NONINTERACTIVE" ] ; then
45     dialog --title "${PN}" \
46       --msgbox "Error: invalid characters specified in hostname: '$VALIDATE' can not be used inside hostnames." \
47       0 0
48     exec "$0"
49   else
50     echo "Error: invalid characters specified in hostname: '$VALIDATE' can not be used inside hostnames." >&2
51     exit 1
52   fi
53 fi
54
55 case $retval in
56   0)
57      echo "$NEW_HOSTNAME" > /etc/hostname
58      echo "$NEW_HOSTNAME" > /etc/mailname
59      # shellcheck disable=SC1117
60      sed -ir "s/^\(127.0.0.1\s*\).*localhost.*/\1$NEW_HOSTNAME localhost/" /etc/hosts
61      # shellcheck disable=SC1117
62      sed -ir "s/^\(::1\s*\).*ip6-localhost.*/\1ip6-localhost ip6-loopback $NEW_HOSTNAME/" /etc/hosts
63
64      POSTFIX=''
65      if [ -r /etc/postfix/main.cf ] ; then
66        sed -i "s/^mydestination = .*/mydestination = $NEW_HOSTNAME, localhost, localhost.localdomain/" /etc/postfix/main.cf && \
67        sed -i "s/^myhostname = .*/myhostname = $NEW_HOSTNAME/" /etc/postfix/main.cf && POSTFIX='
68 Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. Do not forget to restart postfix if necessary.'
69      fi
70
71      if [ -n "$DISPLAY" ] ; then
72        if sudo -u $RUNASUSER xauth add "$(xauth -n list | grep "${OLDHOSTNAME}/unix:0" | sed "s|${OLDHOSTNAME}/unix:0|${NEW_HOSTNAME}/unix:0|")" ; then
73           sudo -u $RUNASUSER xauth remove "${OLDHOSTNAME}/unix:0"
74        fi
75      fi
76
77      if [ -x /etc/init.d/avahi-daemon ] && pgrep avahi-daemon >/dev/null ; then
78        /etc/init.d/avahi-daemon restart
79      fi
80
81      hostname "$NEW_HOSTNAME"
82
83      if [ -z "$NONINTERACTIVE" ] ; then
84         dialog --stdout --title "${PN}" --msgbox "Setting hostname to $NEW_HOSTNAME was successful.$POSTFIX" 0 0
85      else
86         echo "Setting hostname to $NEW_HOSTNAME: done"
87      fi
88      exit 0
89      ;;
90   1)
91      echo "Cancel pressed."
92      ;;
93   3)
94      $0 ;;
95   255)
96      echo "ESC pressed."
97      ;;
98 esac
99
100 ## END OF FILE #################################################################