use std::path::PathBuf;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(windows)]
mod windows;
#[cfg(test)]
mod tests;
#[cfg(all(target_os = "macos", feature = "test-support"))]
pub mod fixture;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ObservedPeer {
Unobserved,
Observed(PeerObservation),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PeerObservation {
pub user: PeerUser,
pub pid: u32,
pub exe: Option<PathBuf>,
pub signing_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PeerUser {
Uid(u32),
Sid(String),
}
#[derive(Clone)]
#[non_exhaustive]
pub struct CapturedPeer {
observation: ObservedPeer,
#[cfg(target_os = "macos")]
token: Option<macos::AuditToken>,
}
impl std::fmt::Debug for CapturedPeer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CapturedPeer")
.field("observation", &self.observation)
.finish_non_exhaustive()
}
}
impl From<ObservedPeer> for CapturedPeer {
fn from(observation: ObservedPeer) -> Self {
Self {
observation,
#[cfg(target_os = "macos")]
token: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CodeRequirementOutcome {
Satisfied(VerifiedCode),
NotSatisfied,
Unverifiable(CodeVerificationError),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct VerifiedCode {
pub short_version: Option<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CodeVerificationError {
UnsupportedPlatform,
MissingAuditToken,
FrameworkUnavailable,
InvalidRequirement,
GuestUnavailable,
SigningInformationUnavailable,
ValidityCheckFailed(i32),
}
impl CapturedPeer {
pub fn observation(&self) -> &ObservedPeer {
&self.observation
}
pub fn has_audit_token(&self) -> bool {
#[cfg(target_os = "macos")]
return self.token.is_some();
#[cfg(not(target_os = "macos"))]
false
}
#[cfg(target_os = "macos")]
pub fn observe_code(&self) -> ObservedPeer {
macos::observe_code(self, ForcedFailures::default())
}
pub fn check_code_requirement(&self, requirement: &str) -> CodeRequirementOutcome {
#[cfg(target_os = "macos")]
return macos::check_requirement(self.token, requirement);
#[cfg(not(target_os = "macos"))]
{
let _ = requirement;
CodeRequirementOutcome::Unverifiable(CodeVerificationError::UnsupportedPlatform)
}
}
}
#[cfg(unix)]
pub fn capture_unix_peer(socket: &impl std::os::fd::AsFd) -> CapturedPeer {
#[cfg(target_os = "macos")]
return macos::capture(socket.as_fd(), ForcedFailures::default());
#[cfg(not(target_os = "macos"))]
observe_unix_peer(socket).into()
}
#[cfg(unix)]
pub fn capture_unix_peer_credentials(socket: &impl std::os::fd::AsFd) -> CapturedPeer {
#[cfg(target_os = "macos")]
return macos::capture_credentials(socket.as_fd(), ForcedFailures::default());
#[cfg(not(target_os = "macos"))]
capture_unix_peer(socket)
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ForcedFailures {
pub user: bool,
pub exe: bool,
pub signing_id: bool,
pub peer_pidfd: bool,
}
#[cfg(unix)]
pub fn observe_unix_peer(socket: &impl std::os::fd::AsFd) -> ObservedPeer {
unix_peer(socket.as_fd(), ForcedFailures::default())
}
#[cfg(all(unix, feature = "test-support"))]
pub fn observe_unix_peer_forcing(
socket: &impl std::os::fd::AsFd,
forced: ForcedFailures,
) -> ObservedPeer {
unix_peer(socket.as_fd(), forced)
}
#[cfg(unix)]
fn unix_peer(socket: std::os::fd::BorrowedFd<'_>, forced: ForcedFailures) -> ObservedPeer {
#[cfg(target_os = "macos")]
return macos::observe(socket, forced);
#[cfg(target_os = "linux")]
return linux::observe(socket, forced);
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
{
let _ = (socket, forced);
ObservedPeer::Unobserved
}
}
#[cfg(windows)]
pub fn observe_pipe_client(pipe: &impl std::os::windows::io::AsHandle) -> ObservedPeer {
windows::observe(pipe.as_handle(), ForcedFailures::default())
}
#[cfg(all(windows, feature = "test-support"))]
pub fn observe_pipe_client_forcing(
pipe: &impl std::os::windows::io::AsHandle,
forced: ForcedFailures,
) -> ObservedPeer {
windows::observe(pipe.as_handle(), forced)
}