3 # Copyright 1995, 1996, 1997 Guy Helmer, Ames, Iowa 50014.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer as
11 # the first lines of this file unmodified.
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.
15 # 3. The name of the author may not be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY GUY HELMER ``AS IS'' AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL GUY HELMER BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # rmuser - Perl script to remove users
31 # Guy Helmer <ghelmer@cs.iastate.edu>, 02/23/97
41 $ENV{"PATH"} = "/bin:/sbin:/usr/bin:/usr/sbin";
44 $passwd_file = "/etc/master.passwd";
45 $new_passwd_file = "${passwd_file}.new.$$";
46 $group_file = "/etc/group";
47 $new_group_file = "${group_file}.new.$$";
48 $mail_dir = "/var/mail";
49 $crontab_dir = "/var/cron/tabs";
50 $atjob_dir = "/var/at/jobs";
58 print STDERR "Caught signal SIG$sig -- cleaning up.\n";
60 if (-e $new_passwd_file) {
61 unlink $new_passwd_file;
67 # Open the password file for reading
68 if (!open(MASTER_PW, "$passwd_file")) {
69 print STDERR "${whoami}: Error: Couldn't open ${passwd_file}: $!\n";
72 # Set the close-on-exec flag just in case
73 fcntl(MASTER_PW, &F_SETFD, 1);
74 # Apply an advisory lock the password file
75 if (!flock(MASTER_PW, &LOCK_EX|&LOCK_NB)) {
76 print STDERR "${whoami}: Error: Couldn't lock ${passwd_file}: $!\n";
82 flock(MASTER_PW, &LOCK_UN);
85 $SIG{'INT'} = 'cleanup';
86 $SIG{'QUIT'} = 'cleanup';
87 $SIG{'HUP'} = 'cleanup';
88 $SIG{'TERM'} = 'cleanup';
90 if ($#ARGV == 1 && $ARGV[0] eq '-y') {
96 print STDERR "usage: ${whoami} [-y] [username]\n";
101 print STDERR "${whoami}: Error: you must be root to use ${whoami}\n";
108 # Username was given as a parameter
109 $login_name = pop(@ARGV);
110 die "Sorry, login name must contain alphanumeric characters only.\n"
111 if ($login_name !~ /^[a-zA-Z0-9_]\w*$/);
114 print STDERR "${whoami}: Error: -y option given without username!\n";
118 # Get the user name from the user
119 $login_name = &get_login_name;
122 if (($pw_ent = &check_login_name($login_name)) eq '0') {
123 print STDERR "${whoami}: Error: User ${login_name} not in password database\n";
128 ($name, $password, $uid, $gid, $class, $change, $expire, $gecos, $home_dir,
129 $shell) = split(/:/, $pw_ent);
132 print "${whoami}: Error: I'd rather not remove a user with a uid of 0.\n";
138 print "Matching password entry:\n\n$pw_ent\n\n";
140 $ans = &get_yn("Is this the entry you wish to remove? ");
143 print "${whoami}: Informational: User ${login_name} not removed.\n";
150 # Get owner of user's home directory; don't remove home dir if not
151 # owned by $login_name
153 $remove_directory = 1;
156 $real_home_dir = &resolvelink($home_dir);
158 $real_home_dir = $home_dir;
162 # If home_dir is a symlink and points to something that isn't a directory,
163 # or if home_dir is not a symlink and is not a directory, don't remove
164 # home_dir -- seems like a good thing to do, but probably isn't necessary...
166 if (((-l $home_dir) && ((-e $real_home_dir) && !(-d $real_home_dir))) ||
167 (!(-l $home_dir) && !(-d $home_dir))) {
168 print STDERR "${whoami}: Informational: Home ${home_dir} is not a directory, so it won't be removed\n";
169 $remove_directory = 0;
172 if (length($real_home_dir) && -d $real_home_dir) {
173 $dir_owner = (stat($real_home_dir))[4]; # UID
174 if ($dir_owner != $uid) {
175 print STDERR "${whoami}: Informational: Home dir ${real_home_dir} is" .
176 " not owned by ${login_name} (uid ${dir_owner})\n," .
177 "\tso it won't be removed\n";
178 $remove_directory = 0;
182 if ($remove_directory && ! $affirm) {
183 $ans = &get_yn("Remove user's home directory ($home_dir)? ");
185 $remove_directory = 0;
192 # Remove the user's crontab, if there is one
193 # (probably needs to be done before password databases are updated)
195 if (-e "$crontab_dir/$login_name") {
196 print STDERR "Removing user's crontab:";
197 system('/usr/bin/crontab', '-u', $login_name, '-r');
198 print STDERR " done.\n";
202 # Remove the user's at jobs, if any
203 # (probably also needs to be done before password databases are updated)
205 &remove_at_jobs($login_name, $uid);
208 # Kill all the user's processes
210 &kill_users_processes($login_name, $uid);
213 # Copy master password file to new file less removed user's entry
218 # Remove the user from all groups in /etc/group
220 &update_group_file($login_name);
223 # Remove the user's home directory
225 if ($remove_directory) {
226 print STDERR "Removing user's home directory ($home_dir):";
227 &remove_dir($home_dir);
228 print STDERR " done.\n";
232 # Remove files related to the user from the mail directory
234 #&remove_files_from_dir($mail_dir, $login_name, $uid);
235 $file = "$mail_dir/$login_name";
236 if (-e $file || -l $file) {
237 print STDERR "Removing user's incoming mail file ${file}:";
239 print STDERR "\n${whoami}: Warning: unlink on $file failed ($!) - continuing\n";
240 print STDERR " done.\n";
244 # Remove some pop daemon's leftover file
246 $file = "$mail_dir/.${login_name}.pop";
247 if (-e $file || -l $file) {
248 print STDERR "Removing pop daemon's temporary mail file ${file}:";
250 print STDERR "\n${whoami}: Warning: unlink on $file failed ($!) - continuing\n";
251 print STDERR " done.\n";
255 # Remove files belonging to the user from the directories /tmp, /var/tmp,
256 # and /var/tmp/vi.recover. Note that this doesn't take care of the
257 # problem where a user may have directories or symbolic links in those
258 # directories -- only regular files are removed.
260 &remove_files_from_dir('/tmp', $login_name, $uid);
261 &remove_files_from_dir('/var/tmp', $login_name, $uid);
262 &remove_files_from_dir('/var/tmp/vi.recover', $login_name, $uid)
263 if (-e '/var/tmp/vi.recover');
272 # Get new user's name
273 local($done, $login_name);
275 for ($done = 0; ! $done; ) {
276 print "Enter login name for user to remove: ";
279 if (!($login_name =~ /^[a-z0-9_][a-z0-9_\-]*$/)) {
280 print STDERR "Sorry, login name must contain alphanumeric characters only.\n";
281 } elsif (length($login_name) > 16 || length($login_name) == 0) {
282 print STDERR "Sorry, login name must be 16 characters or less.\n";
288 print "User name is ${login_name}\n" if $debug;
292 sub check_login_name {
294 # Check to see whether login name is in password file
295 local($login_name) = @_;
296 local($Mname, $Mpassword, $Muid, $Mgid, $Mclass, $Mchange, $Mexpire,
297 $Mgecos, $Mhome_dir, $Mshell);
300 seek(MASTER_PW, 0, 0);
301 while ($i = <MASTER_PW>) {
303 ($Mname, $Mpassword, $Muid, $Mgid, $Mclass, $Mchange, $Mexpire,
304 $Mgecos, $Mhome_dir, $Mshell) = split(/:/, $i);
305 if ($Mname eq $login_name) {
306 seek(MASTER_PW, 0, 0);
307 return($i); # User is in password database
310 seek(MASTER_PW, 0, 0);
312 return '0'; # User wasn't found
317 # Get a yes or no answer; return 'Y' or 'N'
321 for ($done = 0; ! $done; ) {
326 if (!($ans =~ /^[YN]/)) {
327 print STDERR "Please answer (y)es or (n)o.\n";
333 return(substr($ans, 0, 1));
336 sub update_passwd_file {
339 print STDERR "Updating password file,";
340 seek(MASTER_PW, 0, 0);
341 open(NEW_PW, ">$new_passwd_file") ||
342 die "\n${whoami}: Error: Couldn't open file ${new_passwd_file}:\n $!\n";
343 chmod(0600, $new_passwd_file) ||
344 print STDERR "\n${whoami}: Warning: couldn't set mode of $new_passwd_file to 0600 ($!)\n\tcontinuing, but please check mode of /etc/master.passwd!\n";
346 while ($i = <MASTER_PW>) {
353 print STDERR "Dropped entry for $login_name\n" if $debug;
358 seek(MASTER_PW, 0, 0);
361 print STDERR "\n${whoami}: Whoops! Didn't find ${login_name}'s entry second time around!\n";
362 unlink($new_passwd_file) ||
363 print STDERR "\n${whoami}: Warning: couldn't unlink $new_passwd_file ($!)\n\tPlease investigate, as this file should not be left in the filesystem\n";
369 # Run pwd_mkdb to install the updated password files and databases
371 print STDERR " updating databases,";
372 system('/usr/sbin/pwd_mkdb', '-p', ${new_passwd_file});
373 print STDERR " done.\n";
375 close(MASTER_PW); # Not useful anymore
378 sub update_group_file {
379 local($login_name) = @_;
381 local($i, $j, $grmember_list, $new_grent, $changes);
382 local($grname, $grpass, $grgid, $grmember_list, @grmembers);
385 print STDERR "Updating group file:";
386 open(GROUP, $group_file) ||
387 die "\n${whoami}: Error: couldn't open ${group_file}: $!\n";
388 if (!flock(GROUP, &LOCK_EX|&LOCK_NB)) {
389 print STDERR "\n${whoami}: Error: couldn't lock ${group_file}: $!\n";
392 local($group_perms, $group_uid, $group_gid) =
393 (stat(GROUP))[2, 4, 5]; # File Mode, uid, gid
394 open(NEW_GROUP, ">$new_group_file") ||
395 die "\n${whoami}: Error: couldn't open ${new_group_file}: $!\n";
396 chmod($group_perms, $new_group_file) ||
397 printf STDERR "\n${whoami}: Warning: could not set permissions of new group file to %o ($!)\n\tContinuing, but please check permissions of $group_file!\n", $group_perms;
398 chown($group_uid, $group_gid, $new_group_file) ||
399 print STDERR "\n${whoami}: Warning: could not set owner/group of new group file to ${group_uid}/${group_gid} ($!)\n\rContinuing, but please check ownership of $group_file!\n";
400 while ($i = <GROUP>) {
401 if (!($i =~ /$login_name/)) {
402 # Line doesn't contain any references to the user, so just add it
407 # Remove the user from the group
411 ($grname, $grpass, $grgid, $grmember_list) = split(/:/, $i);
412 @grmembers = split(/,/, $grmember_list);
413 undef @new_grmembers;
414 local(@new_grmembers);
415 foreach $j (@grmembers) {
416 if ($j ne $login_name) {
417 push(@new_grmembers, $j);
419 print STDERR " $grname";
423 if ($grname eq $login_name && $#new_grmembers == -1) {
424 # Remove a user's personal group if empty
425 print STDERR " (removing group $grname -- personal group is empty)";
428 $grmember_list = join(',', @new_grmembers);
429 $new_grent = join(':', $grname, $grpass, $grgid, $grmember_list);
430 print NEW_GROUP "$new_grent\n";
435 rename($new_group_file, $group_file) || # Replace old group file with new
436 die "\n${whoami}: Error: couldn't rename $new_group_file to $group_file ($!)\n";
437 close(GROUP); # File handle is worthless now
438 print STDERR " (no changes)" if (! $changes);
439 print STDERR " done.\n";
443 # Remove the user's home directory
448 $linkdir = &resolvelink($dir);
449 # Remove the symbolic link
451 warn "${whoami}: Warning: could not unlink symlink $dir: $!\n";
452 if (!(-e $linkdir)) {
454 # Dangling symlink - just return now
457 # Set dir to be the resolved pathname
461 print STDERR "${whoami}: Warning: $dir is not a directory\n";
462 unlink($dir) || warn "${whoami}: Warning: could not unlink $dir: $!\n";
465 system('/bin/rm', '-rf', $dir);
468 sub remove_files_from_dir {
469 local($dir, $login_name, $uid) = @_;
470 local($path, $i, $owner);
472 print STDERR "Removing files belonging to ${login_name} from ${dir}:";
474 if (!opendir(DELDIR, $dir)) {
475 print STDERR "\n${whoami}: Warning: couldn't open directory ${dir} ($!)\n";
478 while ($i = readdir(DELDIR)) {
482 $owner = (stat("$dir/$i"))[4]; # UID
483 if ($uid == $owner) {
487 print STDERR "\n${whoami}: Warning: unlink on ${dir}/${i} failed ($!) - continuing\n";
489 print STDERR " ($i not a regular file - skipped)";
495 printf STDERR " done.\n";
499 local($login_name, $uid) = @_;
500 local($i, $owner, $found);
503 opendir(ATDIR, $atjob_dir) || return;
504 while ($i = readdir(ATDIR)) {
507 next if $i eq '.lockfile';
509 $owner = (stat("$atjob_dir/$i"))[4]; # UID
510 if ($uid == $owner) {
512 print STDERR "Removing user's at jobs:";
515 # Use atrm to remove the job
517 system('/usr/bin/atrm', $i);
522 print STDERR " done.\n";
530 while (-l $path && -e $path) {
531 if (!defined($l = readlink($path))) {
532 die "${whoami}: readlink on $path failed (but it should have worked!): $!\n";
539 $path =~ s/\/[^\/]+\/?$/\/$l/; # Replace last component of path
545 sub kill_users_processes {
546 local($login_name, $uid) = @_;
547 local($pid, $result);
550 # Do something a little complex: fork a child that changes its
551 # real and effective UID to that of the removed user, then issues
552 # a "kill(9, -1)" to kill all processes of the same uid as the sender
553 # (see kill(2) for details).
554 # The parent waits for the exit of the child and then returns.
559 } elsif (defined $pid) {
563 if ($< != $uid || $> != $uid) {
564 print STDERR "${whoami}: Error (kill_users_processes):\n" .
565 "\tCouldn't reset uid/euid to ${uid}: current uid/euid's are $< and $>\n";
568 $result = kill(9, -1);
569 print STDERR "Killed process(es) belonging to $login_name.\n"
574 print STDERR "${whoami}: Error: couldn't fork to kill ${login_name}'s processes - continuing\n";