diff --git a/btd/src/route.rs b/btd/src/route.rs index 6919b1c7..8518a02f 100644 --- a/btd/src/route.rs +++ b/btd/src/route.rs @@ -204,6 +204,13 @@ fn permits(call: &proto::Call) -> bool { // Unlike `resetToGolden` it discards nothing. SystemReboot => true, + // ssh on wifi until the next reboot. The shipping image listens on the head's USB-C link + // only (microduck_yocto#110), so without this a shell over wifi means opening the robot. + // BLE is where it belongs for the same reason as `net.connect`: whoever asks is in the + // room, holding a phone that proved the setup code, and the login it opens still needs + // the operator password on top. + SystemOpenWifiSsh => true, + // ── the gamepad ───────────────────────────────────────────────────── // // Pairing a controller from the phone, which is where it belongs: whoever is holding the @@ -641,6 +648,7 @@ mod tests { proto::method::NET_FORGET, proto::method::SYSTEM_SET_NAME, proto::method::SYSTEM_REBOOT, + proto::method::SYSTEM_OPEN_WIFI_SSH, // Bonding a gamepad, which afterwards can enable the walking policy. Allowed for // the same reason as provisioning: it takes a pad held in pairing mode next to the // robot, so BLE's physical-presence claim (§4.2) is not being stretched — and the @@ -707,6 +715,7 @@ mod tests { name: "duck".into(), }), proto::Call::SystemReboot, + proto::Call::SystemOpenWifiSsh, ] { assert_eq!( upstream_for(&call), diff --git a/configd/src/lib.rs b/configd/src/lib.rs index e3c2c416..1cd35941 100644 --- a/configd/src/lib.rs +++ b/configd/src/lib.rs @@ -35,5 +35,6 @@ pub mod net; pub mod nm; pub mod pad; pub mod power; +pub mod ssh; pub mod store; pub mod units; diff --git a/configd/src/main.rs b/configd/src/main.rs index d3b4e4db..821b259e 100644 --- a/configd/src/main.rs +++ b/configd/src/main.rs @@ -620,6 +620,29 @@ async fn dispatch( ) } + proto::Call::SystemOpenWifiSsh => { + let switch = std::path::Path::new(configd::ssh::SWITCH); + let installed = std::path::Path::new(configd::ssh::INSTALLED); + match configd::ssh::open(switch, installed) { + Ok(result) => { + // Warn, not info: a root-capable login just became reachable from the + // network, and the journal is where anyone auditing that will look. + tracing::warn!( + switched = result.switched, + "ssh on wifi requested; open until reboot" + ); + proto::Response::ok(Some(id), &result) + } + Err(e) => { + tracing::warn!(error = %e, "could not open ssh on wifi"); + proto::Response::err( + Some(id), + proto::Error::new(proto::code::INTERNAL_ERROR, e.to_string()), + ) + } + } + } + // `update.*` is `updaterd`'s and `robot.*` is `robotd`'s. A client reaching here aimed at // the wrong socket, so say that rather than report a generic failure. other => proto::Response::err( diff --git a/configd/src/ssh.rs b/configd/src/ssh.rs new file mode 100644 index 00000000..ca72b38b --- /dev/null +++ b/configd/src/ssh.rs @@ -0,0 +1,65 @@ +//! `system.openWifiSsh`: ssh on wifi until the next reboot. +//! +//! The shipping image answers ssh on the head's USB-C link only (microduck_yocto#110). Its +//! `microduck-ssh-access` package watches for [`SWITCH`] and, when it appears, installs a `/run` +//! drop-in that reopens port 22 on every interface. All this does is create that file: the image +//! owns the mechanism, and a reboot closes it again because `/run` is a tmpfs. +//! +//! The request reaches here only from an authenticated caller — over BLE that is the secure +//! session the setup code opens — so it is gated exactly as joining a network is. + +use std::io; +use std::path::Path; + +use duck_ipc_proto as proto; + +/// The file the image's path unit watches. +pub const SWITCH: &str = "/run/microduck-ssh-wifi"; + +/// What the image installs alongside the path unit. Its absence means an image with no switch to +/// press: the dev image, which listens on wifi already, or a board with no USB link. +pub const INSTALLED: &str = "/usr/share/microduck-ssh-access/30-wifi-until-reboot.conf"; + +/// Press the switch, if this image has one. +/// +/// Creating a file that is already there is success: the caller asked for ssh to be open, and it +/// is. Reporting `switched = false` there would read as "this image cannot", which is untrue. +pub fn open(switch: &Path, installed: &Path) -> io::Result { + if !installed.exists() { + return Ok(proto::OpenWifiSshResult { switched: false }); + } + std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .write(true) + .open(switch)?; + Ok(proto::OpenWifiSshResult { switched: true }) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// An image without the package must not grow a stray file in `/run` that nothing watches, and + /// must say it did nothing so a client does not promise the user a shell. + #[test] + fn an_image_without_the_switch_is_left_alone() { + let dir = tempfile::tempdir().unwrap(); + let switch = dir.path().join("switch"); + let result = open(&switch, &dir.path().join("absent")).unwrap(); + assert!(!result.switched); + assert!(!switch.exists()); + } + + /// Pressing it twice is the normal case — a phone that retried — and must not fail. + #[test] + fn pressing_twice_is_success() { + let dir = tempfile::tempdir().unwrap(); + let installed = dir.path().join("installed"); + std::fs::write(&installed, "").unwrap(); + let switch = dir.path().join("switch"); + assert!(open(&switch, &installed).unwrap().switched); + assert!(open(&switch, &installed).unwrap().switched); + assert!(switch.exists()); + } +} diff --git a/duck-ipc-proto/src/lib.rs b/duck-ipc-proto/src/lib.rs index 7d3b2621..01b9021d 100644 --- a/duck-ipc-proto/src/lib.rs +++ b/duck-ipc-proto/src/lib.rs @@ -536,7 +536,13 @@ pub const JSONRPC_VERSION: &str = "2.0"; /// [`RobotState::impact_guard`]: what ROBOTIS's impact guard firmware is doing on each servo, for /// recording a robot that runs it. Additive: absent from an older `robotd`, and from a robot whose /// servos have no guard. -pub const API_VERSION: u32 = 53; +/// +/// # v54 — ssh on wifi, on request +/// +/// [`method::SYSTEM_OPEN_WIFI_SSH`]: a shipping image answers ssh on the head's USB-C link only +/// (microduck_yocto#110), and this is the request that image was built to accept instead of +/// opening the robot. An older `configd` refuses it by name. +pub const API_VERSION: u32 = 54; /// The observation width every policy this robot family runs is built against. /// @@ -1040,6 +1046,9 @@ pub mod method { pub const SYSTEM_SET_NAME: &str = "system.setName"; /// Reboot, cleanly, through systemd. pub const SYSTEM_REBOOT: &str = "system.reboot"; + /// Open ssh on wifi until the next reboot. A shipping image listens on the head's USB-C link + /// only, so without this a shell over wifi means opening the robot. + pub const SYSTEM_OPEN_WIFI_SSH: &str = "system.openWifiSsh"; /// The Bluetooth pairing PIN. Never reachable over Bluetooth itself. pub const SYSTEM_PAIRING_PIN: &str = "system.pairingPin"; /// Set the Bluetooth pairing PIN. @@ -1336,6 +1345,8 @@ pub enum Call { SystemLogs(LogsParams), SystemSetName(SetNameParams), SystemReboot, + /// See [`method::SYSTEM_OPEN_WIFI_SSH`]. + SystemOpenWifiSsh, /// Read the pairing PIN. /// /// Exists so `btd` can answer a BLE passkey request without owning config. It must never be @@ -1494,6 +1505,7 @@ impl Call { Call::SystemLogs(_) => method::SYSTEM_LOGS, Call::SystemSetName(_) => method::SYSTEM_SET_NAME, Call::SystemReboot => method::SYSTEM_REBOOT, + Call::SystemOpenWifiSsh => method::SYSTEM_OPEN_WIFI_SSH, Call::SystemPairingPin => method::SYSTEM_PAIRING_PIN, Call::SystemSetPairingPin(_) => method::SYSTEM_SET_PAIRING_PIN, Call::SystemAuthenticate(_) => method::SYSTEM_AUTHENTICATE, @@ -1529,6 +1541,9 @@ impl Call { | Call::NetForget(_) | Call::SystemSetName(_) | Call::SystemReboot + // A root shell reachable from the network is at least as consequential as a + // reboot, even with the password still required. + | Call::SystemOpenWifiSsh | Call::SystemSetPairingPin(_) // Powering the machine off is at least as disruptive as rebooting it. | Call::RobotShutdown @@ -1695,6 +1710,7 @@ impl Call { | Call::SystemLogs(_) | Call::SystemSetName(_) | Call::SystemReboot + | Call::SystemOpenWifiSsh | Call::SystemPairingPin | Call::SystemSetPairingPin(_) | Call::PadStatus @@ -1843,6 +1859,7 @@ impl Call { | Call::SystemInfo | Call::SystemServices | Call::SystemReboot + | Call::SystemOpenWifiSsh | Call::SystemPairingPin | Call::PadStatus | Call::PadBindings @@ -1950,6 +1967,7 @@ impl Call { method::SYSTEM_LOGS => Call::SystemLogs(decode(params)?), method::SYSTEM_SET_NAME => Call::SystemSetName(decode(params)?), method::SYSTEM_REBOOT => Call::SystemReboot, + method::SYSTEM_OPEN_WIFI_SSH => Call::SystemOpenWifiSsh, method::SYSTEM_PAIRING_PIN => Call::SystemPairingPin, method::SYSTEM_SET_PAIRING_PIN => Call::SystemSetPairingPin(decode(params)?), method::SYSTEM_AUTHENTICATE => Call::SystemAuthenticate(decode(params)?), @@ -2156,6 +2174,7 @@ pub mod test_support { name: "duck-01".into(), }), Call::SystemReboot, + Call::SystemOpenWifiSsh, Call::SystemPairingPin, Call::SystemSetPairingPin(SetPairingPinParams { pin: "042042".into(), @@ -4899,6 +4918,17 @@ pub struct RebootResult { pub in_seconds: u64, } +/// Answer to [`Call::SystemOpenWifiSsh`]. +/// +/// The opening lasts until the next reboot and no longer: the image's switch is a file in `/run`, +/// a tmpfs. There is no call to close it early, because a reboot already does. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct OpenWifiSshResult { + /// False on an image with no such switch (the dev image, which listens on wifi already, or a + /// board with no USB link). Nothing was done, and ssh on wifi is whatever the image makes it. + pub switched: bool, +} + // ── pad.* results ──────────────────────────────────────────────────────────── /// A gamepad this robot knows about. @@ -6474,7 +6504,7 @@ mod tests { fn every_call_covers_every_variant() { assert_eq!( every_call().len(), - 78, + 79, "a Call variant was added or removed — update every_call() and this count" ); } @@ -6706,6 +6736,7 @@ mod tests { method::NET_FORGET, method::SYSTEM_SET_NAME, method::SYSTEM_REBOOT, + method::SYSTEM_OPEN_WIFI_SSH, method::SYSTEM_SET_PAIRING_PIN, // Bonding a pad decides what may drive this robot. `pad.status` must stay off this // list: reading which pads are paired is exactly the kind of inspection support diff --git a/mediad/src/route.rs b/mediad/src/route.rs index 1d77331e..a9dd9c85 100644 --- a/mediad/src/route.rs +++ b/mediad/src/route.rs @@ -275,6 +275,10 @@ fn permits(call: &proto::Call) -> bool { // Drops this session, and unlike an update leaves nothing mid-transition: the robot comes // back and the client reconnects. It is what you offer a confused robot. SystemReboot => true, + // Refused: a WebRTC peer need not be in the room — the relay exists so it can be + // anywhere — and opening a root-capable login to the network is a decision for whoever + // holds the robot. BLE carries it, behind the setup code. + SystemOpenWifiSsh => false, // Read-only network state. Useful for showing a remote operator why the link is poor. NetStatus | NetScan => true, PadStatus | PadForget(_) => true, diff --git a/updater/src/ipc.rs b/updater/src/ipc.rs index de2fa55b..f8af36d0 100644 --- a/updater/src/ipc.rs +++ b/updater/src/ipc.rs @@ -918,6 +918,7 @@ impl Server { | Call::SystemLogs(_) | Call::SystemSetName(_) | Call::SystemReboot + | Call::SystemOpenWifiSsh | Call::SystemPairingPin | Call::SystemSetPairingPin(_) // `pad.*` is `configd`'s for the same reason: pairing a gamepad is a root-only question