chpasswd.php3 5.34 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003, 2005, 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15
# All rights reserved.
#
include("defs.php3");

# Display a simpler version of this page
$simple = 0;
if (isset($_REQUEST['simple'])) {
    $simple = $_REQUEST['simple'];
}

# Form arguments.
16
$user      = $_REQUEST['user'];
17 18 19 20
$keyB      = $_REQUEST['key'];
# We also need the other half of the key from the browser.
$keyA      = $HTTP_COOKIE_VARS[$TBAUTHCOOKIE];

21 22 23 24 25 26 27 28 29 30
# If the browser part is missing, direct user to answer
if ((isset($keyB) && $keyB != "") && (!isset($keyA) || $keyA == "")) {
    PAGEHEADER("Reset Your Password", $view);

    USERERROR("Oops, not able to proceed!<br>".
	      "Please read this ".
	      "<a href='kb-show.php3?xref_tag=forgotpassword'>".
	      "Knowledge Base Entry</a> to see what the likely cause is.", 1);
}

31
if (!isset($user) || $user == "" || !User::ValidWebID($user) ||
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
    !isset($keyA) || $keyA == "" || !preg_match("/^[\w]+$/", $keyA) ||
    !isset($keyB) || $keyB == "" || !preg_match("/^[\w]+$/", $keyB)) {
    PAGEARGERROR();
}
# The complete key.
$key = $keyA . $keyB;

# Must use https!
if (!isset($SSL_PROTOCOL)) {
    PAGEHEADER("Reset Your Password", $view);
    USERERROR("Must use https:// to access this page!", 1);
}

#
# Turn off some of the decorations and menus for the simple view
#
if ($simple) {
    $view = array('hide_banner' => 1, 'hide_copyright' => 1,
	'hide_sidebar' => 1);
} else {
    $view = array();
}

#
# Must not be logged in.
# 
58 59
if (GETLOGIN() != FALSE) {
    PAGEHEADER("Reset Your Password", $view);
60

61
    echo "<h3>
62
              You are logged in. You must already know your password!
63
          </h3>\n";
64

65 66
    PAGEFOOTER($view);
    die("");
67 68 69 70 71
}

#
# Spit out the form.
# 
72
function SPITFORM($target_user, $key, $failed, $simple, $view)
73 74
{
    global	$TBBASE;
75 76

    $uid = $target_user->uid();
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    
    PAGEHEADER("Reset Your Password", $view);

    if ($failed) {
	echo "<center>
              <font size=+1 color=red>
              $failed. Please try again.
              </font>
              </center><br>\n";
    }
    else {
	echo "<center>
              <font size=+1>
              Please enter a new password.<br><br>
              </font>
              </center>\n";
    }

95 96
    $chpass_url = CreateURL("chpasswd", $target_user,
			    "key", $key, "simple", $simple);
97 98
	
    echo "<table align=center border=1>
99
          <form action='${TBBASE}/$chpass_url' method=post>\n";
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

    echo "<tr>
              <td>Password:</td>
              <td class=left>
                  <input type=password
                         name=\"password1\"
                         size=12></td>
          </tr>\n";

    echo "<tr>
              <td>Retype Password:</td>
              <td class=left>
                  <input type=password
                         name=\"password2\"
                         size=12></td>
         </tr>\n";

    echo "<tr>
             <td align=center colspan=2>
                 <b><input type=submit value=\"Reset Password\"
                           name=reset></b></td>
          </tr>\n";
    

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

#
# Check to make sure that the key is valid and that the timeout has not
# expired.
#
132 133 134 135 136 137 138
if (! ($target_user = User::Lookup($user))) {
    # Silent error about invalid users.
    PAGEARGERROR();
}
$usr_email  = $target_user->email();
$usr_name   = $target_user->name();
$target_uid = $target_user->uid();
139 140

# Silent error when there is no key/timeout set for the user.
141 142
if (!$target_user->chpasswd_key() || !$target_user->chpasswd_expires()) {
    PAGEARGERROR();
143
}
144
if ($target_user->chpasswd_key() != $key) {
145 146
    USERERROR("You do not have permission to change your password!", 1);
}
147
if (time() > $target_user->chpasswd_expires()) {
148 149 150 151 152 153 154 155
    USERERROR("Your key has expired. Please request a
               <a href='password.php3'>new key</a>.", 1);
}

#
# If not clicked, then put up a form.
#
if (! isset($reset)) {
156 157
    SPITFORM($target_user, $keyB, 0, $simple, $view);
    PAGEFOOTER();
158 159 160 161 162 163 164 165 166 167 168
    return;
}

#
# Reset clicked. Verify a proper password. 
#
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];

if (!isset($password1) || $password1 == "" ||
    !isset($password2) || $password2 == "") {
169 170 171
    SPITFORM($target_user, $keyB,
	     "You must supply a password", $simple, $view);
    PAGEFOOTER();
172 173 174
    return;
}
if ($password1 != $password2) {
175 176 177
    SPITFORM($target_user, $keyB,
	     "Two passwords do not match", $simple, $view);
    PAGEFOOTER();
178 179
    return;
}
180 181 182 183
if (! CHECKPASSWORD($target_uid,
		    $password1, $usr_name, $usr_email, $checkerror)){
    SPITFORM($target_user, $keyB, $checkerror, $simple, $view);
    PAGEFOOTER();
184 185 186 187 188 189 190 191 192 193
    return;
}

# Clear the cookie from the browser.
setcookie($TBAUTHCOOKIE, "", time() - 1000000, "/", $TBAUTHDOMAIN, 0);

# Okay to spit this now that the cookie has been sent (cleared).
PAGEHEADER("Reset Your Password", $view);

$encoding = crypt("$password1");
194
$safe_encoding = escapeshellarg($encoding);
195

196
STARTBUSY("Resetting your password");
197

198 199 200 201 202 203
#
# Invoke backend to deal with this.
#
if (!HASREALACCOUNT($target_uid)) {
    SUEXEC("nobody", "nobody",
	   "webtbacct passwd $target_uid $safe_encoding",
204
	   SUEXEC_ACTION_DIE);
205 206 207 208 209 210 211 212
}
else {
    SUEXEC($target_uid, "nobody",
	   "webtbacct passwd $target_uid $safe_encoding",
	   SUEXEC_ACTION_DIE);
}

CLEARBUSY();
213 214 215 216 217 218 219 220 221

echo "<br>
      Your password has been changed.\n";

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