#!/bin/bash
#######################################################
#
# Removeusers 
# A bulk user removing program
# Author: Phil Jones (phil@lfsp.org)
#
########################################################


##################
# Checks section #
##################

# See if at least one file has been specified on the command line.
if [ $# -eq 0 ]; then
   echo "Usage: removeusers [filename]" >&2
   exit 1 
fi

# If more than one file specified, tell user only one file
# can be taken at a time.
if [ $# -gt 1 ]; then
   echo -n "Sorry, removeusers can only accept one file " >&2
   echo    "at a time."
   exit 1 
fi

# Convert DOS newlines (CR/LF) to Unix format
pid=$$
sed s/
$// $1 > /tmp/usersfile.$pid
mv /tmp/usersfile.$pid $1

# Check the format of the input file.
echo "Verifying input file '$1'..."
if ! /usr/local/lib/createusers/verify_removeusers_input.pl $1
then
   exit 1
fi


# Make sure the checks file is installed and executable.
if ! test -x /usr/local/lib/createusers/checks > /dev/null; then
     echo "unable to execute" \  
          "'/usr/local/lib/createusers/checks'." >&2
     exit 1
fi

# Run checks.
if ! /usr/local/lib/createusers/checks $1; then
     echo "One or more checks failed." >&2
     exit 1
fi



#################
# Get variables #
#################

# Get number of lines.
linecount=`/usr/local/lib/createusers/linecount.pl $1`
currentline=1

config_file=/etc/createusers.conf

make_mysql_databases=`grep "^make_mysql_databases" \
                     $config_file | cut -f2 -d "="`

mysql_server=`grep "^mysql_server" \
                     $config_file | cut -f2 -d "="`

make_postgres_databases=`grep "^make_postgres_databases" \
                         $config_file`

postgres_server=`grep "^postgres_server" \
                       $config_file | cut -f2 -d "="`

create_samba_users=`grep "^create_samba_users" \
                    $config_file`

smbpasswd_file=`grep "^smbpasswd_file" \
                     $config_file | cut -f2 -d "="`



################
# Delete users #
################

cat $1 | while read a
do
 user=`echo $a | cut -f1 -d","`
 home=`grep "^$user:" /etc/passwd | cut -f6 -d ":"`

 echo "Working on line" \
       $currentline \
       "of" \
       $linecount \
       "in input file '$1'" 

 user=`echo $a | cut -f1 -d","`


###################################
# User login and home dir section #
###################################

# Delete user's account. 
if /usr/sbin/userdel $user; then
   echo "Deleted login '$user'" 
fi

# Delete user's home directory if it exists.
if test -d $home; then
   if rm -fr $home; then
      echo "Deleted home directory '$home'"
   fi
fi


#################
# MySQL section #
#################

# See if MySQL support is enabled.
if [ $make_mysql_databases ]; then

   # See if MySQL user exists.
   if mysql -h $mysql_server -u root mysql \
            -e "select user from user where user='$user';" \
            | grep "$user" > /dev/null
   then
      # Delete the user.
      if mysql -h $mysql_server -u root mysql \
               -e "delete from user where user='$user'; \
                   flush privileges;"   
      then
         echo "Deleted MySQL user 'user'"
      fi
   else
      echo "MySQL user '$user' does not exist" >&2
   fi
   # end if user exists

   # See if user has any access to their database.
   if mysql -h $mysql_server -u root mysql \
            -e "select user from db where db='$user' and user='$user';" \
            | grep "$user" > /dev/null
   then
       # Delete user's access to their database.
       if mysql -h $mysql_server -u root mysql \
                -e "delete from db where db='$user' and user='$user'; \
                flush privileges;" 
       then
          echo "Revoked access of MySQL user '$user' to database '$user'"
       fi
   else
       echo "MySQL user '$user' does not have access to database '$user'" >&2
   fi
   # end if MySQL user has access

   # Drop user's database.
   if mysql -h $mysql_server -u root mysql \
            -e "drop database $user" 
   then
      echo "Dropped MySQL database '$user'"
   fi

fi
# end if MySQL enabled


 ######################
 # PostgreSQL section #
 ######################

 # See if PostgreSQL is enabled.
 if [ $make_postgres_databases ]
 then
 
    # Remove PostgreSQL user database. 
    if cat ~/.pg.password | psql -h $postgres_server \
                                 -U postgres \
                                 --command "drop database $user" \
                                 -W
    then
       echo "Dropped PostgreSQL database '$user'"
    fi

    # Remove PostgreSQL user account. Assumes the user didn't create
    # any new databases of their own while they were using their
    # account.
    if cat ~/.pg.password | psql -h $postgres_server \
                                 -U postgres \
                                 --command "drop user $user" \
                                 -W
    then
       echo "Dropped PostgreSQL user '$user'"
    fi

fi
# end if PostgreSQL is enabled.


#################
# Samba section #
#################

# See if Samba support is enabled.
if [ $create_samba_users ]; then

   # See if Samba user exists.
   if ! grep "^$user:" $smbpasswd_file > /dev/null; then
      echo "Samba user '$user' does not exist" >&2

   else

   # Copy existing smbpasswd file to a temporary file. Append the 
   # process id of this script to the temporary file, so it
   # doesn't conflict with another instance of this script that
   # may be running at the same time.
   cp $smbpasswd_file /tmp/smbpasswd.$pid

   # Pass file through sed. Delete the line for the specified user.
   # Redirect output of sed to overwrite the existing smbpasswd file.

   cat /tmp/smbpasswd.$pid | sed /^$user:/d > $smbpasswd_file
   echo "Deleted Samba user '$user'"

   # Clean up by deleting the temporary file.
   rm -f /tmp/smbpasswd.$pid

   fi
   # end if Samba user exists

fi
# end if Samba support is enabled


############
# Feedback #
############

echo "Done user '$user'."
echo "-------------------------"

# Increment the current line status variable.
 currentline=$(( $currentline + 1 ))

done
echo "Finished."
