#!/bin/bash ## deleteuser--Deletes a user account without a trace... # Not for use with OS X setglobal homedir = '"/home'" setglobal pwfile = '"/etc/passwd'" setglobal shadow = '"/etc/shadow'" setglobal newpwfile = '"/etc/passwd.new'" setglobal newshadow = '"/etc/shadow.new'" setglobal locker = '"/etc/passwd.lock'" if test -z $1 { echo "Usage: $0 account" > !2; exit 1 } elif test $[whoami] != "root" { echo "Error: you must be 'root' to run this command."> !2; exit 1 } suspenduser $1 # Suspend their account while we do the dirty work. setglobal uid = $[grep -E "^$(1):" $pwfile | cut -d: -f3] if test -z $uid { echo "Error: no account $1 found in $pwfile" > !2; exit 1 } # Remove from the password and shadow files. grep -vE "^$(1):" $pwfile > $newpwfile grep -vE "^$(1):" $shadow > $newshadow setglobal lockcmd = $[which lockfile] # Find lockfile app in the path. if test ! -z $lockcmd { # let's use the system lockfile eval $lockcmd -r 15 $locker } else { # Ulp, let's do it ourselves. while test -e $locker { echo "waiting for the password file" ; sleep 1 } touch $locker # created a file-based lock } mv $newpwfile $pwfile mv $newshadow $shadow rm -f $locker # click! unlocked again chmod 644 $pwfile chmod 400 $shadow # Now remove home directory and list anything left... rm -rf $homedir/$1 echo "Files still left to remove (if any):" find / -uid $uid -print !2 >/dev/null | sed 's/^/ /' echo "" echo "Account $1 (uid $uid) has been deleted, and their home directory " echo "($homedir/$1) has been removed." exit 0