freebsd-nq/usr.sbin/bsdconfig/networking/include/hostname.subr
Devin Teske 641a6cfb86 Import bsdconfig(8) as a replacement for the post-install abilities of
deprecated sysinstall(8). NOTE: WITH_BSDCONFIG is currently required.

Submitted by:	Devin Teske (dteske), Ron McDowell <rcm@fuzzwad.org>
Reviewed by:	Ron McDowell <rcm@fuzzwad.org>
Approved by:	Ed Maste (emaste)
2012-07-14 03:16:57 +00:00

212 lines
6.4 KiB
Plaintext

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_LIBE="/usr/libexec/bsdconfig"
. $BSDCFG_LIBE/include/common.subr || exit 1
f_include $BSDCFG_LIBE/include/sysrc.subr
f_include $BSDCFG_LIBE/include/dialog.subr
APP_DIR="120.networking"
f_include $BSDCFG_LIBE/$APP_DIR/include/common.subr
f_include $BSDCFG_LIBE/$APP_DIR/include/resolv.subr
f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
############################################################ FUNCTIONS
# 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
#
# 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_dialog_msgbox function.
#
f_dialog_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
)
#
# Produce an appropriate error message if necessary.
#
local retval=$?
case $retval in
1) f_dialog_msgbox "$( printf \
"$msg_hostname_label_contains_invalid_chars" "$fqhn" )";;
2) f_dialog_msgbox "$( printf \
"$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn" )";;
3) f_dialog_msgbox "$( printf \
"$msg_hostname_label_is_null" "$fqhn" )";;
63) f_dialog_msgbox "$( printf \
"$msg_hostname_label_exceeds_max_length" "$fqhn" )";;
255) f_dialog_msgbox "$( printf \
"$msg_hostname_exceeds_max_length" "$fqhn" )";;
esac
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
local hline="$hline_alnum_punc_tab_enter"
#
# Loop until the user provides taint-free input.
#
local size height width
while :; do
size=$( f_dialog_inputbox_size \
"$DIALOG_TITLE" \
"$DIALOG_BACKTITLE" \
"$msg" \
"$hostname" \
"$hline" )
eval $DIALOG \
--title \"\$DIALOG_TITLE\" \
--backtitle \"\$DIALOG_BACKTITLE\" \
--hline \"\$hline\" \
--ok-label \"\$msg_ok\" \
--cancel-label \"\$msg_cancel\" \
--inputbox \"\$msg\" $size \
\"\$hostname\" \
2> "$DIALOG_TMPDIR/dialog.inputbox.$$"
local retval=$?
hostname=$( f_dialog_inputstr )
[ $retval -eq $SUCCESS ] || return $retval
# 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" ] && 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_dialog_msgbox "$(
printf "$msg_activate_hostname_x11warning" \
"$( hostname )" "$hostname"
)"
else
f_dialog_yesno "$(
printf "$msg_activate_hostname" \
"$( hostname )" "$hostname" \
)" \
&& hostname "$hostname"
fi
fi
return $SUCCESS
}
fi # ! $_NETWORKING_HOSTNAME_SUBR