Skip to content

Commit

Permalink
Merge pull request #119 from MaxVerevkin/freebsd
Browse files Browse the repository at this point in the history
auth: send ScmCreds on FreeBSD and Dragonfly
  • Loading branch information
KillingSpark authored Mar 21, 2024
2 parents 20af8f2 + c36ded7 commit 5875f1f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rustbus/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Deals with authentication to the other side. You probably do not need this.
use nix::sys::socket::{self, sendmsg};
use nix::unistd::getuid;
use std::io::{Read, Write};
use std::io::{IoSlice, Read, Write};
use std::os::fd::AsRawFd;
use std::os::unix::net::UnixStream;

fn write_message(msg: &str, stream: &mut UnixStream) -> std::io::Result<()> {
Expand Down Expand Up @@ -79,8 +81,21 @@ pub enum AuthResult {
}

pub fn do_auth(stream: &mut UnixStream) -> std::io::Result<AuthResult> {
// The D-Bus daemon expects an SCM_CREDS first message on FreeBSD and Dragonfly
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
let cmsgs = [socket::ControlMessage::ScmCreds];
#[cfg(not(any(target_os = "freebsd", target_os = "dragonfly")))]
let cmsgs = [];

// send a null byte as the first thing
stream.write_all(&[0])?;
sendmsg::<()>(
stream.as_raw_fd(),
&[IoSlice::new(&[0])],
&cmsgs,
socket::MsgFlags::empty(),
None,
)?;

write_message(&format!("AUTH EXTERNAL {}", get_uid_as_hex()), stream)?;

let mut read_buf = Vec::new();
Expand Down

0 comments on commit 5875f1f

Please sign in to comment.