Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth: send ScmCreds on FreeBSD and Dragonfly #119

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading