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
//! D-Bus address handling.
//!
//! Server addresses consist of a transport name followed by a colon, and then an optional,
//! comma-separated list of keys and values in the form key=value.
//!
//! See also:
//!
//! * [Server addresses] in the D-Bus specification.
//!
//! [Server addresses]: https://dbus.freedesktop.org/doc/dbus-specification.html#addresses

use std::env;

#[cfg(all(unix, not(target_os = "macos")))]
use nix::unistd::Uid;
use thiserror::Error;

pub mod transport;

mod address;
pub use address::{DBusAddr, ToDBusAddrs};

mod address_list;
pub use address_list::{DBusAddrList, DBusAddrListIter};

mod percent;
pub use percent::*;

/// Error returned when an address is invalid.
#[derive(Error, Debug, Clone, Eq, PartialEq)]
pub enum Error {
    #[error("Missing transport in address")]
    MissingTransport,

    #[error("Encoding error: {0}")]
    Encoding(String),

    #[error("Duplicate key: `{0}`")]
    DuplicateKey(String),

    #[error("Missing key: `{0}`")]
    MissingKey(String),

    #[error("Missing value for key: `{0}`")]
    MissingValue(String),

    #[error("Invalid value for key: `{0}`")]
    InvalidKey(String),

    #[error("Invalid value for key: `{0}`")]
    InvalidValue(String),

    #[error("Unknown TCP address family: `{0}`")]
    UnknownTcpFamily(String),

    #[error("Other error: {0}")]
    Other(String),
}

pub type Result<T> = std::result::Result<T, Error>;

/// Get the address for session socket respecting the DBUS_SESSION_BUS_ADDRESS environment
/// variable. If we don't recognize the value (or it's not set) we fall back to
/// $XDG_RUNTIME_DIR/bus
pub fn session() -> Result<DBusAddrList<'static>> {
    match env::var("DBUS_SESSION_BUS_ADDRESS") {
        Ok(val) => DBusAddrList::try_from(val),
        _ => {
            #[cfg(windows)]
            {
                return DBusAddrList::try_from("autolaunch:scope=*user;autolaunch:");
            }

            #[cfg(all(unix, not(target_os = "macos")))]
            {
                let runtime_dir = env::var("XDG_RUNTIME_DIR")
                    .unwrap_or_else(|_| format!("/run/user/{}", Uid::effective()));
                let path = format!("unix:path={runtime_dir}/bus");

                DBusAddrList::try_from(path)
            }

            #[cfg(target_os = "macos")]
            return DBusAddrList::try_from("launchd:env=DBUS_LAUNCHD_SESSION_BUS_SOCKET");
        }
    }
}

