Skip to main content

endpoint_sec/event/
event_authorization_petition.rs

1//! [`EventAuthorizationPetition`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{es_event_authorization_petition_t, es_string_token_t};
6
7use crate::Process;
8
9/// Notification that a process petitioned for certain authorization rights
10#[doc(alias = "es_event_authorization_petition_t")]
11pub struct EventAuthorizationPetition<'a> {
12    /// The raw reference.
13    pub(crate) raw: &'a es_event_authorization_petition_t,
14    /// The version of the message.
15    pub(crate) version: u32,
16}
17
18impl<'a> EventAuthorizationPetition<'a> {
19    /// Process that submitted the petition (XPC caller)
20    #[inline(always)]
21    pub fn instigator(&self) -> Process<'a> {
22        // Safety: 'a tied to self, object obtained through ES
23        Process::new(unsafe { self.raw.instigator.as_ref() }, self.version)
24    }
25
26    /// Process that created the petition
27    #[inline(always)]
28    pub fn petitioner(&self) -> Option<Process<'a>> {
29        Some(Process::new(
30            // Safety: 'a tied to self, object obtained through ES
31            unsafe { self.raw.petitioner.as_ref()? },
32            self.version,
33        ))
34    }
35
36    /// Flags associated with the petition. Defined in Security framework "Authorization/Authorization.h"
37    #[inline(always)]
38    pub fn flags(&self) -> u32 {
39        self.raw.flags
40    }
41
42    /// Number of rights being requested.
43    #[inline(always)]
44    pub fn right_count(&self) -> usize {
45        self.raw.right_count
46    }
47
48    /// Iterator over the rights
49    #[inline(always)]
50    pub fn rights<'event>(&'event self) -> AuthorizationPetitionRights<'event, 'a> {
51        AuthorizationPetitionRights::new(self)
52    }
53}
54
55// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
56unsafe impl Send for EventAuthorizationPetition<'_> {}
57// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
58unsafe impl Sync for EventAuthorizationPetition<'_> {}
59
60impl_debug_eq_hash_with_functions!(EventAuthorizationPetition<'a> with version; instigator, petitioner, flags, right_count);
61
62/// Read the `idx` right of `raw`
63///
64/// # Safety
65///
66/// Must be called with a valid event for which `idx` is in range `0..raw.right_count`
67unsafe fn read_nth_right(raw: &es_event_authorization_petition_t, idx: usize) -> es_string_token_t {
68    std::ptr::read(raw.rights.add(idx))
69}
70
71make_event_data_iterator!(
72    EventAuthorizationPetition;
73    /// Iterator over the rights of an [`EventAuthorizationPetition`]
74    AuthorizationPetitionRights with right_count (usize);
75    &'raw OsStr;
76    read_nth_right,
77    super::as_os_str,
78);