#!/bin/sh
# Script to setup the initial usernames / passwords on a jail
# Args $1 = IP
# Args $2 = rootPW
# Args $3 = userName
# Args $4 = userPW
######################################################################

# Source our functions
PROGDIR="`pbreg get /PC-BSD/TheWarden/ProgDir`"

if [ -z "${PROGDIR}" ]
then
   echo "PROGDIR unset! Is The Warden installed properly?"
   exit 155
fi

# Source our variables
. ${PROGDIR}/conf/warden.conf


IP="$1"
ROOTPW="${2}"
USERNAME="${3}"
USERPW="${4}"

export USERNAME USERPW ROOTPW


if [ -z "$IP" ]
then
  echo "ERROR: You must specify an IP of a jail"
  exit 5
fi

if [ -z "${JDIR}" ]
then
  echo "ERROR: JDIR is unset!!!!"
  exit 5
fi

if [ ! -d "${JDIR}/${IP}" ]
then
  echo "ERROR: No jail located at $JDIR/$IP"
  exit 5
fi

if [ -z "${ROOTPW}" -o -z "${USER}" -o -z "${USERPW}" ]
then
  echo "ERROR: Missing root / user settings!"
  exit 5
fi



# End of error checking, now lets add the users / passwords
#####################################################################

echo "Changing root password on: ${IP}"
echo "Adding user ${USERNAME} on: ${IP}"

echo '#!/bin/sh

echo "${ROOTPW}" | pw usermod root -h 0
echo "${USERPW}" | pw useradd -n "${USERNAME}" -G wheel,operator -h 0 -s /bin/csh -d "/home/${USERNAME}" -m -c "${USERNAME}"

' > "${JDIR}/${IP}/.chpass.sh"
chmod 755 "${JDIR}/${IP}/.chpass.sh"
chroot "${JDIR}/${IP}" /.chpass.sh
rm "${JDIR}/${IP}/.chpass.sh"


unset ROOTPW
unset USERPW
unset USERNAME
