endpoint_sec/event/
event_openssh_login.rs

1//! [`EventOpensshLogin`]
2
3use std::ffi::OsStr;
4use std::net::IpAddr;
5use std::str::FromStr;
6
7use endpoint_sec_sys::{es_address_type_t, es_event_openssh_login_t, es_openssh_login_result_type_t, uid_t};
8
9/// OpenSSH login event.
10#[doc(alias = "es_event_openssh_login_t")]
11pub struct EventOpensshLogin<'a> {
12    /// Raw event
13    pub(crate) raw: &'a es_event_openssh_login_t,
14}
15
16impl<'a> EventOpensshLogin<'a> {
17    /// True iff login was successful.
18    #[inline(always)]
19    pub fn success(&self) -> bool {
20        self.raw.success
21    }
22
23    /// Result type for the login attempt.
24    #[inline(always)]
25    pub fn result_type(&self) -> es_openssh_login_result_type_t {
26        self.raw.result_type
27    }
28
29    /// Type of source address.
30    #[inline(always)]
31    pub fn source_address_type(&self) -> es_address_type_t {
32        self.raw.source_address_type
33    }
34
35    /// Source address of connection.
36    #[inline(always)]
37    pub fn source_address(&self) -> &'a OsStr {
38        // Safety: 'a tied to self, object obtained through ES
39        unsafe { self.raw.source_address.as_os_str() }
40    }
41
42    /// Username used for login.
43    #[inline(always)]
44    pub fn username(&self) -> &'a OsStr {
45        // Safety: 'a tied to self, object obtained through ES
46        unsafe { self.raw.username.as_os_str() }
47    }
48
49    /// Describes whether or not the UID of the user logged in is available.
50    #[inline(always)]
51    pub fn has_uid(&self) -> bool {
52        self.raw.has_uid
53    }
54
55    /// UID of user that was logged in.
56    #[inline(always)]
57    pub fn uid(&self) -> Option<uid_t> {
58        // Safety: access is gated on documented conditions
59        #[allow(clippy::unnecessary_lazy_evaluations)]
60        self.has_uid().then(|| unsafe { self.raw.anon0.uid })
61    }
62
63    /// Source address as an [`IpAddr`] from the standard library, if possible.
64    #[inline(always)]
65    pub fn source_address_std(&self) -> Option<IpAddr> {
66        let sa = self.source_address().to_str()?;
67        IpAddr::from_str(sa).ok()
68    }
69}
70
71// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
72unsafe impl Send for EventOpensshLogin<'_> {}
73// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
74unsafe impl Sync for EventOpensshLogin<'_> {}
75
76impl_debug_eq_hash_with_functions!(
77    EventOpensshLogin<'a>;
78    success, result_type, source_address_type, source_address, username, has_uid, uid,
79);