The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.4KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2013 Tinyboard Development Group
  4. */
  5. defined('TINYBOARD') or exit;
  6. // create a hash/salt pair for validate logins
  7. function mkhash($username, $password, $salt = false) {
  8. global $config;
  9. if (!$salt) {
  10. // create some sort of salt for the hash
  11. $salt = substr(base64_encode(sha1(rand() . time(), true) . $config['cookies']['salt']), 0, 15);
  12. $generated_salt = true;
  13. }
  14. // generate hash (method is not important as long as it's strong)
  15. $hash = substr(
  16. base64_encode(
  17. md5(
  18. $username . $config['cookies']['salt'] . sha1(
  19. $username . $password . $salt . (
  20. $config['mod']['lock_ip'] ? $_SERVER['REMOTE_ADDR'] : ''
  21. ), true
  22. ) . sha1($config['password_crypt_version']) // Log out users being logged in with older password encryption schema
  23. , true
  24. )
  25. ), 0, 20
  26. );
  27. if (isset($generated_salt))
  28. return array($hash, $salt);
  29. else
  30. return $hash;
  31. }
  32. function crypt_password_old($password) {
  33. $salt = generate_salt();
  34. $password = hash('sha256', $salt . sha1($password));
  35. return array($salt, $password);
  36. }
  37. function crypt_password($password) {
  38. global $config;
  39. // `salt` database field is reused as a version value. We don't want it to be 0.
  40. $version = $config['password_crypt_version'] ? $config['password_crypt_version'] : 1;
  41. $new_salt = generate_salt();
  42. $password = crypt($password, $config['password_crypt'] . $new_salt . "$");
  43. return array($version, $password);
  44. }
  45. function test_password($password, $salt, $test) {
  46. global $config;
  47. // Version = 0 denotes an old password hashing schema. In the same column, the
  48. // password hash was kept previously
  49. $version = (strlen($salt) <= 8) ? (int) $salt : 0;
  50. if ($version == 0) {
  51. $comp = hash('sha256', $salt . sha1($test));
  52. }
  53. else {
  54. $comp = crypt($test, $password);
  55. }
  56. return array($version, hash_equals($password, $comp));
  57. }
  58. function generate_salt() {
  59. // 128 bits of entropy
  60. if (function_exists('random_bytes')) {
  61. return strtr(base64_encode(random_bytes(16)), '+', '.');
  62. } else {
  63. return strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
  64. }
  65. }
  66. function login($username, $password) {
  67. global $mod, $config;
  68. $query = prepare("SELECT `id`, `type`, `boards`, `password`, `version` FROM ``mods`` WHERE BINARY `username` = :username");
  69. $query->bindValue(':username', $username);
  70. $query->execute() or error(db_error($query));
  71. if ($user = $query->fetch(PDO::FETCH_ASSOC)) {
  72. list($version, $ok) = test_password($user['password'], $user['version'], $password);
  73. if ($ok) {
  74. if ($config['password_crypt_version'] > $version) {
  75. // It's time to upgrade the password hashing method!
  76. list ($user['version'], $user['password']) = crypt_password($password);
  77. $query = prepare("UPDATE ``mods`` SET `password` = :password, `version` = :version WHERE `id` = :id");
  78. $query->bindValue(':password', $user['password']);
  79. $query->bindValue(':version', $user['version']);
  80. $query->bindValue(':id', $user['id']);
  81. $query->execute() or error(db_error($query));
  82. }
  83. return $mod = array(
  84. 'id' => $user['id'],
  85. 'type' => $user['type'],
  86. 'username' => $username,
  87. 'hash' => mkhash($username, $user['password']),
  88. 'boards' => explode(',', $user['boards'])
  89. );
  90. }
  91. }
  92. return false;
  93. }
  94. function setCookies() {
  95. global $mod, $config;
  96. if (!$mod)
  97. error('setCookies() was called for a non-moderator!');
  98. setcookie($config['cookies']['mod'],
  99. $mod['username'] . // username
  100. ':' .
  101. $mod['hash'][0] . // password
  102. ':' .
  103. $mod['hash'][1], // salt
  104. time() + $config['cookies']['expire'], $config['cookies']['jail'] ? $config['cookies']['path'] : '/', null, !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off', $config['cookies']['httponly']);
  105. }
  106. function destroyCookies() {
  107. global $config;
  108. // Delete the cookies
  109. setcookie($config['cookies']['mod'], 'deleted', time() - $config['cookies']['expire'], $config['cookies']['jail']?$config['cookies']['path'] : '/', null, !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off', true);
  110. }
  111. function modLog($action, $_board=null) {
  112. global $mod, $board, $config;
  113. $query = prepare("INSERT INTO ``modlogs`` VALUES (:id, :ip, :board, :time, :text)");
  114. $query->bindValue(':id', (isset($mod['id']) ? $mod['id'] : -1), PDO::PARAM_INT);
  115. $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
  116. $query->bindValue(':time', time(), PDO::PARAM_INT);
  117. $query->bindValue(':text', $action);
  118. if (isset($_board))
  119. $query->bindValue(':board', $_board);
  120. elseif (isset($board))
  121. $query->bindValue(':board', $board['uri']);
  122. else
  123. $query->bindValue(':board', null, PDO::PARAM_NULL);
  124. $query->execute() or error(db_error($query));
  125. if ($config['syslog'])
  126. _syslog(LOG_INFO, '[mod/' . $mod['username'] . ']: ' . $action);
  127. }
  128. function create_pm_header() {
  129. global $mod, $config;
  130. if ($config['cache']['enabled'] && ($header = cache::get('pm_unread_' . $mod['id'])) != false) {
  131. if ($header === true)
  132. return false;
  133. return $header;
  134. }
  135. $query = prepare("SELECT `id` FROM ``pms`` WHERE `to` = :id AND `unread` = 1");
  136. $query->bindValue(':id', $mod['id'], PDO::PARAM_INT);
  137. $query->execute() or error(db_error($query));
  138. if ($pm = $query->fetch(PDO::FETCH_ASSOC))
  139. $header = array('id' => $pm['id'], 'waiting' => $query->rowCount() - 1);
  140. else
  141. $header = true;
  142. if ($config['cache']['enabled'])
  143. cache::set('pm_unread_' . $mod['id'], $header);
  144. if ($header === true)
  145. return false;
  146. return $header;
  147. }
  148. function make_secure_link_token($uri) {
  149. global $mod, $config;
  150. return substr(sha1($config['cookies']['salt'] . '-' . $uri . '-' . $mod['id']), 0, 8);
  151. }
  152. function check_login($prompt = false) {
  153. global $config, $mod;
  154. // Validate session
  155. if (isset($_COOKIE[$config['cookies']['mod']])) {
  156. // Should be username:hash:salt
  157. $cookie = explode(':', $_COOKIE[$config['cookies']['mod']]);
  158. if (count($cookie) != 3) {
  159. // Malformed cookies
  160. destroyCookies();
  161. if ($prompt) mod_login();
  162. exit;
  163. }
  164. $query = prepare("SELECT `id`, `type`, `boards`, `password` FROM ``mods`` WHERE `username` = :username");
  165. $query->bindValue(':username', $cookie[0]);
  166. $query->execute() or error(db_error($query));
  167. $user = $query->fetch(PDO::FETCH_ASSOC);
  168. // validate password hash
  169. if ($cookie[1] !== mkhash($cookie[0], $user['password'], $cookie[2])) {
  170. // Malformed cookies
  171. destroyCookies();
  172. if ($prompt) mod_login();
  173. exit;
  174. }
  175. $mod = array(
  176. 'id' => $user['id'],
  177. 'type' => $user['type'],
  178. 'username' => $cookie[0],
  179. 'boards' => explode(',', $user['boards'])
  180. );
  181. }
  182. }