summaryrefslogtreecommitdiff
path: root/src/daemon_dbus.rs
blob: 5615bab11d849f22bf4cba3138a0293b4924d1a5 (plain)
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
416
417
418
419
420
421
422
423
424
425
426
427
use dbus::{
    channel::{MatchingReceiver, Token},
    message::MatchRule,
    nonblock::SyncConnection,
};
use dbus_crossroads::{Context, Crossroads, MethodErr};
use defguard_wireguard_rs::{key::Key, net::IpAddrMask, WireguardInterfaceApi};
use log::{debug, error, info, warn};
use rand::Rng;
use std::{
    collections::{hash_map::Entry, HashMap},
    marker::PhantomData,
    net::SocketAddr,
    ops::DerefMut,
    path::PathBuf,
    str::FromStr,
    sync::Arc,
    time::SystemTime,
};
use tokio::{net::TcpListener, sync::RwLock, task};

use crate::{daemon::*, daemon_config::*, daemon_network::*};

pub async fn start_dbus(
    state: Arc<RwLock<State>>,
    config_path: PathBuf,
) -> Result<(Arc<SyncConnection>, Token), DaemonError> {
    let mut cr = Crossroads::new();
    let if_token = cr.register("de.a.maesch", move |b| {
        //b.signal::<(String, String), _>("Proposal", ("network", "peer_data"));

        let state_rem_network = state.clone();
        let config_path_rem_network = config_path.clone();

        let state_add_peer = state.clone();
        let config_path_add_peer = config_path.clone();

        b.method_with_cr_async(
            "AddNetwork",
            ("name", "key", "ip", "listen_port", "maesch_port"),
            ("success",),
            move |ctx, _, args: (String, String, String, u16, u16)| {
                debug!("Received AddNetwork");
                handle_add_network(ctx, state.clone(), config_path.clone(), args)
            },
        );
        b.method_with_cr_async(
            "RemoveNetwork",
            ("name",),
            ("success",),
            move |ctx, _, args: (String,)| {
                debug!("Received RemoveNetwork");
                handle_remove_network(
                    ctx,
                    state_rem_network.clone(),
                    config_path_rem_network.clone(),
                    args,
                )
            },
        );
        b.method_with_cr_async(
            "AddPeer",
            (
                "network",
                "key",
                "psk",
                "use_hostnames",
                "endpoint",
                "maesch_endpoint",
                "ips",
            ),
            ("success",),
            move |ctx,
                  _,
                  args: (
                String,
                String,
                String,
                bool,
                String,
                String,
                Vec<(String, String)>,
            )| {
                debug!("Received AddPeer");
                handle_add_peer(
                    ctx,
                    state_add_peer.clone(),
                    config_path_add_peer.clone(),
                    args,
                )
            },
        );
    });
    cr.insert("/de/a/maesch", &[if_token], ());

    // drive dbus interface
    let (res, c) = dbus_tokio::connection::new_system_sync()?;
    cr.set_async_support(Some((
        c.clone(),
        Box::new(|x| {
            tokio::spawn(x);
        }),
    )));
    let _ = tokio::spawn(print_error(async {
        res.await;
        Result::<!, &'static str>::Err("lost connection to dbus!")
    }));
    let receive_token = c.start_receive(
        MatchRule::new_method_call(),
        Box::new(move |msg, conn| {
            cr.handle_message(msg, conn).unwrap();
            true
        }),
    );
    c.request_name("de.a.maesch", false, true, false).await?;

    Ok((c, receive_token))
}

