File: hostname

package info (click to toggle)
base-config 2.53.10.3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,072 kB
  • ctags: 36
  • sloc: sh: 1,755; makefile: 101; perl: 48
file content (130 lines) | stat: -rwxr-xr-x 3,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh
# Set the current hostname if empty, and create /etc/hostname if missing.
set -e

. /usr/share/debconf/confmodule

db_capb backup

hostnamefile=/etc/hostname

currname=`uname -n`
defaultname=debian

log() {
    echo "base-config: $@" >&2
}

info() {
    log "info: $@"
}

set_default_from_dns() {
	# The interface testing work best if the loopback device is skipped.
	# Sorted order to test eth1 before eth0, so that the setting in eth0
	# is the one that take effect
	interfaces=`netstat -i | tail -n +3 | awk '{print $1}' | grep -v '^lo$' | sort -r`
	for interface in $interfaces; do
		ip=`/sbin/ifconfig $interface 2>&1 | grep 'inet addr:' | tr a-zA-Z: " " | awk '{print $1}'`
		if [ "$ip" ]; then
		    dnsname=`getent hosts $ip | awk '{ print $2 }'`
		    if [ "$dnsname" ]; then
				db_set base-config/get-hostname "$dnsname"
				priority=medium
		    else
				info "Unable to find IP address '$ip' in DNS."
		    fi
		else
			info "Unable to find IP address on if '$interface'"
		fi
	done
}

# Check the hostname for RFC 1123 compliance.  2 < length of each part
# < 63, only characters 'a-z.-', and no dash at the start or at the
# end.
is_hostname_bad() {
	filtered=`echo -n "$1" | sed 's/[^A-Za-z0-9.-]//'`
	if [ "$1" != "$filtered" ]; then
	    return 0;
	fi
	for part in `echo -n "$1" | tr . " "`; do
	    length=`echo -n "$part" |wc -c`
	    if [ 2 -gt "$length" ] || [ "$length" -ge 63 ]; then
		return 0
	    fi
	    if echo -n "$part" | grep -E -q "^-|-$"; then
		return 0
	    fi
	done
	return 1
}

if [ -z "$currname" ] || [ "$currname" = localhost ]; then
	# Check IP addresses of interfaces, try to look up
	# using DNS, use that name as default hostname, and
	# ask medium priority question if there is a
	# default, and high priority question if the default
	# is empty.
	priority=high

	# Make sure there is some good default.  Only change
	# the default value, to make it possible to override
	# this using the debconf database.
	db_set base-config/get-hostname $defaultname
	set_default_from_dns
else
	# Ask at medium priority so the menu item does something if
	# manaully selected.
	priority=medium
	db_set base-config/get-hostname $currname
fi

# Prompt for the hostname, using the current name, if any, as the default.
hostname=
LOOPCOUNT=2
while [ -z "$hostname" ]; do
	db_input "$priority" base-config/get-hostname || break
	if ! db_go; then
		db_fset base-config/get-hostname seen false
		exit 30 # back to menu
	fi
	db_fset base-config/get-hostname seen false
	db_get base-config/get-hostname
	hostname="$RET"

	if is_hostname_bad "$hostname"; then
		db_subst base-config/invalid-hostname HOSTNAME "$hostname"
		db_input critical base-config/invalid-hostname || [ $? -eq 30 ]
		if ! db_go; then
			db_fset base-config/invalid-hostname seen false
			exit 30 # back to menu TODO should use state machine
		fi
		db_fset base-config/invalid-hostname seen false
		hostname=
	fi

	# Only loop LOOPCOUNT times.
	LOOPCOUNT=`expr $LOOPCOUNT - 1 || true`
	if [ "0" -ge "$LOOPCOUNT" ]; then
		if [ -z "$hostname" ]; then
			hostname="$defaultname"
			info "Terminating loop, setting hostname to '$hostname'"
		fi
	fi
done

# If the user failed to enter a hostname, then use the current name.
if [ -z "$hostname" ]; then
	if [ -z "$currname" ]; then
		hostname=$defaultname
	else
		hostname=$currname
	fi
fi

# Set hostname.
if [ "$hostname" != "$currname" ]; then
	echo "$hostname" > "$hostnamefile"
	hostname "$hostname"
fi