approveuser.php3 14.6 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003, 2005, 2006, 2007 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
7 8
include("defs.php3");

9 10 11 12 13
#
# Standard Testbed Header
#
PAGEHEADER("New Users Approved");

14
#
15
# Only known and logged in users.
16
#
17 18
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
19

20 21 22 23 24 25
#
# The reason for this call is to make sure that globals are set properly.
#
$reqargs = RequiredPageArguments();

# Local used below.
26 27
$projectchecks = array();

28
# Hmm, is this needed?
29 30
ignore_user_abort(1);

31 32 33 34
#
# Walk the list of post variables, looking for the special post format.
# See approveuser_form.php3:
#
Leigh Stoller's avatar
Leigh Stoller committed
35
#             uid     menu     project/group
36 37
#	name=Uxxxx$$approval-testbed/testbed value=approved,denied,postpone
#	name=Uxxxx$$trust-testbed/testbed value=user,local_root
38 39 40 41 42
#
# We make two passes over the post vars. The first does a sanity check so
# that we can bail out without doing anything. This allows the user to
# back up and make changes without worrying about some stuff being done and
# other stuff not. 
43 44 45 46 47 48 49 50 51 52
#

#
# copy of HTTP_POST_VARS we use to do actual work.
# this is so I can insert an implicit default-group approval
# while iterating over HTTP_POST_VARS. 
# A bit kludgey, indeed.
#
$POST_VARS_COPY = array();
 
