xref: /freebsd/usr.sbin/bsdconfig/networking/share/hostname.subr (revision f8ea072a542112d5e0e74a2d6ecf75d967c3054c)
1ab2043b8SDevin Teskeif [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1
2ab2043b8SDevin Teske#
37323adacSDevin Teske# Copyright (c) 2006-2013 Devin Teske
4*f8ea072aSDevin Teske# All rights reserved.
5ab2043b8SDevin Teske#
6ab2043b8SDevin Teske# Redistribution and use in source and binary forms, with or without
7ab2043b8SDevin Teske# modification, are permitted provided that the following conditions
8ab2043b8SDevin Teske# are met:
9ab2043b8SDevin Teske# 1. Redistributions of source code must retain the above copyright
10ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer.
11ab2043b8SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
12ab2043b8SDevin Teske#    notice, this list of conditions and the following disclaimer in the
13ab2043b8SDevin Teske#    documentation and/or other materials provided with the distribution.
14ab2043b8SDevin Teske#
15ab2043b8SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16ab2043b8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17ab2043b8SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ab2043b8SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ab2043b8SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ab2043b8SDevin Teske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21ab2043b8SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22ab2043b8SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23ab2043b8SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24ab2043b8SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25ab2043b8SDevin Teske# SUCH DAMAGE.
26ab2043b8SDevin Teske#
27ab2043b8SDevin Teske# $FreeBSD$
28ab2043b8SDevin Teske#
29ab2043b8SDevin Teske############################################################ INCLUDES
30ab2043b8SDevin Teske
31ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
32ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3356961fd7SDevin Teskef_dprintf "%s: loading includes..." networking/hostname.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/common.subr
36ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/resolv.subr
371de60ff0SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr
38ab2043b8SDevin Teske
39ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
40ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41ab2043b8SDevin Teske
42ab2043b8SDevin Teske############################################################ FUNCTIONS
43ab2043b8SDevin Teske
4435a157a0SDevin Teske# f_dialog_hnerror $error $hostname
45ab2043b8SDevin Teske#
4635a157a0SDevin Teske# Display a msgbox with the appropriate error message for an error returned by
4735a157a0SDevin Teske# the f_validate_hostname function.
48ab2043b8SDevin Teske#
4935a157a0SDevin Teskef_dialog_hnerror()
5035a157a0SDevin Teske{
5135a157a0SDevin Teske	local error="$1" fqhn="$2"
5235a157a0SDevin Teske
5335a157a0SDevin Teske	[ ${error:-0} -ne 0 ] || return $SUCCESS
5435a157a0SDevin Teske
5535a157a0SDevin Teske	case "$error" in
56fa2e39c8SDevin Teske	1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn" ;;
57c9c67103SDevin Teske	2) f_show_msg \
58c9c67103SDevin Teske		"$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn" ;;
59fa2e39c8SDevin Teske	3) f_show_msg "$msg_hostname_label_is_null" "$fqhn" ;;
60fa2e39c8SDevin Teske	63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn" ;;
61fa2e39c8SDevin Teske	255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn" ;;
62ab2043b8SDevin Teske	esac
6335a157a0SDevin Teske}
6435a157a0SDevin Teske
6535a157a0SDevin Teske# f_dialog_validate_hostname $hostname
6635a157a0SDevin Teske#
6735a157a0SDevin Teske# Returns zero if the given argument (a fully-qualified hostname) is compliant
6835a157a0SDevin Teske# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
6935a157a0SDevin Teske#
7035a157a0SDevin Teske# RFC 952 - DoD Internet host table specification
7135a157a0SDevin Teske# http://tools.ietf.org/html/rfc952
7235a157a0SDevin Teske#
7335a157a0SDevin Teske# RFC 1123 - Requirements for Internet Hosts - Application and Support
7435a157a0SDevin Teske# http://tools.ietf.org/html/rfc1123
7535a157a0SDevin Teske#
7635a157a0SDevin Teske# If the hostname is determined to be invalid, the appropriate error will be
7735a157a0SDevin Teske# displayed using the f_dialog_hnerror function above.
7835a157a0SDevin Teske#
7935a157a0SDevin Teskef_dialog_validate_hostname()
8035a157a0SDevin Teske{
8135a157a0SDevin Teske	local fqhn="$1"
8235a157a0SDevin Teske
8335a157a0SDevin Teske	f_validate_hostname "$fqhn"
8435a157a0SDevin Teske	local retval=$?
8535a157a0SDevin Teske
8635a157a0SDevin Teske	# Produce an appropriate error message if necessary.
8735a157a0SDevin Teske	[ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn"
88ab2043b8SDevin Teske
89ab2043b8SDevin Teske	return $retval
90ab2043b8SDevin Teske}
91ab2043b8SDevin Teske
92ab2043b8SDevin Teske# f_dialog_input_hostname
93ab2043b8SDevin Teske#
94ab2043b8SDevin Teske# Edits the current hostname.
95ab2043b8SDevin Teske#
96ab2043b8SDevin Teskef_dialog_input_hostname()
97ab2043b8SDevin Teske{
98ab2043b8SDevin Teske	local hostname="$( f_sysrc_get 'hostname:-$(hostname)' )"
99ab2043b8SDevin Teske	local hostname_orig="$hostname" # for change-tracking
100ab2043b8SDevin Teske
101ab2043b8SDevin Teske	local msg
102ab2043b8SDevin Teske	if [ "$USE_XDIALOG" ]; then
103ab2043b8SDevin Teske		msg="$xmsg_please_enter_fqhn"
104ab2043b8SDevin Teske	else
105ab2043b8SDevin Teske		msg="$msg_please_enter_fqhn"
106ab2043b8SDevin Teske	fi
107ab2043b8SDevin Teske
108ab2043b8SDevin Teske	#
109ab2043b8SDevin Teske	# Loop until the user provides taint-free input.
110ab2043b8SDevin Teske	#
111ab2043b8SDevin Teske	while :; do
112ec7120b5SDevin Teske		f_dialog_input hostname "$msg" "$hostname" \
113ec7120b5SDevin Teske		               "$hline_alnum_punc_tab_enter" || return
114ab2043b8SDevin Teske		# Taint-check the user's input
115ab2043b8SDevin Teske		f_dialog_validate_hostname "$hostname" && break
116ab2043b8SDevin Teske	done
117ab2043b8SDevin Teske
118ab2043b8SDevin Teske	#
119ab2043b8SDevin Teske	# Save hostname only if the user changed the hostname.
120ab2043b8SDevin Teske	#
121ab2043b8SDevin Teske	if [ "$hostname" != "$hostname_orig" ]; then
122ab2043b8SDevin Teske		f_dialog_info "$msg_saving_hostname"
123ab2043b8SDevin Teske		f_sysrc_set hostname "$hostname"
124ab2043b8SDevin Teske	fi
125ab2043b8SDevin Teske
126ab2043b8SDevin Teske	#
127ab2043b8SDevin Teske	# Update resolv.conf(5) search/domain directives
128ab2043b8SDevin Teske	#
129ab2043b8SDevin Teske	f_dialog_resolv_conf_update "$hostname"
130ab2043b8SDevin Teske
131ab2043b8SDevin Teske	#
132ab2043b8SDevin Teske	# Only ask to apply setting if the current hostname is different than
133ab2043b8SDevin Teske	# the stored configuration (in rc.conf(5)).
134ab2043b8SDevin Teske	#
135ab2043b8SDevin Teske	if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then
136f788e301SDevin Teske		[ ! "$USE_XDIALOG" ] && f_dialog_clear
137ab2043b8SDevin Teske
138ab2043b8SDevin Teske		#
139ab2043b8SDevin Teske		# If connected via ssh(1) and performing X11-Forwarding, don't
140ab2043b8SDevin Teske		# allow the hostname to be changed to prevent the fatal error
141ab2043b8SDevin Teske		# "X11 connection rejected because of wrong authentication."
142ab2043b8SDevin Teske		#
143ab2043b8SDevin Teske		if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then
144fa2e39c8SDevin Teske			f_show_msg "$msg_activate_hostname_x11warning" \
145ab2043b8SDevin Teske			           "$( hostname )" "$hostname"
146ab2043b8SDevin Teske		else
14727ff90aaSDevin Teske			f_yesno "$msg_activate_hostname" \
148ab2043b8SDevin Teske			        "$( hostname )" "$hostname" \
149ab2043b8SDevin Teske			&& hostname "$hostname"
150ab2043b8SDevin Teske		fi
151ab2043b8SDevin Teske	fi
152ab2043b8SDevin Teske
153ab2043b8SDevin Teske	return $SUCCESS
154ab2043b8SDevin Teske}
155ab2043b8SDevin Teske
15656961fd7SDevin Teske############################################################ MAIN
15756961fd7SDevin Teske
15856961fd7SDevin Teskef_dprintf "%s: Successfully loaded." networking/hostname.subr
15956961fd7SDevin Teske
160ab2043b8SDevin Teskefi # ! $_NETWORKING_HOSTNAME_SUBR
161