]>
git.cameronkatri.com Git - pw-darwin.git/blob - adduser/rmuser.sh
abaf004e7f6699962c7b8b675f76f9c7c6741b0d
3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2002, 2003 Michael Telahun Makonnen. All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 # Email: Mike Makonnen <mtm@FreeBSD.Org>
32 ATJOBDIR
="/var/at/jobs"
33 CRONJOBDIR
="/var/cron/tabs"
36 TEMPDIRS
="/tmp /var/tmp"
37 THISCMD
=`/usr/bin/basename $0`
38 PWCMD
="${PWCMD:-/usr/sbin/pw}"
41 # Display $msg on stderr.
44 echo 1>&2 ${THISCMD}: $
*
48 # Returns 0 if verbose mode is set, 1 if it is not.
51 [ -n "$vflag" ] && return 0 || return 1
55 # Removes files or empty directories belonging to $login from various
56 # temporary directories.
59 # The argument is required
60 [ -n $1 ] && login
=$1 || return
63 for _dir
in ${TEMPDIRS} ; do
65 if [ ! -d $_dir ]; then
66 err
"$_dir is not a valid directory."
69 verbose
&& echo -n "Removing files owned by ($login) in $_dir:"
70 filecount
=`find 2>/dev/null "$_dir" -user "$login" -delete -print |
72 verbose
&& echo " $filecount removed."
73 totalcount
=$(($totalcount + $filecount))
75 ! verbose
&& [ $totalcount -ne 0 ] && echo -n " files($totalcount)"
79 # Removes unix mail and pop daemon files belonging to the user
80 # specified in the $login argument.
83 # The argument is required
84 [ -n $1 ] && login
=$1 || return
86 verbose
&& echo -n "Removing mail spool(s) for ($login):"
87 if [ -f ${MAILSPOOL}/$login ]; then
88 verbose
&& echo -n " ${MAILSPOOL}/$login" ||
90 rm ${MAILSPOOL}/$login
92 if [ -f ${MAILSPOOL}/.
${login}.pop
]; then
93 verbose
&& echo -n " ${MAILSPOOL}/.${login}.pop" ||
95 rm ${MAILSPOOL}/.
${login}.pop
101 # Send a SIGKILL to all processes owned by $login.
104 # The argument is required
105 [ -n $1 ] && login
=$1 || return
107 verbose
&& echo -n "Terminating all processes owned by ($login):"
109 proclist
=`ps 2>/dev/null -U $login | grep -v '^\ *PID' | awk '{print $1}'`
110 for _pid
in $proclist ; do
111 kill 2>/dev
/null
${SIGKILL} $_pid
112 killcount
=$(($killcount + 1))
114 verbose
&& echo " ${SIGKILL} signal sent to $killcount processes."
115 ! verbose
&& [ $killcount -ne 0 ] && echo -n " processes(${killcount})"
119 # Remove at (1) jobs belonging to $login.
122 # The argument is required
123 [ -n $1 ] && login
=$1 || return
125 atjoblist
=`find 2>/dev/null ${ATJOBDIR} -maxdepth 1 -user $login -print`
127 verbose
&& echo -n "Removing at(1) jobs owned by ($login):"
128 for _atjob
in $atjoblist ; do
130 jobcount
=$(($jobcount + 1))
132 verbose
&& echo " $jobcount removed."
133 ! verbose
&& [ $jobcount -ne 0 ] && echo -n " at($jobcount)"
137 # Removes crontab file belonging to user $login.
140 # The argument is required
141 [ -n $1 ] && login
=$1 || return
143 verbose
&& echo -n "Removing crontab for ($login):"
144 if [ -f ${CRONJOBDIR}/$login ]; then
145 verbose
&& echo -n " ${CRONJOBDIR}/$login" || echo -n " crontab"
146 rm -f ${CRONJOBDIR}/$login
152 # Remove all IPC mechanisms which are owned by $login.
155 verbose
&& echo -n "Removing IPC mechanisms"
158 awk -v i
=$i -v login
=$1 '$1 == i && $5 == login { print $2 }' |
165 # Remove user $login from the system. This subroutine makes use
166 # of the pw(8) command to remove a user from the system. The pw(8)
167 # command will remove the specified user from the user database
168 # and group file and remove any crontabs. His home
169 # directory will be removed if it is owned by him and contains no
170 # files or subdirectories owned by other users. Mail spool files will
174 # The argument is required
175 [ -n $1 ] && login
=$1 || return
177 verbose
&& echo -n "Removing user ($login)"
178 [ -n "$pw_rswitch" ] && {
179 verbose
&& echo -n " (including home directory)"
180 ! verbose
&& echo -n " home"
182 ! verbose
&& echo -n " passwd"
183 verbose
&& echo -n " from the system:"
184 ${PWCMD} userdel
-n $login $pw_rswitch
185 verbose
&& echo ' Done.'
189 # Prompts the user with a $msg. The answer is expected to be
190 # yes, no, or some variation thereof. This subroutine returns 0
191 # if the answer was yes, 1 if it was not.
194 # The argument is required
195 [ -n "$1" ] && msg
="$1" || return
204 [Yy
][Ee
][Ss
]|[Yy
][Ee
]|[Yy
])
215 # Display usage message.
218 echo "usage: ${THISCMD} [-yv] [-f file] [user ...]"
219 echo " if the -y switch is used, either the -f switch or"
220 echo " one or more user names must be given"
223 #### END SUBROUTINE DEFENITION ####
233 procowner
=`/usr/bin/id -u`
234 if [ "$procowner" != "0" ]; then
235 err
'you must be root (0) to use this utility.'
239 args
=`getopt 2>/dev/null yvf: $*`
240 if [ "$?" != "0" ]; then
267 # Get user names from a file if the -f switch was used. Otherwise,
268 # get them from the commandline arguments. If we're getting it
269 # from a file, the file must be owned by and writable only by root.
272 _insecure
=`find $ffile ! -user 0 -or -perm +0022`
273 if [ -n "$_insecure" ]; then
274 err
"file ($ffile) must be owned by and writeable only by root."
277 if [ -r "$ffile" ]; then
278 userlist
=`cat $ffile | while read _user _junk ; do
283 echo -n "$userlist $_user"
290 userlist
="$userlist $1"
295 # If the -y or -f switch has been used and the list of users to remove
296 # is empty it is a fatal error. Otherwise, prompt the user for a list
297 # of one or more user names.
299 if [ ! "$userlist" ]; then
301 err
"($ffile) does not exist or does not contain any user names."
303 elif [ $yflag ]; then
307 echo -n "Please enter one or more usernames: "
314 for _user
in $userlist ; do
315 # Make sure the name exists in the passwd database and that it
316 # does not have a uid of 0
318 userrec
=`pw 2>/dev/null usershow -n $_user`
319 if [ "$?" != "0" ]; then
320 err
"user ($_user) does not exist in the password database."
323 _uid
=`echo $userrec | awk -F: '{print $3}'`
324 if [ "$_uid" = "0" ]; then
325 err
"user ($_user) has uid 0. You may not remove this user."
329 # If the -y switch was not used ask for confirmation to remove the
330 # user and home directory.
332 if [ -z "$yflag" ]; then
333 echo "Matching password entry:"
337 if ! prompt_yesno
"Is this the entry you wish to remove? " ; then
340 _homedir
=`echo $userrec | awk -F: '{print $9}'`
341 if prompt_yesno
"Remove user's home directory ($_homedir)? "; then
348 # Disable any further attempts to log into this account
349 ${PWCMD} 2>/dev
/null lock
$_user
351 # Remove crontab, mail spool, etc. Then obliterate the user from
352 # the passwd and group database.
354 ! verbose
&& echo -n "Removing user ($_user):"
362 ! verbose
&& echo "."