53 54
while (list ($header, $value) = each ($HTTP_POST_VARS)) {
    #echo "$header: $value<br>\n";
55
    $POST_VARS_COPY[$header] = $value; 
56 57 58 59 60 61

    $approval_string = strstr($header, "\$\$approval-");
    if (! $approval_string) {
	continue;
    }

62
    $user     = substr($header, 1, strpos($header, "\$\$", 0) - 1);
Leigh Stoller's avatar
Leigh Stoller committed
63 64 65
    $projgrp  = substr($approval_string, strlen("\$\$approval-"));
    $project  = substr($projgrp, 0, strpos($projgrp, "/", 0));
    $group    = substr($projgrp, strpos($projgrp, "/", 0) + 1);
66 67 68 69 70 71 72 73
    $approval = $value;

    if (!$user || strcmp($user, "") == 0) {
	TBERROR("Parse error finding user in approveuser.php3", 1);
    }
    if (!$project || strcmp($project, "") == 0) {
	TBERROR("Parse error finding project in approveuser.php3", 1);
    }
Leigh Stoller's avatar
Leigh Stoller committed
74 75 76
    if (!$group || strcmp($group, "") == 0) {
	TBERROR("Parse error finding group in approveuser.php3", 1);
    }
77 78 79 80 81 82 83 84
    if (!$approval || strcmp($approval, "") == 0) {
	TBERROR("Parse error finding approval in approveuser.php3", 1);
    }

    #
    # There should be a corresponding trust variable in the POST vars.
    # Note that we construct the variable name and indirect to it.
    #
85 86 87 88
    $foo      = "U${user}\$\$trust-$project/$group";
    #echo "$foo<br>\n";
    
    $newtrust = $HTTP_POST_VARS[$foo];
89 90 91
    if (!$newtrust || strcmp($newtrust, "") == 0) {
	TBERROR("Parse error finding trust in approveuser.php3", 1);
    }
Leigh Stoller's avatar
Leigh Stoller committed
92 93 94 95 96 97
    #echo "User $user, Project $project,
    #      Group $group, Approval $approval, Trust $newtrust<br>\n";
    
    if (strcmp($newtrust, "user") &&
	strcmp($newtrust, "local_root") &&
	strcmp($newtrust, "group_root")) {
98 99 100 101
	TBERROR("Invalid trust $newtrust for user $user approveuser.php3.", 1);
    }

    #
102
    # Verify an actual user that is being approved.
103
    #
104
    if (! ($target_user = User::Lookup($user))) {
105
	TBERROR("Trying to approve unknown user $user.", 1);
106
    }
107
    $target_uid = $target_user->uid();
108 109 110 111 112 113 114 115 116 117

    # Ditto the project.
    if (! ($target_project = Project::Lookup($project))) {
	TBERROR("Trying to approve user into unknown project $project.", 1);
    }

    # Ditto the group.
    if (! ($target_group = Group::LookupByPidGid($project, $group))) {
	TBERROR("Trying to approve user into unknown group $group", 1);
    }
118
    
119
    #
120 121
    # Check that the current uid has the necessary trust level
    # to approver users in the project/group. Also, only project leaders
122
    # can add someone to the default group as group_root.
123
    #
124
    if (! $target_group->AccessCheck($this_user, $TB_PROJECT_ADDUSER)) {
Leigh Stoller's avatar
Leigh Stoller committed
125 126
	USERERROR("You are not allowed to approve users in ".
		  "$project/$group!", 1);
127
    }
128

129 130 131 132 133
    if ($newtrust == "group_root" && $project == $group &&
	!$target_project->AccessCheck($this_user,
				      $TB_PROJECT_BESTOWGROUPROOT)) {
	USERERROR("You do not have permission to add new users with group ".
		  "root trust to the default group!", 1);
134
    }
Leigh Stoller's avatar
Leigh Stoller committed
135
    
136
    #
137 138
    # Check if already approved in the project/group. If already an
    # approved member, something went wrong.
139
    #
140
    $target_group->IsMember($target_user, $isapproved);
Leigh Stoller's avatar
Leigh Stoller committed
141
    if ($isapproved) {
142
	USERERROR("$target_uid is already an approved member of ".
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		  "$project/$group!", 1);
    }

    #
    # Verify approval value. 
    #
    if (strcmp($approval, "postpone") &&
	strcmp($approval, "deny") &&
	strcmp($approval, "nuke") &&
	strcmp($approval, "approve")) {
	TBERROR("Invalid approval value $approval in approveuser.php3.", 1);
    }

    #
    # If denying project membership, then there must be equivalent denial
    # for all subgroups. We can either do it for the user, or require that the
    # user understand whats happening. I prefer the latter, so look for this
    # and spit back an error. Note that we cannot rely on the post vars for
    # this, but must look in the DB for the group set, and then check to make
    # sure there are post vars for *all* of them.
    #
    if (strcmp($project, $group) == 0 &&
	(strcmp($approval, "deny") == 0 ||
	 strcmp($approval, "nuke") == 0)) {
167 168 169 170 171 172

	# List of subgroup membership in this project.
	$grouplist = $target_project->GroupList($target_user);

	foreach ($grouplist as $subgroup) {
	    $gid = $subgroup->gid();
173 174 175 176 177

            #
            # Create and indirect through post var for subgroup approval value.
            #
	    $foo = "$user\$\$approval-$project/$gid";
178 179 180 181
	    
	    if (!isset($HTTP_POST_VARS[$foo]) ||
		($HTTP_POST_VARS[$foo] != "deny" &&
		 $HTTP_POST_VARS[$foo] != "nuke")) {
182 183 184 185
		USERERROR("If you wish to deny/nuke user $target_uid in ".
			  "project $project then you must deny/nuke in all ".
			  "of the subgroups $target_uid is attempting to ".
			  "join.", 1);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	    }
	}
    }

    if (strcmp($approval, "approve") == 0)
	$projectchecks[$user][] = array($project, $group, $newtrust);

    #
    # When operating on a user for a subgroup, the user must already be in the
    # default group, or there must be an appropriate default group operation
    # in the POST vars. In other words, we do not allow users to be
    # approved/denied/postponed to a subgroup without a default group
    # operation as well. At present, all users must be in the default group
    # in addition to subgroups.
    #
    if (strcmp($project, $group) == 0)
	continue;

204
    $target_project->IsMember($target_user, $isapproved);
205 206 207 208 209 210
    if ($isapproved)
	continue;

    #
    # Create and indirect through post var for project approval value.
    #
211 212
    $foo = "U${user}\$\$approval-$project/$project";
    $bar = "U${user}\$\$trust-$project/$project";
213 214
    $default_approval =
	(isset($HTTP_POST_VARS[$foo]) ? $HTTP_POST_VARS[$foo] : "");
215
    
216
    if ($default_approval == "") {
217 218 219 220
	# Implicit group approval as user.
	# Short circuit all the perms-checking, and squeeze it in
	# all the appropriate places.
	
221
	# 1. For the strcmp below.
222 223
	$default_approval = $approval;

224
	# 2. For the sanity check
225 226
	$projectchecks[$user][] = array($project, $project, "user");	

227 228 229 230
	# 3. For the while loop which does the actual work
	$POST_VARS_COPY[$foo] = $approval;
	$HTTP_POST_VARS[$foo] = $approval;
	$HTTP_POST_VARS[$bar] = "user";
231 232 233
    }
    if (strcmp($approval, "approve") == 0 &&
	strcmp($default_approval, "approve")) {
234
	USERERROR("You cannot approve $target_uid in $project/$group without ".
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		  "approval in the default group ($project/$project)!", 1);
    }
}

