1use std::path::PathBuf;
31
32#[cfg(target_os = "linux")]
33mod linux;
34#[cfg(target_os = "macos")]
35mod macos;
36#[cfg(windows)]
37mod windows;
38
39#[cfg(test)]
40mod tests;
41
42#[cfg(all(target_os = "macos", feature = "test-support"))]
43pub mod fixture;
44
45#[derive(Clone, Debug, PartialEq, Eq)]
47pub enum ObservedPeer {
48 Unobserved,
50 Observed(PeerObservation),
52}
53
54#[derive(Clone, Debug, PartialEq, Eq)]
56pub struct PeerObservation {
57 pub user: PeerUser,
59 pub pid: u32,
61 pub exe: Option<PathBuf>,
63 pub signing_id: Option<String>,
66}
67
68#[derive(Clone, Debug, PartialEq, Eq)]
70pub enum PeerUser {
71 Uid(u32),
74 Sid(String),
76}
77
78#[derive(Clone)]
81#[non_exhaustive]
82pub struct CapturedPeer {
83 observation: ObservedPeer,
84 #[cfg(target_os = "macos")]
85 token: Option<macos::AuditToken>,
86}
87
88impl std::fmt::Debug for CapturedPeer {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 f.debug_struct("CapturedPeer")
91 .field("observation", &self.observation)
92 .finish_non_exhaustive()
93 }
94}
95
96impl From<ObservedPeer> for CapturedPeer {
97 fn from(observation: ObservedPeer) -> Self {
98 Self {
99 observation,
100 #[cfg(target_os = "macos")]
101 token: None,
102 }
103 }
104}
105
106#[derive(Clone, Debug, PartialEq, Eq)]
109#[non_exhaustive]
110pub enum CodeRequirementOutcome {
111 Satisfied(VerifiedCode),
113 NotSatisfied,
115 Unverifiable(CodeVerificationError),
117}
118
119#[derive(Clone, Debug, PartialEq, Eq)]
121#[non_exhaustive]
122pub struct VerifiedCode {
123 pub short_version: Option<String>,
125}
126
127#[derive(Clone, Copy, Debug, PartialEq, Eq)]
129#[non_exhaustive]
130pub enum CodeVerificationError {
131 UnsupportedPlatform,
132 MissingAuditToken,
133 FrameworkUnavailable,
134 InvalidRequirement,
135 GuestUnavailable,
136 SigningInformationUnavailable,
137 ValidityCheckFailed(i32),
138}
139
140impl CapturedPeer {
141 pub fn observation(&self) -> &ObservedPeer {
142 &self.observation
143 }
144
145 pub fn has_audit_token(&self) -> bool {
147 #[cfg(target_os = "macos")]
148 return self.token.is_some();
149 #[cfg(not(target_os = "macos"))]
150 false
151 }
152
153 #[cfg(target_os = "macos")]
157 pub fn observe_code(&self) -> ObservedPeer {
158 macos::observe_code(self, ForcedFailures::default())
159 }
160
161 pub fn check_code_requirement(&self, requirement: &str) -> CodeRequirementOutcome {
165 #[cfg(target_os = "macos")]
166 return macos::check_requirement(self.token, requirement);
167 #[cfg(not(target_os = "macos"))]
168 {
169 let _ = requirement;
170 CodeRequirementOutcome::Unverifiable(CodeVerificationError::UnsupportedPlatform)
171 }
172 }
173}
174
175#[cfg(unix)]
178pub fn capture_unix_peer(socket: &impl std::os::fd::AsFd) -> CapturedPeer {
179 #[cfg(target_os = "macos")]
180 return macos::capture(socket.as_fd(), ForcedFailures::default());
181 #[cfg(not(target_os = "macos"))]
182 observe_unix_peer(socket).into()
183}
184
185#[cfg(unix)]
188pub fn capture_unix_peer_credentials(socket: &impl std::os::fd::AsFd) -> CapturedPeer {
189 #[cfg(target_os = "macos")]
190 return macos::capture_credentials(socket.as_fd(), ForcedFailures::default());
191 #[cfg(not(target_os = "macos"))]
192 capture_unix_peer(socket)
193}
194
195#[cfg(unix)]
199pub fn capture_unix_peer_with_diagnostic_pid(
200 socket: &impl std::os::fd::AsFd,
201) -> (CapturedPeer, Option<u32>) {
202 #[cfg(target_os = "linux")]
203 {
204 let (peer, diagnostic_pid) = linux::observe_with_pid(
205 socket.as_fd(),
206 ForcedFailures::default(),
207 linux::pidfd_alive,
208 );
209 (peer.into(), diagnostic_pid)
210 }
211 #[cfg(not(target_os = "linux"))]
212 {
213 use std::os::fd::AsRawFd;
214 (
215 capture_unix_peer_credentials(socket),
216 unix_peer_pid(socket.as_fd().as_raw_fd()),
217 )
218 }
219}
220
221#[cfg(unix)]
222pub(crate) fn unix_peer_pid(socket: std::os::fd::RawFd) -> Option<u32> {
223 #[cfg(target_os = "linux")]
224 return linux::peer_pid(socket);
225 #[cfg(target_os = "macos")]
226 return macos::peer_pid(socket);
227 #[cfg(not(any(target_os = "linux", target_os = "macos")))]
228 {
229 let _ = socket;
230 None
231 }
232}
233
234#[derive(Debug, Clone)]
237pub struct DiagnosticProcess {
238 pid: u32,
239 #[cfg(any(target_os = "linux", target_os = "macos"))]
240 start: Option<u64>,
241 #[cfg(windows)]
242 process: Option<std::sync::Arc<std::os::windows::io::OwnedHandle>>,
243}
244
245impl DiagnosticProcess {
246 pub fn capture(pid: u32) -> Self {
249 Self {
250 pid,
251 #[cfg(any(target_os = "linux", target_os = "macos"))]
252 start: crate::start_time(pid),
253 #[cfg(windows)]
254 process: windows::open_diagnostic_process(pid).map(std::sync::Arc::new),
255 }
256 }
257
258 pub fn pid(&self) -> u32 {
259 self.pid
260 }
261
262 pub fn short_name(&self) -> Option<String> {
264 #[cfg(target_os = "linux")]
265 let name = {
266 let bytes = std::fs::read(format!("/proc/{}/comm", self.pid)).ok()?;
267 String::from_utf8_lossy(bytes.strip_suffix(b"\n").unwrap_or(&bytes)).into_owned()
268 };
269 #[cfg(target_os = "macos")]
270 let name = crate::process_tree::program_name(self.pid);
271
272 #[cfg(any(target_os = "linux", target_os = "macos"))]
273 return (!name.is_empty() && self.start? == crate::start_time(self.pid)?).then_some(name);
274 #[cfg(windows)]
275 return windows::process_name(self.process.as_deref()?);
276 #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
277 None
278 }
279}
280
281#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
284pub struct ForcedFailures {
285 pub user: bool,
287 pub exe: bool,
289 pub signing_id: bool,
292 pub peer_pidfd: bool,
295}
296
297#[cfg(unix)]
301pub fn observe_unix_peer(socket: &impl std::os::fd::AsFd) -> ObservedPeer {
302 unix_peer(socket.as_fd(), ForcedFailures::default())
303}
304
305#[cfg(all(unix, feature = "test-support"))]
307pub fn observe_unix_peer_forcing(
308 socket: &impl std::os::fd::AsFd,
309 forced: ForcedFailures,
310) -> ObservedPeer {
311 unix_peer(socket.as_fd(), forced)
312}
313
314#[cfg(unix)]
315fn unix_peer(socket: std::os::fd::BorrowedFd<'_>, forced: ForcedFailures) -> ObservedPeer {
316 #[cfg(target_os = "macos")]
317 return macos::observe(socket, forced);
318 #[cfg(target_os = "linux")]
319 return linux::observe(socket, forced);
320 #[cfg(not(any(target_os = "macos", target_os = "linux")))]
321 {
322 let _ = (socket, forced);
323 ObservedPeer::Unobserved
324 }
325}
326
327#[cfg(windows)]
330pub fn observe_pipe_client(pipe: &impl std::os::windows::io::AsHandle) -> ObservedPeer {
331 windows::observe(pipe.as_handle(), ForcedFailures::default())
332}
333
334#[cfg(windows)]
338pub fn capture_pipe_client_with_diagnostic_pid(
339 pipe: &impl std::os::windows::io::AsRawHandle,
340) -> std::io::Result<(CapturedPeer, u32)> {
341 let pid = crate::named_pipe::local_client_pid(pipe.as_raw_handle())?;
342 Ok((
343 windows::observe_local_process(pid, ForcedFailures::default()).into(),
344 pid,
345 ))
346}
347
348#[cfg(all(windows, feature = "test-support"))]
350pub fn observe_pipe_client_forcing(
351 pipe: &impl std::os::windows::io::AsHandle,
352 forced: ForcedFailures,
353) -> ObservedPeer {
354 windows::observe(pipe.as_handle(), forced)
355}