Skip to content

Commit a7f6828

Browse files
committed
Feat(sockopt): Add new wrapper around libc::LOCAL_PEERTOKEN
audit_token_t is taken from endpoint-sec-sys. Signed-off-by: Paul Mabileau <[email protected]>
1 parent 31ebc98 commit a7f6828

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/sys/socket/mod.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,63 @@ cfg_if! {
499499
}
500500
}
501501

502+
cfg_if! {
503+
if #[cfg(apple_targets)] {
504+
use std::fmt;
505+
506+
/// Return type of [`LocalPeerToken`].
507+
///
508+
/// The audit token is an opaque token which identifies Mach tasks and
509+
/// senders of Mach messages as subjects to the BSM audit system. Only
510+
/// the appropriate BSM library routines should be used to interpret
511+
/// the contents of the audit token as the representation of the
512+
/// subject identity within the token may change over time.
513+
///
514+
/// Starting with macOS 11, almost all audit functions have been
515+
/// deprecated (see the system header `bsm/libbsm.h`), do not use them
516+
/// if your program target more recent versions of macOS.
517+
///
518+
/// [`LocalPeerToken`]: crate::sys::socket::sockopt::LocalPeerToken
519+
#[repr(C)]
520+
#[derive(Default, Copy, Clone, PartialEq, Eq, Hash)]
521+
pub struct audit_token_t {
522+
/// Value of the token.
523+
///
524+
/// This is considered an opaque value, do not rely on its format.
525+
pub val: [libc::c_uint; 8],
526+
}
527+
528+
// Make the debug representation a hex string to make it shorter and clearer.
529+
impl fmt::Debug for audit_token_t {
530+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
531+
f.debug_tuple("audit_token_t")
532+
.field(&format!("0x{:08X}", self))
533+
.finish()
534+
}
535+
}
536+
537+
impl fmt::LowerHex for audit_token_t {
538+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
539+
for v in self.val {
540+
fmt::LowerHex::fmt(&v, f)?;
541+
}
542+
543+
Ok(())
544+
}
545+
}
546+
547+
impl fmt::UpperHex for audit_token_t {
548+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
549+
for v in self.val {
550+
fmt::UpperHex::fmt(&v, f)?;
551+
}
552+
553+
Ok(())
554+
}
555+
}
556+
}
557+
}
558+
502559
feature! {
503560
#![feature = "net"]
504561
/// Request for multicast socket operations

src/sys/socket/sockopt.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,16 @@ sockopt_impl!(
582582
libc::LOCAL_PEERPID,
583583
libc::c_int
584584
);
585+
#[cfg(apple_targets)]
586+
sockopt_impl!(
587+
/// Get the audit token of the peer process of a connected unix domain
588+
/// socket.
589+
LocalPeerToken,
590+
GetOnly,
591+
libc::SOL_LOCAL,
592+
libc::LOCAL_PEERTOKEN,
593+
super::audit_token_t
594+
);
585595
#[cfg(linux_android)]
586596
sockopt_impl!(
587597
/// Return the credentials of the foreign process connected to this socket.

0 commit comments

Comments
 (0)