diff --git a/crates/bakashrot-config/example.toml b/crates/bakashrot-config/example.toml index c22f07a..10f4bc6 100644 --- a/crates/bakashrot-config/example.toml +++ b/crates/bakashrot-config/example.toml @@ -25,6 +25,11 @@ database = "bakashrot.sqlite" # second or so. ("off" isn't allowed.) synchronous = "full" +# Replication cut partway through an attachment resumes once the peer has +# read back and hashed what it already has, in 30 s at most: up to about +# 4 GB received on a hard disk, 15 GB on a SATA SSD, 30-60 GB on NVMe +# (estimates). Past that, resuming fails at every retry: keep attachments +# well under it (more than a few GB is a lot for this anyway). max_attachment_size = "1G" # bytes, or K M G T (1024-based); 0: none retention_interval = "1m" # how often old messages are deleted @@ -42,7 +47,7 @@ logs = "30d" push_to = [] # dialed and sent to: ["10.0.0.2:7000", "server-2.lan:7000"] accept_from = [] # taken from: ["10.0.0.2", "server-2.lan"], or ["*"] retry_delay = "10s" -sync_interval = "5s" +sync_interval = "5s" # keep it under 5 min: a link idle that long is closed batch_size = 1000 # messages per packet, 1-10000 # What certificates with a role may do. A certificate gets everything its diff --git a/crates/bakashrot-server/src/lib.rs b/crates/bakashrot-server/src/lib.rs index d769ca5..e648106 100644 --- a/crates/bakashrot-server/src/lib.rs +++ b/crates/bakashrot-server/src/lib.rs @@ -72,6 +72,7 @@ impl Server { /// Reads the server's certificates and CRL, opens its database, starts /// its writer. Errors say which file and why. pub fn start(config: Config) -> Result { + check_clock(SystemTime::now())?; let server = &config.server; let ca = read(&server.ca)?; let certificate = read(&server.certificate)?; @@ -307,6 +308,15 @@ fn load_revocations(path: Option<&Path>, ca: &str) -> Result Result<(), String> { + match now.duration_since(UNIX_EPOCH) { + Ok(_) => Ok(()), + Err(_) => Err("the system's clock is before 1970: set it right first".into()), + } +} + /// Now, in ms since 1970 UTC: when a message is pushed. fn now() -> u64 { SystemTime::now() @@ -319,3 +329,17 @@ fn stamp(path: &Path) -> Option<(SystemTime, u64)> { let metadata = std::fs::metadata(path).ok()?; Some((metadata.modified().ok()?, metadata.len())) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_clock_before_1970_is_refused() { + assert!(check_clock(UNIX_EPOCH).is_ok()); + assert!(check_clock(SystemTime::now()).is_ok()); + let before = UNIX_EPOCH - Duration::from_secs(1); + let error = check_clock(before).unwrap_err(); + assert!(error.contains("before 1970"), "{error}"); + } +} diff --git a/crates/bakashrot-tools/src/bin/bakashrot-cert.rs b/crates/bakashrot-tools/src/bin/bakashrot-cert.rs index a32b964..8505920 100644 --- a/crates/bakashrot-tools/src/bin/bakashrot-cert.rs +++ b/crates/bakashrot-tools/src/bin/bakashrot-cert.rs @@ -99,8 +99,12 @@ fn run(command: Command) -> Result<(), String> { match command { Command::Ca { name, days, out } => { let ca = Ca::create(&name, days).map_err(|e| e.to_string())?; - write_new(&out.join("ca.crt"), &ca.certificate, false)?; - write_new(&out.join("ca.key"), &ca.key, true)?; + write_pair( + &out.join("ca.crt"), + &ca.certificate, + &out.join("ca.key"), + &ca.key, + )?; } Command::Issue { name, @@ -118,8 +122,12 @@ fn run(command: Command) -> Result<(), String> { .issue(&identity, usage, days) .map_err(|e| e.to_string())?; let file = file.unwrap_or(name); - write_new(&out.join(format!("{file}.crt")), &issued.certificate, false)?; - write_new(&out.join(format!("{file}.key")), &issued.key, true)?; + write_pair( + &out.join(format!("{file}.crt")), + &issued.certificate, + &out.join(format!("{file}.key")), + &issued.key, + )?; } Command::Revoke { certificates, @@ -159,8 +167,28 @@ fn write_new(path: &Path, text: &str, private: bool) -> Result<(), String> { .open(path) .map_err(|error| format!("{}: {error}", path.display()))?; file.write_all(text.as_bytes()) - .map_err(|error| format!("{}: {error}", path.display()))?; - println!("Wrote {}", path.display()); + .map_err(|error| format!("{}: {error}", path.display())) +} + +/// Writes a certificate and its key, both or neither: nothing is written +/// when either is there already, the certificate is removed again when +/// its key can't be written. +fn write_pair(certificate: &Path, pem: &str, key: &Path, key_pem: &str) -> Result<(), String> { + for path in [certificate, key] { + if path.exists() { + return Err(format!( + "{}: already there, nothing written", + path.display() + )); + } + } + write_new(certificate, pem, false)?; + if let Err(error) = write_new(key, key_pem, true) { + let _ = fs::remove_file(certificate); + return Err(format!("{error}, nothing written")); + } + println!("Wrote {}", certificate.display()); + println!("Wrote {}", key.display()); Ok(()) } diff --git a/crates/bakashrot-tools/tests/cert.rs b/crates/bakashrot-tools/tests/cert.rs index 745f51a..116cf95 100644 --- a/crates/bakashrot-tools/tests/cert.rs +++ b/crates/bakashrot-tools/tests/cert.rs @@ -121,6 +121,20 @@ fn nothing_is_overwritten_or_mixed_up() { assert!(!ok, "{text}"); assert_eq!(read(&folder, "ca.key"), key, "the CA's key is still there"); + // Half of a pair there already: refused, the other half not written. + let half = folder.join("half"); + fs::create_dir_all(&half).unwrap(); + fs::write(half.join("ca.key"), "kept").unwrap(); + let (ok, text) = cert(&half, &["ca", "--name", "Half"]); + assert!(!ok && text.contains("nothing written"), "{text}"); + assert!(!half.join("ca.crt").exists()); + assert_eq!(read(&half, "ca.key"), "kept"); + fs::write(folder.join("porch.key"), "kept").unwrap(); + let (ok, text) = cert(&folder, &["issue", "--name", "porch"]); + assert!(!ok && text.contains("nothing written"), "{text}"); + assert!(!folder.join("porch.crt").exists()); + assert_eq!(read(&folder, "porch.key"), "kept"); + assert!(cert(&folder, &["issue", "--name", "kitchen"]).0); assert!( !cert(&folder, &["issue", "--name", "kitchen"]).0, diff --git a/docs/certificates.md b/docs/certificates.md index a184489..ca5aa62 100644 --- a/docs/certificates.md +++ b/docs/certificates.md @@ -106,8 +106,10 @@ bakashrot-cert show kitchen.crt `--ca` (the folder of `ca.crt` / `ca.key`, `.` by default), `--out` (where to write), `--days`, `--file` (the files' name when it isn't the node's: -another certificate of the same name, `--file server-1b`). Files are PEM. Nothing is ever written over an -existing certificate or key; keys are readable by their owner only on +another certificate of the same name, `--file server-1b`). Files are +PEM. Nothing is ever written over an existing certificate or key, and a +certificate and its key are written both or neither (one of them there +already: nothing written); keys are readable by their owner only on Unix. `revoke` only takes certificates the CA issued. ## Tests diff --git a/docs/known-issues.md b/docs/known-issues.md index 55a7209..cfb2a3f 100644 --- a/docs/known-issues.md +++ b/docs/known-issues.md @@ -58,14 +58,20 @@ Reproduce one before fixing it, and move it up (or drop it) once known. - **`sync_interval` over 5 min.** The server dialed closes the idle link (`IDLE_TIMEOUT`), so every check-in fails once ("closed the link", warned), then the link is dialed again after `retry_delay`. Nothing - in the config caps `sync_interval`. Fix: cap it under the idle - timeout, or have an idle link ask its `SyncState` more often. + in the config caps `sync_interval`. Left as it is (the owner, + 2026-11-16): replicating every 5 min or more isn't a real use, the + example config says to keep it under. Fix if needed: cap it under the + idle timeout, or have an idle link ask its `SyncState` more often. - **Resuming a big replicated attachment.** Before answering `Continue`, the receiver reads back and hashes every part it already has (`accept_from.rs`, `replicate_attachment`, `hash_parts`), while the - sender waits 30 s for that answer (`push_to.rs`, `ANSWER_TIMEOUT`). On - a slow disk, hundreds of parts may take longer: the link drops, and - the same happens at every retry. Fix: a longer wait for that answer, + sender waits 30 s for that answer (`push_to.rs`, `ANSWER_TIMEOUT`). + Past what the disk reads and hashes in 30 s (estimated: about 4 GB + on a hard disk, 15 GB on a SATA SSD, 30-60 GB on NVMe), the link + drops, and the same happens at every retry. Left as it is (the + owner, 2026-11-16): attachments over a few GB are excessive for this; + the example config says so by `max_attachment_size` (1 GiB by + default). Fix if needed: a wait scaled to the parts already received, or the hash's state kept with the attachment. - **Two peers sending the same attachment at once.** Both can be answered `Continue` for the same `from_part` (the check and the first @@ -127,11 +133,14 @@ Reproduce one before fixing it, and move it up (or drop it) once known. its buffers), any amount pushed: an allowed client can fill the disk or the server's memory. Fine for the threat model; first thing to add if it changes (a cap on connections, a quota per node). -- **Small ones:** the client's connect timeout is per socket - operation, not for the whole connection; `bakashrot-cert ca` writes - `ca.crt` before failing when only `ca.key` is there already; a - server whose clock is before 1970 stamps `pushed_at` 0, which - readers refuse when decoding. +- **A clock set before 1970 while running.** The server refuses to + start with one (`check_clock`, `bakashrot-server/src/lib.rs`), but + isn't checked again: set back that far afterwards, pushes are stamped + `pushed_at` 0 (`now()`), which readers refuse when decoding. Fix if + ever needed: refuse pushes (`ServerError`) while the clock is before + 1970. +- **A small one:** the client's connect timeout is per socket + operation, not for the whole connection. ## Ideas for later diff --git a/docs/server.md b/docs/server.md index cd46b24..98c0066 100644 --- a/docs/server.md +++ b/docs/server.md @@ -16,8 +16,9 @@ bakashrot-server server.toml ``` The config (`config.md`) gives where to listen, the certificates, the CRL, -the database, the topics and the roles. Decided 2026-11-14 -(`../revamp.md`, "Operations"): +the database, the topics and the roles. A system clock before 1970 +is refused at start (messages are stamped with it, `pushed_at`). +Decided 2026-11-14 (`../revamp.md`, "Operations"): - **Stopping:** SIGTERM or SIGINT on Linux; Ctrl+C, Ctrl+Break or its console closed on Windows. It stops taking connections and writes