aboutsummaryrefslogtreecommitdiff
path: root/x11/wezterm/files/patch-zbus
blob: 614cf51d53f0dc48419afbbe7cc4f3b2753c2664 (plain) (blame)
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
https://gitlab.freedesktop.org/dbus/zbus/-/commit/01d8f0ec41d3
https://gitlab.freedesktop.org/dbus/zbus/-/commit/807909a6c7bf
https://gitlab.freedesktop.org/dbus/zbus/-/commit/e3e2f7ddfd6e
https://gitlab.freedesktop.org/dbus/zbus/-/commit/35d1b8778754
https://github.com/wez/wezterm/pull/829

--- cargo-crates/zbus-1.9.1/Cargo.toml.orig	1970-01-01 00:00:00 UTC
+++ cargo-crates/zbus-1.9.1/Cargo.toml
@@ -48,7 +48,7 @@ version = "0.3.8"
 version = "1.0.2"
 
 [dependencies.nix]
-version = "^0.17"
+version = "0.20.0"
 
 [dependencies.once_cell]
 version = "1.4.0"
--- cargo-crates/zbus-1.9.1/src/address.rs.orig	1970-01-01 00:00:00 UTC
+++ cargo-crates/zbus-1.9.1/src/address.rs
@@ -55,8 +55,9 @@ impl Address {
         match env::var("DBUS_SESSION_BUS_ADDRESS") {
             Ok(val) => Self::from_str(&val),
             _ => {
-                let uid = Uid::current();
-                let path = format!("unix:path=/run/user/{}/bus", uid);
+                let runtime_dir = env::var("XDG_RUNTIME_DIR")
+                    .unwrap_or_else(|_| format!("/run/user/{}", Uid::current()));
+                let path = format!("unix:path={}/bus", runtime_dir);
 
                 Self::from_str(&path)
             }
--- cargo-crates/zbus-1.9.1/src/azync/connection.rs.orig	1970-01-01 00:00:00 UTC
+++ cargo-crates/zbus-1.9.1/src/azync/connection.rs
@@ -177,13 +177,29 @@ where
     /// Upon successful return, the connection is fully established and negotiated: D-Bus messages
     /// can be sent and received.
     pub async fn new_server(stream: S, guid: &Guid) -> Result<Self> {
-        use nix::sys::socket::{getsockopt, sockopt::PeerCredentials};
+        #[cfg(any(target_os = "android", target_os = "linux"))]
+        let client_uid = {
+            use nix::sys::socket::{getsockopt, sockopt::PeerCredentials};
 
-        // FIXME: Could and should this be async?
-        let creds = getsockopt(stream.as_raw_fd(), PeerCredentials)
-            .map_err(|e| Error::Handshake(format!("Failed to get peer credentials: {}", e)))?;
+            let creds = getsockopt(stream.as_raw_fd(), PeerCredentials)
+                .map_err(|e| Error::Handshake(format!("Failed to get peer credentials: {}", e)))?;
 
-        let auth = Authenticated::server(Async::new(stream)?, guid.clone(), creds.uid()).await?;
+            creds.uid()
+        };
+        #[cfg(any(
+            target_os = "macos",
+            target_os = "ios",
+            target_os = "freebsd",
+            target_os = "dragonfly",
+            target_os = "openbsd",
+            target_os = "netbsd"
+        ))]
+        let client_uid = nix::unistd::getpeereid(stream.as_raw_fd())
+            .map_err(|e| Error::Handshake(format!("Failed to get peer credentials: {}", e)))?
+            .0
+            .into();
+
+        let auth = Authenticated::server(Async::new(stream)?, guid.clone(), client_uid).await?;
 
         Ok(Self::new_authenticated(auth))
     }
