#! /bin/sh
#----------------------------------------------------------------------------
# /var/install/bin/add-user - add user srcipt - Eisfair-2/Eisxen version
#
# Copyright (c) 2001-2009 The Eisfair Team, c/o Frank Meyer, frank(at)eisfair(dot)org
#
# Creation:     04.11.2001  fm
# Last Update:  $Id: add-user,v 1.9 2009-03-31 10:45:16 jv Exp $
#
# 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
. /var/install/include/eislib

#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

           case "XX$home" in
                "XX")
                    home="/home/$user"
                    mkdir -p ${home}
                    ;;
                XX/dev/null)
                    home="/home/__dummyhome__"
                    ;;
                XX/*)
                    mkdir -p ${home}
                    ;;
           esac

           if [ -z "$shell" ]
           then
               shell=/bin/bash
           fi
            
           # use -r if create system a acount
           systemswitch=""
           if [ $uid -lt 2000 ] 
           then
               systemswitch="-r"
           fi

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

           case $flags in
                *-d)
                    # disable password
                    passwd -d $user >/dev/null
                    ;;
                *-l)
                    # lock password
                    passwd -l $user >/dev/null
                    ;;
                  *)
                    # 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