use std::path::Path;
use std::sync::Arc;
use anyhow::{Context, anyhow};
use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use second_brain_core::keys::{
DeviceRoute, RecoveryPhrase, every_held_epoch, found_identity, load_device_keys,
};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_sync::dial;
use second_brain_sync::keyring::{Joined, Keyring, REMOVED, take_admission};
use second_brain_sync::mdns;
use second_brain_sync::pairing::{Admission, JoinRequest, KeyRequest};
use second_brain_sync::pairing::{HostOutcome, JoinCode, PAIR_PORT, PairingHost, Taken, join};
use second_brain_sync::ssh::{device_alias, known_hosts_path_in, pin_alias, pin_host_keys};
use serde::{Deserialize, Serialize};
use zeroize::Zeroize;
use super::{bad_request, error_response, internal_error};
use crate::device_access::DeviceAccess;
use crate::pairing::{advertise_route, local_addresses};
use crate::{AppState, identity_report};
#[derive(Default, Deserialize)]
pub(super) struct StartRequest {
port: Option<u16>,
#[serde(default)]
restore_keys: bool,
}
#[derive(Serialize)]
struct FoundedIdentity {
identity: String,
phrase: String,
fingerprint: String,
}
impl Drop for FoundedIdentity {
fn drop(&mut self) {
self.phrase.zeroize();
}
}
#[derive(Serialize)]
struct Started {
#[serde(flatten)]
status: StatusView,
addresses: Vec<String>,
port: u16,
default_port: bool,
founded: Option<FoundedIdentity>,
}
#[derive(Serialize)]
struct DeviceView {
machine_id: String,
name: String,
}
#[derive(Serialize)]
struct StatusView {
state: &'static str,
code: Option<String>,
invite: Option<String>,
wrong_tries: u32,
retired: u32,
expires_in_secs: u64,
device: Option<DeviceView>,
last_error: Option<String>,
}
impl Drop for StatusView {
fn drop(&mut self) {
self.code.zeroize();
self.invite.zeroize();
}
}
impl StatusView {
fn none() -> Self {
Self {
state: "none",
code: None,
invite: None,
wrong_tries: 0,
retired: 0,
expires_in_secs: 0,
device: None,
last_error: None,
}
}
fn of(host: &PairingHost) -> Self {
let status = host.status();
let (state, device) = match &status.outcome {
HostOutcome::Waiting => ("waiting", None),
HostOutcome::Paired { machine_id, name } => (
"paired",
Some(DeviceView {
machine_id: machine_id.clone(),
name: name.clone(),
}),
),
HostOutcome::HandedOver { name } => (
"handed_over",
Some(DeviceView {
machine_id: String::new(),
name: name.clone(),
}),
),
HostOutcome::Expired => ("expired", None),
HostOutcome::Closed => ("closed", None),
};
let waiting = status.outcome == HostOutcome::Waiting;
Self {
state,
code: waiting.then(|| status.code.to_string()),
invite: waiting.then(|| status.invite.to_string()),
wrong_tries: status.wrong_tries,
retired: status.retired,
expires_in_secs: status.remaining.as_secs(),
device,
last_error: status.last_error,
}
}
}
#[derive(Debug)]
struct Refused(String);
impl std::fmt::Display for Refused {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for Refused {}
fn start_key_handover(state: &Arc<AppState>, port: Option<u16>) -> anyhow::Result<Started> {
let device = load_device_keys(&state.data_dir)?
.context("this device has no device key yet; restart the daemon to create one")?;
if every_held_epoch(&state.sqlite, &device)?.is_empty() {
return Err(Refused(
"this device holds no key of any sync identity, so it has nothing to hand over"
.to_string(),
)
.into());
}
let listener = state.pairing.reserve(port)?;
let name = state.machine.name.clone();
let addresses = local_addresses(&name);
let hand_over = {
let store = state.sqlite.clone();
let data_dir = state.data_dir.clone();
Box::new(move |request: &KeyRequest| {
Keyring::load(&store, &data_dir)?.hand_over_keys(request, &name)
})
};
let port = state
.pairing
.start_key_handover(listener, addresses.clone(), hand_over)?;
let status = state
.pairing
.with_host(|host| host.map(StatusView::of))
.ok_or_else(|| anyhow!("the pairing listener closed as it opened"))?;
Ok(Started {
status,
addresses,
port,
default_port: port == PAIR_PORT,
founded: None,
})
}
fn start(state: &Arc<AppState>, port: Option<u16>) -> anyhow::Result<Started> {
let listener = state.pairing.reserve(port)?;
let now = state.clock.wall_micros();
let name = state.machine.name.clone();
let keyring = Keyring::load(&state.sqlite, &state.data_dir)?;
if keyring.identity().is_some() && keyring.is_removed()? {
return Err(Refused(REMOVED.to_string()).into());
}
let founded = if keyring.identity().is_none() {
let device = load_device_keys(&state.data_dir)?
.context("this device has no device key yet; restart the daemon to create one")?;
let phrase = RecoveryPhrase::generate()?;
let founded = found_identity(&state.sqlite, &device, &name, &[phrase.recipient()?], now)?;
state.sqlite.mark_recovery_offered(now)?;
if let Err(e) = state.bootstrap.cancel(&state.sqlite) {
tracing::warn!("cannot end this device's wait to join an identity: {e:#}");
}
identity_report::report(&state.sqlite, &state.data_dir, &state.health);
tracing::info!(
"founded sync identity {} to pair from",
founded.tag.as_str()
);
Some(FoundedIdentity {
identity: founded.tag.as_str().to_string(),
phrase: String::from(phrase.expose_text().as_str()),
fingerprint: phrase.fingerprint()?,
})
} else {
None
};
let addresses = local_addresses(&name);
let ssh = state.pairing.local_ssh();
advertise_route(&state.sqlite, &state.pairing, &name, &state.data_dir);
let admit = {
let store = state.sqlite.clone();
let data_dir = state.data_dir.clone();
let access = state.device_access.clone();
let addresses = addresses.clone();
Box::new(move |request: &JoinRequest| {
let admission = Keyring::load(&store, &data_dir)?.admit(
request,
&name,
ssh.clone(),
addresses.clone(),
)?;
let host_keys = request.ssh.as_ref().map(|s| s.host_keys.clone());
let route = DeviceRoute {
machine_id: request.machine_id.clone(),
addresses: request.addresses.clone(),
ssh_login: request.ssh.as_ref().and_then(|s| s.login.clone()),
ssh_port: request.ssh.as_ref().map(|s| s.port),
};
let failed = reach_paired(
&store,
&data_dir,
&access,
route,
&host_keys.unwrap_or_default(),
);
if !failed.is_empty() {
tracing::warn!(
"paired {} but could not finish setting it up: {}",
request.machine_id,
failed.join("; ")
);
}
Ok(admission)
})
};
let port = state.pairing.start(listener, addresses.clone(), admit)?;
let status = state
.pairing
.with_host(|host| host.map(StatusView::of))
.ok_or_else(|| anyhow!("the pairing listener closed as it opened"))?;
Ok(Started {
status,
addresses,
port,
default_port: port == PAIR_PORT,
founded,
})
}
pub(super) async fn pair_start(
State(state): State<Arc<AppState>>,
body: Option<Json<StartRequest>>,
) -> Response {
let body = body.map(|Json(b)| b).unwrap_or_default();
let (port, restore_keys) = (body.port, body.restore_keys);
let started = tokio::task::spawn_blocking(move || {
if restore_keys {
start_key_handover(&state, port)
} else {
start(&state, port)
}
});
match started.await {
Ok(Ok(started)) => Json(started).into_response(),
Ok(Err(e)) if e.downcast_ref::<Refused>().is_some() => {
error_response(StatusCode::CONFLICT, format!("{e:#}"))
}
Ok(Err(e)) => internal_error(format!("{e:#}")),
Err(e) => internal_error(e),
}
}
pub(super) async fn pair_status(State(state): State<Arc<AppState>>) -> Response {
let status = state
.pairing
.with_host(|host| host.map_or_else(StatusView::none, StatusView::of));
Json(status).into_response()
}
pub(super) async fn pair_close(State(state): State<Arc<AppState>>) -> Response {
Json(serde_json::json!({"closed": state.pairing.close()})).into_response()
}
pub(super) async fn pair_stop_waiting(State(state): State<Arc<AppState>>) -> Response {
match tokio::task::spawn_blocking(move || state.bootstrap.cancel(&state.sqlite)).await {
Ok(Ok(cancelled)) => Json(serde_json::json!({"cancelled": cancelled})).into_response(),
Ok(Err(e)) => internal_error(format!("{e:#}")),
Err(e) => internal_error(e),
}
}
#[derive(Deserialize)]
pub(super) struct JoinBody {
code: String,
host: Option<String>,
}
impl Drop for JoinBody {
fn drop(&mut self) {
self.code.zeroize();
}
}
#[derive(Serialize)]
struct JoinedView {
identity: String,
host: DeviceView,
epochs: usize,
moved: bool,
devices: usize,
known_hosts: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
warning: Option<String>,
}
fn joined_view(
joined: &Joined,
admission_host: DeviceView,
pinned: Vec<String>,
warning: Option<String>,
) -> JoinedView {
JoinedView {
identity: joined.identity.as_str().to_string(),
host: admission_host,
epochs: joined.epochs.len(),
moved: joined.moved,
devices: joined.devices,
known_hosts: pinned,
warning,
}
}
fn join_host(
state: &Arc<AppState>,
code: &JoinCode,
addresses: &[(String, u16)],
) -> anyhow::Result<JoinedView> {
let request = Keyring::load(&state.sqlite, &state.data_dir)?.join_request(
&state.machine.name,
state.pairing.local_ssh(),
local_addresses(&state.machine.name),
)?;
let bootstraps = !state.sqlite.holds_replicated_rows()?;
let marked = bootstraps && !state.bootstrap.is_pending();
if marked {
state.bootstrap.mark_pending(&state.sqlite)?;
}
let admitted: std::cell::RefCell<Option<(String, String)>> = std::cell::RefCell::new(None);
let result = join(addresses, code, &request, |admission, dialed| {
let joined = take_admission(
&state.sqlite,
&state.data_dir,
admission,
state.clock.wall_micros(),
)?;
*admitted.borrow_mut() = Some((admission.machine_id.clone(), admission.name.clone()));
let follow_up = after_join(state, admission, dialed);
Ok((joined, follow_up))
});
match admitted.into_inner() {
Some((host_id, host_name)) => {
if state.bootstrap.is_pending() && !dials_device(state, &host_id) {
tracing::warn!(
"{host_name} runs no sshd this device can reach, so its snapshot never \
comes; ingesting without it"
);
if let Err(e) = state.bootstrap.cancel(&state.sqlite) {
tracing::warn!("cannot end the wait for a bootstrap: {e:#}");
}
}
}
None if marked && result.is_err() => {
if let Err(e) = state.bootstrap.cancel(&state.sqlite) {
tracing::warn!("cannot clear the bootstrap a failed join marked: {e:#}");
}
}
None => {}
}
let Taken {
taken: (joined, follow_up),
admission,
unacked,
} = result?;
identity_report::report(&state.sqlite, &state.data_dir, &state.health);
tracing::info!(
"paired into identity {} through {} ({})",
joined.identity.as_str(),
admission.name,
admission.machine_id
);
let warning = join_warning(&admission.name, &follow_up, unacked.as_deref());
if let Some(warning) = &warning {
tracing::warn!("{warning}");
}
let pinned = follow_up.pinned;
Ok(joined_view(
&joined,
DeviceView {
machine_id: admission.machine_id.clone(),
name: admission.name.clone(),
},
pinned,
warning,
))
}
fn dials_device(state: &Arc<AppState>, machine_id: &str) -> bool {
match dial::device_dials(&state.sqlite) {
Ok(dials) => dials.iter().any(|d| d.key == machine_id),
Err(e) => {
tracing::warn!("cannot read the devices to sync with: {e:#}");
true
}
}
}
fn join_warning(host: &str, follow_up: &FollowUp, unacked: Option<&str>) -> Option<String> {
let mut problems = Vec::new();
if let Some(e) = unacked {
problems.push(format!(
"{host} was not told the join succeeded ({e}), so it may show the pairing as failed"
));
}
if !follow_up.failed.is_empty() {
problems.push(format!(
"setting up {host} did not finish: {}",
follow_up.failed.join("; ")
));
}
if follow_up.pin_failed {
problems.push(format!(
"the host keys were not pinned, so ssh will ask you to trust {host} the first time \
it connects"
));
}
(!problems.is_empty()).then(|| format!("the join is done, but {}", problems.join("; ")))
}
struct FollowUp {
pinned: Vec<String>,
pin_failed: bool,
failed: Vec<String>,
}
fn after_join(state: &Arc<AppState>, admission: &Admission, dialed: &str) -> FollowUp {
let mut failed: Vec<String> = Vec::new();
let members: Vec<String> = admission
.keys
.devices
.iter()
.map(|row| row.record.machine_id.clone())
.collect();
if let Err(e) = state.sqlite.seed_floor_neighbours(&members) {
failed.push(format!("seeding floor neighbours: {e:#}"));
}
let mut route_addresses = vec![dialed.to_string()];
route_addresses.extend(admission.addresses.iter().cloned());
let route = DeviceRoute {
machine_id: admission.machine_id.clone(),
addresses: route_addresses,
ssh_login: admission.ssh.as_ref().and_then(|s| s.login.clone()),
ssh_port: admission.ssh.as_ref().map(|s| s.port),
};
let host_keys = admission
.ssh
.as_ref()
.map(|s| s.host_keys.clone())
.unwrap_or_default();
failed.extend(reach_paired(
&state.sqlite,
&state.data_dir,
&state.device_access,
route,
&host_keys,
));
let mut pinned = Vec::new();
let mut pin_failed = false;
if let Some(ssh) = &admission.ssh {
let mut hosts = vec![dialed.to_string()];
for address in &admission.addresses {
if !hosts.contains(address) {
hosts.push(address.clone());
}
}
match pin_host_keys(
&known_hosts_path_in(&state.data_dir),
&hosts,
ssh.port,
&ssh.host_keys,
) {
Ok(written) => pinned = written,
Err(e) => {
pin_failed = true;
failed.push(format!(
"pinning its host keys in {}: {e:#}",
known_hosts_path_in(&state.data_dir).display()
));
}
}
}
if !failed.is_empty() {
tracing::warn!(
"joined through {} but could not finish setting it up: {}",
admission.machine_id,
failed.join("; ")
);
}
FollowUp {
pinned,
pin_failed,
failed,
}
}
fn reach_paired(
sqlite: &SqliteStore,
data_dir: &Path,
access: &DeviceAccess,
route: DeviceRoute,
host_keys: &[String],
) -> Vec<String> {
let mut failed = Vec::new();
let alias = device_alias(&route.machine_id);
if let Err(e) = sqlite.set_device_route(route) {
failed.push(format!("storing its route: {e:#}"));
}
if !host_keys.is_empty()
&& let Err(e) = pin_alias(&known_hosts_path_in(data_dir), &alias, host_keys)
{
failed.push(format!("pinning its host keys: {e:#}"));
}
if let Err(e) = access.reconcile(sqlite, data_dir) {
failed.push(format!("updating ssh access: {e:#}"));
}
access.kick();
failed
}
pub(super) async fn pair_join(
State(state): State<Arc<AppState>>,
Json(body): Json<JoinBody>,
) -> Response {
let code = match JoinCode::parse(&body.code) {
Ok(code) => code,
Err(e) => return bad_request(format!("{e:#}")),
};
let addresses = match code.addresses(body.host.as_deref()) {
Ok(addresses) => addresses,
Err(e) => return bad_request(format!("{e:#}")),
};
let joined = tokio::task::spawn_blocking(move || {
let addresses = if addresses.is_empty() {
pairing_devices_found(&state)
} else {
addresses
};
if addresses.is_empty() {
return Ok(None);
}
join_host(&state, &code, &addresses).map(Some)
})
.await;
match joined {
Ok(Ok(Some(joined))) => Json(joined).into_response(),
Ok(Ok(None)) => bad_request(
"no pairing device answered on this network; name it with --host, as `sb pair` \
shows on that device",
),
Ok(Err(e)) => error_response(StatusCode::CONFLICT, format!("{e:#}")),
Err(e) => internal_error(e),
}
}
fn pairing_devices_found(state: &AppState) -> Vec<(String, u16)> {
let Some(net) = state.pairing.discovery() else {
return Vec::new();
};
let found = match mdns::browse(net, mdns::PAIR_SERVICE, mdns::LOOKUP_WINDOW) {
Ok(found) => found,
Err(e) => {
tracing::warn!("cannot look for a pairing device on the network: {e:#}");
return Vec::new();
}
};
let own = state.sqlite.machine_id();
let mut addresses = Vec::new();
for listener in found {
let address = (listener.address.to_string(), listener.port);
if listener.get(mdns::TXT_ID) != Some(own) && !addresses.contains(&address) {
addresses.push(address);
}
}
addresses
}
#[cfg(test)]
mod tests {
use super::*;
fn follow_up(failed: &[&str], pin_failed: bool) -> FollowUp {
FollowUp {
pinned: Vec::new(),
pin_failed,
failed: failed.iter().map(|s| s.to_string()).collect(),
}
}
#[test]
fn a_join_that_stands_warns_about_each_step_that_did_not_finish() {
assert_eq!(join_warning("desk", &follow_up(&[], false), None), None);
let unacked = join_warning("desk", &follow_up(&[], false), Some("broken pipe")).unwrap();
assert!(unacked.starts_with("the join is done, but "), "{unacked}");
assert!(
unacked.contains("desk was not told the join succeeded (broken pipe)"),
"{unacked}"
);
assert!(!unacked.contains("pinned"), "{unacked}");
let unpinned = join_warning(
"desk",
&follow_up(&["pinning its host keys in /d/known_hosts: denied"], true),
None,
)
.unwrap();
assert!(unpinned.contains("/d/known_hosts: denied"), "{unpinned}");
assert!(unpinned.contains("host keys were not pinned"), "{unpinned}");
assert!(
unpinned.contains("ssh will ask you to trust desk"),
"{unpinned}"
);
assert!(!unpinned.contains("was not told"), "{unpinned}");
}
}