--- cargo-crates/zbus-1.9.1/src/connection.rs.orig	1970-01-01 00:00:00 UTC
+++ cargo-crates/zbus-1.9.1/src/connection.rs
@@ -160,12 +160,29 @@ impl Connection {
     /// Upon successful return, the connection is fully established and negotiated: D-Bus messages
     /// can be sent and received.
     pub fn new_unix_server(stream: UnixStream, guid: &Guid) -> Result<Self> {
-        use nix::sys::socket::{getsockopt, sockopt::PeerCredentials};
+        #[cfg(any(target_os = "android", target_os = "linux"))]
+        let client_uid = {
+            use nix::sys::socket::{getsockopt, sockopt::PeerCredentials};
 
-        let creds = getsockopt(stream.as_raw_fd(), PeerCredentials)
-            .map_err(|e| Error::Handshake(format!("Failed to get peer credentials: {}", e)))?;
+            let creds = getsockopt(stream.as_raw_fd(), PeerCredentials)
+                .map_err(|e| Error::Handshake(format!("Failed to get peer credentials: {}", e)))?;
 
-        let handshake = ServerHandshake::new(stream, guid.clone(), creds.uid());
+            creds.uid()
+        };
+        #[cfg(any(
+            target_os = "macos",
+            target_os = "ios",
+            target_os = "freebsd",
+            target_os = "dragonfly",
+            target_os = "openbsd",
+            target_os = "netbsd"
+        ))]
+        let client_uid = nix::unistd::getpeereid(stream.as_raw_fd())
+            .map_err(|e| Error::Handshake(format!("Failed to get peer credentials: {}", e)))?
+            .0
+            .into();
+
+        let handshake = ServerHandshake::new(stream, guid.clone(), client_uid);
         handshake
             .blocking_finish()
             .map(Connection::new_authenticated_unix)
