#!/bin/sh
##############################################################
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)reset_passwd 1.5	03/11/14 SMI"
#
##############################################################
#
#  Usage:  reset_passwd [all | admin | storage | guest]
#
##############################################################


#echo Arg0 = $0 
#echo Arg1 = $1
#echo Num args = $#
#echo Process ID = $$
valid=0
admin=0
storage=0
guest=0


#
# Figure out what passwords to update & set the appropriate variable if we
#  need to update that password
#
if [ $# = 0 ]
then
  echo "$0: Enter the userid you want reset (all, admin, storage, guest)"
  exit
elif [ $1 = "help" ]
then
  echo "$0: Enter the userid you want reset (all, admin, storage, guest)"
  valid=1
elif [ $1 = "all" ]
then
  echo Resetting passwords to default for admin, storage, and guest
  valid=1
  storage=1
  admin=1
  guest=1
elif [ $1 = "storage" ]
then
  echo Resetting password to default for storage
  valid=1
  storage=1
elif [ $1 = "admin" ]
then
  echo Resetting password to default for admin
  valid=1
  admin=1
elif [ $1 = "guest" ]
then
  echo Resetting password to default for guest
  valid=1
  guest=1
fi

if [ $valid = 0 ]
then
  echo Invalid argument $1
  exit
fi


#
#  Replace the passwords in the shadow file if needed,
#   else just move the unaltered file through
#
cat /etc/shadow > /tmp/tmp.$$

if [ $storage = 1 ]
then
  cat /tmp/tmp.$$ | sed 's/^storage.*$/storage:LPk85b\/klyAOU:11982::::::/g' > /tmp/tmp1.$$
else
  cat /tmp/tmp.$$ > /tmp/tmp1.$$
fi

if [ $admin = 1 ]
then
  cat /tmp/tmp1.$$ | sed 's/^admin.*$/admin:5RTxG7s9kWYqE:11982::::::/g' > /tmp/tmp.$$
else
  cat /tmp/tmp1.$$ > /tmp/tmp.$$
fi

if [ $guest = 1 ]
then
  cat /tmp/tmp.$$ | sed 's/^guest.*$/guest:bRDvJvNL.idZI:11982::::::/g' > /tmp/tmp1.$$
else
  cat /tmp/tmp.$$ > /tmp/tmp1.$$
fi


#
#  Replace the shadow file and remove temp files
#
mv /tmp/tmp1.$$ /etc/shadow
rm /tmp/tmp.$$

printf "Sanity Checking ..."
printf "passwd file ..."
#
#  Now check to make sure that we have everything, If not tell the user
#
grep '^root' /etc/passwd > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Error - root not in passwd file"
fi

grep '^admin' /etc/passwd > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Error - admin not in passwd file"
fi

grep '^guest' /etc/passwd > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Error - guest not in passwd file"
fi

grep '^storage' /etc/passwd > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Error - storage not in passwd file"
fi

#
#  Shadow file (if not here put it in)
#
if [ -f /etc/shadow ]
then
  printf "shadow file ..."
else
  echo "Warning no /etc/shadow file -- attempting to correct"
  touch /etc/shadow
fi

grep '^root' /etc/shadow > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Warning - root not in shadow file -- attempting to correct"
  echo "root:aQg2kf8y9HJkA:11982::::::" >> /etc/shadow
fi

grep '^admin' /etc/shadow > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Warning - admin not in shadow file -- attempting to correct"
  echo "admin:5RTxG7s9kWYqE:11982::::::" >> /etc/shadow
fi

grep '^guest' /etc/shadow > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Warning - guest not in shadow file -- attempting to correct"
  echo "guest:bRDvJvNL.idZI:11982::::::" >> /etc/shadow
fi

grep '^storage' /etc/shadow > /dev/null
rc=$?
if [ $rc != 0 ]
then
  echo "Warning - storage not in shadow file -- attempting to correct"
  echo "storage:LPk85b/klyAOU:11982::::::" >> /etc/shadow
fi

printf "Done\n"

exit

