4f630f
From 99dbba52eb45628c7f290e9ed3aeabb2a2a67db4 Mon Sep 17 00:00:00 2001
4f630f
From: Mark Reynolds <mreynolds@redhat.com>
4f630f
Date: Tue, 13 Dec 2022 09:41:34 -0500
4f630f
Subject: [PATCH] Issue 5413 - Allow mutliple MemberOf fixup tasks with
4f630f
 different bases/filters
4f630f
4f630f
Description:
4f630f
4f630f
A change was made to only allow a single fixup task at a time, but there are
4f630f
cases where you would want to run mutliple tasks but on different branches/filters.
4f630f
4f630f
Now we maintain a linked list of bases/filters of the current running tasks to
4f630f
monitor this.
4f630f
4f630f
relates: https://github.com/389ds/389-ds-base/issues/5413
4f630f
4f630f
Reviewed by: tbordaz(Thanks!)
4f630f
---
4f630f
 .../suites/memberof_plugin/fixup_test.py      |   5 +-
4f630f
 ldap/servers/plugins/memberof/memberof.c      | 101 ++++++++++++++----
4f630f
 2 files changed, 85 insertions(+), 21 deletions(-)
4f630f
4f630f
diff --git a/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py b/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py
4f630f
index 9566e144c..d5369439f 100644
4f630f
--- a/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py
4f630f
+++ b/dirsrvtests/tests/suites/memberof_plugin/fixup_test.py
4f630f
@@ -59,12 +59,15 @@ def test_fixup_task_limit(topo):
4f630f
     with pytest.raises(ldap.UNWILLING_TO_PERFORM):
4f630f
         memberof.fixup(DEFAULT_SUFFIX)
4f630f
 
4f630f
+    # Add second task but on different suffix which should be allowed
4f630f
+    memberof.fixup("ou=people," + DEFAULT_SUFFIX)
4f630f
+
4f630f
     # Wait for first task to complete
4f630f
     task.wait()
4f630f
 
4f630f
     # Add new task which should be allowed now
4f630f
     memberof.fixup(DEFAULT_SUFFIX)
4f630f
-     
4f630f
+
4f630f
 
4f630f
 if __name__ == '__main__':
4f630f
     # Run isolated
4f630f
diff --git a/ldap/servers/plugins/memberof/memberof.c b/ldap/servers/plugins/memberof/memberof.c
4f630f
index 0b8cfe95c..a14617044 100644
4f630f
--- a/ldap/servers/plugins/memberof/memberof.c
4f630f
+++ b/ldap/servers/plugins/memberof/memberof.c
4f630f
@@ -52,7 +52,6 @@ static Slapi_DN* _pluginDN = NULL;
4f630f
 MemberOfConfig *qsortConfig = 0;
4f630f
 static int usetxn = 0;
4f630f
 static int premodfn = 0;
4f630f
-static PRBool fixup_running = PR_FALSE;
4f630f
 static PRLock *fixup_lock = NULL;
4f630f
 static int32_t fixup_progress_count = 0;
4f630f
 static int64_t fixup_progress_elapsed = 0;
4f630f
@@ -65,6 +64,15 @@ typedef struct _memberofstringll
4f630f
     void *next;
4f630f
 } memberofstringll;
4f630f
 
4f630f
+typedef struct _fixup_ll
4f630f
+{
4f630f
+    Slapi_DN *sdn;
4f630f
+    char *filter_str;
4f630f
+    void *next;
4f630f
+} mo_fixup_ll;
4f630f
+
4f630f
+static mo_fixup_ll *fixup_list = NULL;
4f630f
+
4f630f
 typedef struct _memberof_get_groups_data
