Skip to main content

coqui_access_acl/
inherit.rs

1//! Copied inheritance: what a new item takes from its container, as Windows and NFSv4 do.
2
3use alloc::vec::Vec;
4
5use coqui_access::Subject;
6
7use crate::entry::{Acl, Entry, Inherit};
8use crate::principal::Principal;
9
10/// What is made inside a container: a leaf, or a container of its own.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum Child {
13    /// A leaf: a file, a document.
14    Item,
15    /// Something that holds items: a directory, a folder.
16    Container,
17}
18
19impl Acl {
20    /// The ACL a child of an item with this ACL gets: `explicit`'s own entries first, then
21    /// the copies this ACL passes down, marked `inherited`, in `explicit`'s mode. A
22    /// `protected` `explicit` takes nothing and comes back as it is; entries `explicit`
23    /// had inherited before are replaced.
24    ///
25    /// The copies follow Windows' table (`items` is OI, `containers` CI):
26    ///
27    /// | parent entry | to an item | to a container |
28    /// | --- | --- | --- |
29    /// | `items` | applies, no flags | inherit-only, keeps `items` |
30    /// | `containers` | nothing | applies, keeps `containers` |
31    /// | both | applies, no flags | applies, keeps both |
32    /// | with `no_propagate` | applies, no flags (if `items`) | applies with no flags (if `containers`), else nothing |
33    ///
34    /// A parent entry that is `inherit_only` is copied the same way; the copy applies.
35    /// With a `creator`, an `Owner` entry (Windows CREATOR OWNER) is copied as an entry for
36    /// the creator, and a container also keeps an inherit-only `Owner` entry to pass on.
37    /// Nothing is walked at check time: store the result.
38    pub fn inherited_by(&self, child: Child, explicit: &Acl, creator: Option<&Subject>) -> Acl {
39        if explicit.protected {
40            return explicit.clone();
41        }
42        let mut entries: Vec<Entry> = explicit
43            .entries
44            .iter()
45            .filter(|entry| !entry.inherited)
46            .cloned()
47            .collect();
48        for entry in &self.entries {
49            let Some(flags) = passed(entry.inherit, child) else {
50                continue;
51            };
52            let copy = |principal: Principal, inherit: Inherit| Entry {
53                principal,
54                inherit,
55                inherited: true,
56                ..entry.clone()
57            };
58            match (&entry.principal, creator) {
59                (Principal::Owner, Some(creator)) => {
60                    if !flags.inherit_only {
61                        entries.push(copy(creator.clone().into(), Inherit::default()));
62                    }
63                    if flags.passes() {
64                        entries.push(copy(Principal::Owner, flags.inherit_only()));
65                    }
66                }
67                (principal, _) => entries.push(copy(principal.clone(), flags)),
68            }
69        }
70        Acl {
71            entries,
72            protected: false,
73            mode: explicit.mode,
74        }
75    }
76}
77
78/// The flags of the copy `child` gets of an entry with `inherit`, or `None` for no copy.
79fn passed(inherit: Inherit, child: Child) -> Option<Inherit> {
80    match child {
81        Child::Item => inherit.items.then(Inherit::default),
82        Child::Container if inherit.no_propagate => inherit.containers.then(Inherit::default),
83        Child::Container if inherit.containers => Some(Inherit::new(inherit.items, true)),
84        Child::Container if inherit.items => Some(Inherit::new(true, false).inherit_only()),
85        Child::Container => None,
86    }
87}