andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 months ago
Clone

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

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