zbus 6.2.0

API for D-Bus communication and its wire format
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
mod auth_mechanism;
mod client;
mod command;
mod common;
#[cfg(feature = "p2p")]
mod server;

use async_trait::async_trait;
#[cfg(unix)]
use rustix::process::geteuid;
use std::fmt::Debug;

#[cfg(windows)]
use crate::win32;
use crate::{Error, OwnedGuid, Result, names::OwnedUniqueName};

use super::socket::{BoxedSplit, ReadHalf, WriteHalf};

pub use auth_mechanism::AuthMechanism;
use client::Client;
use command::Command;
use common::Common;
#[cfg(feature = "p2p")]
use server::Server;

/// The result of a finalized handshake
///
/// The result of a finalized [`ClientHandshake`] or [`ServerHandshake`].
///
/// [`ClientHandshake`]: struct.ClientHandshake.html
/// [`ServerHandshake`]: struct.ServerHandshake.html
#[derive(Debug)]
pub struct Authenticated {
    pub(crate) socket_write: Box<dyn WriteHalf>,
    /// The server Guid
    pub(crate) server_guid: OwnedGuid,
    /// Whether file descriptor passing has been accepted by both sides
    #[cfg(unix)]
    pub(crate) cap_unix_fd: bool,

    pub(crate) socket_read: Option<Box<dyn ReadHalf>>,
    pub(crate) already_received_bytes: Vec<u8>,
    #[cfg(unix)]
    pub(crate) already_received_fds: Vec<std::os::fd::OwnedFd>,
    pub(crate) unique_name: Option<OwnedUniqueName>,
}

impl Authenticated {
    /// Create a client-side `Authenticated` for the given `socket`.
    pub async fn client(
        socket: BoxedSplit,
        server_guid: Option<OwnedGuid>,
        mechanism: Option<AuthMechanism>,
        bus: bool,
        user_id: Option<u32>,
    ) -> Result<Self> {
        Client::new(socket, mechanism, server_guid, bus, user_id)
            .perform()
            .await
    }

    /// Create a server-side `Authenticated` for the given `socket`.
    ///
    /// The function takes `client_uid` on Unix and WASI only. On Windows, it takes `client_sid`
    /// instead.
    #[cfg(feature = "p2p")]
    pub async fn server(
        socket: BoxedSplit,
        guid: OwnedGuid,
        #[cfg(any(unix, target_os = "wasi"))] client_uid: Option<u32>,
        #[cfg(windows)] client_sid: Option<String>,
        auth_mechanism: Option<AuthMechanism>,
        unique_name: Option<OwnedUniqueName>,
    ) -> Result<Self> {
        Server::new(
            socket,
            guid,
            #[cfg(any(unix, target_os = "wasi"))]
            client_uid,
            #[cfg(windows)]
            client_sid,
            auth_mechanism,
            unique_name,
        )?
        .perform()
        .await
    }
}

#[async_trait]
pub trait Handshake {
    /// Perform the handshake.
    ///
    /// On a successful handshake, you get an `Authenticated`. If you need to send a Bus Hello,
    /// this remains to be done.
    async fn perform(mut self) -> Result<Authenticated>;
}

fn sasl_auth_id() -> Result<String> {
    #[cfg(unix)]
    {
        Ok(geteuid().as_raw().to_string())
    }

    #[cfg(windows)]
    {
        Ok(win32::ProcessToken::open(None)?.sid()?)
    }

    // A WASI program has no user of its own: the one it authenticates as is the one the other end
    // of the socket knows its host by, which only whoever builds the connection can say.
    #[cfg(target_os = "wasi")]
    {
        Err(Error::Handshake(
            "WASI has no user ID to authenticate with `EXTERNAL` as: set one with \
             `Builder::user_id`"
                .to_owned(),
        ))
    }
}

// The handshake runs over a real socket pair.
#[cfg(all(test, unix, feature = "p2p"))]
mod tests {
    use std::{
        io::{self, Write},
        os::{fd::BorrowedFd, unix::net::UnixStream},
    };