4f630f
 {
4f630f
     MemberOfConfig *config;
4f630f
@@ -438,6 +446,15 @@ memberof_postop_close(Slapi_PBlock *pb __attribute__((unused)))
4f630f
     PR_DestroyLock(fixup_lock);
4f630f
     fixup_lock = NULL;
4f630f
 
4f630f
+    mo_fixup_ll *fixup_task = fixup_list;
4f630f
+    while (fixup_task != NULL) {
4f630f
+        mo_fixup_ll *tmp = fixup_task;
4f630f
+        fixup_task = fixup_task->next;
4f630f
+        slapi_sdn_free(&tmp->sdn);
4f630f
+        slapi_ch_free_string(&tmp->filter_str);
4f630f
+        slapi_ch_free((void**)&tmp);
4f630f
+    }
4f630f
+
4f630f
     slapi_log_err(SLAPI_LOG_TRACE, MEMBEROF_PLUGIN_SUBSYSTEM,
4f630f
                   "<-- memberof_postop_close\n");
4f630f
     return 0;
4f630f
@@ -2817,7 +2834,6 @@ memberof_fixup_task_thread(void *arg)
4f630f
     }
4f630f
 
4f630f
     PR_Lock(fixup_lock);
4f630f
-    fixup_running = PR_TRUE;
4f630f
     fixup_progress_count = 0;
4f630f
     fixup_progress_elapsed = slapi_current_rel_time_t();
4f630f
     fixup_start_time = slapi_current_rel_time_t();
4f630f
@@ -2849,11 +2865,10 @@ memberof_fixup_task_thread(void *arg)
4f630f
     /* Mark this as a task operation */
4f630f
     configCopy.fixup_task = 1;
4f630f
     configCopy.task = task;
