dhcpcd: Update to dhcpcd-9.3.3 with the following changes:
[dragonfly.git] / contrib / dhcpcd / hooks / 30-hostname.in
blobabeb369672210351bf2d6253cb14c8dd5ff2fe09
1 # Set the hostname from DHCP data if required
3 # A hostname can either be a short hostname or a FQDN.
4 # hostname_fqdn=true
5 # hostname_fqdn=false
6 # hostname_fqdn=server
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.
14 # As such, the default is hostname_fqdn=true so that a consistent hostname
15 # is always assigned.
16 : ${hostname_fqdn:=true}
18 # If we used to set the hostname, but relinquish control of it, we should
19 # reset to the default value.
20 : ${hostname_default=@DEFAULT_HOSTNAME@}
22 # Some systems don't have hostname(1)
23 _hostname()
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 type 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
40         if [ -w /proc/sys/kernel/hostname ]; then
41                 echo "$1" >/proc/sys/kernel/hostname
42         elif [ -n "$1" ] && type 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
54 is_default_hostname()
56         case "$1" in
57         ""|"$hostname_default"|localhost|localhost.localdomain)
58                 return 0;;
59         esac
60         return 1
63 need_hostname()
65         # Always load the hostname variable for future use
66         hostname="$(_hostname)"
67         is_default_hostname "$hostname" && return 0
69         case "$force_hostname" in
70         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;;
71         esac
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
100 try_hostname()
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
111 set_hostname()
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
121         need_hostname || return
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
148 # For ease of use, map DHCP6 names onto our DHCP4 names
149 case "$reason" in
150 BOUND6|RENEW6|REBIND6|REBOOT6|INFORM6)
151         new_fqdn="$new_dhcp6_fqdn"
152         old_fqdn="$old_dhcp6_fqdn"
153         ;;
154 esac
156 if $if_configured && $if_up && [ "$reason" != ROUTERADVERT ]; then
157         set_hostname