async fn handle_add_peer(
    mut ctx: Context,
    state: Arc<RwLock<State>>,
    config_path: PathBuf,
    (nw_name, key, may_psk, use_hostnames, may_endpoint, may_mäsch_endpoint, allowed_ips): (
        String,
        String,
        String,
        bool,
        String,
        String,
        Vec<(String, String)>,
    ),
) -> PhantomData<(bool,)> {
    let key = match Key::from_str(&key) {
        Ok(k) => k,
        Err(e) => {
            warn!("AddPeer with bad key: {e}");
            return ctx.reply(Err(MethodErr::invalid_arg(&e)));
        }
    };

    let psk = match may_psk.as_str() {
        "" => None,
        _ => match Key::from_str(&may_psk) {
            Ok(k) => Some(k),
            Err(e) => {
                warn!("AddPeer with bad pre-shared key: {e}");
                return ctx.reply(Err(MethodErr::invalid_arg(&e)));
            }
        },
    };

    // use_hostnames is already a boolean, so i guess we require it to be set?

    let endpoint = match may_endpoint.as_str() {
        "" => None,
        _ => match SocketAddr::from_str(&may_endpoint) {
            Ok(addr) => Some(Endpoint::Ip(addr)),
            Err(_) => {
                let mut parts_it = may_endpoint.split(':');
                match (parts_it.next(), parts_it.next(), parts_it.next()) {
                    (Some(domain), Some(port), None) if let Ok(port) = port.parse() => {
                        Some(Endpoint::Domain(domain.to_owned(), port))
                    }
                    _ => {
                        warn!("AddPeer with bad endpoint: {may_endpoint}");
                        return ctx.reply(Err(MethodErr::invalid_arg("Could not parse endpoint")));
                    }
                }
            }
        },
    };

    let mäsch_endpoint = match SocketAddr::from_str(&may_mäsch_endpoint) {
        Ok(addr) => addr,
        Err(e) => {
            warn!("AddPeer with bad mäsch endpoint: {e}");
            return ctx.reply(Err(MethodErr::invalid_arg(&e)));
        }
    };

    // let may_allowed_ips = mapM (\(addr, hostname) -> (, if hostname == "" then Nothing else Just hostname) <$> readMaybe addr) allowed_ips
    let may_allowed_ips: Result<Vec<(IpAddrMask, Option<String>)>, _> = allowed_ips
        .into_iter()
        .map(|(addr, hostname)| {
            addr.parse()
                .map(|ip_mask| (ip_mask, if hostname == "" { None } else { Some(hostname) }))
        })
        .collect();
    let allowed_ips = match may_allowed_ips {
        Ok(v) => v,
        Err(e) => {
            warn!("AddPeer with bad allowed ips: {e}");
            return ctx.reply(Err(MethodErr::invalid_arg(&e)));
        }
    };

    let mut state_rw_guard = state.write().await;
    if !state_rw_guard.conf.networks.contains_key(&nw_name) {
        warn!("AddPeer for non-existent network");
        return ctx.reply(Err(MethodErr::invalid_arg("bad network")));
    };

    let allowed_ips_without_domains = allowed_ips.iter().map(|(ip, _)| ip.clone()).collect();

    let wg_api = &state_rw_guard
        .nw_handles
        .get(&nw_name)
        .expect("state.conf.networks and state.nw_handles desynced")
        .0;
    match add_peer(
        wg_api,
        key.clone(),
        psk.clone(),
        endpoint.clone(),
        allowed_ips_without_domains,
    )
    .await
    {
        Ok(_) => info!("Added peer"),
        Err(e) => {
            warn!("AddPeer failed: {e}");
            return ctx.reply(Err(MethodErr::invalid_arg(&e)));
        }
    };

    match state_rw_guard
        .conf
        .networks
        .get_mut(&nw_name)
        .expect("state.conf.networks changed while lock was held")
        .peers
        .entry(key)
    {
        Entry::Vacant(e) => {
            e.insert(PeerConfig {
                psk,
                ips: allowed_ips,
                use_hostnames,
                endpoint,
                last_changed: SystemTime::now(),
                known_to: vec![],
                mäsch_endpoint,
            });
        }
        Entry::Occupied(e) => {
            let r = e.into_mut();
            r.psk = psk;
            r.ips = allowed_ips;
            r.use_hostnames = use_hostnames;
            r.endpoint = endpoint;
            r.last_changed = SystemTime::now();
            r.mäsch_endpoint = mäsch_endpoint;
        }
    };

    match write_config(&state_rw_guard.conf, &config_path) {
        Ok(_) => info!("Synced config"),
        Err(e) => {
            error!("Couldn't sync config: {e}");
            return ctx.reply(Err(MethodErr::failed(&e)));
        }
    };

    return ctx.reply(Ok((true,)));
}

async fn handle_remove_network(
    mut ctx: Context,
    state: Arc<RwLock<State>>,
    config_path: PathBuf,
    (name,): (String,),
) -> PhantomData<(bool,)> {
    let mut state_rw_guard = state.write().await;

    if let Some(_) = state_rw_guard.conf.networks.remove(&name) {
        let (wg_api, h) = state_rw_guard
            .nw_handles
            .remove(&name)
            .expect("state.conf.networks and state.nw_handles desynced");
        h.abort();

        let ri = wg_api.remove_interface();
        let _ = h.await;

        match write_config(&state_rw_guard.conf, &config_path) {
            Ok(_) => info!("Synced config"),
            Err(e) => {
                error!("Couldn't sync config: {e}");
                return ctx.reply(Err(MethodErr::failed(&e)));
            }
        };
        drop(state_rw_guard);

        match ri {
            Ok(_) => info!("Removed network: {name}"),
            Err(e) => {
                error!("Removing network: {e}");
                return ctx.reply(Err(MethodErr::failed(&e)));
            }
        };
    } else {
        warn!("Tried to remove non-existent network: {name}");
        return ctx.reply(Err(MethodErr::invalid_arg("bad network")));
    }

    ctx.reply(Ok((true,)))
}

