]> git.cameronkatri.com Git - pw-darwin.git/blob - adduser/rmuser.perl
o Rename "namespace" argument to "attrnamespace" as namespace is a C++
[pw-darwin.git] / adduser / rmuser.perl
1 #!/usr/bin/perl
2 # -*- perl -*-
3 # Copyright 1995, 1996, 1997 Guy Helmer, Ames, Iowa 50014.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
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.
17 #
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.
28 #
29 # rmuser - Perl script to remove users
30 #
31 # Guy Helmer <ghelmer@cs.iastate.edu>, 02/23/97
32 #
33 # $FreeBSD$
34
35 sub LOCK_SH {0x01;}
36 sub LOCK_EX {0x02;}
37 sub LOCK_NB {0x04;}
38 sub LOCK_UN {0x08;}
39 sub F_SETFD {2;}
40
41 $ENV{"PATH"} = "/bin:/sbin:/usr/bin:/usr/sbin";
42 umask(022);
43 $whoami = $0;
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";
51 $affirm = 0;
52
53 #$debug = 1;
54
55 sub cleanup {
56 local($sig) = @_;
57
58 print STDERR "Caught signal SIG$sig -- cleaning up.\n";
59 &unlockpw;
60 if (-e $new_passwd_file) {
61 unlink $new_passwd_file;
62 }
63 exit(0);
64 }
65
66 sub lockpw {
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";
70 exit(1);
71 }
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";
77 exit(1);
78 }
79 }
80
81 sub unlockpw {
82 flock(MASTER_PW, &LOCK_UN);
83 }
84
85 $SIG{'INT'} = 'cleanup';
86 $SIG{'QUIT'} = 'cleanup';
87 $SIG{'HUP'} = 'cleanup';
88 $SIG{'TERM'} = 'cleanup';
89
90 if ($#ARGV == 1 && $ARGV[0] eq '-y') {
91 shift @ARGV;
92 $affirm = 1;
93 }
94
95 if ($#ARGV > 0) {
96 print STDERR "usage: ${whoami} [-y] [username]\n";
97 exit(1);
98 }
99
100 if ($< != 0) {
101 print STDERR "${whoami}: Error: you must be root to use ${whoami}\n";
102 exit(1);
103 }
104
105 &lockpw;
106
107 if ($#ARGV == 0) {
108 # Username was given as a parameter
109 $login_name = pop(@ARGV);
110 } else {
111 if ($affirm) {
112 print STDERR "${whoami}: Error: -y option given without username!\n";
113 &unlockpw;
114 exit 1;
115 }
116 # Get the user name from the user
117 $login_name = &get_login_name;
118 }
119
120 ($name, $password, $uid, $gid, $change, $class, $gecos, $home_dir, $shell) =
121 (getpwnam("$login_name"));
122
123 if ($?) {
124 print STDERR "${whoami}: Error: User ${login_name} not in password database\n";
125 &unlockpw;
126 exit 1;
127 }
128
129 if ($uid == 0) {
130 print "${whoami}: Error: I'd rather not remove a user with a uid of 0.\n";
131 &unlockpw;
132 exit 1;
133 }
134
135 if (! $affirm) {
136 print "Matching password entry:\n\n$name\:$password\:$uid\:$gid\:$class\:$change\:0\:$gecos\:$home_dir\:$shell\n\n";
137
138 $ans = &get_yn("Is this the entry you wish to remove? ");
139
140 if ($ans eq 'N') {
141 print "${whoami}: Informational: User ${login_name} not removed.\n";
142 &unlockpw;
143 exit 0;
144 }
145 }
146
147 #
148 # Get owner of user's home directory; don't remove home dir if not
149 # owned by $login_name
150
151 $remove_directory = 1;
152
153 if (-l $home_dir) {
154 $real_home_dir = &resolvelink($home_dir);
155 } else {
156 $real_home_dir = $home_dir;
157 }
158
159 #
160 # If home_dir is a symlink and points to something that isn't a directory,
161 # or if home_dir is not a symlink and is not a directory, don't remove
162 # home_dir -- seems like a good thing to do, but probably isn't necessary...
163
164 if (((-l $home_dir) && ((-e $real_home_dir) && !(-d $real_home_dir))) ||
165 (!(-l $home_dir) && !(-d $home_dir))) {
166 print STDERR "${whoami}: Informational: Home ${home_dir} is not a directory, so it won't be removed\n";
167 $remove_directory = 0;
168 }
169
170 if (length($real_home_dir) && -d $real_home_dir) {
171 $dir_owner = (stat($real_home_dir))[4]; # UID
172 if ($dir_owner != $uid) {
173 print STDERR "${whoami}: Informational: Home dir ${real_home_dir} is" .
174 " not owned by ${login_name} (uid ${dir_owner})\n," .
175 "\tso it won't be removed\n";
176 $remove_directory = 0;
177 }
178 }
179
180 if ($remove_directory && ! $affirm) {
181 $ans = &get_yn("Remove user's home directory ($home_dir)? ");
182 if ($ans eq 'N') {
183 $remove_directory = 0;
184 }
185 }
186
187 #exit 0 if $debug;
188
189 #
190 # Remove the user's crontab, if there is one
191 # (probably needs to be done before password databases are updated)
192
193 if (-e "$crontab_dir/$login_name") {
194 print STDERR "Removing user's crontab:";
195 system('/usr/bin/crontab', '-u', $login_name, '-r');
196 print STDERR " done.\n";
197 }
198
199 #
200 # Remove the user's at jobs, if any
201 # (probably also needs to be done before password databases are updated)
202
203 &remove_at_jobs($login_name, $uid);
204
205 #
206 # Kill all the user's processes
207
208 &kill_users_processes($login_name, $uid);
209
210 #
211 # Copy master password file to new file less removed user's entry
212
213 &update_passwd_file;
214
215 #
216 # Remove the user from all groups in /etc/group
217
218 &update_group_file($login_name);
219
220 #
221 # Remove the user's home directory
222
223 if ($remove_directory) {
224 print STDERR "Removing user's home directory ($home_dir):";
225 &remove_dir($home_dir);
226 print STDERR " done.\n";
227 }
228
229 #
230 # Remove files related to the user from the mail directory
231
232 #&remove_files_from_dir($mail_dir, $login_name, $uid);
233 $file = "$mail_dir/$login_name";
234 if (-e $file || -l $file) {
235 print STDERR "Removing user's incoming mail file ${file}:";
236 unlink $file ||
237 print STDERR "\n${whoami}: Warning: unlink on $file failed ($!) - continuing\n";
238 print STDERR " done.\n";
239 }
240
241 #
242 # Remove some pop daemon's leftover file
243
244 $file = "$mail_dir/.${login_name}.pop";
245 if (-e $file || -l $file) {
246 print STDERR "Removing pop daemon's temporary mail file ${file}:";
247 unlink $file ||
248 print STDERR "\n${whoami}: Warning: unlink on $file failed ($!) - continuing\n";
249 print STDERR " done.\n";
250 }
251
252 #
253 # Remove files belonging to the user from the directories /tmp, /var/tmp,
254 # and /var/tmp/vi.recover. Note that this doesn't take care of the
255 # problem where a user may have directories or symbolic links in those
256 # directories -- only regular files are removed.
257
258 &remove_files_from_dir('/tmp', $login_name, $uid);
259 &remove_files_from_dir('/var/tmp', $login_name, $uid);
260 &remove_files_from_dir('/var/tmp/vi.recover', $login_name, $uid)
261 if (-e '/var/tmp/vi.recover');
262
263 #
264 # All done!
265
266 exit 0;
267
268 sub get_login_name {
269 #
270 # Get new user's name
271 local($done, $login_name);
272
273 for ($done = 0; ! $done; ) {
274 print "Enter login name for user to remove: ";
275 $login_name = <>;
276 chomp $login_name;
277 if (not getpwnam("$login_name")) {
278 print STDERR "Sorry, login name not in password database.\n";
279 } else {
280 $done = 1;
281 }
282 }
283
284 print "User name is ${login_name}\n" if $debug;
285 return($login_name);
286 }
287
288 sub get_yn {
289 #
290 # Get a yes or no answer; return 'Y' or 'N'
291 local($prompt) = @_;
292 local($done, $ans);
293
294 for ($done = 0; ! $done; ) {
295 print $prompt;
296 $ans = <>;
297 chop $ans;
298 $ans =~ tr/a-z/A-Z/;
299 if (!($ans =~ /^[YN]/)) {
300 print STDERR "Please answer (y)es or (n)o.\n";
301 } else {
302 $done = 1;
303 }
304 }
305
306 return(substr($ans, 0, 1));
307 }
308
309 sub update_passwd_file {
310 local($skipped);
311
312 print STDERR "Updating password file,";
313 seek(MASTER_PW, 0, 0);
314 open(NEW_PW, ">$new_passwd_file") ||
315 die "\n${whoami}: Error: Couldn't open file ${new_passwd_file}:\n $!\n";
316 chmod(0600, $new_passwd_file) ||
317 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";
318 $skipped = 0;
319 while (<MASTER_PW>) {
320 if (not /^\Q$login_name:/io) {
321 print NEW_PW;
322 } else {
323 print STDERR "Dropped entry for $login_name\n" if $debug;
324 $skipped = 1;
325 }
326 }
327 close(NEW_PW);
328 seek(MASTER_PW, 0, 0);
329
330 if ($skipped == 0) {
331 print STDERR "\n${whoami}: Whoops! Didn't find ${login_name}'s entry second time around!\n";
332 unlink($new_passwd_file) ||
333 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";
334 &unlockpw;
335 exit 1;
336 }
337
338 #
339 # Run pwd_mkdb to install the updated password files and databases
340
341 print STDERR " updating databases,";
342 system('/usr/sbin/pwd_mkdb', '-p', ${new_passwd_file});
343 print STDERR " done.\n";
344
345 close(MASTER_PW); # Not useful anymore
346 }
347
348 sub update_group_file {
349 local($login_name) = @_;
350
351 local($i, $j, $grmember_list, $new_grent, $changes);
352 local($grname, $grpass, $grgid, $grmember_list, @grmembers);
353
354 $changes = 0;
355 print STDERR "Updating group file:";
356 open(GROUP, $group_file) ||
357 die "\n${whoami}: Error: couldn't open ${group_file}: $!\n";
358 if (!flock(GROUP, &LOCK_EX|&LOCK_NB)) {
359 print STDERR "\n${whoami}: Error: couldn't lock ${group_file}: $!\n";
360 exit 1;
361 }
362 local($group_perms, $group_uid, $group_gid) =
363 (stat(GROUP))[2, 4, 5]; # File Mode, uid, gid
364 open(NEW_GROUP, ">$new_group_file") ||
365 die "\n${whoami}: Error: couldn't open ${new_group_file}: $!\n";
366 chmod($group_perms, $new_group_file) ||
367 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;
368 chown($group_uid, $group_gid, $new_group_file) ||
369 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";
370 while ($i = <GROUP>) {
371 if (!($i =~ /$login_name/)) {
372 # Line doesn't contain any references to the user, so just add it
373 # to the new file
374 print NEW_GROUP $i;
375 } else {
376 #
377 # Remove the user from the group
378 if ($i =~ /\n$/) {
379 chop $i;
380 }
381 ($grname, $grpass, $grgid, $grmember_list) = split(/:/, $i);
382 @grmembers = split(/,/, $grmember_list);
383 undef @new_grmembers;
384 local(@new_grmembers);
385 foreach $j (@grmembers) {
386 if ($j ne $login_name) {
387 push(@new_grmembers, $j);
388 } else {
389 print STDERR " $grname";
390 $changes = 1;
391 }
392 }
393 if ($grname eq $login_name && $#new_grmembers == -1) {
394 # Remove a user's personal group if empty
395 print STDERR " (removing group $grname -- personal group is empty)";
396 $changes = 1;
397 } else {
398 $grmember_list = join(',', @new_grmembers);
399 $new_grent = join(':', $grname, $grpass, $grgid, $grmember_list);
400 print NEW_GROUP "$new_grent\n";
401 }
402 }
403 }
404 close(NEW_GROUP);
405 rename($new_group_file, $group_file) || # Replace old group file with new
406 die "\n${whoami}: Error: couldn't rename $new_group_file to $group_file ($!)\n";
407 close(GROUP); # File handle is worthless now
408 print STDERR " (no changes)" if (! $changes);
409 print STDERR " done.\n";
410 }
411
412 sub remove_dir {
413 # Remove the user's home directory
414 local($dir) = @_;
415 local($linkdir);
416
417 if (-l $dir) {
418 $linkdir = &resolvelink($dir);
419 # Remove the symbolic link
420 unlink($dir) ||
421 warn "${whoami}: Warning: could not unlink symlink $dir: $!\n";
422 if (!(-e $linkdir)) {
423 #
424 # Dangling symlink - just return now
425 return;
426 }
427 # Set dir to be the resolved pathname
428 $dir = $linkdir;
429 }
430 if (!(-d $dir)) {
431 print STDERR "${whoami}: Warning: $dir is not a directory\n";
432 unlink($dir) || warn "${whoami}: Warning: could not unlink $dir: $!\n";
433 return;
434 }
435 system('/bin/rm', '-rf', $dir);
436 }
437
438 sub remove_files_from_dir {
439 local($dir, $login_name, $uid) = @_;
440 local($path, $i, $owner);
441
442 print STDERR "Removing files belonging to ${login_name} from ${dir}:";
443
444 if (!opendir(DELDIR, $dir)) {
445 print STDERR "\n${whoami}: Warning: couldn't open directory ${dir} ($!)\n";
446 return;
447 }
448 while ($i = readdir(DELDIR)) {
449 next if $i eq '.';
450 next if $i eq '..';
451
452 $owner = (stat("$dir/$i"))[4]; # UID
453 if ($uid == $owner) {
454 if (-f "$dir/$i") {
455 print STDERR " $i";
456 unlink "$dir/$i" ||
457 print STDERR "\n${whoami}: Warning: unlink on ${dir}/${i} failed ($!) - continuing\n";
458 } else {
459 print STDERR " ($i not a regular file - skipped)";
460 }
461 }
462 }
463 closedir(DELDIR);
464
465 printf STDERR " done.\n";
466 }
467
468 sub remove_at_jobs {
469 local($login_name, $uid) = @_;
470 local($i, $owner, $found);
471
472 $found = 0;
473 opendir(ATDIR, $atjob_dir) || return;
474 while ($i = readdir(ATDIR)) {
475 next if $i eq '.';
476 next if $i eq '..';
477 next if $i eq '.lockfile';
478
479 $owner = (stat("$atjob_dir/$i"))[4]; # UID
480 if ($uid == $owner) {
481 if (!$found) {
482 print STDERR "Removing user's at jobs:";
483 $found = 1;
484 }
485 # Use atrm to remove the job
486 print STDERR " $i";
487 system('/usr/bin/atrm', $i);
488 }
489 }
490 closedir(ATDIR);
491 if ($found) {
492 print STDERR " done.\n";
493 }
494 }
495
496 sub resolvelink {
497 local($path) = @_;
498 local($l);
499
500 while (-l $path && -e $path) {
501 if (!defined($l = readlink($path))) {
502 die "${whoami}: readlink on $path failed (but it should have worked!): $!\n";
503 }
504 if ($l =~ /^\//) {
505 # Absolute link
506 $path = $l;
507 } else {
508 # Relative link
509 $path =~ s/\/[^\/]+\/?$/\/$l/; # Replace last component of path
510 }
511 }
512 return $path;
513 }
514
515 sub kill_users_processes {
516 local($login_name, $uid) = @_;
517 local($pid, $result);
518
519 #
520 # Do something a little complex: fork a child that changes its
521 # real and effective UID to that of the removed user, then issues
522 # a "kill(9, -1)" to kill all processes of the same uid as the sender
523 # (see kill(2) for details).
524 # The parent waits for the exit of the child and then returns.
525
526 if ($pid = fork) {
527 # Parent process
528 waitpid($pid, 0);
529 } elsif (defined $pid) {
530 # Child process
531 $< = $uid;
532 $> = $uid;
533 if ($< != $uid || $> != $uid) {
534 print STDERR "${whoami}: Error (kill_users_processes):\n" .
535 "\tCouldn't reset uid/euid to ${uid}: current uid/euid's are $< and $>\n";
536 exit 1;
537 }
538 $result = kill(9, -1);
539 print STDERR "Killed process(es) belonging to $login_name.\n"
540 if $result;
541 exit 0;
542 } else {
543 # Couldn't fork!
544 print STDERR "${whoami}: Error: couldn't fork to kill ${login_name}'s processes - continuing\n";
545 }
546 }