2012-04-12 18:11:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2013-01-20 11:23:46 +01:00
|
|
|
* Copyright (c) 2010-2013 Tinyboard Development Group
|
2012-04-12 18:11:41 +02:00
|
|
|
*/
|
|
|
|
|
2024-04-30 11:31:06 +02:00
|
|
|
use Vichan\Functions\Net;
|
|
|
|
|
2013-09-06 12:12:04 +02:00
|
|
|
defined('TINYBOARD') or exit;
|
2012-04-12 18:11:41 +02:00
|
|
|
|
|
|
|
// create a hash/salt pair for validate logins
|
2024-05-11 00:04:20 +02:00
|
|
|
function mkhash(string $username, string $password, mixed $salt = false): array|string {
|
2012-04-12 18:11:41 +02:00
|
|
|
global $config;
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2012-04-12 18:11:41 +02:00
|
|
|
if (!$salt) {
|
|
|
|
// create some sort of salt for the hash
|
|
|
|
$salt = substr(base64_encode(sha1(rand() . time(), true) . $config['cookies']['salt']), 0, 15);
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2012-04-12 18:11:41 +02:00
|
|
|
$generated_salt = true;
|
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2012-04-12 18:11:41 +02:00
|
|
|
// generate hash (method is not important as long as it's strong)
|
2016-04-22 05:35:43 +02: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
|
|
|
|
);
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
if (isset($generated_salt)) {
|
|
|
|
return [ $hash, $salt ];
|
|
|
|
} else {
|
2012-04-12 18:11:41 +02:00
|
|
|
return $hash;
|
2024-04-30 19:34:26 +02:00
|
|
|
}
|
2012-04-12 18:11:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function crypt_password(string $password): array {
|
2016-04-22 05:35:43 +02:00
|
|
|
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 . "$");
|
2024-04-30 19:34:26 +02:00
|
|
|
return [ $version, $password ];
|
2016-04-22 05:35:43 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function test_password(string $password, string $salt, string $test): array {
|
2016-04-22 05:35:43 +02:00
|
|
|
// Version = 0 denotes an old password hashing schema. In the same column, the
|
|
|
|
// password hash was kept previously
|
2024-04-30 19:34:26 +02:00
|
|
|
$version = strlen($salt) <= 8 ? (int)$salt : 0;
|
2016-04-22 05:35:43 +02:00
|
|
|
|
|
|
|
if ($version == 0) {
|
|
|
|
$comp = hash('sha256', $salt . sha1($test));
|
2024-04-30 19:34:26 +02:00
|
|
|
} else {
|
2016-04-22 05:35:43 +02:00
|
|
|
$comp = crypt($test, $password);
|
2012-04-12 18:11:41 +02:00
|
|
|
}
|
2024-04-30 19:34:26 +02:00
|
|
|
return [ $version, hash_equals($password, $comp) ];
|
2016-04-22 05:35:43 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function generate_salt(): string {
|
2017-10-24 22:16:25 +02:00
|
|
|
return strtr(base64_encode(random_bytes(16)), '+', '.');
|
2016-04-22 05:35:43 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function calc_cookie_name(bool $is_https, bool $is_path_jailed, string $base_name): string {
|
2024-04-29 16:46:03 +02:00
|
|
|
if ($is_https) {
|
|
|
|
if ($is_path_jailed) {
|
|
|
|
return "__Host-$base_name";
|
|
|
|
} else {
|
|
|
|
return "__Secure-$base_name";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $base_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function login(string $username, string $password): array|false {
|
2016-04-22 05:35:43 +02:00
|
|
|
global $mod, $config;
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2014-02-13 02:04:32 +01:00
|
|
|
$query = prepare("SELECT `id`, `type`, `boards`, `password`, `version` FROM ``mods`` WHERE BINARY `username` = :username");
|
2012-04-12 18:11:41 +02:00
|
|
|
$query->bindValue(':username', $username);
|
|
|
|
$query->execute() or error(db_error($query));
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2013-07-24 17:15:55 +02:00
|
|
|
if ($user = $query->fetch(PDO::FETCH_ASSOC)) {
|
2016-05-05 06:43:22 +02:00
|
|
|
list($version, $ok) = test_password($user['password'], $user['version'], $password);
|
2016-04-22 05:35:43 +02:00
|
|
|
|
|
|
|
if ($ok) {
|
|
|
|
if ($config['password_crypt_version'] > $version) {
|
|
|
|
// It's time to upgrade the password hashing method!
|
2016-05-05 06:43:22 +02:00
|
|
|
list ($user['version'], $user['password']) = crypt_password($password);
|
|
|
|
$query = prepare("UPDATE ``mods`` SET `password` = :password, `version` = :version WHERE `id` = :id");
|
2016-04-22 05:35:43 +02:00
|
|
|
$query->bindValue(':password', $user['password']);
|
2016-05-05 06:43:22 +02:00
|
|
|
$query->bindValue(':version', $user['version']);
|
2016-04-22 05:35:43 +02:00
|
|
|
$query->bindValue(':id', $user['id']);
|
|
|
|
$query->execute() or error(db_error($query));
|
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
return $mod = [
|
2013-07-24 17:15:55 +02:00
|
|
|
'id' => $user['id'],
|
|
|
|
'type' => $user['type'],
|
|
|
|
'username' => $username,
|
|
|
|
'hash' => mkhash($username, $user['password']),
|
|
|
|
'boards' => explode(',', $user['boards'])
|
2024-04-30 19:34:26 +02:00
|
|
|
];
|
2013-07-24 17:15:55 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2013-07-24 17:15:55 +02:00
|
|
|
return false;
|
2012-04-12 18:11:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function setCookies(): void {
|
2012-04-12 18:11:41 +02:00
|
|
|
global $mod, $config;
|
2024-04-29 16:46:03 +02:00
|
|
|
if (!$mod) {
|
2012-04-12 18:11:41 +02:00
|
|
|
error('setCookies() was called for a non-moderator!');
|
2024-04-29 16:46:03 +02:00
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2024-04-30 11:31:06 +02:00
|
|
|
$is_https = Net\is_connection_secure();
|
2024-04-29 16:46:03 +02:00
|
|
|
$is_path_jailed = $config['cookies']['jail'];
|
|
|
|
$name = calc_cookie_name($is_https, $is_path_jailed, $config['cookies']['mod']);
|
|
|
|
|
|
|
|
// <username>:<password>:<salt>
|
|
|
|
$value = "{$mod['username']}:{$mod['hash'][0]}:{$mod['hash'][1]}";
|
|
|
|
|
|
|
|
$options = [
|
|
|
|
'expires' => time() + $config['cookies']['expire'],
|
|
|
|
'path' => $is_path_jailed ? $config['cookies']['path'] : '/',
|
|
|
|
'secure' => $is_https,
|
|
|
|
'httponly' => $config['cookies']['httponly'],
|
|
|
|
'samesite' => 'Strict'
|
|
|
|
];
|
|
|
|
|
|
|
|
setcookie($name, $value, $options);
|
2012-04-12 18:11:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function destroyCookies(): void {
|
2012-04-12 18:11:41 +02:00
|
|
|
global $config;
|
2024-04-29 16:46:03 +02:00
|
|
|
$base_name = $config['cookies']['mod'];
|
|
|
|
$del_time = time() - 60 * 60 * 24 * 365; // 1 year.
|
|
|
|
$jailed_path = $config['cookies']['jail'] ? $config['cookies']['path'] : '/';
|
|
|
|
$http_only = $config['cookies']['httponly'];
|
|
|
|
|
|
|
|
$options_multi = [
|
|
|
|
$base_name => [
|
|
|
|
'expires' => $del_time,
|
|
|
|
'path' => $jailed_path ,
|
|
|
|
'secure' => false,
|
|
|
|
'httponly' => $http_only,
|
|
|
|
'samesite' => 'Strict'
|
|
|
|
],
|
|
|
|
"__Host-$base_name" => [
|
|
|
|
'expires' => $del_time,
|
|
|
|
'path' => $jailed_path,
|
|
|
|
'secure' => true,
|
|
|
|
'httponly' => $http_only,
|
|
|
|
'samesite' => 'Strict'
|
|
|
|
],
|
|
|
|
"__Secure-$base_name" => [
|
|
|
|
'expires' => $del_time,
|
|
|
|
'path' => '/',
|
|
|
|
'secure' => true,
|
|
|
|
'httponly' => $http_only,
|
|
|
|
'samesite' => 'Strict'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($options_multi as $name => $options) {
|
2024-04-30 15:58:42 +02:00
|
|
|
if (isset($_COOKIE[$name])) {
|
|
|
|
setcookie($name, 'deleted', $options);
|
|
|
|
unset($_COOKIE[$name]);
|
|
|
|
}
|
2024-04-29 16:46:03 +02:00
|
|
|
}
|
2012-04-12 18:11:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function modLog(string $action, ?string $_board = null): void {
|
2012-04-12 18:11:41 +02:00
|
|
|
global $mod, $board, $config;
|
2013-08-01 04:14:26 +02:00
|
|
|
$query = prepare("INSERT INTO ``modlogs`` VALUES (:id, :ip, :board, :time, :text)");
|
2015-02-26 02:21:49 +01:00
|
|
|
$query->bindValue(':id', (isset($mod['id']) ? $mod['id'] : -1), PDO::PARAM_INT);
|
2012-04-12 18:11:41 +02: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));
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
if ($config['syslog']) {
|
2012-04-12 18:11:41 +02:00
|
|
|
_syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
|
2024-04-30 19:34:26 +02:00
|
|
|
}
|
2012-04-12 18:11:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function create_pm_header(): mixed {
|
2012-05-20 11:06:27 +02:00
|
|
|
global $mod, $config;
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2013-03-27 15:09:39 +01:00
|
|
|
if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) != false) {
|
2024-04-30 19:34:26 +02:00
|
|
|
if ($header === true) {
|
2012-05-20 11:06:27 +02:00
|
|
|
return false;
|
2024-04-30 19:34:26 +02:00
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2012-05-20 11:06:27 +02:00
|
|
|
return $header;
|
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2013-08-01 04:14:26 +02:00
|
|
|
$query = prepare("SELECT `id` FROM ``pms`` WHERE `to` = :id AND `unread` = 1");
|
2012-04-13 14:00:40 +02:00
|
|
|
$query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
|
|
|
|
$query->execute() or error(db_error($query));
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
if ($pm = $query->fetch(PDO::FETCH_ASSOC)) {
|
|
|
|
$header = [ 'id' => $pm['id'], 'waiting' => $query->rowCount() - 1 ];
|
|
|
|
} else {
|
2012-05-20 11:06:27 +02:00
|
|
|
$header = true;
|
2024-04-30 19:34:26 +02:00
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
if ($config['cache']['enabled']) {
|
2012-05-20 11:06:27 +02:00
|
|
|
cache::set('pm_unread_' . $mod['id'], $header);
|
2024-04-30 19:34:26 +02:00
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
if ($header === true) {
|
2012-05-20 11:06:27 +02:00
|
|
|
return false;
|
2024-04-30 19:34:26 +02:00
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2012-05-20 11:06:27 +02:00
|
|
|
return $header;
|
2012-04-13 14:00:40 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function make_secure_link_token(string $uri): string {
|
2012-08-27 07:19:05 +02:00
|
|
|
global $mod, $config;
|
|
|
|
return substr(sha1($config['cookies']['salt'] . '-' . $uri . '-' . $mod['id']), 0, 8);
|
|
|
|
}
|
|
|
|
|
2024-04-30 19:34:26 +02:00
|
|
|
function check_login(bool $prompt = false): void {
|
2015-01-01 07:35:21 +01:00
|
|
|
global $config, $mod;
|
2024-04-30 11:31:06 +02:00
|
|
|
|
|
|
|
$is_https = Net\is_connection_secure();
|
2024-04-29 16:46:03 +02:00
|
|
|
$is_path_jailed = $config['cookies']['jail'];
|
|
|
|
$expected_cookie_name = calc_cookie_name($is_https, $is_path_jailed, $config['cookies']['mod']);
|
|
|
|
|
2015-01-01 07:35:21 +01:00
|
|
|
// Validate session
|
2024-05-11 00:29:46 +02:00
|
|
|
if (isset($_COOKIE[$expected_cookie_name])) {
|
2015-01-01 07:35:21 +01:00
|
|
|
// Should be username:hash:salt
|
2024-04-29 16:46:03 +02:00
|
|
|
$cookie = explode(':', $_COOKIE[$expected_cookie_name]);
|
2015-01-01 07:35:21 +01:00
|
|
|
if (count($cookie) != 3) {
|
|
|
|
// Malformed cookies
|
|
|
|
destroyCookies();
|
|
|
|
if ($prompt) mod_login();
|
|
|
|
exit;
|
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2015-01-01 07:35:21 +01:00
|
|
|
$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);
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2015-01-01 07:35:21 +01:00
|
|
|
// validate password hash
|
|
|
|
if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) {
|
|
|
|
// Malformed cookies
|
|
|
|
destroyCookies();
|
|
|
|
if ($prompt) mod_login();
|
|
|
|
exit;
|
|
|
|
}
|
2024-04-30 11:34:53 +02:00
|
|
|
|
2015-01-01 07:35:21 +01:00
|
|
|
$mod = array(
|
2018-07-27 14:08:03 +02:00
|
|
|
'id' => (int)$user['id'],
|
|
|
|
'type' => (int)$user['type'],
|
2015-01-01 07:35:21 +01:00
|
|
|
'username' => $cookie[0],
|
|
|
|
'boards' => explode(',', $user['boards'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|