74ca47
From 578d207cd66e97e9ff8211559c62114a961e35a8 Mon Sep 17 00:00:00 2001
74ca47
From: Mark Reynolds <mreynolds@redhat.com>
74ca47
Date: Tue, 28 Mar 2017 14:21:47 -0400
74ca47
Subject: [PATCH] Issue 49039 - password min age should be ignored if password
74ca47
 needs to be reset
74ca47
74ca47
Description:  Do not check the password minimum age when changing a password
74ca47
              if the password "must" be reset.
74ca47
74ca47
https://pagure.io/389-ds-base/issue/49039
74ca47
74ca47
Reviewed by: firstyear(Thanks!)
74ca47
---
74ca47
 dirsrvtests/tests/tickets/ticket49039_test.py | 79 +++++++++++++++++++++++++++
74ca47
 ldap/servers/slapd/modify.c                   |  4 +-
74ca47
 2 files changed, 81 insertions(+), 2 deletions(-)
74ca47
 create mode 100644 dirsrvtests/tests/tickets/ticket49039_test.py
74ca47
74ca47
diff --git a/dirsrvtests/tests/tickets/ticket49039_test.py b/dirsrvtests/tests/tickets/ticket49039_test.py
74ca47
new file mode 100644
74ca47
index 0000000..e6d4c03
74ca47
--- /dev/null
74ca47
+++ b/dirsrvtests/tests/tickets/ticket49039_test.py
74ca47
@@ -0,0 +1,79 @@
74ca47
+import time
74ca47
+import ldap
74ca47
+import logging
74ca47
+import pytest
74ca47
+from lib389 import Entry
74ca47
+from lib389._constants import *
74ca47
+from lib389.properties import *
74ca47
+from lib389.tasks import *
74ca47
+from lib389.utils import *
74ca47
+from lib389.topologies import topology_st as topo
74ca47
+
74ca47
+DEBUGGING = os.getenv("DEBUGGING", default=False)
74ca47
+if DEBUGGING:
74ca47
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
74ca47
+else:
74ca47
+    logging.getLogger(__name__).setLevel(logging.INFO)
74ca47
+log = logging.getLogger(__name__)
74ca47
+
74ca47
+USER_DN = 'uid=user,dc=example,dc=com'
74ca47
+
74ca47
+
74ca47
+def test_ticket49039(topo):
74ca47
+    """Test "password must change" verses "password min age".  Min age should not
74ca47
+    block password update if the password was reset.
74ca47
+    """
74ca47
+
74ca47
+    # Configure password policy
74ca47
+    try:
74ca47
+        topo.standalone.modify_s("cn=config", [(ldap.MOD_REPLACE, 'nsslapd-pwpolicy-local', 'on'),
74ca47
+                                               (ldap.MOD_REPLACE, 'passwordMustChange', 'on'),
74ca47
+                                               (ldap.MOD_REPLACE, 'passwordExp', 'on'),
74ca47
+                                               (ldap.MOD_REPLACE, 'passwordMaxAge', '86400000'),
74ca47
+                                               (ldap.MOD_REPLACE, 'passwordMinAge', '8640000'),
74ca47
+                                               (ldap.MOD_REPLACE, 'passwordChange', 'on')])
74ca47
+    except ldap.LDAPError as e:
74ca47
+        log.fatal('Failed to set password policy: ' + str(e))
74ca47
+
74ca47
+    # Add user, bind, and set password
74ca47
+    try:
74ca47
+        topo.standalone.add_s(Entry((USER_DN, {
74ca47
+            'objectclass': 'top extensibleObject'.split(),
74ca47
+            'uid': 'user1',
74ca47
+            'userpassword': PASSWORD
74ca47
+        })))
74ca47
+    except ldap.LDAPError as e:
74ca47
+        log.fatal('Failed to add user: error ' + e.message['desc'])
74ca47
+        assert False
74ca47
+
74ca47
+    # Reset password as RootDN
74ca47
+    try:
74ca47
+        topo.standalone.modify_s(USER_DN, [(ldap.MOD_REPLACE, 'userpassword', PASSWORD)])
74ca47
+    except ldap.LDAPError as e:
74ca47
+        log.fatal('Failed to bind: error ' + e.message['desc'])
74ca47
+        assert False
74ca47
+
74ca47
+    time.sleep(1)
74ca47
+
74ca47
+    # Reset password as user
74ca47
+    try:
74ca47
+        topo.standalone.simple_bind_s(USER_DN, PASSWORD)
74ca47
+    except ldap.LDAPError as e:
74ca47
+        log.fatal('Failed to bind: error ' + e.message['desc'])
74ca47
+        assert False
74ca47
+
74ca47
+    try:
74ca47
+        topo.standalone.modify_s(USER_DN, [(ldap.MOD_REPLACE, 'userpassword', PASSWORD)])
74ca47
+    except ldap.LDAPError as e:
74ca47
+        log.fatal('Failed to change password: error ' + e.message['desc'])
74ca47
+        assert False
74ca47
+
74ca47
+    log.info('Test Passed')
74ca47
+
74ca47
+
74ca47
+if __name__ == '__main__':
74ca47
+    # Run isolated
74ca47
+    # -s for DEBUG mode
74ca47
+    CURRENT_FILE = os.path.realpath(__file__)
74ca47
+    pytest.main("-s %s" % CURRENT_FILE)
74ca47
+
74ca47
diff --git a/ldap/servers/slapd/modify.c b/ldap/servers/slapd/modify.c
74ca47
index 4bef90a..32defae 100644
74ca47
--- a/ldap/servers/slapd/modify.c
74ca47
+++ b/ldap/servers/slapd/modify.c
74ca47
@@ -1326,8 +1326,8 @@ static int op_shared_allow_pw_change (Slapi_PBlock *pb, LDAPMod *mod, char **old
74ca47
 	       
74ca47
 	/* check if password is within password minimum age;
74ca47
 	   error result is sent directly from check_pw_minage */	
74ca47
-	if ((internal_op || !pb->pb_conn->c_needpw) && 
74ca47
-         check_pw_minage(pb, &sdn, mod->mod_bvalues) == 1)
74ca47
+	if (!pb->pb_conn->c_needpw &&
74ca47
+	    check_pw_minage(pb, &sdn, mod->mod_bvalues) == 1)
74ca47
 	{
74ca47
 		if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
74ca47
 		{
74ca47
-- 
74ca47
2.9.3
74ca47