use std::{
ffi::{CStr, OsStr},
fs::{File, OpenOptions},
mem::MaybeUninit,
os::unix::{
ffi::OsStrExt,
fs::{MetadataExt, OpenOptionsExt},
},
path::{Path, PathBuf},
};
use anyhow::Context;
pub(crate) fn account_lock_path(file_name: &str, what: &str) -> anyhow::Result<PathBuf> {
let user = unsafe { libc::geteuid() };
lock_path_in(&account_home(user, what)?, user, file_name, what)
}
pub(super) fn lock_path_in(
home: &Path,
user: libc::uid_t,
file_name: &str,
what: &str,
) -> anyhow::Result<PathBuf> {
let home = home
.canonicalize()
.context("could not resolve the account home")?;
let metadata = home.metadata()?;
if !metadata.is_dir() || metadata.uid() != user || metadata.mode() & 0o022 != 0 {
anyhow::bail!(
"{what} lock home {} must be a directory owned by uid {user} with no group or other write access",
home.display()
);
}
Ok(home.join(file_name))
}
fn account_home(user: libc::uid_t, what: &str) -> anyhow::Result<PathBuf> {
let suggested = unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) };
let mut buffer = vec![
0_u8;
usize::try_from(suggested)
.unwrap_or_default()
.max(std::mem::size_of::<libc::passwd>())
];
loop {
let mut record = MaybeUninit::<libc::passwd>::uninit();
let mut found = std::ptr::null_mut();
let status = unsafe {
libc::getpwuid_r(
user,
record.as_mut_ptr(),
buffer.as_mut_ptr().cast(),
buffer.len(),
&mut found,
)
};
if status == libc::ERANGE {
buffer.try_reserve(buffer.len())?;
buffer.resize(buffer.capacity(), 0);
continue;
}
if status != 0 {
return Err(std::io::Error::from_raw_os_error(status)).with_context(|| {
format!("could not look up the account home for the {what} lock")
});
}
if found.is_null() {
anyhow::bail!("no OS account for uid {user}; cannot locate the {what} lock");
}
let record = unsafe { record.assume_init() };
if record.pw_dir.is_null() {
anyhow::bail!("OS account for uid {user} has no home directory");
}
let home = unsafe { CStr::from_ptr(record.pw_dir) };
let home = PathBuf::from(OsStr::from_bytes(home.to_bytes()));
if !home.is_absolute() {
anyhow::bail!("OS account for uid {user} has a non-absolute home directory");
}
return Ok(home);
}
}
pub(crate) fn lock_account_file(path: &Path, what: &str) -> anyhow::Result<File> {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.mode(0o600)
.custom_flags(libc::O_NOFOLLOW)
.open(path)
.with_context(|| format!("could not open {what} mutation lock {}", path.display()))?;
let metadata = file.metadata()?;
let user = unsafe { libc::geteuid() };
if !metadata.is_file() || metadata.uid() != user || metadata.mode() & 0o077 != 0 {
anyhow::bail!(
"{what} mutation lock {} must be a regular file owned by uid {user} with no group or other access",
path.display()
);
}
fs2::FileExt::lock_exclusive(&file)
.with_context(|| format!("could not lock {what} mutations at {}", path.display()))?;
Ok(file)
}
#[cfg(test)]
#[path = "account_tests.rs"]
mod tests;