#!/bin/ksh
#*******************************************************************************
#
#	Name:			%M%
#	SCCS Id:		%W%	%G%
#
#	Description:	This file is a script for system set up, phase 1.  Phase 1
#					is immediately after Solaris has been installed, but BEFORE
#					the initial reboot.
#
#					This has to be a ksh script, as sh doesn't handle piped
#					reads how we need them to.
#
#
#			Copyright © 2000 - 2002 by Rich Teer.  All rights reserved.
#
#*******************************************************************************


OS_REL=`uname -r`
HOSTNAME=`uname -n`
DEFAULT_DOMAIN="richteer.ca"

echo "Starting system hardening for $HOSTNAME, Phase 1"

umask 022

# echo "Enter domain name [$DEFAULT_DOMAIN]: \c"
# read LINE
# DOMAIN=${LINE:-$DEFAULT_DOMAIN}
# echo $DOMAIN > /a/etc/defaultdomain

IP_ADDR=`grep "\<$HOSTNAME\>" /a/etc/hosts | awk '{ print $1 }'`
BROADCAST=`ifconfig -a | grep $IP_ADDR | awk '{ print $6 }'`
echo "$BROADCAST" | sed 's/\./ /g' | read BYTE1 BYTE2 BYTE3 BYTE4
BYTE4=`expr $BYTE4 - 1`
DEFAULT_ROUTER="$BYTE1.$BYTE2.$BYTE3.$BYTE4"
echo "Enter default router [$DEFAULT_ROUTER]: \c"
read LINE
ROUTER=${LINE:-$DEFAULT_ROUTER}

case "$OS_REL" in
	'5.5' | '5.5.1' | '5.6' | '5.7')
		echo "Setting up /etc/rc?.d/S00umask.sh...  \c"
		echo "umask 022" > /a/etc/init.d/umask.sh
		for i in /a/etc/rc?.d; do
			ln -s /etc/init.d/umask.sh $i/S00umask.sh
		done
		echo "Done."
		;;

	*)
		;;
esac

case "$OS_REL" in
	'5.5' | '5.5.1' | '5.6')
		echo "Enabling savecore...  \c"
ed /a/etc/init.d/sysetup << EOF > /dev/null
\$-5,\$s/^#//
w
q
EOF
		echo "Done."
		;;

	*)
		;;
esac

echo "Setting up default router...  \c"
echo $ROUTER > /a/etc/defaultrouter
echo "Done."

echo "Building root's new home directory:"
echo "    Making /root"
mkdir -m 0700 /a/root
chown root:root /a/root
FLAG=false
echo "    Copying files: \c"
cat phase1_files/INDEX | while read SRC DEST OWNER GROUP PERMS; do
	if [ $FLAG = "false" ]; then
		echo "$SRC\c"
		FLAG=true
	else
		echo ", $SRC\c"
	fi
	cp phase1_files/$SRC $DEST
	chown $OWNER:$GROUP $DEST
	chmod $PERMS $DEST
done
echo "."
echo "    Updating /etc/passwd"
ed /a/etc/passwd << EOF > /dev/null
1s/\//\/root/
w
q
EOF
echo "Done."

echo "Changing passwd encryption algorithm to Blowfish... \c"
ed /a/etc/security/policy.conf << EOF
1,\$s/CRYPT_DEFAULT=__unix__/CRYPT_DEFAULT=2a/p
w
q
EOF
echo "Done."

sync

echo "\n"
echo "Phase 1 of the system hardening for $HOSTNAME is complete."
echo ""
echo "Reboot this machine now, install the latest Recommended"
echo "and Security patches from SunSolve, and then proceed"
echo "with Phase 2 of the system hardening."
