use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs::File;
use std::io;
use std::os::windows::ffi::OsStringExt;
use std::os::windows::fs::MetadataExt;
use std::os::windows::io::AsHandle;
use std::os::windows::io::BorrowedHandle;
use std::os::windows::io::OwnedHandle;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_DIRECTORY;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_REPARSE_POINT;
use windows_sys::Win32::Storage::FileSystem::FILE_LIST_DIRECTORY;
use windows_sys::Win32::Storage::FileSystem::FILE_READ_ATTRIBUTES;
use windows_sys::Win32::Storage::FileSystem::FILE_TRAVERSE;
use windows_sys::Win32::Storage::FileSystem::READ_CONTROL;
use windows_sys::Win32::Storage::FileSystem::WRITE_DAC;
use super::Leaf;
use super::nt;
use super::nt::Request;
use super::nt::security;
use super::nt::security::Derived;
use super::nt::security::Descriptor;
use super::nt::security::EffectiveToken;
use super::nt::security::ObjectKind;
use super::nt::security::Part;
use super::nt::security::SecurityDescriptor;
const ENTRY_ACCESS: u32 = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES;
const LISTING_ACCESS: u32 = FILE_LIST_DIRECTORY | FILE_TRAVERSE | FILE_READ_ATTRIBUTES;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EntryClass {
Tree,
Link,
Leaf,
}
impl EntryClass {
const fn of(attributes: u32) -> Self {
if attributes & FILE_ATTRIBUTE_DIRECTORY == 0 {
Self::Leaf
} else if attributes & FILE_ATTRIBUTE_REPARSE_POINT == 0 {
Self::Tree
} else {
Self::Link
}
}
const fn kind(self) -> ObjectKind {
match self {
Self::Tree | Self::Link => ObjectKind::Container,
Self::Leaf => ObjectKind::Leaf,
}
}
}
struct Entry {
handle: OwnedHandle,
class: EntryClass,
}
enum Computed<'a> {
Root(&'a Descriptor<'a>),
Derived(Descriptor<'static>),
}
impl<'a> Computed<'a> {
const fn get(&self) -> &Descriptor<'a> {
match self {
Self::Root(root) => root,
Self::Derived(derived) => derived,
}
}
}
struct Level<'d> {
listing: OwnedHandle,
apply: Option<OwnedHandle>,
computed: Computed<'d>,
names: Vec<OsString>,
}
fn leaf_of(name: &OsStr) -> io::Result<Leaf<'_>> {
Leaf::of_name(name).ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))
}
fn list(listing: BorrowedHandle<'_>) -> io::Result<Vec<OsString>> {
Ok(nt::list_names(listing)?
.into_iter()
.map(|units| OsString::from_wide(&units))
.collect())
}
fn open_entry(parent: BorrowedHandle<'_>, name: &OsStr) -> io::Result<Entry> {
let handle = nt::create_at(parent, leaf_of(name)?, Request::open(ENTRY_ACCESS))?;
let file = File::from(handle);
let attributes = file.metadata()?.file_attributes();
Ok(Entry {
handle: OwnedHandle::from(file),
class: EntryClass::of(attributes),
})
}
pub(super) fn rewrite_children(
root: BorrowedHandle<'_>,
computed: &Descriptor<'_>,
token: &EffectiveToken,
) -> io::Result<()> {
let listing = nt::reopen_dir(root, LISTING_ACCESS)?;
let names = list(listing.as_handle())?;
let mut stack = vec![Level {
listing,
apply: None,
computed: Computed::Root(computed),
names,
}];
while let Some(level) = stack.last_mut() {
let Some(name) = level.names.pop() else {
if let Some(done) = stack.pop()
&& let Some(apply) = done.apply
{
security::apply_dacl(apply.as_handle(), done.computed.get())?;
}
continue;
};
let entry = open_entry(level.listing.as_handle(), &name)?;
let creator = SecurityDescriptor::read(entry.handle.as_handle(), Part::OwnerAndGroup)?;
let derived = Derived::new(level.computed.get(), &creator, entry.class.kind(), token)?;
let computed = Computed::Derived(Descriptor::Derived(derived));
if entry.class == EntryClass::Tree {
let listing = nt::reopen_dir(entry.handle.as_handle(), LISTING_ACCESS)?;
let names = list(listing.as_handle())?;
stack.push(Level {
listing,
apply: Some(entry.handle),
computed,
names,
});
} else {
security::apply_dacl(entry.handle.as_handle(), computed.get())?;
}
}
Ok(())
}