async fn handle_add_network(
    mut ctx: Context,
    state: Arc<RwLock<State>>,
    config_path: PathBuf,
    (name, may_key, may_ip, may_lp, may_mp): (String, String, String, u16, u16),
) -> PhantomData<(bool,)> {
    let mut state_rw_guard = state.write().await;
    let state_rw = state_rw_guard.deref_mut();

    // Scary!
    let prev_entry = state_rw.conf.networks.remove(&name);

    let key = if may_key.as_str() == "" {
        prev_entry
            .as_ref()
            .map(|nw| nw.privkey.clone())
            .unwrap_or_else(|| Key::new(rand::thread_rng().gen()).to_string())
    } else {
        may_key
    };

    // we store the ip as the original string, but should validate it regardless
    let (ip, ip_string) = match may_ip.as_str() {
        "" => match prev_entry.as_ref() {
            Some(e) => (
                IpAddrMask::from_str(&e.address)
                    .expect("Impossible: old config broken!")
                    .ip,
                e.address.clone(),
            ),
            None => {
                warn!("AddNetwork with no ip");
                prev_entry.map(|pe| state_rw.conf.networks.insert(name, pe));
                return ctx.reply(Err(MethodErr::invalid_arg("ip required")));
            }
        },
        _ => match IpAddrMask::from_str(&may_ip) {
            Ok(ip_mask) => (ip_mask.ip, may_ip),
            Err(_) => {
                warn!("AddNetwork with bad ip");
                prev_entry.map(|pe| state_rw.conf.networks.insert(name, pe));
                return ctx.reply(Err(MethodErr::invalid_arg("invalid ip")));
            }
        },
    };

    let lp = if may_lp == 0 {
        prev_entry
            .as_ref()
            .map(|nw| nw.listen_port)
            .unwrap_or(25565)
    } else {
        may_lp
    };
    let mp = if may_mp == 0 {
        prev_entry.as_ref().map(|nw| nw.mäsch_port).unwrap_or(51820)
    } else {
        may_mp
    };

    let (wg_api, hostnames) = match add_network(
        name.clone(),
        key.clone(),
        ip_string.clone(),
        lp,
        &HashMap::new(),
    )
    .await
    {
        Ok(v) => v,
        Err(e) => {
            warn!("AddNetwork couldn't add network: {e}");
            prev_entry.map(|pe| state_rw.conf.networks.insert(name, pe));
            return ctx.reply(Err(MethodErr::failed(&e)));
        }
    };

    let listener = match TcpListener::bind((ip, mp)).await {
        Ok(l) => l,
        Err(e) => {
            let _ = wg_api.remove_interface();
            warn!("AddNetwork couldn't start listener: {e}");
            prev_entry.map(|pe| state_rw.conf.networks.insert(name, pe));
            return ctx.reply(Err(MethodErr::failed(&e)));
        }
    };
    let h = task::spawn(print_error(run_network(
        state.clone(),
        listener,
        name.clone(),
    )));

    state_rw.nw_handles.insert(name.clone(), (wg_api, h));
    state_rw.conf.networks.insert(
        name,
        Network {
            privkey: key,
            address: ip_string,
            listen_port: lp,
            peers: prev_entry.map_or_else(|| HashMap::new(), |nw: Network| nw.peers),
            mäsch_port: mp,
        },
    );

    // NOTE this _is_ thread-safe, as we hold an exclusive write handle to the state.
    match write_config(&state_rw.conf, &config_path) {
        Ok(_) => info!("Synced config"),
        Err(e) => error!("Couldn't sync config: {e}"),
    }
    // similarly, this is still racy w.r.t. other processes running on the system, but at least
    // no other thread from this program should be able to concurrently call this
    match sync_hostnames(&hostnames) {
        Ok(_) => (),
        Err(e) => error!("Failed to sync hostnames to disk: {e}"),
    };

    ctx.reply(Ok((true,)))
}