Blame SOURCES/0009-Disabling-gracelimit-does-not-prevent-LDAP-binds_rhbz#2109236.patch

706194
From 1316cd8b2252c2543cf2ef2186956a8833037b1e Mon Sep 17 00:00:00 2001
706194
From: Rob Crittenden <rcritten@redhat.com>
706194
Date: Thu, 21 Jul 2022 09:28:46 -0400
706194
Subject: [PATCH] Disabling gracelimit does not prevent LDAP binds
706194
706194
Originally the code treated 0 as disabled. This was
706194
changed during the review process to -1 but one remnant
706194
was missed effetively allowing gracelimit 0 to also mean
706194
disabled.
706194
706194
Add explicit tests for testing with gracelimit = 0 and
706194
gracelimit = -1.
706194
706194
Also remove some extranous "str(self.master.domain.basedn)"
706194
lines from some of the tests.
706194
706194
Fixes: https://pagure.io/freeipa/issue/9206
706194
706194
Signed-off-by: Rob Crittenden <rcritten@redhat.com>
706194
Reviewed-By: Francisco Trivino <ftrivino@redhat.com>
706194
---
706194
 .../ipa-graceperiod/ipa_graceperiod.c         |  2 +-
706194
 ipatests/test_integration/test_pwpolicy.py    | 55 ++++++++++++++++++-
706194
 2 files changed, 53 insertions(+), 4 deletions(-)
706194
706194
diff --git a/daemons/ipa-slapi-plugins/ipa-graceperiod/ipa_graceperiod.c b/daemons/ipa-slapi-plugins/ipa-graceperiod/ipa_graceperiod.c
706194
index a3f57cb4b..345e1dee7 100644
706194
--- a/daemons/ipa-slapi-plugins/ipa-graceperiod/ipa_graceperiod.c
706194
+++ b/daemons/ipa-slapi-plugins/ipa-graceperiod/ipa_graceperiod.c
706194
@@ -479,7 +479,7 @@ static int ipagraceperiod_preop(Slapi_PBlock *pb)
706194
         if (pwresponse_requested) {
706194
             slapi_pwpolicy_make_response_control(pb, -1, grace_limit - grace_user_time , -1);
706194
         }
706194
-    } else if ((grace_limit > 0) && (grace_user_time >= grace_limit)) {
706194
+    } else if (grace_user_time >= grace_limit) {
706194
         LOG_TRACE("%s password is expired and out of grace limit\n", dn);
706194
         errstr = "Password is expired.\n";
706194
         ret = LDAP_INVALID_CREDENTIALS;
706194
diff --git a/ipatests/test_integration/test_pwpolicy.py b/ipatests/test_integration/test_pwpolicy.py
706194
index 6d6698284..41d6e9070 100644
706194
--- a/ipatests/test_integration/test_pwpolicy.py
706194
+++ b/ipatests/test_integration/test_pwpolicy.py
706194
@@ -36,7 +36,7 @@ class TestPWPolicy(IntegrationTest):
706194
         cls.master.run_command(['ipa', 'group-add-member', POLICY,
706194
                                 '--users', USER])
706194
         cls.master.run_command(['ipa', 'pwpolicy-add', POLICY,
706194
-                                '--priority', '1'])
706194
+                                '--priority', '1', '--gracelimit', '-1'])
706194
         cls.master.run_command(['ipa', 'passwd', USER],
706194
                                stdin_text='{password}\n{password}\n'.format(
706194
                                password=PASSWORD
706194
@@ -265,7 +265,6 @@ class TestPWPolicy(IntegrationTest):
706194
 
706194
     def test_graceperiod_expired(self):
706194
         """Test the LDAP bind grace period"""
706194
-        str(self.master.domain.basedn)
706194
         dn = "uid={user},cn=users,cn=accounts,{base_dn}".format(
706194
              user=USER, base_dn=str(self.master.domain.basedn))
706194
 
706194
@@ -308,7 +307,6 @@ class TestPWPolicy(IntegrationTest):
706194
 
706194
     def test_graceperiod_not_replicated(self):
706194
         """Test that the grace period is reset on password reset"""
706194
-        str(self.master.domain.basedn)
706194
         dn = "uid={user},cn=users,cn=accounts,{base_dn}".format(
706194
              user=USER, base_dn=str(self.master.domain.basedn))
706194
 
706194
@@ -341,3 +339,54 @@ class TestPWPolicy(IntegrationTest):
706194
         )
706194
         assert 'passwordgraceusertime: 0' in result.stdout_text.lower()
706194
         self.reset_password(self.master)
706194
+
706194
+    def test_graceperiod_zero(self):
706194
+        """Test the LDAP bind with zero grace period"""
706194
+        dn = "uid={user},cn=users,cn=accounts,{base_dn}".format(
706194
+             user=USER, base_dn=str(self.master.domain.basedn))
706194
+
706194
+        self.master.run_command(
706194
+            ["ipa", "pwpolicy-mod", POLICY, "--gracelimit", "0", ],
706194
+        )
706194
+
706194
+        # Resetting the password will mark it as expired
706194
+        self.reset_password(self.master)
706194
+
706194
+        # Now grace is done and binds should fail.
706194
+        result = self.master.run_command(
706194
+            ["ldapsearch", "-e", "ppolicy", "-D", dn,
706194
+             "-w", PASSWORD, "-b", dn], raiseonerr=False
706194
+        )
706194
+        assert result.returncode == 49
706194
+
706194
+        assert 'Password is expired' in result.stderr_text
706194
+        assert 'Password expired, 0 grace logins remain' in result.stderr_text
706194
+
706194
+    def test_graceperiod_disabled(self):
706194
+        """Test the LDAP bind with grace period disabled (-1)"""
706194
+        str(self.master.domain.basedn)
706194
+        dn = "uid={user},cn=users,cn=accounts,{base_dn}".format(
706194
+             user=USER, base_dn=str(self.master.domain.basedn))
706194
+
706194
+        # This can fail if gracelimit is already -1 so ignore it
706194
+        self.master.run_command(
706194
+            ["ipa", "pwpolicy-mod", POLICY, "--gracelimit", "-1",],
706194
+            raiseonerr=False,
706194
+        )
706194
+
706194
+        # Ensure the password is expired
706194
+        self.reset_password(self.master)
706194
+
706194
+        result = self.kinit_as_user(self.master, PASSWORD, PASSWORD)
706194
+
706194
+        for _i in range(0, 10):
706194
+            result = self.master.run_command(
706194
+                ["ldapsearch", "-e", "ppolicy", "-D", dn,
706194
+                 "-w", PASSWORD, "-b", dn]
706194
+            )
706194
+
706194
+        # With graceperiod disabled it should not increment
706194
+        result = tasks.ldapsearch_dm(
706194
+            self.master, dn, ['passwordgraceusertime',],
706194
+        )
706194
+        assert 'passwordgraceusertime: 0' in result.stdout_text.lower()
706194
-- 
706194
2.37.2
706194