#! /bin/sh
#
# adduser 1.92: a utility to add users to the system
#
# Copyright (C) 1994 Debian Association, Inc.
#
#    adduser 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.
#
#    adduser is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with adduser; if not, write to the Free Software Foundation,
#    Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

set -e

# Everything happens too fast, so don't let the user interrupt.
# We certainly don't want a half-done job.
trap "" 1 2 3 15

exist=0
numarg=$#

username="$1"

defaults="/etc/adduser.conf"
passwd="/etc/passwd"
group="/etc/group"
pbak="/etc/passwd~"
gbak="/etc/group~"

# This is the standard method of locking the password file.
# Is there a ``standard'' method of locking the group file?
plock="/etc/ptmp"

# We use this to lock the group file in Debian GNU/Linux.
glock="/etc/gtmp"

# If USERGROUPS is "no" and there are no users in the default group,
# then add the user to the group in $group without a leading comma.
# If there are, then add the user to the group in $group preceded by
# a comma.  If USERGROUPS is "yes", then simply create the new group
# and add the user to it.
add_to_group()
{
  if [ $USERGROUPS = "no" ]
  then
    # First of all, determine the name of the group.
    group_number=$1
    group_name=`grep "::$group_number:" $group | cut -f 1 -d ":"`
    echo -n "($group_name)... "
    if grep -x "^$group_name::$group_number:$" $group >/dev/null 2>&1
    then
      sed "/^$group_name/s/\$/$username/" $group > $glock
    else
      sed "/^$group_name/s/\$/,$username/" $group > $glock
    fi
    if [ -f $glock ]
    then
      cp $group $gbak
      mv $glock $group
    fi
  else
    # Note that arguments are completely ignored in this case.
    echo -n "($username)... "
    echo "$username::$nuid:$username" >> $glock
    cp $group $gbak
    mv $glock $group
  fi
}

# What do we do if $defaults doesn't exist? 
no_defaults ()
{
  DSHELL="/bin/bash"
  DHOME="/home"
  SKEL="/etc/skel"
  USERGROUPS="yes"
  FIRST_UID=1000
  USERS_GID=1000
}

# Make sure that we can add $username to the system.
if [ `whoami` != "root" ]
then
  echo "$0: only root may add users to the system."
  exit 1
fi
if [ $numarg = 0 ]
then
  echo "$0: you need to specify the username to add;"
  echo "for example, \`$0 imurdock'."
  echo "The default variables are defined in the file"
  echo "$defaults."
  exit 1
fi
if grep ^$username /etc/passwd >/dev/null
then
  echo "$0: the user $username already exists." ; exit 1
fi
if [ -f $plock ]
then
  echo "$0: $passwd is locked.  Try again later." ; exit 1
fi
if [ -f $glock ]
then
  echo "$0: $group is locked.  Try again later." ; exit 1
fi

# Okay, we can.
cp $passwd $plock
cp $group  $glock
# Damn! We don't have link(1) (or is it link(8)?),
# so we'll have to live with the race condition - iwj

# And now the programs begins.

# Check that we have a valid username.

if [ -f $defaults ]
then
  echo -n "$0: reading in $defaults... " ; source $defaults ; echo "done."
else
  echo "$0: no defaults file found.  Using built-in defaults." ; no_defaults
fi

echo "" ; echo -n "Looking for first available UID... "
nuid=`cat /etc/passwd | sed /^nobody:/d | cut -f 3 -d ":" | sort -n | \
      tail -1` ; nuid=`expr $nuid + 1`
if [ $nuid -lt $FIRST_UID ]
then
  nuid=$FIRST_UID
fi
if [ $USERGROUPS = "no" ]
then
  ngid=$USERS_GID
elif [ $USERGROUPS = "yes" ]
then
  ngid=$nuid
else
  echo -e "\n$0: USERGROUPS should be \`yes' or \`no'."
  rm -f $plock $glock
  exit 1
fi
echo "done.  Using UID $nuid and GID $ngid."

if [ $nuid = 0 -o $nuid -gt 65534 ]		# _Definitely_ don't want that!
then
  echo ""
  echo "$0: Ack!  Something went wrong!  Aborting!"
  echo ""
  rm -f $plock $glock
  exit 1					# _Big_ time...
fi

echo -n "Adding user $username... "
echo "$username:*:$nuid:$ngid::$DHOME/$username:$DSHELL" >> $plock
cp $passwd $pbak
mv $plock $passwd
echo "done."

echo -n "Adding $username to group $ngid "
add_to_group $ngid
echo "done."

echo -n "Creating home directory: $DHOME/$username... "
if [ -d $DHOME/$username ]
then
  echo -e "\n*** $DHOME/$username already exists!  Not copying files from $SKEL. ***"
else
  mkdir $DHOME/$username
  if [ "$USERGROUPS" = "yes" ]
  then
    chown $nuid.$ngid $DHOME/$username
    chmod 2775 $DHOME/$username
  fi
  cp -i $SKEL/.[a-z]* $SKEL/* $DHOME/$username >/dev/null 2>&1 || true
  if [ "$USERGROUPS" = "yes" ]
  then
    for dotfile in .bash_profile .profile .login
    do
      if [ -f $DHOME/$username/$dotfile ]
      then
        sed 's/umask 0\([267]\)\1/umask 00\1/' $DHOME/$username/$dotfile \
         > $DHOME/$username/$dotfile.new
        mv $DHOME/$username/$dotfile.new \
         $DHOME/$username/$dotfile
      fi
    done
    chmod g+w $DHOME/$username/.[a-z]*
  fi
  # Probably will never happen, but just in case... we don't want all
  # files on the system to be `chown'ed to $nuid.$ngid!
  if [ "$DHOME/$username" != "/" ]
  then
    chown -R $nuid.$ngid $DHOME/$username
  fi
  echo "done."
fi

set -f

passwd $username
pass=`cat $passwd | fgrep $username | cut -d : -f 2`
while [ "$pass" = "*" ]
do
{
  echo -e "\nPlease try again."
  passwd $username
  pass=`cat $passwd | fgrep $username | cut -d : -f 2`
}
done

set +f
chfn $username
if [ -x /usr/bin/finger ]
then
  finger $username
  echo -ne "\n\nIs this okay (y/n)? "
else
  echo -ne "\n\nIs this finger information correct (y/n)? "
fi
read input

until [ $input = y -o $input = Y ]
do
{
  chfn $username
  if [ -x /usr/bin/finger ]
  then
    finger $username
    echo -ne "\n\nIs this okay [y/n]? "
  else
    echo -ne "\n\nIs this finger information correct [y/n]? "
  fi
  read input
}
done

# EOF
