Blame SOURCES/0057-Ticket-49370-Add-all-the-password-policy-defaults-to.patch

058656
From 86efa0314c59550f0660c8d143a52a57b1dffb96 Mon Sep 17 00:00:00 2001
058656
From: Mark Reynolds <mreynolds@redhat.com>
058656
Date: Thu, 18 Jan 2018 09:56:17 -0500
058656
Subject: [PATCH] Ticket 49370 - Add all the password policy defaults to a new 
058656
 local policy
058656
058656
Bug Description:  When processing a local password policy we were not pulling
058656
                  in the defaults for the "on/off" settings.  This patch
058656
                  addresses that.
058656
058656
Fix Description:  Create common default init functions for all password policies
058656
058656
https://pagure.io/389-ds-base/issue/49370
058656
058656
Reviewed by: tbordaz, wibrown, and spichugi (Thanks!!!)
058656
058656
(cherry picked from commit c8b388bf9f5269e1e1dc8c7c70ec8e58e825204a)
058656
---
058656
 .../tests/suites/password/regression_test.py       | 58 +++++++++++++--
058656
 ldap/servers/slapd/libglobs.c                      | 84 ++++++++++++++--------
058656
 ldap/servers/slapd/pw.c                            | 29 ++------
058656
 ldap/servers/slapd/slap.h                          |  2 +
058656
 4 files changed, 113 insertions(+), 60 deletions(-)
058656
058656
diff --git a/dirsrvtests/tests/suites/password/regression_test.py b/dirsrvtests/tests/suites/password/regression_test.py
058656
index f6ee16773..800294057 100644
058656
--- a/dirsrvtests/tests/suites/password/regression_test.py
058656
+++ b/dirsrvtests/tests/suites/password/regression_test.py
058656
@@ -6,9 +6,10 @@
058656
 # --- END COPYRIGHT BLOCK ---
058656
 #
058656
 import pytest
058656
-from lib389._constants import SUFFIX, PASSWORD
058656
+import time
058656
+from lib389._constants import SUFFIX, PASSWORD, DN_DM
058656
 from lib389.idm.user import UserAccounts
058656
-from lib389.utils import ldap, os, logging
058656
+from lib389.utils import ldap, os, logging, ensure_bytes
058656
 from lib389.topologies import topology_st as topo
058656
 
058656
 DEBUGGING = os.getenv("DEBUGGING", default=False)
058656
@@ -20,6 +21,7 @@ log = logging.getLogger(__name__)
058656
 
058656
 user_data = {'cn': 'CNpwtest1', 'sn': 'SNpwtest1', 'uid': 'UIDpwtest1', 'mail': 'MAILpwtest1@redhat.com',
058656
              'givenname': 'GNpwtest1'}
058656
+
058656
 TEST_PASSWORDS = list(user_data.values())
058656
 # Add substring/token values of "CNpwtest1"
