#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/add-user - add user
#
# Creation:     2001-11-04 fm
# Last Update:  $Id$
#
# Copyright (c) 2001-2009 the eisfair team, team(at)eisfair(dot)org
#
# usage: add-user
#    or: add-user [-d|-l] user encrypted-password uid gid name home shell
#
#                 option -d : disable password
#                 option -l : lock password
#
# if password is empty, user will be prompted
# if uid or gid is empty, values will be evaluated
#
# 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.
#----------------------------------------------------------------------------

# include eislib etc.
. /var/install/include/eislib
. /var/install/include/check-eisfair-version

#-----IMPORTANT--------------------------------
# This script is used on all eisfair systems
#----------------------------------------------

#exec 2>./add-user-trace-$$.log
#set -x

### -------------------------------------------------------------------------
### get optional flags
### -------------------------------------------------------------------------
flags=''
noadd=0
while [ 1 ]
do
    case "$1" in
        -d)
            if [ $noadd -eq 0 ]
            then
                flags="$flags -d"
                noadd=1
            else
                mecho -warn "Redundant option '-d' will be ignored!"
            fi
            shift
            ;;
        -l)
            if [ $noadd -eq 0 ]
            then
                flags="$flags -l"
                noadd=1
            else
                mecho -warn "Redundant option '-l' will be ignored!"
            fi
            shift
            ;;
         *)
            break
            ;;
    esac
done

### -------------------------------------------------------------------------
### get parameter
### -------------------------------------------------------------------------
case $# in
    0)
        interactive='true'

        user=""
        password=""
        uid=""
        gid=""
        name=""
        home=""
        shell=""
    ;;
    7)
        interactive='false'

        user="$1"
        password="$2"
        uid="$3"
        gid="$4"
        name="$5"
        home="$6"
        shell="$7"
    ;;
    *)
        echo "usage: `basename $0`" >&2
        echo "   or: `basename $0` user encrypted-password uid gid name home shell" >&2
        exit 1
    ;;
esac

### -------------------------------------------------------------------------
### clear screen
### -------------------------------------------------------------------------
if [ "$interactive" = "true" ]
then
    clrhome
    mecho -info "Add user"
    echo
fi

### -------------------------------------------------------------------------
### create uid
### -------------------------------------------------------------------------
if [ -z "$uid" ]
then
    oldifs="$IFS"
    IFS=':'
    uid=2000
    while read line
    do
        set -- $line
        if [ $3 -gt $uid -a $3 -lt 3000 ]
        then
            uid=$3
        fi
    done </etc/passwd
    IFS="$oldifs"

    uid=`expr $uid + 1`
else
    for a in `cat /etc/passwd | cut -d":" -f3`
    do
        if [ "$uid" = "$a" ]
        then
             mecho -error "uid $uid already exists"
             echo
             anykey
             exit 1
        fi
    done
fi

### -------------------------------------------------------------------------
### get user name
### -------------------------------------------------------------------------
if [ -z "$user" ]
then
    mecho "Information: The Username or Login should be written in lowercase"
    echo
    /var/install/bin/ask "Username or Login (e.g. 'newuser'):" '' '*' > /tmp/ask.$$
    rc=$?
    user=`cat /tmp/ask.$$`
    rm -f /tmp/ask.$$
    if [ $rc = 255 ]
    then
        exit 1
    fi
fi

### -------------------------------------------------------------------------
### get password and append user to files
### -------------------------------------------------------------------------
echo
case "$user" in
    "")
        mecho -warn "no user added"
        echo
        anykey
        exit 1
    ;;
    *)
        grep "^$user:" /etc/passwd >/dev/null
        if [ $? = 0 ]
        then
            mecho -error "user $user already exists"
            echo
            anykey
            exit 1
        else
            if [ -z "$name" ]
            then
                /var/install/bin/ask "Name of user (comment field):" '' '*' > /tmp/ask.$$
                rc=$?
                name=`cat /tmp/ask.$$`
                rm -f /tmp/ask.$$
                if [ $rc = 255 ]
                then
                    exit 1
                fi
            fi

            grep -q "^users:" /etc/group
            if [ $? != 0 ]
            then
                /usr/sbin/groupadd -g 100 users
            fi

            if [ -z "$gid" ]
            then
                gid=100
            else
                gidexists=false

                for a in `cat /etc/group | cut -d":" -f3`
                do
                    if [ "$gid" = "$a" ]
                    then
                        gidexists=true
                    fi
                done

                if [ "$gidexists" = "false" ]
                then
                    mecho -error "gid $gid doesn't exists"
                    echo
                    anykey
                    exit 1
                fi
            fi

            if [ -z "$shell" ]
            then
                shell=/bin/bash
            fi



            # use -r if create system a acount
            homeswitch=""
            systemswitch=""
            case ${EISFAIR_SYSTEM} in
                eisfair-1)
                    #old home handling used from eisfair-1 only
                    homeswitch="-m"

                    case "${home}" in
                        "")
                            home="/home/${user}"
                            ;;
                        "/dev/null")
                            home="/home/__dummyhome__"
                            homeswitch="-M"
                            ;;
                    esac
                    ;;
                *)
                    # eisfair-2 or eisxen-1 ...
                    if [ ${uid} -lt 2000 ]
                    then
                        systemswitch="-r"
                    else
                        homeswitch="-m"
                    fi
                    case "XX$home" in
                        "XX")
                            home="/home/$user"
                            if [ -z "$homeswitch" ]
                            then
                                mkdir -p ${home}
                            fi
                            ;;
                        XX/dev/null)
                            home="/home/__dummyhome__"
                            ;;
                        XX/*)
                            if [ -z "$homeswitch" ]
                            then
                                mkdir -p ${home}
                            fi
                            ;;
                    esac
                    ;;
            esac

            /usr/sbin/useradd $systemswitch -u $uid -g $gid -c "$name" -s $shell -d $home $homeswitch $user

            case $flags in
                *-d)
                    # disable password
                    passwd -d $user >/dev/null
                ;;
                *-l)
                    # lock password
                    case ${EISFAIR_SYSTEM} in
                        eisfair-1)
                            # use passwd for eisfair-1
                            passwd -l ${user} >/dev/null
                            ;;
                        eisfair-2|eisxen-1)
                            # use eisfair-2 or eisxen-1
                            usermod -L ${user}
                            ;;
                    esac
                ;;
                *)
                    # get / set password
                    if [ -z "$password" ]
                    then
                        idx=1
                        while [ $idx -le 3 ]
                        do
                            passwd $user
                            if [ $? = 0 ]
                            then
                                break
                            fi
                            idx=`/usr/bin/expr $idx + 1`
                            if [ $idx -eq 4 ]
                            then
                                # disable password
                                passwd -d $user >/dev/null
                                echo
                                mecho -error "Failed to get a valid password. Login for $user disabled."
                                mecho -std "Please use 'passwd $user' to change password manually."
                                anykey
                            fi
                        done
                    fi
                ;;
            esac

            # set permissions of home
            if [ "$home" != "/home/__dummyhome__" ]
            then
                chown $user $home
                chgrp $gid  $home
                chmod 700   $home
            fi
        fi
    ;;
esac

if [ "$interactive" = "true" ]
then
    echo
    anykey
fi

exit 0