use std::io::Write as _;
use std::path::Path;
pub fn open_append(path: &Path) -> std::io::Result<std::io::BufWriter<std::fs::File>> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let file = private_log_options().append(true).open(path)?;
restrict_to_owner(&file)?;
Ok(std::io::BufWriter::new(file))
}
pub fn private_log_options() -> std::fs::OpenOptions {
let mut options = std::fs::OpenOptions::new();
options.create(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600).custom_flags(libc::O_NOFOLLOW);
}
options
}
pub fn restrict_to_owner(file: &std::fs::File) -> std::io::Result<()> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
if file
.set_permissions(std::fs::Permissions::from_mode(0o600))
.is_err()
&& file
.metadata()
.is_ok_and(|metadata| metadata.permissions().mode() & 0o077 != 0)
{
tracing::warn!(
"a private log could not be narrowed to 0600 and is still readable by group or \
other; this filesystem does not apply Unix mode bits"
);
}
}
#[cfg(not(unix))]
let _ = file;
Ok(())
}
pub fn flush_and_sync(writer: &mut std::io::BufWriter<std::fs::File>) -> std::io::Result<()> {
writer.flush()?;
writer.get_ref().sync_all()
}
#[cfg(test)]
mod tests {
use super::{flush_and_sync, open_append};
#[test]
fn flush_and_sync_writes_and_syncs() {
use std::io::Write as _;
let tmp = tempfile::tempdir().expect("tempdir");
let path = tmp.path().join("append.log");
{
let mut writer = open_append(&path).expect("open_append");
writeln!(writer, "line 1").expect("write");
flush_and_sync(&mut writer).expect("flush_and_sync");
writeln!(writer, "line 2").expect("write");
flush_and_sync(&mut writer).expect("flush_and_sync");
}
let content = std::fs::read_to_string(&path).expect("read");
assert_eq!(content, "line 1\nline 2\n");
}
#[cfg(unix)]
#[test]
fn append_logs_are_owner_only_and_not_opened_through_links() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().expect("tempdir");
let log = dir.path().join("audit.log");
drop(open_append(&log).expect("open"));
let mode = std::fs::metadata(&log).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
std::fs::set_permissions(&log, std::fs::Permissions::from_mode(0o644)).unwrap();
drop(open_append(&log).expect("reopen"));
let mode = std::fs::metadata(&log).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
let target = dir.path().join("elsewhere");
std::fs::write(&target, "").unwrap();
let link = dir.path().join("linked.log");
std::os::unix::fs::symlink(&target, &link).unwrap();
assert!(open_append(&link).is_err());
}
}