]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
AuthCrypt now tidied up and enabled by default.
authorMikael Nordfeldth <mmn@hethane.se>
Thu, 17 Oct 2013 14:32:53 +0000 (16:32 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Thu, 17 Oct 2013 14:36:15 +0000 (16:36 +0200)
EVENTS.txt
INSTALL
lib/passwordhashexception.php [new file with mode: 0644]
lib/siteprofile.php
lib/util.php
plugins/AuthCrypt/AuthCryptPlugin.php
plugins/AuthCrypt/README

index a113bb56a1040b046b075e4a620cf12a364274a3..7e08430218c27fee283d029344ff1f8241765266 100644 (file)
@@ -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 ea700ebdb7da9faa8b0208006cc2733963ad1dd8..bac8d3400504fca8b93466575e0e196b1ee25bb9 100644 (file)
--- 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 (file)
index 0000000..d51505f
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Class for an exception when password hashing was unsuccessful
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Exception
+ * @package   GNUsocial
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
+ * @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);
+    }
+}
index dbb74a81b2fb9816f41e562077402f953c8abdd1..86ef13b5506544b8a73e541c6df575f420f9691f 100644 (file)
@@ -80,6 +80,7 @@ abstract class SiteProfileSettings
 
     static function defaultPlugins() {
         return array(
+            'AuthCrypt'               => null,
             'Bookmark'                => null,
             'Event'                   => null,
             'OpenID'                  => null,
index 81643f9a192e8d9deb741a1e44ac9cf87f5767ab..608938c4c0c45db366fada5bcc90aac17039d496 100644 (file)
@@ -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));
+    }
+    if (empty($hashed)) {
+        throw new PasswordHashException();
     }
-    return md5($password . $id);
+
+    return $hashed;
 }
 
 /**
index 83885ab799885f39919a1013eec4becf863cccda..26366879e6664827ad73d2fa35eb10fc01cf278c 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Plugin
- * @package   StatusNet
+ * @package   GNUsocial
  * @author    Mikael Nordfeldth <mmn@hethane.se>
  * @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 <mmn@hethane.se>
- * @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,
index cc0541c35f1ce2c2bffc8b6cdd8d95153a9756a5..5d1d1bbbfcba647111fc78fe7850103e6d0c5dbb 100644 (file)
@@ -9,16 +9,11 @@ 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.
+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', array(
-        'authoritative'=>true,
-        'hostile'=>true,
-    ));
+    addPlugin('AuthCrypt');
 
-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?