moduserinfo.php3 19.6 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
7 8 9 10 11 12 13 14 15 16 17 18 19 20
include("defs.php3");
include("showstuff.php3");

#
# No PAGEHEADER since we spit out a Location header later. See below.
# 

#
# Only known and logged in users can modify info.
#
# Note different test though, since we want to allow logged in
# users with expired passwords to change them.
#
$uid = GETLOGIN();
21 22
LOGGEDINORDIE($uid,
	      CHECKLOGIN_USERSTATUS|CHECKLOGIN_PSWDEXPIRED|CHECKLOGIN_WEBONLY);
23 24
$isadmin = ISADMIN($uid);

25

26 27 28 29 30 31
# temporarily make tcsh the only option until backend stuff
# gets put into place.
$shelllist = array( 'tcsh' );
# $shelllist = array( 'tcsh', 'bash', 'csh', 'sh' );

# used if db is NULL (shouldn't happen.)
32 33
$defaultshell = 'tcsh';

34 35 36 37 38 39
#
# Spit the form out using the array of data and error strings (if any).
# 
function SPITFORM($formfields, $errors)
{
    global $TBDB_UIDLEN, $TBDB_PIDLEN, $TBDB_GIDLEN, $isadmin;
40
    global $target_uid;
41
    global $shelllist, $defaultshell;
42 43 44 45 46 47 48 49
    
    #
    # Standard Testbed Header. Written late cause of password
    # expiration interaction. See below.
    #
    PAGEHEADER("Modify User Information");

    if ($errors) {
50 51
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
52
              <tr>
53
                 <th align=center colspan=2>
54
                   <font size=+1 color=red>
55
                      &nbsp;Oops, please fix the following errors!&nbsp;
56 57 58 59 60 61
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
62 63 64 65
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
66 67 68 69 70 71 72
                  </tr>\n";
	}
	echo "</table><br>\n";
    }

    echo "<table align=center border=1> 
          <tr>
73
            <td align=center colspan=3>
74
                Fields marked with * are required.
75 76 77
            </td>
          </tr>\n

78
          <form action=moduserinfo.php3 method=post>\n";
79 80 81 82 83

        #
        # UserName. This is a constant field. 
        #
        echo "<tr>
84
                  <td colspan=2>Username:</td>
85 86 87 88 89 90 91 92 93 94 95 96 97 98
                  <td class=left>
                      $formfields[target_uid]
                      <input type=hidden
                             name=\"formfields[target_uid]\"
                             value=\"" . $formfields[target_uid] . "\"
                             size=$TBDB_UIDLEN
   	                     maxlength=$TBDB_UIDLEN>
              </td>
             </tr>\n";

	#
	# Full Name
	#
        echo "<tr>
99
                  <td colspan=2>*Full Name:</td>
100 101 102 103 104 105 106 107 108 109 110 111
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_name]\"
                             value=\"" . $formfields[usr_name] . "\"
	                     size=30>
                  </td>
              </tr>\n";

        #
	# Title/Position:
	# 
	echo "<tr>
112
                  <td colspan=2>*Title/Position:</td>
113 114 115 116 117 118 119 120 121 122 123 124
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_title]\"
                             value=\"" . $formfields[usr_title] . "\"
	                     size=30>
                  </td>
              </tr>\n";

        #
	# Affiliation:
	# 
	echo "<tr>
125
                  <td colspan=2>*Institutional<br>Affiliation:</td>
126 127 128 129 130 131 132 133 134 135 136 137
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_affil]\"
                             value=\"" . $formfields[usr_affil] . "\"
	                     size=40>
                  </td>
              </tr>\n";

	#
	# User URL
	#
	echo "<tr>
138
                  <td colspan=2>Home Page URL:</td>
139 140 141 142 143 144 145 146 147 148 149 150
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_URL]\"
                             value=\"" . $formfields[usr_URL] . "\"
	                     size=45>
                  </td>
              </tr>\n";

	#
	# Email:
	#
	echo "<tr>
151
                  <td colspan=2>*Email Address[<b>1</b>]:</td>
152 153 154 155 156 157 158 159 160 161 162 163 164
                  <td class=left> ";
	if ($isadmin)
	    echo "    <input type=text ";
	else
	    echo "    $formfields[usr_email]
                      <input type=hidden ";

	echo "               name=\"formfields[usr_email]\"
                             value=\"" . $formfields[usr_email] . "\"
	                     size=30>";
        echo "    </td>
              </tr>\n";

