diff --git a/plugins/AuthCrypt/AuthCryptPlugin.php b/plugins/AuthCrypt/AuthCryptPlugin.php new file mode 100644 index 0000000000..7a077eb290 --- /dev/null +++ b/plugins/AuthCrypt/AuthCryptPlugin.php @@ -0,0 +1,149 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Mikael Nordfeldth + * @copyright 2012 StatusNet, Inc. + * @copyright 2012 Free Software Foundation, Inc http://www.fsf.org + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Plugin to use crypt() for user password hashes + * + * @category Plugin + * @package StatusNet + * @author Mikael Nordfeldth + * @copyright 2012 StatusNet, Inc. + * @copyright 2012 Free Software Foundation, Inc http://www.fsf.org + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ +class AuthCryptPlugin extends AuthenticationPlugin +{ + public $hash; // defaults to SHA512, i.e. '$6$', in onInitializePlugin() + public $hostile; // if true, password login means change password to crypt() hash + public $overwrite; // if true, password change means always store crypt() hash + + /* + * FUNCTIONALITY + */ + + function checkPassword($username, $password) { + $user = User::staticGet('nickname', common_canonical_nickname($username)); + + // crypt cuts the second parameter to its appropriate length based on hash scheme + if (!empty($user) && $user->password === crypt($password, $user->password)) { + return $user; + } + // if we're hostile, check the password against old-style hashing + if (!empty($user) && $this->hostile && $user->password === md5($password . $user->id)) { + // update password hash entry to crypt() compatible + $this->changePassword($username, $password, $password); + return $user; + } + + return false; + } + + // $oldpassword is already verified when calling this function... shouldn't this be private?! + function changePassword($username, $oldpassword, $newpassword) { + if (!$this->password_changeable) { + return false; + } + + $user = User::staticGet('nickname', $username); + if (empty($user)) { + return false; + } + $original = clone($user); + + // a new, unique salt per new record stored... but common_good_rand should be more diverse than hexdec + $user->password = crypt($newpassword, $this->hash . common_good_rand(CRYPT_SALT_LENGTH)); + + return (true === $user->validate() && $user->update($original)); + } + + /* + * EVENTS + */ + + function onInitializePlugin() { + $this->provider_name = 'crypt'; // we don't actually use the provider_name + + if (!isset($this->hash)) { + $this->hash = '$6$'; // SHA512 supported on all systems since PHP 5.3.2 + } + if (!isset($this->hostile)) { + $this->hostile = false; // overwrite old style password hashes? + } + if (!isset($this->overwrite)) { + $this->overwrite = true; // overwrite old style password hashes? + } + } + + function onStartChangePassword($user, $oldpassword, $newpassword) { + if (!$this->checkPassword($user->nickname, $oldpassword)) { + // if we ARE in overwrite mode, test password with common_check_user + if (!$this->overwrite || !common_check_user($user->nickname, $oldpassword)) { + // either we're not in overwrite mode, or the password was incorrect + return !$this->authoritative; + } + // oldpassword was apparently ok + } + $changed = $this->changePassword($user->nickname, $oldpassword, $newpassword); + + return (!$changed && empty($this->authoritative)); + } + + function onStartCheckPassword($nickname, $password, &$authenticatedUser) { + $authenticatedUser = $this->checkPassword($nickname, $password); + return (empty($authenticatedUser) && empty($this->authoritative)); + } + + function onCheckSchema() { + // we only use the User database, so default AuthenticationPlugin stuff can be ignored + return true; + } + + function onUserDeleteRelated($user, &$tables) { + // not using User_username table, so no need to add it here. + return true; + } + + function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'AuthCrypt', + 'version' => STATUSNET_VERSION, + 'author' => 'Mikael Nordfeldth', + 'homepage' => 'http://status.net/wiki/Plugin:AuthCrypt', + 'rawdescription' => + // TRANS: Plugin description. + _m('Authentication and password hashing with crypt()')); + return true; + } +} diff --git a/plugins/AuthCrypt/README b/plugins/AuthCrypt/README new file mode 100644 index 0000000000..cc0541c35f --- /dev/null +++ b/plugins/AuthCrypt/README @@ -0,0 +1,48 @@ +AuthCrypt allows for StatusNet to use crypt() hashing to store password credentials. + +Requirements +============ +* PHP >= 5.3.2 with php5-mcrypt extension for certain SHA512 support +* lib/authenticationplugin.php in your StatusNet install + +Installation +============ +Add either of the following configurations to your config.php (see Example below for more options): + + addPlugin('AuthCrypt'); + +The recommended use is to overwrite old hash values when logging in (hostile mode) and be the authority on password checks when logging in. If you only wish to update entries on password change the default values are enough. + + addPlugin('AuthCrypt', array( + 'authoritative'=>true, + 'hostile'=>true, + )); + +To disable updating to crypt() on password change, simply set the 'overwrite' setting to false: + + addPlugin('AuthCrypt', array( + 'overwrite'=>false, + )); + +Settings +======== +Default values in parenthesis. Many settings are inherited from the AuthenticationPlugin class. + +authoritative (false): Set to true when all passwords are hashed with crypt() + (warning: this may disable all other password verification, also when changing passwords!) +hash ('$6$'): Hash signature to use, defaults to SHA512. See all supported strings at http://php.net/crypt + (warning: set this to something crypt() understands, or you will default to the very weak 2-char DES scheme) +hostile (false): Do we update the password hash entries on login? + (notice: will check password login against old-style hash and then update using crypt()) +overwrite (true): Do we overwrite old style password hashes with crypt() hashes on password change? + (notice: to make use of stronger security or migrate to crypt() hashes, this must be true) +password_changeable (true): Enables or disables password changing. + (notice: if combined with authoritative, it disables changing passwords and removes option from menu.) +autoregistration: This setting is ignored. Password can never be valid without existing User. +provider_name: This setting defaults to 'crypt' but is never stored anywhere. + +Todo +==== +This does not in any way get in touch with common_munge_password, which is +called upon User::register(...) and RecoverpasswordAction->resetPassword(). +Maybe there should be an event like StartMungePassword in the core?