    use futures_util::future::join;
    use ntest::timeout;
    use test_log::test;

    use super::*;
    use crate::{
        Guid, Message,
        connection::socket::{BoxedSplit, Split},
        runtime::{
            Runtime,
            io::{UnixOps, registered},
            test_runtime::under_every_runtime,
        },
    };

    #[test]
    #[timeout(15000)]
    fn handshake() {
        under_every_runtime(|runtime| async move {
            let (p0, p1) = socket_pair(&runtime);

            let guid = OwnedGuid::from(Guid::generate());
            let client = Client::new(p0, None, Some(guid.clone()), false, None);
            let server = Server::new(p1, guid, Some(geteuid().as_raw()), None, None).unwrap();

            let (client, server) =
                join(async move { client.perform().await.unwrap() }, async move {
                    server.perform().await.unwrap()
                })
                .await;

            assert_eq!(client.server_guid, server.server_guid);
            assert_eq!(client.cap_unix_fd, server.cap_unix_fd);
        });
    }

    #[test]
    #[timeout(15000)]
    fn holding_sockets() {
        under_every_runtime(|runtime| async move {
            let (p0, p1) = socket_pair(&runtime);
            let (p0, p1) = (holding(p0), holding(p1));

            let guid = OwnedGuid::from(Guid::generate());
            let client = Client::new(p0, None, Some(guid.clone()), false, None);
            let server = Server::new(p1, guid, Some(geteuid().as_raw()), None, None).unwrap();

            // Neither side would see the other's commands if they stayed held.
            let (mut client, mut server) =
                join(async move { client.perform().await.unwrap() }, async move {
                    server.perform().await.unwrap()
                })
                .await;

            // Nor a message sent once the handshake is done.
            let sent = Message::method_call("/", "Ping")
                .interface("org.freedesktop.DBus.Peer")
                .build(&())
                .unwrap();
            client.socket_write.send_message(&sent).await.unwrap();
            let received = server
                .socket_read
                .as_mut()
                .unwrap()
                .receive_message(
                    0,
                    &mut server.already_received_bytes,
                    &mut server.already_received_fds,
                )
                .await
                .unwrap();

            assert_eq!(received.to_string(), sent.to_string());
        });
    }

    #[test]
    #[timeout(15000)]
    fn pipelined_handshake() {
        let commands = format!(
            "\0AUTH EXTERNAL {}\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n",
            hex::encode(sasl_auth_id().unwrap()),
        );

        under_every_runtime(|runtime| {
            let commands = commands.clone();

            async move {
                let server = server_greeted_with(&runtime, commands.as_bytes(), None);

                assert!(server.await.unwrap().cap_unix_fd);
            }
        });
    }

    #[test]
    #[timeout(15000)]
    fn pipelining_client() {
        under_every_runtime(|runtime| async move {
            let (client, server) = UnixStream::pair().unwrap();
            let guid = OwnedGuid::from(Guid::generate());

            // A server that only answers once it has read the whole of the client's side of the
            // handshake, which a client waiting for an answer to `AUTH` would never send.
            let server = std::thread::spawn({
                let guid = guid.clone();

                move || {
                    let mut received = vec![];
                    while !received.ends_with(b"BEGIN\r\n") {
                        let mut buffer = [0; 1024];
                        let read = std::io::Read::read(&mut &server, &mut buffer).unwrap();
                        assert_ne!(read, 0, "the client hung up halfway through the handshake");
                        received.extend_from_slice(&buffer[..read]);
                    }
                    (&server)
                        .write_all(format!("OK {guid}\r\nAGREE_UNIX_FD\r\n").as_bytes())
                        .unwrap();

                    (received, server)
                }
            });

            let client = Client::new(
                registered(&runtime, client, UnixOps).unwrap().into(),
                None,
                Some(guid),
                false,
                None,
            );
            let authenticated = client.perform().await.unwrap();
            let (received, _server) = server.join().unwrap();

            let expected = format!(
                "\0AUTH EXTERNAL {}\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n",
                hex::encode(sasl_auth_id().unwrap()),
            );
            assert_eq!(received, expected.as_bytes());
            assert!(authenticated.cap_unix_fd);
        });
    }

