use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt as _;
use std::path::Path;
use std::ptr;
use windows_sys::Win32::Foundation::{ERROR_SUCCESS, LocalFree};
use windows_sys::Win32::Security::Authorization::{
ConvertStringSecurityDescriptorToSecurityDescriptorW, GetNamedSecurityInfoW, SE_FILE_OBJECT,
SetNamedSecurityInfoW,
};
use windows_sys::Win32::Security::{
DACL_SECURITY_INFORMATION, GROUP_SECURITY_INFORMATION, OWNER_SECURITY_INFORMATION,
PROTECTED_DACL_SECURITY_INFORMATION, PSECURITY_DESCRIPTOR, PSID,
};
mod identity;
use identity::{current_user, sid_string};
pub fn set_owner_only(path: &Path, inheritable: bool) -> std::io::Result<()> {
install_owner_only(path, inheritable).map_err(|()| {
std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"cannot set owner-only ACL",
)
})
}
pub fn verify_owner_only(path: &Path, protected: bool, inherited: bool) -> std::io::Result<()> {
check_owner_only(path, protected, inherited).map_err(|()| {
std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"file ACL is not owner-only",
)
})
}
fn install_owner_only(path: &Path, inheritable: bool) -> Result<(), ()> {
let mut descriptor: PSECURITY_DESCRIPTOR = ptr::null_mut();
let user = current_user()?;
let sid = sid_string(user.sid())?;
let flags = if inheritable { "OICI" } else { "" };
let sddl: Vec<u16> = OsStr::new(&format!("D:P(A;{flags};FA;;;{sid})"))
.encode_wide()
.chain(Some(0))
.collect();
let ok = unsafe {
ConvertStringSecurityDescriptorToSecurityDescriptorW(
sddl.as_ptr(),
1,
&mut descriptor,
ptr::null_mut(),
)
};
if ok == 0 {
return Err(());
}
let result = apply_descriptor(path, descriptor);
unsafe { LocalFree(descriptor.cast()) };
result
}
fn apply_descriptor(path: &Path, descriptor: PSECURITY_DESCRIPTOR) -> Result<(), ()> {
use windows_sys::Win32::Security::GetSecurityDescriptorDacl;
let mut present = 0;
let mut defaulted = 0;
let mut dacl = ptr::null_mut();
let ok =
unsafe { GetSecurityDescriptorDacl(descriptor, &mut present, &mut dacl, &mut defaulted) };
if ok == 0 || present == 0 {
return Err(());
}
let mut wide = wide_path(path);
let status = unsafe {
SetNamedSecurityInfoW(
wide.as_mut_ptr(),
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
ptr::null_mut(),
ptr::null_mut(),
dacl,
ptr::null_mut(),
)
};
(status == ERROR_SUCCESS).then_some(()).ok_or(())
}
fn check_owner_only(path: &Path, protected: bool, inherited: bool) -> Result<(), ()> {
let mut owner = ptr::null_mut();
let mut group = ptr::null_mut();
let mut dacl = ptr::null_mut();
let mut descriptor = ptr::null_mut();
let mut wide = wide_path(path);
let status = unsafe {
GetNamedSecurityInfoW(
wide.as_mut_ptr(),
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
&mut owner,
&mut group,
&mut dacl,
ptr::null_mut(),
&mut descriptor,
)
};
if status != ERROR_SUCCESS || owner.is_null() || dacl.is_null() {
return Err(());
}
let result = descriptor_is_private(descriptor, dacl, owner, protected, inherited);
unsafe { LocalFree(descriptor.cast()) };
result
}
fn descriptor_is_private(
descriptor: PSECURITY_DESCRIPTOR,
dacl: *mut windows_sys::Win32::Security::ACL,
owner: PSID,
protected: bool,
inherited: bool,
) -> Result<(), ()> {
use windows_sys::Win32::Security::{
ACCESS_ALLOWED_ACE, EqualSid, GetAce, GetSecurityDescriptorControl, SE_DACL_PROTECTED,
};
const INHERITED_ACE_FLAG: u8 = 0x10;
let mut control = 0;
let mut revision = 0;
let control_ok =
unsafe { GetSecurityDescriptorControl(descriptor, &mut control, &mut revision) } != 0;
if !control_ok || (control & SE_DACL_PROTECTED != 0) != protected {
return Err(());
}
let user = current_user()?;
if unsafe { EqualSid(owner, user.sid()) } == 0 {
return Err(());
}
let count = unsafe { (*dacl).AceCount };
if count != 1 {
return Err(());
}
let mut ace = ptr::null_mut();
if unsafe { GetAce(dacl, 0, &mut ace) } == 0 {
return Err(());
}
let allowed = ace.cast::<ACCESS_ALLOWED_ACE>();
if unsafe { (*allowed).Header.AceType } != 0 {
return Err(());
}
if (unsafe { (*allowed).Header.AceFlags } & INHERITED_ACE_FLAG != 0) != inherited {
return Err(());
}
let sid = unsafe { ptr::addr_of_mut!((*allowed).SidStart).cast() };
(unsafe { EqualSid(user.sid(), sid) } != 0)
.then_some(())
.ok_or(())
}
fn wide_path(path: &Path) -> Vec<u16> {
path.as_os_str().encode_wide().chain(Some(0)).collect()
}