#!/bin/sh
#
#    Copyright (c) 2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   chkdefaultpasswds 
#    Module name: fabos/src/utils/sys
#
#    This script is to check at admin login time if
#    the default passwords have been changed or not. If passwords 
#    for admin, factory or user are still default then admin is prompted
#    for new passwords, and finally the changes are saved to stable storage.
#
PATH=/bin:/fabos/bin:/fabos/sbin:/fabos/libexec:/fabos/cliexec
ECHO=/bin/echo
myexit( ) 
{
   printf "\n"
   printf "Password was not changed. Will prompt again at next login\n"
   printf "until password is changed."
   printf "\n"
   /bin/stty echo
   if [ $paschanged -eq 1 ]; then
	config save /etc/passwd
   fi
   exit 1
}

# To change the default passwords user should have root or admin or factory chassisrole. It is depending on the CHASSIS_ROLE_ID which is the GID for the CHASSIS Role. 
if [ "$VF_ENABLED" != 0 ]; then
	if [ "$CHASSIS_ROLEID" != "0" ] && [ "$CHASSIS_ROLEID" != "600" ] \
		&& [ "$CHASSIS_ROLEID" != "601" ]; then
		exit 1
	fi
fi
currentuser=`$ECHO $SWLOGNAME`
if [ "$currentuser" == "root" ]; then
	USERS="root factory admin user"
else
        USERS="admin user"
fi
INPUT=""
exit_msg_displayed=0
trap myexit  INT
paschanged=0

for username in $USERS;
do {
	if [ "$currentuser" == "admin" ]; then
		warn="Warning: Default password not changed for"
		warnand=" and "
		message=". Please login as 'root' to change it."
		messageand=". Please login as 'root' to change them."
		chkpasswd "root" "admin"
		stat1=$?
		if [ $stat1 -eq 1 ]; then
			warn1=" 'root'"
		fi
		chkpasswd "factory" "admin"
		stat2=$?
		if [ $stat2 -eq 1 ]; then
			warn2=" 'factory'"
		fi
		if [ $stat1 -eq 1 ] && [ $stat2 -eq 1 ]; then
			$ECHO $warn$warn1$warnand$warn2$messageand
		elif [ $stat1 -eq 1 ] || [ $stat2 -eq 1 ]; then
			$ECHO $warn$warn1$warn2$message
		fi
	fi
chkpasswd $username
stat=$?
if [ $stat -eq 1 ]; then
	if [ $exit_msg_displayed -eq 0 ]; then
		$ECHO "Use Control-C to exit or press 'Enter' key to proceed."
		read INPUT
		exit_msg_displayed=1

		# Defect 35041 Requires warning before chaninge root or factory
		if [ "$username" = "root" -o "$username" = "factory" ]; then
			$ECHO "Warning:  Access to  the Root  and Factory accounts may be required  for"
			$ECHO "proper  support  of  the switch.  Please  ensure  the Root  and  Factory"
			$ECHO "passwords are  documented in a secure location.  Recovery of a lost Root"
			$ECHO "or Factory password will result in fabric downtime."
			$ECHO 			
		fi
	fi
		$ECHO "for user - $username"
		/bin/passwd $username
		stat=$?
		if [ $stat -eq 0 ]; then
			paschanged=1
			passwd_notify -s $FABOS_SWITCHNO $username
		fi
fi
}
done

trap  "" INT
if [ $paschanged -eq 1 ]; then
$ECHO "Saving passwords to stable storage."
	/fabos/cliexec/config save /etc/passwd
	configsave_result=$?
	if [ $configsave_result -ne 0 ]; then
		$ECHO "Failed to update passwords in stable storage"
	fi
	/fabos/cliexec/config save /etc/shadow
configsave_result=$?
if [ $configsave_result -ne 0 ]; then
$ECHO "Failed to update passwords in stable storage"
else 
$ECHO "Passwords saved to stable storage successfully"
fi
fi
