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

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