approvewauser.php3 9.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");

#
# Standard Testbed Header
#
PAGEHEADER("Widearea Accounts Approval Form");

#
# Only known and logged in users can be verified.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
if (! ISADMIN($uid)) {
20
    USERERROR("Only testbed administrators people can access this page!", 1);
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
}
ignore_user_abort(1);

#
# Walk the list of post variables, looking for the special post format.
# See approvewauser_form.php3:
#
#             uid     menu     node_id
#	name=stoller$$approval-node_id value=approved,denied,postpone
#	name=stoller$$trust-node_id value=user,local_root
#
# 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. 
# 
while (list ($header, $value) = each ($HTTP_POST_VARS)) {
    #echo "$header: $value<br>\n";

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

    $user     = substr($header, 0, strpos($header, "\$\$", 0));
    $node_id  = substr($approval_string, strlen("\$\$approval-"));
    $approval = $value;

    if (!$user || strcmp($user, "") == 0) {
	TBERROR("Parse error finding user in approvewauser.php3", 1);
    }
    if (!$node_id || strcmp($node_id, "") == 0) {
	TBERROR("Parse error finding node_id in approvewauser.php3", 1);
    }
    if (!$approval || strcmp($approval, "") == 0) {
	TBERROR("Parse error finding approval in approvewauser.php3", 1);
    }

    #
    # There should be a corresponding trust variable in the POST vars.
    # Note that we construct the variable name and indirect to it.
    #
    $foo      = "$user\$\$trust-$node_id";
    $newtrust = $$foo;
    if (!$newtrust || strcmp($newtrust, "") == 0) {
	TBERROR("Parse error finding trust in approvewauser.php3", 1);
    }
    #echo "User $user, NodeID $node_id,
    #      Approval $approval, Trust $newtrust<br>\n";
    
    if (strcmp($newtrust, "user") &&
	strcmp($newtrust, "local_root")) {
	TBERROR("Invalid trust $newtrust for user $user approvewauser.php3.",
		1);
    }

    #
    # Verify an actual user that is being approved.
    #
    if (! TBCurrentUser($user)) {
	TBERROR("Trying to approve unknown user $user.", 1);
    }
    
    #
    # Check if already approved. If already an approved account,
    # something went wrong.
    #
    $query_result =
	DBQueryFatal("select trust from widearea_accounts ".
		     "where uid='$user' and node_id='$node_id' and ".
		     "      trust!='none'");
    if (mysql_num_rows($query_result)) {
	$row   = mysql_fetch_array($query_result);
	$trust = $row[trust];
	
	USERERROR("$user is already approved on $node_id with $trust!", 1);
    }

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

reset($HTTP_POST_VARS);

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

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

    $user     = substr($header, 0, strpos($header, "\$\$", 0));
    $node_id  = substr($approval_string, strlen("\$\$approval-"));
    $approval = $value;

    #
    # Corresponding trust value.
    #
    $foo      = "$user\$\$trust-$node_id";
    $newtrust = $$foo;

    #
    # Get the current status for the user, which we might need to change.
    #
    # We change the status only if this person is getting a new account.
    # 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. 
    #
    $query_result =
        DBQueryFatal("SELECT status,usr_email,usr_name from users where ".
		     "uid='$user'");
    if (mysql_num_rows($query_result) == 0) {
	TBERROR("Unknown user $user", 1);
    }
    $row = mysql_fetch_row($query_result);
    $curstatus  = $row[0];
    $user_email = $row[1];
    $user_name  = $row[2];
    #echo "Status = $curstatus, Email = $user_email<br>\n";

    #
    # Email info for current user.
    # 
    TBUserInfo($uid, $uid_name, $uid_email);

    #
    # Well, looks like everything is okay. Change the project membership
    # value appropriately.
    #
    if (strcmp($approval, "postpone") == 0) {
	echo "<p>
                  Account status for user $user was
                  <b>postponed</b> for later decision.\n";
        continue;
    }
    if (strcmp($approval, "deny") == 0) {
        #
        # Must delete the widearea_account record since we require that the 
        # user reapply once denied. Send the luser email to let him know.
        #
        $query_result =
	    DBQueryFatal("delete from widearea_accounts ".
			 "where uid='$user' and node_id='$node_id'");

        TBMAIL("$user_name '$user' <$user_email>",
             "Account request on $node_id denied",
	     "\n".
             "This message is to notify you that you have been denied\n".
	     "local account access on $node_id!\n".
             "\n\n".
             "Thanks,\n".
             "Testbed Operations\n",
             "From: $uid_name <$uid_email>\n".
	     "Cc: $TBMAIL_OPS\n".
             "Bcc: $TBMAIL_AUDIT\n".
             "Errors-To: $TBMAIL_WWW");

	echo "<p>
                User $user was <b>denied</b> an account on $node_id.
                <br>
                The user will need to reapply again if this was in error.\n";

	continue;
    }
    if (strcmp($approval, "nuke") == 0) {
        #
        # Must delete the group_membership record since we require that the 
        # user reapply once denied. Send the luser email to let him know.
        #
        $query_result =
	    DBQueryFatal("delete from widearea_accounts ".
			 "where uid='$user' and node_id='$node_id'");

	#
	# See if user is in any other projects (even unapproved).
	#
        $query_result =
	    DBQueryFatal("select * from group_membership where uid='$user'");

	#
	# If yes, then we cannot safely delete the user account.
	#
	if (mysql_num_rows($query_result)) {
	    echo "<p>
                  User $user was <b>denied</b> an account on $node_id.
                  <br>
                  Since the user is a member (or requesting membership)
		  in other projects, the account cannot be safely removed.\n";
	    
	    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")) {
	    echo "<p>
                  User $user was <b>denied</b> an account on $node_id.
                  <br>
                  Since the user has been approved by, or was active in other
		  projects in the past, the account cannot be safely removed.
                  \n";
	    continue;
	}
	
	$query_result = DBQueryFatal("delete FROM users where uid='$user'");
	
	echo "<p>
                User $user was <b>denied</b> an account on $node_id.
                <br>
		The account has also been <b>terminated</b> with prejudice!\n";

	continue;
    }
    if (strcmp($approval, "approve") == 0) {
        #
        # Change the trust value accordingly.
        #
        $query_result =
	    DBQueryFatal("UPDATE widearea_accounts ".
			 "set trust='$newtrust',date_approved=now() ".
			 "WHERE uid='$user' and node_id='$node_id'");

        #
        # 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.
	#
        if (strcmp($curstatus, "active")) {
	    if (strcmp($curstatus, "newuser") == 0) {
		$newstatus = "unverified";
            }
	    elseif (strcmp($curstatus, "unapproved") == 0) {
		$newstatus = "active";
	    }
	    elseif (strcmp($curstatus, "unverified") == 0) {
		$newstatus = "unverified";
	    }
	    else {
	        TBERROR("Invalid $user status $curstatus!", 1);
	    }
	    $query_result =
		DBQueryFatal("UPDATE users set status='$newstatus' ".
			     "WHERE uid='$user'");
	}

        TBMAIL("$user_name '$user' <$user_email>",
	       "Widearea account granted on '$node_id' ",
	       "\n".
	       "This message is to notify you that you have been granted an\n".
	       "account on $node_id with $newtrust permissions.\n".
	       "\n".
	       "In order to log into this node, you must upload an ssh key\n".
	       "via: ${TBBASE}/showpubkeys.php3?target_uid=$user\n".
	       "\n\n".
	       "Thanks,\n".
	       "Testbed Operations\n",
	       "From: $uid_name <$uid_email>\n".
	       "Cc:  $TBMAIL_OPS\n".
	       "Bcc: $TBMAIL_AUDIT\n".
	       "Errors-To: $TBMAIL_WWW");

	echo "<p>
                  User $user was <b>granted</b> an account on $node_id
                  with $newtrust permissions.\n";

	#
	# XXX
	#
	DBQueryFatal("update nodes set update_accounts=update_accounts+1 ".
		     "where node_id='$node_id' and update_accounts<2");
		
	continue;
    }
}

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>