1ab2043b8SDevin Teskeif [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1 2ab2043b8SDevin Teske# 3ab2043b8SDevin Teske# Copyright (c) 2006-2012 Devin Teske 4ab2043b8SDevin 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 33ab2043b8SDevin Teskef_include $BSDCFG_SHARE/sysrc.subr 34ab2043b8SDevin Teskef_include $BSDCFG_SHARE/dialog.subr 35ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/common.subr 36ab2043b8SDevin Teskef_include $BSDCFG_SHARE/networking/resolv.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 43*35a157a0SDevin Teske# f_validate_hostname $hostname 44ab2043b8SDevin Teske# 45ab2043b8SDevin Teske# Returns zero if the given argument (a fully-qualified hostname) is compliant 46ab2043b8SDevin Teske# with standards set-forth in RFC's 952 and 1123 of the Network Working Group: 47ab2043b8SDevin Teske# 48ab2043b8SDevin Teske# RFC 952 - DoD Internet host table specification 49ab2043b8SDevin Teske# http://tools.ietf.org/html/rfc952 50ab2043b8SDevin Teske# 51ab2043b8SDevin Teske# RFC 1123 - Requirements for Internet Hosts - Application and Support 52ab2043b8SDevin Teske# http://tools.ietf.org/html/rfc1123 53ab2043b8SDevin Teske# 54ab2043b8SDevin Teske# See http://en.wikipedia.org/wiki/Hostname for a brief overview. 55ab2043b8SDevin Teske# 56ab2043b8SDevin Teske# The return status for invalid hostnames is one of: 57ab2043b8SDevin Teske# 255 Entire hostname exceeds the maximum length of 255 characters. 58ab2043b8SDevin Teske# 63 One or more individual labels within the hostname (separated by 59ab2043b8SDevin Teske# dots) exceeds the maximum of 63 characters. 60ab2043b8SDevin Teske# 1 One or more individual labels within the hostname contains one 61ab2043b8SDevin Teske# or more invalid characters. 62ab2043b8SDevin Teske# 2 One or more individual labels within the hostname starts or 63ab2043b8SDevin Teske# ends with a hyphen (hyphens are allowed, but a label cannot 64ab2043b8SDevin Teske# begin or end with a hyphen). 65ab2043b8SDevin Teske# 3 One or more individual labels within the hostname are null. 66ab2043b8SDevin Teske# 67ab2043b8SDevin Teske# If the hostname is determined to be invalid, the appropriate error will be 68*35a157a0SDevin Teske# displayed using the f_show_msg function. 69ab2043b8SDevin Teske# 70*35a157a0SDevin Teskef_validate_hostname() 71ab2043b8SDevin Teske{ 72ab2043b8SDevin Teske local fqhn="$1" 73ab2043b8SDevin Teske 74ab2043b8SDevin Teske ( # Operate within a sub-shell to protect the parent environment 75ab2043b8SDevin Teske 76ab2043b8SDevin Teske # Return error if the hostname exceeds 255 characters 77ab2043b8SDevin Teske [ ${#fqhn} -gt 255 ] && exit 255 78ab2043b8SDevin Teske 79ab2043b8SDevin Teske IFS="." # Split on `dot' 80ab2043b8SDevin Teske for label in $fqhn; do 81ab2043b8SDevin Teske 82ab2043b8SDevin Teske # Return error if the label exceeds 63 characters 83ab2043b8SDevin Teske [ ${#label} -gt 63 ] && exit 63 84ab2043b8SDevin Teske 85ab2043b8SDevin Teske # Return error if the label is null 86ab2043b8SDevin Teske [ "$label" ] || exit 3 87ab2043b8SDevin Teske 88ab2043b8SDevin Teske # Return error if label begins/ends with dash 89ab2043b8SDevin Teske case "$label" in 90ab2043b8SDevin Teske -*|*-) exit 2 91ab2043b8SDevin Teske esac 92ab2043b8SDevin Teske 93ab2043b8SDevin Teske # Return error if the label contains any invalid chars 94ab2043b8SDevin Teske echo "$label" | grep -q '^[[:alnum:]-]*$' || exit 1 95ab2043b8SDevin Teske 96ab2043b8SDevin Teske done 97ab2043b8SDevin Teske ) 98*35a157a0SDevin Teske} 99ab2043b8SDevin Teske 100*35a157a0SDevin Teske# f_dialog_hnerror $error $hostname 101ab2043b8SDevin Teske# 102*35a157a0SDevin Teske# Display a msgbox with the appropriate error message for an error returned by 103*35a157a0SDevin Teske# the f_validate_hostname function. 104ab2043b8SDevin Teske# 105*35a157a0SDevin Teskef_dialog_hnerror() 106*35a157a0SDevin Teske{ 107*35a157a0SDevin Teske local error="$1" fqhn="$2" 108*35a157a0SDevin Teske 109*35a157a0SDevin Teske [ ${error:-0} -ne 0 ] || return $SUCCESS 110*35a157a0SDevin Teske 111*35a157a0SDevin Teske case "$error" in 112fa2e39c8SDevin Teske 1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn";; 113fa2e39c8SDevin Teske 2) f_show_msg "$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn";; 114fa2e39c8SDevin Teske 3) f_show_msg "$msg_hostname_label_is_null" "$fqhn";; 115fa2e39c8SDevin Teske 63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn";; 116fa2e39c8SDevin Teske 255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn";; 117ab2043b8SDevin Teske esac 118*35a157a0SDevin Teske} 119*35a157a0SDevin Teske 120*35a157a0SDevin Teske# f_dialog_validate_hostname $hostname 121*35a157a0SDevin Teske# 122*35a157a0SDevin Teske# Returns zero if the given argument (a fully-qualified hostname) is compliant 123*35a157a0SDevin Teske# with standards set-forth in RFC's 952 and 1123 of the Network Working Group: 124*35a157a0SDevin Teske# 125*35a157a0SDevin Teske# RFC 952 - DoD Internet host table specification 126*35a157a0SDevin Teske# http://tools.ietf.org/html/rfc952 127*35a157a0SDevin Teske# 128*35a157a0SDevin Teske# RFC 1123 - Requirements for Internet Hosts - Application and Support 129*35a157a0SDevin Teske# http://tools.ietf.org/html/rfc1123 130*35a157a0SDevin Teske# 131*35a157a0SDevin Teske# If the hostname is determined to be invalid, the appropriate error will be 132*35a157a0SDevin Teske# displayed using the f_dialog_hnerror function above. 133*35a157a0SDevin Teske# 134*35a157a0SDevin Teskef_dialog_validate_hostname() 135*35a157a0SDevin Teske{ 136*35a157a0SDevin Teske local fqhn="$1" 137*35a157a0SDevin Teske 138*35a157a0SDevin Teske f_validate_hostname "$fqhn" 139*35a157a0SDevin Teske local retval=$? 140*35a157a0SDevin Teske 141*35a157a0SDevin Teske # Produce an appropriate error message if necessary. 142*35a157a0SDevin Teske [ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn" 143ab2043b8SDevin Teske 144ab2043b8SDevin Teske return $retval 145ab2043b8SDevin Teske} 146ab2043b8SDevin Teske 147ab2043b8SDevin Teske# f_dialog_input_hostname 148ab2043b8SDevin Teske# 149ab2043b8SDevin Teske# Edits the current hostname. 150ab2043b8SDevin Teske# 151ab2043b8SDevin Teskef_dialog_input_hostname() 152ab2043b8SDevin Teske{ 153ab2043b8SDevin Teske local hostname="$( f_sysrc_get 'hostname:-$(hostname)' )" 154ab2043b8SDevin Teske local hostname_orig="$hostname" # for change-tracking 155ab2043b8SDevin Teske 156ab2043b8SDevin Teske local msg 157ab2043b8SDevin Teske if [ "$USE_XDIALOG" ]; then 158ab2043b8SDevin Teske msg="$xmsg_please_enter_fqhn" 159ab2043b8SDevin Teske else 160ab2043b8SDevin Teske msg="$msg_please_enter_fqhn" 161ab2043b8SDevin Teske fi 162ab2043b8SDevin Teske 163ab2043b8SDevin Teske # 164ab2043b8SDevin Teske # Loop until the user provides taint-free input. 165ab2043b8SDevin Teske # 166ab2043b8SDevin Teske while :; do 167298cf604SDevin Teske hostname=$( f_dialog_input "$msg" "$hostname" \ 168298cf604SDevin Teske "$hline_alnum_punc_tab_enter" 169298cf604SDevin Teske ) || return 170ab2043b8SDevin Teske # Taint-check the user's input 171ab2043b8SDevin Teske f_dialog_validate_hostname "$hostname" && break 172ab2043b8SDevin Teske done 173ab2043b8SDevin Teske 174ab2043b8SDevin Teske # 175ab2043b8SDevin Teske # Save hostname only if the user changed the hostname. 176ab2043b8SDevin Teske # 177ab2043b8SDevin Teske if [ "$hostname" != "$hostname_orig" ]; then 178ab2043b8SDevin Teske f_dialog_info "$msg_saving_hostname" 179ab2043b8SDevin Teske f_sysrc_set hostname "$hostname" 180ab2043b8SDevin Teske fi 181ab2043b8SDevin Teske 182ab2043b8SDevin Teske # 183ab2043b8SDevin Teske # Update resolv.conf(5) search/domain directives 184ab2043b8SDevin Teske # 185ab2043b8SDevin Teske f_dialog_resolv_conf_update "$hostname" 186ab2043b8SDevin Teske 187ab2043b8SDevin Teske # 188ab2043b8SDevin Teske # Only ask to apply setting if the current hostname is different than 189ab2043b8SDevin Teske # the stored configuration (in rc.conf(5)). 190ab2043b8SDevin Teske # 191ab2043b8SDevin Teske if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then 192ab2043b8SDevin Teske [ ! "$USE_XDIALOG" ] && dialog_clear 193ab2043b8SDevin Teske 194ab2043b8SDevin Teske # 195ab2043b8SDevin Teske # If connected via ssh(1) and performing X11-Forwarding, don't 196ab2043b8SDevin Teske # allow the hostname to be changed to prevent the fatal error 197ab2043b8SDevin Teske # "X11 connection rejected because of wrong authentication." 198ab2043b8SDevin Teske # 199ab2043b8SDevin Teske if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then 200fa2e39c8SDevin Teske f_show_msg "$msg_activate_hostname_x11warning" \ 201ab2043b8SDevin Teske "$( hostname )" "$hostname" 202ab2043b8SDevin Teske else 203ab2043b8SDevin Teske f_dialog_yesno "$( 204ab2043b8SDevin Teske printf "$msg_activate_hostname" \ 205ab2043b8SDevin Teske "$( hostname )" "$hostname" \ 206ab2043b8SDevin Teske )" \ 207ab2043b8SDevin Teske && hostname "$hostname" 208ab2043b8SDevin Teske fi 209ab2043b8SDevin Teske fi 210ab2043b8SDevin Teske 211ab2043b8SDevin Teske return $SUCCESS 212ab2043b8SDevin Teske} 213ab2043b8SDevin Teske 214ab2043b8SDevin Teskefi # ! $_NETWORKING_HOSTNAME_SUBR 215