Blame SOURCES/0004-Ticket-4326-entryuuid-fixup-did-not-work-correctly-4.patch

6d0b66
From c167d6127db45d8426437c273060c8c8f7fbcb9b Mon Sep 17 00:00:00 2001
6d0b66
From: Firstyear <william.brown@suse.com>
6d0b66
Date: Wed, 23 Sep 2020 09:19:34 +1000
6d0b66
Subject: [PATCH 04/12] Ticket 4326 - entryuuid fixup did not work correctly
6d0b66
 (#4328)
6d0b66
6d0b66
Bug Description: due to an oversight in how fixup tasks
6d0b66
worked, the entryuuid fixup task did not work correctly and
6d0b66
would not persist over restarts.
6d0b66
6d0b66
Fix Description: Correctly implement entryuuid fixup.
6d0b66
6d0b66
fixes: #4326
6d0b66
6d0b66
Author: William Brown <william@blackhats.net.au>
6d0b66
6d0b66
Review by: mreynolds (thanks!)
6d0b66
---
6d0b66
 .../tests/suites/entryuuid/basic_test.py      |  24 +++-
6d0b66
 src/plugins/entryuuid/src/lib.rs              |  43 ++++++-
6d0b66
 src/slapi_r_plugin/src/constants.rs           |   5 +
6d0b66
 src/slapi_r_plugin/src/entry.rs               |   8 ++
6d0b66
 src/slapi_r_plugin/src/lib.rs                 |   2 +
6d0b66
 src/slapi_r_plugin/src/macros.rs              |   2 +-
6d0b66
 src/slapi_r_plugin/src/modify.rs              | 118 ++++++++++++++++++
6d0b66
 src/slapi_r_plugin/src/pblock.rs              |   7 ++
6d0b66
 src/slapi_r_plugin/src/value.rs               |   4 +
6d0b66
 9 files changed, 206 insertions(+), 7 deletions(-)
6d0b66
 create mode 100644 src/slapi_r_plugin/src/modify.rs
6d0b66
6d0b66
diff --git a/dirsrvtests/tests/suites/entryuuid/basic_test.py b/dirsrvtests/tests/suites/entryuuid/basic_test.py
6d0b66
index beb73701d..4d8a40909 100644
6d0b66
--- a/dirsrvtests/tests/suites/entryuuid/basic_test.py
6d0b66
+++ b/dirsrvtests/tests/suites/entryuuid/basic_test.py
6d0b66
@@ -12,6 +12,7 @@ import time
6d0b66
 import shutil
6d0b66
 from lib389.idm.user import nsUserAccounts, UserAccounts
6d0b66
 from lib389.idm.account import Accounts
6d0b66
+from lib389.idm.domain import Domain
6d0b66
 from lib389.topologies import topology_st as topology
6d0b66
 from lib389.backend import Backends
6d0b66
 from lib389.paths import Paths
6d0b66
@@ -190,6 +191,7 @@ def test_entryuuid_fixup_task(topology):
6d0b66
         3. Enable the entryuuid plugin
6d0b66
         4. Run the fixup
6d0b66
         5. Assert the entryuuid now exists
6d0b66
+        6. Restart and check they persist
6d0b66
 
6d0b66
     :expectedresults:
6d0b66
         1. Success
6d0b66
@@ -197,6 +199,7 @@ def test_entryuuid_fixup_task(topology):
6d0b66
         3. Success
6d0b66
         4. Success
6d0b66
         5. Suddenly EntryUUID!
6d0b66
+        6. Still has EntryUUID!
6d0b66
     """
6d0b66
     # 1. Disable the plugin
6d0b66
     plug = EntryUUIDPlugin(topology.standalone)
6d0b66
@@ -220,7 +223,22 @@ def test_entryuuid_fixup_task(topology):
6d0b66
     assert(task.is_complete() and task.get_exit_code() == 0)
6d0b66
     topology.standalone.config.loglevel(vals=(ErrorLog.DEFAULT,))
6d0b66
 
6d0b66
-    # 5. Assert the uuid.
6d0b66
-    euuid = account.get_attr_val_utf8('entryUUID')
6d0b66
-    assert(euuid is not None)
6d0b66
+    # 5.1 Assert the uuid on the user.
6d0b66
+    euuid_user = account.get_attr_val_utf8('entryUUID')
6d0b66
+    assert(euuid_user is not None)
6d0b66
+
6d0b66
+    # 5.2 Assert it on the domain entry.
6d0b66
+    domain = Domain(topology.standalone, dn=DEFAULT_SUFFIX)
6d0b66
+    euuid_domain = domain.get_attr_val_utf8('entryUUID')
6d0b66
+    assert(euuid_domain is not None)
6d0b66
+
6d0b66
+    # Assert it persists after a restart.
6d0b66
+    topology.standalone.restart()
6d0b66
+    # 6.1 Assert the uuid on the use.
6d0b66
+    euuid_user_2 = account.get_attr_val_utf8('entryUUID')
6d0b66
+    assert(euuid_user_2 == euuid_user)
6d0b66
+
6d0b66
+    # 6.2 Assert it on the domain entry.
6d0b66
+    euuid_domain_2 = domain.get_attr_val_utf8('entryUUID')
6d0b66
+    assert(euuid_domain_2 == euuid_domain)
6d0b66
 
6d0b66
diff --git a/src/plugins/entryuuid/src/lib.rs b/src/plugins/entryuuid/src/lib.rs
6d0b66
index 6b5e8d1bb..92977db05 100644
6d0b66
--- a/src/plugins/entryuuid/src/lib.rs
6d0b66
+++ b/src/plugins/entryuuid/src/lib.rs
6d0b66
@@ -187,9 +187,46 @@ impl SlapiPlugin3 for EntryUuid {
6d0b66
     }
6d0b66
 }
6d0b66
 
6d0b66
-pub fn entryuuid_fixup_mapfn(mut e: EntryRef, _data: &()) -> Result<(), PluginError> {
6d0b66
-    assign_uuid(&mut e);
6d0b66
-    Ok(())
6d0b66
+pub fn entryuuid_fixup_mapfn(e: &EntryRef, _data: &()) -> Result<(), PluginError> {
6d0b66
+    /* Supply a modification to the entry. */
6d0b66
+    let sdn = e.get_sdnref();
6d0b66
+
6d0b66
+    /* Sanity check that entryuuid doesn't already exist */
6d0b66
+    if e.contains_attr("entryUUID") {
6d0b66
+        log_error!(
6d0b66
+            ErrorLevel::Trace,
6d0b66
+            "skipping fixup for -> {}",
6d0b66
+            sdn.to_dn_string()
6d0b66
+        );
6d0b66
+        return Ok(());
6d0b66
+    }
6d0b66
+
6d0b66
+    // Setup the modifications
6d0b66
+    let mut mods = SlapiMods::new();
6d0b66
+
6d0b66
+    let u: Uuid = Uuid::new_v4();
6d0b66
+    let uuid_value = Value::from(&u);
6d0b66
+    let values: ValueArray = std::iter::once(uuid_value).collect();
6d0b66
+    mods.append(ModType::Replace, "entryUUID", values);
6d0b66
+
6d0b66
+    /* */
6d0b66
+    let lmod = Modify::new(&sdn, mods, plugin_id())?;
6d0b66
+
6d0b66
+    match lmod.execute() {
6d0b66
+        Ok(_) => {
6d0b66
+            log_error!(ErrorLevel::Trace, "fixed-up -> {}", sdn.to_dn_string());
6d0b66
+            Ok(())
6d0b66
+        }
6d0b66
+        Err(e) => {
6d0b66
+            log_error!(
6d0b66
+                ErrorLevel::Error,
6d0b66
+                "entryuuid_fixup_mapfn -> fixup failed -> {} {:?}",
6d0b66
+                sdn.to_dn_string(),
6d0b66
+                e
6d0b66
+            );
6d0b66
+            Err(PluginError::GenericFailure)
6d0b66
+        }
6d0b66
+    }
6d0b66
 }
6d0b66
 
6d0b66
 #[cfg(test)]
6d0b66
diff --git a/src/slapi_r_plugin/src/constants.rs b/src/slapi_r_plugin/src/constants.rs
6d0b66
index cf76ccbdb..34845c2f4 100644
6d0b66
--- a/src/slapi_r_plugin/src/constants.rs
6d0b66
+++ b/src/slapi_r_plugin/src/constants.rs
6d0b66
@@ -5,6 +5,11 @@ use std::os::raw::c_char;
6d0b66
 pub const LDAP_SUCCESS: i32 = 0;
6d0b66
 pub const PLUGIN_DEFAULT_PRECEDENCE: i32 = 50;
6d0b66
 
6d0b66
+#[repr(i32)]
6d0b66
+pub enum OpFlags {
6d0b66
+    ByassReferrals = 0x0040_0000,
6d0b66
+}
6d0b66
+
6d0b66
 #[repr(i32)]
6d0b66
 /// The set of possible function handles we can register via the pblock. These
6d0b66
 /// values correspond to slapi-plugin.h.
6d0b66
diff --git a/src/slapi_r_plugin/src/entry.rs b/src/slapi_r_plugin/src/entry.rs
6d0b66
index 034efe692..22ae45189 100644
6d0b66
--- a/src/slapi_r_plugin/src/entry.rs
6d0b66
+++ b/src/slapi_r_plugin/src/entry.rs
6d0b66
@@ -70,6 +70,14 @@ impl EntryRef {
6d0b66
         }
6d0b66
     }
6d0b66
 
6d0b66
+    pub fn contains_attr(&self, name: &str) -> bool {
6d0b66
+        let cname = CString::new(name).expect("invalid attr name");
6d0b66
+        let va = unsafe { slapi_entry_attr_get_valuearray(self.raw_e, cname.as_ptr()) };
6d0b66
+
6d0b66
+        // If it's null, it's not present, so flip the logic.
6d0b66
+        !va.is_null()
6d0b66
+    }
6d0b66
+
6d0b66
     pub fn add_value(&mut self, a: &str, v: &ValueRef) {
6d0b66
         // turn the attr to a c string.
6d0b66
         // TODO FIX
6d0b66
diff --git a/src/slapi_r_plugin/src/lib.rs b/src/slapi_r_plugin/src/lib.rs
6d0b66
index d7fc22e52..076907bae 100644
6d0b66
--- a/src/slapi_r_plugin/src/lib.rs
6d0b66
+++ b/src/slapi_r_plugin/src/lib.rs
6d0b66
@@ -9,6 +9,7 @@ pub mod dn;
6d0b66
 pub mod entry;
6d0b66
 pub mod error;
6d0b66
 pub mod log;
6d0b66
+pub mod modify;
6d0b66
 pub mod pblock;
6d0b66
 pub mod plugin;
6d0b66
 pub mod search;
6d0b66
@@ -24,6 +25,7 @@ pub mod prelude {
6d0b66
     pub use crate::entry::EntryRef;
6d0b66
     pub use crate::error::{DseCallbackStatus, LDAPError, PluginError, RPluginError};
6d0b66
     pub use crate::log::{log_error, ErrorLevel};
6d0b66
+    pub use crate::modify::{ModType, Modify, SlapiMods};
6d0b66
     pub use crate::pblock::{Pblock, PblockRef};
6d0b66
     pub use crate::plugin::{register_plugin_ext, PluginIdRef, SlapiPlugin3};
6d0b66
     pub use crate::search::{Search, SearchScope};
6d0b66
diff --git a/src/slapi_r_plugin/src/macros.rs b/src/slapi_r_plugin/src/macros.rs
6d0b66
index 030449632..bc8dfa60f 100644
6d0b66
--- a/src/slapi_r_plugin/src/macros.rs
6d0b66
+++ b/src/slapi_r_plugin/src/macros.rs
6d0b66
@@ -825,7 +825,7 @@ macro_rules! slapi_r_search_callback_mapfn {
6d0b66
                 let e = EntryRef::new(raw_e);
6d0b66
                 let data_ptr = raw_data as *const _;
6d0b66
                 let data = unsafe { &(*data_ptr) };
6d0b66
-                match $cb_mod_ident(e, data) {
6d0b66
+                match $cb_mod_ident(&e, data) {
6d0b66
                     Ok(_) => LDAPError::Success as i32,
6d0b66
                     Err(e) => e as i32,
6d0b66
                 }
6d0b66
diff --git a/src/slapi_r_plugin/src/modify.rs b/src/slapi_r_plugin/src/modify.rs
6d0b66
new file mode 100644
6d0b66
index 000000000..30864377a
6d0b66
--- /dev/null
6d0b66
+++ b/src/slapi_r_plugin/src/modify.rs
6d0b66
@@ -0,0 +1,118 @@
6d0b66
+use crate::constants::OpFlags;
6d0b66
+use crate::dn::SdnRef;
6d0b66
+use crate::error::{LDAPError, PluginError};
6d0b66
+use crate::pblock::Pblock;
6d0b66
+use crate::plugin::PluginIdRef;
6d0b66
+use crate::value::{slapi_value, ValueArray};
6d0b66
+
6d0b66
+use std::ffi::CString;
6d0b66
+use std::ops::{Deref, DerefMut};
6d0b66
+use std::os::raw::c_char;
6d0b66
+
6d0b66
+extern "C" {
6d0b66
+    fn slapi_modify_internal_set_pb_ext(
6d0b66
+        pb: *const libc::c_void,
6d0b66
+        dn: *const libc::c_void,
6d0b66
+        mods: *const *const libc::c_void,
6d0b66
+        controls: *const *const libc::c_void,
6d0b66
+        uniqueid: *const c_char,
6d0b66
+        plugin_ident: *const libc::c_void,
6d0b66
+        op_flags: i32,
6d0b66
+    );
6d0b66
+    fn slapi_modify_internal_pb(pb: *const libc::c_void);
6d0b66
+    fn slapi_mods_free(smods: *const *const libc::c_void);
6d0b66
+    fn slapi_mods_get_ldapmods_byref(smods: *const libc::c_void) -> *const *const libc::c_void;
6d0b66
+    fn slapi_mods_new() -> *const libc::c_void;
6d0b66
+    fn slapi_mods_add_mod_values(
6d0b66
+        smods: *const libc::c_void,
6d0b66
+        mtype: i32,
6d0b66
+        attrtype: *const c_char,
6d0b66
+        value: *const *const slapi_value,
6d0b66
+    );
6d0b66
+}
6d0b66
+
6d0b66
+#[derive(Debug)]
6d0b66
+#[repr(i32)]
6d0b66
+pub enum ModType {
6d0b66
+    Add = 0,
6d0b66
+    Delete = 1,
6d0b66
+    Replace = 2,
6d0b66
+}
6d0b66
+
6d0b66
+pub struct SlapiMods {
6d0b66
+    inner: *const libc::c_void,
6d0b66
+    vas: Vec<ValueArray>,
6d0b66
+}
6d0b66
+
6d0b66
+impl Drop for SlapiMods {
6d0b66
+    fn drop(&mut self) {
6d0b66
+        unsafe { slapi_mods_free(&self.inner as *const *const libc::c_void) }
6d0b66
+    }
6d0b66
+}
6d0b66
+
6d0b66
+impl SlapiMods {
6d0b66
+    pub fn new() -> Self {
6d0b66
+        SlapiMods {
6d0b66
+            inner: unsafe { slapi_mods_new() },
6d0b66
+            vas: Vec::new(),
6d0b66
+        }
6d0b66
+    }
6d0b66
+
6d0b66
+    pub fn append(&mut self, modtype: ModType, attrtype: &str, values: ValueArray) {
6d0b66
+        // We can get the value array pointer here to push to the inner
6d0b66
+        // because the internal pointers won't change even when we push them
6d0b66
+        // to the list to preserve their lifetime.
6d0b66
+        let vas = values.as_ptr();
6d0b66
+        // We take ownership of this to ensure it lives as least as long as our
6d0b66
+        // slapimods structure.
6d0b66
+        self.vas.push(values);
6d0b66
+        // now we can insert these to the modes.
6d0b66
+        let c_attrtype = CString::new(attrtype).expect("failed to allocate attrtype");
6d0b66
+        unsafe { slapi_mods_add_mod_values(self.inner, modtype as i32, c_attrtype.as_ptr(), vas) };
6d0b66
+    }
6d0b66
+}
6d0b66
+
6d0b66
+pub struct Modify {
6d0b66
+    pb: Pblock,
6d0b66
+    mods: SlapiMods,
6d0b66
+}
6d0b66
+
6d0b66
+pub struct ModifyResult {
6d0b66
+    pb: Pblock,
6d0b66
+}
6d0b66
+
6d0b66
+impl Modify {
6d0b66
+    pub fn new(dn: &SdnRef, mods: SlapiMods, plugin_id: PluginIdRef) -> Result<Self, PluginError> {
6d0b66
+        let pb = Pblock::new();
6d0b66
+        let lmods = unsafe { slapi_mods_get_ldapmods_byref(mods.inner) };
6d0b66
+        // OP_FLAG_ACTION_LOG_ACCESS
6d0b66
+
6d0b66
+        unsafe {
6d0b66
+            slapi_modify_internal_set_pb_ext(
6d0b66
+                pb.deref().as_ptr(),
6d0b66
+                dn.as_ptr(),
6d0b66
+                lmods,
6d0b66
+                std::ptr::null(),
6d0b66
+                std::ptr::null(),
6d0b66
+                plugin_id.raw_pid,
6d0b66
+                OpFlags::ByassReferrals as i32,
6d0b66
+            )
6d0b66
+        };
6d0b66
+
6d0b66
+        Ok(Modify { pb, mods })
6d0b66
+    }
6d0b66
+
6d0b66
+    pub fn execute(self) -> Result<ModifyResult, LDAPError> {
6d0b66
+        let Modify {
6d0b66
+            mut pb,
6d0b66
+            mods: _mods,
6d0b66
+        } = self;
6d0b66
+        unsafe { slapi_modify_internal_pb(pb.deref().as_ptr()) };
6d0b66
+        let result = pb.get_op_result();
6d0b66
+
6d0b66
+        match result {
6d0b66
+            0 => Ok(ModifyResult { pb }),
6d0b66
+            _e => Err(LDAPError::from(result)),
6d0b66
+        }
6d0b66
+    }
6d0b66
+}
6d0b66
diff --git a/src/slapi_r_plugin/src/pblock.rs b/src/slapi_r_plugin/src/pblock.rs
6d0b66
index b69ce1680..0f83914f3 100644
6d0b66
--- a/src/slapi_r_plugin/src/pblock.rs
6d0b66
+++ b/src/slapi_r_plugin/src/pblock.rs
6d0b66
@@ -11,6 +11,7 @@ pub use crate::log::{log_error, ErrorLevel};
6d0b66
 extern "C" {
6d0b66
     fn slapi_pblock_set(pb: *const libc::c_void, arg: i32, value: *const libc::c_void) -> i32;
6d0b66
     fn slapi_pblock_get(pb: *const libc::c_void, arg: i32, value: *const libc::c_void) -> i32;
6d0b66
+    fn slapi_pblock_destroy(pb: *const libc::c_void);
6d0b66
     fn slapi_pblock_new() -> *const libc::c_void;
6d0b66
 }
6d0b66
 
6d0b66
@@ -41,6 +42,12 @@ impl DerefMut for Pblock {
6d0b66
     }
6d0b66
 }
6d0b66
 
6d0b66
+impl Drop for Pblock {
6d0b66
+    fn drop(&mut self) {
6d0b66
+        unsafe { slapi_pblock_destroy(self.value.raw_pb) }
6d0b66
+    }
6d0b66
+}
6d0b66
+
6d0b66
 pub struct PblockRef {
6d0b66
     raw_pb: *const libc::c_void,
6d0b66
 }
6d0b66
diff --git a/src/slapi_r_plugin/src/value.rs b/src/slapi_r_plugin/src/value.rs
6d0b66
index 5a40dd279..46246837a 100644
6d0b66
--- a/src/slapi_r_plugin/src/value.rs
6d0b66
+++ b/src/slapi_r_plugin/src/value.rs
6d0b66
@@ -96,6 +96,10 @@ impl ValueArray {
6d0b66
         let bs = vs.into_boxed_slice();
6d0b66
         Box::leak(bs) as *const _ as *const *const slapi_value
6d0b66
     }
6d0b66
+
6d0b66
+    pub fn as_ptr(&self) -> *const *const slapi_value {
6d0b66
+        self.data.as_ptr() as *const *const slapi_value
6d0b66
+    }
6d0b66
 }
6d0b66
 
6d0b66
 impl FromIterator<Value> for ValueArray {
6d0b66
-- 
6d0b66
2.26.3
6d0b66