hostname.subr revision 260678
1228571Sglebiusif [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1
2228571Sglebius#
3228571Sglebius# Copyright (c) 2006-2013 Devin Teske
4228571Sglebius# All rights reserved.
5228571Sglebius#
6142215Sglebius# Redistribution and use in source and binary forms, with or without
7142215Sglebius# modification, are permitted provided that the following conditions
8142215Sglebius# are met:
9142215Sglebius# 1. Redistributions of source code must retain the above copyright
10142215Sglebius#    notice, this list of conditions and the following disclaimer.
11142215Sglebius# 2. Redistributions in binary form must reproduce the above copyright
12142215Sglebius#    notice, this list of conditions and the following disclaimer in the
13142215Sglebius#    documentation and/or other materials provided with the distribution.
14142215Sglebius#
15142215Sglebius# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16142215Sglebius# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17142215Sglebius# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18142215Sglebius# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19142215Sglebius# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20142215Sglebius# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21142215Sglebius# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22142215Sglebius# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23142215Sglebius# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24142215Sglebius# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25142215Sglebius# SUCH DAMAGE.
26142215Sglebius#
27142215Sglebius# $FreeBSD: stable/10/usr.sbin/bsdconfig/networking/share/hostname.subr 260678 2014-01-15 07:49:17Z dteske $
28142215Sglebius#
29172467Ssilby############################################################ INCLUDES
30172467Ssilby
31172467SsilbyBSDCFG_SHARE="/usr/share/bsdconfig"
32142215Sglebius. $BSDCFG_SHARE/common.subr || exit 1
33142215Sglebiusf_dprintf "%s: loading includes..." networking/hostname.subr
34142215Sglebiusf_include $BSDCFG_SHARE/dialog.subr
35142215Sglebiusf_include $BSDCFG_SHARE/networking/common.subr
36142215Sglebiusf_include $BSDCFG_SHARE/networking/resolv.subr
37142215Sglebiusf_include $BSDCFG_SHARE/sysrc.subr
38228571Sglebius
39228571SglebiusBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
40142215Sglebiusf_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41142215Sglebius
42142215Sglebius############################################################ FUNCTIONS
43142215Sglebius
44142215Sglebius# f_dialog_hnerror $error $hostname
45164033Srwatson#
46142215Sglebius# Display a msgbox with the appropriate error message for an error returned by
47211157Swill# the f_validate_hostname function.
48228571Sglebius#
49228571Sglebiusf_dialog_hnerror()
50142215Sglebius{
51142215Sglebius	local error="$1" fqhn="$2"
52228736Sglebius
53142215Sglebius	[ ${error:-0} -ne 0 ] || return $SUCCESS
54142215Sglebius
55142215Sglebius	case "$error" in
56142215Sglebius	1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn" ;;
57152410Sru	2) f_show_msg \
58228571Sglebius		"$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn" ;;
59142215Sglebius	3) f_show_msg "$msg_hostname_label_is_null" "$fqhn" ;;
60228571Sglebius	63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn" ;;
61142215Sglebius	255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn" ;;
62196019Srwatson	esac
63142215Sglebius}
64221130Sbz
65142215Sglebius# f_dialog_validate_hostname $hostname
66142215Sglebius#
67221130Sbz# Returns zero if the given argument (a fully-qualified hostname) is compliant
68221130Sbz# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
69221130Sbz#
70221130Sbz# RFC 952 - DoD Internet host table specification
71221130Sbz# http://tools.ietf.org/html/rfc952
72142215Sglebius#
73142215Sglebius# RFC 1123 - Requirements for Internet Hosts - Application and Support
74142215Sglebius# http://tools.ietf.org/html/rfc1123
75142215Sglebius#
76142215Sglebius# If the hostname is determined to be invalid, the appropriate error will be
77142215Sglebius# displayed using the f_dialog_hnerror function above.
78142215Sglebius#
79211157Swillf_dialog_validate_hostname()
80228571Sglebius{
81142215Sglebius	local fqhn="$1"
82148387Sume
83142215Sglebius	f_validate_hostname "$fqhn"
84142215Sglebius	local retval=$?
85142215Sglebius
86142215Sglebius	# Produce an appropriate error message if necessary.
87142215Sglebius	[ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn"
88228571Sglebius
89142215Sglebius	return $retval
90142215Sglebius}
91228571Sglebius
92228571Sglebius# f_dialog_input_hostname
93228571Sglebius#
94228571Sglebius# Edits the current hostname.
95221130Sbz#
96228571Sglebiusf_dialog_input_hostname()
97221130Sbz{
98142215Sglebius	local funcname=f_dialog_input_hostname
99228571Sglebius	local hostname="$( f_sysrc_get 'hostname:-$(hostname)' )"
100228571Sglebius	local hostname_orig="$hostname" # for change-tracking
101228571Sglebius
102142215Sglebius	local msg
103228571Sglebius	if [ "$USE_XDIALOG" ]; then
104228571Sglebius		msg="$xmsg_please_enter_fqhn"
105228571Sglebius	else
106228571Sglebius		msg="$msg_please_enter_fqhn"
107228571Sglebius	fi
108228571Sglebius
109228571Sglebius	#
110142215Sglebius	# Loop until the user provides taint-free input.
111228571Sglebius	#
112228571Sglebius	while :; do
113142215Sglebius		f_dialog_input hostname "$msg" "$hostname" \
114228571Sglebius		               "$hline_alnum_punc_tab_enter" || return $?
115142215Sglebius		# Taint-check the user's input
116142215Sglebius		f_dialog_validate_hostname "$hostname" && break
117228571Sglebius	done
118228571Sglebius
119142215Sglebius	#
120142215Sglebius	# Save hostname only if the user changed the hostname.
121228571Sglebius	#
122142215Sglebius	if [ "$hostname" != "$hostname_orig" ]; then
123142215Sglebius		f_dialog_info "$msg_saving_hostname"
124142215Sglebius		f_eval_catch $funcname f_sysrc_set \
125142215Sglebius			'f_sysrc_set hostname "%s"' "$hostname"
126228571Sglebius	fi
127228571Sglebius
128142215Sglebius	#
129142215Sglebius	# Update resolv.conf(5) search/domain directives
130228571Sglebius	#
131228571Sglebius	f_dialog_resolv_conf_update "$hostname"
132228571Sglebius
133228571Sglebius	#
134228571Sglebius	# Only ask to apply setting if the current hostname is different than
135228571Sglebius	# the stored configuration (in rc.conf(5)).
136228571Sglebius	#
137228571Sglebius	if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then
138228571Sglebius		[ ! "$USE_XDIALOG" ] && f_dialog_clear
139228571Sglebius
140228571Sglebius		#
141228571Sglebius		# If connected via ssh(1) and performing X11-Forwarding, don't
142228571Sglebius		# allow the hostname to be changed to prevent the fatal error
143228571Sglebius		# "X11 connection rejected because of wrong authentication."
144228571Sglebius		#
145228571Sglebius		if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then
146228571Sglebius			f_show_msg "$msg_activate_hostname_x11warning" \
147228571Sglebius			           "$( hostname )" "$hostname"
148228571Sglebius		else
149228571Sglebius			f_yesno "$msg_activate_hostname" \
150228571Sglebius			        "$( hostname )" "$hostname" \
151228571Sglebius			&& hostname "$hostname"
152228571Sglebius		fi
153228571Sglebius	fi
154228571Sglebius
155228571Sglebius	return $DIALOG_OK
156228571Sglebius}
157228571Sglebius
158228571Sglebius############################################################ MAIN
159228571Sglebius
160228571Sglebiusf_dprintf "%s: Successfully loaded." networking/hostname.subr
161228571Sglebius
162228571Sglebiusfi # ! $_NETWORKING_HOSTNAME_SUBR
163228571Sglebius