2012-04-12 12:11:41 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2013-01-20 05:23:46 -05:00
|
|
|
* Copyright (c) 2010-2013 Tinyboard Development Group
|
2012-04-12 12:11:41 -04:00
|
|
|
*/
|
|
|
|
|
2013-09-06 06:12:04 -04:00
|
|
|
defined('TINYBOARD') or exit;
|
2012-04-12 12:11:41 -04:00
|
|
|
|
|
|
|
// create a hash/salt pair for validate logins
|
|
|
|
function mkhash($username, $password, $salt = false) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
if (!$salt) {
|
|
|
|
// create some sort of salt for the hash
|
|
|
|
$salt = substr(base64_encode(sha1(rand() . time(), true) . $config['cookies']['salt']), 0, 15);
|
|
|
|
|
|
|
|
$generated_salt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate hash (method is not important as long as it's strong)
|
2016-04-21 23:35:43 -04:00
|
|
|
$hash = substr(
|
|
|
|
base64_encode(
|
|
|
|
md5(
|
|
|
|
$username . $config['cookies']['salt'] . sha1(
|
|
|
|
$username . $password . $salt . (
|
|
|
|
$config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''
|
|
|
|
), true
|
|
|
|
) . sha1($config['password_crypt_version']) // Log out users being logged in with older password encryption schema
|
|
|
|
, true
|
|
|
|
)
|
|
|
|
), 0, 20
|
|
|
|
);
|
2012-04-12 12:11:41 -04:00
|
|
|
|
|
|
|
if (isset($generated_salt))
|
2012-08-27 07:50:15 -04:00
|
|
|
return array($hash, $salt);
|
2012-04-12 12:11:41 -04:00
|
|
|
else
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
2016-04-21 23:35:43 -04:00
|
|
|
function crypt_password_old($password) {
|
|
|
|
$salt = generate_salt();
|
|
|
|
$password = hash('sha256', $salt . sha1($password));
|
|
|
|
return array($salt, $password);
|
2013-07-24 11:15:55 -04:00
|
|
|
}
|
|
|
|
|
2016-04-21 23:35:43 -04:00
|
|
|
function crypt_password($password) {
|
|
|
|
global $config;
|
|
|
|
// `salt` database field is reused as a version value. We don't want it to be 0.
|
|
|
|
$version = $config['password_crypt_version'] ? $config['password_crypt_version'] : 1;
|
|
|
|
$new_salt = generate_salt();
|
|
|
|
$password = crypt($password, $config['password_crypt'] . $new_salt . "$");
|
|
|
|
return array($version, $password);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_password($password, $salt, $test) {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
// Version = 0 denotes an old password hashing schema. In the same column, the
|
|
|
|
// password hash was kept previously
|
|
|
|
$version = (strlen($salt) <= 8) ? (int) $salt : 0;
|
|
|
|
|
|
|
|
if ($version == 0) {
|
|
|
|
$comp = hash('sha256', $salt . sha1($test));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$comp = crypt($test, $password);
|
2012-04-12 12:11:41 -04:00
|
|
|
}
|
2016-04-21 23:35:43 -04:00
|
|
|
return array($version, hash_equals($password, $comp));
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_salt() {
|
|
|
|
// 128 bits of entropy
|
2017-05-03 20:28:54 -04:00
|
|
|
if (function_exists('random_bytes')) {
|
|
|
|
return strtr(base64_encode(random_bytes(16)), '+', '.');
|
|
|
|
} else {
|
|
|
|
return strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
|
|
|
|
}
|
2016-04-21 23:35:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function login($username, $password) {
|
|
|
|
global $mod, $config;
|
2012-04-12 12:11:41 -04:00
|
|
|
|
2014-02-12 20:04:32 -05:00
|
|
|
$query = prepare("SELECT `id`, `type`, `boards`, `password`, `version` FROM ``mods`` WHERE BINARY `username` = :username");
|
2012-04-12 12:11:41 -04:00
|
|
|
$query->bindValue(':username', $username);
|
|
|
|
$query->execute() or error(db_error($query));
|
|
|
|
|
2013-07-24 11:15:55 -04:00
|
|
|
if ($user = $query->fetch(PDO::FETCH_ASSOC)) {
|
2016-05-05 00:43:22 -04:00
|
|
|
list($version, $ok) = test_password($user['password'], $user['version'], $password);
|
2016-04-21 23:35:43 -04:00
|
|
|
|
|
|
|
if ($ok) {
|
|
|
|
if ($config['password_crypt_version'] > $version) {
|
|
|
|
// It's time to upgrade the password hashing method!
|
2016-05-05 00:43:22 -04:00
|
|
|
list ($user['version'], $user['password']) = crypt_password($password);
|
|
|
|
$query = prepare("UPDATE ``mods`` SET `password` = :password, `version` = :version WHERE `id` = :id");
|
2016-04-21 23:35:43 -04:00
|
|
|
$query->bindValue(':password', $user['password']);
|
2016-05-05 00:43:22 -04:00
|
|
|
$query->bindValue(':version', $user['version']);
|
2016-04-21 23:35:43 -04:00
|
|
|
$query->bindValue(':id', $user['id']);
|
|
|
|
$query->execute() or error(db_error($query));
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:15:55 -04:00
|
|
|
return $mod = array(
|
|
|
|
'id' => $user['id'],
|
|
|
|
'type' => $user['type'],
|
|
|
|
'username' => $username,
|
|
|
|
'hash' => mkhash($username, $user['password']),
|
|
|
|
'boards' => explode(',', $user['boards'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-04-12 12:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function setCookies() {
|
|
|
|
global $mod, $config;
|
|
|
|
if (!$mod)
|
|
|
|
error('setCookies() was called for a non-moderator!');
|
|
|
|
|
|
|
|
setcookie($config['cookies']['mod'],
|
|
|
|
$mod['username'] . // username
|
|
|
|
':' .
|
|
|
|
$mod['hash'][0] . // password
|
|
|
|
':' .
|
|
|
|
$mod['hash'][1], // salt
|
2014-06-11 00:07:45 -04:00
|
|
|
time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off', $config['cookies']['httponly']);
|
2012-04-12 12:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function destroyCookies() {
|
|
|
|
global $config;
|
|
|
|
// Delete the cookies
|
2014-06-11 00:07:45 -04:00
|
|
|
setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off', true);
|
2012-04-12 12:11:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function modLog($action, $_board=null) {
|
|
|
|
global $mod, $board, $config;
|
2013-07-31 22:14:26 -04:00
|
|
|
$query = prepare("INSERT INTO ``modlogs`` VALUES (:id, :ip, :board, :time, :text)");
|
2015-02-25 20:21:49 -05:00
|
|
|
$query->bindValue(':id', (isset($mod['id']) ? $mod['id'] : -1), PDO::PARAM_INT);
|
2012-04-12 12:11:41 -04:00
|
|
|
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
|
|
|
|
$query->bindValue(':time', time(), PDO::PARAM_INT);
|
|
|
|
$query->bindValue(':text', $action);
|
|
|
|
if (isset($_board))
|
|
|
|
$query->bindValue(':board', $_board);
|
|
|
|
elseif (isset($board))
|
|
|
|
$query->bindValue(':board', $board['uri']);
|
|
|
|
else
|
|
|
|
$query->bindValue(':board', null, PDO::PARAM_NULL);
|
|
|
|
$query->execute() or error(db_error($query));
|
|
|
|
|
|
|
|
if ($config['syslog'])
|
|
|
|
_syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:00:40 -04:00
|
|
|
function create_pm_header() {
|
2012-05-20 05:06:27 -04:00
|
|
|
global $mod, $config;
|
|
|
|
|
2013-03-27 10:09:39 -04:00
|
|
|
if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) != false) {
|
2012-05-20 05:06:27 -04:00
|
|
|
if ($header === true)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return $header;
|
|
|
|
}
|
|
|
|
|
2013-07-31 22:14:26 -04:00
|
|
|
$query = prepare("SELECT `id` FROM ``pms`` WHERE `to` = :id AND `unread` = 1");
|
2012-04-13 08:00:40 -04:00
|
|
|
$query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
|
|
|
|
$query->execute() or error(db_error($query));
|
|
|
|
|
2013-07-31 20:51:43 -04:00
|
|
|
if ($pm = $query->fetch(PDO::FETCH_ASSOC))
|
2012-08-27 07:50:15 -04:00
|
|
|
$header = array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1);
|
2012-05-20 05:06:27 -04:00
|
|
|
else
|
|
|
|
$header = true;
|
|
|
|
|
|
|
|
if ($config['cache']['enabled'])
|
|
|
|
cache::set('pm_unread_' . $mod['id'], $header);
|
|
|
|
|
|
|
|
if ($header === true)
|
|
|
|
return false;
|
2012-04-13 08:00:40 -04:00
|
|
|
|
2012-05-20 05:06:27 -04:00
|
|
|
return $header;
|
2012-04-13 08:00:40 -04:00
|
|
|
}
|
|
|
|
|
2012-08-27 01:19:05 -04:00
|
|
|
function make_secure_link_token($uri) {
|
|
|
|
global $mod, $config;
|
|
|
|
return substr(sha1($config['cookies']['salt'] . '-' . $uri . '-' . $mod['id']), 0, 8);
|
|
|
|
}
|
|
|
|
|
2015-01-01 01:35:21 -05:00
|
|
|
function check_login($prompt = false) {
|
|
|
|
global $config, $mod;
|
|
|
|
// Validate session
|
|
|
|
if (isset($_COOKIE[$config['cookies']['mod']])) {
|
|
|
|
// Should be username:hash:salt
|
|
|
|
$cookie = explode(':', $_COOKIE[$config['cookies']['mod']]);
|
|
|
|
if (count($cookie) != 3) {
|
|
|
|
// Malformed cookies
|
|
|
|
destroyCookies();
|
|
|
|
if ($prompt) mod_login();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = prepare("SELECT `id`, `type`, `boards`, `password` FROM ``mods`` WHERE `username` = :username");
|
|
|
|
$query->bindValue(':username', $cookie[0]);
|
|
|
|
$query->execute() or error(db_error($query));
|
|
|
|
$user = $query->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
|
|
|
// validate password hash
|
|
|
|
if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) {
|
|
|
|
// Malformed cookies
|
|
|
|
destroyCookies();
|
|
|
|
if ($prompt) mod_login();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mod = array(
|
|
|
|
'id' => $user['id'],
|
|
|
|
'type' => $user['type'],
|
|
|
|
'username' => $cookie[0],
|
|
|
|
'boards' => explode(',', $user['boards'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|