4f630f
-
4f630f
+    Slapi_DN *sdn = slapi_sdn_new_dn_byref(td->dn);
4f630f
     if (usetxn) {
4f630f
-        Slapi_DN *sdn = slapi_sdn_new_dn_byref(td->dn);
4f630f
         Slapi_Backend *be = slapi_be_select_exact(sdn);
4f630f
-        slapi_sdn_free(&sdn;;
4f630f
+
4f630f
         if (be) {
4f630f
             fixup_pb = slapi_pblock_new();
4f630f
             slapi_pblock_set(fixup_pb, SLAPI_BACKEND, be);
4f630f
@@ -2894,14 +2909,37 @@ done:
4f630f
                           fixup_progress_count, slapi_current_rel_time_t() - fixup_start_time);
4f630f
     slapi_task_inc_progress(task);
4f630f
 
4f630f
+    /* Cleanup task linked list */
4f630f
+    PR_Lock(fixup_lock);
4f630f
+    mo_fixup_ll *prev = NULL;
4f630f
+    for (mo_fixup_ll *curr = fixup_list; curr; curr = curr->next) {
4f630f
+        mo_fixup_ll *next = curr->next;
4f630f
+        if (slapi_sdn_compare(curr->sdn, sdn) == 0 &&
4f630f
+            strcasecmp(curr->filter_str, td->filter_str) == 0)
4f630f
+        {
4f630f
+            /* free current code */
4f630f
+            slapi_sdn_free(&curr->sdn);
4f630f
+            slapi_ch_free_string(&curr->filter_str);
4f630f
+            slapi_ch_free((void**)&curr);
4f630f
+
4f630f
+            /* update linked list */
4f630f
+            if (prev == NULL) {
4f630f
+                /* first node */
4f630f
+                fixup_list = next;
4f630f
+            } else {
4f630f
+                prev->next = next;
4f630f
+            }
4f630f
+            break;
4f630f
+        }
4f630f
+        prev = curr;
4f630f
+    }
4f630f
+    PR_Unlock(fixup_lock);
4f630f
+    slapi_sdn_free(&sdn;;
4f630f
+
4f630f
     /* this will queue the destruction of the task */
4f630f
     slapi_task_finish(task, rc);
4f630f
     slapi_task_dec_refcount(task);
4f630f
 
4f630f
-    PR_Lock(fixup_lock);
4f630f
-    fixup_running = PR_FALSE;
4f630f
-    PR_Unlock(fixup_lock);
4f630f
-
4f630f
     slapi_log_err(SLAPI_LOG_INFO, MEMBEROF_PLUGIN_SUBSYSTEM,
4f630f
                   "memberof_fixup_task_thread - Memberof task finished (processed %d entries in %ld seconds)\n",
4f630f
                   fixup_progress_count, slapi_current_rel_time_t() - fixup_start_time);
4f630f
@@ -2919,23 +2957,13 @@ memberof_task_add(Slapi_PBlock *pb,
4f630f
     int rv = SLAPI_DSE_CALLBACK_OK;
4f630f
     task_data *mytaskdata = NULL;
4f630f
     Slapi_Task *task = NULL;
4f630f
+    Slapi_DN *sdn = NULL;
4f630f
     char *bind_dn;
4f630f
     const char *filter;
4f630f
     const char *dn = 0;
4f630f
 
4f630f
     *returncode = LDAP_SUCCESS;
4f630f
 
4f630f
-    PR_Lock(fixup_lock);
4f630f
-    if (fixup_running) {
4f630f
-        PR_Unlock(fixup_lock);
4f630f
-        *returncode = LDAP_UNWILLING_TO_PERFORM;
4f630f
-        slapi_log_err(SLAPI_LOG_ERR, MEMBEROF_PLUGIN_SUBSYSTEM,
4f630f
-                "memberof_task_add - there is already a fixup task running\n");
4f630f
-        rv = SLAPI_DSE_CALLBACK_ERROR;
4f630f
-        goto out;
4f630f
-    }
4f630f
-    PR_Unlock(fixup_lock);
4f630f
-
4f630f
     /* get arg(s) */
4f630f
     if ((dn = slapi_entry_attr_get_ref(e, "basedn")) == NULL) {
4f630f
         *returncode = LDAP_OBJECT_CLASS_VIOLATION;
4f630f
@@ -2949,6 +2977,39 @@ memberof_task_add(Slapi_PBlock *pb,
4f630f
         goto out;
4f630f
     }
4f630f
 
4f630f
+    PR_Lock(fixup_lock);
4f630f
+    sdn = slapi_sdn_new_dn_byval(dn);
4f630f
+    if (fixup_list == NULL) {
4f630f
+        fixup_list = (mo_fixup_ll *)slapi_ch_calloc(1, sizeof(mo_fixup_ll));
4f630f
+        fixup_list->sdn = sdn;
4f630f
+        fixup_list->filter_str = slapi_ch_strdup(filter);
4f630f
+    } else {
4f630f
+        for (mo_fixup_ll *fixup_task = fixup_list; fixup_task; fixup_task = fixup_task->next) {
4f630f
+            if (slapi_sdn_compare(sdn, fixup_task->sdn) == 0 &&
4f630f
+                strcasecmp(filter, fixup_task->filter_str) == 0)
4f630f
+            {
4f630f
+                /* Found an identical running task, reject it */
4f630f
+                PR_Unlock(fixup_lock);
4f630f
+                slapi_log_err(SLAPI_LOG_ERR, MEMBEROF_PLUGIN_SUBSYSTEM,
4f630f
+                        "memberof_task_add - there is already an identical fixup task running: base: %s filter: %s\n",
4f630f
+                        slapi_sdn_get_dn(sdn), filter);
4f630f
+                slapi_sdn_free(&sdn;;
4f630f
+                *returncode = LDAP_UNWILLING_TO_PERFORM;
4f630f
+                rv = SLAPI_DSE_CALLBACK_ERROR;
4f630f
+                goto out;
4f630f
+            }
4f630f
+        }
4f630f
+        /* Add the new task DN to the top of the list */
4f630f
+        mo_fixup_ll *head = fixup_list;
4f630f
+        mo_fixup_ll *new_task = (mo_fixup_ll *)slapi_ch_calloc(1, sizeof(mo_fixup_ll));
4f630f
+        new_task->sdn = sdn;
4f630f
+        new_task->filter_str = slapi_ch_strdup(filter);
4f630f
+        new_task->next = head;
4f630f
+        fixup_list = new_task;
4f630f
+    }
4f630f
+    PR_Unlock(fixup_lock);
4f630f
+
4f630f
+
4f630f
     /* setup our task data */
4f630f
     slapi_pblock_get(pb, SLAPI_REQUESTOR_DN, &bind_dn);
4f630f
     mytaskdata = (task_data *)slapi_ch_malloc(sizeof(task_data));
4f630f
-- 
4f630f
2.38.1
4f630f