Skip to main content

endpoint_sec/event/
event_od_group_set.rs

1//! [`EventOdGroupSet`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{
6    es_event_od_group_set_t, es_od_member_id_array_t, es_od_member_id_array_t_anon0, es_od_member_type_t,
7    es_string_token_t,
8};
9
10use crate::{AuditToken, Process};
11
12/// Notification that a group had it's members initialised or replaced.
13#[doc(alias = "es_event_od_group_set_t")]
14pub struct EventOdGroupSet<'a> {
15    /// The raw reference.
16    pub(crate) raw: &'a es_event_od_group_set_t,
17    /// The version of the message.
18    pub(crate) version: u32,
19}
20
21impl<'a> EventOdGroupSet<'a> {
22    /// Process that instigated operation (XPC caller).
23    #[inline(always)]
24    pub fn instigator(&self) -> Option<Process<'a>> {
25        // Safety: 'a tied to self, object obtained through ES
26        let process = unsafe { self.raw.instigator()? };
27        Some(Process::new(process, self.version))
28    }
29
30    /// Audit token of the process that instigated this event.
31    pub fn instigator_token(&self) -> AuditToken {
32        #[cfg(feature = "macos_15_0_0")]
33        if self.version >= 8 {
34            return AuditToken(self.raw.instigator_token);
35        }
36
37        // On old versions, the process was always non-null, and we can get
38        // its token easily.
39        self.instigator().unwrap().audit_token()
40    }
41
42    /// Result code for the operation.
43    #[inline(always)]
44    pub fn error_code(&self) -> i32 {
45        self.raw.error_code
46    }
47
48    /// The group to which members were set.
49    #[inline(always)]
50    pub fn group_name(&self) -> &'a OsStr {
51        // Safety: 'a tied to self, object obtained through ES
52        unsafe { self.raw.group_name.as_os_str() }
53    }
54
55    /// Array of new members.
56    #[inline(always)]
57    pub fn members(&self) -> OdMemberIdArray<'a> {
58        OdMemberIdArray {
59            // Safety: 'a tied to self, object obtained through ES
60            raw: unsafe { self.raw.members.as_ref() },
61        }
62    }
63
64    /// OD node being mutated.
65    ///
66    /// Typically one of "/Local/Default", "/LDAPv3/<server>" or "/Active Directory/<domain>".
67    #[inline(always)]
68    pub fn node_name(&self) -> &'a OsStr {
69        // Safety: 'a tied to self, object obtained through ES
70        unsafe { self.raw.node_name.as_os_str() }
71    }
72
73    /// Optional. If node_name is "/Local/Default", this is, the path of the database against which
74    /// OD is authenticating.
75    #[inline(always)]
76    pub fn db_path(&self) -> Option<&'a OsStr> {
77        if self.node_name() == OsStr::new("/Local/Default") {
78            // Safety: 'a tied to self, object obtained through ES
79            Some(unsafe { self.raw.db_path.as_os_str() })
80        } else {
81            None
82        }
83    }
84}
85
86// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
87unsafe impl Send for EventOdGroupSet<'_> {}
88// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
89unsafe impl Sync for EventOdGroupSet<'_> {}
90
91impl_debug_eq_hash_with_functions!(EventOdGroupSet<'a> with version; instigator, instigator_token, error_code, group_name, members, node_name, db_path);
92
93/// An array of group member identities.
94#[doc(alias = "es_od_member_id_array_t")]
95pub struct OdMemberIdArray<'a> {
96    /// The raw reference.
97    pub(crate) raw: &'a es_od_member_id_array_t,
98}
99
100impl<'a> OdMemberIdArray<'a> {
101    /// Indicates the type of the members, and how they are identified.
102    #[inline(always)]
103    pub fn member_type(&self) -> es_od_member_type_t {
104        self.raw.member_type
105    }
106
107    /// The number of elements.
108    #[inline(always)]
109    pub fn member_count(&self) -> usize {
110        self.raw.member_count
111    }
112
113    /// The members identity, as its raw value.
114    #[inline(always)]
115    pub fn raw_member_array(&self) -> &'a es_od_member_id_array_t_anon0 {
116        &self.raw.member_array
117    }
118
119    /// Iterator over the relevant union value based on the member type.
120    #[inline(always)]
121    pub fn members<'arr>(&'arr self) -> Option<OdMemberIdArrayIters<'arr, 'a>> {
122        let res = match self.member_type() {
123            es_od_member_type_t::ES_OD_MEMBER_TYPE_USER_NAME => {
124                OdMemberIdArrayIters::UserName(OdMemberIdArrayNames::new(self))
125            },
126            es_od_member_type_t::ES_OD_MEMBER_TYPE_USER_UUID => {
127                OdMemberIdArrayIters::UserUuid(OdMemberIdArrayUuids::new(self))
128            },
129            es_od_member_type_t::ES_OD_MEMBER_TYPE_GROUP_UUID => {
130                OdMemberIdArrayIters::GroupUuid(OdMemberIdArrayUuids::new(self))
131            },
132            _ => return None,
133        };
134        Some(res)
135    }
136}
137
138// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
139unsafe impl Send for OdMemberIdArray<'_> {}
140
141impl_debug_eq_hash_with_functions!(OdMemberIdArray<'a>; member_type, member_count);
142
143/// One of the possible iterator for [`OdMemberIdArray`]
144#[non_exhaustive]
145pub enum OdMemberIdArrayIters<'arr, 'raw> {
146    /// Users, designated by name
147    UserName(OdMemberIdArrayNames<'arr, 'raw>),
148    /// Users, designated by UUID
149    UserUuid(OdMemberIdArrayUuids<'arr, 'raw>),
150    /// Groups, designated by UUID
151    GroupUuid(OdMemberIdArrayUuids<'arr, 'raw>),
152}
153
154/// Read the `idx` name of `raw`
155///
156/// # Safety
157///
158/// Must be called with a valid member array for which `idx` is in range `0..raw.member_count` and the
159/// member type is correct.
160unsafe fn read_nth_name(raw: &es_od_member_id_array_t, idx: usize) -> es_string_token_t {
161    // SAFETY:
162    //  * upheld by the caller for the index;
163    //  * also for the union access;
164    //  * `raw.member_array.names` is given to us by ES, so adding to it preserves alignment;
165    unsafe { std::ptr::read(raw.member_array.names.as_ptr().add(idx)) }
166}
167
168make_event_data_iterator!(
169    OdMemberIdArray;
170    /// Iterator over the names in an [`OdMemberIdArray`]
171    OdMemberIdArrayNames with member_count (usize);
172    &'raw OsStr;
173    read_nth_name,
174    super::as_os_str,
175);
176
177/// Read the `idx` uuid of `raw`
178///
179/// # Safety
180///
181/// Must be called with a valid member array for which `idx` is in range `0..raw.member_count` and the
182/// member type is correct.
183unsafe fn read_nth_uuid(raw: &es_od_member_id_array_t, idx: usize) -> libc::uuid_t {
184    // SAFETY:
185    //  * upheld by the caller for the index;
186    //  * also for the union access;
187    //  * `raw.member_array.uuids` is given to us by ES, so adding to it preserves alignment;
188    unsafe { std::ptr::read(raw.member_array.uuids.as_ptr().add(idx)) }
189}
190
191make_event_data_iterator!(
192    OdMemberIdArray;
193    /// Iterator over the uuids in an [`OdMemberIdArray`]
194    OdMemberIdArrayUuids with member_count (usize);
195    libc::uuid_t;
196    read_nth_uuid,
197    std::convert::identity,
198);