#!/bin/sh
##############################################################################
##
## $Id: vpasswd,v 1.2 1998/06/17 23:24:51 chris Exp $
## Change a POP user password - Version 2.0
##
## Chris Johnson, Copyright (C) April 1998
## Email: sixie@nccnet.co.uk
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##
##############################################################################
trap "" 1 2 3 15

PATH="`dirname $0`:$PATH"
export PATH

POPACCT=vpopmail
POPGRP=vchkpw

###############################################################################
## No user servicable parts below
###############################################################################

POPUSER=`echo $1 | cut -d@ -f1`
echo $1 | grep '\@' > /dev/null 2>&1
if [ $? -eq 0 ]
then
        HOST=`echo $1 | cut -d@ -f2`
else
        HOST='';
fi


grep "^${POPACCT}\:" /etc/passwd > /dev/null 2>&1
if [ $? -ne 0 ]
then
	echo "Cannot find master POP user $POPACCT in /etc/passwd"
	exit 
else
	POPHOME=`grep "^${POPACCT}\:" /etc/passwd | cut -d: -f6`
	if [ "x$HOST" = "x" ]
	then
		PASSWD="$POPHOME/vpasswd"
		PBAK="$POPHOME/vpasswd-"
		PLOCK="$POPHOME/.pwd.lock"
		HOMEDIR="$POPHOME/users"
	else
		PASSWD="$POPHOME/domains/$HOST/vpasswd"
		PBAK="$POPHOME/domains/$HOST/vpasswd-"
		PLOCK="$POPHOME/domains/$HOST/.pwd.lock"
		HOMEDIR="$POPHOME/domains/$HOST"
	fi
fi

which vmkpasswd > /dev/null 2>&1
if [ $? = 1 ]
then
	echo "Yikes! Cant find vmkpasswd - needed to make encrypted password"
	echo "Aborting!!"
	exit
fi

if [ `id -u` != 0 ]
then
	echo 'Must be root to change POP passwords.'
	exit 1
fi

if [ $# -eq 0 ]
then
	echo "Syntax: `basename $0` <POP username> [apop|pass]"
	echo ""
	echo "If 'apop' is specified, then the user will be flagged to use"
	echo "the APOP mechanism (RFC1939)."
	echo ""
	echo "If 'pass' is specified, then the user will be flagged to use"
	echo "standard user/password authentication."
	echo ""
	echo "The default is to use method the user is already using."
	exit 1
fi

grep "^${POPUSER}\:" $PASSWD > /dev/null 2>&1
if [ $? -eq 1 ]
then
	echo "The login $POPUSER dosent exist - cant change password!"
	exit 1
fi

if [ -f $PLOCK ]; then
	echo "$PASSWD is locked - try again later."
	exit 1
fi
touch $PLOCK

POPENTRY=`grep "^$POPUSER\:" $PASSWD`
POPPW=`echo $POPENTRY | cut -d: -f2`
CAUTH=`echo $POPENTRY | cut -d: -f3`
OAUTH=$CAUTH
AUTHMODE=$2

if [ "x$AUTHMODE" = "xapop" ]
then
	CAUTH=2
fi
if [ "x$AUTHMODE" = "xpass" ]
then
	CAUTH=1
fi

PW=""
while [ "x$PW" = "x" ]
do
	if [ "x$CAUTH" = "x2" ]
	then
		read PW
	else
		PW=`vmkpasswd $POPPW`
	fi
done

cp $PASSWD $PBAK
sed "s|^${POPUSER}\:${POPPW}\:${OAUTH}\:|${POPUSER}\:${PW}\:${CAUTH}\:|" $PBAK > $PASSWD
chmod 600 $PASSWD $PBAK

rm -f $PLOCK
