#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# tcpip720 src/tcpip/usr/sbin/dhcpremove/dhcpremove.S 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1996,2003 
# 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 
# @(#)73	1.6  src/tcpip/usr/sbin/dhcpremove/dhcpremove.S, dhcp, tcpip720 8/26/03 15:19:33
#
# COMPONENT_NAME: (DHCP)
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 1996
# 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 3 arguments with possibly more.
#
# dhcpremove <ipaddr> <BOTH|A|PTR|NONE> <NONIM|NIM>
#
# 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 released.  The reason that this second 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

ipaddr=$1
whichrecord=$2
shouldnim=$3

primaryname=`/usr/sbin/namerslv -s -I | awk '{print $2}'`
machname=`host $ipaddr 2>&1 | awk '{ print $1 }'`
#actualipaddr=`host $machname 2>&1 | awk '{ print $3 }'`
hostname=`echo $machname | awk -F. '{ print $1 }'`

if [ $machname = "host:" ]
then
	echo "PTR record does not exist, no records could be removed."
	whichrecord=NONE
fi

#Should not have to worry if the ipaddr matches because rfc2136(2.5.1-4) states that
#the resource record must match or the update is ignored by the server
#if [ "$actualipaddr" != "$ipaddr" ]
#then
#	whichrecord=NONE
#fi

leasetime=1

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


if [ "NIM" = "$shouldnim" ]
then
	CHANGED=0	
	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
			mv $tmp /etc/nim.dhcp.addresses
			CHANGED=1
		fi
	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