use std::fs;
use std::io::{self, Write as _};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use hmac::{Hmac, Mac};
use sha2::{Digest, Sha256};
use tinystore::pipe::Prove;
const PROTOCOL: u64 = 1;
pub(crate) struct Serve {
pub(crate) instance: [u8; 16],
pub(crate) secret: [u8; 32],
pub(crate) endpoints: Vec<String>,
pub(crate) sidecar: bool,
}
impl Serve {
pub(crate) fn new(endpoints: Vec<String>, sidecar: bool) -> io::Result<Serve> {
let (mut instance, mut secret) = ([0; 16], [0; 32]);
getrandom::fill(&mut instance).and_then(|()| getrandom::fill(&mut secret)).map_err(io::Error::other)?;
Ok(Serve { instance, secret, endpoints, sidecar })
}
fn json(&self) -> String {
let mut published = serde_json::json!({
"protocol": PROTOCOL,
"server": env!("CARGO_PKG_VERSION"),
"pid": std::process::id(),
"instance": base64url(&self.instance),
"secret": base64url(&self.secret),
"endpoints": self.endpoints,
});
if self.sidecar {
published["sidecar"] = serde_json::Value::Bool(true);
}
published.to_string()
}
pub(crate) fn prove(&self) -> Prove {
let secret = self.secret;
Arc::new(move |challenge: &[u8]| {
let mut mac = Hmac::<Sha256>::new_from_slice(&secret).expect("HMAC takes a key of any length");
mac.update(challenge);
mac.finalize().into_bytes().to_vec()
})
}
}
pub(crate) fn server_dir(store: &Path) -> io::Result<PathBuf> {
let dir = store.join("server");
fs::create_dir_all(&dir)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(&dir, fs::Permissions::from_mode(0o700)).map_err(|error| {
io::Error::new(error.kind(), format!("{}: making it the user's alone: {error}", dir.display()))
})?;
}
#[cfg(windows)]
crate::owner_only::protect_dir(&dir)?;
Ok(dir)
}
pub(crate) fn publish(server: &Path, serve: &Serve) -> io::Result<()> {
let written = server.join(format!("SERVE.{}.tmp", hex(&serve.instance[..4])));
let mut file = create_private(&written)?;
file.write_all(serve.json().as_bytes())?;
drop(file);
patiently(|| fs::rename(&written, server.join("SERVE")))
}
fn create_private(path: &Path) -> io::Result<fs::File> {
match fs::remove_file(path) {
Err(error) if error.kind() != io::ErrorKind::NotFound => return Err(error),
_ => {}
}
#[cfg(unix)]
let file = {
use std::os::unix::fs::OpenOptionsExt;
fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(path)?
};
#[cfg(windows)]
let file = crate::owner_only::create_new(path)?;
Ok(file)
}
pub(crate) fn unpublish(server: &Path) -> io::Result<()> {
match patiently(|| fs::remove_file(server.join("SERVE"))) {
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
done => done,
}
}
fn patiently(change: impl Fn() -> io::Result<()>) -> io::Result<()> {
let mut pause = Duration::from_millis(1);
for _ in 0..7 {
match change() {
Err(error) if error.kind() == io::ErrorKind::PermissionDenied => thread::sleep(pause),
done => return done,
}
pause = (pause * 2).min(Duration::from_millis(64));
}
change()
}
pub(crate) fn hash(store: &Path) -> String {
hex(&Sha256::digest(store.to_string_lossy().as_bytes())[..8])
}
#[cfg(unix)]
pub(crate) fn socket_path(store: &Path, server: &Path) -> io::Result<PathBuf> {
let fits = if cfg!(target_os = "linux") { 108 } else { 104 };
let inside = server.join("tinystore.sock");
if inside.as_os_str().len() < fits {
return Ok(inside);
}
let runtime = std::env::var_os("XDG_RUNTIME_DIR").map_or_else(std::env::temp_dir, PathBuf::from);
let own = runtime.join(format!("tinystore-{}", hash(store)));
{
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
fs::DirBuilder::new().recursive(true).mode(0o700).create(&own)?;
fs::set_permissions(&own, fs::Permissions::from_mode(0o700))?;
}
Ok(own.join("tinystore.sock"))
}
#[cfg(windows)]
pub(crate) fn pipe_name(store: &Path) -> String {
format!("tinystore-{}", hash(store))
}
#[cfg(windows)]
pub(crate) fn pipe_instance(name: &str, first: bool) -> io::Result<tokio::net::windows::named_pipe::NamedPipeServer> {
let mut options = tokio::net::windows::named_pipe::ServerOptions::new();
options.first_pipe_instance(first).reject_remote_clients(true);
crate::owner_only::create_pipe(&options, name)
}
pub(crate) fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
pub(crate) fn base64url(bytes: &[u8]) -> String {
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
let mut text = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let joined =
chunk.iter().enumerate().fold(0u32, |joined, (at, byte)| joined | u32::from(*byte) << (16 - 8 * at));
for at in 0..=chunk.len() {
text.push(char::from(ALPHABET[(joined >> (18 - 6 * at) & 63) as usize]));
}
}
text
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn random_bytes_are_spelled_as_serve_spells_them() {
assert_eq!(base64url(b"foobar"), "Zm9vYmFy");
assert_eq!(base64url(b"fo"), "Zm8");
assert_eq!(base64url(&[0xfb, 0xff]), "-_8");
assert_eq!(hex(&[0x0f, 0xa0]), "0fa0");
}
#[test]
fn the_proof_is_the_hmac_of_the_challenge() {
let serve = Serve { instance: [0; 16], secret: [7; 32], endpoints: vec!["pipe:x".to_owned()], sidecar: true };
let mut mac = Hmac::<Sha256>::new_from_slice(&[7; 32]).unwrap();
mac.update(b"0123456789abcdef");
assert_eq!(serve.prove()(b"0123456789abcdef"), mac.finalize().into_bytes().to_vec());
let published: serde_json::Value = serde_json::from_str(&serve.json()).unwrap();
assert_eq!((published["protocol"].as_u64(), published["sidecar"].as_bool()), (Some(1), Some(true)));
assert_eq!(published["secret"].as_str().unwrap().len(), 43, "32 bytes, unpadded");
}
#[cfg(unix)]
#[test]
fn server_and_serve_are_the_users_alone_whatever_the_store_lets_others_do() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o777)).unwrap();
let server = server_dir(dir.path()).unwrap();
fs::write(server.join(format!("SERVE.{}.tmp", hex(&[0; 4]))), "left by a server that died").unwrap();
let serve = Serve { instance: [0; 16], secret: [7; 32], endpoints: vec![], sidecar: false };
publish(&server, &serve).unwrap();
let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode(&server), 0o700);
assert_eq!(mode(&server.join("SERVE")), 0o600);
}
}