chpasswd.php3 6.17 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2007, 2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25
#
include("defs.php3");

26 27 28 29 30 31 32 33 34
#
# 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.
Leigh Stoller's avatar
Leigh Stoller committed
35 36 37 38 39 40 41
if (isset($simple) && $simple) {
    $simple = 1;
    $view = array('hide_banner' => 1,
		  'hide_copyright' => 1,
		  'hide_sidebar' => 1);
}
else {
42
    $simple = 0;
Leigh Stoller's avatar
Leigh Stoller committed
43
    $view   = array();
44 45
}

46 47
# Half the key in the URL.
$keyB = $key;
48
# We also need the other half of the key from the browser.
49
$keyA = (isset($_COOKIE[$TBAUTHCOOKIE]) ? $_COOKIE[$TBAUTHCOOKIE] : "");
50

51 52 53 54 55 56
# 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 ".
57
	      "<a href='$WIKIDOCURL/kb69'>".
58 59 60
	      "Knowledge Base Entry</a> to see what the likely cause is.", 1);
}

61
if (!isset($user) || $user == "" || !User::ValidWebID($user) ||
62 63 64 65 66 67 68 69
    !isset($keyA) || $keyA == "" || !preg_match("/^[\w]+$/", $keyA) ||
    !isset($keyB) || $keyB == "" || !preg_match("/^[\w]+$/", $keyB)) {
    PAGEARGERROR();
}
# The complete key.
$key = $keyA . $keyB;

# Must use https!
70
if (!isset($_SERVER["SSL_PROTOCOL"])) {
71 72 73 74 75 76 77
    PAGEHEADER("Reset Your Password", $view);
    USERERROR("Must use https:// to access this page!", 1);
}

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

81
    echo "<h3>
82
              You are logged in. You must already know your password!
83
          </h3>\n";
84

85 86
    PAGEFOOTER($view);
    die("");
87 88 89 90 91
}

#
# Spit out the form.
# 
92
function SPITFORM($target_user, $key, $failed, $simple, $view)
93 94
{
    global	$TBBASE;
95 96

    $uid = $target_user->uid();
97 98 99 100
    
    PAGEHEADER("Reset Your Password", $view);

    if ($failed) {
101 102
	# XSS prevention
	$failed = CleanString($failed);
103 104 105 106 107 108 109 110 111 112 113 114 115 116
	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";
    }

117
    $chpass_url = CreateURL("chpasswd", $target_user,
118
			    "key", urlencode($key), "simple", $simple);
119 120
	
    echo "<table align=center border=1>
121
          <form action='${TBBASE}/$chpass_url' method=post>\n";
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

    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.
#
154 155 156 157 158 159 160
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();
161 162

# Silent error when there is no key/timeout set for the user.
163 164
if (!$target_user->chpasswd_key() || !$target_user->chpasswd_expires()) {
    PAGEARGERROR();
165
}
166
if ($target_user->chpasswd_key() != $key) {
167 168
    USERERROR("You do not have permission to change your password!", 1);
}
169
if (time() > $target_user->chpasswd_expires()) {
170 171 172 173 174 175 176 177
    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)) {
178 179
    SPITFORM($target_user, $keyB, 0, $simple, $view);
    PAGEFOOTER();
180 181 182 183 184 185 186 187 188 189 190
    return;
}

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

if (!isset($password1) || $password1 == "" ||
    !isset($password2) || $password2 == "") {
191 192 193
    SPITFORM($target_user, $keyB,
	     "You must supply a password", $simple, $view);
    PAGEFOOTER();
194 195 196
    return;
}
if ($password1 != $password2) {
197 198 199
    SPITFORM($target_user, $keyB,
	     "Two passwords do not match", $simple, $view);
    PAGEFOOTER();
200 201
    return;
}
202 203 204 205
if (! CHECKPASSWORD($target_uid,
		    $password1, $usr_name, $usr_email, $checkerror)){
    SPITFORM($target_user, $keyB, $checkerror, $simple, $view);
    PAGEFOOTER();
206 207 208 209 210 211 212 213 214 215
    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");
216
$safe_encoding = escapeshellarg($encoding);
217

218
STARTBUSY("Resetting your password");
219

220 221 222 223 224 225
#
# Invoke backend to deal with this.
#
if (!HASREALACCOUNT($target_uid)) {
    SUEXEC("nobody", "nobody",
	   "webtbacct passwd $target_uid $safe_encoding",
226
	   SUEXEC_ACTION_DIE);
227 228 229 230 231 232 233 234
}
else {
    SUEXEC($target_uid, "nobody",
	   "webtbacct passwd $target_uid $safe_encoding",
	   SUEXEC_ACTION_DIE);
}

CLEARBUSY();
235 236 237 238 239 240 241 242 243

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

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