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
//! Connection credentials.
use super::{Gid, Pid, Uid};
/// Credentials of a peer connection.
#[derive(Debug)]
pub struct Credentials {
unix_user_id: Uid,
unix_primary_group_id: Gid,
#[cfg(target_os = "linux")]
unix_supplementary_group_ids: Vec<Gid>,
process_id: Pid,
#[cfg(target_os = "linux")]
process_fd: std::os::fd::OwnedFd,
}
impl Credentials {
/// Create new credentials for a peer connection.
///
/// # Arguments
/// * `unix_user_id` - The numeric Unix user ID.
/// * `process_id` - The numeric process ID.
/// * `process_fd` (Linux only) - A file descriptor pinning the process.
pub(crate) fn new(
unix_user_id: Uid,
unix_primary_group_id: Gid,
#[cfg(target_os = "linux")] unix_supplementary_group_ids: Vec<Gid>,
process_id: Pid,
#[cfg(target_os = "linux")] process_fd: std::os::fd::OwnedFd,
) -> Self {
Self {
unix_user_id,
unix_primary_group_id,
#[cfg(target_os = "linux")]
unix_supplementary_group_ids,
process_id,
#[cfg(target_os = "linux")]
process_fd,
}
}
/// The numeric Unix user ID, as defined by POSIX.
pub fn unix_user_id(&self) -> Uid {
self.unix_user_id
}
/// The numeric process ID, on platforms that have this concept.
///
/// On Unix, this is the process ID defined by POSIX.
pub fn process_id(&self) -> Pid {
self.process_id
}
/// The numeric Unix group ID, as defined by POSIX.
pub fn unix_primary_group_id(&self) -> Gid {
self.unix_primary_group_id
}
/// The set of numeric supplementary Unix group IDs, as defined by POSIX.
///
/// Currently, this method is only available for Linux targets.
#[cfg(target_os = "linux")]
pub fn unix_supplementary_group_ids(&self) -> &[Gid] {
&self.unix_supplementary_group_ids
}
/// A file descriptor pinning the process, on platforms that have this concept.
///
/// On Linux, the SO_PEERPIDFD socket option is a suitable implementation. This is safer to use
/// to identify a process than the ProcessID, as the latter is subject to re-use attacks, while
/// the FD cannot be recycled. If the original process no longer exists the FD will no longer
/// be resolvable.
#[cfg(target_os = "linux")]
pub fn process_fd(&self) -> std::os::fd::BorrowedFd<'_> {
use std::os::fd::AsFd;
self.process_fd.as_fd()
}
}