#!/bin/sh
#
-# Copyright (c) 2002 Michael Telahun Makonnen. All rights reserved.
+# Copyright (c) 2002, 2003 Michael Telahun Makonnen. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
-# 3. The name of the author may not be used to endorse or promote products
-# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
-# Email: Mike Makonnen <mtm@identd.net>
+# Email: Mike Makonnen <mtm@FreeBSD.Org>
#
# $FreeBSD$
#
SIGKILL="-KILL"
TEMPDIRS="/tmp /var/tmp"
THISCMD=`/usr/bin/basename $0`
+PWCMD="${PWCMD:-/usr/sbin/pw}"
# err msg
# Display $msg on stderr.
echo 1>&2 ${THISCMD}: $*
}
+# verbose
+# Returns 0 if verbose mode is set, 1 if it is not.
+#
+verbose() {
+ [ -n "$vflag" ] && return 0 || return 1
+}
+
# rm_files login
# Removes files or empty directories belonging to $login from various
# temporary directories.
rm_files() {
# The argument is required
[ -n $1 ] && login=$1 || return
-
+
+ totalcount=0
for _dir in ${TEMPDIRS} ; do
+ filecount=0
if [ ! -d $_dir ]; then
err "$_dir is not a valid directory."
continue
fi
- echo -n "Removing files owned by ($login) in $_dir:"
- filecount=`find 2>/dev/null "$_dir" -user "$login" -delete -print | \
+ verbose && echo -n "Removing files owned by ($login) in $_dir:"
+ filecount=`find 2>/dev/null "$_dir" -user "$login" -delete -print |
wc -l | sed 's/ *//'`
- echo " $filecount removed."
+ verbose && echo " $filecount removed."
+ totalcount=$(($totalcount + $filecount))
done
+ ! verbose && [ $totalcount -ne 0 ] && echo -n " files($totalcount)"
}
# rm_mail login
# The argument is required
[ -n $1 ] && login=$1 || return
- echo -n "Removing mail spool(s) for ($login):"
+ verbose && echo -n "Removing mail spool(s) for ($login):"
if [ -f ${MAILSPOOL}/$login ]; then
- echo -n " ${MAILSPOOL}/$login"
+ verbose && echo -n " ${MAILSPOOL}/$login" ||
+ echo -n " mailspool"
rm ${MAILSPOOL}/$login
fi
- if [ -f ${MAILSPOOL}/${login}.pop ]; then
- echo -n " ${MAILSPOOL}/${login}.pop"
- rm ${MAILSPOOL}/${login}.pop
+ if [ -f ${MAILSPOOL}/.${login}.pop ]; then
+ verbose && echo -n " ${MAILSPOOL}/.${login}.pop" ||
+ echo -n " pop3"
+ rm ${MAILSPOOL}/.${login}.pop
fi
- echo '.'
+ verbose && echo '.'
}
# kill_procs login
# The argument is required
[ -n $1 ] && login=$1 || return
- echo -n "Terminating all processes owned by ($login):"
+ verbose && echo -n "Terminating all processes owned by ($login):"
killcount=0
proclist=`ps 2>/dev/null -U $login | grep -v '^\ *PID' | awk '{print $1}'`
for _pid in $proclist ; do
kill 2>/dev/null ${SIGKILL} $_pid
killcount=$(($killcount + 1))
done
- echo " ${SIGKILL} signal sent to $killcount processes."
+ verbose && echo " ${SIGKILL} signal sent to $killcount processes."
+ ! verbose && [ $killcount -ne 0 ] && echo -n " processes(${killcount})"
}
# rm_at_jobs login
atjoblist=`find 2>/dev/null ${ATJOBDIR} -maxdepth 1 -user $login -print`
jobcount=0
- echo -n "Removing at(1) jobs owned by ($login):"
+ verbose && echo -n "Removing at(1) jobs owned by ($login):"
for _atjob in $atjoblist ; do
rm -f $_atjob
jobcount=$(($jobcount + 1))
done
- echo " $jobcount removed."
+ verbose && echo " $jobcount removed."
+ ! verbose && [ $jobcount -ne 0 ] && echo -n " at($jobcount)"
}
# rm_crontab login
# The argument is required
[ -n $1 ] && login=$1 || return
- echo -n "Removing crontab for ($login):"
+ verbose && echo -n "Removing crontab for ($login):"
if [ -f ${CRONJOBDIR}/$login ]; then
- echo -n " ${CRONJOBDIR}/$login"
+ verbose && echo -n " ${CRONJOBDIR}/$login" || echo -n " crontab"
rm -f ${CRONJOBDIR}/$login
fi
- echo '.'
+ verbose && echo '.'
+}
+
+# rm_ipc login
+# Remove all IPC mechanisms which are owned by $login.
+#
+rm_ipc() {
+ verbose && echo -n "Removing IPC mechanisms"
+ for i in s m q; do
+ ipcs -$i |
+ awk -v i=$i -v login=$1 '$1 == i && $5 == login { print $2 }' |
+ xargs -n 1 ipcrm -$i
+ done
+ verbose && echo '.'
}
# rm_user login
# The argument is required
[ -n $1 ] && login=$1 || return
- echo -n "Removing user ($login)"
- [ -n "$pw_rswitch" ] && echo -n " (including home directory)"
- echo -n " from the system:"
- pw userdel -n $login $pw_rswitch
- echo ' Done.'
+ verbose && echo -n "Removing user ($login)"
+ [ -n "$pw_rswitch" ] && {
+ verbose && echo -n " (including home directory)"
+ ! verbose && echo -n " home"
+ }
+ ! verbose && echo -n " passwd"
+ verbose && echo -n " from the system:"
+ ${PWCMD} userdel -n $login $pw_rswitch
+ verbose && echo ' Done.'
}
# prompt_yesno msg
# Display usage message.
#
show_usage() {
- echo "usage: ${THISCMD} [-y] [-f file] [user ...]"
+ echo "usage: ${THISCMD} [-yv] [-f file] [user ...]"
echo " if the -y switch is used, either the -f switch or"
echo " one or more user names must be given"
}
pw_rswitch=
userlist=
yflag=
+vflag=
procowner=`/usr/bin/id -u`
if [ "$procowner" != "0" ]; then
exit 1
fi
-args=`getopt 2>/dev/null yf: $*`
+args=`getopt 2>/dev/null yvf: $*`
if [ "$?" != "0" ]; then
show_usage
exit 1
yflag=1
shift
;;
+ -v)
+ vflag=1
+ shift
+ ;;
-f)
fflag=1
ffile="$2"
show_usage
exit 1
else
- echo -n "Please enter one or more user name's: "
+ echo -n "Please enter one or more usernames: "
read userlist
fi
fi
fi
# Disable any further attempts to log into this account
- pw 2>/dev/null lock $_user
+ ${PWCMD} 2>/dev/null lock $_user
# Remove crontab, mail spool, etc. Then obliterate the user from
# the passwd and group database.
+ #
+ ! verbose && echo -n "Removing user ($_user):"
rm_crontab $_user
rm_at_jobs $_user
+ rm_ipc $_user
kill_procs $_user
- rm_mail $_user
rm_files $_user
+ rm_mail $_user
rm_user $_user
+ ! verbose && echo "."
done