xref: /freebsd/usr.sbin/bsdconfig/networking/share/hostname.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1ab2043b8SDevin Teskeif [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1
2ab2043b8SDevin Teske#
37323adacSDevin Teske# Copyright (c) 2006-2013 Devin Teske
4f8ea072aSDevin 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
168e37a7c8SDevin 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
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, 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#
28ab2043b8SDevin Teske############################################################ INCLUDES
29ab2043b8SDevin Teske
30ab2043b8SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
31ab2043b8SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3256961fd7SDevin Teskef_dprintf "%s: loading includes..." networking/hostname.subr
33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/common.subr
35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/resolv.subr
361de60ff0SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr
37ab2043b8SDevin Teske
38ab2043b8SDevin TeskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
39ab2043b8SDevin Teskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
40ab2043b8SDevin Teske
41ab2043b8SDevin Teske############################################################ FUNCTIONS
42ab2043b8SDevin Teske
4335a157a0SDevin Teske# f_dialog_hnerror $error $hostname
44ab2043b8SDevin Teske#
4535a157a0SDevin Teske# Display a msgbox with the appropriate error message for an error returned by
4635a157a0SDevin Teske# the f_validate_hostname function.
47ab2043b8SDevin Teske#
4835a157a0SDevin Teskef_dialog_hnerror()
4935a157a0SDevin Teske{
5035a157a0SDevin Teske	local error="$1" fqhn="$2"
5135a157a0SDevin Teske
5235a157a0SDevin Teske	[ ${error:-0} -ne 0 ] || return $SUCCESS
5335a157a0SDevin Teske
5435a157a0SDevin Teske	case "$error" in
55fa2e39c8SDevin Teske	1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn" ;;
56c9c67103SDevin Teske	2) f_show_msg \
57c9c67103SDevin Teske		"$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn" ;;
58fa2e39c8SDevin Teske	3) f_show_msg "$msg_hostname_label_is_null" "$fqhn" ;;
59fa2e39c8SDevin Teske	63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn" ;;
60fa2e39c8SDevin Teske	255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn" ;;
61ab2043b8SDevin Teske	esac
6235a157a0SDevin Teske}
6335a157a0SDevin Teske
6435a157a0SDevin Teske# f_dialog_validate_hostname $hostname
6535a157a0SDevin Teske#
6635a157a0SDevin Teske# Returns zero if the given argument (a fully-qualified hostname) is compliant
6735a157a0SDevin Teske# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
6835a157a0SDevin Teske#
6935a157a0SDevin Teske# RFC 952 - DoD Internet host table specification
70*a2aef24aSEitan Adler# https://tools.ietf.org/html/rfc952
7135a157a0SDevin Teske#
7235a157a0SDevin Teske# RFC 1123 - Requirements for Internet Hosts - Application and Support
73*a2aef24aSEitan Adler# https://tools.ietf.org/html/rfc1123
7435a157a0SDevin Teske#
7535a157a0SDevin Teske# If the hostname is determined to be invalid, the appropriate error will be
7635a157a0SDevin Teske# displayed using the f_dialog_hnerror function above.
7735a157a0SDevin Teske#
7835a157a0SDevin Teskef_dialog_validate_hostname()
7935a157a0SDevin Teske{
8035a157a0SDevin Teske	local fqhn="$1"
8135a157a0SDevin Teske
8235a157a0SDevin Teske	f_validate_hostname "$fqhn"
8335a157a0SDevin Teske	local retval=$?
8435a157a0SDevin Teske
8535a157a0SDevin Teske	# Produce an appropriate error message if necessary.
8635a157a0SDevin Teske	[ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn"
87ab2043b8SDevin Teske
88ab2043b8SDevin Teske	return $retval
89ab2043b8SDevin Teske}
90ab2043b8SDevin Teske
91ab2043b8SDevin Teske# f_dialog_input_hostname
92ab2043b8SDevin Teske#
93ab2043b8SDevin Teske# Edits the current hostname.
94ab2043b8SDevin Teske#
95ab2043b8SDevin Teskef_dialog_input_hostname()
96ab2043b8SDevin Teske{
97d4ae33f0SDevin Teske	local funcname=f_dialog_input_hostname
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" \
113f677a9e2SDevin 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"
123d4ae33f0SDevin Teske		f_eval_catch $funcname f_sysrc_set \
124d4ae33f0SDevin Teske			'f_sysrc_set hostname "%s"' "$hostname"
125ab2043b8SDevin Teske	fi
126ab2043b8SDevin Teske
127ab2043b8SDevin Teske	#
128ab2043b8SDevin Teske	# Update resolv.conf(5) search/domain directives
129ab2043b8SDevin Teske	#
130ab2043b8SDevin Teske	f_dialog_resolv_conf_update "$hostname"
131ab2043b8SDevin Teske
132ab2043b8SDevin Teske	#
133ab2043b8SDevin Teske	# Only ask to apply setting if the current hostname is different than
134ab2043b8SDevin Teske	# the stored configuration (in rc.conf(5)).
135ab2043b8SDevin Teske	#
136ab2043b8SDevin Teske	if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then
137f788e301SDevin Teske		[ ! "$USE_XDIALOG" ] && f_dialog_clear
138ab2043b8SDevin Teske
139ab2043b8SDevin Teske		#
140ab2043b8SDevin Teske		# If connected via ssh(1) and performing X11-Forwarding, don't
141ab2043b8SDevin Teske		# allow the hostname to be changed to prevent the fatal error
142ab2043b8SDevin Teske		# "X11 connection rejected because of wrong authentication."
143ab2043b8SDevin Teske		#
144ab2043b8SDevin Teske		if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then
145fa2e39c8SDevin Teske			f_show_msg "$msg_activate_hostname_x11warning" \
146ab2043b8SDevin Teske			           "$( hostname )" "$hostname"
147ab2043b8SDevin Teske		else
14827ff90aaSDevin Teske			f_yesno "$msg_activate_hostname" \
149ab2043b8SDevin Teske			        "$( hostname )" "$hostname" \
150ab2043b8SDevin Teske			&& hostname "$hostname"
151ab2043b8SDevin Teske		fi
152ab2043b8SDevin Teske	fi
153ab2043b8SDevin Teske
154f677a9e2SDevin Teske	return $DIALOG_OK
155ab2043b8SDevin Teske}
156ab2043b8SDevin Teske
15756961fd7SDevin Teske############################################################ MAIN
15856961fd7SDevin Teske
15956961fd7SDevin Teskef_dprintf "%s: Successfully loaded." networking/hostname.subr
16056961fd7SDevin Teske
161ab2043b8SDevin Teskefi # ! $_NETWORKING_HOSTNAME_SUBR
162