#!/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
myexit( ) 
{
   printf "\n"
   printf "Password was not changed. Will prompt again at next login\n"
   printf "until password is changed."
   printf "\n"
   exit 1
}
USERS="root factory admin user"
INPUT=""
exit_msg_displayed=0
trap myexit  INT
paschanged=0

for username in $USERS;
do {
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
	fi
echo "for user - $username"
  if [ "$username" = "root" ]; then
    /bin/setrootpass
   else
    /bin/passwd $username
   fi
stat=$?
if [ $stat -eq 0 ]; then
paschanged=1
fi
fi
}
done

trap  "" INT
if [ $paschanged -eq 1 ]; then
echo "Saving passwords to stable storage."
config save /etc/passwd
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
