#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# tcpip720 src/tcpip/usr/sbin/rmtcpip/rmtcpip 1.5.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2000,2012 
# 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 
#################################################################################
# @(#)07        1.5.1.1  src/tcpip/usr/sbin/rmtcpip/rmtcpip, tcpras, tcpip720 8/22/12 16:00:43

# CAUTION: After running this script, all the previous network configuration,
# either statically configured by mktcpip, or dynamically configured by dhcpcd,
# will be unconfigured!
# We will only clean up /etc/hosts file and remove /etc/resolv.conf file
# to its freshly installed state.
# Any other network configuration files such as  /etc/dhcpcd.ini will be 
# left untouched.
# Any daemon specified in /etc/rc.tcpip which is commented out by default
# (e.g. dhcpcd), but is running at the time this script is invoked will be stopped 
# after rmtcpip.
# And the daemon will be  commented out in /etc/rc.tcpip file so that 
# it won't start itself again at next system reboot

export PATH=/usr/bin:/usr/sbin:$PATH

################################# usage #########################################
usage(){
	msg=`/usr/bin/dspcat rmtcpip.cat 1 2 2>/dev/null`
	if [ -n "$msg" ]; then
		/usr/bin/dspcat rmtcpip.cat 1 2 >&2
		echo $0
	else
 	cat <<- HERE >&2
   	Usage: $0
HERE
	fi
   	exit 2  
}

################################## stopdaemon ###################################
# daemonstop -
#       stops daemons using src and kill
# args:
#       $1: name of daemon
#
stopdaemon()
{
	DAEMON=$1
	RTNCODE=0
	# get daemon name and process id
        DAEMONNAME=`ps -e | grep "$DAEMON"| awk '"$DAEMON" {print $4}'`
        DAEMONPID=`ps -e | grep "$DAEMON"| awk '"$DAEMON" {print $1}'`

        # try using src first
        if [  -n "$DAEMONNAME" ]; then
		msg=`/usr/bin/dspcat rmtcpip.cat 1 3 2>/dev/null`
		if [ -n "$msg" ]; then
			/usr/bin/dspcat rmtcpip.cat 1 3 >&2
			echo $DAEMONNAME
		else
			echo "Stopping the daemon: $DAEMONNAME"
		fi
                stopsrc -s $DAEMONNAME >/dev/null 2>&1
                RTNCODE=$?
        fi

	# If the daemon isn't started by srcmstr,
        # then try to kill the pid of the daemon
	if [ $RTNCODE -ne 0 ]; then
		if [ -n "$DAEMONPID" ]; then
               		kill $DAEMONPID 2>/dev/null
               		sleep 1
               		kill -9 $DAEMONPID 2>/dev/null
		fi
        fi
	# block the line in /etc/rc.tcpip for this specific daemon
	chrctcp -d $DAEMONNAME >/dev/null 2>&1
}

############################## problem ###########################################
# DESCRIPTION: Issue "problem" message and exit
# INPUT:
#        $RTNCODE
#        $1             Name of command that encountered an error
#
problem(){
   echo "rmtcpip: Problem with $1 command, return code = $RTNCODE" >&2
   exit $RTNCODE
}

##################################################################################

