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