#
# Sanity check. I hate this stuff.
# 
while (list ($user, $value) = each ($projectchecks)) {
    $projtrust   = array();
    $grouptrust  = array();
    $pidlist     = array();
    
    while (list ($a, $b) = each ($value)) {
	$pid   = $b[0];
	$gid   = $b[1];
	$trust = $b[2];

Leigh Stoller's avatar
Leigh Stoller committed
252
	#echo "$user $pid $gid $trust<br>\n";
253

254 255 256 257 258 259 260
	if (! ($target_group = Group::LookupByPidGid($pid, $gid))) {
	    TBERROR("Could not find group object for $project/$group", 1);
	}

	if (! ($target_user = User::Lookup($user))) {
	    TBERROR("Could not find user object for $user", 1);
	}
261
	$target_uid = $target_user->uid();
262
	
263 264 265 266 267 268 269 270
	#
	# This looks for different trust levels in different subgroups
	# of the same project. We are only checking the form arguments
	# here; we will do a check against the DB below. 
	# 
	if (strcmp($pid, $gid)) {
	    if (isset($grouptrust[$pid]) &&
		strcmp($grouptrust[$pid], $trust)) {
271 272
		USERERROR("User $target_uid may not have different trust ".
			  "levels in different subgroups of $pid!", 1);
273 274 275 276 277 278 279 280 281 282 283
	    }
	    $grouptrust[$pid] = $trust;
	}
	else {
	    #
	    # Stash the project default group trust so that we can also
	    # do a consistency check against it.
	    #
	    $projtrust[$pid] = $trust;
	}
	$pidlist[$pid] = $pid;
284

285
	$target_group->CheckTrustConsistency($target_user, $trust, 1);
286 287 288 289 290 291 292 293 294 295
    }
    
    reset($value);
}

reset($HTTP_POST_VARS);

