xref: /freebsd/libexec/rc/rc.d/hostname (revision 0696600c41600d80bcd993bfd8e675d0ae6951fe)
1*0696600cSBjoern A. Zeeb#!/bin/sh
2*0696600cSBjoern A. Zeeb#
3*0696600cSBjoern A. Zeeb# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4*0696600cSBjoern A. Zeeb#
5*0696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
6*0696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
7*0696600cSBjoern A. Zeeb# are met:
8*0696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
9*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
10*0696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
11*0696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
12*0696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
13*0696600cSBjoern A. Zeeb#
14*0696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15*0696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*0696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*0696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
18*0696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*0696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*0696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*0696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*0696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*0696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*0696600cSBjoern A. Zeeb# SUCH DAMAGE.
25*0696600cSBjoern A. Zeeb#
26*0696600cSBjoern A. Zeeb# $FreeBSD$
27*0696600cSBjoern A. Zeeb#
28*0696600cSBjoern A. Zeeb
29*0696600cSBjoern A. Zeeb# PROVIDE: hostname
30*0696600cSBjoern A. Zeeb# REQUIRE: FILESYSTEMS
31*0696600cSBjoern A. Zeeb# BEFORE:  netif
32*0696600cSBjoern A. Zeeb
33*0696600cSBjoern A. Zeeb. /etc/rc.subr
34*0696600cSBjoern A. Zeeb. /etc/network.subr
35*0696600cSBjoern A. Zeeb
36*0696600cSBjoern A. Zeebname="hostname"
37*0696600cSBjoern A. Zeebdesc="Set the system\'s hostname"
38*0696600cSBjoern A. Zeebstart_cmd="hostname_start"
39*0696600cSBjoern A. Zeebstop_cmd=":"
40*0696600cSBjoern A. Zeeb
41*0696600cSBjoern A. Zeebhostname_start()
42*0696600cSBjoern A. Zeeb{
43*0696600cSBjoern A. Zeeb	# If we are not inside a jail, set the host name.
44*0696600cSBjoern A. Zeeb	# If we are inside a jail, set the host name if it is permitted.
45*0696600cSBjoern A. Zeeb	#
46*0696600cSBjoern A. Zeeb	if [ `$SYSCTL_N security.jail.jailed` -eq 1 ]; then
47*0696600cSBjoern A. Zeeb		if [ `$SYSCTL_N security.jail.set_hostname_allowed` -eq 0 ]; then
48*0696600cSBjoern A. Zeeb			return
49*0696600cSBjoern A. Zeeb		fi
50*0696600cSBjoern A. Zeeb	else
51*0696600cSBjoern A. Zeeb		# If we're not in a jail and rc.conf doesn't specify a
52*0696600cSBjoern A. Zeeb		# hostname, see if we can get one from kenv.
53*0696600cSBjoern A. Zeeb		#
54*0696600cSBjoern A. Zeeb		if [ -z "${hostname}" -a \
55*0696600cSBjoern A. Zeeb		    -n "`/bin/kenv dhcp.host-name 2> /dev/null`" ]; then
56*0696600cSBjoern A. Zeeb			hostname=`/bin/kenv dhcp.host-name`
57*0696600cSBjoern A. Zeeb		fi
58*0696600cSBjoern A. Zeeb	fi
59*0696600cSBjoern A. Zeeb
60*0696600cSBjoern A. Zeeb	# Have we got a hostname yet?
61*0696600cSBjoern A. Zeeb	#
62*0696600cSBjoern A. Zeeb	if [ -z "${hostname}" ]; then
63*0696600cSBjoern A. Zeeb		# Null hostname is probably OK if DHCP is in use,
64*0696600cSBjoern A. Zeeb		# or when hostname is already set (common for jails).
65*0696600cSBjoern A. Zeeb		#
66*0696600cSBjoern A. Zeeb		if [ -z "`list_net_interfaces dhcp`" -a \
67*0696600cSBjoern A. Zeeb		     -z "`/bin/hostname`" ]; then
68*0696600cSBjoern A. Zeeb			warn "\$hostname is not set -- see rc.conf(5)."
69*0696600cSBjoern A. Zeeb		fi
70*0696600cSBjoern A. Zeeb		return
71*0696600cSBjoern A. Zeeb	fi
72*0696600cSBjoern A. Zeeb
73*0696600cSBjoern A. Zeeb	# All right, it is safe to invoke hostname(1) now.
74*0696600cSBjoern A. Zeeb	#
75*0696600cSBjoern A. Zeeb	check_startmsgs && echo -n "Setting hostname: ${hostname}"
76*0696600cSBjoern A. Zeeb	/bin/hostname "${hostname}"
77*0696600cSBjoern A. Zeeb	check_startmsgs && echo '.'
78*0696600cSBjoern A. Zeeb}
79*0696600cSBjoern A. Zeeb
80*0696600cSBjoern A. Zeebload_rc_config $name
81*0696600cSBjoern A. Zeebrun_rc_command "$1"
82