rmuser.in 7.36 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003, 2005 University of Utah and the Flux Group.
5 6 7 8 9 10 11
# All rights reserved.
#
use English;
use Getopt::Std;

sub usage()
{
12
    print STDOUT "Usage: rmuser [-p <pid> [-n]] uid\n" .
13 14 15
	"Use the -p option to remove user from a specific project\n";
    exit(-1);
}
16 17 18

sub fatal($);

19
my  $optlist = "p:n";
20 21 22 23 24 25 26 27 28 29 30

#
# Configure variables
#
my $TB      = "@prefix@";
my $TBOPS   = "@TBOPSEMAIL@";
my $TBLOGS  = "@TBLOGSEMAIL@";
my $CONTROL = "@USERNODE@";
my $BOSSNODE= "@BOSSNODE@";

my $HOMEDIR	= "/users";
31
my $MODGROUPS	= "$TB/sbin/modgroups";
32
my $DELACCT	= "$TB/sbin/tbacct del";
33 34 35 36 37

my $dbuid;
my $user;
my $query_result;
my $pidmode	= 0;
38
my $nuke	= 0;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
my $pid;

#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

#
# This script is setuid, so please do not run it as root. Hard to track
# what has happened.
# 
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root! Its already setuid!\n");
}

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

# Load the Testbed support stuff. 
use lib "@prefix@/lib";
use libaudit;
use libdb;
use libtestbed;

#
# Check args.
#
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"p"})) {
    $pid     = $options{"p"};
    $pidmode = 1;

    # untaint;
    if ($pid =~ /^([-\@\w.]+)$/) {
	$pid = $1;
    }
    else {
	die("Tainted argument $pid!\n");
    }
}
98 99 100 101 102 103
if (defined($options{"n"})) {
    $nuke = 1;
}
if ($nuke && !$pidmode) {
    usage();
}
104 105 106 107 108 109 110 111
if (@ARGV != 1) {
    usage();
}
$user = $ARGV[0];

#
# Untaint the argument.
#
Robert Ricci's avatar
Robert Ricci committed
112
if ($user =~ /^([a-z0-9_]+)$/i) {
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    $user = $1;
}
else {
    die("Tainted argument $user!\n");
}

if (! UNIX2DBUID($UID, \$dbuid)) {
    die("*** $0:\n".
	"    You are not a valid emulab user!\n");
}

#
# Only TB admins are allowed to do this. At some point permit project
# leaders to delete accounts in their projects, but for now lets not.
# There are issues of people in multiple projects.
#
if (!TBAdmin($UID)) {
    if (! defined($pid) ||
	! TBProjAccessCheck($UID, $pid, $pid, TB_PROJECT_DELUSER)) {
	die("*** $0:\n".
	    "    You do not have enough permission to remove user $user!\n");
    }
}

#
# Note hardwired control node. 
# 
my $control_node = $CONTROL;

#
# Sanity check. Must not be the head of any experiments (in the project).
#
$query_result =
    DBQueryFatal("select * from experiments ".
		 "where expt_head_uid='$user' ".
		 (defined($pid) ? "and pid='$pid'" : ""));

if ($query_result->numrows) {
    die("*** $0:\n".
	"    $user is still heading up experiments! Please terminate them.\n");
}