/// Get the address for system bus respecting the DBUS_SYSTEM_BUS_ADDRESS environment
/// variable. If we don't recognize the value (or it's not set) we fall back to
/// /var/run/dbus/system_bus_socket
pub fn system() -> Result<DBusAddrList<'static>> {
    match env::var("DBUS_SYSTEM_BUS_ADDRESS") {
        Ok(val) => DBusAddrList::try_from(val),
        _ => {
            #[cfg(all(unix, not(target_os = "macos")))]
            return DBusAddrList::try_from("unix:path=/var/run/dbus/system_bus_socket");

            #[cfg(windows)]
            return DBusAddrList::try_from("autolaunch:");

            #[cfg(target_os = "macos")]
            return DBusAddrList::try_from("launchd:env=DBUS_LAUNCHD_SESSION_BUS_SOCKET");
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{borrow::Cow, ffi::OsStr};

    use super::{
        transport::{Transport, UnixAddrKind},
        DBusAddr,
    };
    use crate::transport::{AutolaunchScope, TcpFamily};

    #[test]
    fn parse_err() {
        assert_eq!(
            DBusAddr::try_from("").unwrap_err().to_string(),
            "Missing transport in address"
        );
        assert_eq!(
            DBusAddr::try_from("foo").unwrap_err().to_string(),
            "Missing transport in address"
        );
        DBusAddr::try_from("foo:opt").unwrap();
        assert_eq!(
            DBusAddr::try_from("foo:opt=1,opt=2")
                .unwrap_err()
                .to_string(),
            "Duplicate key: `opt`"
        );
        assert_eq!(
            DBusAddr::try_from("foo:opt=%1").unwrap_err().to_string(),
            "Encoding error: Incomplete percent-encoded sequence"
        );
        assert_eq!(
            DBusAddr::try_from("foo:opt=%1z").unwrap_err().to_string(),
            "Encoding error: Invalid hexadecimal character in percent-encoded sequence"
        );
        assert_eq!(
            DBusAddr::try_from("foo:opt=1\rz").unwrap_err().to_string(),
            "Encoding error: Invalid character in address"
        );
        assert_eq!(
            DBusAddr::try_from("foo:guid=9406e28972c595c590766c9564ce623")
                .unwrap_err()
                .to_string(),
            "Invalid value for key: `guid`"
        );
        assert_eq!(
            DBusAddr::try_from("foo:guid=9406e28972c595c590766c9564ce623g")
                .unwrap_err()
                .to_string(),
            "Invalid value for key: `guid`"
        );

        let addr = DBusAddr::try_from("foo:guid=9406e28972c595c590766c9564ce623f").unwrap();
        addr.guid().unwrap();
    }

    #[test]
    fn parse_unix() {
        let addr =
            DBusAddr::try_from("unix:path=/tmp/dbus-foo,guid=9406e28972c595c590766c9564ce623f")
                .unwrap();
        let Transport::Unix(u) = addr.transport().unwrap() else {
            panic!();
        };
        assert_eq!(
            u.kind(),
            &UnixAddrKind::Path(Cow::Borrowed(OsStr::new("/tmp/dbus-foo")))
        );

        assert_eq!(
            DBusAddr::try_from("unix:foo=blah").unwrap_err().to_string(),
            "Other error: invalid `unix:` address, missing required key"
        );
        assert_eq!(
            DBusAddr::try_from("unix:path=/blah,abstract=foo")
                .unwrap_err()
                .to_string(),
            "Other error: invalid address, only one of `path` `dir` `tmpdir` `abstract` or `runtime` expected"
        );
        assert_eq!(
            DBusAddr::try_from("unix:runtime=no")
                .unwrap_err()
                .to_string(),
            "Invalid value for key: `runtime`"
        );
        DBusAddr::try_from(String::from("unix:path=/tmp/foo")).unwrap();
    }

    #[test]
    fn parse_launchd() {
        let addr = DBusAddr::try_from("launchd:env=FOOBAR").unwrap();
        let Transport::Launchd(t) = addr.transport().unwrap() else {
            panic!();
        };
        assert_eq!(t.env(), "FOOBAR");

        assert_eq!(
            DBusAddr::try_from("launchd:weof").unwrap_err().to_string(),
            "Missing key: `env`"
        );
    }

    #[test]
    fn parse_systemd() {
        let addr = DBusAddr::try_from("systemd:").unwrap();
        let Transport::Systemd(_) = addr.transport().unwrap() else {
            panic!();
        };
    }

    #[test]
    fn parse_tcp() {
        let addr = DBusAddr::try_from("tcp:host=localhost,bind=*,port=0,family=ipv4").unwrap();
        let Transport::Tcp(t) = addr.transport().unwrap() else {
            panic!();
        };
        assert_eq!(t.host().unwrap(), "localhost");
        assert_eq!(t.bind().unwrap(), "*");
        assert_eq!(t.port().unwrap(), 0);
        assert_eq!(t.family().unwrap(), TcpFamily::IPv4);

        let addr = DBusAddr::try_from("tcp:").unwrap();
        let Transport::Tcp(t) = addr.transport().unwrap() else {
            panic!();
        };
        assert!(t.host().is_none());
        assert!(t.bind().is_none());
        assert!(t.port().is_none());
        assert!(t.family().is_none());
    }

    #[test]
    fn parse_nonce_tcp() {
        let addr =
            DBusAddr::try_from("nonce-tcp:host=localhost,bind=*,port=0,family=ipv6,noncefile=foo")
                .unwrap();
        let Transport::NonceTcp(t) = addr.transport().unwrap() else {
            panic!();
        };
        assert_eq!(t.host().unwrap(), "localhost");
        assert_eq!(t.bind().unwrap(), "*");
        assert_eq!(t.port().unwrap(), 0);
        assert_eq!(t.family().unwrap(), TcpFamily::IPv6);
        assert_eq!(t.noncefile().unwrap(), "foo");
    }

    #[test]
    fn parse_unixexec() {
        let addr = DBusAddr::try_from("unixexec:path=/bin/test,argv2=foo").unwrap();
        let Transport::Unixexec(t) = addr.transport().unwrap() else {
            panic!();
        };

        assert_eq!(t.path(), "/bin/test");
        assert_eq!(t.argv(), &[(2, Cow::from("foo"))]);

        assert_eq!(
            DBusAddr::try_from("unixexec:weof").unwrap_err().to_string(),
            "Missing key: `path`"
        );
    }

    #[test]
    fn parse_autolaunch() {
        let addr = DBusAddr::try_from("autolaunch:scope=*user").unwrap();
        let Transport::Autolaunch(t) = addr.transport().unwrap() else {
            panic!();
        };
        assert_eq!(t.scope().unwrap(), &AutolaunchScope::User);
    }

    #[test]
    #[cfg(feature = "vsock")]
    fn parse_vsock() {
        let addr = DBusAddr::try_from("vsock:cid=12,port=32").unwrap();
        let Transport::Vsock(t) = addr.transport().unwrap() else {
            panic!();
        };
        assert_eq!(t.port(), Some(32));
        assert_eq!(t.cid(), Some(12));

        assert_eq!(
            DBusAddr::try_from("vsock:port=abc")
                .unwrap_err()
                .to_string(),
            "Invalid value for key: `port`"
        );
    }
}