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

###############################################################################
##
##  Check if a user exists in the system
##
###############################################################################

UserExists () {
    username=$1

    if grep -e "^$username:" "$passwdFile" &>/dev/null && grep -e "^$username:" "$shadowFile" &>/dev/null ; then
        return 0
    else
        return 1
    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:-"@KEEP@"}
    usershell=${5:-"@KEEP@"}
    uid=${6:-"@KEEP@"}
    gid=${7:-"@KEEP@"}
    gecos=${8:-"@KEEP@"}

    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 UserExists "$username" ; then
        userExists=true
    else
        userExists=false
    fi

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

    ##
    ##  Check mode and user exists: return SUCCESS
    ##
    if [ "$mode" = check ] && $userExists ; then
       return 0
    fi

    ##
    ##  If change mode: get the old passwd settings for the user and set the @KEEP@ fields
    ##
    if [ "$mode" = change ] ; then
        pwdLine=$(grep -e "^$username:" "$passwdFile" 2>/dev/null)
        OIFS=$IFS ; IFS=':' ; set $pwdLine "" ; IFS=$OIFS
        oldUsername=$1 ; oldPassword=$2 ; oldUid=$3 ; oldGid=$4 ; oldGecos=$5 ; oldHomedir=$6 ; oldUsershell=$7

        [ "$uid"       = "@KEEP@" ] && uid=$oldUid
        [ "$gid"       = "@KEEP@" ] && gid=$oldGid
        [ "$gecos"     = "@KEEP@" ] && gecos=$oldGecos
        [ "$homedir"   = "@KEEP@" ] && homedir=$oldHomedir
        [ "$usershell" = "@KEEP@" ] && usershell=$oldUsershell
    else
        [ "$uid"       = "@KEEP@" ] && uid=9999
        [ "$gid"       = "@KEEP@" ] && gid=9999
        [ "$gecos"     = "@KEEP@" ] && gecos="Default GECOS"
        [ "$homedir"   = "@KEEP@" ] && homedir=/tmp
        [ "$usershell" = "@KEEP@" ] && usershell=/bin/sh
    fi
 
    grep -v -e "^$username:" "$passwdFile" > $tmpfile
    if [ "$mode" != del ] ; then
        echo "$username:x:$uid:$gid:$gecos:$homedir:$usershell" >> $tmpfile
    fi
    mv $tmpfile "$passwdFile"

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

    ##
    ##  Set the new password for the user.
    ##
    if [ "$mode" != del ] ; then
        /usr/bin/makepasswd -e shmd5 -p "$password" \
            | awk "{print \"$username:\"\$2}" \
            | /usr/bin/chpasswd -e
    fi

    return $?
}

###############################################################################
##
##  MAIN
##
###############################################################################

cmd=$1

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

case "$cmd" in
    add|check|change|del)
        shift
        UserSet $cmd "$@"
        exit $?
        ;;

    delete)
        shift
        UserSet del "$@"
        exit $?
        ;;

    backup)
        username=$2
        backupFile=$3

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

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

        if UserExists "$username" ; 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"
        else
            echo "ERROR: user '$username' doesn't exist in $passwdFile or $shadowFile!"
            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")
        shadowLine=$(awk 'FNR == 3 { print $0 }' < "$backupFile")
        username=$(echo $passwdLine | awk -F: '{print $1}')

        if [ -n "$passwdLine" -a -n "$shadowLine" -a -n "$username" ] ; then
            grep -v -e "^$username:" "$passwdFile" > $tmpfile
            echo "$passwdLine" >> $tmpfile
            mv $tmpfile "$passwdFile"

            grep -v -e "^$username:" "$shadowFile" > $tmpfile
            echo "$shadowLine" >> $tmpfile
            mv $tmpfile "$shadowFile"
        else
            echo "ERROR: $0 $cmd: username: '$username' passwdLine: '$passwdLine' shadowLine: '$shadowLine' empty!"
            exit 1
        fi
        ;;

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

exit 0
