Skip to main content

zlink_core/connection/
credentials.rs

1//! Connection credentials.
2
3use super::{Gid, Pid, Uid};
4
5/// Credentials of a peer connection.
6#[derive(Debug)]
7pub struct Credentials {
8    basic: PassedCredentials,
9    #[cfg(target_os = "linux")]
10    unix_supplementary_group_ids: Vec<Gid>,
11    #[cfg(target_os = "linux")]
12    process_fd: Option<std::os::fd::OwnedFd>,
13}
14
15impl Credentials {
16    /// Create new credentials for a peer connection.
17    pub(crate) fn new(
18        basic: PassedCredentials,
19        #[cfg(target_os = "linux")] unix_supplementary_group_ids: Vec<Gid>,
20        #[cfg(target_os = "linux")] process_fd: Option<std::os::fd::OwnedFd>,
21    ) -> Self {
22        Self {
23            basic,
24            #[cfg(target_os = "linux")]
25            unix_supplementary_group_ids,
26            #[cfg(target_os = "linux")]
27            process_fd,
28        }
29    }
30
31    /// The numeric Unix user ID, as defined by POSIX.
32    pub fn unix_user_id(&self) -> Uid {
33        self.basic.unix_user_id
34    }
35
36    /// The numeric process ID, on platforms that have this concept.
37    ///
38    /// On Unix, this is the process ID defined by POSIX.
39    pub fn process_id(&self) -> Pid {
40        self.basic.process_id
41    }
42
43    /// The numeric Unix group ID, as defined by POSIX.
44    pub fn unix_primary_group_id(&self) -> Gid {
45        self.basic.unix_primary_group_id
46    }
47
48    /// The set of numeric supplementary Unix group IDs, as defined by POSIX.
49    ///
50    /// Currently, this method is only available for Linux targets.
51    #[cfg(target_os = "linux")]
52    pub fn unix_supplementary_group_ids(&self) -> &[Gid] {
53        &self.unix_supplementary_group_ids
54    }
55
56    /// A file descriptor pinning the process, on platforms that have this concept.
57    ///
58    /// On Linux, the SO_PEERPIDFD socket option is a suitable implementation. This is safer to use
59    /// to identify a process than the ProcessID, as the latter is subject to re-use attacks, while
60    /// the FD cannot be recycled. If the original process no longer exists the FD will no longer
61    /// be resolvable.
62    /// The SO_PEERPIDFD socket option was added to Linux in version 6.5. This method will return
63    /// None on older kernel versions.
64    #[cfg(target_os = "linux")]
65    pub fn process_fd(&self) -> Option<std::os::fd::BorrowedFd<'_>> {
66        use std::os::fd::AsFd;
67
68        self.process_fd.as_ref().map(|fd| fd.as_fd())
69    }
70}
71
72/// Credentials passed over of socket.
73#[derive(Debug)]
74pub struct PassedCredentials {
75    unix_user_id: Uid,
76    unix_primary_group_id: Gid,
77    process_id: Pid,
78}
79
80impl PassedCredentials {
81    /// Create a new `PassedCredentials` instance.
82    pub fn new(unix_user_id: Uid, unix_primary_group_id: Gid, process_id: Pid) -> Self {
83        Self {
84            unix_user_id,
85            unix_primary_group_id,
86            process_id,
87        }
88    }
89
90    /// The numeric Unix user ID, as defined by POSIX.
91    pub fn unix_user_id(&self) -> Uid {
92        self.unix_user_id
93    }
94
95    /// The numeric process ID, on platforms that have this concept.
96    ///
97    /// On Unix, this is the process ID defined by POSIX.
98    pub fn process_id(&self) -> Pid {
99        self.process_id
100    }
101
102    /// The numeric Unix group ID, as defined by POSIX.
103    pub fn unix_primary_group_id(&self) -> Gid {
104        self.unix_primary_group_id
105    }
106}