058656
 TEST_PASSWORDS += ['CNpwtest1ZZZZ', 'ZZZZZCNpwtest1',
058656
@@ -37,13 +39,20 @@ def passw_policy(topo, request):
058656
     """Configure password policy with PasswordCheckSyntax attribute set to on"""
058656
 
058656
     log.info('Configure Pwpolicy with PasswordCheckSyntax and nsslapd-pwpolicy-local set to on')
058656
+    topo.standalone.simple_bind_s(DN_DM, PASSWORD)
058656
     topo.standalone.config.set('PasswordExp', 'on')
058656
     topo.standalone.config.set('PasswordCheckSyntax', 'off')
058656
     topo.standalone.config.set('nsslapd-pwpolicy-local', 'on')
058656
 
058656
     subtree = 'ou=people,{}'.format(SUFFIX)
058656
     log.info('Configure subtree password policy for {}'.format(subtree))
058656
-    topo.standalone.subtreePwdPolicy(subtree, {'passwordchange': 'on', 'passwordCheckSyntax': 'on'})
058656
+    topo.standalone.subtreePwdPolicy(subtree, {'passwordchange': ensure_bytes('on'),
058656
+                                               'passwordCheckSyntax': ensure_bytes('on'),
058656
+                                               'passwordLockout': ensure_bytes('on'),
058656
+                                               'passwordResetFailureCount': ensure_bytes('3'),
058656
+                                               'passwordLockoutDuration': ensure_bytes('3'),
058656
+                                               'passwordMaxFailure': ensure_bytes('2')})
058656
+    time.sleep(1)
058656
 
058656
     def fin():
058656
         log.info('Reset pwpolicy configuration settings')
058656
@@ -76,6 +85,47 @@ def test_user(topo, request):
058656
     return tuser
058656
 
058656
 
058656
+def test_pwp_local_unlock(topo, passw_policy, test_user):
058656
+    """Test subtree policies use the same global default for passwordUnlock
058656
+
058656
+    :id: 741a8417-5f65-4012-b9ed-87987ce3ca1b
058656
+    :setup: Standalone instance
058656
+    :steps:
058656
+        1. Test user can bind
058656
+        2. Bind with bad passwords to lockout account, and verify account is locked
058656
+        3. Wait for lockout interval, and bind with valid password
058656
+    :expectedresults:
058656
+        1. Bind successful
058656
+        2. Entry is locked
058656
+        3. Entry can bind with correct password
058656
+    """
058656
+
058656
+    log.info("Verify user can bind...")
058656
+    test_user.bind(PASSWORD)
058656
+
058656
+    log.info('Test passwordUnlock default - user should be able to reset password after lockout')
058656
+    for i in range(0,2):
058656
+        try:
058656
+            test_user.bind("bad-password")
058656
+        except ldap.INVALID_CREDENTIALS:
058656
+            # expected
058656
+            pass
058656
+        except ldap.LDAPError as e:
058656
+            log.fatal("Got unexpected failure: " + atr(e))
058656
+            raise e
058656
+
058656
+
058656
+    log.info('Verify account is locked')
058656
+    with pytest.raises(ldap.CONSTRAINT_VIOLATION):
058656
+        test_user.bind(PASSWORD)
058656
+
058656
+    log.info('Wait for lockout duration...')
058656
+    time.sleep(4)
058656
+
058656
+    log.info('Check if user can now bind with correct password')
058656
+    test_user.bind(PASSWORD)
058656
+
058656
+
058656
 @pytest.mark.bz1465600
058656
 @pytest.mark.parametrize("user_pasw", TEST_PASSWORDS)
058656
 def test_trivial_passw_check(topo, passw_policy, test_user, user_pasw):
058656
@@ -143,4 +193,4 @@ if __name__ == '__main__':
058656
     # Run isolated
058656
     # -s for DEBUG mode
058656
     CURRENT_FILE = os.path.realpath(__file__)
058656
-    pytest.main("-s {}".format(CURRENT_FILE))
058656
+    pytest.main(["-s", CURRENT_FILE])
058656
diff --git a/ldap/servers/slapd/libglobs.c b/ldap/servers/slapd/libglobs.c
058656
index 1ba30002f..c1a765aca 100644
058656
--- a/ldap/servers/slapd/libglobs.c
058656
+++ b/ldap/servers/slapd/libglobs.c
058656
@@ -1401,6 +1401,56 @@ getFrontendConfig(void)
058656
  */
058656
 
058656
 void
058656
+pwpolicy_init_defaults (passwdPolicy *pw_policy)
058656
+{
058656
+    pw_policy->pw_change = LDAP_ON;
058656
+    pw_policy->pw_must_change = LDAP_OFF;
058656
+    pw_policy->pw_syntax = LDAP_OFF;
058656
+    pw_policy->pw_exp = LDAP_OFF;
058656
+    pw_policy->pw_send_expiring = LDAP_OFF;
058656
+    pw_policy->pw_minlength = SLAPD_DEFAULT_PW_MINLENGTH;
058656
+    pw_policy->pw_mindigits = SLAPD_DEFAULT_PW_MINDIGITS;
058656
+    pw_policy->pw_minalphas = SLAPD_DEFAULT_PW_MINALPHAS;
058656
+    pw_policy->pw_minuppers = SLAPD_DEFAULT_PW_MINUPPERS;
058656
+    pw_policy->pw_minlowers = SLAPD_DEFAULT_PW_MINLOWERS;
058656
+    pw_policy->pw_minspecials = SLAPD_DEFAULT_PW_MINSPECIALS;
058656
+    pw_policy->pw_min8bit = SLAPD_DEFAULT_PW_MIN8BIT;
058656
+    pw_policy->pw_maxrepeats = SLAPD_DEFAULT_PW_MAXREPEATS;
058656
+    pw_policy->pw_mincategories = SLAPD_DEFAULT_PW_MINCATEGORIES;
058656
+    pw_policy->pw_mintokenlength = SLAPD_DEFAULT_PW_MINTOKENLENGTH;
058656
+    pw_policy->pw_maxage = SLAPD_DEFAULT_PW_MAXAGE;
058656
+    pw_policy->pw_minage = SLAPD_DEFAULT_PW_MINAGE;
058656
+    pw_policy->pw_warning = SLAPD_DEFAULT_PW_WARNING;
058656
+    pw_policy->pw_history = LDAP_OFF;
058656
+    pw_policy->pw_inhistory = SLAPD_DEFAULT_PW_INHISTORY;
058656
+    pw_policy->pw_lockout = LDAP_OFF;
058656
+    pw_policy->pw_maxfailure = SLAPD_DEFAULT_PW_MAXFAILURE;
058656
+    pw_policy->pw_unlock = LDAP_ON;
058656
+    pw_policy->pw_lockduration = SLAPD_DEFAULT_PW_LOCKDURATION;
058656
+    pw_policy->pw_resetfailurecount = SLAPD_DEFAULT_PW_RESETFAILURECOUNT;
058656
+    pw_policy->pw_gracelimit = SLAPD_DEFAULT_PW_GRACELIMIT;
058656
+    pw_policy->pw_admin = NULL;
058656
+    pw_policy->pw_admin_user = NULL;
058656
+    pw_policy->pw_is_legacy = LDAP_ON;
058656
+    pw_policy->pw_track_update_time = LDAP_OFF;
058656
+}
058656
+
058656
+static void
058656
+pwpolicy_fe_init_onoff(passwdPolicy *pw_policy)
058656
+{
058656
+    init_pw_change = pw_policy->pw_change;
058656
+    init_pw_must_change = pw_policy->pw_must_change;
058656
+    init_pw_syntax = pw_policy->pw_syntax;
058656
+    init_pw_exp = pw_policy->pw_exp;
058656
+    init_pw_send_expiring = pw_policy->pw_send_expiring;
058656
+    init_pw_history = pw_policy->pw_history;
058656
+    init_pw_lockout = pw_policy->pw_lockout;
058656
+    init_pw_unlock = pw_policy->pw_unlock;
058656
+    init_pw_is_legacy = pw_policy->pw_is_legacy;
058656
+    init_pw_track_update_time = pw_policy->pw_track_update_time;
058656
+}
058656
+
058656
+void
058656
 FrontendConfig_init(void)
058656
 {
058656
     slapdFrontendConfig_t *cfg = getFrontendConfig();
058656
@@ -1511,41 +1561,13 @@ FrontendConfig_init(void)
058656
     * let clients abide by the LDAP standards and send us a SASL/EXTERNAL bind
058656
     * if that's what they want to do */
058656
     init_force_sasl_external = cfg->force_sasl_external = LDAP_OFF;
058656
-
058656
     init_readonly = cfg->readonly = LDAP_OFF;
058656
+
058656
+    pwpolicy_init_defaults(&cfg->pw_policy);
058656
+    pwpolicy_fe_init_onoff(&cfg->pw_policy);
058656
     init_pwpolicy_local = cfg->pwpolicy_local = LDAP_OFF;
058656
     init_pwpolicy_inherit_global = cfg->pwpolicy_inherit_global = LDAP_OFF;
058656
-    init_pw_change = cfg->pw_policy.pw_change = LDAP_ON;
058656
-    init_pw_must_change = cfg->pw_policy.pw_must_change = LDAP_OFF;
058656
     init_allow_hashed_pw = cfg->allow_hashed_pw = LDAP_OFF;
058656
-    init_pw_syntax = cfg->pw_policy.pw_syntax = LDAP_OFF;
058656
-    init_pw_exp = cfg->pw_policy.pw_exp = LDAP_OFF;
058656
-    init_pw_send_expiring = cfg->pw_policy.pw_send_expiring = LDAP_OFF;
058656
-    cfg->pw_policy.pw_minlength = SLAPD_DEFAULT_PW_MINLENGTH;
058656
-    cfg->pw_policy.pw_mindigits = SLAPD_DEFAULT_PW_MINDIGITS;
058656
-    cfg->pw_policy.pw_minalphas = SLAPD_DEFAULT_PW_MINALPHAS;
058656
-    cfg->pw_policy.pw_minuppers = SLAPD_DEFAULT_PW_MINUPPERS;
058656
-    cfg->pw_policy.pw_minlowers = SLAPD_DEFAULT_PW_MINLOWERS;
058656
-    cfg->pw_policy.pw_minspecials = SLAPD_DEFAULT_PW_MINSPECIALS;
058656
-    cfg->pw_policy.pw_min8bit = SLAPD_DEFAULT_PW_MIN8BIT;
058656
-    cfg->pw_policy.pw_maxrepeats = SLAPD_DEFAULT_PW_MAXREPEATS;
058656
-    cfg->pw_policy.pw_mincategories = SLAPD_DEFAULT_PW_MINCATEGORIES;
058656
-    cfg->pw_policy.pw_mintokenlength = SLAPD_DEFAULT_PW_MINTOKENLENGTH;
058656
-    cfg->pw_policy.pw_maxage = SLAPD_DEFAULT_PW_MAXAGE;
058656
-    cfg->pw_policy.pw_minage = SLAPD_DEFAULT_PW_MINAGE;
058656
-    cfg->pw_policy.pw_warning = SLAPD_DEFAULT_PW_WARNING;
058656
-    init_pw_history = cfg->pw_policy.pw_history = LDAP_OFF;
058656
-    cfg->pw_policy.pw_inhistory = SLAPD_DEFAULT_PW_INHISTORY;
058656
-    init_pw_lockout = cfg->pw_policy.pw_lockout = LDAP_OFF;
058656
-    cfg->pw_policy.pw_maxfailure = SLAPD_DEFAULT_PW_MAXFAILURE;
058656
-    init_pw_unlock = cfg->pw_policy.pw_unlock = LDAP_ON;
058656
-    cfg->pw_policy.pw_lockduration = SLAPD_DEFAULT_PW_LOCKDURATION;
058656
-    cfg->pw_policy.pw_resetfailurecount = SLAPD_DEFAULT_PW_RESETFAILURECOUNT;
058656
-    cfg->pw_policy.pw_gracelimit = SLAPD_DEFAULT_PW_GRACELIMIT;
058656
-    cfg->pw_policy.pw_admin = NULL;
058656
-    cfg->pw_policy.pw_admin_user = NULL;
058656
-    init_pw_is_legacy = cfg->pw_policy.pw_is_legacy = LDAP_ON;
058656
-    init_pw_track_update_time = cfg->pw_policy.pw_track_update_time = LDAP_OFF;
058656
     init_pw_is_global_policy = cfg->pw_is_global_policy = LDAP_OFF;
058656
 
058656
     init_accesslog_logging_enabled = cfg->accesslog_logging_enabled = LDAP_ON;
058656
diff --git a/ldap/servers/slapd/pw.c b/ldap/servers/slapd/pw.c
058656
index 53464c64a..3a545e12e 100644
058656
--- a/ldap/servers/slapd/pw.c
058656
+++ b/ldap/servers/slapd/pw.c
058656
@@ -1730,32 +1730,11 @@ new_passwdPolicy(Slapi_PBlock *pb, const char *dn)
058656
                 goto done;
058656
             }
058656
 
058656
-            /* Set the default values */
058656
-            pwdpolicy->pw_mintokenlength = SLAPD_DEFAULT_PW_MINTOKENLENGTH;
058656
-            pwdpolicy->pw_minlength = SLAPD_DEFAULT_PW_MINLENGTH;
058656
-            pwdpolicy->pw_mindigits = SLAPD_DEFAULT_PW_MINDIGITS;
058656
-            pwdpolicy->pw_minalphas = SLAPD_DEFAULT_PW_MINALPHAS;
058656
-            pwdpolicy->pw_minuppers = SLAPD_DEFAULT_PW_MINUPPERS;
058656
-            pwdpolicy->pw_minlowers = SLAPD_DEFAULT_PW_MINLOWERS;
058656
-            pwdpolicy->pw_minspecials = SLAPD_DEFAULT_PW_MINSPECIALS;
058656
-            pwdpolicy->pw_min8bit = SLAPD_DEFAULT_PW_MIN8BIT;
058656
-            pwdpolicy->pw_maxrepeats = SLAPD_DEFAULT_PW_MAXREPEATS;
058656
-            pwdpolicy->pw_mincategories = SLAPD_DEFAULT_PW_MINCATEGORIES;
058656
-            pwdpolicy->pw_maxage = SLAPD_DEFAULT_PW_MAXAGE;
058656
-            pwdpolicy->pw_minage = SLAPD_DEFAULT_PW_MINAGE;
058656
-            pwdpolicy->pw_warning = SLAPD_DEFAULT_PW_WARNING;
058656
-            pwdpolicy->pw_inhistory = SLAPD_DEFAULT_PW_INHISTORY;
058656
-            pwdpolicy->pw_maxfailure = SLAPD_DEFAULT_PW_MAXFAILURE;
058656
-            pwdpolicy->pw_lockduration = SLAPD_DEFAULT_PW_LOCKDURATION;
058656
-            pwdpolicy->pw_resetfailurecount = SLAPD_DEFAULT_PW_RESETFAILURECOUNT;
058656
-            pwdpolicy->pw_gracelimit = SLAPD_DEFAULT_PW_GRACELIMIT;
058656
-
058656
-            /* set the default passwordLegacyPolicy setting */
058656
-            pwdpolicy->pw_is_legacy = 1;
058656
-
058656
-            /* set passwordTrackUpdateTime */
058656
-            pwdpolicy->pw_track_update_time = slapdFrontendConfig->pw_policy.pw_track_update_time;
058656
+            /* Set the default values (from libglobs.c) */
058656
+            pwpolicy_init_defaults(pwdpolicy);
058656
+            pwdpolicy->pw_storagescheme = slapdFrontendConfig->pw_storagescheme;
058656
 
058656
+            /* Set the defined values now */
058656
             for (slapi_entry_first_attr(pw_entry, &attr); attr;
058656
                  slapi_entry_next_attr(pw_entry, attr, &attr)) {
058656
                 slapi_attr_get_type(attr, &attr_name);
058656
diff --git a/ldap/servers/slapd/slap.h b/ldap/servers/slapd/slap.h
058656
index 08754d8fb..f6fc374a4 100644
058656
--- a/ldap/servers/slapd/slap.h
058656
+++ b/ldap/servers/slapd/slap.h
058656
@@ -1773,6 +1773,8 @@ typedef struct passwordpolicyarray
058656
     Slapi_DN **pw_admin_user;
058656
 } passwdPolicy;
058656
 
058656
+void pwpolicy_init_defaults (passwdPolicy *pw_policy);
058656
+
058656
 Slapi_PBlock *slapi_pblock_clone(Slapi_PBlock *pb); /* deprecated */
058656
 
058656
 passwdPolicy *slapi_pblock_get_pwdpolicy(Slapi_PBlock *pb);
058656
-- 
058656
2.13.6
058656