165 166 167 168 169 170
        #
        # Country needs a default for older users.
        #
	if (! strcmp($formfields[usr_country], "")) {
	    $formfields[usr_country] = "USA";
	}
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
	echo "<tr><td colspan=3>*Address:<br /><center>
		<table>
		  <tr><td>Line 1</td><td colspan=3>
                    <input type=text
                           name=\"formfields[usr_addr]\"
                           value=\"" . $formfields[usr_addr] . "\"
	                   size=45></td></tr>
		  <tr><td>Line 2</td><td colspan=3>
                    <input type=text
                           name=\"formfields[usr_addr2]\"
                           value=\"" . $formfields[usr_addr2] . "\"
	                   size=45></td></tr>
		  <tr><td>City</td><td>
                    <input type=text
                           name=\"formfields[usr_city]\"
                           value=\"" . $formfields[usr_city] . "\"
	                   size=25></td>
		      <td>State/Province</td><td>
                    <input type=text
                           name=\"formfields[usr_state]\"
                           value=\"" . $formfields[usr_state] . "\"
	                   size=2></td></tr>
		  <tr><td>ZIP/Postal Code</td><td>
                    <input type=text
                           name=\"formfields[usr_zip]\"
                           value=\"" . $formfields[usr_zip] . "\"
	                   size=10></td>
		      <td>Country</td><td>
                    <input type=text
                           name=\"formfields[usr_country]\"
                           value=\"" . $formfields[usr_country] . "\"
	                   size=15></td></tr>
               </table></center></td></tr>";
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	#
	# Default Group
	#

	# $q = DBQueryWarn("select gid, pid

	# Default Shell
        echo "<tr><td colspan=2>Shell:</td>
                  <td class=left>";
        echo "<select name=\"formfields[usr_shell]\">";
	foreach ($shelllist as $s) {
	    if ((!isset($formfields[usr_shell]) &&
		0 == strcmp($defaultshell, $s)) ||
		0 == strcmp($formfields[usr_shell],$s)) {
		$sel = "selected='1'";
	    } else {
		$sel = "";
	    }
	    echo "<option value='$s' $sel>$s</option>";
	}	
        echo "</select></td></tr>";

228 229 230 231
	#
	# Phone
	#
	echo "<tr>
232
                  <td colspan=2>*Phone #:</td>
233 234 235 236 237 238 239 240 241 242 243 244 245
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_phone]\"
                             value=\"" . $formfields[usr_phone] . "\"
	                     size=15>
                  </td>
              </tr>\n";

	#
	# Password. Note that we do not resend the password. User
	# must retype on error.
	#
	echo "<tr>
246
                  <td colspan=2>Password[<b>1</b>]:</td>
247 248 249 250 251 252 253
                  <td class=left>
                      <input type=password
                             name=\"formfields[password1]\"
                             size=8></td>
              </tr>\n";

        echo "<tr>
254
                  <td colspan=2>Retype Password:</td>
255 256 257 258 259 260 261
                  <td class=left>
                      <input type=password
                             name=\"formfields[password2]\"
                             size=8></td>
             </tr>\n";

    echo "<tr>
262
              <td colspan=3 align=center>
263 264 265 266 267 268 269 270 271 272 273 274 275
                 <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

    echo "</form>
          </table>\n";

    echo "<h4><blockquote><blockquote>
          <ol>
            <li> Please consult our
                 <a href = 'docwrapper.php3?docname=security.html'>
                 security policies</a> for information
                 regarding passwords and email addresses.
276 277
            <li> You can also
                 <a href='showpubkeys.php3?target_uid=$target_uid'>
278 279 280
                 edit your ssh public keys</a> and your
                 <a href='showsfskeys.php3?target_uid=$target_uid'>
		 sfs public keys</a>.
281 282
            <li> The City, State, ZIP/Postal Code, and Country fields 
                 were added later, so
283 284
                 some early users will be forced to adjust their addresses
                 before they can proceed. Sorry for the inconvenience.
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
          </ol>
          </blockquote></blockquote>
          </h4>\n";
}

#
# The target uid and the current uid will generally be the same, unless
# its an admin user modifying someone elses data. Must verify this case.
#
if (! isset($submit)) {
    if (! isset($target_uid))
	$target_uid = $uid;
}
elseif (! isset($formfields[target_uid]) ||
	strcmp($formfields[target_uid], "") == 0) {
    USERERROR("You must supply a target user id!", 1);
}
else {
    $target_uid = $formfields[target_uid];
}

