| 1 |
<?PHP |
|---|
| 2 |
/* |
|---|
| 3 |
* Copyright (c) 2006 Gregor Richards |
|---|
| 4 |
* |
|---|
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
|---|
| 6 |
* copy of this software and associated documentation files (the "Software"), |
|---|
| 7 |
* to deal in the Software without restriction, including without limitation |
|---|
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|---|
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
|---|
| 10 |
* Software is furnished to do so, subject to the following conditions: |
|---|
| 11 |
* |
|---|
| 12 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 13 |
* all copies or substantial portions of the Software. |
|---|
| 14 |
* |
|---|
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 21 |
* DEALINGS IN THE SOFTWARE. |
|---|
| 22 |
*/ |
|---|
| 23 |
|
|---|
| 24 |
require("pws.php"); |
|---|
| 25 |
|
|---|
| 26 |
// Check a password, return true or false |
|---|
| 27 |
function chkpw($un, $pw) |
|---|
| 28 |
{ |
|---|
| 29 |
global $pws; |
|---|
| 30 |
if (isset($pws[$un]) && |
|---|
| 31 |
$pws[$un] == sha1($pw)) return true; |
|---|
| 32 |
return false; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
// Generate a key/hash = array(key, hash) |
|---|
| 36 |
function genkeyhash($un) |
|---|
| 37 |
{ |
|---|
| 38 |
global $pws; |
|---|
| 39 |
if (!isset($pws[$un])) return false; |
|---|
| 40 |
|
|---|
| 41 |
$key = time(); |
|---|
| 42 |
$prehash = $pws[$un] . "DSSSSMI" . $key; |
|---|
| 43 |
$hash = sha1($prehash); |
|---|
| 44 |
|
|---|
| 45 |
return array($key, $hash); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
// Check a key/hash, return true or false |
|---|
| 49 |
function chkkeyhash($un, $key, $hash) |
|---|
| 50 |
{ |
|---|
| 51 |
global $pws; |
|---|
| 52 |
if (!isset($pws[$un])) return false; |
|---|
| 53 |
|
|---|
| 54 |
// key has expired |
|---|
| 55 |
$tm = time(); |
|---|
| 56 |
if ($key < $tm - 3600 || |
|---|
| 57 |
$key > $tm) return false; |
|---|
| 58 |
|
|---|
| 59 |
// key is wrong |
|---|
| 60 |
$prehash = $pws[$un] . "DSSSSMI" . $key; |
|---|
| 61 |
$goodhash = sha1($prehash); |
|---|
| 62 |
if ($hash != $goodhash) return false; |
|---|
| 63 |
|
|---|
| 64 |
return true; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
// Validate the login, DIES on failure |
|---|
| 68 |
function validate() |
|---|
| 69 |
{ |
|---|
| 70 |
global $pws; |
|---|
| 71 |
$failmsg = "Invalid login, perhaps your session has expired."; |
|---|
| 72 |
|
|---|
| 73 |
if (!isset($_COOKIE['DSSS_SMI_UN']) || |
|---|
| 74 |
!isset($_COOKIE['DSSS_SMI_KEY']) || |
|---|
| 75 |
!isset($_COOKIE['DSSS_SMI_HASH'])) |
|---|
| 76 |
die($failmsg); |
|---|
| 77 |
|
|---|
| 78 |
global $un; |
|---|
| 79 |
$un = $_COOKIE['DSSS_SMI_UN']; |
|---|
| 80 |
|
|---|
| 81 |
if (!chkkeyhash($_COOKIE['DSSS_SMI_UN'], |
|---|
| 82 |
$_COOKIE['DSSS_SMI_KEY'], |
|---|
| 83 |
$_COOKIE['DSSS_SMI_HASH'])) |
|---|
| 84 |
die($failmsg); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
// Export the pws array, return false on failure |
|---|
| 88 |
function savepws() |
|---|
| 89 |
{ |
|---|
| 90 |
global $pws; |
|---|
| 91 |
|
|---|
| 92 |
$handle = fopen("pws.php", "w"); |
|---|
| 93 |
if ($handle === false) return false; |
|---|
| 94 |
|
|---|
| 95 |
flock($handle, LOCK_EX); |
|---|
| 96 |
|
|---|
| 97 |
fwrite($handle, "<?PHP\n" . |
|---|
| 98 |
"\$pws = array(\n"); |
|---|
| 99 |
foreach (array_keys($pws) as $un) { |
|---|
| 100 |
if ($un == "0") continue; |
|---|
| 101 |
fwrite($handle, "\"$un\" => \"" . $pws[$un] . "\",\n"); |
|---|
| 102 |
} |
|---|
| 103 |
fwrite($handle, "0);\n" . |
|---|
| 104 |
"?>\n"); |
|---|
| 105 |
|
|---|
| 106 |
flock($handle, LOCK_UN); |
|---|
| 107 |
fclose($handle); |
|---|
| 108 |
|
|---|
| 109 |
return true; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
// Add a user, return true on success |
|---|
| 113 |
function adduser($un, $pw) |
|---|
| 114 |
{ |
|---|
| 115 |
global $pws; |
|---|
| 116 |
if (isset($pws[$un])) return false; |
|---|
| 117 |
|
|---|
| 118 |
$pws[$un] = sha1($pw); |
|---|
| 119 |
return savepws(); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
// Change a user's password: Assumes the user's old password has been checked |
|---|
| 123 |
function chpasswd($un, $newpw) |
|---|
| 124 |
{ |
|---|
| 125 |
global $pws; |
|---|
| 126 |
if (!isset($pws[$un])) return false; |
|---|
| 127 |
|
|---|
| 128 |
$pws[$un] = sha1($newpw); |
|---|
| 129 |
return savepws(); |
|---|
| 130 |
} |
|---|
| 131 |
?> |
|---|