#!/bin/sh
# Script to reset the Zeus configuration admin password
# You can either supply the new password in the command-line, 
# or be prompted for one

# ------------------------------------------------------------------
# Prints the error message and exits

fatal()
{
    cat<<EOF
FATAL: $1
EOF
    exit 1
}

# ------------------------------------------------------------------
# Get a password if not supplied in the command line

get_password()
{
    PASSWD=""
    until [ "$PASSWD" != "" ]
    do
	printf "$1: "
	stty -echo
	read password1
	stty echo
	printf "\nRe-enter password: "
	stty -echo
	read password2
	stty echo
	echo
	if [ "$password1" = "$password2" ]
	then 
	    PASSWD=$password1
	else
	    echo "Passwords do not match!"
	fi
    done
}

# ------------------------------------------------------------------
# Returns a user-supplied value in $VALUE
# $1 prompt
# $2 default value
# $3 regexp of correct answers

get_setting() {
    while [ 1 ]
    do
	printf "$1 [$2]: "
	read VALUE
	[ -z "$VALUE" ] && VALUE=$2
	[ -z "$3" ] && return 0
	case "$VALUE" in
	$3) return 0
	    ;;
	esac
    done
}

# ------------------------------------------------------------------
# Start of the main block

if test "x$1" = x--help; then
  echo "Usage:"
  echo ""
  echo "  reset_password [PASSWORD]"
  echo
  exit 0
fi

# Ensure ZEUSHOME is setup to point to our installation
if [ -z "$ZEUSHOME" ]
then
    # Have a good guess, we live in $ZEUSHOME/admin/bin
    OLD_PWD=`pwd`
    cd `dirname $0`/../..
    ZEUSHOME=`pwd`
    export ZEUSHOME
    cd $OLD_PWD
fi

if [ ! -d "$ZEUSHOME/admin" ]
then
  echo 'FATAL: $ZEUSHOME is not set, or is set incorrectly'
  echo 'It should point to the Zeus installation running the adminserver'
  exit 1
fi
cd $ZEUSHOME

ZXTM=0
PASSFILE="$ZEUSHOME/admin/website"
if [ -d "$ZEUSHOME/zxtmadmin" ]
then
   ZXTM=1
   PASSFILE="$ZEUSHOME/zxtm/conf/users"

   if [ ! -f $PASSFILE ] ; then
       # Ensure the passfile exists
       touch $PASSFILE 2>/dev/null >/dev/null
       if [ $? != 0 ] ; then
	   echo "FATAL: Failed to create $PASSFILE, perhaps you don't have sufficient permissions"
	   exit 1;
       fi
   fi
fi

if [ ! -w $PASSFILE ]
then
  echo 'FATAL: You do not have sufficient permission to change the admin password'
  echo 'Rerun this script as the correct user'
  exit 1
fi

# Warn user, explain permission problems & ask whether to continue
if [ $ZXTM = 1 ] ; then
    echo WARNING: Executing this command will update the password for the 'admin'
    echo local echo user, creating the user if necessary, and ensuring the user
    echo is in the 'admin' group
    echo
else
    echo WARNING: Executing this command will change the admin password for your
    echo admin server.
    echo
fi

get_setting "Do you wish to continue?" "Y" "[YyNn]"
if [ "$VALUE" = "n" -o "$VALUE" = "N" ]
then
    echo Password has NOT been updated.
    exit 1
fi
echo

# make sure we have a new password to set
PASSWD=$1
if [ "$PASSWD" = "" ]
then
    get_password "Enter new 'admin' password"
fi

if [ $ZXTM = 0 ]
then
   # stop the server ...
   cd $ZEUSHOME  # Should be here already, but won't hurt
   echo
   echo Stopping the admin server ...
   admin/rc stop
fi

# backup existing settings
echo
echo Backing up current settings to $PASSFILE.backup
if [ $ZXTM = 1 ]
then
   cp zxtm/conf/users zxtm/conf/users.backup
else
   cp admin/website admin/website.backup
fi

# Generate new password and write it into the file
echo Updating the 'admin' users password
echo $PASSFILE
NEWPASSWD=""
if [ $ZXTM = 1 ]
then
   # crypt the $PASSWD
   NEWPASSWD=`$ZEUSHOME/perl/miniperl -I$ZEUSHOME/admin/lib/perl -mZeus::Util -e "print Zeus::Util::PWcrypt( \"$PASSWD\" );"`

   # Remove any old admin user (group, password and status)
   cat zxtm/conf/users.backup | grep -v 'user!admin!\(password\|group\|status\)' > zxtm/conf/users

   # Ensure the 'admin' user exists, and is valid
   echo "user!admin!status 1" >> zxtm/conf/users
   echo "user!admin!password $NEWPASSWD" >> zxtm/conf/users
   echo "user!admin!group admin" >> zxtm/conf/users
else
   # md5 the $PASSWD
   NEWPASSWD=`printf $PASSWD | admin/bin/md5 -pem -`

   # copy md5 password into the new config file
   sed "s@^modules!access!users!admin.*@modules!access!users!admin $NEWPASSWD@" < admin/website.backup > admin/website
fi

if [ $ZXTM = 0 ]
then
   # restart the server ...
   cd $ZEUSHOME
   echo
   echo Restarting the admin server ...
   admin/rc start
fi

# check to see incorporation of the new password into the file succeeded
# it is possible that the user only had +rw access to website, and not
# +x access to the admin/rc command, in which case the change will take
# effect next time the admin server is restarted
echo
VALUE=""
if [ $ZXTM = 1 ]
then
   VALUE=`grep $NEWPASSWD zxtm/conf/users`
else
   VALUE=`grep $NEWPASSWD admin/website`
fi

if [ "$VALUE" = "" ]
then
    fatal "Update of password failed.  Check access permissions."  
else
    echo " INFO:Configuration password has been reset"
fi

exit 0

=head1 NAME

reset_password - change the Zeus Administration Server password

=head1 SYNOPSIS

B<reset_password> [ B<password> ]

=head1 DESCRIPTION

B<reset_password> allows you to change the password to the Zeus
Administration server. The old password is not required, however you
will need the appropriate permissions to alter the configuration files
involved.

If run interactively, B<reset_password> prompts for a password, which must
be entered twice to ensure there are no typos. Alternatively, the password
can be supplied on the command line.

B<reset_password> is a component of the administration server package, and is 
located in I<$ZEUSHOME/admin/bin/> on an Administration Server machine.

=head1 NOTES

Whilst supplying the password on the command line is helpful for
installation scripts, it should not be used on a live system, because
other users could be able to see the password in the program arguments.
Please use carefully!

=head1 COPYRIGHT

Copyright (C) 2000-2014 Zeus Technology Limited. All rights reserved.

=cut
