diff --git a/src/bin/busd.rs b/src/bin/busd.rs index 7dcc8ab..fdd1424 100644 --- a/src/bin/busd.rs +++ b/src/bin/busd.rs @@ -6,7 +6,7 @@ use std::{fs::File, io::Write, os::fd::FromRawFd}; use busd::bus; use anyhow::Result; -use clap::{Parser, ValueEnum}; +use clap::Parser; #[cfg(unix)] use tokio::{select, signal::unix::SignalKind}; use tracing::error; @@ -25,11 +25,6 @@ struct Args { #[clap(long)] print_address: bool, - /// The authentication mechanism to use. - #[clap(long)] - #[arg(value_enum, default_value_t = AuthMechanism::External)] - auth_mechanism: AuthMechanism, - /// File descriptor to which readiness notifications are sent. /// /// Once the server is listening to connections on the specified socket, it will print @@ -43,33 +38,13 @@ struct Args { ready_fd: Option, } -#[derive(Copy, Clone, Debug, ValueEnum)] -enum AuthMechanism { - /// This is the recommended authentication mechanism on platforms where credentials can be - /// transferred out-of-band, in particular Unix platforms that can perform credentials-passing - /// over UNIX domain sockets. - External, - /// Does not perform any authentication at all (not recommended). - Anonymous, -} - -impl From for zbus::AuthMechanism { - fn from(auth_mechanism: AuthMechanism) -> Self { - match auth_mechanism { - AuthMechanism::External => zbus::AuthMechanism::External, - AuthMechanism::Anonymous => zbus::AuthMechanism::Anonymous, - } - } -} - #[tokio::main] async fn main() -> Result<()> { busd::tracing_subscriber::init(); let args = Args::parse(); - let mut bus = - bus::Bus::for_address(args.address.as_deref(), args.auth_mechanism.into()).await?; + let mut bus = bus::Bus::for_address(args.address.as_deref()).await?; #[cfg(unix)] if let Some(fd) = args.ready_fd { diff --git a/src/bus/mod.rs b/src/bus/mod.rs index 23c5d70..4eb0a05 100644 --- a/src/bus/mod.rs +++ b/src/bus/mod.rs @@ -45,7 +45,7 @@ enum Listener { } impl Bus { - pub async fn for_address(address: Option<&str>, auth_mechanism: AuthMechanism) -> Result { + pub async fn for_address(address: Option<&str>) -> Result { let mut address = match address { Some(address) => Address::from_str(address)?, None => Address::from_str(&default_address())?, @@ -59,14 +59,21 @@ impl Bus { guid.into() } }; - let listener = match address.transport() { + let (listener, auth_mechanism) = match address.transport() { #[cfg(unix)] - Transport::Unix(unix) => Self::unix_stream(unix).await, - Transport::Tcp(tcp) => Self::tcp_stream(tcp).await, + Transport::Unix(unix) => (Self::unix_stream(unix).await?, AuthMechanism::External), + Transport::Tcp(tcp) => { + #[cfg(not(windows))] + let auth_mechanism = AuthMechanism::Anonymous; + #[cfg(windows)] + let auth_mechanism = AuthMechanism::External; + + (Self::tcp_stream(tcp).await?, auth_mechanism) + } #[cfg(windows)] Transport::Autolaunch(_) => bail!("`autolaunch` transport is not supported (yet)."), _ => bail!("Unsupported address `{}`.", address), - }?; + }; let peers = Peers::new(); diff --git a/tests/fdo.rs b/tests/fdo.rs index 9b70873..6a98f5d 100644 --- a/tests/fdo.rs +++ b/tests/fdo.rs @@ -15,7 +15,7 @@ use tracing::instrument; use zbus::{ fdo::{self, DBusProxy, ReleaseNameReply, RequestNameFlags, RequestNameReply}, names::{BusName, WellKnownName}, - AuthMechanism, CacheProperties, ConnectionBuilder, + CacheProperties, ConnectionBuilder, }; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -30,18 +30,16 @@ async fn name_ownership_changes() { let s = Alphanumeric.sample_string(&mut thread_rng(), 10); let path = temp_dir().join(s); let address = format!("unix:path={}", path.display()); - name_ownership_changes_(&address, AuthMechanism::External).await; + name_ownership_changes_(&address).await; } // TCP socket let address = "tcp:host=127.0.0.1,port=4242".to_string(); - name_ownership_changes_(&address, AuthMechanism::Anonymous).await; + name_ownership_changes_(&address).await; } -async fn name_ownership_changes_(address: &str, auth_mechanism: AuthMechanism) { - let mut bus = Bus::for_address(Some(address), auth_mechanism) - .await - .unwrap(); +async fn name_ownership_changes_(address: &str) { + let mut bus = Bus::for_address(Some(address)).await.unwrap(); let (tx, rx) = tokio::sync::oneshot::channel(); let handle = tokio::spawn(async move { diff --git a/tests/greet.rs b/tests/greet.rs index 610a4f9..6fec8df 100644 --- a/tests/greet.rs +++ b/tests/greet.rs @@ -17,8 +17,8 @@ use zbus::{ fdo::{self, DBusProxy}, interface, proxy, zvariant::ObjectPath, - AsyncDrop, AuthMechanism, CacheProperties, Connection, ConnectionBuilder, MatchRule, - MessageHeader, MessageStream, SignalContext, + AsyncDrop, CacheProperties, Connection, ConnectionBuilder, MatchRule, MessageHeader, + MessageStream, SignalContext, }; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -33,18 +33,16 @@ async fn greet() { let s = Alphanumeric.sample_string(&mut thread_rng(), 10); let path = temp_dir().join(s); let address = format!("unix:path={}", path.display()); - greet_(&address, AuthMechanism::External).await; + greet_(&address).await; } // TCP socket let address = "tcp:host=127.0.0.1,port=4248".to_string(); - greet_(&address, AuthMechanism::Anonymous).await; + greet_(&address).await; } -async fn greet_(socket_addr: &str, auth_mechanism: AuthMechanism) { - let mut bus = Bus::for_address(Some(socket_addr), auth_mechanism) - .await - .unwrap(); +async fn greet_(socket_addr: &str) { + let mut bus = Bus::for_address(Some(socket_addr)).await.unwrap(); let (tx, mut rx) = channel(1); let handle = tokio::spawn(async move { diff --git a/tests/monitor.rs b/tests/monitor.rs index 84fd463..49d5790 100644 --- a/tests/monitor.rs +++ b/tests/monitor.rs @@ -7,7 +7,7 @@ use tracing::instrument; use zbus::{ fdo::{DBusProxy, MonitoringProxy, NameAcquired, NameLost, NameOwnerChanged, RequestNameFlags}, names::BusName, - AuthMechanism, CacheProperties, ConnectionBuilder, MessageStream, MessageType, + CacheProperties, ConnectionBuilder, MessageStream, MessageType, }; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] @@ -17,9 +17,7 @@ async fn become_monitor() { busd::tracing_subscriber::init(); let address = "tcp:host=127.0.0.1,port=4242".to_string(); - let mut bus = Bus::for_address(Some(&address), AuthMechanism::Anonymous) - .await - .unwrap(); + let mut bus = Bus::for_address(Some(&address)).await.unwrap(); let (tx, rx) = tokio::sync::oneshot::channel(); let handle = tokio::spawn(async move { diff --git a/tests/multiple_conns.rs b/tests/multiple_conns.rs index 45821d1..68cb0b8 100644 --- a/tests/multiple_conns.rs +++ b/tests/multiple_conns.rs @@ -11,7 +11,7 @@ use rand::{ }; use tokio::{select, sync::oneshot::channel}; use tracing::instrument; -use zbus::{AuthMechanism, ConnectionBuilder}; +use zbus::ConnectionBuilder; #[tokio::test(flavor = "multi_thread", worker_threads = 8)] #[instrument] @@ -24,18 +24,16 @@ async fn multi_conenct() { let s = Alphanumeric.sample_string(&mut thread_rng(), 10); let path = temp_dir().join(s); let address = format!("unix:path={}", path.display()); - multi_conenct_(&address, AuthMechanism::External).await; + multi_conenct_(&address).await; } // TCP socket let address = "tcp:host=127.0.0.1,port=4246".to_string(); - multi_conenct_(&address, AuthMechanism::Anonymous).await; + multi_conenct_(&address).await; } -async fn multi_conenct_(socket_addr: &str, auth_mechanism: AuthMechanism) { - let mut bus = Bus::for_address(Some(socket_addr), auth_mechanism) - .await - .unwrap(); +async fn multi_conenct_(socket_addr: &str) { + let mut bus = Bus::for_address(Some(socket_addr)).await.unwrap(); let (tx, rx) = channel(); let handle = tokio::spawn(async move {