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

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