Blame SOURCES/0047-Ticket-49524-Password-policy-minimum-token-length-fa.patch

058656
From a85f64d2c4fa2718748a205d4ae0ebab47513199 Mon Sep 17 00:00:00 2001
058656
From: Mark Reynolds <mreynolds@redhat.com>
058656
Date: Mon, 8 Jan 2018 11:34:02 -0500
058656
Subject: [PATCH] Ticket 49524 - Password policy: minimum token length fails 
058656
 when the token length is equal to attribute length
058656
058656
Bug Description:  The token checking breaks when the password is the
058656
                  exact value of the entry attribute.
058656
058656
Fix Description:  Remove the "equal" part of the string comparisons.
058656
058656
https://pagure.io/389-ds-base/issue/49524
058656
058656
Reviewed by: firstyear & spichugi(Thanks!!)
058656
058656
(cherry picked from commit 790be09fc434d394239bf2486d01f212b36cf0e3)
058656
---
058656
 .../tests/suites/password/pwdPolicy_token_test.py  | 75 ++++++++++++++++++++++
058656
 ldap/servers/slapd/pw.c                            |  2 +-
058656
 ldap/servers/slapd/utf8.c                          |  2 +-
058656
 3 files changed, 77 insertions(+), 2 deletions(-)
058656
 create mode 100644 dirsrvtests/tests/suites/password/pwdPolicy_token_test.py
058656
058656
diff --git a/dirsrvtests/tests/suites/password/pwdPolicy_token_test.py b/dirsrvtests/tests/suites/password/pwdPolicy_token_test.py
058656
new file mode 100644
058656
index 000000000..7a4de9c85
058656
--- /dev/null
058656
+++ b/dirsrvtests/tests/suites/password/pwdPolicy_token_test.py
058656
@@ -0,0 +1,75 @@
058656
+import logging
058656
+import pytest
058656
+import os
058656
+import time
058656
+import ldap
058656
+from lib389._constants import *
058656
+from lib389.idm.user import UserAccounts
058656
+from lib389.topologies import topology_st as topo
058656
+
058656
+DEBUGGING = os.getenv("DEBUGGING", default=False)
058656
+if DEBUGGING:
058656
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
058656
+else:
058656
+    logging.getLogger(__name__).setLevel(logging.INFO)
058656
+log = logging.getLogger(__name__)
058656
+
058656
+USER_DN = 'uid=Test_user1,ou=People,dc=example,dc=com'
058656
+TOKEN = 'test_user1'
058656
+
058656
+user_properties = {
058656
+    'uid': 'Test_user1',
058656
+    'cn': 'test_user1',
058656
+    'sn': 'test_user1',
058656
+    'uidNumber': '1001',
058656
+    'gidNumber': '2001',
058656
+    'userpassword': PASSWORD,
058656
+    'description': 'userdesc',
058656
+    'homeDirectory': '/home/{}'.format('test_user')}
058656
+
058656
+
058656
+def pwd_setup(topo):
058656
+    topo.standalone.config.replace_many(('passwordCheckSyntax', 'on'),
058656
+                                        ('passwordMinLength', '4'),
058656
+                                        ('passwordMinCategories', '1'))
058656
+    users = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
058656
+    return users.create(properties=user_properties)
058656
+
058656
+
058656
+def test_token_lengths(topo):
058656
+    """Test that password token length is enforced for various lengths including
058656
+    the same length as the attribute being checked by the policy.
058656
+
058656
+    :id: dae9d916-2a03-4707-b454-9e901d295b13
058656
+    :setup: Standalone instance
058656
+    :steps:
058656
+        1. Test token length rejects password of the same length as rdn value
058656
+    :expectedresults:
058656
+        1. Passwords are rejected
058656
+    """
058656
+    user = pwd_setup(topo)
058656
+    for length in ['4', '6', '10']:
058656
+        topo.standalone.simple_bind_s(DN_DM, PASSWORD)
058656
+        topo.standalone.config.set('passwordMinTokenLength', length)
058656
+        topo.standalone.simple_bind_s(USER_DN, PASSWORD)
058656
+        time.sleep(1)
058656
+
058656
+        try:
058656
+            passwd = TOKEN[:int(length)]
058656
+            log.info("Testing password len {} token ({})".format(length, passwd))
058656
+            user.replace('userpassword', passwd)
058656
+            log.fatal('Password incorrectly allowed!')
058656
+            assert False
058656
+        except ldap.CONSTRAINT_VIOLATION as e:
058656
+            log.info('Password correctly rejected: ' + str(e))
058656
+        except ldap.LDAPError as e:
058656
+            log.fatal('Unexpected failure ' + str(e))
058656
+            assert False
058656
+
058656
+
058656
+if __name__ == '__main__':
058656
+    # Run isolated
058656
+    # -s for DEBUG mode
058656
+    CURRENT_FILE = os.path.realpath(__file__)
058656
+    pytest.main("-s %s" % CURRENT_FILE)
058656
+
058656
diff --git a/ldap/servers/slapd/pw.c b/ldap/servers/slapd/pw.c
058656
index e625962e8..0cf795b41 100644
058656
--- a/ldap/servers/slapd/pw.c
058656
+++ b/ldap/servers/slapd/pw.c
058656
@@ -1465,7 +1465,7 @@ check_trivial_words(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Value **vals, char *
058656
             sp = slapi_ch_strdup(slapi_value_get_string(valp));
058656
             ep = sp + strlen(sp);
058656
             ep = ldap_utf8prevn(sp, ep, toklen);
058656
-            if (!ep || (sp >= ep)) {
058656
+            if (!ep || (sp > ep)) {
058656
                 slapi_ch_free_string(&sp);
058656
                 continue;
058656
             }
058656
diff --git a/ldap/servers/slapd/utf8.c b/ldap/servers/slapd/utf8.c
058656
index b0667c636..4538625b3 100644
058656
--- a/ldap/servers/slapd/utf8.c
058656
+++ b/ldap/servers/slapd/utf8.c
058656
@@ -152,7 +152,7 @@ ldap_utf8prevn(char *s, char *from, int n)
058656
     }
058656
     for (; n > 0; --n) {
058656
         prev = ldap_utf8prev(prev);
058656
-        if ((prev <= s) && (n > 0)) {
058656
+        if ((n > 0) && (prev < s)) {
058656
             return NULL;
058656
         }
058656
     }
058656
-- 
058656
2.13.6
058656