#!/bin/sh --

# Check hostname configuration as per the sendmail code.
#
# See http://www.sendmail.org/sun-specific/migration.html#FQHN for details.
#
# Copyright (c) 1997-2000 by Sun Microsystems, Inc.
# All Rights Reserved.
#
# @(#)check-hostname.sh	1.6 (Sun) 05/27/00
# ident	"@(#)check-hostname.sh	1.6	00/05/27 SMI"

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
}

# Check the `nismatch $1 hosts` output.  Its output is different from ypmatch
# and the hosts file.  Field 1 is a cname (i.e., alias), field 2 is the
# proper name, field 3 is the IP address and field 4 is comment.

check_nisplus() {
	for hst in `nismatch $1 hosts.org_dir | \
		awk '{for (f=1; f <= 2; 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 or 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)
#       * nisplus (parse nismatch 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"
		;;
	nisplus)
		check_nisplus $myhostname
		nis_domains="$nis_domains nisplus"
		;;
	esac
done

for entry in $nis_domains
do
	case $entry in
	nis)
		check_nis_domain $myhostname "" $first_hosts_entry
		;;
	nisplus)
		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
