From e2c50d202fba2e953dc3798091bdb2858ab8a8ab Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 17 Oct 2013 16:32:53 +0200 Subject: [PATCH] AuthCrypt now tidied up and enabled by default. --- EVENTS.txt | 5 ++ INSTALL | 2 +- lib/passwordhashexception.php | 44 +++++++++++ lib/siteprofile.php | 1 + lib/util.php | 16 ++-- plugins/AuthCrypt/AuthCryptPlugin.php | 104 +++++++++++++------------- plugins/AuthCrypt/README | 23 ++---- 7 files changed, 119 insertions(+), 76 deletions(-) create mode 100644 lib/passwordhashexception.php diff --git a/EVENTS.txt b/EVENTS.txt index a113bb56a1..7e08430218 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -603,6 +603,11 @@ StartChangePassword: Before changing a password EndChangePassword: After changing a password - $user: user +StartHashPassword: Generate a hashed version of the password (like a salted crypt) +- &$hashed: Hashed version of the password, later put in the database +- $password: The password that should be hashed +- $profile: Profile that this password and hash belongs to. Can be null. + StartSetUser: Before setting the currently logged in user - $user: user diff --git a/INSTALL b/INSTALL index ea700ebdb7..bac8d34005 100644 --- a/INSTALL +++ b/INSTALL @@ -4,7 +4,7 @@ Prerequisites The following software packages are *required* for this software to run correctly. -- PHP 5.3+ For newer versions, some functions that are used may be +- PHP 5.3.2+ For newer versions, some functions that are used may be disabled by default, such as the pcntl_* family. See the section on 'Queues and daemons' for more information. - MariaDB 5.x GNU Social uses, by default, a MariaDB server for data diff --git a/lib/passwordhashexception.php b/lib/passwordhashexception.php new file mode 100644 index 0000000000..d51505f261 --- /dev/null +++ b/lib/passwordhashexception.php @@ -0,0 +1,44 @@ +. + * + * @category Exception + * @package GNUsocial + * @author Mikael Nordfeldth + * @copyright 2013 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://www.gnu.org/software/social/ + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +class PasswordHashException extends ServerException +{ + public $obj; // The object with query that gave no results + + public function __construct($msg=null, $code=500) + { + if ($msg === null) { + $msg = _('Password hashing failed.'); + } + + parent::__construct($msg, $code); + } +} diff --git a/lib/siteprofile.php b/lib/siteprofile.php index dbb74a81b2..86ef13b550 100644 --- a/lib/siteprofile.php +++ b/lib/siteprofile.php @@ -80,6 +80,7 @@ abstract class SiteProfileSettings static function defaultPlugins() { return array( + 'AuthCrypt' => null, 'Bookmark' => null, 'Event' => null, 'OpenID' => null, diff --git a/lib/util.php b/lib/util.php index 81643f9a19..608938c4c0 100644 --- a/lib/util.php +++ b/lib/util.php @@ -210,14 +210,18 @@ function common_language() /** * Salted, hashed passwords are stored in the DB. */ -function common_munge_password($password, $id) +function common_munge_password($password, $id, Profile $profile=null) { - if (is_object($id) || is_object($password)) { - $e = new Exception(); - common_log(LOG_ERR, __METHOD__ . ' object in param to common_munge_password ' . - str_replace("\n", " ", $e->getTraceAsString())); + $hashed = null; + + if (Event::handle('StartHashPassword', array(&$hashed, $password, $profile))) { + Event::handle('EndHashPassword', array(&$hashed, $password, $profile)); } - return md5($password . $id); + if (empty($hashed)) { + throw new PasswordHashException(); + } + + return $hashed; } /** diff --git a/plugins/AuthCrypt/AuthCryptPlugin.php b/plugins/AuthCrypt/AuthCryptPlugin.php index 83885ab799..26366879e6 100644 --- a/plugins/AuthCrypt/AuthCryptPlugin.php +++ b/plugins/AuthCrypt/AuthCryptPlugin.php @@ -20,50 +20,46 @@ * along with this program. If not, see . * * @category Plugin - * @package StatusNet + * @package GNUsocial * @author Mikael Nordfeldth * @copyright 2012 StatusNet, Inc. - * @copyright 2012 Free Software Foundation, Inc http://www.fsf.org + * @copyright 2013 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/ + * @link http://www.gnu.org/software/social/ */ -if (!defined('STATUSNET')) { - exit(1); -} +if (!defined('GNUSOCIAL')) { 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 + protected $hash = '$6$'; // defaults to SHA512, i.e. '$6$', in onInitializePlugin() + protected $statusnet = true; // if true, also check StatusNet style password hash + protected $overwrite = true; // if true, password change means overwrite with crypt() + + public $provider_name = 'crypt'; // not actually used /* * FUNCTIONALITY */ - function checkPassword($username, $password) { - $user = User::getKV('nickname', common_canonical_nickname($username)); + function checkPassword($username, $password) + { + $user = User::getKV('nickname', $username); + if (!($user instanceof User)) { + return false; + } // crypt cuts the second parameter to its appropriate length based on hash scheme - if (!empty($user) && $user->password === crypt($password, $user->password)) { + if ($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); + + // If we check StatusNet hash, for backwards compatibility and migration + if ($this->statusnet && $user->password === md5($password . $user->id)) { + // and update password hash entry to crypt() compatible + if ($this->overwrite) { + $this->changePassword($user->nickname, null, $password); + } return $user; } @@ -71,7 +67,8 @@ class AuthCryptPlugin extends AuthenticationPlugin } // $oldpassword is already verified when calling this function... shouldn't this be private?! - function changePassword($username, $oldpassword, $newpassword) { + function changePassword($username, $oldpassword, $newpassword) + { if (!$this->password_changeable) { return false; } @@ -82,32 +79,25 @@ class AuthCryptPlugin extends AuthenticationPlugin } $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)); + $user->password = $this->hashPassword($newpassword, $user->getProfile()); return (true === $user->validate() && $user->update($original)); } + public function hashPassword($password, Profile $profile=null) + { + // A new, unique salt per new record stored... + // TODO: common_good_rand should be more diverse than hexdec + return crypt($password, $this->hash . common_good_rand(CRYPT_SALT_LENGTH)); + } + /* * 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)) { + public 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 @@ -117,25 +107,35 @@ class AuthCryptPlugin extends AuthenticationPlugin } $changed = $this->changePassword($user->nickname, $oldpassword, $newpassword); - return (!$changed && empty($this->authoritative)); + return (!$changed && empty($this->authoritative)); } - function onStartCheckPassword($nickname, $password, &$authenticatedUser) { + public function onStartCheckPassword($nickname, $password, &$authenticatedUser) + { $authenticatedUser = $this->checkPassword($nickname, $password); - return (empty($authenticatedUser) && empty($this->authoritative)); + // if we failed, only return false to stop plugin execution if we're authoritative + return (!($authenticatedUser instanceof User) && empty($this->authoritative)); } - function onCheckSchema() { + public function onStartHashPassword(&$hashed, $password, Profile $profile=null) + { + $hashed = $this->hashPassword($password, $profile); + return false; + } + + public function onCheckSchema() + { // we only use the User database, so default AuthenticationPlugin stuff can be ignored return true; } - function onUserDeleteRelated($user, &$tables) { + public function onUserDeleteRelated($user, &$tables) + { // not using User_username table, so no need to add it here. return true; } - function onPluginVersion(&$versions) + public function onPluginVersion(&$versions) { $versions[] = array('name' => 'AuthCrypt', 'version' => STATUSNET_VERSION, diff --git a/plugins/AuthCrypt/README b/plugins/AuthCrypt/README index cc0541c35f..5d1d1bbbfc 100644 --- a/plugins/AuthCrypt/README +++ b/plugins/AuthCrypt/README @@ -9,16 +9,11 @@ Installation ============ Add either of the following configurations to your config.php (see Example below for more options): +The recommended use is to overwrite old hash values when logging in (statusnet + overwrite modes) 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. 'statusnet' and 'overwrite' are true by default. 'authoritative' is only necessary if we want to exclude other plugins from verifying the password. + 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: +To disable updating to crypt() on password change (and login with the 'statusnet' compatibility mode), simply set the 'overwrite' setting to false: addPlugin('AuthCrypt', array( 'overwrite'=>false, @@ -28,21 +23,15 @@ 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() +authoritative (false): Set this 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()) +statusnet (true): Do we check the password against legacy StatusNet md5 hash? + (notice: will check password login against old-style hash and if 'overwrite' is enabled 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?