1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! [`EventAuthentication`]

use std::ffi::OsStr;

use endpoint_sec_sys::{
    es_authentication_type_t, es_auto_unlock_type_t, es_event_authentication_auto_unlock_t,
    es_event_authentication_od_t, es_event_authentication_t, es_event_authentication_t_anon0,
    es_event_authentication_token_t, es_event_authentication_touchid_t, es_touchid_mode_t, uid_t,
};

use crate::Process;

/// An authentication was performed.
#[doc(alias = "es_event_authentication_t")]
pub struct EventAuthentication<'a> {
    /// Raw event
    pub(crate) raw: &'a es_event_authentication_t,
    /// Message version
    pub(crate) version: u32,
}

impl<'a> EventAuthentication<'a> {
    /// True iff authentication was successful.
    #[inline(always)]
    pub fn success(&self) -> bool {
        self.raw.success
    }

    /// The type of authentication.
    #[inline(always)]
    pub fn type_(&self) -> es_authentication_type_t {
        self.raw.type_
    }

    /// Type-specific data describing the authentication.
    #[inline(always)]
    pub fn raw_data(&self) -> &'a es_event_authentication_t_anon0 {
        &self.raw.data
    }

    /// Details about event
    #[inline(always)]
    pub fn data(&self) -> Option<AuthenticationData<'a>> {
        let res = match self.type_() {
            es_authentication_type_t::ES_AUTHENTICATION_TYPE_OD => AuthenticationData::Od(EventAuthenticationOd {
                // Safety: access to union is gated on relevant enum
                raw: unsafe { self.raw_data().od.as_opt()? },
                version: self.version,
            }),
            es_authentication_type_t::ES_AUTHENTICATION_TYPE_TOUCHID => {
                AuthenticationData::TouchId(EventAuthenticationTouchId {
                    // Safety: access to union is gated on relevant enum
                    raw: unsafe { self.raw_data().touchid.as_opt()? },
                    version: self.version,
                    success: self.success(),
                })
            },
            es_authentication_type_t::ES_AUTHENTICATION_TYPE_TOKEN => {
                AuthenticationData::Token(EventAuthenticationToken {
                    // Safety: access to union is gated on relevant enum
                    raw: unsafe { self.raw_data().token.as_opt()? },
                    version: self.version,
                })
            },
            es_authentication_type_t::ES_AUTHENTICATION_TYPE_AUTO_UNLOCK => {
                AuthenticationData::AutoUnlock(EventAuthenticationAutoUnlock {
                    // Safety: access to union is gated on relevant enum
                    raw: unsafe { self.raw_data().auto_unlock.as_opt()? },
                })
            },
            _ => return None,
        };
        Some(res)
    }
}

// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for EventAuthentication<'_> {}

impl_debug_eq_hash_with_functions!(EventAuthentication<'a>; success, type_, data);

/// See [`es_event_authentication_t_anon0`]
#[doc(alias = "es_event_authentication_t_anon0")]
#[doc(alias = "es_authentication_type_t")]
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum AuthenticationData<'a> {
    /// Wrapped [`es_event_authentication_t_anon_0.od`]
    Od(EventAuthenticationOd<'a>),
    /// Wrapped [`es_event_authentication_t_anon_0.touchid`]
    TouchId(EventAuthenticationTouchId<'a>),
    /// Wrapped [`es_event_authentication_t_anon_0.token`]
    Token(EventAuthenticationToken<'a>),
    /// Wrapped [`es_event_authentication_t_anon_0.auto_unlock`]
    AutoUnlock(EventAuthenticationAutoUnlock<'a>),
}

/// OpenDirectory authentication data
#[doc(alias = "es_event_authentication_od_t")]
pub struct EventAuthenticationOd<'a> {
    /// Raw event
    raw: &'a es_event_authentication_od_t,
    /// Message version
    version: u32,
}

impl<'a> EventAuthenticationOd<'a> {
    /// Process that instigated the authentication (XPC caller that asked for authentication).
    #[inline(always)]
    pub fn instigator(&self) -> Process<'a> {
        Process::new(
            // Safety: 'a tied to self, object obtained through ES
            unsafe { self.raw.instigator.as_ref() },
            self.version,
        )
    }

    /// OD record type against which OD is authenticating. Typically `Users`, but other record types
    /// can auth too.
    #[inline(always)]
    pub fn record_type(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.record_type.as_os_str() }
    }

    /// OD record name against which OD is authenticating. For record type `Users`, this is the
    /// username.
    #[inline(always)]
    pub fn record_name(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.record_name.as_os_str() }
    }

    /// OD node against which OD is authenticating. Typically one of `/Local/Default`, `/LDAPv3/
    /// <server>` or `/Active Directory/<domain>`.
    #[inline(always)]
    pub fn node_name(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.node_name.as_os_str() }
    }

    /// Optional. If node_name is "/Local/Default", this is the path of the database against which
    /// OD is authenticating.
    #[inline(always)]
    pub fn db_path(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.db_path.as_os_str() }
    }
}

// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for EventAuthenticationOd<'_> {}

impl_debug_eq_hash_with_functions!(EventAuthenticationOd<'a>; instigator, record_name, node_name, db_path);

/// TouchID authentication data
#[doc(alias = "es_event_authentication_touchid_t")]
pub struct EventAuthenticationTouchId<'a> {
    /// Raw event
    raw: &'a es_event_authentication_touchid_t,
    /// Message version
    version: u32,
    /// Overall identification success
    success: bool,
}

impl<'a> EventAuthenticationTouchId<'a> {
    /// Process that instigated the authentication (XPC caller that asked for authentication).
    #[inline(always)]
    pub fn instigator(&self) -> Process<'a> {
        Process::new(
            // Safety: 'a tied to self, object obtained through ES
            unsafe { self.raw.instigator.as_ref() },
            self.version,
        )
    }

    /// TouchID authentication type
    #[inline(always)]
    pub fn touchid_mode(&self) -> es_touchid_mode_t {
        self.raw.touchid_mode
    }

    /// Describes whether or not the uid of the user authenticated is available
    #[inline(always)]
    pub fn has_uid(&self) -> bool {
        self.raw.has_uid
    }

    /// UID of user that was authenticated.
    #[inline(always)]
    pub fn uid(&self) -> Option<uid_t> {
        match (self.has_uid(), self.success, self.touchid_mode()) {
            // Safety: access is gated on documented conditions
            (true, true, es_touchid_mode_t::ES_TOUCHID_MODE_VERIFICATION) => unsafe { Some(self.raw.anon0.uid) },
            _ => None,
        }
    }
}

// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for EventAuthenticationTouchId<'_> {}

impl_debug_eq_hash_with_functions!(EventAuthenticationTouchId<'a>; instigator, touchid_mode, has_uid, uid);

/// Token authentication data
#[doc(alias = "es_event_authentication_token_t")]
pub struct EventAuthenticationToken<'a> {
    /// Raw event
    raw: &'a es_event_authentication_token_t,
    /// Message version
    version: u32,
}

impl<'a> EventAuthenticationToken<'a> {
    /// Process that instigated the authentication (XPC caller that asked for authentication).
    #[inline(always)]
    pub fn instigator(&self) -> Process<'a> {
        Process::new(
            // Safety: 'a tied to self, object obtained through ES
            unsafe { self.raw.instigator.as_ref() },
            self.version,
        )
    }

    /// Hash of the public key which CryptoTokenKit is authenticating.
    #[inline(always)]
    pub fn pubkey_hash(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.pubkey_hash.as_os_str() }
    }

    /// Token identifier of the event which CryptoTokenKit is authenticating.
    #[inline(always)]
    pub fn token_id(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.token_id.as_os_str() }
    }

    /// Optional. This will be available if token is used for GSS PKINIT authentication for
    /// obtaining a kerberos TGT. `NULL` in all other cases.
    #[inline(always)]
    pub fn kerberos_principal(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.kerberos_principal.as_os_str() }
    }
}

// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for EventAuthenticationToken<'_> {}

impl_debug_eq_hash_with_functions!(EventAuthenticationToken<'a>; instigator, pubkey_hash, token_id, kerberos_principal);

/// Auto unlock authentication data
#[doc(alias = "es_event_authentication_auto_unlock_t")]
pub struct EventAuthenticationAutoUnlock<'a> {
    /// Raw event
    raw: &'a es_event_authentication_auto_unlock_t,
}

impl<'a> EventAuthenticationAutoUnlock<'a> {
    /// Username for which the authentication was attempted.
    #[inline(always)]
    pub fn username(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.username.as_os_str() }
    }

    /// Purpose of the authentication.
    #[inline(always)]
    pub fn type_(&self) -> es_auto_unlock_type_t {
        self.raw.type_
    }
}

// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for EventAuthenticationAutoUnlock<'_> {}

impl_debug_eq_hash_with_functions!(EventAuthenticationAutoUnlock<'a>; username, type_);