Rollup merge of #88025 - devnexen:netbsd_scm_creds, r=Amanieu

ScmCredentials netbsd implementation.
This commit is contained in:
Dylan DPC 2022-04-05 22:58:54 +02:00 committed by GitHub
commit d2e1e6dc75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 13 deletions

View file

@ -10,7 +10,7 @@ use crate::slice::from_raw_parts;
use crate::sys::net::Socket; use crate::sys::net::Socket;
// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here? // FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
#[cfg(all(doc, not(target_os = "linux"), not(target_os = "android")))] #[cfg(all(doc, not(target_os = "linux"), not(target_os = "android"), not(target_os = "netbsd")))]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
mod libc { mod libc {
pub use libc::c_int; pub use libc::c_int;
@ -177,13 +177,24 @@ impl<'a, T> Iterator for AncillaryDataIter<'a, T> {
} }
} }
#[cfg(all(doc, not(target_os = "android"), not(target_os = "linux"), not(target_os = "netbsd")))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
#[derive(Clone)]
pub struct SocketCred(());
/// Unix credential. /// Unix credential.
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(target_os = "android", target_os = "linux",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
#[derive(Clone)] #[derive(Clone)]
pub struct SocketCred(libc::ucred); pub struct SocketCred(libc::ucred);
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(target_os = "netbsd")]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
#[derive(Clone)]
pub struct SocketCred(libc::sockcred);
#[doc(cfg(any(target_os = "android", target_os = "linux")))]
#[cfg(any(target_os = "android", target_os = "linux"))]
impl SocketCred { impl SocketCred {
/// Create a Unix credential struct. /// Create a Unix credential struct.
/// ///
@ -234,6 +245,61 @@ impl SocketCred {
} }
} }
#[cfg(target_os = "netbsd")]
impl SocketCred {
/// Create a Unix credential struct.
///
/// PID, UID and GID is set to 0.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn new() -> SocketCred {
SocketCred(libc::sockcred {
sc_pid: 0,
sc_uid: 0,
sc_euid: 0,
sc_gid: 0,
sc_egid: 0,
sc_ngroups: 0,
sc_groups: [0u32; 1],
})
}
/// Set the PID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_pid(&mut self, pid: libc::pid_t) {
self.0.sc_pid = pid;
}
/// Get the current PID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn get_pid(&self) -> libc::pid_t {
self.0.sc_pid
}
/// Set the UID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_uid(&mut self, uid: libc::uid_t) {
self.0.sc_uid = uid;
}
/// Get the current UID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn get_uid(&self) -> libc::uid_t {
self.0.sc_uid
}
/// Set the GID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_gid(&mut self, gid: libc::gid_t) {
self.0.sc_gid = gid;
}
/// Get the current GID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn get_gid(&self) -> libc::gid_t {
self.0.sc_gid
}
}
/// This control message contains file descriptors. /// This control message contains file descriptors.
/// ///
/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_RIGHTS`. /// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_RIGHTS`.
@ -249,14 +315,22 @@ impl<'a> Iterator for ScmRights<'a> {
} }
} }
#[cfg(all(doc, not(target_os = "android"), not(target_os = "linux"), not(target_os = "netbsd")))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, ()>);
/// This control message contains unix credentials. /// This control message contains unix credentials.
/// ///
/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_CREDENTIALS` or `SCM_CREDS`. /// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_CREDENTIALS` or `SCM_CREDS`.
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(target_os = "android", target_os = "linux",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>); pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>);
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(target_os = "netbsd")]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::sockcred>);
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
impl<'a> Iterator for ScmCredentials<'a> { impl<'a> Iterator for ScmCredentials<'a> {
type Item = SocketCred; type Item = SocketCred;
@ -278,7 +352,7 @@ pub enum AncillaryError {
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub enum AncillaryData<'a> { pub enum AncillaryData<'a> {
ScmRights(ScmRights<'a>), ScmRights(ScmRights<'a>),
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
ScmCredentials(ScmCredentials<'a>), ScmCredentials(ScmCredentials<'a>),
} }
@ -300,8 +374,8 @@ impl<'a> AncillaryData<'a> {
/// # Safety /// # Safety
/// ///
/// `data` must contain a valid control message and the control message must be type of /// `data` must contain a valid control message and the control message must be type of
/// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDENTIALS`. /// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDS`.
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
unsafe fn as_credentials(data: &'a [u8]) -> Self { unsafe fn as_credentials(data: &'a [u8]) -> Self {
let ancillary_data_iter = AncillaryDataIter::new(data); let ancillary_data_iter = AncillaryDataIter::new(data);
let scm_credentials = ScmCredentials(ancillary_data_iter); let scm_credentials = ScmCredentials(ancillary_data_iter);
@ -320,6 +394,8 @@ impl<'a> AncillaryData<'a> {
libc::SCM_RIGHTS => Ok(AncillaryData::as_rights(data)), libc::SCM_RIGHTS => Ok(AncillaryData::as_rights(data)),
#[cfg(any(target_os = "android", target_os = "linux",))] #[cfg(any(target_os = "android", target_os = "linux",))]
libc::SCM_CREDENTIALS => Ok(AncillaryData::as_credentials(data)), libc::SCM_CREDENTIALS => Ok(AncillaryData::as_credentials(data)),
#[cfg(target_os = "netbsd")]
libc::SCM_CREDS => Ok(AncillaryData::as_credentials(data)),
cmsg_type => { cmsg_type => {
Err(AncillaryError::Unknown { cmsg_level: libc::SOL_SOCKET, cmsg_type }) Err(AncillaryError::Unknown { cmsg_level: libc::SOL_SOCKET, cmsg_type })
} }
@ -531,7 +607,7 @@ impl<'a> SocketAncillary<'a> {
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET` /// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
/// and type `SCM_CREDENTIALS` or `SCM_CREDS`. /// and type `SCM_CREDENTIALS` or `SCM_CREDS`.
/// ///
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool { pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool {
self.truncated = false; self.truncated = false;
@ -540,7 +616,10 @@ impl<'a> SocketAncillary<'a> {
&mut self.length, &mut self.length,
creds, creds,
libc::SOL_SOCKET, libc::SOL_SOCKET,
#[cfg(not(target_os = "netbsd"))]
libc::SCM_CREDENTIALS, libc::SCM_CREDENTIALS,
#[cfg(target_os = "netbsd")]
libc::SCM_CREDS,
) )
} }

View file

@ -865,7 +865,7 @@ impl UnixDatagram {
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> { pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
self.0.set_passcred(passcred) self.0.set_passcred(passcred)
@ -877,7 +877,7 @@ impl UnixDatagram {
/// Get the socket option `SO_PASSCRED`. /// Get the socket option `SO_PASSCRED`.
/// ///
/// [`set_passcred`]: UnixDatagram::set_passcred /// [`set_passcred`]: UnixDatagram::set_passcred
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn passcred(&self) -> io::Result<bool> { pub fn passcred(&self) -> io::Result<bool> {
self.0.passcred() self.0.passcred()

View file

@ -415,7 +415,7 @@ impl UnixStream {
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> { pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
self.0.set_passcred(passcred) self.0.set_passcred(passcred)
@ -427,7 +427,7 @@ impl UnixStream {
/// Get the socket option `SO_PASSCRED`. /// Get the socket option `SO_PASSCRED`.
/// ///
/// [`set_passcred`]: UnixStream::set_passcred /// [`set_passcred`]: UnixStream::set_passcred
#[cfg(any(doc, target_os = "android", target_os = "linux",))] #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn passcred(&self) -> io::Result<bool> { pub fn passcred(&self) -> io::Result<bool> {
self.0.passcred() self.0.passcred()

View file

@ -419,6 +419,17 @@ impl Socket {
Ok(passcred != 0) Ok(passcred != 0)
} }
#[cfg(target_os = "netbsd")]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, passcred as libc::c_int)
}
#[cfg(target_os = "netbsd")]
pub fn passcred(&self) -> io::Result<bool> {
let passcred: libc::c_int = getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)?;
Ok(passcred != 0)
}
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))] #[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
let mut nonblocking = nonblocking as libc::c_int; let mut nonblocking = nonblocking as libc::c_int;