Blame SOURCES/0014-Issue-49192-Deleting-suffix-can-hang-server.patch

61f723
From 353955ba9b4c487e30315d39d1880b6b784817d2 Mon Sep 17 00:00:00 2001
61f723
From: Mark Reynolds <mreynolds@redhat.com>
61f723
Date: Mon, 27 Mar 2017 10:59:40 -0400
61f723
Subject: [PATCH] Issue 49192 - Deleting suffix can hang server
61f723
61f723
Description: If you attempt to bind as an inactive user the backend rwlock
61f723
             is not unlocked.  Regression introduced from issue 49051.
61f723
61f723
https://pagure.io/389-ds-base/issue/49192
61f723
61f723
Reviewed by: nhosoi(Thanks!)
61f723
---
61f723
 dirsrvtests/tests/tickets/ticket49192_test.py | 177 ++++++++++++++++++++++++++
61f723
 ldap/servers/slapd/bind.c                     |   3 -
61f723
 ldap/servers/slapd/pw_verify.c                |   8 +-
61f723
 3 files changed, 179 insertions(+), 9 deletions(-)
61f723
 create mode 100644 dirsrvtests/tests/tickets/ticket49192_test.py
61f723
61f723
diff --git a/dirsrvtests/tests/tickets/ticket49192_test.py b/dirsrvtests/tests/tickets/ticket49192_test.py
61f723
new file mode 100644
61f723
index 0000000..f770ba7
61f723
--- /dev/null
61f723
+++ b/dirsrvtests/tests/tickets/ticket49192_test.py
61f723
@@ -0,0 +1,177 @@
61f723
+import time
61f723
+import ldap
61f723
+import logging
61f723
+import pytest
61f723
+from lib389 import Entry
61f723
+from lib389._constants import *
61f723
+from lib389.properties import *
61f723
+from lib389.tasks import *
61f723
+from lib389.utils import *
61f723
+from lib389.topologies import topology_st as topo
61f723
+
61f723
+DEBUGGING = os.getenv("DEBUGGING", default=False)
61f723
+if DEBUGGING:
61f723
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
61f723
+else:
61f723
+    logging.getLogger(__name__).setLevel(logging.INFO)
61f723
+log = logging.getLogger(__name__)
61f723
+
61f723
+INDEX_DN = 'cn=index,cn=Second_Backend,cn=ldbm database,cn=plugins,cn=config'
61f723
+SUFFIX_DN = 'cn=Second_Backend,cn=ldbm database,cn=plugins,cn=config'
61f723
+MY_SUFFIX = "o=hang.com"
61f723
+USER_DN = 'uid=user,' + MY_SUFFIX
61f723
+
61f723
+
61f723
+def test_ticket49192(topo):
61f723
+    """Trigger deadlock when removing suffix
61f723
+    """
61f723
+
61f723
+    #
61f723
+    # Create a second suffix/backend
61f723
+    #
61f723
+    log.info('Creating second backend...')
61f723
+    topo.standalone.backends.create(None, properties={
61f723
+        BACKEND_NAME: "Second_Backend",
61f723
+        'suffix': "o=hang.com",
61f723
+        })
61f723
+    try:
61f723
+        topo.standalone.add_s(Entry(("o=hang.com", {
61f723
+            'objectclass': 'top organization'.split(),
61f723
+            'o': 'hang.com'})))
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to create 2nd suffix: error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    #
61f723
+    # Add roles
61f723
+    #
61f723
+    log.info('Adding roles...')
61f723
+    try:
61f723
+        topo.standalone.add_s(Entry(('cn=nsManagedDisabledRole,' + MY_SUFFIX, {
61f723
+            'objectclass': ['top', 'LdapSubEntry',
61f723
+                            'nsRoleDefinition',
61f723
+                            'nsSimpleRoleDefinition',
61f723
+                            'nsManagedRoleDefinition'],
61f723
+            'cn': 'nsManagedDisabledRole'})))
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to add managed role: error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    try:
61f723
+        topo.standalone.add_s(Entry(('cn=nsDisabledRole,' + MY_SUFFIX, {
61f723
+            'objectclass': ['top', 'LdapSubEntry',
61f723
+                            'nsRoleDefinition',
61f723
+                            'nsComplexRoleDefinition',
61f723
+                            'nsNestedRoleDefinition'],
61f723
+            'cn': 'nsDisabledRole',
61f723
+            'nsRoledn': 'cn=nsManagedDisabledRole,' + MY_SUFFIX})))
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to add nested role: error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    try:
61f723
+        topo.standalone.add_s(Entry(('cn=nsAccountInactivationTmp,' + MY_SUFFIX, {
61f723
+            'objectclass': ['top', 'nsContainer'],
61f723
+            'cn': 'nsAccountInactivationTmp'})))
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to add container: error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    try:
61f723
+        topo.standalone.add_s(Entry(('cn=\"cn=nsDisabledRole,' + MY_SUFFIX + '\",cn=nsAccountInactivationTmp,'  + MY_SUFFIX, {
61f723
+            'objectclass': ['top', 'extensibleObject', 'costemplate',
61f723
+                            'ldapsubentry'],
61f723
+            'nsAccountLock': 'true'})))
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to add cos1: error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    try:
61f723
+        topo.standalone.add_s(Entry(('cn=nsAccountInactivation_cos,' + MY_SUFFIX, {
61f723
+            'objectclass': ['top', 'LdapSubEntry', 'cosSuperDefinition',
61f723
+                            'cosClassicDefinition'],
61f723
+            'cn': 'nsAccountInactivation_cos',
61f723
+            'cosTemplateDn': 'cn=nsAccountInactivationTmp,' + MY_SUFFIX,
61f723
+            'cosSpecifier': 'nsRole',
61f723
+            'cosAttribute': 'nsAccountLock operational'})))
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to add cos2 : error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    #
61f723
+    # Add test entry
61f723
+    #
61f723
+    try:
61f723
+        topo.standalone.add_s(Entry((USER_DN, {
61f723
+            'objectclass': 'top extensibleObject'.split(),
61f723
+            'uid': 'user',
61f723
+            'userpassword': 'password',
61f723
+        })))
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to add user: error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    #
61f723
+    # Inactivate the user account
61f723
+    #
61f723
+    try:
61f723
+        topo.standalone.modify_s(USER_DN,
61f723
+                                [(ldap.MOD_ADD,
61f723
+                                  'nsRoleDN',
61f723
+                                  'cn=nsManagedDisabledRole,' + MY_SUFFIX)])
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Failed to disable user: error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    time.sleep(1)
61f723
+
61f723
+    # Bind as user (should fail)
61f723
+    try:
61f723
+        topo.standalone.simple_bind_s(USER_DN, 'password')
61f723
+        log.error("Bind incorrectly worked")
61f723
+        assert False
61f723
+    except ldap.UNWILLING_TO_PERFORM:
61f723
+        log.info('Got error 53 as expected')
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('Bind has unexpected error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    # Bind as root DN
61f723
+    try:
61f723
+        topo.standalone.simple_bind_s(DN_DM, PASSWORD)
61f723
+    except ldap.LDAPError as e:
61f723
+        log.fatal('RootDN Bind has unexpected error ' + e.message['desc'])
61f723
+        assert False
61f723
+
61f723
+    #
61f723
+    # Delete suffix
61f723
+    #
61f723
+    log.info('Delete the suffix and children...')
61f723
+    try:
61f723
+        index_entries = topo.standalone.search_s(
61f723
+            SUFFIX_DN, ldap.SCOPE_SUBTREE, 'objectclass=top')
61f723
+    except ldap.LDAPError as e:
61f723
+            log.error('Failed to search: %s - error %s' % (SUFFIX_DN, str(e)))
61f723
+
61f723
+    for entry in reversed(index_entries):
61f723
+        try:
61f723
+            log.info("Deleting: " + entry.dn)
61f723
+            if entry.dn != SUFFIX_DN and entry.dn != INDEX_DN:
61f723
+                topo.standalone.search_s(entry.dn,
61f723
+                                         ldap.SCOPE_ONELEVEL,
61f723
+                                         'objectclass=top')
61f723
+            topo.standalone.delete_s(entry.dn)
61f723
+        except ldap.LDAPError as e:
61f723
+            log.fatal('Failed to delete entry: %s - error %s' %
61f723
+                      (entry.dn, str(e)))
61f723
+            assert False
61f723
+
61f723
+    log.info("Test Passed")
61f723
+
61f723
+
61f723
+if __name__ == '__main__':
61f723
+    # Run isolated
61f723
+    # -s for DEBUG mode
61f723
+    CURRENT_FILE = os.path.realpath(__file__)
61f723
+    pytest.main("-s %s" % CURRENT_FILE)
61f723
+
61f723
diff --git a/ldap/servers/slapd/bind.c b/ldap/servers/slapd/bind.c
61f723
index 5c4fada..f83df7d 100644
61f723
--- a/ldap/servers/slapd/bind.c
61f723
+++ b/ldap/servers/slapd/bind.c
61f723
@@ -771,9 +771,6 @@ do_bind( Slapi_PBlock *pb )
61f723
                     /* need_new_pw failed; need_new_pw already send_ldap_result in it. */
61f723
                     goto free_and_return;
61f723
                 } 
