From d2b945efae99a995de34d7c39c003bd6c1d7b39a Mon Sep 17 00:00:00 2001 From: Wolfram Schneider Date: Sun, 11 Aug 1996 13:03:25 +0000 Subject: [PATCH] delete rule 3 (advertising) from copyright resolve symlinks before removing user's home directory with /bin/rm -rf Submitted by: Guy Helmer --- share/examples/removeuser/README.removeuser | 2 - share/examples/removeuser/removeuser.8 | 7 +-- share/examples/removeuser/removeuser.perl | 61 ++++++++++++++++++--- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/share/examples/removeuser/README.removeuser b/share/examples/removeuser/README.removeuser index 28d354c1a69..ac8ac4b7c80 100644 --- a/share/examples/removeuser/README.removeuser +++ b/share/examples/removeuser/README.removeuser @@ -1,5 +1,3 @@ -# $Id$ - Here lies removeuser(8) for FreeBSD 2.x. It will remove a user's password entry, home directory, and incoming mail file from /var/mail. diff --git a/share/examples/removeuser/removeuser.8 b/share/examples/removeuser/removeuser.8 index b17f5573bf6..b1b3d667f9e 100644 --- a/share/examples/removeuser/removeuser.8 +++ b/share/examples/removeuser/removeuser.8 @@ -10,10 +10,7 @@ .\" 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. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by Guy Helmer. -.\" 4. The name of the author may not be used to endorse or promote products +.\" 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 GUY HELMER ``AS IS'' AND ANY EXPRESS OR @@ -29,7 +26,7 @@ .\" .\" $Id$ .\" -.Dd June 28, 1996 +.Dd July 16, 1996 .Dt REMOVEUSER 8 .Os .Sh NAME diff --git a/share/examples/removeuser/removeuser.perl b/share/examples/removeuser/removeuser.perl index a0fd742a4f4..c2258e9e2be 100644 --- a/share/examples/removeuser/removeuser.perl +++ b/share/examples/removeuser/removeuser.perl @@ -29,7 +29,7 @@ # /usr/sbin/removeuser # Perl script to remove users # -# Guy Helmer , 06/30/96 +# Guy Helmer , 07/17/96 # # $Id$ @@ -138,13 +138,26 @@ if ($ans eq 'N') { $remove_directory = 1; -if (!(-d $home_dir)) { +if (-l $home_dir) { + $real_home_dir = &resolvelink($home_dir); +} else { + $real_home_dir = $home_dir; +} + +# +# If home_dir is a symlink and points to something that isn't a directory, +# or if home_dir is not a symlink and is not a directory, don't remove +# home_dir -- seems like a good thing to do, but probably isn't necessary... +if (((-l $home_dir) && ((-e $real_home_dir) && !(-d $real_home_dir))) || + (!(-l $home_dir) && !(-d $home_dir))) { print STDERR "${whoami}: Home ${home_dir} is not a directory, so it won't be removed\n"; $remove_directory = 0; -} else { - $dir_owner = (stat($home_dir))[4]; # UID +} + +if (length($real_home_dir) && -d $real_home_dir) { + $dir_owner = (stat($real_home_dir))[4]; # UID if ($dir_owner != $uid) { - print STDERR "${whoami}: Home dir ${home_dir} is not owned by ${login_name} (uid ${dir_owner})\n"; + print STDERR "${whoami}: Home dir ${real_home_dir} is not owned by ${login_name} (uid ${dir_owner})\n"; $remove_directory = 0; } } @@ -377,11 +390,26 @@ sub update_group_file { } sub remove_dir { - # Recursively remove directories + # Remove the user's home directory local($dir) = @_; + local($linkdir); + if (-l $dir) { + $linkdir = &resolvelink($dir); + # Remove the symbolic link + unlink($dir) || + warn "${whoami}: Warning: could not unlink symlink $dir: $!\n"; + if (!(-e $linkdir)) { + # + # Dangling symlink - just return now + return; + } + # Set dir to be the resolved pathname + $dir = $linkdir; + } if (!(-d $dir)) { - print STDERR "${whoami}: Error: $dir is not a directory\n"; + print STDERR "${whoami}: Warning: $dir is not a directory\n"; + unlink($dir) || warn "${whoami}: Warning: could not unlink $dir: $!\n"; return; } system('/bin/rm', '-rf', $dir); @@ -414,3 +442,22 @@ sub remove_at_jobs { print STDERR " done.\n"; } } + +sub resolvelink { + local($path) = @_; + local($l); + + while (-l $path && -e $path) { + if (!defined($l = readlink($path))) { + die "${whoami}: readlink on $path failed (but it should have worked!): $!\n"; + } + if ($l =~ /^\//) { + # Absolute link + $path = $l; + } else { + # Relative link + $path =~ s/\/[^\/]+\/?$/\/$l/; # Replace last component of path + } + } + return $path; +}