Skip to main content

endpoint_sec/event/
event_od_group_add.rs

1//! [`EventOdGroupAdd`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{es_event_od_group_add_t, es_od_member_id_t, es_od_member_id_t_anon0, es_od_member_type_t};
6
7use crate::Process;
8
9/// Notification that a member was added to a group.
10///
11/// This event does not indicate that a member was actually added. For example when adding a user
12/// to a group they are already a member of.
13#[doc(alias = "es_event_od_group_add_t")]
14pub struct EventOdGroupAdd<'a> {
15    /// The raw reference.
16    pub(crate) raw: &'a es_event_od_group_add_t,
17    /// The version of the message.
18    pub(crate) version: u32,
19}
20
21impl<'a> EventOdGroupAdd<'a> {
22    /// Process that instigated operation (XPC caller).
23    #[inline(always)]
24    pub fn instigator(&self) -> Process<'a> {
25        // Safety: 'a tied to self, object obtained through ES
26        Process::new(unsafe { self.raw.instigator.as_ref() }, self.version)
27    }
28
29    /// Result code for the operation.
30    #[inline(always)]
31    pub fn error_code(&self) -> i32 {
32        self.raw.error_code
33    }
34
35    /// The group to which the member was added.
36    #[inline(always)]
37    pub fn group_name(&self) -> &'a OsStr {
38        // Safety: 'a tied to self, object obtained through ES
39        unsafe { self.raw.group_name.as_os_str() }
40    }
41
42    /// The identity of the member added.
43    #[inline(always)]
44    pub fn member(&self) -> OdMemberId<'a> {
45        OdMemberId {
46            // Safety: 'a tied to self, object obtained through ES
47            raw: unsafe { self.raw.member.as_ref() },
48        }
49    }
50
51    /// OD node being mutated.
52    ///
53    /// Typically one of "/Local/Default", "/LDAPv3/<server>" or "/Active Directory/<domain>".
54    #[inline(always)]
55    pub fn node_name(&self) -> &'a OsStr {
56        // Safety: 'a tied to self, object obtained through ES
57        unsafe { self.raw.node_name.as_os_str() }
58    }
59
60    /// Optional. If node_name is "/Local/Default", this is, the path of the database against which
61    /// OD is authenticating.
62    #[inline(always)]
63    pub fn db_path(&self) -> Option<&'a OsStr> {
64        if self.node_name() == OsStr::new("/Local/Default") {
65            // Safety: 'a tied to self, object obtained through ES
66            Some(unsafe { self.raw.db_path.as_os_str() })
67        } else {
68            None
69        }
70    }
71}
72
73// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
74unsafe impl Send for EventOdGroupAdd<'_> {}
75// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
76unsafe impl Sync for EventOdGroupAdd<'_> {}
77
78impl_debug_eq_hash_with_functions!(EventOdGroupAdd<'a> with version; instigator, error_code, group_name, member, node_name, db_path);
79
80/// The identity of a group member
81#[doc(alias = "es_od_member_id_t")]
82pub struct OdMemberId<'a> {
83    /// The raw reference.
84    pub(crate) raw: &'a es_od_member_id_t,
85}
86
87impl<'a> OdMemberId<'a> {
88    /// Indicates the type of the member, and how it is identified.
89    #[inline(always)]
90    pub fn member_type(&self) -> es_od_member_type_t {
91        self.raw.member_type
92    }
93
94    /// The member identity, as its raw value.
95    #[inline(always)]
96    pub fn raw_member_value(&self) -> &'a es_od_member_id_t_anon0 {
97        &self.raw.member_value
98    }
99
100    /// The member identity.
101    #[inline(always)]
102    pub fn member_value(&self) -> Option<OdMemberIdValue<'a>> {
103        // Safety in general: we check against the 'member_type' before accessing the union
104        let res = match self.member_type() {
105            es_od_member_type_t::ES_OD_MEMBER_TYPE_USER_UUID => {
106                // Safety: 'a tied to self, object obtained through ES
107                OdMemberIdValue::UserUuid(unsafe { self.raw.member_value.uuid })
108            },
109            es_od_member_type_t::ES_OD_MEMBER_TYPE_GROUP_UUID => {
110                // Safety: 'a tied to self, object obtained through ES
111                OdMemberIdValue::GroupUuid(unsafe { self.raw.member_value.uuid })
112            },
113            es_od_member_type_t::ES_OD_MEMBER_TYPE_USER_NAME => {
114                // Safety: 'a tied to self, object obtained through ES
115                OdMemberIdValue::UserName(unsafe { self.raw.member_value.name.as_os_str() })
116            },
117            _ => return None,
118        };
119
120        Some(res)
121    }
122}
123
124// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
125unsafe impl Send for OdMemberId<'_> {}
126
127impl_debug_eq_hash_with_functions!(OdMemberId<'a>; member_type, member_value);
128
129/// A member identity.
130#[derive(Debug, PartialEq, Eq, Hash)]
131pub enum OdMemberIdValue<'a> {
132    /// Group member is a user, designated by name
133    UserName(&'a OsStr),
134    /// Group member is a user, designated by UUID
135    UserUuid(libc::uuid_t),
136    /// Group member is another group, designated by UUID
137    GroupUuid(libc::uuid_t),
138}
139
140// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
141unsafe impl Send for OdMemberIdValue<'_> {}