Skip to main content

fez/capabilities/network/
mod.rs

1//! NetworkManager inspection capability.
2//!
3//! Reads the readable NetworkManager surface over the cockpit-bridge
4//! `dbus-json3` channel (`org.freedesktop.NetworkManager`, system bus,
5//! unprivileged). Two actions: `network list` (device inventory) and
6//! `network show <device>` (full per-device detail). Read-only: no mutation,
7//! no privilege escalation.
8
9use crate::capabilities::{render, CapabilityContext, View};
10use crate::cli::{Cli, NetworkAction};
11use crate::error::Result;
12
13mod model;
14mod reads;
15
16pub(super) const NM_NAME: &str = "org.freedesktop.NetworkManager";
17pub(super) const NM_MGR_PATH: &str = "/org/freedesktop/NetworkManager";
18pub(super) const NM_MGR_IFACE: &str = "org.freedesktop.NetworkManager";
19pub(super) const PROPS_IFACE: &str = "org.freedesktop.DBus.Properties";
20pub(super) const DEVICE_IFACE: &str = "org.freedesktop.NetworkManager.Device";
21pub(super) const IP4_IFACE: &str = "org.freedesktop.NetworkManager.IP4Config";
22pub(super) const IP6_IFACE: &str = "org.freedesktop.NetworkManager.IP6Config";
23pub(super) const ACTIVE_IFACE: &str = "org.freedesktop.NetworkManager.Connection.Active";
24pub(super) const DHCP4_IFACE: &str = "org.freedesktop.NetworkManager.DHCP4Config";
25
26/// Route a parsed `network` action to its handler and render the result.
27///
28/// Returns the process exit code.
29pub fn dispatch(cli: &Cli, action: &NetworkAction) -> i32 {
30    let result = run(cli, action);
31    render(cli, result)
32}
33
34/// Connect to the bridge and dispatch the requested read action.
35fn run(cli: &Cli, action: &NetworkAction) -> Result<View> {
36    let mut client = crate::capabilities::connect(cli)?;
37    let host = client.host().to_string();
38    let channel = client.dbus_open(NM_NAME)?;
39    let mut ctx = CapabilityContext {
40        client: &mut client,
41        channel: &channel,
42        host: &host,
43    };
44    match action {
45        NetworkAction::List { all } => reads::list(&mut ctx, *all),
46        NetworkAction::Show { device } => reads::show(&mut ctx, device),
47    }
48}