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