#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# tcpip720 src/tcpip/usr/sbin/dhcpaction/dhcpaction.S 1.8 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1995,2006 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)82	1.8  src/tcpip/usr/sbin/dhcpaction/dhcpaction.S, dhcp, tcpip720 11/7/06 00:04:38
#
# COMPONENT_NAME: (DHCP)
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 1995
# All Rights Reserved
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# Licensed Material - Property of IBM
#
#
# This program takes atleast 5 arguments with possibly more.
#
# dhcpaction <hostname> <domainname> <ipaddr> <leasetime> <BOTH|A|PTR|NONE>
#
# The user should feel free to do more things in this file as the need
# arises.  This is called by dhcpsd and dhcpcd everytime an address/hostname
# pair is updated.  The reason that this fifth parameter is BOTH|A|PTR|NONE
# is to allow for a policy statement.  The dynamic dns update capability
# allows for either the server or the client to update the A and PTR record.
# The default is that the client assigns the A record and the server does
# the PTR record, but this can be changed.
#

umask 077
unset PATH
export PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb

typeset TMPDIR=${TMPDIR:-/tmp}/${0##*/}.$$
typeset rc tmp

[[ ! -w ${TMPDIR%/*} ]] && {
        print -u2 "${0##*/}: could not write to directory \"${TMPDIR%/*}\""
        exit 1
}

while ! mkdir -m 0700 $TMPDIR 2>/dev/null ; do
        TMPDIR=${TMPDIR%.*}.$(( $$ + $RANDOM ))
done
function cleanup {
        cd /
        /bin/rm -rf $TMPDIR 2>/dev/null
}
trap 'rc=$?; trap "" EXIT; cleanup; exit $rc' INT TERM QUIT HUP
trap 'cleanup; exit' EXIT

tmp=$TMPDIR/tmpfile1

hostname=$1
domainname=$2
ipaddr=$3
leasetime=$4
whichrecord=$5
shouldnim=$6
primaryname=`/usr/sbin/namerslv -s -I | awk '{print $2}'`

if [ -n "$domainname" ]
then
	machname="$hostname.$domainname"
else
	machname="$hostname"
fi
MAXIMUM_TTL=2147483647
if [ "$leasetime" -gt "$MAXIMUM_TTL" ]
then
        leasetime="$MAXIMUM_TTL"
fi

if [ "$whichrecord" = "BOTH" ] || [ "$whichrecord" = "PTR" ]
then
	/usr/sbin/nsupdate -q -s"r;d;ptr;*;a;ptr;$machname.;s;$leasetime;3110400;x;q" -r $ipaddr -p$primaryname
	if [ $? -eq 1 ]
	then
		msg=`/usr/bin/dspmsg -s 1 /usr/lib/nls/msg/$LANG/nsupdate.cat 34 2> /dev/null`
		if [ $? -eq 0 ]
		then
			echo $msg " $machname:$ipaddr"
		else
			echo "nsupdate: Failed to add the ptr record for $machname:$ipaddr"
		fi
	fi
fi

if [ "$whichrecord" = "BOTH" ] || [ "$whichrecord" = "A" ]
then
	/usr/sbin/nsupdate -q -s"r;d;a;*;a;a;$ipaddr;s;$leasetime;3110400;x;q" -h $machname -p $primaryname
	if [ $? -eq 1 ]
	then
		msg=`/usr/bin/dspmsg -s 1 /usr/lib/nls/msg/$LANG/nsupdate.cat 35 2> /dev/null`
		if [ $? -eq 0 ]
		then
			echo $msg " $machname:$ipaddr"
		else
			echo "nsupdate: Failed to add the a record for $machname:$ipaddr"
		fi
	fi
fi


oldhostname=`hostname`
if [ "$oldhostname" = "localhost" ] || [ "$oldhostname" = "" ] || [ "$oldhostname" = "$hostname" ]
then
	/bin/hostname $machname
	/usr/sbin/hostid $ipaddr
	/bin/uname -S`hostname|sed 's/\..*$//'`
fi


if [ "NIM" = "$shouldnim" ]
then
	if [ -f /etc/nim.dhcp.addresses ]
	then
		grep $hostname /etc/nim.dhcp.addresses | grep -q "$ipaddr:"
		if [ $? -eq 0 ]
		then
			CHANGED=0	
		else
			grep -v $hostname /etc/nim.dhcp.addresses > $tmp
			echo "$hostname:$ipaddr:" >> $tmp
			mv $tmp /etc/nim.dhcp.addresses
			CHANGED=1
		fi
	else
		CHANGED=1
		echo "$hostname:$ipaddr:" > /etc/nim.dhcp.addresses	
	fi

	if [ $CHANGED -eq 1 ]
	then
		lsnim -a if1 -t standalone | grep -q $hostname 
		if [ $? -eq 0 ]
		then
			object=0
			for i in `lsnim -t standalone | awk '{ print $1 }'`
			do
				lsnim -a if1 -t standalone $i | grep -q $hostname
				if [ $? -eq 0 ]
				then
					object=$i
				fi
			done

			if [ "$object" = 0 ]
			then
				exit 1
			fi

			OLDLANG=$LANG
			export LANG=C
			lsnim -a Cstate $object | grep -q "BOS installation has been enabled"
			rc=$?
			export LANG=$OLDLANG
			if [ $rc -eq 0 ]
			then
				nim -Fo reset $object >/dev/null 2>&1
				if [ $? -eq 0 ]
				then
					msg=`/usr/bin/dspmsg -s 1 /usr/lib/nls/msg/$LANG/nsupdate.cat 36 2> /dev/null`
					if [ $? -ne 0 ]
					then
						msg="Because of an address change, DHCP has reset the following object: "
					fi

					echo $msg $object >> /dev/console
					nim -o change -a info="$msg $object" $object
				fi
			fi

			lsnim -a serves $object | awk '(NR > 1) { print $3 }' |\
			while read resource
			do
				if [ `lsnim -l $resource | grep type | awk '{print $3}'` = spot ]
				then
					lsnim -a spot | grep -q $resource
					if [ $? -eq 0 ]
					then
						for client in `lsnim -t standalone | awk '{ print $1 }'`
						do
							lsnim -a spot $client | grep -q $resource
							if [ $? -eq 0 ]
							then
								OLDLANG=$LANG
								export LANG=C
								lsnim -a Cstate $client | grep -q "BOS installation has been enabled"
								rc=$?
								export LANG=$OLDLANG
								if [ $rc -eq 0 ]
								then
									nim -Fo reset $client 2>&1 >/dev/null
									if [ $? -eq 0 ]
									then
										msg=`/usr/bin/dspmsg -s 1 /usr/lib/nls/msg/$LANG/nsupdate.cat 36 2> /dev/null`
										if [ $? -ne 0 ]
										then
											msg="Because of an address change, DHCP has reset the following object: "
										fi

										echo $msg $client >> /dev/console
										nim -o change -a info="$msg $client" $client
									fi
								fi
							fi
						done
					fi
				fi
			done	
		fi
	fi
fi