    #[test]
    #[timeout(15000)]
    fn separate_external_data() {
        let commands = format!(
            "\0AUTH EXTERNAL\r\nDATA {}\r\nBEGIN\r\n",
            hex::encode(sasl_auth_id().unwrap()),
        );

        under_every_runtime(|runtime| {
            let commands = commands.clone();

            async move {
                server_greeted_with(&runtime, commands.as_bytes(), None)
                    .await
                    .unwrap();
            }
        });
    }

    #[test]
    #[timeout(15000)]
    fn missing_external_data() {
        under_every_runtime(|runtime| async move {
            server_greeted_with(&runtime, b"\0AUTH EXTERNAL\r\nDATA\r\nBEGIN\r\n", None)
                .await
                .unwrap();
        });
    }

    #[test]
    #[timeout(15000)]
    fn anonymous_handshake() {
        under_every_runtime(|runtime| async move {
            server_greeted_with(
                &runtime,
                b"\0AUTH ANONYMOUS abcd\r\nBEGIN\r\n",
                Some(AuthMechanism::Anonymous),
            )
            .await
            .unwrap();
        });
    }

    #[test]
    #[timeout(15000)]
    fn separate_anonymous_data() {
        under_every_runtime(|runtime| async move {
            server_greeted_with(
                &runtime,
                b"\0AUTH ANONYMOUS\r\nDATA abcd\r\nBEGIN\r\n",
                Some(AuthMechanism::Anonymous),
            )
            .await
            .unwrap();
        });
    }

    /// A server handshake over a socket a client has already sent `commands` down.
    ///
    /// Everything the client has to say is in the socket before the server reads a byte, which is
    /// what a client that pipelines its commands does.
    fn server_greeted_with(
        runtime: &Runtime,
        commands: &[u8],
        mechanism: Option<AuthMechanism>,
    ) -> impl Future<Output = Result<Authenticated>> {
        let (client, server) = UnixStream::pair().unwrap();
        (&client).write_all(commands).unwrap();

        let server = Server::new(
            registered(runtime, server, UnixOps).unwrap().into(),
            Guid::generate().into(),
            Some(geteuid().as_raw()),
            mechanism,
            None,
        )
        .unwrap();

        async move {
            let authenticated = server.perform().await;
            drop(client);

            authenticated
        }
    }

    /// Both ends of a socket pair, each registered on `runtime`.
    fn socket_pair(runtime: &Runtime) -> (BoxedSplit, BoxedSplit) {
        let (p0, p1) = UnixStream::pair().unwrap();

        (
            registered(runtime, p0, UnixOps).unwrap().into(),
            registered(runtime, p1, UnixOps).unwrap().into(),
        )
    }

    /// `socket`, with a write half that holds on to what it is given until it is flushed.
    fn holding(socket: BoxedSplit) -> BoxedSplit {
        let (read, write) = socket.take();

        Split::new(
            read,
            Box::new(Holding {
                half: write,
                held: vec![],
            }),
        )
    }

    /// A write half that only sends what it is given once it is flushed.
    #[derive(Debug)]
    struct Holding {
        half: Box<dyn WriteHalf>,
        held: Vec<u8>,
    }

    #[async_trait]
    impl WriteHalf for Holding {
        async fn sendmsg(&mut self, buffer: &[u8], fds: &[BorrowedFd<'_>]) -> io::Result<usize> {
            assert!(
                fds.is_empty(),
                "no file descriptors are sent in these tests"
            );
            self.held.extend_from_slice(buffer);

            Ok(buffer.len())
        }

        async fn flush(&mut self) -> io::Result<()> {
            let mut held = &self.held[..];
            while !held.is_empty() {
                let written = self.half.sendmsg(held, &[]).await?;
                held = &held[written..];
            }
            self.held.clear();

            self.half.flush().await
        }

        async fn close(&mut self) -> io::Result<()> {
            self.half.close().await
        }
    }
}