summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsdconfig/networking/share/hostname.subr
blob: b2760597e2c5a24cf2b065099b2f6b0fd589a838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
if [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1
#
# Copyright (c) 2006-2012 Devin Teske
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES

BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." networking/hostname.subr
f_include $BSDCFG_SHARE/sysrc.subr
f_include $BSDCFG_SHARE/dialog.subr
f_include $BSDCFG_SHARE/networking/common.subr
f_include $BSDCFG_SHARE/networking/resolv.subr

BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr

############################################################ FUNCTIONS

# f_validate_hostname $hostname
#
# Returns zero if the given argument (a fully-qualified hostname) is compliant
# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
#
# RFC 952 - DoD Internet host table specification
# http://tools.ietf.org/html/rfc952
#
# RFC 1123 - Requirements for Internet Hosts - Application and Support
# http://tools.ietf.org/html/rfc1123
#
# See http://en.wikipedia.org/wiki/Hostname for a brief overview.
#
# The return status for invalid hostnames is one of:
# 	255	Entire hostname exceeds the maximum length of 255 characters.
# 	 63	One or more individual labels within the hostname (separated by
# 	   	dots) exceeds the maximum of 63 characters.
# 	  1	One or more individual labels within the hostname contains one
# 	   	or more invalid characters.
# 	  2	One or more individual labels within the hostname starts or
# 	   	ends with a hyphen (hyphens are allowed, but a label cannot
# 	   	begin or end with a hyphen).
# 	  3	One or more individual labels within the hostname are null.
#
# If the hostname is determined to be invalid, the appropriate error will be
# displayed using the f_show_msg function.
#
f_validate_hostname()
{
	local fqhn="$1"

	( # Operate within a sub-shell to protect the parent environment

		# Return error if the hostname exceeds 255 characters
		[ ${#fqhn} -gt 255 ] && exit 255

		IFS="." # Split on `dot'
		for label in $fqhn; do

			# Return error if the label exceeds 63 characters
			[ ${#label} -gt 63 ] && exit 63

			# Return error if the label is null
			[ "$label" ] || exit 3

			# Return error if label begins/ends with dash
			case "$label" in
			-*|*-) exit 2
			esac

			# Return error if the label contains any invalid chars
			echo "$label" | grep -q '^[[:alnum:]-]*$' || exit 1

		done
	)
}

# f_dialog_hnerror $error $hostname
#
# Display a msgbox with the appropriate error message for an error returned by
# the f_validate_hostname function.
#
f_dialog_hnerror()
{
	local error="$1" fqhn="$2"

	[ ${error:-0} -ne 0 ] || return $SUCCESS

	case "$error" in
	1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn";;
	2) f_show_msg "$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn";;
	3) f_show_msg "$msg_hostname_label_is_null" "$fqhn";;
	63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn";;
	255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn";;
	esac
}

# f_dialog_validate_hostname $hostname
#
# Returns zero if the given argument (a fully-qualified hostname) is compliant
# with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
#
# RFC 952 - DoD Internet host table specification
# http://tools.ietf.org/html/rfc952
#
# RFC 1123 - Requirements for Internet Hosts - Application and Support
# http://tools.ietf.org/html/rfc1123
#
# If the hostname is determined to be invalid, the appropriate error will be
# displayed using the f_dialog_hnerror function above.
#
f_dialog_validate_hostname()
{
	local fqhn="$1"

	f_validate_hostname "$fqhn"
	local retval=$?

	# Produce an appropriate error message if necessary.
	[ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn"

	return $retval
}

# f_dialog_input_hostname
#
# Edits the current hostname.
#
f_dialog_input_hostname()
{
	local hostname="$( f_sysrc_get 'hostname:-$(hostname)' )"
	local hostname_orig="$hostname" # for change-tracking

	local msg
	if [ "$USE_XDIALOG" ]; then
		msg="$xmsg_please_enter_fqhn"
	else
		msg="$msg_please_enter_fqhn"
	fi

	#
	# Loop until the user provides taint-free input.
	#
	while :; do
		hostname=$( f_dialog_input "$msg" "$hostname" \
		                           "$hline_alnum_punc_tab_enter"
		          ) || return
		# Taint-check the user's input
		f_dialog_validate_hostname "$hostname" && break
	done

	#
	# Save hostname only if the user changed the hostname.
	#
	if [ "$hostname" != "$hostname_orig" ]; then
		f_dialog_info "$msg_saving_hostname"
		f_sysrc_set hostname "$hostname"
	fi

	#
	# Update resolv.conf(5) search/domain directives
	#
	f_dialog_resolv_conf_update "$hostname"

	#
	# Only ask to apply setting if the current hostname is different than
	# the stored configuration (in rc.conf(5)).
	#
	if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then
		[ ! "$USE_XDIALOG" ] && f_dialog_clear

		#
		# If connected via ssh(1) and performing X11-Forwarding, don't
		# allow the hostname to be changed to prevent the fatal error
		# "X11 connection rejected because of wrong authentication."
		#
		if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then
			f_show_msg "$msg_activate_hostname_x11warning" \
			           "$( hostname )" "$hostname"
		else
			f_yesno "$msg_activate_hostname" \
			        "$( hostname )" "$hostname" \
			&& hostname "$hostname"
		fi
	fi

	return $SUCCESS
}

############################################################ MAIN

f_dprintf "%s: Successfully loaded." networking/hostname.subr

fi # ! $_NETWORKING_HOSTNAME_SUBR
OpenPOWER on IntegriCloud