AuthCryptPlugin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to use crypt() for user password hashes
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package GNUsocial
  24. * @author Mikael Nordfeldth <mmn@hethane.se>
  25. * @copyright 2012 StatusNet, Inc.
  26. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://www.gnu.org/software/social/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. class AuthCryptPlugin extends AuthenticationPlugin
  32. {
  33. const PLUGIN_VERSION = '2.0.0';
  34. protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializePlugin()
  35. protected $statusnet = true; // if true, also check StatusNet style password hash
  36. protected $overwrite = true; // if true, password change means overwrite with crypt()
  37. public $provider_name = 'crypt'; // not actually used
  38. /*
  39. * FUNCTIONALITY
  40. */
  41. function checkPassword($username, $password)
  42. {
  43. $username = Nickname::normalize($username);
  44. $user = User::getKV('nickname', $username);
  45. if (!($user instanceof User)) {
  46. return false;
  47. }
  48. // crypt understands what the salt part of $user->password is
  49. if ($user->password === crypt($password, $user->password)) {
  50. return $user;
  51. }
  52. // If we check StatusNet hash, for backwards compatibility and migration
  53. if ($this->statusnet && $user->password === md5($password . $user->id)) {
  54. // and update password hash entry to crypt() compatible
  55. if ($this->overwrite) {
  56. $this->changePassword($user->nickname, null, $password);
  57. }
  58. return $user;
  59. }
  60. return false;
  61. }
  62. protected function cryptSalt($len=CRYPT_SALT_LENGTH)
  63. {
  64. $chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  65. $salt = '';
  66. for ($i=0; $i<$len; $i++) {
  67. $salt .= $chars{mt_rand(0, strlen($chars)-1)};
  68. }
  69. return $salt;
  70. }
  71. // $oldpassword is already verified when calling this function... shouldn't this be private?!
  72. function changePassword($username, $oldpassword, $newpassword)
  73. {
  74. $username = Nickname::normalize($username);
  75. if (!$this->password_changeable) {
  76. return false;
  77. }
  78. $user = User::getKV('nickname', $username);
  79. if (empty($user)) {
  80. return false;
  81. }
  82. $original = clone($user);
  83. $user->password = $this->hashPassword($newpassword, $user->getProfile());
  84. return (true === $user->validate() && $user->update($original));
  85. }
  86. public function hashPassword($password, Profile $profile=null)
  87. {
  88. // A new, unique salt per new record stored...
  89. return crypt($password, $this->hash . self::cryptSalt());
  90. }
  91. /*
  92. * EVENTS
  93. */
  94. public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
  95. {
  96. if (!$this->checkPassword($target->getNickname(), $oldpassword)) {
  97. // if we ARE in overwrite mode, test password with common_check_user
  98. if (!$this->overwrite || !common_check_user($target->getNickname(), $oldpassword)) {
  99. // either we're not in overwrite mode, or the password was incorrect
  100. return !$this->authoritative;
  101. }
  102. // oldpassword was apparently ok
  103. }
  104. $changed = $this->changePassword($target->getNickname(), $oldpassword, $newpassword);
  105. return (!$changed && empty($this->authoritative));
  106. }
  107. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  108. {
  109. $authenticatedUser = $this->checkPassword($nickname, $password);
  110. // if we failed, only return false to stop plugin execution if we're authoritative
  111. return (!($authenticatedUser instanceof User) && empty($this->authoritative));
  112. }
  113. public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
  114. {
  115. $hashed = $this->hashPassword($password, $profile);
  116. return false;
  117. }
  118. public function onCheckSchema()
  119. {
  120. // we only use the User database, so default AuthenticationPlugin stuff can be ignored
  121. return true;
  122. }
  123. public function onUserDeleteRelated($user, &$tables)
  124. {
  125. // not using User_username table, so no need to add it here.
  126. return true;
  127. }
  128. public function onPluginVersion(array &$versions)
  129. {
  130. $versions[] = array('name' => 'AuthCrypt',
  131. 'version' => self::PLUGIN_VERSION,
  132. 'author' => 'Mikael Nordfeldth',
  133. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/AuthCrypt',
  134. 'rawdescription' =>
  135. // TRANS: Plugin description.
  136. _m('Authentication and password hashing with crypt()'));
  137. return true;
  138. }
  139. }