rmuser.in 7.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# 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
my  $optlist = "p:n";
17 18 19 20 21 22 23 24 25 26 27 28

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

my $HOMEDIR	= "/users";
my $SETGROUPS	= "$TB/sbin/setgroups";
29
my $DELACCT	= "$TB/sbin/tbacct del";
30 31 32 33 34

my $dbuid;
my $user;
my $query_result;
my $pidmode	= 0;
35
my $nuke	= 0;
36 37 38 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
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");
    }
}
95 96 97 98 99 100
if (defined($options{"n"})) {
    $nuke = 1;
}
if ($nuke && !$pidmode) {
    usage();
}
101 102 103 104 105 106 107 108
if (@ARGV != 1) {
    usage();
}
$user = $ARGV[0];

#
# Untaint the argument.
#
109
if ($user =~ /^([a-z0-9_]+)$/i) {
110 111 112 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
    $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");
    }
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
#
# 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");
    }
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#
# 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
213
# project (tmcd will return the new group membership). 
214 215 216 217 218 219 220 221 222 223 224 225 226 227
#
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).
#
228 229 230 231
# 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. 
# 
232 233 234 235 236 237
TBNodeUpdateAccountsByUID($user);

#
# In pidmode, call setgroups to alter the users membership on boss/ops.
# Thats all that needs to be done.
#
238
if ($pidmode && !$nuke) {
239 240 241 242 243 244 245 246 247 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
    #
    # Drop root for calling setgroups since its setuid.
    #
    $EUID = $UID;

    system("$SETGROUPS $user");
    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'");
273
DBQueryFatal("delete from user_stats where uid='$user'");
274 275

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

282 283
    system("$DELACCT $user") == 0 or
	fatal("$DELACCT $user failed!");
284

285 286
    $EUID = 0;
}
287 288 289 290 291

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

    if (rename("$HOMEDIR/$user", $newname)) {
295 296
	print "Renamed homedir to $newname. Remember to delete it!\n";
	
297 298 299 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
	#
	# 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");
}