endpoint_sec/event/
event_od_attribute_set.rs

1//! [`EventOdAttributeSet`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{es_event_od_attribute_set_t, es_od_record_type_t, es_string_token_t};
6
7use crate::Process;
8
9/// Notification that an attribute is being set.
10///
11/// Attributes conceptually have the type `Map String (Set String)`.
12/// Each OD record has a Map of attribute name to Set of attribute value.
13/// When an attribute value is added, it is inserted into the set of values for that name.
14///
15/// The new set of attribute values may be empty.
16#[doc(alias = "es_event_od_attribute_set_t")]
17pub struct EventOdAttributeSet<'a> {
18    /// The raw reference.
19    pub(crate) raw: &'a es_event_od_attribute_set_t,
20    /// The version of the message.
21    pub(crate) version: u32,
22}
23
24impl<'a> EventOdAttributeSet<'a> {
25    /// Process that instigated operation (XPC caller).
26    #[inline(always)]
27    pub fn instigator(&self) -> Process<'a> {
28        // Safety: 'a tied to self, object obtained through ES
29        Process::new(unsafe { self.raw.instigator.as_ref() }, self.version)
30    }
31
32    /// Result code for the operation.
33    #[inline(always)]
34    pub fn error_code(&self) -> i32 {
35        self.raw.error_code
36    }
37
38    /// The type of the record for which the attribute is being set.
39    #[inline(always)]
40    pub fn record_type(&self) -> es_od_record_type_t {
41        self.raw.record_type
42    }
43
44    /// The name of the record for which the attribute is being set.
45    #[inline(always)]
46    pub fn record_name(&self) -> &'a OsStr {
47        // Safety: 'a tied to self, object obtained through ES
48        unsafe { self.raw.record_name.as_os_str() }
49    }
50
51    /// The name of the attribute that was set.
52    #[inline(always)]
53    pub fn attribute_name(&self) -> &'a OsStr {
54        // Safety: 'a tied to self, object obtained through ES
55        unsafe { self.raw.attribute_name.as_os_str() }
56    }
57
58    /// The size of attribute_value_array.
59    #[inline(always)]
60    pub fn attribute_value_count(&self) -> usize {
61        self.raw.attribute_value_count
62    }
63
64    /// Iterator over the attribute values that were set.
65    #[inline(always)]
66    pub fn attribute_values<'s>(&'s self) -> AttributeValues<'s, 'a> {
67        AttributeValues::new(self)
68    }
69
70    /// OD node being mutated.
71    ///
72    /// Typically one of "/Local/Default", "/LDAPv3/<server>" or "/Active Directory/<domain>".
73    #[inline(always)]
74    pub fn node_name(&self) -> &'a OsStr {
75        // Safety: 'a tied to self, object obtained through ES
76        unsafe { self.raw.node_name.as_os_str() }
77    }
78
79    /// Optional. If node_name is "/Local/Default", this is, the path of the database against which
80    /// OD is authenticating.
81    #[inline(always)]
82    pub fn db_path(&self) -> Option<&'a OsStr> {
83        if self.node_name() == OsStr::new("/Local/Default") {
84            // Safety: 'a tied to self, object obtained through ES
85            Some(unsafe { self.raw.db_path.as_os_str() })
86        } else {
87            None
88        }
89    }
90}
91
92// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
93unsafe impl Send for EventOdAttributeSet<'_> {}
94// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
95unsafe impl Sync for EventOdAttributeSet<'_> {}
96
97impl_debug_eq_hash_with_functions!(EventOdAttributeSet<'a> with version; instigator, error_code, record_type, record_name, attribute_name, attribute_value_count, node_name, db_path);
98
99/// Read the `idx` attribute value of `raw`
100///
101/// # Safety
102///
103/// Must be called with a valid event for which `idx` is in range `0..raw.attribute_value_count`
104unsafe fn read_nth_attribute_value(raw: &es_event_od_attribute_set_t, idx: usize) -> es_string_token_t {
105    std::ptr::read(raw.attribute_value_array.add(idx))
106}
107
108make_event_data_iterator!(
109    EventOdAttributeSet;
110    /// Iterator over the attribute values of an [`EventOdAttributeSet`]
111    AttributeValues with attribute_value_count (usize);
112    &'raw OsStr;
113    read_nth_attribute_value,
114    super::as_os_str,
115);