#
# Admin types can change anyone. 
#
if (! $isadmin &&
    $uid != $target_uid &&
    ! TBUserInfoAccessCheck($uid, $target_uid, $TB_USERINFO_MODIFYINFO)) {
    USERERROR("You do not have permission to modify information for ".
	      "user: $target_uid!", 1);
}

#
# The conclusion of a modify operation. See below.
# 
if (isset($finished)) {
    PAGEHEADER("Modify User Information");
    
    echo "<center>
          <br>
          <h3>User information successfully modified!</h3><p>
          </center>\n";
    
    SHOWUSER($target_uid);
    PAGEFOOTER();
    return;
}

#
# Suck the current info out of the database.
#
$query_result =
    DBQueryFatal("select * from users where uid='$target_uid'");

if (mysql_num_rows($query_result) == 0) {
    USERERROR("No such user: $target_uid!", 1);
}

342 343 344 345 346 347 348 349 350 351 352 353 354
$defaults = array();
    
#
# Construct a defaults array based on current DB info. Used for the initial
# form, and to determine if any changes were made. This is to avoid churning
# the passwd file for no reason, given that most people use this page
# simply yo change their password. 
# 
$row = mysql_fetch_array($query_result);
$defaults[target_uid]  = $target_uid;
$defaults[usr_email]   = $row[usr_email];
$defaults[usr_URL]     = $row[usr_URL];
$defaults[usr_addr]    = stripslashes($row[usr_addr]);
355 356 357 358
$defaults[usr_addr2]   = stripslashes($row[usr_addr2]);
$defaults[usr_city]    = stripslashes($row[usr_city]);
$defaults[usr_state]   = stripslashes($row[usr_state]);
$defaults[usr_zip]     = stripslashes($row[usr_zip]);
359
$defaults[usr_country] = stripslashes($row[usr_country]);
360 361 362 363
$defaults[usr_name]    = stripslashes($row[usr_name]);
$defaults[usr_phone]   = $row[usr_phone];
$defaults[usr_title]   = stripslashes($row[usr_title]);
$defaults[usr_affil]   = stripslashes($row[usr_affil]);
364
$defaults[usr_shell]   = stripslashes($row[usr_shell]);
365

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
#
# On first load, display a form consisting of current user values, and exit.
#
if (! isset($submit)) {
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

#
# Otherwise, must validate and redisplay if errors
#
$errors = array();

#
# These fields are required!
#
if (!isset($formfields[usr_title]) ||
    strcmp($formfields[usr_title], "") == 0) {
    $errors["Title/Position"] = "Missing Field";
}
if (!isset($formfields[usr_name]) ||
    strcmp($formfields[usr_name], "") == 0) {
    $errors["Full Name"] = "Missing Field";
}
if (!isset($formfields[usr_affil]) ||
    strcmp($formfields[usr_affil], "") == 0) {
    $errors["Affiliation"] = "Missing Field";
}
395 396 397 398
if (!isset($formfields[usr_shell]) ||
    !in_array($formfields[usr_shell], $shelllist)) {
    $errors["Shell"] = "Invalid Shell";
}
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
if (!isset($formfields[usr_email]) ||
    strcmp($formfields[usr_email], "") == 0) {
    $errors["Email Address"] = "Missing Field";
}
else {
    $usr_email    = $formfields[usr_email];
    $email_domain = strstr($usr_email, "@");
    
    if (! $email_domain ||
	strcmp($usr_email, $email_domain) == 0 ||
	strlen($email_domain) <= 1 ||
	! strstr($email_domain, ".")) {
	$errors["Email Address"] = "Looks invalid!";
    }
}
if (isset($formfields[usr_URL]) &&
    strcmp($formfields[usr_URL], "") &&
    strcmp($formfields[usr_URL], $HTTPTAG) &&
    ! CHECKURL($formfields[usr_URL], $urlerror)) {
    $errors["Home Page URL"] = $urlerror;
}
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
if (!$isadmin) {
    if (!isset($formfields[usr_addr]) ||
	strcmp($formfields[usr_addr], "") == 0) {
	$errors["Postal Address"] = "Missing Field";
    }
    if (!isset($formfields[usr_city]) ||
	strcmp($formfields[usr_city], "") == 0) {
	$errors["City"] = "Missing Field";
    }
    if (!isset($formfields[usr_state]) ||
	strcmp($formfields[usr_state], "") == 0) {
	$errors["State"] = "Missing Field";
    }
    if (!isset($formfields[usr_zip]) ||
	strcmp($formfields[usr_zip], "") == 0) {
	$errors["ZIP/Postal Code"] = "Missing Field";
    }
    if (!isset($formfields[usr_country]) ||
	strcmp($formfields[usr_country], "") == 0) {
	$errors["Country"] = "Missing Field";
    }
    if (!isset($formfields[usr_phone]) ||
	strcmp($formfields[usr_phone], "") == 0) {
	$errors["Phone #"] = "Missing Field";
    } 
    elseif (! ereg("^[-0-9ext\(\)\+\. ]+$", $formfields[usr_phone])) {
        $errors["Phone #"] = "Invalid characters";
    }
448 449 450 451 452
}
if (isset($formfields[password1]) &&
    strcmp($formfields[password1], "")) {
    if (!isset($formfields[password2]) ||
	strcmp($formfields[password2], "") == 0) {
453
	$errors["Retype Password"] = "Missing Field";
454 455
    }
    elseif (strcmp($formfields[password1], $formfields[password2])) {
456
	$errors["Retype Password"] = "Two Passwords Do Not Match";
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    }
    elseif (! CHECKPASSWORD($formfields[target_uid],
			    $formfields[password1],
			    $formfields[usr_name],
			    $formfields[usr_email], $checkerror)) {
	$errors["Password"] = "$checkerror";
    }
}
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

$usr_title    = addslashes($formfields[usr_title]);
$usr_name     = addslashes($formfields[usr_name]);
$usr_affil    = addslashes($formfields[usr_affil]);
$usr_email    = $formfields[usr_email];
$usr_addr     = addslashes($formfields[usr_addr]);
476 477 478
$usr_city     = addslashes($formfields[usr_city]);
$usr_state    = addslashes($formfields[usr_state]);
$usr_zip      = addslashes($formfields[usr_zip]);
479
$usr_country  = addslashes($formfields[usr_country]);
480
$usr_phone    = $formfields[usr_phone];
481
$usr_shell    = $formfields[usr_shell];
482 483 484 485 486 487 488 489 490 491 492 493
$password1    = $formfields[password1];
$password2    = $formfields[password2];

if (! isset($formfields[usr_URL]) ||
    strcmp($formfields[usr_URL], "") == 0 ||
    strcmp($formfields[usr_URL], $HTTPTAG) == 0) {
    $usr_URL = "";
}
else {
    $usr_URL = $formfields[usr_URL];
}

494 495 496 497 498 499 500
if (! isset($formfields[usr_addr2])) {
    $usr_addr2 = "";
}
else {
    $usr_addr2 = addslashes($formfields[usr_addr2]);
}

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
#
# Check that email address looks reasonable. Only admin types can change
# the email address. If its different, the user circumvented the form,
# and so its okay to blast it.
#
TBUserInfo($target_uid, $dbusr_name, $dbusr_email);

if (strcmp($usr_email, $dbusr_email)) {
    if (!$isadmin) {
	USERERROR("You are not allowed to change your password. <br> ".
		  "Please contact Testbed Operations.", 1);
    }
    DBQueryFatal("update users set usr_email='$usr_email' ".
		 "where uid='$target_uid'");
}

#
# Now see if the user is requesting to change the password. We checked
# them above when the form was submitted.
#
if ((isset($password1) && strcmp($password1, "")) &&
    (isset($password2) && strcmp($password2, ""))) {
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

    $query_result =
	DBQueryFatal("select usr_pswd from users WHERE uid='$target_uid'");
    if (! mysql_num_rows($query_result)) {
	TBERROR("Error getting usr_pswd for $target_uid", 1);
    }
    $row = mysql_fetch_array($query_result);
    $old_encoding = $row[usr_pswd];
    $new_encoding = crypt("$password1", $old_encoding);

    #
    # Compare. Must change it!
    # 
    if (! strcmp($old_encoding, $new_encoding)) {
	$errors["New Password"] = "New password is the same as old password";

	SPITFORM($formfields, $errors);
	PAGEFOOTER();
	return;
    }

544 545 546 547 548 549
    #
    # Do it again. This ensures we use the current algorithm, not whatever
    # it was encoded with last time.
    #
    $new_encoding = crypt("$password1");

550 551 552 553 554 555 556 557 558 559 560
    #
    # Insert into database. When changing password for someone else,
    # always set the expiration to right now so that the target user
    # is "forced" to change it. 
    #
    if ($uid != $target_uid)
	$expires = "now()";
    else
	$expires = "date_add(now(), interval 1 year)";
    
    $insert_result =
561
	DBQueryFatal("UPDATE users SET usr_pswd='$new_encoding', ".
562 563 564
		     "pswd_expires=$expires ".
		     "WHERE uid='$target_uid'");

565 566 567
    if (HASREALACCOUNT($uid)) {
	SUEXEC($uid, "nobody", "webtbacct passwd $target_uid", 1);
    }
568 569 570
}

#
571 572 573
# Now change the rest of the information, but only if the user actually
# changed the info. We use the original info in the defaults array and
# the value in the formfields array to compare, cause of addslashes stuff. 
574
#
575 576 577
if (strcmp($defaults[usr_name],  $formfields[usr_name]) ||
    strcmp($defaults[usr_URL],   $formfields[usr_URL]) ||
    strcmp($defaults[usr_addr],  $formfields[usr_addr]) ||
578 579 580 581
    strcmp($defaults[usr_addr2], $formfields[usr_addr2]) ||
    strcmp($defaults[usr_city],  $formfields[usr_city]) ||
    strcmp($defaults[usr_state], $formfields[usr_state]) ||
    strcmp($defaults[usr_zip],   $formfields[usr_zip]) ||
582
    strcmp($defaults[usr_country],   $formfields[usr_country]) ||
583 584 585
    strcmp($defaults[usr_phone], $formfields[usr_phone]) ||
    strcmp($defaults[usr_title], $formfields[usr_title]) ||
    strcmp($defaults[usr_affil], $formfields[usr_affil]) ||
586
    strcmp($defaults[usr_shell], $formfields[usr_shell]) ||
587 588 589 590 591 592 593
    # Check this too, since we want to call out if the email addr changed.
    strcmp($defaults[usr_email], $formfields[usr_email])) {

    DBQueryFatal("UPDATE users SET ".
		 "usr_name=\"$usr_name\",       ".
		 "usr_URL=\"$usr_URL\",         ".
		 "usr_addr=\"$usr_addr\",       ".
594 595 596 597
		 "usr_addr2=\"$usr_addr2\",     ".
		 "usr_city=\"$usr_city\",       ".
		 "usr_state=\"$usr_state\",     ".
		 "usr_zip=\"$usr_zip\",         ".
598
		 "usr_country=\"$usr_country\", ".
599 600 601
		 "usr_phone=\"$usr_phone\",     ".
		 "usr_title=\"$usr_title\",     ".
		 "usr_affil=\"$usr_affil\",     ".
602
		 "usr_shell=\"$usr_shell\",     ".
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
		 "usr_modified=now()            ".
		 "WHERE uid=\"$target_uid\"");

    # Only to user. We care about password and email changes only.
    $BCC = "";
    if (strcmp($usr_email, $dbusr_email)) {
	$BCC = "Bcc: $TBMAIL_AUDIT\n";
    }
    
    TBMAIL("$usr_name <$usr_email>",
	   "User Information for '$target_uid' Modified",
	   "\n".
	   "User information for '$target_uid' changed by '$uid'.\n".
	   "\n".
	   "Name:              $usr_name\n".
	   "Email:             $usr_email\n".
	   "URL:               $usr_URL\n".
	   "Affiliation:       $usr_affil\n".
621 622 623 624
	   "Address1:          $usr_addr\n".
	   "Address2:          $usr_addr2\n".
	   "City:              $usr_city\n".
	   "State:             $usr_state\n".
625 626
	   "ZIP/Postal Code:   $usr_zip\n".
	   "Country:           $usr_country\n".
627
	   "Phone:             $usr_phone\n".
628 629
	   "Title:             $usr_title\n".
	   "Shell:             $usr_shell\n",
630 631 632
	   "From: $TBMAIL_OPS\n".
	   $BCC .
	   "Errors-To: $TBMAIL_WWW");
633

634 635 636 637 638 639 640
    #
    # mkacct updates the user gecos
    #
    if (HASREALACCOUNT($uid)) {
	SUEXEC($uid, "nobody", "webtbacct mod $target_uid", 1);
    }
}
641 642 643 644 645 646 647 648 649

#
# Spit out a redirect so that the history does not include a post
# in it. The back button skips over the post and to the form.
# See above for conclusion.
# 
header("Location: moduserinfo.php3?target_uid=$target_uid&finished=1");

?>