1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use async_io::Async;

use std::{
    fmt::Debug,
    future::Future,
    marker::PhantomData,
    ops::Deref,
    os::unix::net::UnixStream,
    pin::Pin,
    str::FromStr,
    task::{Context, Poll},
};

use crate::{
    address::{self, Address},
    guid::Guid,
    handshake::{self, Handshake as SyncHandshake, IoOperation},
    raw::Socket,
    Error, Result,
};

/// The asynchronous sibling of [`handshake::Handshake`].
///
/// The underlying socket is in nonblocking mode. Enabling blocking mode on it, will lead to
/// undefined behaviour.
pub(crate) struct Authenticated<S>(handshake::Authenticated<S>);

impl<S> Authenticated<S>
where
    S: Socket,
{
    /// Unwraps the inner [`handshake::Authenticated`].
    pub fn into_inner(self) -> handshake::Authenticated<S> {
        self.0
    }
}

impl<S> Deref for Authenticated<S> {
    type Target = handshake::Authenticated<S>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<S> Authenticated<Async<S>>
where
    S: Debug + Unpin,
    Async<S>: Socket,
{
    /// Create a client-side `Authenticated` for the given `socket`.
    pub async fn client(socket: Async<S>) -> Result<Self> {
        Handshake {
            handshake: Some(handshake::ClientHandshake::new(socket)),
            phantom: PhantomData,
        }
        .await
    }

    /// Create a server-side `Authenticated` for the given `socket`.
    pub async fn server(socket: Async<S>, guid: Guid, client_uid: u32) -> Result<Self> {
        Handshake {
            handshake: Some(handshake::ServerHandshake::new(socket, guid, client_uid)),
            phantom: PhantomData,
        }
        .await
    }
}

impl Authenticated<Async<UnixStream>> {
    /// Create a `Authenticated` for the session/user message bus.
    ///
    /// Although, session bus hardly ever runs on anything other than UNIX domain sockets, if you
    /// want your code to be able to handle those rare cases, use [`AuthenticatedType::session`]
    /// instead.
    pub async fn session() -> Result<Self> {
        match Address::session()?.connect_async().await? {
            address::AsyncStream::Unix(a) => Self::client(a).await,
        }
    }

    /// Create a `Authenticated` for the system-wide message bus.
    ///
    /// Although, system bus hardly ever runs on anything other than UNIX domain sockets, if you
    /// want your code to be able to handle those rare cases, use [`AuthenticatedType::system`]
    /// instead.
    pub async fn system() -> Result<Self> {
        match Address::system()?.connect_async().await? {
            address::AsyncStream::Unix(a) => Self::client(a).await,
        }
    }
}

struct Handshake<H, S> {
    handshake: Option<H>,
    phantom: PhantomData<S>,
}

impl<H, S> Future for Handshake<H, S>
where
    H: SyncHandshake<Async<S>> + Unpin + Debug,
    S: Unpin,
{
    type Output = Result<Authenticated<Async<S>>>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let self_mut = &mut self.get_mut();
        let handshake = self_mut
            .handshake
            .as_mut()
            .expect("ClientHandshake::poll() called unexpectedly");

        loop {
            match handshake.advance_handshake() {
                Ok(()) => {
                    let handshake = self_mut
                        .handshake
                        .take()
                        .expect("<Handshake as Future>::poll() called unexpectedly");
                    let authenticated = handshake
                        .try_finish()
                        .expect("Failed to finish a successfull handshake");

                    return Poll::Ready(Ok(Authenticated(authenticated)));
                }
                Err(Error::Io(e)) => {
                    if e.kind() == std::io::ErrorKind::WouldBlock {
                        let poll = match handshake.next_io_operation() {
                            IoOperation::Read => handshake.socket().poll_readable(cx),
                            IoOperation::Write => handshake.socket().poll_writable(cx),
                            IoOperation::None => panic!("Invalid handshake state"),
                        };
                        match poll {
                            Poll::Pending => return Poll::Pending,
                            // Guess socket became ready already so let's try it again.
                            Poll::Ready(Ok(_)) => continue,
                            Poll::Ready(Err(e)) => return Poll::Ready(Err(e.into())),
                        }
                    } else {
                        return Poll::Ready(Err(Error::Io(e)));
                    }
                }
                Err(e) => return Poll::Ready(Err(e)),
            }
        }
    }
}

/// Type representing all concrete [`Authenticated`] types, provided by zbus.
///
/// For maximum portability, use constructor methods provided by this type instead of ones provided
/// by [`Authenticated`].
pub(crate) enum AuthenticatedType {
    Unix(Authenticated<Async<UnixStream>>),
}

impl AuthenticatedType {
    /// Create a `AuthenticatedType` for the given [D-Bus address].
    ///
    /// [D-Bus address]: https://dbus.freedesktop.org/doc/dbus-specification.html#addresses
    pub async fn for_address(address: &str) -> Result<Self> {
        match Address::from_str(address)?.connect_async().await? {
            address::AsyncStream::Unix(a) => Authenticated::client(a).await.map(Self::Unix),
        }
    }

    /// Create a `AuthenticatedType` for the session/user message bus.
    pub async fn session() -> Result<Self> {
        match Address::session()?.connect_async().await? {
            address::AsyncStream::Unix(a) => Authenticated::client(a).await.map(Self::Unix),
        }
    }

    /// Create a `AuthenticatedType` for the system-wide message bus.
    pub async fn system() -> Result<Self> {
        match Address::system()?.connect_async().await? {
            address::AsyncStream::Unix(a) => Authenticated::client(a).await.map(Self::Unix),
        }
    }
}

#[cfg(test)]
mod tests {
    use nix::unistd::Uid;
    use std::os::unix::net::UnixStream;

    use super::*;

    use crate::{Guid, Result};

    #[test]
    fn async_handshake() {
        futures::executor::block_on(handshake()).unwrap();
    }

    async fn handshake() -> Result<()> {
        // a pair of non-blocking connection UnixStream
        let (p0, p1) = UnixStream::pair()?;

        // initialize both handshakes
        let client = Authenticated::client(Async::new(p0)?);
        let server =
            Authenticated::server(Async::new(p1)?, Guid::generate(), Uid::current().into());

        // proceed to the handshakes
        let (client_auth, server_auth) = futures::try_join!(client, server)?;

        assert_eq!(client_auth.server_guid, server_auth.server_guid);
        assert_eq!(client_auth.cap_unix_fd, server_auth.cap_unix_fd);

        Ok(())
    }
}