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