AuthCryptPlugin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializePlugin()
  34. protected $statusnet = true; // if true, also check StatusNet style password hash
  35. protected $overwrite = true; // if true, password change means overwrite with crypt()
  36. public $provider_name = 'crypt'; // not actually used
  37. /*
  38. * FUNCTIONALITY
  39. */
  40. function checkPassword($username, $password)
  41. {
  42. $username = Nickname::normalize($username);
  43. $user = User::getKV('nickname', $username);
  44. if (!($user instanceof User)) {
  45. return false;
  46. }
  47. // crypt understands what the salt part of $user->password is
  48. if ($user->password === crypt($password, $user->password)) {
  49. return $user;
  50. }
  51. // If we check StatusNet hash, for backwards compatibility and migration
  52. if ($this->statusnet && $user->password === md5($password . $user->id)) {
  53. // and update password hash entry to crypt() compatible
  54. if ($this->overwrite) {
  55. $this->changePassword($user->nickname, null, $password);
  56. }
  57. return $user;
  58. }
  59. return false;
  60. }
  61. protected function cryptSalt($len=CRYPT_SALT_LENGTH)
  62. {
  63. $chars = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  64. $salt = '';
  65. for ($i=0; $i<$len; $i++) {
  66. $salt .= $chars{mt_rand(0, strlen($chars)-1)};
  67. }
  68. return $salt;
  69. }
  70. // $oldpassword is already verified when calling this function... shouldn't this be private?!
  71. function changePassword($username, $oldpassword, $newpassword)
  72. {
  73. $username = Nickname::normalize($username);
  74. if (!$this->password_changeable) {
  75. return false;
  76. }
  77. $user = User::getKV('nickname', $username);
  78. if (empty($user)) {
  79. return false;
  80. }
  81. $original = clone($user);
  82. $user->password = $this->hashPassword($newpassword, $user->getProfile());
  83. return (true === $user->validate() && $user->update($original));
  84. }
  85. public function hashPassword($password, Profile $profile=null)
  86. {
  87. // A new, unique salt per new record stored...
  88. return crypt($password, $this->hash . self::cryptSalt());
  89. }
  90. /*
  91. * EVENTS
  92. */
  93. public function onStartChangePassword(Profile $target, $oldpassword, $newpassword)
  94. {
  95. if (!$this->checkPassword($target->getNickname(), $oldpassword)) {
  96. // if we ARE in overwrite mode, test password with common_check_user
  97. if (!$this->overwrite || !common_check_user($target->getNickname(), $oldpassword)) {
  98. // either we're not in overwrite mode, or the password was incorrect
  99. return !$this->authoritative;
  100. }
  101. // oldpassword was apparently ok
  102. }
  103. $changed = $this->changePassword($target->getNickname(), $oldpassword, $newpassword);
  104. return (!$changed && empty($this->authoritative));
  105. }
  106. public function onStartCheckPassword($nickname, $password, &$authenticatedUser)
  107. {
  108. $authenticatedUser = $this->checkPassword($nickname, $password);
  109. // if we failed, only return false to stop plugin execution if we're authoritative
  110. return (!($authenticatedUser instanceof User) && empty($this->authoritative));
  111. }
  112. public function onStartHashPassword(&$hashed, $password, Profile $profile=null)
  113. {
  114. $hashed = $this->hashPassword($password, $profile);
  115. return false;
  116. }
  117. public function onCheckSchema()
  118. {
  119. // we only use the User database, so default AuthenticationPlugin stuff can be ignored
  120. return true;
  121. }
  122. public function onUserDeleteRelated($user, &$tables)
  123. {
  124. // not using User_username table, so no need to add it here.
  125. return true;
  126. }
  127. public function onPluginVersion(array &$versions)
  128. {
  129. $versions[] = array('name' => 'AuthCrypt',
  130. 'version' => GNUSOCIAL_VERSION,
  131. 'author' => 'Mikael Nordfeldth',
  132. 'homepage' => 'http://status.net/wiki/Plugin:AuthCrypt',
  133. 'rawdescription' =>
  134. // TRANS: Plugin description.
  135. _m('Authentication and password hashing with crypt()'));
  136. return true;
  137. }
  138. }