61f723
-                if (be) {
61f723
-                    slapi_be_Unlock(be);
61f723
-                }
61f723
             } else {	/* anonymous */
61f723
                 /* set bind creds here so anonymous limits are set */
61f723
                 bind_credentials_set(pb->pb_conn, authtype, NULL, NULL, NULL, NULL, NULL);
61f723
diff --git a/ldap/servers/slapd/pw_verify.c b/ldap/servers/slapd/pw_verify.c
61f723
index a9fd9ec..852b027 100644
61f723
--- a/ldap/servers/slapd/pw_verify.c
61f723
+++ b/ldap/servers/slapd/pw_verify.c
61f723
@@ -50,8 +50,6 @@ pw_verify_root_dn(const char *dn, const Slapi_Value *cred)
61f723
  *
61f723
  * In the future, this will use the credentials and do mfa.
61f723
  *
61f723
- * If you get SLAPI_BIND_SUCCESS or SLAPI_BIND_ANONYMOUS you need to unlock
61f723
- * the backend.
61f723
  * All other results, it's already released.
61f723
  */
61f723
 int
61f723
@@ -81,10 +79,8 @@ pw_verify_be_dn(Slapi_PBlock *pb, Slapi_Entry **referral)
61f723
     set_db_default_result_handlers(pb);
61f723
     /* now take the dn, and check it */
61f723
     rc = (*be->be_bind)(pb);
61f723
-    /* now attempt the bind. */
61f723
-    if (rc != SLAPI_BIND_SUCCESS && rc != SLAPI_BIND_ANONYMOUS) {
61f723
-        slapi_be_Unlock(be);
61f723
-    }
61f723
+    slapi_be_Unlock(be);
61f723
+
61f723
     return rc;
61f723
 }
61f723
 
61f723
-- 
61f723
2.9.3
61f723