# There shouldn't be any extra parameter for this script.
if [ $# -gt 0 ] ; then
	usage
fi

HOSTID=`hostid`
# First check to see if the machine is in maintainance mode
if [ "$HOSTID" != "0" ]; then  # It's running machine.
	# Check the permisson, either user as "root", or group as "system":
	UID=`id | awk '{print $1}' | cut -c5`
	GID=`id | awk '{print $2}' | cut -c5`
	if [ "$UID" != "0" a "$GID" != "0" ]; then
		msg=`/usr/bin/dspcat rmtcpip.cat 1 1 2>/dev/null`
		if [ -n "$msg" ]; then
			/usr/bin/dspcat rmtcpip.cat 1 1 >&2
		else
			echo "Permission denied"
		fi
		exit 2
	fi

	# Check to see if dhcpcd is running.
	# If so, stop the dchcpcd daemon
	stopdaemon dhcpcd

	# check the /etc/dhcpcd.ini file to get the interface names(e.g. tr0, en0)
	# for dchcpcd configured network interface we need to use
	# "ifconfig <ifname> detach" to clean up after the "rmdev -l <ifname> -d" call.
	INTERFACE=`awk '$1=="interface" {print $2}' /etc/dhcpcd.ini`
	for IFNAME in $INTERFACE
		do
			msg=`/usr/bin/dspcat rmtcpip.cat 1 4 2>/dev/null`
			if [ -n "$msg" ]; then
				/usr/bin/dspcat rmtcpip.cat 1 4 >&2
				echo $IFNAME
			else
				echo "Running: rmdev -d -l $IFNAME"
			fi
			rmdev -l $IFNAME -d > /dev/null 2>&1
			RTNCODE=$?
			if [ $RTNCODE -ne 0 ]; then
				problem rmdev
			fi
	
			msg=`/usr/bin/dspcat rmtcpip.cat 1 5 2>/dev/null`
			if [ -n "$msg" ]; then
				/usr/bin/dspcat rmtcpip.cat 1 5 >&2
				echo "$IFNAME detach"
			else
				echo "Running: ifconfig $IFNAME detach"
			fi
			ifconfig $IFNAME detach > /dev/null 2>&1
			RTNCODE=$?
			if [ $RTNCODE -ne 0 ]; then
				problem ifconfig
			fi
		done
	
	# Next from the "netstat -ni" output, check to see which interfaces
	# are configured. These are usually configured by mktcpip,
	# and the configuration information is stored in an ODM CuAt object.
	INTERFACE=`netstat -ni | awk '$1!="lo0" && $1!="Name"  {print $0}' | \
					awk 'substr($3,1,4)=="link" {print $1}' | sed -e 's/\*$//'`
	for IFNAME in $INTERFACE
		do
			# Don't try to delete devices that don't exist in ODM!
			if [ "x`lsdev -C -l $IFNAME`" != x ]; then
			        msg=`/usr/bin/dspcat rmtcpip.cat 1 4 2>/dev/null`
				if [ -n "$msg" ]; then
					/usr/bin/dspcat rmtcpip.cat 1 4 >&2
					echo $IFNAME
				else
					echo "Running: rmdev -d -l $IFNAME"
				fi
				rmdev -l $IFNAME -d > /dev/null 2>&1
				RTNCODE=$?
				if [ $RTNCODE -ne 0 ]; then
					problem rmdev
				fi
			fi
		done

	# The first time the network is configured via mktcpip, an ODM object
	# in CuAt class called inet0 is created. This object holds information
	# such as the hostname and default route. It contains network 
	# configuration information that is related to a host as opposed to
	# an interface.
	msg=`/usr/bin/dspcat rmtcpip.cat 1 8 2>/dev/null`
	if [ -n "$msg" ]; then
		/usr/bin/dspcat rmtcpip.cat 1 8 >&2
	else
		echo "Deleting inet0  ODM entry"
	fi
	odmdelete -o CuAt -q name="inet0" > /dev/null 2>&1

	msg=`/usr/bin/dspcat rmtcpip.cat 1 9 2>/dev/null`
	if [ -n "$msg" ]; then
		/usr/bin/dspcat rmtcpip.cat 1 9 >&2
	else
		echo "Running cfgmgr to initialize the network interfaces"
	fi
	cfgmgr > /dev/null 2>&1
	RTNCODE=$?
	if [ $RTNCODE -ne 0 ]; then
		problem cfgmr
	fi

else  # machine is booted in maintainance mode
	msg=`/usr/bin/dspcat rmtcpip.cat 1 14 2>/dev/null`
	if [ -n "$msg" ]; then
		/usr/bin/dspcat rmtcpip.cat 1 14 >&2
		echo inet0
	else
		echo "Running: odmdelete -o CuAt -q name=inet0"
	fi
	odmdelete -o CuAt -q name=inet0  > /dev/null 2>&1
	RTNCODE=$?
	if [ $RTNCODE -ne 0 ]; then
		problem odmdelete
	fi

	INTERFACE=`odmget -q attribute=netaddr CuAt | grep name | awk '{print substr($3,2,length($3)-2)}'`
	for IFNAME in $INTERFACE
	do
		msg=`/usr/bin/dspcat rmtcpip.cat 1 14 2>/dev/null`
		if [ -n "$msg" ]; then
			/usr/bin/dspcat rmtcpip.cat 1 14 >&2
			echo $IFNAME
		else
			echo "Running: odmdelete -o CuAt -q name=$IFNAME"
		fi
		odmdelete -o CuAt -q name=$IFNAME  > /dev/null 2>&1
		RTNCODE=$?
		if [ $RTNCODE -ne 0 ]; then
			problem odmdelete
		fi
	done

	# save the change in ODM.
	savebase  > /dev/null 2>&1
fi

# If it's a running machine, stop all the unnecessary daemons which are not 
# started by default on a freshly installed system. 
# Then block the line for that daemon specified in  in /etc/rc.tcpip.
# If it's in maintaninance mode, only block the line for that daemon specified 
# in  in /etc/rc.tcpip.
# It will prevent the specific daemon from being restarted the next time 
# when the machine is rebooted.
for DAEMON in \
		dhcpcd \
		autoconf6 \
		ndpd-host \
		ndpd-router \
		lpd \
		gated \
		named \
		timed \
		xntpd \
		rwhod \
		dhcpsd \
		dhcprd \
		routed \
		mrouted \
		pxed \
		binld ; 
do
	if [ "$HOSTID" != "0" ]; then
		stopdaemon $DAEMON
	else
		# only block line in /etc/rc.tcpip for this specific daemon
	        chrctcp -d $DAEMON >/dev/null 2>&1
	fi
done

msg=`/usr/bin/dspcat rmtcpip.cat 1 10 2>/dev/null`
if [ -n "$msg" ]; then
	/usr/bin/dspcat rmtcpip.cat 1 10 >&2
else
	echo "Saving current /etc/hosts file to /etc/hosts.save file. Then cleaning up /etc/hosts file"
fi
cp /etc/hosts /etc/hosts.save > /dev/null 2>&1
hostent -X  
echo "127.0.0.1               loopback localhost      # loopback (lo0) name/address" >> /etc/hosts 2>/dev/null

msg=`/usr/bin/dspcat rmtcpip.cat 1 11 2>/dev/null`
if [ -n "$msg" ]; then
	/usr/bin/dspcat rmtcpip.cat 1 11 >&2
else
	echo "Saving current /etc/resolv.conf file to /etc/resolv.conf.save. Then removing /etc/resolv.conf file"
fi
mv /etc/resolv.conf  /etc/resolv.conf.save > /dev/null 2>&1

# set the hostid and hostname back to the freshly installed state.
msg=`/usr/bin/dspcat rmtcpip.cat 1 12 2>/dev/null`
if [ -n "$msg" ]; then
	/usr/bin/dspcat rmtcpip.cat 1 12 >&2
else
	echo "Setting host name to: localhost"
fi
hostname localhost > /dev/null 2>&1

msg=`/usr/bin/dspcat rmtcpip.cat 1 13 2>/dev/null`
if [ -n "$msg" ]; then
	/usr/bin/dspcat rmtcpip.cat 1 13 >&2
else
	echo "Setting the hostid to: 127.0.0.1"
fi
hostid 127.0.0.1 > /dev/null 2>&1

# Ensure the file system integrity.
sync  /dev/null 2>&1

msg=`/usr/bin/dspcat rmtcpip.cat 1 16 2>/dev/null`
if [ -n "$msg" ]; then
	/usr/bin/dspcat rmtcpip.cat 1 16 >&2
else
	echo "Please reboot the machine!"
fi
