#! /bin/sh # # Filename: deluser # # Programmer: Br. David Carlson # # Date: December 23, 1997 # # Usage: deluser # # Purpose: To delete a user's account. Can only be used by root. # The program should only be run when no one else is on the system # and logins have been disabled. The program will ask the user what # to do. Note that this script only works for users whose accounts # are located in /usr/home, but that you can change this easily in the # variables section below. # The following function forces the user to enter y or n and returns # that value via the variable Reply. get_yn() { read Reply until [ "$Reply" = "y" -o "$Reply" = "n" ] do echo echo "Enter just a y or n (lower case):" read Reply done } # Set up variables -- change temporarily for debugging: UserId="$1" PasswdLoc="/etc" PasswdFile="$PasswdLoc/passwd" PasswdTmpFile="$PasswdLoc/passwd.tmp" HomeLoc="/usr/home" #HomeLoc="/home" UserHome="$HomeLoc/$UserId" OldusersLoc="/home/admin/oldusers" UserHomeSav="$OldusersLoc/$UserId" MailLoc="/var/spool/mail" MailQueue="$MailLoc/UserId" CgiLoc="/var/lib/httpd/cgi-bin" CgiTmpFile="$OldusersLoc/cgi.tmp" CgiSav="$UserHomeSav/cgi.sav" # Check that it is root running this script: if [ `whoami` != "root" ] then echo echo "ERROR: only root can run this script" exit 1 fi # Check the number of command-line parameters: if [ $# -ne 1 ] then echo echo "ERROR: one parameter needed -- the user login id" echo "Usage: deluser " exit 2 fi echo # Check for a passwd entry for this user. To do this, look for the userid # at the start of the line ^ and ending with a :, the field separator. echo "A search for a matching passwd entry yields:" if grep "^$UserId:" "$PasswdFile" then echo "Do you wish to remove this person's account (y/n)?" get_yn if [ "$Reply" = "n" ] then echo echo "Ending script without removing this account" exit 0 fi else echo echo "No passwd entry for $UserId" exit 3 fi # Look for stuff belonging to this user in the cgi-bin directory. # First, list all files in that dir and cut out the owner and filename fields. # Then grep for lines that start with this user's ID, saving the results # in a file for use below. ls -l "$CgiLoc" | cut -c16-23,56- | grep "^$UserId" > "$CgiTmpFile" # See if the user has a home directory: if [ -d "$UserHome" ] then # Check if there already is a backup directory for a user with the same id: if [ -d "UserHomeSav" ] then echo echo "$UserHomeSav already exists, aborting script without" echo "removing the account." echo "Check to see if this directory is for the same person or not." exit 4 else echo echo "Do you wish to move files for $UserId to $UserHomeSav (y/n)?" get_yn if [ "$Reply" = "y" ] then echo echo "Moving files for $UserId to $UserHomeSav" cp -r "$UserHome" "$OldusersLoc" rm -r "$UserHome" chown -R root "$UserHomeSav" chgrp -R root "$UserHomeSav" chmod 700 "$UserHomeSav" # See if the CgiTmpFile has nonzero size: if [ -s $CgiTmpFile ] then # Move any cgi-bin stuff owned by this user. # Read one line at a time - input redirected in from the CgiTmpFile. mkdir "$CgiSav" echo echo "Moving cgi-bin files owned by this user to $CgiSav." while read line do # Characters 9 and onward are the file (or directory) name. Filename=`echo "$line" | cut -c9-` cp -r "$CgiLoc/$Filename" "$CgiSav" rm -r "$CgiLoc/$Filename" done < "$CgiTmpFile" fi # Check for a mail queue for this user. First, see if file exists: if [ -f "$MailQueue" ] then # See if file has non-zero size: if [ -s "$MailQueue" ] then echo echo "Moving mail queue for $UserId" mv "$MailQueue" "$UserHomeSav/$UserId.OldMail" else echo echo "Removing empty mail queue for $UserId" rm "$MailQueue" fi fi else # Reply was n echo echo "Deleting all files for $UserId" rm -r "$UserHome" rm "$MailQueue" # Remove any cgi-bin stuff owned by this user: while read line do # Characters 9 and onward are the file (or directory) name. Filename=`echo "$line" | cut -c9-` rm -r "$CgiLoc/$Filename" done < "$CgiTmpFile" fi fi else echo echo "No home directory for $UserId." fi # Remove from the passwd file the line containing the matching entry: grep -v "^$UserId:" "$PasswdFile" > "$PasswdTmpFile" cp "$PasswdTmpFile" "$PasswdFile" rm "$PasswdTmpFile" sync echo echo "The passwd entry for $UserId has been removed" rm "$CgiTmpFile" exit 0