#
# Okay, all sanity tests passed for all post vars. Now do the actual work.
# 
296
while (list ($header, $value) = each ($POST_VARS_COPY)) {
297 298 299 300
    #echo "$header: $value<br>\n";

    $approval_string = strstr($header, "\$\$approval-");
    if (! $approval_string) {
Leigh Stoller's avatar
Leigh Stoller committed
301
	continue;
302 303
    }

304
    $user     = substr($header, 1, strpos($header, "\$\$", 0) - 1);
305 306 307 308 309
    $projgrp  = substr($approval_string, strlen("\$\$approval-"));
    $project  = substr($projgrp, 0, strpos($projgrp, "/", 0));
    $group    = substr($projgrp, strpos($projgrp, "/", 0) + 1);
    $approval = $value;

310
    #
311 312
    # Corresponding trust value.
    #
313 314
    $foo      = "U${user}\$\$trust-$project/$group";
    $newtrust = $HTTP_POST_VARS[$foo];
315 316 317 318 319 320 321 322

    #
    # Get the current status for the user, which we might need to change.
    #
    # We change the status only if this person is joining his first project.
    # In this case, the status will be either "newuser" or "unapproved",
    # and we will change it to "unapproved" or "active", respectively.
    # If the status is "active", we leave it alone. 
323
    #
324 325
    if (! ($target_user = User::Lookup($user))) {
	TBERROR("Trying to approve unknown user $user.", 1);
326
    }
327 328 329
    $curstatus  = $target_user->status();
    $user_email = $target_user->email();
    $user_name  = $target_user->name();
330
    $user_uid   = $target_user->uid();
331 332
    #echo "Status = $curstatus, Email = $user_email<br>\n";

333 334 335 336 337 338 339 340
    # Ditto the project and group
    if (! ($target_project = Project::Lookup($project))) {
	TBERROR("Trying to approve user into unknown project $project.", 1);
    }
    if (! ($target_group = Group::LookupByPidGid($project, $group))) {
	TBERROR("Trying to approve user into unknown group $group", 1);
    }

341 342
    #
    # Email info for current user.
343 344 345
    #
    $uid_name  = $this_user->name();
    $uid_email = $this_user->email();
346 347

    #
348
    # Email info for the proj/group leaders too.
349
    #
350
    $leaders = $target_group->LeaderMailList();
351

352 353 354 355 356
    #
    # Well, looks like everything is okay. Change the project membership
    # value appropriately.
    #
    if (strcmp($approval, "postpone") == 0) {
357
	echo "<p>
358
                  Membership status for user $user_uid in $project/$group was
359
                  <b>postponed</b> for later decision.\n";
360 361 362 363
        continue;
    }
    if (strcmp($approval, "deny") == 0) {
        #
Leigh Stoller's avatar
Leigh Stoller committed
364 365
        # Must delete the group_membership record since we require that the 
        # user reapply once denied. Send the luser email to let him know.
366
        #
367
	$target_group->DeleteMember($target_user);
Leigh Stoller's avatar
Leigh Stoller committed
368

369 370
	TBMAIL("$user_name '$user_uid' <$user_email>",
	     "Membership Denied in '$project/$group'",
371
	     "\n".
372
	     "This message is to notify you that you have been denied\n".
373
	     "membership in project/group $project/$group.\n".
374 375 376 377 378 379 380
	     "\n\n".
	     "Thanks,\n".
	     "Testbed Operations\n",
	     "From: $uid_name <$uid_email>\n".
	     "Cc:  $leaders\n".
	     "Bcc: $TBMAIL_AUDIT\n".
	     "Errors-To: $TBMAIL_WWW");
381

382
	echo "<p>
383
                User $user_uid was <b>denied</b> membership in $project/$group.
384 385
                <br>
                The user will need to reapply again if this was in error.\n";
386 387 388

	continue;
    }
389 390
    if (strcmp($approval, "nuke") == 0) {
        #
Leigh Stoller's avatar
Leigh Stoller committed
391 392
        # Must delete the group_membership record since we require that the 
        # user reapply once denied. Send the luser email to let him know.
393
        #
394
	$target_group->DeleteMember($target_user);
395 396 397

	#
	# See if user is in any other projects (even unapproved).
Leigh Stoller's avatar
Leigh Stoller committed
398
	#
399
	$project_list = $target_user->ProjectMembershipList();
400 401 402 403

	#
	# If yes, then we cannot safely delete the user account.
	#
404
	if (count($project_list)) {
405
	    echo "<p>
406 407
                  User $user_uid was <b>denied</b> membership in
                  $project/$group.
408
                  <br>
409
                  Since the user is a member (or requesting membership)
410
		  in other projects, the account cannot be safely removed.\n";
411 412 413 414 415 416 417 418 419 420 421 422
	    
	    continue;
	}

	#
	# No other project membership. If the user is unapproved/newuser, 
	# it means he was never approved in any project, and so will
	# likely not be missed. He will be unapproved if he did his
	# verification.
	#
	if (strcmp($curstatus, "newuser") &&
	    strcmp($curstatus, "unapproved")) {
423
	    echo "<p>
424 425
                  User $user_uid was <b>denied</b> membership in
                  $project/$group.
426
                  <br>
427 428
                  Since the user has been approved by, or was active in other
		  projects in the past, the account cannot be safely removed.
429
                  \n";
430 431
	    continue;
	}
432
	SUEXEC($uid, $TBADMINGROUP, "webrmuser -n -p $project $user_uid", 1); 
433

434
	echo "<p>
435
                User $user_uid was <b>denied</b> membership in $project/$group.
436
                <br>
437
		The account has also been <b>terminated</b>!\n";
438 439 440

	continue;
    }
441 442 443 444 445 446
    if (strcmp($approval, "approve") == 0) {
        #
        # Change the status if necessary. This only happens for new
	# users being added to their first project. After this, the status is
        # going to be "active", and we just leave it that way.
	#
447 448 449
	if ($curstatus != TBDB_USERSTATUS_ACTIVE) {
	    if ($curstatus == TBDB_USERSTATUS_UNAPPROVED) {
		$target_user->SetStatus(TBDB_USERSTATUS_ACTIVE);
450
	    }
451
	    else {
452
	        TBERROR("Invalid $user status $curstatus", 1);
453
	    }
454 455
	    if (!($user_interface =
		  $target_project->default_user_interface())) {
456
		$user_interface = TBDB_USER_INTERFACE_EMULAB;
457
	    }
458
	    $target_user->SetUserInterface($user_interface);
459 460 461 462

            #
            # Create user account on control node.
            #
463
	    SUEXEC($uid, $TBADMINGROUP, "webtbacct add $user_uid", 1);
464
	}
465 466 467 468
        #
	# Only need to add new membership.
	# 
	SUEXEC($uid, $TBADMINGROUP,
469
	       "webmodgroups -a $project:$group:$newtrust $user_uid", 1);
470

471
	echo "<p>
472 473
                  User $user_uid was <b>granted</b> membership in
                  $project/$group with $newtrust permissions.\n";
474
		
475 476 477 478
	continue;
    }
}

479 480 481 482
#
# Standard Testbed Footer
# 
PAGEFOOTER();
483
?>