--- cargo-crates/zbus-1.9.1/src/handshake.rs.orig	1970-01-01 00:00:00 UTC
+++ cargo-crates/zbus-1.9.1/src/handshake.rs
@@ -124,7 +124,7 @@ impl<S: Socket> ClientHandshake<S> {
 
     fn flush_buffer(&mut self) -> Result<()> {
         while !self.buffer.is_empty() {
-            let written = self.socket.sendmsg(&self.buffer, &[])?;
+            let written = self.socket.sendmsg(&self.buffer, &[], false)?;
             self.buffer.drain(..written);
         }
         Ok(())
@@ -182,6 +182,15 @@ impl<S: Socket> Handshake<S> for ClientHandshake<S> {
                     self.step = ClientHandshakeStep::SendingOauth;
                 }
                 ClientHandshakeStep::SendingOauth => {
+                    #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
+                    {
+                        let zero = self.buffer.drain(..1).next().unwrap();
+                        if self.socket.sendmsg(&[zero], &[], true)? != 1 {
+                            return Err(Error::Handshake(
+                                "Could not send zero byte with credentials".to_string(),
+                            ));
+                        }
+                    }
                     self.flush_buffer()?;
                     self.step = ClientHandshakeStep::WaitOauth;
                 }
@@ -385,7 +394,7 @@ impl<S: Socket> ServerHandshake<S> {
 
     fn flush_buffer(&mut self) -> Result<()> {
         while !self.buffer.is_empty() {
-            let written = self.socket.sendmsg(&self.buffer, &[])?;
+            let written = self.socket.sendmsg(&self.buffer, &[], false)?;
             self.buffer.drain(..written);
         }
         Ok(())
--- cargo-crates/zbus-1.9.1/src/raw/connection.rs.orig	1970-01-01 00:00:00 UTC
+++ cargo-crates/zbus-1.9.1/src/raw/connection.rs
@@ -47,7 +47,7 @@ impl<S: Socket> Connection<S> {
             // VecDeque should never return an empty front buffer if the VecDeque
             // itself is not empty
             debug_assert!(!front.is_empty());
-            let written = self.socket.sendmsg(front, &[])?;
+            let written = self.socket.sendmsg(front, &[], false)?;
             self.raw_out_buffer.drain(..written);
         }
 
@@ -55,14 +55,14 @@ impl<S: Socket> Connection<S> {
         while let Some(msg) = self.msg_out_buffer.front() {
             let mut data = msg.as_bytes();
             let fds = msg.fds();
-            let written = self.socket.sendmsg(data, &fds)?;
+            let written = self.socket.sendmsg(data, &fds, false)?;
             // at least some part of the message has been sent, see if we can/need to send more
             // now the message must be removed from msg_out_buffer and any leftover bytes
             // must be stored into raw_out_buffer
             let msg = self.msg_out_buffer.pop_front().unwrap();
             data = &msg.as_bytes()[written..];
             while !data.is_empty() {
-                match self.socket.sendmsg(data, &[]) {
+                match self.socket.sendmsg(data, &[], false) {
                     Ok(n) => data = &data[n..],
                     Err(e) => {
                         // an error occured, we cannot send more, store the remaining into
--- cargo-crates/zbus-1.9.1/src/raw/socket.rs.orig	1970-01-01 00:00:00 UTC
+++ cargo-crates/zbus-1.9.1/src/raw/socket.rs
@@ -47,7 +47,7 @@ pub trait Socket {
     ///
     /// If the underlying transport does not support transmitting file descriptors, this
     /// will return `Err(ErrorKind::InvalidInput)`.
-    fn sendmsg(&mut self, buffer: &[u8], fds: &[RawFd]) -> io::Result<usize>;
+    fn sendmsg(&mut self, buffer: &[u8], fds: &[RawFd], creds: bool) -> io::Result<usize>;
 
     /// Close the socket.
     ///
@@ -74,6 +74,10 @@ impl Socket for UnixStream {
             Ok(msg) => {
                 let mut fds = vec![];
                 for cmsg in msg.cmsgs() {
+                    #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
+                    if let ControlMessageOwned::ScmCreds(_) = cmsg {
+                        continue;
+                    }
                     if let ControlMessageOwned::ScmRights(fd) = cmsg {
                         fds.extend(fd.iter().map(|&f| unsafe { OwnedFd::from_raw_fd(f) }));
                     } else {
@@ -90,12 +94,23 @@ impl Socket for UnixStream {
         }
     }
 
-    fn sendmsg(&mut self, buffer: &[u8], fds: &[RawFd]) -> io::Result<usize> {
-        let cmsg = if !fds.is_empty() {
+    fn sendmsg(&mut self, buffer: &[u8], fds: &[RawFd], creds: bool) -> io::Result<usize> {
+        let mut cmsg = if !fds.is_empty() {
             vec![ControlMessage::ScmRights(fds)]
         } else {
             vec![]
         };
+        #[cfg(any(target_os = "android", target_os = "linux"))]
+        let unix_creds;
+        if creds {
+            #[cfg(any(target_os = "android", target_os = "linux"))]
+            {
+                unix_creds = Some(nix::sys::socket::UnixCredentials::new());
+                cmsg.push(ControlMessage::ScmCredentials(unix_creds.as_ref().unwrap()));
+            }
+            #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
+            cmsg.push(ControlMessage::ScmCreds);
+        }
         let iov = [IoVec::from_slice(buffer)];
         match sendmsg(self.as_raw_fd(), &iov, &cmsg, MsgFlags::empty(), None) {
             // can it really happen?
@@ -124,8 +139,8 @@ where
         self.get_mut().recvmsg(buffer)
     }
 
-    fn sendmsg(&mut self, buffer: &[u8], fds: &[RawFd]) -> io::Result<usize> {
-        self.get_mut().sendmsg(buffer, fds)
+    fn sendmsg(&mut self, buffer: &[u8], fds: &[RawFd], creds: bool) -> io::Result<usize> {
+        self.get_mut().sendmsg(buffer, fds, creds)
     }
 
     fn close(&self) -> io::Result<()> {
--- wezterm-toast-notification/Cargo.toml.orig	2021-08-14 19:44:38 UTC
+++ wezterm-toast-notification/Cargo.toml
@@ -11,7 +11,7 @@ build = "build.rs"
 open = "2.0"
 log = "0.4"
 
-[target.'cfg(all(not(windows), not(target_os="macos"), not(target_os="freebsd")))'.dependencies]
+[target.'cfg(all(not(windows), not(target_os="macos")))'.dependencies]
 serde = {version="1.0", features = ["derive"]}
 zbus = "1.8"
 zvariant = "2.0"
--- wezterm-toast-notification/src/dbus.rs.orig	2021-08-14 19:44:38 UTC
+++ wezterm-toast-notification/src/dbus.rs
@@ -1,4 +1,4 @@
-#![cfg(all(not(target_os = "macos"), not(windows), not(target_os = "freebsd")))]
+#![cfg(all(not(target_os = "macos"), not(windows)))]
 //! See <https://developer.gnome.org/notification-spec/>
 
 use crate::ToastNotification;
--- wezterm-toast-notification/src/lib.rs.orig	2021-08-14 19:44:38 UTC
+++ wezterm-toast-notification/src/lib.rs
@@ -18,7 +18,7 @@ impl ToastNotification {
 
 #[cfg(windows)]
 use crate::windows as backend;
-#[cfg(all(not(target_os = "macos"), not(windows), not(target_os = "freebsd")))]
+#[cfg(all(not(target_os = "macos"), not(windows)))]
 use dbus as backend;
 #[cfg(target_os = "macos")]
 use macos as backend;
@@ -31,9 +31,6 @@ mod nop {
         Ok(())
     }
 }
-
-#[cfg(target_os = "freebsd")]
-use nop as backend;
 
 pub fn show(notif: ToastNotification) {
     if let Err(err) = backend::show_notif(notif) {