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

cmd=$1

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

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

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

        ##
        ##  Add user with password to the system
        ##
        if [ -z "$username" -o -z "$password" ] ; then
            echo "ERROR: $0 $cmd: need args <username> and <password>"
            exit 1
        fi
        if [ $cmd == change ] ; then
            if ! grep -e "^$username:" $passwdFile &>/dev/null || ! grep -e "^$username:" $shadowFile &>/dev/null ; then
                echo "ERROR: $0 $cmd: user $username doesn't exist!"
                exit 1
            fi
        fi

        grep -v -e "^$username:" $passwdFile > $tmpfile
        echo "$username:x:$uid:$gid::$homedir:$usershell" >> $tmpfile
        mv $tmpfile $passwdFile

        grep -v -e "^$username:" $shadowFile > $tmpfile
        echo "$username:password:012681:0:99999:7:::" >> $tmpfile
        mv $tmpfile $shadowFile

        /usr/bin/makepasswd -e shmd5 -p $password \
            | awk "{print \"$username:\"\$2}" \
            | /usr/bin/chpasswd -e
        ;;

    del|delete)
        username=$2

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

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

        grep -v -e "^$username:" $shadowFile > $tmpfile
        mv $tmpfile $shadowFile
        ;;

    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 && grep -e "^$username:" $shadowFile &>/dev/null ; then
            echo "PASSWD_BACKUP $(date '+%Y-%m-%d_%H:%M:%S')" > "$backupFile"
            grep -e "^$username:" $passwdFile | tail -n 1 >> "$backupFile"
            grep -e "^$username:" $shadowFile | tail -n 1 >> "$backupFile"
        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")
        shadowLine=$(awk 'FNR == 3 { print $0 }' < "$backupFile")
        username=$(echo $passwdLine | awk -F: '{print $1}')

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

        grep -v -e "^$username:" $shadowFile > $tmpfile
        echo "$shadowLine" >> $tmpfile
        mv $tmpfile $shadowFile
        ;;

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

exit 0
