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
|
#!/bin/sh --
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Check hostname configuration as per the sendmail code.
#
# See http://www.sendmail.org/sun-specific/migration.html#FQHN for details.
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
PATH=/bin:/usr/sbin
# If $1 has a ".", accept it and exit.
accept_if_fully_qualified() {
case $1 in
*.*)
echo "Hostname $myhostname OK: fully qualified as $1"
exit 0
;;
esac
}
# Check the `getent hosts $1` output, skipping the 1st entry (IP address).
check_gethostbyname() {
for host in `getent hosts $1 | awk '{for (f=2; f <= NF; f++) print $f}'`
do
accept_if_fully_qualified $host
done
}
# Parse /etc/hosts, looking for $1 as an entry by itself, and try to find
# a long name on the same line. First kill all comments, then check for
# $1 as a word by itself, then take just the first such line, then skip
# its first entry (IP address).
check_hosts_file() {
for entry in `sed -e 's/#.*$//' /etc/hosts | \
awk '/[ ]'$1'([ ]|$)/ \
{for (f=2; f <= NF; f++) print $f; exit}'`
do
accept_if_fully_qualified $entry
done
}
# Parse the output of `nslookup $1`, checking the Name and Aliases.
check_dns() {
for host in `nslookup $1 2>/dev/null | \
awk '$1 == "Name:" || $1 == "Aliases:"{print $2}'`
do
accept_if_fully_qualified $host
done
}
# Check the `ypmatch $1 hosts` output, skipping the 1st entry (IP address).
check_nis() {
for hst in `ypmatch $1 hosts | awk '{for (f=2; f <= NF; f++) print $f}'`
do
accept_if_fully_qualified $hst
done
}
# Recommend how to reconfigure to get $1.$2 as the FQHN.
# $3 is the first entry for hosts in /etc/nsswitch.conf .
suggest_fix_and_exit() {
myhost=$1
suggested_domain=$2
fhe=$3
myipaddr=`getent hosts $myhost | head -1 | awk '{print $1}'`
# aliases: skip the 1st & 2nd entries: IP address & canonical name
set -- '' '' '[ aliases ... ]'
set -- `grep "^$myipaddr[ ]" /etc/hosts 2>/dev/null`
result=$?
shift 2
echo "We recommend \c"
if [ "x$fhe" != "xfiles" ] ; then
echo "listing files first for hosts in /etc/nsswitch.conf"
echo "and then \c"
fi
if [ $result = 0 ] ; then
echo "changing the /etc/hosts entry:\n"
echo "$myipaddr $myhost $*\n"
echo "to:\n"
else
echo "adding the /etc/hosts entry:\n"
fi
echo "$myipaddr $myhost $myhost.$suggested_domain $*"
exit 0
}
# Fall back to the NIS domain, minus the first label. If it is non-null,
# use it but recommend against it. $2 is just informative, indicating whether
# we're checking the NIS domain. $3 is to pass on.
check_nis_domain() {
nisdomain=`domainname`
realdomain=`echo $nisdomain | sed 's/[^.]*\.//'`
if [ "x$realdomain" != "x" ] ; then
echo "Hostname $1 can be fully qualified using NIS$2 domain"
echo " $nisdomain"
echo "resulting in the name"
echo " $1.$realdomain"
echo "but this is bad practice.\n"
suggest_fix_and_exit $1 $realdomain $3
fi
}
# Goal: try to fully qualify `hostname` as sendmail would.
# Algorithm (stop as soon as a name with a dot is found):
# 1. gethostbyname (simulate with getent hosts)
# 2. fall back to individual hosts: methods in nsswitch.conf, using
# only those that are configured, in their configured order
# * files (parse /etc/hosts directly)
# * dns (parse nslookup output)
# * nis (parse ypmatch output)
# 3. fall back to the NIS domain name.
# If none of the above succeed, give up. Recommend:
# a. the domain entry in /etc/resolv.conf, if one exists
# b. "pick.some.domain"
myhostname=`hostname`
check_gethostbyname $myhostname
hosts_line=`sed -n -e 's/^hosts:\([^#]*\).*/\1/p' /etc/nsswitch.conf`
first_hosts_entry=`echo $hosts_line | awk '{print $1}'`
nis_domains=""
for entry in $hosts_line
do
case $entry in
files)
check_hosts_file $myhostname
;;
dns)
check_dns $myhostname
;;
nis)
check_nis $myhostname
nis_domains="$nis_domains nis"
;;
esac
done
for entry in $nis_domains
do
case $entry in
nis)
check_nis_domain $myhostname "" $first_hosts_entry
;;
esac
done
realdomain=`awk '$1 ~ /^domain/ {print $2}' 2>/dev/null < /etc/resolv.conf`
case $realdomain in
*.*)
# OK
;;
*)
realdomain="pick.some.domain"
;;
esac
echo "Hostname $myhostname could not be fully qualified."
suggest_fix_and_exit $myhostname $realdomain $first_hosts_entry
|