hostname.subr revision 244797
1if [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1 2# 3# Copyright (c) 2006-2012 Devin Teske 4# All Rights Reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# 27# $FreeBSD: head/usr.sbin/bsdconfig/networking/share/hostname.subr 244797 2012-12-28 23:27:17Z dteske $ 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_dprintf "%s: loading includes..." networking/hostname.subr 34f_include $BSDCFG_SHARE/sysrc.subr 35f_include $BSDCFG_SHARE/dialog.subr 36f_include $BSDCFG_SHARE/networking/common.subr 37f_include $BSDCFG_SHARE/networking/resolv.subr 38 39BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking" 40f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr 41 42############################################################ FUNCTIONS 43 44# f_validate_hostname $hostname 45# 46# Returns zero if the given argument (a fully-qualified hostname) is compliant 47# with standards set-forth in RFC's 952 and 1123 of the Network Working Group: 48# 49# RFC 952 - DoD Internet host table specification 50# http://tools.ietf.org/html/rfc952 51# 52# RFC 1123 - Requirements for Internet Hosts - Application and Support 53# http://tools.ietf.org/html/rfc1123 54# 55# See http://en.wikipedia.org/wiki/Hostname for a brief overview. 56# 57# The return status for invalid hostnames is one of: 58# 255 Entire hostname exceeds the maximum length of 255 characters. 59# 63 One or more individual labels within the hostname (separated by 60# dots) exceeds the maximum of 63 characters. 61# 1 One or more individual labels within the hostname contains one 62# or more invalid characters. 63# 2 One or more individual labels within the hostname starts or 64# ends with a hyphen (hyphens are allowed, but a label cannot 65# begin or end with a hyphen). 66# 3 One or more individual labels within the hostname are null. 67# 68# If the hostname is determined to be invalid, the appropriate error will be 69# displayed using the f_show_msg function. 70# 71f_validate_hostname() 72{ 73 local fqhn="$1" 74 75 ( # Operate within a sub-shell to protect the parent environment 76 77 # Return error if the hostname exceeds 255 characters 78 [ ${#fqhn} -gt 255 ] && exit 255 79 80 IFS="." # Split on `dot' 81 for label in $fqhn; do 82 83 # Return error if the label exceeds 63 characters 84 [ ${#label} -gt 63 ] && exit 63 85 86 # Return error if the label is null 87 [ "$label" ] || exit 3 88 89 # Return error if label begins/ends with dash 90 case "$label" in 91 -*|*-) exit 2 92 esac 93 94 # Return error if the label contains any invalid chars 95 echo "$label" | grep -q '^[[:alnum:]-]*$' || exit 1 96 97 done 98 ) 99} 100 101# f_dialog_hnerror $error $hostname 102# 103# Display a msgbox with the appropriate error message for an error returned by 104# the f_validate_hostname function. 105# 106f_dialog_hnerror() 107{ 108 local error="$1" fqhn="$2" 109 110 [ ${error:-0} -ne 0 ] || return $SUCCESS 111 112 case "$error" in 113 1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn";; 114 2) f_show_msg "$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn";; 115 3) f_show_msg "$msg_hostname_label_is_null" "$fqhn";; 116 63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn";; 117 255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn";; 118 esac 119} 120 121# f_dialog_validate_hostname $hostname 122# 123# Returns zero if the given argument (a fully-qualified hostname) is compliant 124# with standards set-forth in RFC's 952 and 1123 of the Network Working Group: 125# 126# RFC 952 - DoD Internet host table specification 127# http://tools.ietf.org/html/rfc952 128# 129# RFC 1123 - Requirements for Internet Hosts - Application and Support 130# http://tools.ietf.org/html/rfc1123 131# 132# If the hostname is determined to be invalid, the appropriate error will be 133# displayed using the f_dialog_hnerror function above. 134# 135f_dialog_validate_hostname() 136{ 137 local fqhn="$1" 138 139 f_validate_hostname "$fqhn" 140 local retval=$? 141 142 # Produce an appropriate error message if necessary. 143 [ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn" 144 145 return $retval 146} 147 148# f_dialog_input_hostname 149# 150# Edits the current hostname. 151# 152f_dialog_input_hostname() 153{ 154 local hostname="$( f_sysrc_get 'hostname:-$(hostname)' )" 155 local hostname_orig="$hostname" # for change-tracking 156 157 local msg 158 if [ "$USE_XDIALOG" ]; then 159 msg="$xmsg_please_enter_fqhn" 160 else 161 msg="$msg_please_enter_fqhn" 162 fi 163 164 # 165 # Loop until the user provides taint-free input. 166 # 167 while :; do 168 hostname=$( f_dialog_input "$msg" "$hostname" \ 169 "$hline_alnum_punc_tab_enter" 170 ) || return 171 # Taint-check the user's input 172 f_dialog_validate_hostname "$hostname" && break 173 done 174 175 # 176 # Save hostname only if the user changed the hostname. 177 # 178 if [ "$hostname" != "$hostname_orig" ]; then 179 f_dialog_info "$msg_saving_hostname" 180 f_sysrc_set hostname "$hostname" 181 fi 182 183 # 184 # Update resolv.conf(5) search/domain directives 185 # 186 f_dialog_resolv_conf_update "$hostname" 187 188 # 189 # Only ask to apply setting if the current hostname is different than 190 # the stored configuration (in rc.conf(5)). 191 # 192 if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then 193 [ ! "$USE_XDIALOG" ] && f_dialog_clear 194 195 # 196 # If connected via ssh(1) and performing X11-Forwarding, don't 197 # allow the hostname to be changed to prevent the fatal error 198 # "X11 connection rejected because of wrong authentication." 199 # 200 if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then 201 f_show_msg "$msg_activate_hostname_x11warning" \ 202 "$( hostname )" "$hostname" 203 else 204 f_dialog_yesno "$( 205 printf "$msg_activate_hostname" \ 206 "$( hostname )" "$hostname" \ 207 )" \ 208 && hostname "$hostname" 209 fi 210 fi 211 212 return $SUCCESS 213} 214 215############################################################ MAIN 216 217f_dprintf "%s: Successfully loaded." networking/hostname.subr 218 219fi # ! $_NETWORKING_HOSTNAME_SUBR 220