Skip to main content

merry_process/
host_resources.rs

1//! Host-client resource declarations and filesystem metadata shared by both sandboxes.
2
3use std::path::{Path, PathBuf};
4
5/// A host filesystem object's relevant client-integration shape.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum HostPathKind {
8    /// A directory.
9    Directory,
10    /// A regular file, suitable for public client data.
11    RegularFile,
12    /// A Unix-domain socket, not a file that merely has a socket-like name.
13    UnixSocket,
14    /// An object not eligible for ordinary client resources.
15    Other,
16}
17
18/// Inspected metadata or equivalent evidence supplied by a host probe.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct HostPathMetadata {
21    kind: HostPathKind,
22    owner_uid: u32,
23    mode: u32,
24}
25
26impl HostPathMetadata {
27    /// Constructs typed host evidence, without granting any resource access.
28    #[must_use]
29    pub const fn new(kind: HostPathKind, owner_uid: u32, mode: u32) -> Self {
30        Self {
31            kind,
32            owner_uid,
33            mode,
34        }
35    }
36
37    /// Reads metadata from the source namespace, following symlinks like bind mounts.
38    #[cfg(unix)]
39    pub fn inspect(path: &Path) -> std::io::Result<Self> {
40        use std::os::unix::fs::{FileTypeExt, MetadataExt};
41        let metadata = std::fs::metadata(path)?;
42        let file_type = metadata.file_type();
43        let kind = if file_type.is_socket() {
44            HostPathKind::UnixSocket
45        } else if file_type.is_file() {
46            HostPathKind::RegularFile
47        } else if file_type.is_dir() {
48            HostPathKind::Directory
49        } else {
50            HostPathKind::Other
51        };
52        Ok(Self::new(kind, metadata.uid(), metadata.mode()))
53    }
54
55    /// Returns the inspected object shape.
56    #[must_use]
57    pub const fn kind(self) -> HostPathKind {
58        self.kind
59    }
60
61    /// Returns the source owner's user ID.
62    #[must_use]
63    pub const fn owner_uid(self) -> u32 {
64        self.owner_uid
65    }
66
67    /// Returns the source mode bits.
68    #[must_use]
69    pub const fn mode(self) -> u32 {
70        self.mode
71    }
72
73    /// Checks the shared native-agent endpoint ownership requirement.
74    #[must_use]
75    pub fn is_owned_socket(self, current_uid: u32) -> bool {
76        self.kind == HostPathKind::UnixSocket && self.owner_uid == current_uid
77    }
78}
79
80/// Conventional public SSH host databases supplied by the SSH-agent integration.
81/// This never includes SSH configuration or private identity files.
82#[must_use]
83pub fn ssh_known_hosts(home: &Path) -> [PathBuf; 2] {
84    [
85        home.join(".ssh/known_hosts"),
86        home.join(".ssh/known_hosts2"),
87    ]
88}