chpasswd.php3 5.49 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
5 6 7 8
# All rights reserved.
#
include("defs.php3");

9 10 11 12 13 14 15 16 17 18 19
#
# Verify page arguments.
#
$reqargs = RequiredPageArguments("user",   PAGEARG_STRING);
$optargs = OptionalPageArguments("simple", PAGEARG_BOOLEAN,
				 "key",    PAGEARG_STRING,
				 "reset",  PAGEARG_STRING);

# Display a simpler version of this page.
if (!isset($simple)) {
    $simple = 0;
20 21
}

22 23
# Half the key in the URL.
$keyB = $key;
24
# We also need the other half of the key from the browser.
25
$keyA = (isset($_COOKIE[$TBAUTHCOOKIE]) ? $_COOKIE[$TBAUTHCOOKIE] : "");
26

27 28 29 30 31 32 33 34 35 36
# 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);
}

37
if (!isset($user) || $user == "" || !User::ValidWebID($user) ||
38 39 40 41 42 43 44 45
    !isset($keyA) || $keyA == "" || !preg_match("/^[\w]+$/", $keyA) ||
    !isset($keyB) || $keyB == "" || !preg_match("/^[\w]+$/", $keyB)) {
    PAGEARGERROR();
}
# The complete key.
$key = $keyA . $keyB;

# Must use https!
46
if (!isset($_SERVER["SSL_PROTOCOL"])) {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    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.
# 
64 65
if (GETLOGIN() != FALSE) {
    PAGEHEADER("Reset Your Password", $view);
66

67
    echo "<h3>
68
              You are logged in. You must already know your password!
69
          </h3>\n";
70

71 72
    PAGEFOOTER($view);
    die("");
73 74 75 76 77
}

#
# Spit out the form.
# 
78
function SPITFORM($target_user, $key, $failed, $simple, $view)
79 80
{
    global	$TBBASE;
81 82

    $uid = $target_user->uid();
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    
    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";
    }

101 102
    $chpass_url = CreateURL("chpasswd", $target_user,
			    "key", $key, "simple", $simple);
103 104
	
    echo "<table align=center border=1>
105
          <form action='${TBBASE}/$chpass_url' method=post>\n";
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

    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.
#
138 139 140 141 142 143 144
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();
145 146

# Silent error when there is no key/timeout set for the user.
147 148
if (!$target_user->chpasswd_key() || !$target_user->chpasswd_expires()) {
    PAGEARGERROR();
149
}
150
if ($target_user->chpasswd_key() != $key) {
151 152
    USERERROR("You do not have permission to change your password!", 1);
}
153
if (time() > $target_user->chpasswd_expires()) {
154 155 156 157 158 159 160 161
    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)) {
162 163
    SPITFORM($target_user, $keyB, 0, $simple, $view);
    PAGEFOOTER();
164 165 166 167 168 169 170 171 172 173 174
    return;
}

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

if (!isset($password1) || $password1 == "" ||
    !isset($password2) || $password2 == "") {
175 176 177
    SPITFORM($target_user, $keyB,
	     "You must supply a password", $simple, $view);
    PAGEFOOTER();
178 179 180
    return;
}
if ($password1 != $password2) {
181 182 183
    SPITFORM($target_user, $keyB,
	     "Two passwords do not match", $simple, $view);
    PAGEFOOTER();
184 185
    return;
}
186 187 188 189
if (! CHECKPASSWORD($target_uid,
		    $password1, $usr_name, $usr_email, $checkerror)){
    SPITFORM($target_user, $keyB, $checkerror, $simple, $view);
    PAGEFOOTER();
190 191 192 193 194 195 196 197 198 199
    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");
200
$safe_encoding = escapeshellarg($encoding);
201

202
STARTBUSY("Resetting your password");
203

204 205 206 207 208 209
#
# Invoke backend to deal with this.
#
if (!HASREALACCOUNT($target_uid)) {
    SUEXEC("nobody", "nobody",
	   "webtbacct passwd $target_uid $safe_encoding",
210
	   SUEXEC_ACTION_DIE);
211 212 213 214 215 216 217 218
}
else {
    SUEXEC($target_uid, "nobody",
	   "webtbacct passwd $target_uid $safe_encoding",
	   SUEXEC_ACTION_DIE);
}

CLEARBUSY();
219 220 221 222 223 224 225 226 227

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

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