leftypol/inc/mod/auth.php

223 lines
6.2 KiB
PHP
Raw Normal View History

2012-04-13 02:11:41 +10:00
<?php
/*
2013-01-20 21:23:46 +11:00
* Copyright (c) 2010-2013 Tinyboard Development Group
2012-04-13 02:11:41 +10:00
*/
2024-12-01 17:10:56 +01:00
use Vichan\Context;
use Vichan\Functions\{Hide, Net};
defined('TINYBOARD') or exit;
2012-04-13 02:11:41 +10:00
// create a hash/salt pair for validate logins
2025-04-23 22:29:23 +02:00
function mkhash(string $username, ?string $password, mixed $salt = false): array|string {
2012-04-13 02:11:41 +10:00
global $config;
2024-04-30 11:34:53 +02:00
2012-04-13 02:11:41 +10:00
if (!$salt) {
// Create some salt for the hash.
$salt = \bin2hex(\random_bytes(15)); // 20 characters.
2012-04-13 02:11:41 +10:00
$generated_salt = true;
} else {
$generated_salt = false;
2012-04-13 02:11:41 +10:00
}
2024-04-30 11:34:53 +02:00
2012-04-13 02:11:41 +10:00
// generate hash (method is not important as long as it's strong)
$hash = \substr(
Hide\secure_hash(
$username . $config['cookies']['salt'] . Hide\secure_hash(
$username . $password . $salt . (
$config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''
), true
) . Hide\secure_hash($config['password_crypt_version'], true), // Log out users being logged in with older password encryption schema
false
),
0,
40
);
2024-04-30 11:34:53 +02:00
if ($generated_salt) {
return [ $hash, $salt ];
} else {
2012-04-13 02:11:41 +10:00
return $hash;
}
2012-04-13 02:11:41 +10:00
}
2025-04-23 22:29:23 +02:00
function crypt_password(string $password): array {
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;
2025-04-16 19:18:50 +02:00
$pre_hash = \hash('tiger160,3', $password, false); // Note that it's truncated to 72 in the next line.
2025-04-16 21:18:30 +02:00
$r = \password_hash($pre_hash, \PASSWORD_BCRYPT, [ 'cost' => 12 ]);
2025-04-16 19:18:50 +02:00
if ($r === false) {
throw new \RuntimeException("Could not hash password");
}
return [ $version, $r ];
}
function test_password(string $db_hash, string|int $version, string $input_password): bool {
$version = (int)$version;
if ($version < 2) {
$ok = \hash_equals($db_hash, \crypt($input_password, $db_hash));
} else {
2025-04-16 19:18:50 +02:00
$pre_hash = \hash('tiger160,3', $input_password, false);
$ok = \password_verify($pre_hash, $db_hash);
2012-04-13 02:11:41 +10:00
}
return $ok;
}
2025-04-16 14:46:11 -03:00
function login(string $username, string $password): array|false {
2025-04-16 21:06:03 +02:00
global $mod;
2024-04-30 11:34:53 +02:00
$query = prepare("SELECT `id`, `type`, `boards`, `password`, `version` FROM ``mods`` WHERE BINARY `username` = :username");
2012-04-13 02:11:41 +10:00
$query->bindValue(':username', $username);
2025-04-16 21:18:30 +02:00
$query->execute();
2024-04-30 11:34:53 +02:00
2013-07-24 11:15:55 -04:00
if ($user = $query->fetch(PDO::FETCH_ASSOC)) {
$ok = test_password($user['password'], $user['version'], $password);
if ($ok) {
if ((int)$user['version'] < 2) {
// It's time to upgrade the password hashing method!
list ($user['version'], $user['password']) = crypt_password($password);
$query = prepare("UPDATE ``mods`` SET `password` = :password, `version` = :version WHERE `id` = :id");
$query->bindValue(':password', $user['password']);
$query->bindValue(':version', $user['version']);
$query->bindValue(':id', $user['id']);
2025-04-16 21:18:30 +02:00
$query->execute();
}
2025-04-23 22:29:23 +02:00
return $mod = [
2013-07-24 11:15:55 -04:00
'id' => $user['id'],
'type' => $user['type'],
'username' => $username,
'hash' => mkhash($username, $user['password']),
'boards' => explode(',', $user['boards'])
2025-04-23 22:29:23 +02:00
];
2013-07-24 11:15:55 -04:00
}
}
2024-04-30 11:34:53 +02:00
2013-07-24 11:15:55 -04:00
return false;
2012-04-13 02:11:41 +10:00
}
2025-04-23 22:29:23 +02:00
function setCookies(): void {
2012-04-13 02:11:41 +10:00
global $mod, $config;
2025-04-23 22:29:23 +02:00
if (!$mod) {
2012-04-13 02:11:41 +10:00
error('setCookies() was called for a non-moderator!');
2025-04-23 22:29:23 +02:00
}
2024-04-30 11:34:53 +02:00
$is_https = Net\is_connection_https();
2012-04-13 02:11:41 +10:00
setcookie($config['cookies']['mod'],
$mod['username'] . // username
2024-04-30 11:34:53 +02:00
':' .
2012-04-13 02:11:41 +10:00
$mod['hash'][0] . // password
':' .
$mod['hash'][1], // salt
time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, $is_https, $config['cookies']['httponly']);
2012-04-13 02:11:41 +10:00
}
2025-04-23 22:29:23 +02:00
function destroyCookies(): void {
2012-04-13 02:11:41 +10:00
global $config;
$is_https = Net\is_connection_https();
2012-04-13 02:11:41 +10:00
// Delete the cookies
setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, $is_https, true);
2012-04-13 02:11:41 +10:00
}
2025-04-23 22:29:23 +02:00
function modLog(string $action, ?string $_board = null): void {
2012-04-13 02:11:41 +10:00
global $mod, $board, $config;
$query = prepare("INSERT INTO ``modlogs`` VALUES (:id, :ip, :board, :time, :text)");
$query->bindValue(':id', (isset($mod['id']) ? $mod['id'] : -1), PDO::PARAM_INT);
2012-04-13 02:11:41 +10: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
2025-04-23 22:29:23 +02:00
if ($config['syslog']) {
2012-04-13 02:11:41 +10:00
_syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
2025-04-23 22:29:23 +02:00
}
2012-04-13 02:11:41 +10:00
}
2025-04-23 22:29:23 +02:00
function create_pm_header(): mixed {
2012-05-20 19:06:27 +10:00
global $mod, $config;
2024-04-30 11:34:53 +02:00
if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) != false) {
2025-04-23 22:29:23 +02:00
if ($header === true) {
2012-05-20 19:06:27 +10:00
return false;
2025-04-23 22:29:23 +02:00
}
2024-04-30 11:34:53 +02:00
2012-05-20 19:06:27 +10:00
return $header;
}
2024-04-30 11:34:53 +02:00
$query = prepare("SELECT `id` FROM ``pms`` WHERE `to` = :id AND `unread` = 1");
2012-04-13 22:00:40 +10:00
$query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
$query->execute() or error(db_error($query));
2024-04-30 11:34:53 +02:00
2025-04-23 22:29:23 +02:00
if ($pm = $query->fetch(PDO::FETCH_ASSOC)) {
$header = [ 'id' => $pm['id'], 'waiting' => $query->rowCount() - 1 ];
} else {
2012-05-20 19:06:27 +10:00
$header = true;
2025-04-23 22:29:23 +02:00
}
2024-04-30 11:34:53 +02:00
2025-04-23 22:29:23 +02:00
if ($config['cache']['enabled']) {
2012-05-20 19:06:27 +10:00
cache::set('pm_unread_' . $mod['id'], $header);
2025-04-23 22:29:23 +02:00
}
2024-04-30 11:34:53 +02:00
2025-04-23 22:29:23 +02:00
if ($header === true) {
2012-05-20 19:06:27 +10:00
return false;
2025-04-23 22:29:23 +02:00
}
2024-04-30 11:34:53 +02:00
2012-05-20 19:06:27 +10:00
return $header;
2012-04-13 22:00:40 +10:00
}
2025-04-23 22:29:23 +02:00
function make_secure_link_token(string $uri): string {
2012-08-27 15:19:05 +10:00
global $mod, $config;
return substr(sha1($config['cookies']['salt'] . '-' . $uri . '-' . $mod['id']), 0, 8);
}
2025-04-23 22:29:23 +02:00
function check_login(Context $ctx, bool $prompt = false): void {
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();
2025-04-23 22:29:23 +02:00
if ($prompt) {
mod_login($ctx);
}
exit;
}
2024-04-30 11:34:53 +02: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
// validate password hash
if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) {
// Malformed cookies
destroyCookies();
2025-04-23 22:29:23 +02:00
if ($prompt) {
mod_login($ctx);
}
exit;
}
2024-04-30 11:34:53 +02:00
$mod = array(
'id' => (int)$user['id'],
'type' => (int)$user['type'],
'username' => $cookie[0],
'boards' => explode(',', $user['boards'])
);
}
2025-04-23 22:29:23 +02:00
}