use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use super::snapshot::DIRECTORY;
use crate::login::host::{self, Result};
use crate::store::fileio;
const FILE: &str = "journal.json";
const FORMAT: u32 = 1;
const LIMIT: u64 = 64 * 1024 * 1024;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct Unit {
pub kind: String,
pub name: String,
pub sha256: String,
pub bytes: u64,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct Left {
pub kind: String,
pub name: String,
pub reason: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct Opened {
pub id: String,
pub transfer_url: String,
pub expires_at: Option<String>,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub(crate) enum Stage {
Begin,
Transfer,
Commit,
Committed,
Succeeded,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct Journal {
pub journal_format: u32,
pub host: String,
pub import_endpoint: String,
pub account: String,
pub credential: String,
pub snapshot: String,
pub begun_at: String,
pub personal: Option<String>,
pub units: Vec<Unit>,
pub local: Vec<Left>,
pub session: Option<Opened>,
pub omitted: Vec<Left>,
pub refused: Vec<Left>,
pub stage: Stage,
#[serde(default)]
pub targets: Vec<super::session::Target>,
}
impl Journal {
pub(crate) fn committed(&self) -> Vec<&Unit> {
let left = |unit: &Unit| {
self.omitted
.iter()
.chain(&self.refused)
.any(|l| l.kind == unit.kind && l.name == unit.name)
};
self.units.iter().filter(|unit| !left(unit)).collect()
}
pub(crate) fn host_omitted(&self) -> Vec<&Left> {
self.omitted.iter().chain(&self.refused).collect()
}
fn validate(&self) -> Result<()> {
if self.journal_format != FORMAT {
return Err("unsupported login import journal".into());
}
host::endpoint(&self.host, &self.import_endpoint, false)?;
if let Some(session) = &self.session {
host::endpoint(&self.host, &session.transfer_url, false)?;
if !id_ok(&session.id) {
return Err("invalid login import journal".into());
}
}
Ok(())
}
}
pub(crate) fn id_ok(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 128
&& id
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
}
fn path(home: &Path) -> PathBuf {
home.join(DIRECTORY).join(FILE)
}
pub(crate) fn read(home: &Path) -> Result<Option<Journal>> {
let path = path(home);
let mut file = match fs::File::open(&path) {
Ok(file) => file,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(_) => return Err("cannot read the login import journal".into()),
};
private(&path)?;
let mut text = String::new();
file.by_ref()
.take(LIMIT + 1)
.read_to_string(&mut text)
.map_err(|_| "cannot read the login import journal")?;
if text.len() as u64 > LIMIT {
return Err("the login import journal is too large".into());
}
let journal: Journal =
serde_json::from_str(&text).map_err(|_| "invalid login import journal")?;
journal.validate()?;
Ok(Some(journal))
}
pub(crate) fn write(home: &Path, journal: &Journal) -> Result<()> {
journal.validate()?;
let text = serde_json::to_string_pretty(journal).map_err(|_| "cannot encode the journal")?;
fileio::atomic_write(&path(home), &text, true)
.map_err(|_| "cannot write the login import journal".into())
}
#[cfg(unix)]
fn private(path: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
for each in [path, path.parent().unwrap_or(path)] {
let meta = fs::symlink_metadata(each).map_err(|_| "cannot inspect the import journal")?;
if meta.file_type().is_symlink() || meta.permissions().mode() & 0o077 != 0 {
return Err("the login import journal must be private to its owner (0700/0600)".into());
}
}
Ok(())
}
#[cfg(not(unix))]
fn private(_: &Path) -> Result<()> {
Ok(())
}