xref: /dragonfly/contrib/dhcpcd/hooks/30-hostname (revision f984587a)
1# Set the hostname from DHCP data if required
2
3# A hostname can either be a short hostname or a FQDN.
4# hostname_fqdn=true
5# hostname_fqdn=false
6# hostname_fqdn=server
7
8# A value of server means just what the server says, don't manipulate it.
9# This could lead to an inconsistent hostname on a DHCPv4 and DHCPv6 network
10# where the DHCPv4 hostname is short and the DHCPv6 has an FQDN.
11# DHCPv6 has no hostname option.
12# RFC4702 section 3.1 says FQDN should be prefered over hostname.
13#
14# As such, the default is hostname_fqdn=true so that a consistent hostname
15# is always assigned.
16: ${hostname_fqdn:=true}
17
18# If we used to set the hostname, but relinquish control of it, we should
19# reset to the default value.
20: ${hostname_default=}
21
22# Some systems don't have hostname(1)
23_hostname()
24{
25	if [ -z "${1+x}" ]; then
26		if [ -r /proc/sys/kernel/hostname ]; then
27			read name </proc/sys/kernel/hostname && echo "$name"
28		elif command -v hostname >/dev/null 2>/dev/null; then
29			hostname
30		elif sysctl kern.hostname >/dev/null 2>&1; then
31			sysctl -n kern.hostname
32		elif sysctl kernel.hostname >/dev/null 2>&1; then
33			sysctl -n kernel.hostname
34		else
35			return 1
36		fi
37		return $?
38	fi
39
40	if [ -w /proc/sys/kernel/hostname ]; then
41		echo "$1" >/proc/sys/kernel/hostname
42	elif [ -n "$1" ] && command -v hostname >/dev/null 2>&1; then
43		hostname "$1"
44	elif sysctl kern.hostname >/dev/null 2>&1; then
45		sysctl -w "kern.hostname=$1" >/dev/null
46	elif sysctl kernel.hostname >/dev/null 2>&1; then
47		sysctl -w "kernel.hostname=$1" >/dev/null
48	else
49		# May fail to set a blank hostname
50		hostname "$1"
51	fi
52}
53
54is_default_hostname()
55{
56	case "$1" in
57	""|"$hostname_default"|localhost|localhost.localdomain)
58		return 0;;
59	esac
60	return 1
61}
62
63need_hostname()
64{
65	# Always load the hostname variable for future use
66	hostname="$(_hostname)"
67	is_default_hostname "$hostname" && return 0
68
69	case "$force_hostname" in
70	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;;
71	esac
72
73	if [ -n "$old_fqdn" ]; then
74		if ${hfqdn} || ! ${hshort}; then
75			[ "$hostname" = "$old_fqdn" ]
76		else
77			[ "$hostname" = "${old_fqdn%%.*}" ]
78		fi
79	elif [ -n "$old_host_name" ]; then
80		if ${hfqdn}; then
81			if [ -n "$old_domain_name" ] &&
82			   [ "$old_host_name" = "${old_host_name#*.}" ]
83			then
84				[ "$hostname" = \
85				    "$old_host_name.$old_domain_name" ]
86			else
87				[ "$hostname" = "$old_host_name" ]
88			fi
89		elif ${hshort}; then
90			[ "$hostname" = "${old_host_name%%.*}" ]
91		else
92			[ "$hostname" = "$old_host_name" ]
93		fi
94	else
95		# No old hostname
96		false
97	fi
98}
99
100try_hostname()
101{
102	[ "$hostname" = "$1" ] && return 0
103	if valid_domainname "$1"; then
104		syslog info "Setting hostname: $1"
105		_hostname "$1"
106	else
107		syslog err "Invalid hostname: $1"
108	fi
109}
110
111set_hostname()
112{
113	hfqdn=false
114	hshort=false
115	case "$hostname_fqdn" in
116	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1)	hfqdn=true;;
117	""|[Ss][Ee][Rr][Vv][Ee][Rr])		;;
118	*)					hshort=true;;
119	esac
120
121	need_hostname || return
122
123	if [ -n "$new_fqdn" ]; then
124		if ${hfqdn} || ! ${hshort}; then
125			try_hostname "$new_fqdn"
126		else
127			try_hostname "${new_fqdn%%.*}"
128		fi
129	elif [ -n "$new_host_name" ]; then
130		if ${hfqdn}; then
131			if [ -n "$new_domain_name" ] &&
132			   [ "$new_host_name" = "${new_host_name#*.}" ]
133			then
134				try_hostname "$new_host_name.$new_domain_name"
135			else
136				try_hostname "$new_host_name"
137			fi
138		elif ${hshort}; then
139			try_hostname "${new_host_name%%.*}"
140		else
141			try_hostname "$new_host_name"
142		fi
143	elif ! is_default_hostname "$hostname"; then
144		try_hostname "$hostname_default"
145	fi
146}
147
148# For ease of use, map DHCP6 names onto our DHCP4 names
149case "$reason" in
150BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
151	new_fqdn="$new_dhcp6_fqdn"
152	old_fqdn="$old_dhcp6_fqdn"
153	;;
154esac
155
156if $if_configured && $if_up && [ "$reason" != ROUTERADVERT ]; then
157	set_hostname
158fi
159