Skip to main content

endpoint_sec/event/
event_authorization_judgement.rs

1//! [`EventAuthorizationJudgement`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{es_authorization_result_t, es_authorization_rule_class_t, es_event_authorization_judgement_t};
6
7use crate::{AuditToken, Process};
8
9/// Notification that a process had it's right petition judged
10#[doc(alias = "es_event_authorization_judgement_t")]
11pub struct EventAuthorizationJudgement<'a> {
12    /// The raw reference.
13    pub(crate) raw: &'a es_event_authorization_judgement_t,
14    /// The version of the message.
15    pub(crate) version: u32,
16}
17
18impl<'a> EventAuthorizationJudgement<'a> {
19    /// Process that submitted the petition (XPC caller)
20    #[inline(always)]
21    pub fn instigator(&self) -> Option<Process<'a>> {
22        // Safety: 'a tied to self, object obtained through ES
23        let process = unsafe { self.raw.instigator()? };
24        Some(Process::new(process, self.version))
25    }
26
27    /// Audit token of the process that instigated this event.
28    pub fn instigator_token(&self) -> AuditToken {
29        #[cfg(feature = "macos_15_0_0")]
30        if self.version >= 8 {
31            return AuditToken(self.raw.instigator_token);
32        }
33
34        // On old versions, the process was always non-null, and we can get
35        // its token easily.
36        self.instigator().unwrap().audit_token()
37    }
38
39    /// Process that created the petition
40    #[inline(always)]
41    pub fn petitioner(&self) -> Option<Process<'a>> {
42        Some(Process::new(
43            // Safety: 'a tied to self, object obtained through ES
44            unsafe { self.raw.petitioner.as_ref()? },
45            self.version,
46        ))
47    }
48
49    /// Audit token of the process that created the petition.
50    pub fn petitioner_token(&self) -> AuditToken {
51        #[cfg(feature = "macos_15_0_0")]
52        if self.version >= 8 {
53            return AuditToken(self.raw.petitioner_token);
54        }
55
56        // On old versions, the process was always non-null, and we can get
57        // its token easily.
58        self.petitioner().unwrap().audit_token()
59    }
60
61    /// The overall result of the petition. 0 indicates success.
62    ///
63    /// Possible return codes are defined in Security framework "Authorization/Authorization.h"
64    #[inline(always)]
65    pub fn return_code(&self) -> i32 {
66        self.raw.return_code
67    }
68
69    /// Number of results.
70    #[inline(always)]
71    pub fn result_count(&self) -> usize {
72        self.raw.result_count
73    }
74
75    /// Iterator over the results
76    #[inline(always)]
77    pub fn rights<'event>(&'event self) -> AuthorizationJudgementResults<'event, 'a> {
78        AuthorizationJudgementResults::new(self)
79    }
80}
81
82// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
83unsafe impl Send for EventAuthorizationJudgement<'_> {}
84// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
85unsafe impl Sync for EventAuthorizationJudgement<'_> {}
86
87impl_debug_eq_hash_with_functions!(EventAuthorizationJudgement<'a> with version; instigator, instigator_token, petitioner, petitioner_token, return_code, result_count);
88
89/// Describes, for a single right, the class of that right and if it was granted
90#[doc(alias = "es_authorization_result_t")]
91pub struct AuthorizationResult<'a> {
92    /// The raw reference.
93    pub(crate) raw: &'a es_authorization_result_t,
94}
95
96impl<'a> AuthorizationResult<'a> {
97    /// The name of the right being considered
98    #[inline(always)]
99    pub fn right_name(&self) -> &'a OsStr {
100        // Safety: 'a tied to self, object obtained through ES
101        unsafe { self.raw.right_name.as_os_str() }
102    }
103
104    /// The class of the right being considered
105    ///
106    /// The rule class determines how the operating system determines if it should be granted or not
107    #[inline(always)]
108    pub fn rule_class(&self) -> es_authorization_rule_class_t {
109        self.raw.rule_class
110    }
111
112    /// Indicates if the right was granted or not
113    #[inline(always)]
114    pub fn granted(&self) -> bool {
115        self.raw.granted
116    }
117}
118
119// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
120unsafe impl Send for AuthorizationResult<'_> {}
121
122impl_debug_eq_hash_with_functions!(AuthorizationResult<'a>; right_name, rule_class, granted);
123
124/// Read the `idx` result of `raw`
125///
126/// # Safety
127///
128/// Must be called with a valid event for which `idx` is in range `0..raw.result_count`
129unsafe fn read_nth_result(raw: &es_event_authorization_judgement_t, idx: usize) -> *const es_authorization_result_t {
130    // SAFETY:
131    //  * upheld by the caller for the index;
132    //  * `raw.results` is given to us by ES, so adding to it preserves alignment;
133    unsafe { raw.results.add(idx).cast_const() }
134}
135
136/// See [`super::as_os_str()`] for lifetime and safety docs
137unsafe fn make_result<'a>(result: *const es_authorization_result_t) -> AuthorizationResult<'a> {
138    assert!(!result.is_null());
139    AuthorizationResult {
140        raw: unsafe { &*result },
141    }
142}
143
144make_event_data_iterator!(
145    EventAuthorizationJudgement;
146    /// Iterator over the rights of an [`EventAuthorizationJudgement`]
147    AuthorizationJudgementResults with result_count (usize);
148    AuthorizationResult<'raw>;
149    read_nth_result,
150    make_result,
151);