#!/bin/sh
###############################################################################
##
##  usertool - add/delete user to /etc/passwd and /etc/shadow
##
##
##
##  2007-01-16	Andreas Vogel (omega)
##
###############################################################################

cmd=$1

## make sure we got the right commands

MAKEPASSWD=/bin/true
CHPASSWD=/bin/true

if [ -e "/usr/bin/makepasswd" ] ; then
	MAKEPASSWD="/usr/bin/makepasswd -e shmd5 -p"
elif [ -e "/usr/bin/cryptpw" ] ; then
	MAKEPASSWD="/usr/bin/cryptpw -m shmd5 -p"
fi

if [ -e "/usr/bin/chpasswd" ] ; then
	CHPASSWD="/usr/bin/chpasswd -e"
elif [ -e "/usr/sbin/chpasswd" ] ; then
	CHPASSWD="/usr/sbin/chpasswd -e"
fi

###############################################################################
##
##  UserSet - add/change/check user and user password
##
##  mode:      add    : add user with password, overwrite user if it already exists
##             check  : add user with password if it doesn't exist
##             change : change password for user, error if user doesn't exist
##             del    : delete user
##  username:  user name
##  password:  plaintext password for user
##
##
###############################################################################
UserSet () {
    mode=$1 ; username=$2 ; password=$3
    homedir=${4:-/root}
    usershell=${5:-/bin/sh}
    uid=${6:-0}
    gid=${7:-0}

    if [ -z "$username" ] ; then
        echo "ERROR: UserSet($mode): empty arg <username>"
        return 1
    fi

    if [ -z "$password" -a "$mode" != del ] ; then
        echo "ERROR: UserSet($mode): empty arg <password>"
        return 1
    fi

    if ! grep -e "^$username:" $passwdFile &>/dev/null ; then
        userExists=false
    else
        userExists=true
    fi

    if [ $mode == change ] && ! $userExists ; then
        echo "ERROR: $0 $mode: user $username doesn't exist!"
        return 1
    fi
    if [ $mode == check ] && $userExists ; then
       return 0
    fi

    grep -v -e "^$username:" $passwdFile > $tmpfile
    if [ "$mode" != del ] ; then
        echo "$username:x:$uid:$gid::$homedir:$usershell" >> $tmpfile
    fi
    mv $tmpfile $passwdFile

    if [ -e $shadowFile ] ; then
        grep -v -e "^$username:" $shadowFile > $tmpfile
        if [ "$mode" != del ] ; then
            echo "$username:password:012681:0:99999:7:::" >> $tmpfile
        fi
        mv $tmpfile $shadowFile
    fi

    if [ "$(uname -m)" != "x86_64" ] ; then
        $MAKEPASSWD "$password" \
            | awk "{print \"$username:\"\$2}" \
            | $CHPASSWD
    else
        /usr/bin/passwd "$username" "$password"
    fi

    return $?
}

###############################################################################

passwdFile=/etc/passwd
shadowFile=/etc/shadow
tmpfile=/tmp/tmp.passwd.$$

case "$cmd" in
    add|check|change)
        username=$2
        password=$3
        homedir=${4:-/root}
        usershell=${5:-/bin/sh}
        uid=${6:-0}
        gid=${7:-0}

        UserSet $cmd "$username" "$password" "$homedir" "$usershell" "$uid" "$gid"
        exit $?
        ;;

    del|delete)
        username=$2

        UserSet del "$username"
        exit $?
        ;;

    backup)
        username=$2
        backupFile=$3

        if [ -z "$username" ] ; then
            echo "ERROR: $0 $cmd: need arg <username>"
            exit 1
        fi

        if [ -z "$backupFile" ] ; then
            echo "ERROR: $0 $cmd: need arg <backupFile>"
            exit 1
        fi

        if grep -e "^$username:" $passwdFile &>/dev/null ; then
            echo "PASSWD_BACKUP $(date '+%Y-%m-%d_%H:%M:%S')" > "$backupFile"
            grep -e "^$username:" $passwdFile | tail -n 1 >> "$backupFile"
            if [ -e $shadowFile ] ; then
                grep -e "^$username:" $shadowFile | tail -n 1 >> "$backupFile"
            fi
        else
            echo "ERROR: user '$username' doesn't exist!"
            exit 1
        fi

        if [ ! -r "$backupFile" ] ; then
            echo "ERROR: $0 $cmd: cannot verify file '$backupFile'"
            exit 1
        fi
        ;;

    restore)
        backupFile=$2

        if [ ! -r "$backupFile" ] ; then
            echo "ERROR: $0 $cmd: cannot open '$backupFile'"
            exit 1
        fi

        magic=$(awk 'FNR == 1 { print $1 }' < "$backupFile")
	if [ "$magic" != "PASSWD_BACKUP" ] ; then
            echo "ERROR: $0 $cmd: '$backupFile' - invalid MAGIC"
            exit 1
        fi

        passwdLine=$(awk 'FNR == 2 { print $0 }' < "$backupFile")
        
        if [ -e $shadowFile ] ; then
            shadowLine=$(awk 'FNR == 3 { print $0 }' < "$backupFile")
        fi

        username=$(echo $passwdLine | awk -F: '{print $1}')

        grep -v -e "^$username:" $passwdFile > $tmpfile
        echo "$passwdLine" >> $tmpfile
        mv $tmpfile $passwdFile

        if [ -e $shadowFile ] ; then
            grep -v -e "^$username:" $shadowFile > $tmpfile
            echo "$shadowLine" >> $tmpfile
            mv $tmpfile $shadowFile
        fi
        ;;

    *)
        echo "ERROR: usage: $0 {add|delete|backup|restore} username password [homedir usershell uid gid]"
        exit 1
        ;;
esac

exit 0
