Home | History | Annotate | Download | only in dhcpcd-hooks
      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 # Some systems don't have hostname(1)
     19 _hostname()
     20 {
     21 	local name=
     22 
     23 	if [ -z "$1" ]; then
     24 		if type hostname >/dev/null 2>&1; then
     25 			hostname
     26 		elif [ -r /proc/sys/kernel/hostname ]; then
     27 			read name </proc/sys/kernel/hostname && echo "$name"
     28 		elif sysctl kern.hostname >/dev/null 2>&1; then
     29 			sysctl -n kern.hostname
     30 		elif sysctl kernel.hostname >/dev/null 2>&1; then
     31 			sysctl -n kernel.hostname
     32 		else
     33 			return 1
     34 		fi
     35 		return $?
     36 	fi
     37 
     38 	# Always prefer hostname(1) if we have it
     39 	if type hostname >/dev/null 2>&1; then
     40 		hostname "$1"
     41 	elif [ -w /proc/sys/kernel/hostname ]; then
     42 		echo "$1" >/proc/sys/kernel/hostname
     43 	elif sysctl kern.hostname >/dev/null 2>&1; then
     44 		sysctl -w "kern.hostname=$1"
     45 	elif sysctl kernel.hostname >/dev/null 2>&1; then
     46 		sysctl -w "kernel.hostname=$1"
     47 	else
     48 		# We know this will fail, but it will now fail
     49 		# with an error to stdout
     50 		hostname "$1"
     51 	fi
     52 }
     53 
     54 need_hostname()
     55 {
     56 	local hostname hfqdn=false hshort=false
     57 
     58 	case "$force_hostname" in
     59 	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;;
     60 	esac
     61 
     62 	hostname="$(_hostname)"
     63 	case "$hostname" in
     64 	""|"(none)"|localhost|localhost.localdomain) return 0;;
     65 	esac
     66 	
     67 	case "$hostname_fqdn" in
     68 	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1)	hfqdn=true;;
     69 	[Ss][Ee][Rr][Vv][Ee][Rr])		;;
     70 	*)					hshort=true;;
     71 	esac
     72 
     73 	if [ -n "$old_fqdn" ]; then
     74 		if ${hfqdn} || ! ${hsort}; 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" -a \
     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 
    100 try_hostname()
    101 {
    102 
    103 	if valid_domainname "$1"; then
    104 		_hostname "$1"
    105 	else
    106 		syslog err "Invalid hostname: $1"
    107 	fi
    108 }
    109 
    110 set_hostname()
    111 {
    112 	local hfqdn=false hshort=false
    113 
    114 	need_hostname || return
    115 
    116 	case "$hostname_fqdn" in
    117 	[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1)	hfqdn=true;;
    118 	"")					;;
    119 	*)					hshort=true;;
    120 	esac
    121 
    122 	if [ -n "$new_fqdn" ]; then
    123 		if ${hfqdn} || ! ${hshort}; then
    124 			try_hostname "$new_fqdn"
    125 		else
    126 			try_hostname "${new_fqdn%%.*}"
    127 		fi
    128 	elif [ -n "$new_host_name" ]; then
    129 		if ${hfqdn}; then
    130 			if [ -n "$new_domain_name" -a \
    131 			    "$new_host_name" = "${new_host_name#*.}" ]
    132 			then
    133 				try_hostname "$new_host_name.$new_domain_name"
    134 			else
    135 				try_hostname "$new_host_name"
    136 			fi
    137 		elif ${hshort}; then
    138 			try_hostname "${new_host_name%%.*}"
    139 		else
    140 			try_hostname "$new_host_name"
    141 		fi
    142 	fi
    143 }
    144 
    145 # For ease of use, map DHCP6 names onto our DHCP4 names
    146 case "$reason" in
    147 BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
    148 	new_fqdn="$new_dhcp6_fqdn"
    149 	;;
    150 esac
    151 
    152 if $if_up; then
    153 	set_hostname
    154 fi
    155