]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/bsdconfig/networking/share/hostname.subr
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / bsdconfig / networking / share / hostname.subr
1 if [ ! "$_NETWORKING_HOSTNAME_SUBR" ]; then _NETWORKING_HOSTNAME_SUBR=1
2 #
3 # Copyright (c) 2006-2013 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 (INCLUDING, 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$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." networking/hostname.subr
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/networking/common.subr
36 f_include $BSDCFG_SHARE/networking/resolv.subr
37 f_include $BSDCFG_SHARE/sysrc.subr
38
39 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="120.networking"
40 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
41
42 ############################################################ FUNCTIONS
43
44 # f_dialog_hnerror $error $hostname
45 #
46 # Display a msgbox with the appropriate error message for an error returned by
47 # the f_validate_hostname function.
48 #
49 f_dialog_hnerror()
50 {
51         local error="$1" fqhn="$2"
52
53         [ ${error:-0} -ne 0 ] || return $SUCCESS
54
55         case "$error" in
56         1) f_show_msg "$msg_hostname_label_contains_invalid_chars" "$fqhn" ;;
57         2) f_show_msg \
58                 "$msg_hostname_label_starts_or_ends_with_hyphen" "$fqhn" ;;
59         3) f_show_msg "$msg_hostname_label_is_null" "$fqhn" ;;
60         63) f_show_msg "$msg_hostname_label_exceeds_max_length" "$fqhn" ;;
61         255) f_show_msg "$msg_hostname_exceeds_max_length" "$fqhn" ;;
62         esac
63 }
64
65 # f_dialog_validate_hostname $hostname
66 #
67 # Returns zero if the given argument (a fully-qualified hostname) is compliant
68 # with standards set-forth in RFC's 952 and 1123 of the Network Working Group:
69 #
70 # RFC 952 - DoD Internet host table specification
71 # http://tools.ietf.org/html/rfc952
72 #
73 # RFC 1123 - Requirements for Internet Hosts - Application and Support
74 # http://tools.ietf.org/html/rfc1123
75 #
76 # If the hostname is determined to be invalid, the appropriate error will be
77 # displayed using the f_dialog_hnerror function above.
78 #
79 f_dialog_validate_hostname()
80 {
81         local fqhn="$1"
82
83         f_validate_hostname "$fqhn"
84         local retval=$?
85
86         # Produce an appropriate error message if necessary.
87         [ $retval -eq $SUCCESS ] || f_dialog_hnerror $retval "$fqhn"
88
89         return $retval
90 }
91
92 # f_dialog_input_hostname
93 #
94 # Edits the current hostname.
95 #
96 f_dialog_input_hostname()
97 {
98         local hostname="$( f_sysrc_get 'hostname:-$(hostname)' )"
99         local hostname_orig="$hostname" # for change-tracking
100
101         local msg
102         if [ "$USE_XDIALOG" ]; then
103                 msg="$xmsg_please_enter_fqhn"
104         else
105                 msg="$msg_please_enter_fqhn"
106         fi
107
108         #
109         # Loop until the user provides taint-free input.
110         #
111         while :; do
112                 f_dialog_input hostname "$msg" "$hostname" \
113                                "$hline_alnum_punc_tab_enter" || return $?
114                 # Taint-check the user's input
115                 f_dialog_validate_hostname "$hostname" && break
116         done
117
118         #
119         # Save hostname only if the user changed the hostname.
120         #
121         if [ "$hostname" != "$hostname_orig" ]; then
122                 f_dialog_info "$msg_saving_hostname"
123                 f_sysrc_set hostname "$hostname"
124         fi
125
126         #
127         # Update resolv.conf(5) search/domain directives
128         #
129         f_dialog_resolv_conf_update "$hostname"
130
131         #
132         # Only ask to apply setting if the current hostname is different than
133         # the stored configuration (in rc.conf(5)).
134         #
135         if [ "$( hostname )" != "$( f_sysrc_get hostname )" ]; then
136                 [ ! "$USE_XDIALOG" ] && f_dialog_clear
137
138                 #
139                 # If connected via ssh(1) and performing X11-Forwarding, don't
140                 # allow the hostname to be changed to prevent the fatal error
141                 # "X11 connection rejected because of wrong authentication."
142                 #
143                 if [ "$USE_XDIALOG" -a "$SSH_CONNECTION" ]; then
144                         f_show_msg "$msg_activate_hostname_x11warning" \
145                                    "$( hostname )" "$hostname"
146                 else
147                         f_yesno "$msg_activate_hostname" \
148                                 "$( hostname )" "$hostname" \
149                         && hostname "$hostname"
150                 fi
151         fi
152
153         return $DIALOG_OK
154 }
155
156 ############################################################ MAIN
157
158 f_dprintf "%s: Successfully loaded." networking/hostname.subr
159
160 fi # ! $_NETWORKING_HOSTNAME_SUBR