#
# Must not be the head of the project being removed from, or any projects
# if being completely removed.
#
if ($pidmode) {
    if (ProjLeader($pid) eq $user) {
	die("*** $0:\n".
	    "    $user is the leader of project $pid!\n");
    }
}
else {
    $query_result =
	DBQueryFatal("select pid from projects where head_uid='$user'");

    if ($query_result->numrows) {
	die("*** $0:\n".
	    "    $user is still heading up projects! Please fix that.\n");
    }
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
#
# If nuke mode is also specified, then the account is being nuked from
# web page because of a project join denial. Check to make sure user
# is not an active user (must be newuser or unapproved).
#
if ($nuke) {
    $query_result =
	DBQueryFatal("select status from users where uid='$user'");
    my ($status) = $query_result->fetchrow_array();

    if ($status ne USERSTATUS_NEWUSER &&
	$status ne USERSTATUS_UNAPPROVED) {
	die("*** $0:\n".
	    "    $user is not an unapproved user in project $pid!\n");
    }

    $query_result =
	DBQueryFatal("select pid from group_membership ".
		     "where uid='$user' and pid!='$pid'");

    if ($query_result->numrows) {
	die("*** $0:\n".
	    "    $user is a member of other projects!\n");
    }
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
#
# This script is always audited. Mail is sent automatically upon exit.
#
if (AuditStart(0)) {
    #
    # Parent exits normally
    #
    exit(0);
}

#
# Set the status to frozen if user being removed completely. This
# prevents possible race conditions since the user is no longer able
# to access the web interface and tmcd will no longer return account
# info for the user. In pidmode, just remove the user from the specific
216
# project (tmcd will return the new group membership). 
217 218 219 220 221 222 223 224 225 226 227 228 229 230
#
if ($pidmode) {
    DBQueryFatal("delete from group_membership ".
		 "where uid='$user' and pid='$pid'");
}
else {
    DBQueryFatal("update users set status='" . TBDB_USERSTATUS_FROZEN . "'" .
		 "where uid='$user'");
}

#
# Now schedule account updates on all the nodes that this person has
# an account on (remaining project/group membership).
#
231 232 233 234
# XXX This is broken. Once the user is removed from the group membership,
# the list of nodes will change, and those nodes for which the user is
# suddenly no longer allowed an account on, will not be updated. 
# 
235 236 237
TBNodeUpdateAccountsByUID($user);

#
238
# In pidmode, call modgroups to alter the users membership on boss/ops.
239 240
# Thats all that needs to be done.
#
241
if ($pidmode && !$nuke) {
242
    #
243
    # Drop root for calling modgroups.
244 245 246
    #
    $EUID = $UID;

247
    system("$MODGROUPS -r $pid:$pid $user");
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    exit($? >> 8);
}

#
# Not in pidmode. Kill the user's entire group membership.
# Must be done *after* the account update!
# 
DBQueryFatal("delete from group_membership where uid='$user'");

#
# Then the pubkey tables.
# 
DBQueryFatal("delete from user_pubkeys where uid='$user'");
DBQueryFatal("delete from user_sfskeys where uid='$user'");

#
# Must do widearea_accounts too!
#
DBQueryFatal("delete from widearea_accounts where uid='$user'");

#
# Then some other pidly tables.
# 
DBQueryFatal("delete from login where uid='$user'");
DBQueryFatal("delete from nodeuidlastlogin where uid='$user'");
DBQueryFatal("delete from uidnodelastlogin where uid='$user'");
DBQueryFatal("delete from unixgroup_membership where uid='$user'");
DBQueryFatal("delete from userslastlogin where uid='$user'");
276
DBQueryFatal("delete from user_stats where uid='$user'");
277 278

#
279 280
# Remove user from both local and control node. No need to do this in
# nukemode (not allowed anyway) since the account never existed.
281
#
282 283
if (! $nuke) {
    $EUID = $UID;
284

285 286
    system("$DELACCT $user") == 0 or
	fatal("$DELACCT $user failed!");
287

288 289
    $EUID = 0;
}
290 291 292 293 294

#
# Rename the users home dir if its there.
#
if (-d "$HOMEDIR/$user") {
295
    my $newname = "$HOMEDIR/$user-" . TBDateTimeFSSafe();
296 297

    if (rename("$HOMEDIR/$user", $newname)) {
298 299
	print "Renamed homedir to $newname. Remember to delete it!\n";
	
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	#
	# Chown the owner/group to root and set the permissions so no
	# one is allowed to look inside.
	#
	if (! chmod(0700, $newname)) {
	    fatal("Could not chmod directory $newname to 0700: $!");
	}
	if (! chown(0, 0, $newname)) {
	    fatal("Could not chown directory $newname to 0/0: $!");
	}
    }
    else {
	fatal("Could not rename user directory to $newname: $!");
    }
}

#
# Then the users table. This is the very last step, since if there were
# any failures above, we can still rerun the script without it barfing.
# 
DBQueryFatal("delete from users where uid='$user'");

print "User removal Complete!\n";
exit(0);

sub fatal($) {
    my($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}